home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 4 / 4009 < prev    next >
Text File  |  1991-09-08  |  41KB  |  1,423 lines

  1. Newsgroups: alt.sources
  2. Path: wupost!zaphod.mps.ohio-state.edu!think.com!news.bbn.com!mips2!bubba!jtsillas
  3. From: jtsillas@bubba.ma30.bull.com (James Tsillas)
  4. Subject: mxgdb 1.0.3 (part 7/10)
  5. Organization: Bull HN, Worldwide Information Systems, Billerica, Mass., USA
  6. Distribution: alt
  7. Date: 6 Sep 91 14:35:32
  8. Message-ID: <JTSILLAS.91Sep6143532@bubba.ma30.bull.com>
  9. Sender: news@mips2.ma30.bull.com (Usenet News Manager)
  10.  
  11. ---- Cut Here and feed the following to sh ----
  12. #!/bin/sh
  13. # this is mxgdb.07 (part 7 of a multipart archive)
  14. # do not concatenate these parts, unpack them in order with /bin/sh
  15. # file mxgdb/regex.c continued
  16. #
  17. if test ! -r _shar_seq_.tmp; then
  18.     echo 'Please unpack part 1 first!'
  19.     exit 1
  20. fi
  21. (read Scheck
  22.  if test "$Scheck" != 7; then
  23.     echo Please unpack part "$Scheck" next!
  24.     exit 1
  25.  else
  26.     exit 0
  27.  fi
  28. ) < _shar_seq_.tmp || exit 1
  29. if test ! -f _shar_wnt_.tmp; then
  30.     echo 'x - still skipping mxgdb/regex.c'
  31. else
  32. echo 'x - continuing file mxgdb/regex.c'
  33. sed 's/^X//' << 'SHAR_EOF' >> 'mxgdb/regex.c' &&
  34. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  35. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  36. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  37. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  38. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  39. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  40. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  41. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  42. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  43. CORRECTION.
  44. X
  45. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  46. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  47. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  48. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  49. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  50. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  51. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  52. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  53. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  54. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  55. X
  56. X        GENERAL PUBLIC LICENSE TO COPY
  57. X
  58. X  1. You may copy and distribute verbatim copies of this source file
  59. as you receive it, in any medium, provided that you conspicuously and
  60. appropriately publish on each copy a valid copyright notice "Copyright
  61. (C) 1985 Free Software Foundation, Inc."; and include following the
  62. copyright notice a verbatim copy of the above disclaimer of warranty
  63. and of this License.  You may charge a distribution fee for the
  64. physical act of transferring a copy.
  65. X
  66. X  2. You may modify your copy or copies of this source file or
  67. any portion of it, and copy and distribute such modifications under
  68. the terms of Paragraph 1 above, provided that you also do the following:
  69. X
  70. X    a) cause the modified files to carry prominent notices stating
  71. X    that you changed the files and the date of any change; and
  72. X
  73. X    b) cause the whole of any work that you distribute or publish,
  74. X    that in whole or in part contains or is a derivative of this
  75. X    program or any part thereof, to be licensed at no charge to all
  76. X    third parties on terms identical to those contained in this
  77. X    License Agreement (except that you may choose to grant more extensive
  78. X    warranty protection to some or all third parties, at your option).
  79. X
  80. X    c) You may charge a distribution fee for the physical act of
  81. X    transferring a copy, and you may at your option offer warranty
  82. X    protection in exchange for a fee.
  83. X
  84. Mere aggregation of another unrelated program with this program (or its
  85. derivative) on a volume of a storage or distribution medium does not bring
  86. the other program under the scope of these terms.
  87. X
  88. X  3. You may copy and distribute this program (or a portion or derivative
  89. of it, under Paragraph 2) in object code or executable form under the terms
  90. of Paragraphs 1 and 2 above provided that you also do one of the following:
  91. X
  92. X    a) accompany it with the complete corresponding machine-readable
  93. X    source code, which must be distributed under the terms of
  94. X    Paragraphs 1 and 2 above; or,
  95. X
  96. X    b) accompany it with a written offer, valid for at least three
  97. X    years, to give any third party free (except for a nominal
  98. X    shipping charge) a complete machine-readable copy of the
  99. X    corresponding source code, to be distributed under the terms of
  100. X    Paragraphs 1 and 2 above; or,
  101. X
  102. X    c) accompany it with the information you received as to where the
  103. X    corresponding source code may be obtained.  (This alternative is
  104. X    allowed only for noncommercial distribution and only if you
  105. X    received the program in object code or executable form alone.)
  106. X
  107. For an executable file, complete source code means all the source code for
  108. all modules it contains; but, as a special exception, it need not include
  109. source code for modules which are standard libraries that accompany the
  110. operating system on which the executable file runs.
  111. X
  112. X  4. You may not copy, sublicense, distribute or transfer this program
  113. except as expressly provided under this License Agreement.  Any attempt
  114. otherwise to copy, sublicense, distribute or transfer this program is void and
  115. your rights to use the program under this License agreement shall be
  116. automatically terminated.  However, parties who have received computer
  117. software programs from you with this License Agreement will not have
  118. their licenses terminated so long as such parties remain in full compliance.
  119. X
  120. X  5. If you wish to incorporate parts of this program into other free
  121. programs whose distribution conditions are different, write to the Free
  122. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  123. worked out a simple rule that can be stated here, but we will often permit
  124. this.  We will be guided by the two goals of preserving the free status of
  125. all derivatives of our free software and of promoting the sharing and reuse of
  126. software.
  127. X
  128. X
  129. In other words, you are welcome to use, share and improve this program.
  130. You are forbidden to forbid anyone else to use, share and improve
  131. what you give them.   Help stamp out software-hoarding!  */
  132. X
  133. X
  134. /* To test, compile with -Dtest.
  135. X This Dtestable feature turns this into a self-contained program
  136. X which reads a pattern, describes how it compiles,
  137. X then reads a string and searches for it.  */
  138. X
  139. /* 
  140. X * Modification: alloca.h included for sparc machines
  141. X * By :         Po Cheung, po@cerc.utexas.edu
  142. X * Date :     July 27, 1990
  143. X */
  144. #ifdef sparc
  145. #include <alloca.h>
  146. #endif
  147. #define FAILURE_STACK   20000           /* max failure stack size */
  148. X
  149. #ifdef __GNUC__
  150. #define alloca __builtin_alloca
  151. #endif
  152. X
  153. #ifdef emacs
  154. X
  155. /* The `emacs' switch turns on certain special matching commands
  156. X that make sense only in emacs. */
  157. X
  158. #include "config.h"
  159. #include "lisp.h"
  160. #include "buffer.h"
  161. #include "syntax.h"
  162. X
  163. #else  /* not emacs */
  164. X
  165. /*
  166. X * Define the syntax stuff, so we can do the \<...\> things.
  167. X */
  168. X
  169. #ifndef Sword /* must be non-zero in some of the tests below... */
  170. #define Sword 1
  171. #endif
  172. X
  173. #define SYNTAX(c) re_syntax_table[c]
  174. X
  175. #ifdef SYNTAX_TABLE
  176. X
  177. char *re_syntax_table;
  178. X
  179. #else
  180. X
  181. static char re_syntax_table[256];
  182. X
  183. static void
  184. init_syntax_once ()
  185. {
  186. X   register int c;
  187. X   static int done = 0;
  188. X
  189. X   if (done)
  190. X     return;
  191. X
  192. X   bzero (re_syntax_table, sizeof re_syntax_table);
  193. X
  194. X   for (c = 'a'; c <= 'z'; c++)
  195. X     re_syntax_table[c] = Sword;
  196. X
  197. X   for (c = 'A'; c <= 'Z'; c++)
  198. X     re_syntax_table[c] = Sword;
  199. X
  200. X   for (c = '0'; c <= '9'; c++)
  201. X     re_syntax_table[c] = Sword;
  202. X
  203. X   done = 1;
  204. }
  205. X
  206. #endif /* SYNTAX_TABLE */
  207. #endif /* not emacs */
  208. X
  209. #include "regex.h"
  210. X
  211. /* Number of failure points to allocate space for initially,
  212. X when matching.  If this number is exceeded, more space is allocated,
  213. X so it is not a hard limit.  */
  214. X
  215. #ifndef NFAILURES
  216. #define NFAILURES 80
  217. #endif NFAILURES
  218. X
  219. /* width of a byte in bits */
  220. X
  221. #define BYTEWIDTH 8
  222. X
  223. #ifndef SIGN_EXTEND_CHAR
  224. #define SIGN_EXTEND_CHAR(x) (x)
  225. #endif
  226. X
  227. /* compile_pattern takes a regular-expression descriptor string in the 
  228. X  user's format and converts it into a buffer full of byte commands 
  229. X  for matching.
  230. X
  231. X  pattern   is the address of the pattern string
  232. X  size      is the length of it.
  233. X  bufp        is a  struct re_pattern_buffer *  which points to the info
  234. X        on where to store the byte commands.
  235. X        This structure contains a  char *  which points to the
  236. X        actual space, which should have been obtained with malloc.
  237. X        compile_pattern may use  realloc  to grow the buffer space.
  238. X
  239. X  The number of bytes of commands can be found out by looking in
  240. X  the  struct re_pattern_buffer  that bufp pointed to,
  241. X  after compile_pattern returns.
  242. */
  243. X
  244. #define PATPUSH(ch) (*b++ = (char) (ch))
  245. X
  246. #define PATFETCH(c) \
  247. X {if (p == pend) goto end_of_pattern; \
  248. X  c = * (unsigned char *) p++; \
  249. X  if (translate) c = translate[c]; }
  250. X
  251. #define PATFETCH_RAW(c) \
  252. X {if (p == pend) goto end_of_pattern; \
  253. X  c = * (unsigned char *) p++; }
  254. X
  255. #define PATUNFETCH p--
  256. X
  257. #define EXTEND_BUFFER \
  258. X  { char *old_buffer = bufp->buffer; \
  259. X    if (bufp->allocated == (1<<16)) goto too_big; \
  260. X    bufp->allocated *= 2; \
  261. X    if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \
  262. X    if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  263. X      goto memory_exhausted; \
  264. X    c = bufp->buffer - old_buffer; \
  265. X    b += c; \
  266. X    if (fixup_jump) \
  267. X      fixup_jump += c; \
  268. X    if (laststart) \
  269. X      laststart += c; \
  270. X    begalt += c; \
  271. X    if (pending_exact) \
  272. X      pending_exact += c; \
  273. X  }
  274. X
  275. static int store_jump (), insert_jump ();
  276. X
  277. char *
  278. re_compile_pattern (pattern, size, bufp)
  279. X     char *pattern;
  280. X     int size;
  281. X     struct re_pattern_buffer *bufp;
  282. {
  283. X  register char *b = bufp->buffer;
  284. X  register char *p = pattern;
  285. X  char *pend = pattern + size;
  286. X  register unsigned c, c1;
  287. X  char *p1;
  288. X  unsigned char *translate = (unsigned char *) bufp->translate;
  289. X
  290. X  /* address of the count-byte of the most recently inserted "exactn" command.
  291. X    This makes it possible to tell whether a new exact-match character
  292. X    can be added to that command or requires a new "exactn" command. */
  293. X     
  294. X  char *pending_exact = 0;
  295. X
  296. X  /* address of the place where a forward-jump should go
  297. X    to the end of the containing expression.
  298. X    Each alternative of an "or", except the last, ends with a forward-jump
  299. X    of this sort. */
  300. X
  301. X  char *fixup_jump = 0;
  302. X
  303. X  /* address of start of the most recently finished expression.
  304. X    This tells postfix * where to find the start of its operand. */
  305. X
  306. X  char *laststart = 0;
  307. X
  308. X  /* In processing a repeat, 1 means zero matches is allowed */
  309. X
  310. X  char zero_times_ok;
  311. X
  312. X  /* In processing a repeat, 1 means many matches is allowed */
  313. X
  314. X  char many_times_ok;
  315. X
  316. X  /* address of beginning of regexp, or inside of last \( */
  317. X
  318. X  char *begalt = b;
  319. X
  320. X  /* Stack of information saved by \( and restored by \).
  321. X     Four stack elements are pushed by each \(:
  322. X       First, the value of b.
  323. X       Second, the value of fixup_jump.
  324. X       Third, the value of regnum.
  325. X       Fourth, the value of begalt.  */
  326. X
  327. X  int stackb[40];
  328. X  int *stackp = stackb;
  329. X  int *stacke = stackb + 40;
  330. X  int *stackt;
  331. X
  332. X  /* Counts \('s as they are encountered.  Remembered for the matching \),
  333. X     where it becomes the "register number" to put in the stop_memory command */
  334. X
  335. X  int regnum = 1;
  336. X
  337. X  bufp->fastmap_accurate = 0;
  338. X
  339. #ifndef emacs
  340. #ifndef SYNTAX_TABLE
  341. X  /*
  342. X   * Initialize the syntax table.
  343. X   */
  344. X   init_syntax_once();
  345. #endif
  346. #endif
  347. X
  348. X  if (bufp->allocated == 0)
  349. X    {
  350. X      bufp->allocated = 28;
  351. X      if (bufp->buffer)
  352. X    /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  353. X    bufp->buffer = (char *) realloc (bufp->buffer, 28);
  354. X      else
  355. X    /* Caller did not allocate a buffer.  Do it for him */
  356. X    bufp->buffer = (char *) malloc (28);
  357. X      if (!bufp->buffer) goto memory_exhausted;
  358. X      begalt = b = bufp->buffer;
  359. X    }
  360. X
  361. X  while (p != pend)
  362. X    {
  363. X      if (b - bufp->buffer > bufp->allocated - 10)
  364. X    /* Note that EXTEND_BUFFER clobbers c */
  365. X    EXTEND_BUFFER;
  366. X
  367. X      PATFETCH (c);
  368. X
  369. X      switch (c)
  370. X    {
  371. X    case '$':
  372. X      /* $ means succeed if at end of line, but only in special contexts.
  373. X        If randonly in the middle of a pattern, it is a normal character. */
  374. X      if (p == pend || (*p == '\\' && (p[1] == ')' || p[1] == '|')))
  375. X        {
  376. X          PATPUSH (endline);
  377. X          break;
  378. X        }
  379. X      goto normal_char;
  380. X
  381. X    case '^':
  382. X      /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  383. X      if (laststart) goto normal_char;
  384. X      PATPUSH (begline);
  385. X      break;
  386. X
  387. X    case '*':
  388. X    case '+':
  389. X    case '?':
  390. X      /* If there is no previous pattern, char not special. */
  391. X      if (!laststart)
  392. X        goto normal_char;
  393. X      /* If there is a sequence of repetition chars,
  394. X         collapse it down to equivalent to just one.  */
  395. X      zero_times_ok = 0;
  396. X      many_times_ok = 0;
  397. X      while (1)
  398. X        {
  399. X          zero_times_ok |= c != '+';
  400. X          many_times_ok |= c != '?';
  401. X          if (p == pend)
  402. X        break;
  403. X          PATFETCH (c);
  404. X          if (!(c == '*' || c == '+' || c == '?'))
  405. X        {
  406. X          PATUNFETCH;
  407. X          break;
  408. X        }
  409. X        }
  410. X
  411. X      /* Now we know whether 0 matches is allowed,
  412. X         and whether 2 or more matches is allowed.  */
  413. X      if (many_times_ok)
  414. X        {
  415. X          /* If more than one repetition is allowed,
  416. X         put in a backward jump at the end.  */
  417. X          store_jump (b, maybe_finalize_jump, laststart - 3);
  418. X          b += 3;
  419. X        }
  420. X      insert_jump (on_failure_jump, laststart, b + 3, b);
  421. X      pending_exact = 0;
  422. X      b += 3;
  423. X      if (!zero_times_ok)
  424. X        {
  425. X          /* At least one repetition required: insert before the loop
  426. X         a skip over the initial on-failure-jump instruction */
  427. X          insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  428. X          b += 3;
  429. X        }
  430. X      break;
  431. X
  432. X    case '.':
  433. X      laststart = b;
  434. X      PATPUSH (anychar);
  435. X      break;
  436. X
  437. X    case '[':
  438. X      if (b - bufp->buffer
  439. X          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  440. X        /* Note that EXTEND_BUFFER clobbers c */
  441. X        EXTEND_BUFFER;
  442. X
  443. X      laststart = b;
  444. X      if (*p == '^')
  445. X        PATPUSH (charset_not), p++;
  446. X      else
  447. X        PATPUSH (charset);
  448. X      p1 = p;
  449. X
  450. X      PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  451. X      /* Clear the whole map */
  452. X      bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  453. X      /* Read in characters and ranges, setting map bits */
  454. X      while (1)
  455. X        {
  456. X          PATFETCH (c);
  457. X          if (c == ']' && p != p1 + 1) break;
  458. X          if (*p == '-')
  459. X        {
  460. X          PATFETCH (c1);
  461. X          PATFETCH (c1);
  462. X          while (c <= c1)
  463. X            b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  464. X        }
  465. X          else
  466. X        {
  467. X          b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  468. X        }
  469. X        }
  470. X      /* Discard any bitmap bytes that are all 0 at the end of the map.
  471. X         Decrement the map-length byte too. */
  472. X      while (b[-1] > 0 && b[b[-1] - 1] == 0)
  473. X        b[-1]--;
  474. X      b += b[-1];
  475. X      break;
  476. X
  477. X        case '\\':
  478. X      if (p == pend) goto invalid_pattern;
  479. X      PATFETCH_RAW (c);
  480. X      switch (c)
  481. X        {
  482. X        case '(':
  483. X          if (stackp == stacke) goto nesting_too_deep;
  484. X          if (regnum < RE_NREGS)
  485. X            {
  486. X          PATPUSH (start_memory);
  487. X          PATPUSH (regnum);
  488. X            }
  489. X          *stackp++ = b - bufp->buffer;
  490. X          *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  491. X          *stackp++ = regnum++;
  492. X          *stackp++ = begalt - bufp->buffer;
  493. X          fixup_jump = 0;
  494. X          laststart = 0;
  495. X          begalt = b;
  496. X          break;
  497. X
  498. X        case ')':
  499. X          if (stackp == stackb) goto unmatched_close;
  500. X          begalt = *--stackp + bufp->buffer;
  501. X          if (fixup_jump)
  502. X        store_jump (fixup_jump, jump, b);
  503. X          if (stackp[-1] < RE_NREGS)
  504. X        {
  505. X          PATPUSH (stop_memory);
  506. X          PATPUSH (stackp[-1]);
  507. X        }
  508. X          stackp -= 2;
  509. X          fixup_jump = 0;
  510. X          if (*stackp)
  511. X        fixup_jump = *stackp + bufp->buffer - 1;
  512. X          laststart = *--stackp + bufp->buffer;
  513. X          break;
  514. X
  515. X        case '|':
  516. X          insert_jump (on_failure_jump, begalt, b + 6, b);
  517. X          pending_exact = 0;
  518. X          b += 3;
  519. X          if (fixup_jump)
  520. X        store_jump (fixup_jump, jump, b);
  521. X          fixup_jump = b;
  522. X          b += 3;
  523. X          laststart = 0;
  524. X          begalt = b;
  525. X          break;
  526. X
  527. #ifdef emacs
  528. X        case '=':
  529. X          PATPUSH (at_dot);
  530. X          break;
  531. X
  532. X        case 's':    
  533. X          laststart = b;
  534. X          PATPUSH (syntaxspec);
  535. X          PATFETCH (c);
  536. X          PATPUSH (syntax_spec_code[c]);
  537. X          break;
  538. X
  539. X        case 'S':
  540. X          laststart = b;
  541. X          PATPUSH (notsyntaxspec);
  542. X          PATFETCH (c);
  543. X          PATPUSH (syntax_spec_code[c]);
  544. X          break;
  545. #endif emacs
  546. X
  547. X        case 'w':
  548. X          laststart = b;
  549. X          PATPUSH (wordchar);
  550. X          break;
  551. X
  552. X        case 'W':
  553. X          laststart = b;
  554. X          PATPUSH (notwordchar);
  555. X          break;
  556. X
  557. X        case '<':
  558. X          PATPUSH (wordbeg);
  559. X          break;
  560. X
  561. X        case '>':
  562. X          PATPUSH (wordend);
  563. X          break;
  564. X
  565. X        case 'b':
  566. X          PATPUSH (wordbound);
  567. X          break;
  568. X
  569. X        case 'B':
  570. X          PATPUSH (notwordbound);
  571. X          break;
  572. X
  573. X        case '`':
  574. X          PATPUSH (begbuf);
  575. X          break;
  576. X
  577. X        case '\'':
  578. X          PATPUSH (endbuf);
  579. X          break;
  580. X
  581. X        case '1':
  582. X        case '2':
  583. X        case '3':
  584. X        case '4':
  585. X        case '5':
  586. X        case '6':
  587. X        case '7':
  588. X        case '8':
  589. X        case '9':
  590. X          c1 = c - '0';
  591. X          if (c1 >= regnum)
  592. X        goto normal_char;
  593. X          for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  594. X         if (*stackt == c1)
  595. X          goto normal_char;
  596. X          laststart = b;
  597. X          PATPUSH (duplicate);
  598. X          PATPUSH (c1);
  599. X          break;
  600. X        default:
  601. X          /* You might think it wuld be useful for \ to mean
  602. X         not to translate; but if we don't translate it
  603. X         it will never match anything.  */
  604. X          if (translate) c = translate[c];
  605. X          goto normal_char;
  606. X        }
  607. X      break;
  608. X
  609. X    default:
  610. X    normal_char:
  611. X      if (!pending_exact || pending_exact + *pending_exact + 1 != b
  612. X          || *pending_exact == 0177 || *p == '*' || *p == '^'
  613. X          || *p == '+' || *p == '?')
  614. X        {
  615. X          laststart = b;
  616. X          PATPUSH (exactn);
  617. X          pending_exact = b;
  618. X          PATPUSH (0);
  619. X        }
  620. X      PATPUSH (c);
  621. X      (*pending_exact)++;
  622. X    }
  623. X    }
  624. X
  625. X  if (fixup_jump)
  626. X    store_jump (fixup_jump, jump, b);
  627. X
  628. X  if (stackp != stackb) goto unmatched_open;
  629. X
  630. X  bufp->used = b - bufp->buffer;
  631. X  return 0;
  632. X
  633. X invalid_pattern:
  634. X  return "Invalid regular expression";
  635. X
  636. X unmatched_open:
  637. X  return "Unmatched \\(";
  638. X
  639. X unmatched_close:
  640. X  return "Unmatched \\)";
  641. X
  642. X end_of_pattern:
  643. X  return "Premature end of regular expression";
  644. X
  645. X nesting_too_deep:
  646. X  return "Nesting too deep";
  647. X
  648. X too_big:
  649. X  return "Regular expression too big";
  650. X
  651. X memory_exhausted:
  652. X  return "Memory exhausted";
  653. }
  654. X
  655. /* Store where `from' points a jump operation to jump to where `to' points.
  656. X  `opcode' is the opcode to store. */
  657. X
  658. static int
  659. store_jump (from, opcode, to)
  660. X     char *from, *to;
  661. X     char opcode;
  662. {
  663. X  from[0] = opcode;
  664. X  from[1] = (to - (from + 3)) & 0377;
  665. X  from[2] = (to - (from + 3)) >> 8;
  666. }
  667. X
  668. /* Open up space at char FROM, and insert there a jump to TO.
  669. X   CURRENT_END gives te end of the storage no in use,
  670. X   so we know how much data to copy up.
  671. X   OP is the opcode of the jump to insert.
  672. X
  673. X   If you call this function, you must zero out pending_exact.  */
  674. X
  675. static int
  676. insert_jump (op, from, to, current_end)
  677. X     char op;
  678. X     char *from, *to, *current_end;
  679. {
  680. X  register char *pto = current_end + 3;
  681. X  register char *pfrom = current_end;
  682. X  while (pfrom != from)
  683. X    *--pto = *--pfrom;
  684. X  store_jump (from, op, to);
  685. }
  686. X
  687. /* Given a pattern, compute a fastmap from it.
  688. X The fastmap records which of the (1 << BYTEWIDTH) possible characters
  689. X can start a string that matches the pattern.
  690. X This fastmap is used by re_search to skip quickly over totally 
  691. X implausible text.
  692. X
  693. X The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  694. X as bufp->fastmap.
  695. X The other components of bufp describe the pattern to be used.  */
  696. X
  697. void
  698. re_compile_fastmap (bufp)
  699. X     struct re_pattern_buffer *bufp;
  700. {
  701. X  unsigned char *pattern = (unsigned char *) bufp->buffer;
  702. X  int size = bufp->used;
  703. X  register char *fastmap = bufp->fastmap;
  704. X  register unsigned char *p = pattern;
  705. X  register unsigned char *pend = pattern + size;
  706. X  register int j, k;
  707. X  unsigned char *translate = (unsigned char *) bufp->translate;
  708. X
  709. X  unsigned char *stackb[NFAILURES];
  710. X  unsigned char **stackp = stackb;
  711. X
  712. X  bzero (fastmap, (1 << BYTEWIDTH));
  713. X  bufp->fastmap_accurate = 1;
  714. X  bufp->can_be_null = 0;
  715. X      
  716. X  while (p)
  717. X    {
  718. X      if (p == pend)
  719. X    {
  720. X      bufp->can_be_null = 1;
  721. X      break;
  722. X    }
  723. #ifdef SWITCH_ENUM_BUG
  724. X      switch ((int) ((enum regexpcode) *p++))
  725. #else
  726. X      switch ((enum regexpcode) *p++)
  727. #endif
  728. X    {
  729. X    case exactn:
  730. X      if (translate)
  731. X        fastmap[translate[p[1]]] = 1;
  732. X      else
  733. X        fastmap[p[1]] = 1;
  734. X      break;
  735. X
  736. X        case begline:
  737. X        case before_dot:
  738. X    case at_dot:
  739. X    case after_dot:
  740. X    case begbuf:
  741. X    case endbuf:
  742. X    case wordbound:
  743. X    case notwordbound:
  744. X    case wordbeg:
  745. X    case wordend:
  746. X      continue;
  747. X
  748. X    case endline:
  749. X      if (translate)
  750. X        fastmap[translate['\n']] = 1;
  751. X      else
  752. X        fastmap['\n'] = 1;
  753. X      if (bufp->can_be_null != 1)
  754. X        bufp->can_be_null = 2;
  755. X      break;
  756. X
  757. X    case finalize_jump:
  758. X    case maybe_finalize_jump:
  759. X    case jump:
  760. X    case dummy_failure_jump:
  761. X      bufp->can_be_null = 1;
  762. X      j = *p++ & 0377;
  763. X      j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  764. X      p += j + 1;        /* The 1 compensates for missing ++ above */
  765. X      if (j > 0)
  766. X        continue;
  767. X      /* Jump backward reached implies we just went through
  768. X         the body of a loop and matched nothing.
  769. X         Opcode jumped to should be an on_failure_jump.
  770. X         Just treat it like an ordinary jump.
  771. X         For a * loop, it has pushed its failure point already;
  772. X         if so, discard that as redundant.  */
  773. X      if ((enum regexpcode) *p != on_failure_jump)
  774. X        continue;
  775. X      p++;
  776. X      j = *p++ & 0377;
  777. X      j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  778. X      p += j + 1;        /* The 1 compensates for missing ++ above */
  779. X      if (stackp != stackb && *stackp == p)
  780. X        stackp--;
  781. X      continue;
  782. X      
  783. X    case on_failure_jump:
  784. X      j = *p++ & 0377;
  785. X      j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  786. X      p++;
  787. X      *++stackp = p + j;
  788. X      continue;
  789. X
  790. X    case start_memory:
  791. X    case stop_memory:
  792. X      p++;
  793. X      continue;
  794. X
  795. X    case duplicate:
  796. X      bufp->can_be_null = 1;
  797. X      fastmap['\n'] = 1;
  798. X    case anychar:
  799. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  800. X        if (j != '\n')
  801. X          fastmap[j] = 1;
  802. X      if (bufp->can_be_null)
  803. X        return;
  804. X      /* Don't return; check the alternative paths
  805. X         so we can set can_be_null if appropriate.  */
  806. X      break;
  807. X
  808. X    case wordchar:
  809. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  810. X        if (SYNTAX (j) == Sword)
  811. X          fastmap[j] = 1;
  812. X      break;
  813. X
  814. X    case notwordchar:
  815. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  816. X        if (SYNTAX (j) != Sword)
  817. X          fastmap[j] = 1;
  818. X      break;
  819. X
  820. #ifdef emacs
  821. X    case syntaxspec:
  822. X      k = *p++;
  823. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  824. X        if (SYNTAX (j) == (enum syntaxcode) k)
  825. X          fastmap[j] = 1;
  826. X      break;
  827. X
  828. X    case notsyntaxspec:
  829. X      k = *p++;
  830. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  831. X        if (SYNTAX (j) != (enum syntaxcode) k)
  832. X          fastmap[j] = 1;
  833. X      break;
  834. #endif emacs
  835. X
  836. X    case charset:
  837. X      for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  838. X        if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  839. X          {
  840. X        if (translate)
  841. X          fastmap[translate[j]] = 1;
  842. X        else
  843. X          fastmap[j] = 1;
  844. X          }
  845. X      break;
  846. X
  847. X    case charset_not:
  848. X      /* Chars beyond end of map must be allowed */
  849. X      for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  850. X        if (translate)
  851. X          fastmap[translate[j]] = 1;
  852. X        else
  853. X          fastmap[j] = 1;
  854. X
  855. X      for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  856. X        if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  857. X          {
  858. X        if (translate)
  859. X          fastmap[translate[j]] = 1;
  860. X        else
  861. X          fastmap[j] = 1;
  862. X          }
  863. X      break;
  864. X    }
  865. X
  866. X      /* Get here means we have successfully found the possible starting
  867. X     characters of one path of the pattern.  We need not follow this 
  868. X     path any farther. Instead, look at the next alternative 
  869. X     remembered in the stack. */
  870. X      if (stackp != stackb)
  871. X    p = *stackp--;
  872. X      else
  873. X    break;
  874. X    }
  875. }
  876. X
  877. /* Like re_search_2, below, but only one string is specified. */
  878. X
  879. int
  880. re_search (pbufp, string, size, startpos, range, regs)
  881. X     struct re_pattern_buffer *pbufp;
  882. X     char *string;
  883. X     int size, startpos, range;
  884. X     struct re_registers *regs;
  885. {
  886. X  return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
  887. }
  888. X
  889. /* Like re_match_2 but tries first a match starting at index STARTPOS,
  890. X   then at STARTPOS + 1, and so on.
  891. X   RANGE is the number of places to try before giving up.
  892. X   If RANGE is negative, the starting positions tried are
  893. X    STARTPOS, STARTPOS - 1, etc.
  894. X   It is up to the caller to make sure that range is not so large
  895. X   as to take the starting position outside of the input strings.
  896. X
  897. The value returned is the position at which the match was found,
  898. X or -1 if no match was found,
  899. X or -2 if error (such as failure stack overflow).  */
  900. X
  901. int
  902. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs, 
  903. X         mstop)
  904. X     struct re_pattern_buffer *pbufp;
  905. X     char *string1, *string2;
  906. X     int size1, size2;
  907. X     int startpos;
  908. X     register int range;
  909. X     struct re_registers *regs;
  910. X     int mstop;
  911. {
  912. X  register char *fastmap = pbufp->fastmap;
  913. X  register unsigned char *translate = (unsigned char *) pbufp->translate;
  914. X  int total = size1 + size2;
  915. X  int val;
  916. X
  917. X  /* Update the fastmap now if not correct already */
  918. X  if (fastmap && !pbufp->fastmap_accurate)
  919. X    re_compile_fastmap (pbufp);
  920. X  
  921. X  /* Don't waste time in a long search for a pattern
  922. X     that says it is anchored.  */
  923. X  if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  924. X      && range > 0)
  925. X    {
  926. X      if (startpos > 0)
  927. X    return -1;
  928. X      else
  929. X    range = 1;
  930. X    }
  931. X
  932. X  while (1)
  933. X    {
  934. X      /* If a fastmap is supplied, skip quickly over characters
  935. X     that cannot possibly be the start of a match.
  936. X     Note, however, that if the pattern can possibly match
  937. X     the null string, we must test it at each starting point
  938. X     so that we take the first null string we get.  */
  939. X
  940. X      if (fastmap && startpos < total && pbufp->can_be_null != 1)
  941. X    {
  942. X      if (range > 0)
  943. X        {
  944. X          register int lim = 0;
  945. X          register unsigned char *p;
  946. X          int irange = range;
  947. X          if (startpos < size1 && startpos + range >= size1)
  948. X        lim = range - (size1 - startpos);
  949. X
  950. X          p = ((unsigned char *)
  951. X           &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  952. X
  953. X          if (translate)
  954. X        {
  955. X          while (range > lim && !fastmap[translate[*p++]])
  956. X            range--;
  957. X        }
  958. X          else
  959. X        {
  960. X          while (range > lim && !fastmap[*p++])
  961. X            range--;
  962. X        }
  963. X          startpos += irange - range;
  964. X        }
  965. X      else
  966. X        {
  967. X          register unsigned char c;
  968. X          if (startpos >= size1) c = string2[startpos - size1];
  969. X          else c = string1[startpos];
  970. X          c &= 0xff;
  971. X          if (translate ? !fastmap[translate[c]] : !fastmap[c])
  972. X        goto advance;
  973. X        }
  974. X    }
  975. X
  976. X      if (range >= 0 && startpos == total
  977. X      && fastmap && pbufp->can_be_null == 0)
  978. X    return -1;
  979. X
  980. X      val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, 
  981. X            regs, mstop);
  982. X      if (0 <= val)
  983. X    {
  984. X      if (val == -2)
  985. X        return -2;
  986. X      return startpos;
  987. X    }
  988. X
  989. #ifdef C_ALLOCA
  990. X      alloca (0);
  991. #endif /* C_ALLOCA */
  992. X
  993. X    advance:
  994. X      if (!range) break;
  995. X      if (range > 0) range--, startpos++; else range++, startpos--;
  996. X    }
  997. X  return -1;
  998. }
  999. X
  1000. #ifndef emacs   /* emacs never uses this */
  1001. int
  1002. re_match (pbufp, string, size, pos, regs)
  1003. X     struct re_pattern_buffer *pbufp;
  1004. X     char *string;
  1005. X     int size, pos;
  1006. X     struct re_registers *regs;
  1007. {
  1008. X  return re_match_2 (pbufp, 0, 0, string, size, pos, regs, size);
  1009. }
  1010. #endif /* emacs */
  1011. X
  1012. /* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1013. /* 
  1014. X * Modification: failure stack size increased from 2000 to FAILURE_STACK
  1015. X * By :         Po Cheung, po@cerc.utexas.edu
  1016. X * Date :     April 15, 1990
  1017. X */
  1018. X
  1019. int re_max_failures = FAILURE_STACK;
  1020. X
  1021. /* Match the pattern described by PBUFP
  1022. X   against data which is the virtual concatenation of STRING1 and STRING2.
  1023. X   SIZE1 and SIZE2 are the sizes of the two data strings.
  1024. X   Start the match at position POS.
  1025. X   Do not consider matching past the position MSTOP.
  1026. X
  1027. X   If pbufp->fastmap is nonzero, then it had better be up to date.
  1028. X
  1029. X   The reason that the data to match are specified as two components
  1030. X   which are to be regarded as concatenated
  1031. X   is so this function can be used directly on the contents of an Emacs buffer.
  1032. X
  1033. X   -1 is returned if there is no match.  -2 is returned if there is
  1034. X   an error (such as match stack overflow).  Otherwise the value is the length
  1035. X   of the substring which was matched.  */
  1036. X
  1037. int
  1038. re_match_2 (pbufp, string1, size1, string2, size2, pos, regs, mstop)
  1039. X     struct re_pattern_buffer *pbufp;
  1040. X     char *string1, *string2;
  1041. X     int size1, size2;
  1042. X     int pos;
  1043. X     struct re_registers *regs;
  1044. X     int mstop;
  1045. {
  1046. X  register char *p = pbufp->buffer;
  1047. X  register char *pend = p + pbufp->used;
  1048. X  /* End of first string */
  1049. X  char *end1;
  1050. X  /* End of second string */
  1051. X  char *end2;
  1052. X  /* Pointer just past last char to consider matching */
  1053. X  char *end_match_1, *end_match_2;
  1054. X  register char *d, *dend;
  1055. X  register int mcnt;
  1056. X  unsigned char *translate = (unsigned char *) pbufp->translate;
  1057. X
  1058. X /* Failure point stack.  Each place that can handle a failure further 
  1059. X    down the line pushes a failure point on this stack.  It consists of 
  1060. X    two char *'s.
  1061. X    The first one pushed is where to resume scanning the pattern;
  1062. X    the second pushed is where to resume scanning the strings.
  1063. X    If the latter is zero, the failure point is a "dummy".
  1064. X    If a failure happens and the innermost failure point is dormant,
  1065. X    it discards that failure point and tries the next one. */
  1066. X
  1067. X  char **stackb = (char **) alloca (2 * NFAILURES * sizeof (char *));
  1068. X  char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1069. X
  1070. X  /* Information on the "contents" of registers.
  1071. X     These are pointers into the input strings; they record
  1072. X     just what was matched (on this attempt) by some part of the pattern.
  1073. X     The start_memory command stores the start of a register's contents
  1074. X     and the stop_memory command stores the end.
  1075. X
  1076. X     At that point, regstart[regnum] points to the first character in the
  1077. X     register, regend[regnum] points to the first character beyond the end 
  1078. X     of the register,
  1079. X     regstart_seg1[regnum] is true iff regstart[regnum] points into string1,
  1080. X     and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */
  1081. X
  1082. X  char *regstart[RE_NREGS];
  1083. X  char *regend[RE_NREGS];
  1084. X  char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1085. X
  1086. X  /* Set up pointers to ends of strings.
  1087. X     Don't allow the second string to be empty unless both are empty.  */
  1088. X  if (!size2)
  1089. X    {
  1090. X      string2 = string1;
  1091. X      size2 = size1;
  1092. X      string1 = 0;
  1093. X      size1 = 0;
  1094. X    }
  1095. X  end1 = string1 + size1;
  1096. X  end2 = string2 + size2;
  1097. X
  1098. X  /* Compute where to stop matching, within the two strings */
  1099. X  if (mstop <= size1)
  1100. X    {
  1101. X      end_match_1 = string1 + mstop;
  1102. X      end_match_2 = string2;
  1103. X    }
  1104. X  else
  1105. X    {
  1106. X      end_match_1 = end1;
  1107. X      end_match_2 = string2 + mstop - size1;
  1108. X    }
  1109. X
  1110. X  /* Initialize \( and \) text positions to -1
  1111. X     to mark ones that no \( or \) has been seen for.  */
  1112. X
  1113. X  for (mcnt = 0; mcnt < sizeof (regstart) / sizeof (*regstart); mcnt++)
  1114. X    regstart[mcnt] = (char *) -1;
  1115. X
  1116. X  /* `p' scans through the pattern as `d' scans through the data.
  1117. X     `dend' is the end of the input string that `d' points within.
  1118. X     `d' is advanced into the following input string whenever necessary,
  1119. X     but this happens before fetching;
  1120. X     therefore, at the beginning of the loop,
  1121. X     `d' can be pointing at the end of a string,
  1122. X     but it cannot equal string2.  */
  1123. X
  1124. X  if (pos <= size1)
  1125. X    d = string1 + pos, dend = end_match_1;
  1126. X  else
  1127. X    d = string2 + pos - size1, dend = end_match_2;
  1128. X
  1129. /* Write PREFETCH; just before fetching a character with *d.  */
  1130. #define PREFETCH \
  1131. X while (d == dend)                            \
  1132. X  { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1133. X    d = string2;  /* end of string1 => advance to string2. */       \
  1134. X    dend = end_match_2; }
  1135. X
  1136. X  /* This loop loops over pattern commands.
  1137. X     It exits by returning from the function if match is complete,
  1138. X     or it drops through if match fails at this starting point in the 
  1139. X     input data. */
  1140. X
  1141. X  while (1)
  1142. X    {
  1143. X      if (p == pend)
  1144. X    /* End of pattern means we have succeeded! */
  1145. X    {
  1146. X      /* If caller wants register contents data back, convert it to indices */
  1147. X      if (regs)
  1148. X        {
  1149. X           regs->start[0] = pos;
  1150. X           if (dend == end_match_1)
  1151. X         regs->end[0] = d - string1;
  1152. X           else
  1153. X         regs->end[0] = d - string2 + size1;
  1154. X           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1155. X        {
  1156. X          if (regstart[mcnt] == (char *) -1)
  1157. X            {
  1158. X              regs->start[mcnt] = -1;
  1159. X              regs->end[mcnt] = -1;
  1160. X              continue;
  1161. X            }
  1162. X           if (regstart_seg1[mcnt])
  1163. X            regs->start[mcnt] = regstart[mcnt] - string1;
  1164. X          else
  1165. X            regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  1166. X           if (regend_seg1[mcnt])
  1167. X            regs->end[mcnt] = regend[mcnt] - string1;
  1168. X          else
  1169. X            regs->end[mcnt] = regend[mcnt] - string2 + size1;
  1170. X        }
  1171. X        }
  1172. X       if (dend == end_match_1)
  1173. X        return (d - string1 - pos);
  1174. X      else
  1175. X        return d - string2 + size1 - pos;
  1176. X    }
  1177. X
  1178. X      /* Otherwise match next pattern command */
  1179. #ifdef SWITCH_ENUM_BUG
  1180. X      switch ((int) ((enum regexpcode) *p++))
  1181. #else
  1182. X      switch ((enum regexpcode) *p++)
  1183. #endif
  1184. X    {
  1185. X
  1186. X    /* \( is represented by a start_memory, \) by a stop_memory.
  1187. X        Both of those commands contain a "register number" argument.
  1188. X        The text matched within the \( and \) is recorded under that number.
  1189. X        Then, \<digit> turns into a `duplicate' command which
  1190. X        is followed by the numeric value of <digit> as the register number. */
  1191. X
  1192. X    case start_memory:
  1193. X      regstart[*p] = d;
  1194. X       regstart_seg1[*p++] = (dend == end_match_1);
  1195. X      break;
  1196. X
  1197. X    case stop_memory:
  1198. X      regend[*p] = d;
  1199. X       regend_seg1[*p++] = (dend == end_match_1);
  1200. X      break;
  1201. X
  1202. X    case duplicate:
  1203. X      {
  1204. X        int regno = *p++;   /* Get which register to match against */
  1205. X        register char *d2, *dend2;
  1206. X
  1207. X        d2 = regstart[regno];
  1208. X         dend2 = (regstart_seg1[regno] == regend_seg1[regno])
  1209. X                ? regend[regno]
  1210. X            : end_match_1;
  1211. X        while (1)
  1212. X          {
  1213. X        /* Advance to next segment in register contents, if necessary */
  1214. X        while (d2 == dend2)
  1215. X          {
  1216. X            if (dend2 == end_match_2) break;
  1217. X            if (dend2 == regend[regno]) break;
  1218. /* end of string1 => advance to string2. */
  1219. X            d2 = string2, dend2 = regend[regno];  
  1220. X          }
  1221. X        /* At end of register contents => success */
  1222. X        if (d2 == dend2) break;
  1223. X
  1224. X        /* Advance to next segment in data being matched, if necessary */
  1225. X        PREFETCH;
  1226. X
  1227. X        /* mcnt gets # consecutive chars to compare */
  1228. X        mcnt = dend - d;
  1229. X        if (mcnt > dend2 - d2)
  1230. X          mcnt = dend2 - d2;
  1231. X        /* Compare that many; failure if mismatch, else skip them. */
  1232. X        if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  1233. X          goto fail;
  1234. X        d += mcnt, d2 += mcnt;
  1235. X          }
  1236. X      }
  1237. X      break;
  1238. X
  1239. X    case anychar:
  1240. X      /* fetch a data character */
  1241. X      PREFETCH;
  1242. X      /* Match anything but a newline.  */
  1243. X      if ((translate ? translate[*(unsigned char *)d++] : *d++) == '\n')
  1244. X        goto fail;
  1245. X      break;
  1246. X
  1247. X    case charset:
  1248. X    case charset_not:
  1249. X      {
  1250. X        /* Nonzero for charset_not */
  1251. X        int not = 0;
  1252. X        register int c;
  1253. X        if (*(p - 1) == (char) charset_not)
  1254. X          not = 1;
  1255. X
  1256. X        /* fetch a data character */
  1257. X        PREFETCH;
  1258. X
  1259. X        if (translate)
  1260. X          c = translate [*(unsigned char *)d];
  1261. X        else
  1262. X          c = *(unsigned char *)d;
  1263. X
  1264. X        if (c < *p * BYTEWIDTH
  1265. X        && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1266. X          not = !not;
  1267. X
  1268. X        p += 1 + *p;
  1269. X
  1270. X        if (!not) goto fail;
  1271. X        d++;
  1272. X        break;
  1273. X      }
  1274. X
  1275. X    case begline:
  1276. X      if (d == string1 || d[-1] == '\n')
  1277. X        break;
  1278. X      goto fail;
  1279. X
  1280. X    case endline:
  1281. X      if (d == end2
  1282. X          || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  1283. X        break;
  1284. X      goto fail;
  1285. X
  1286. X    /* "or" constructs ("|") are handled by starting each alternative
  1287. X        with an on_failure_jump that points to the start of the next alternative.
  1288. X        Each alternative except the last ends with a jump to the joining point.
  1289. X        (Actually, each jump except for the last one really jumps
  1290. X         to the following jump, because tensioning the jumps is a hassle.) */
  1291. X
  1292. X    /* The start of a stupid repeat has an on_failure_jump that points
  1293. X       past the end of the repeat text.
  1294. X       This makes a failure point so that, on failure to match a repetition,
  1295. X       matching restarts past as many repetitions have been found
  1296. X       with no way to fail and look for another one.  */
  1297. X
  1298. X    /* A smart repeat is similar but loops back to the on_failure_jump
  1299. X       so that each repetition makes another failure point. */
  1300. X
  1301. X    case on_failure_jump:
  1302. X      if (stackp == stacke)
  1303. X        {
  1304. X          char **stackx;
  1305. X          if (stacke - stackb > re_max_failures)
  1306. X        return -2;
  1307. X          stackx = (char **) alloca (2 * (stacke - stackb) * sizeof (char *));
  1308. X          bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1309. X          stackp += stackx - stackb;
  1310. X          stacke = stackx + 2 * (stacke - stackb);
  1311. X          stackb = stackx;
  1312. X        }
  1313. X      mcnt = *p++ & 0377;
  1314. X      mcnt += SIGN_EXTEND_CHAR (*p) << 8;
  1315. X      p++;
  1316. X      *stackp++ = mcnt + p;
  1317. X      *stackp++ = d;
  1318. X      break;
  1319. X
  1320. X    /* The end of a smart repeat has an maybe_finalize_jump back.
  1321. X       Change it either to a finalize_jump or an ordinary jump. */
  1322. X
  1323. X    case maybe_finalize_jump:
  1324. X      mcnt = *p++ & 0377;
  1325. X      mcnt += SIGN_EXTEND_CHAR (*p) << 8;
  1326. X      p++;
  1327. X      /* Compare what follows with the begining of the repeat.
  1328. X         If we can establish that there is nothing that they would
  1329. X         both match, we can change to finalize_jump */
  1330. X      if (p == pend)
  1331. X        p[-3] = (char) finalize_jump;
  1332. X      else if (*p == (char) exactn || *p == (char) endline)
  1333. X        {
  1334. X          register int c = *p == (char) endline ? '\n' : p[2];
  1335. X          register char *p1 = p + mcnt;
  1336. X          /* p1[0] ... p1[2] are an on_failure_jump.
  1337. X         Examine what follows that */
  1338. X          if (p1[3] == (char) exactn && p1[5] != c)
  1339. X        p[-3] = (char) finalize_jump;
  1340. X          else if (p1[3] == (char) charset || p1[3] == (char) charset_not)
  1341. X        {
  1342. X          int not = p1[3] == (char) charset_not;
  1343. X          if (c < p1[4] * BYTEWIDTH
  1344. X              && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1345. X            not = !not;
  1346. X          /* not is 1 if c would match */
  1347. X          /* That means it is not safe to finalize */
  1348. X          if (!not)
  1349. X            p[-3] = (char) finalize_jump;
  1350. X        }
  1351. X        }
  1352. X      p -= 2;
  1353. X      if (p[-1] != (char) finalize_jump)
  1354. X        {
  1355. X          p[-1] = (char) jump;
  1356. X          goto nofinalize;
  1357. X        }
  1358. X
  1359. X    /* The end of a stupid repeat has a finalize-jump
  1360. X       back to the start, where another failure point will be made
  1361. X       which will point after all the repetitions found so far. */
  1362. X
  1363. X    case finalize_jump:
  1364. X      stackp -= 2;
  1365. X
  1366. X    case jump:
  1367. X    nofinalize:
  1368. X      mcnt = *p++ & 0377;
  1369. X      mcnt += SIGN_EXTEND_CHAR (*p) << 8;
  1370. X      p += mcnt + 1;    /* The 1 compensates for missing ++ above */
  1371. X      break;
  1372. X
  1373. X    case dummy_failure_jump:
  1374. X      if (stackp == stacke)
  1375. X        {
  1376. X          char **stackx = (char **) alloca (2 * (stacke - stackb) *
  1377. X                        sizeof (char *));
  1378. X          bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1379. X          stackp += stackx - stackb;
  1380. X          stacke = stackx + 2 * (stacke - stackb);
  1381. X          stackb = stackx;
  1382. X        }
  1383. X      *stackp++ = 0;
  1384. X      *stackp++ = 0;
  1385. X      goto nofinalize;
  1386. X
  1387. X    case wordbound:
  1388. X      if (d == string1  /* Points to first char */
  1389. X          || d == end2  /* Points to end */
  1390. X          || (d == end1 && size2 == 0)) /* Points to end */
  1391. X        break;
  1392. X      if ((SYNTAX (((unsigned char *)d)[-1]) == Sword)
  1393. X          != (SYNTAX (d == end1 ? *(unsigned char *)string2 : 
  1394. X              *(unsigned char *)d) == Sword))
  1395. X        break;
  1396. X      goto fail;
  1397. X
  1398. X    case notwordbound:
  1399. X      if (d == string1  /* Points to first char */
  1400. X          || d == end2  /* Points to end */
  1401. X          || (d == end1 && size2 == 0)) /* Points to end */
  1402. X        goto fail;
  1403. X      if ((SYNTAX (((unsigned char *)d)[-1]) == Sword)
  1404. X          != (SYNTAX (d == end1 ? *(unsigned char *)string2 : 
  1405. X              *(unsigned char *)d) == Sword))
  1406. X        goto fail;
  1407. X      break;
  1408. X
  1409. X    case wordbeg:
  1410. X      if (d == end2  /* Points to end */
  1411. X          || (d == end1 && size2 == 0) /* Points to end */
  1412. X          || SYNTAX (*(unsigned char *) (d == end1 ? string2 : d)) != 
  1413. X          Sword) /* Next char not a letter */
  1414. X        goto fail;
  1415. X /* prev char not letter */
  1416. SHAR_EOF
  1417. true || echo 'restore of mxgdb/regex.c failed'
  1418. fi
  1419. echo 'End of  part 7'
  1420. echo 'File mxgdb/regex.c is continued in part 8'
  1421. echo 8 > _shar_seq_.tmp
  1422. exit 0
  1423.