home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3641 < prev    next >
Encoding:
Text File  |  1991-07-17  |  40.4 KB  |  1,475 lines

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