home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / sed-2.03-src.lha / src / amiga / sed-2.03 / rx.c < prev    next >
C/C++ Source or Header  |  1993-08-07  |  222KB  |  8,785 lines

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. /* NOTE!!!  AIX requires this to be the first thing in the file.
  18.  * Do not put ANYTHING before it!  
  19.  */
  20. #if !defined (__GNUC__) && defined (_AIX)
  21.  #pragma alloca
  22. #endif
  23.  
  24. static char rx_version_string[] = "GNU Rx version 0.03";
  25.  
  26.             /* ``Too hard!''
  27.              *        -- anon.
  28.              */
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #ifndef isgraph
  33. #define isgraph(c) (isprint (c) && !isspace (c))
  34. #endif
  35. #ifndef isblank
  36. #define isblank(c) ((c) == ' ' || (c) == '\t')
  37. #endif
  38.  
  39. #include <sys/types.h>
  40. #include <stdio.h>
  41. #include "rx.h"
  42.  
  43. #undef MAX
  44. #undef MIN
  45. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  46. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  47.  
  48. typedef char boolean;
  49. #define false 0
  50. #define true 1
  51.  
  52.  
  53. /* This page is decls to the interesting subsystems and lower layers
  54.  * of rx.  Everything which doesn't have a public counterpart in 
  55.  * regex.c is declared here.
  56.  * 
  57.  * A useful (i hope) system is obtained by removing all or part of the regex.c
  58.  * reimplementation and making these all extern.  I think this package
  59.  * could be used to implement on-line lexers and parsers and who knows what 
  60.  * else.
  61.  */
  62. /* In the definitions, these functions are qualified by `RX_DECL' */
  63. #define RX_DECL static
  64.  
  65. #ifdef __STDC__
  66.  
  67. RX_DECL int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
  68. RX_DECL void rx_bitset_null (int size, rx_Bitset b);
  69. RX_DECL void rx_bitset_universe (int size, rx_Bitset b);
  70. RX_DECL void rx_bitset_complement (int size, rx_Bitset b);
  71. RX_DECL void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
  72. RX_DECL void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
  73. RX_DECL void rx_bitset_intersection (int size,
  74.                      rx_Bitset a, rx_Bitset b);
  75. RX_DECL void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
  76. RX_DECL unsigned long rx_bitset_hash (int size, rx_Bitset b);
  77. RX_DECL struct rx_hash_item * rx_hash_find (struct rx_hash * table,
  78.                         unsigned long hash,
  79.                         void * value,
  80.                         struct rx_hash_rules * rules);
  81. RX_DECL struct rx_hash_item * rx_hash_store (struct rx_hash * table,
  82.                          unsigned long hash,
  83.                          void * value,
  84.                          struct rx_hash_rules * rules);
  85. RX_DECL void rx_hash_free (struct rx_hash_item * it,
  86.                struct rx_hash_rules * rules);
  87. RX_DECL rx_Bitset rx_cset (struct rx *rx);
  88. RX_DECL rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
  89. RX_DECL void rx_free_cset (struct rx * rx, rx_Bitset c);
  90. RX_DECL struct rexp_node * rexp_node (struct rx *rx,
  91.                       enum rexp_node_type type);
  92. RX_DECL struct rexp_node * rx_mk_r_cset (struct rx * rx,
  93.                      rx_Bitset b);
  94. RX_DECL struct rexp_node * rx_mk_r_concat (struct rx * rx,
  95.                        struct rexp_node * a,
  96.                        struct rexp_node * b);
  97. RX_DECL struct rexp_node * rx_mk_r_alternate (struct rx * rx,
  98.                           struct rexp_node * a,
  99.                           struct rexp_node * b);
  100. RX_DECL struct rexp_node * rx_mk_r_opt (struct rx * rx,
  101.                     struct rexp_node * a);
  102. RX_DECL struct rexp_node * rx_mk_r_star (struct rx * rx,
  103.                      struct rexp_node * a);
  104. RX_DECL struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
  105.                         struct rexp_node * a,
  106.                         struct rexp_node * b);
  107. RX_DECL struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
  108.                         rx_side_effect a);
  109. RX_DECL struct rexp_node * rx_mk_r_data  (struct rx * rx,
  110.                       void * a);
  111. RX_DECL void rx_free_rexp (struct rx * rx, struct rexp_node * node);
  112. RX_DECL struct rexp_node * rx_copy_rexp (struct rx *rx,
  113.                      struct rexp_node *node);
  114. RX_DECL struct rx_nfa_state * rx_nfa_state (struct rx *rx);
  115. RX_DECL void rx_free_nfa_state (struct rx_nfa_state * n);
  116. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
  117.                           int id);
  118. RX_DECL struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
  119.                       enum rx_nfa_etype type,
  120.                       struct rx_nfa_state *start,
  121.                       struct rx_nfa_state *dest);
  122. RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge * e);
  123. RX_DECL void rx_free_nfa (struct rx *rx);
  124. RX_DECL int rx_build_nfa (struct rx *rx,
  125.               struct rexp_node *rexp,
  126.               struct rx_nfa_state **start,
  127.               struct rx_nfa_state **end);
  128. RX_DECL void rx_name_nfa_states (struct rx *rx);
  129. RX_DECL int rx_eclose_nfa (struct rx *rx);
  130. RX_DECL void rx_delete_epsilon_transitions (struct rx *rx);
  131. RX_DECL int rx_compactify_nfa (struct rx *rx,
  132.                    void **mem, unsigned long *size);
  133. RX_DECL struct rx_superset * rx_superstate_eclosure_union
  134.   (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl) ;
  135. RX_DECL void rx_release_superset (struct rx *rx,
  136.                   struct rx_superset *set);
  137. RX_DECL struct rx_superstate * rx_superstate (struct rx *rx,
  138.                           struct rx_superset *set);
  139. RX_DECL struct rx_inx * rx_handle_cache_miss
  140.   (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data) ;
  141.  
  142. #else /* ndef __STDC__ */
  143. RX_DECL int rx_bitset_is_subset ();
  144. RX_DECL void rx_bitset_null ();
  145. RX_DECL void rx_bitset_universe ();
  146. RX_DECL void rx_bitset_complement ();
  147. RX_DECL void rx_bitset_assign ();
  148. RX_DECL void rx_bitset_union ();
  149. RX_DECL void rx_bitset_intersection ();
  150. RX_DECL void rx_bitset_difference ();
  151. RX_DECL unsigned long rx_bitset_hash ();
  152. RX_DECL struct rx_hash_item * rx_hash_find ();
  153. RX_DECL struct rx_hash_item * rx_hash_store ();
  154. RX_DECL void rx_hash_free ();
  155. RX_DECL rx_Bitset rx_cset ();
  156. RX_DECL rx_Bitset rx_copy_cset ();
  157. RX_DECL void rx_free_cset ();
  158. RX_DECL struct rexp_node * rexp_node ();
  159. RX_DECL struct rexp_node * rx_mk_r_cset ();
  160. RX_DECL struct rexp_node * rx_mk_r_concat ();
  161. RX_DECL struct rexp_node * rx_mk_r_alternate ();
  162. RX_DECL struct rexp_node * rx_mk_r_opt ();
  163. RX_DECL struct rexp_node * rx_mk_r_star ();
  164. RX_DECL struct rexp_node * rx_mk_r_2phase_star ();
  165. RX_DECL struct rexp_node * rx_mk_r_side_effect ();
  166. RX_DECL struct rexp_node * rx_mk_r_data  ();
  167. RX_DECL void rx_free_rexp ();
  168. RX_DECL struct rexp_node * rx_copy_rexp ();
  169. RX_DECL struct rx_nfa_state * rx_nfa_state ();
  170. RX_DECL void rx_free_nfa_state ();
  171. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state ();
  172. RX_DECL struct rx_nfa_edge * rx_nfa_edge ();
  173. RX_DECL void rx_free_nfa_edge ();
  174. RX_DECL void rx_free_nfa ();
  175. RX_DECL int rx_build_nfa ();
  176. RX_DECL void rx_name_nfa_states ();
  177. RX_DECL int rx_eclose_nfa ();
  178. RX_DECL void rx_delete_epsilon_transitions ();
  179. RX_DECL int rx_compactify_nfa ();
  180. RX_DECL struct rx_superset * rx_superstate_eclosure_union ();
  181. RX_DECL void rx_release_superset ();
  182. RX_DECL struct rx_superstate * rx_superstate ();
  183. RX_DECL struct rx_inx * rx_handle_cache_miss ();
  184.   
  185. #endif /* ndef __STDC__ */
  186.  
  187.  
  188.  
  189. /* Emacs already defines alloca, sometimes.  */
  190. #ifndef alloca
  191.  
  192. /* Make alloca work the best possible way.  */
  193. #ifdef __GNUC__
  194. #define alloca __builtin_alloca
  195. #else /* not __GNUC__ */
  196. #if HAVE_ALLOCA_H
  197. #include <alloca.h>
  198. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  199. #ifndef _AIX /* Already did AIX, up at the top.  */
  200. char *alloca ();
  201. #endif /* not _AIX */
  202. #endif /* not HAVE_ALLOCA_H */ 
  203. #endif /* not __GNUC__ */
  204.  
  205. #endif /* not alloca */
  206.  
  207.  
  208. /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
  209.  * use `alloca' instead of `malloc' for the backtracking stack.
  210.  *
  211.  * Emacs will die miserably if we don't do this.
  212.  */
  213.  
  214. #ifdef REGEX_MALLOC
  215.  
  216. #define REGEX_ALLOCATE malloc
  217.  
  218. #else /* not REGEX_MALLOC  */
  219.  
  220. #define REGEX_ALLOCATE alloca
  221.  
  222. #endif /* not REGEX_MALLOC */
  223.  
  224.  
  225.  
  226.  
  227. /* Memory management and stuff for emacs. */
  228.  
  229. #define BYTEWIDTH 8 /* In bits.  */
  230.  
  231. /* (Re)Allocate N items of type T using malloc.  */
  232. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  233. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  234.  
  235. #define remalloc(M, S) (M ? realloc (M, S) : malloc (S))
  236.  
  237. #ifdef emacs
  238. /* The `emacs' switch turns on certain matching commands
  239.  * that make sense only in Emacs. 
  240.  */
  241.  
  242. #include "config.h"
  243. #include "lisp.h"
  244. #include "buffer.h"
  245. #include "syntax.h"
  246.  
  247. /* Emacs uses `NULL' as a predicate.  */
  248. #undef NULL
  249. #else  /* not emacs */
  250.  
  251. /* Setting RX_MEMDBUG is useful if you have dbmalloc.  Maybe with similar
  252.  * packages too.
  253.  */
  254. #ifdef RX_MEMDBUG
  255. #include <malloc.h>
  256. #else /* not RX_RX_MEMDBUG */
  257.  
  258. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  259.  * `BSTRING', as far as I know, and neither of them use this code.  
  260.  */
  261. #if HAVE_STRING_H || STDC_HEADERS
  262. #include <string.h>
  263. #ifndef bcmp
  264. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  265. #endif
  266. #ifndef bcopy
  267. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  268. #endif
  269. #ifndef bzero
  270. #define bzero(s, n)    memset ((s), 0, (n))
  271. #endif
  272. #else
  273. #include <strings.h>
  274. #endif
  275.  
  276. #ifdef STDC_HEADERS
  277. #include <stdlib.h>
  278. #else /* not STDC_HEADERS */
  279.  
  280. char *malloc ();
  281. char *realloc ();
  282. #endif /* not STDC_HEADERS */
  283.  
  284. #endif /* not RX_RX_MEMDBUG */
  285.  
  286.  
  287.  
  288. /* Define the syntax basics for \<, \>, etc.
  289.  * This must be nonzero for the wordchar and notwordchar pattern
  290.  * commands in re_match_2.
  291.  */
  292. #ifndef Sword 
  293. #define Sword 1
  294. #endif
  295.  
  296. #ifdef SYNTAX_TABLE
  297. extern char *re_syntax_table;
  298. #else /* not SYNTAX_TABLE */
  299.  
  300. /* How many characters in the character set.  */
  301. #define CHAR_SET_SIZE (1 << BYTEWIDTH)
  302. static char re_syntax_table[CHAR_SET_SIZE];
  303.  
  304. #ifdef __STDC__
  305. static void
  306. init_syntax_once (void)
  307. #else
  308. static void
  309. init_syntax_once ()
  310. #endif
  311. {
  312.    register int c;
  313.    static int done = 0;
  314.  
  315.    if (done)
  316.      return;
  317.  
  318.    bzero (re_syntax_table, sizeof re_syntax_table);
  319.  
  320.    for (c = 'a'; c <= 'z'; c++)
  321.      re_syntax_table[c] = Sword;
  322.  
  323.    for (c = 'A'; c <= 'Z'; c++)
  324.      re_syntax_table[c] = Sword;
  325.  
  326.    for (c = '0'; c <= '9'; c++)
  327.      re_syntax_table[c] = Sword;
  328.  
  329.    re_syntax_table['_'] = Sword;
  330.  
  331.    done = 1;
  332. }
  333. #endif /* not SYNTAX_TABLE */
  334.  
  335. #define SYNTAX(c) re_syntax_table[c]
  336.  
  337. #endif /* not emacs */
  338.  
  339.  
  340. /* Compile with `-DRX_DEBUG' and use the following flags.
  341.  *
  342.  * Debugging flags:
  343.  *       rx_debug - print information as a regexp is compiled
  344.  *     rx_debug_trace - print information as a regexp is executed
  345.  */
  346.  
  347. #ifdef RX_DEBUG
  348.  
  349. int rx_debug_compile = 0;
  350. int rx_debug_trace = 0;
  351. static struct re_pattern_buffer * dbug_rxb = 0;
  352.  
  353. #ifdef __STDC__
  354. typedef void (*side_effect_printer) (struct rx *, void *, FILE *);
  355. #else
  356. typedef void (*side_effect_printer) ();
  357. #endif
  358.  
  359. #ifdef __STDC__
  360. static void print_cset (struct rx *rx, rx_Bitset cset, FILE * fp);
  361. #else
  362. static void print_cset ();
  363. #endif
  364.  
  365. #ifdef __STDC__
  366. static void
  367. print_rexp (struct rx *rx,
  368.         struct rexp_node *node, int depth,
  369.         side_effect_printer seprint, FILE * fp)
  370. #else
  371. static void
  372. print_rexp (rx, node, depth, seprint, fp)
  373.      struct rx *rx;
  374.      struct rexp_node *node;
  375.      int depth;
  376.      side_effect_printer seprint;
  377.      FILE * fp;
  378. #endif
  379. {
  380.   if (!node)
  381.     return;
  382.   else
  383.     {
  384.       switch (node->type)
  385.     {
  386.     case r_cset:
  387.       {
  388.         fprintf (fp, "%*s", depth, "");
  389.         print_cset (rx, node->params.cset, fp);
  390.         fputc ('\n', fp);
  391.         break;
  392.       }
  393.  
  394.      case r_opt:
  395.     case r_star:
  396.       fprintf (fp, "%*s%s\n", depth, "",
  397.            node->type == r_opt ? "opt" : "star");
  398.       print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp);
  399.       break;
  400.  
  401.     case r_2phase_star:
  402.       fprintf (fp, "%*s2phase star\n", depth, "");
  403.       print_rexp (rx, node->params.pair.right, depth + 3, seprint, fp);
  404.       print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp);
  405.       break;
  406.  
  407.  
  408.     case r_alternate:
  409.     case r_concat:
  410.       fprintf (fp, "%*s%s\n", depth, "",
  411.            node->type == r_alternate ? "alt" : "concat");
  412.       print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp);
  413.       print_rexp (rx, node->params.pair.right, depth + 3, seprint, fp);
  414.       break;
  415.     case r_side_effect:
  416.       fprintf (fp, "%*sSide effect: ", depth, "");
  417.       seprint (rx, node->params.side_effect, fp);
  418.       fputc ('\n', fp);
  419.     }
  420.     }
  421. }
  422.  
  423.  
  424. #ifdef __STDC__
  425. static void
  426. print_nfa (struct rx * rx,
  427.        struct rx_nfa_state * n,
  428.        side_effect_printer seprint, FILE * fp)
  429. #else
  430. static void
  431. print_nfa (rx, n, seprint, fp)
  432.      struct rx * rx;
  433.      struct rx_nfa_state * n;
  434.      side_effect_printer seprint;
  435.      FILE * fp;
  436. #endif
  437. {
  438.   while (n)
  439.     {
  440.       struct rx_nfa_edge *e = n->edges;
  441.       struct rx_possible_future *ec = n->futures;
  442.       fprintf (fp, "node %d %s\n", n->id,
  443.            n->is_final ? "final" : (n->is_start ? "start" : ""));
  444.       while (e)
  445.     {
  446.       fprintf (fp, "   edge to %d, ", e->dest->id);
  447.       switch (e->type)
  448.         {
  449.         case ne_epsilon:
  450.           fprintf (fp, "epsilon\n");
  451.           break;
  452.         case ne_side_effect:
  453.           fprintf (fp, "side effect ");
  454.           seprint (rx, e->params.side_effect, fp);
  455.           fputc ('\n', fp);
  456.           break;
  457.         case ne_cset:
  458.           fprintf (fp, "cset ");
  459.           print_cset (rx, e->params.cset, fp);
  460.           fputc ('\n', fp);
  461.           break;
  462.         }
  463.       e = e->next;
  464.     }
  465.  
  466.       while (ec)
  467.     {
  468.       int x;
  469.       struct rx_nfa_state_set * s;
  470.       struct rx_se_list * l;
  471.       fprintf (fp, "   eclosure to {");
  472.       for (s = ec->destset; s; s = s->cdr)
  473.         fprintf (fp, "%d ", s->car->id);
  474.       fprintf (fp, "} (");
  475.       for (l = ec->effects; l; l = l->cdr)
  476.         {
  477.           seprint (rx, l->car, fp);
  478.           fputc (' ', fp);
  479.         }
  480.       fprintf (fp, ")\n");
  481.       ec = ec->next;
  482.     }
  483.       n = n->next;
  484.     }
  485. }
  486.  
  487. static char * efnames [] =
  488. {
  489.   "bogon",
  490.   "re_se_try",
  491.   "re_se_pushback",
  492.   "re_se_push0",
  493.   "re_se_pushpos",
  494.   "re_se_chkpos",
  495.   "re_se_poppos",
  496.   "re_se_at_dot",
  497.   "re_se_syntax",
  498.   "re_se_not_syntax",
  499.   "re_se_begbuf",
  500.   "re_se_hat",
  501.   "re_se_wordbeg",
  502.   "re_se_wordbound",
  503.   "re_se_notwordbound",
  504.   "re_se_wordend",
  505.   "re_se_endbuf",
  506.   "re_se_dollar",
  507.   "re_se_fail",
  508. };
  509.  
  510. static char * efnames2[] =
  511. {
  512.   "re_se_win"
  513.   "re_se_lparen",
  514.   "re_se_rparen",
  515.   "re_se_backref",
  516.   "re_se_iter",
  517.   "re_se_end_iter",
  518.   "re_se_tv"
  519. };
  520.  
  521. static char * inx_names[] = 
  522. {
  523.   "rx_backtrack_point",
  524.   "rx_do_side_effects",
  525.   "rx_cache_miss",
  526.   "rx_next_char",
  527.   "rx_backtrack",
  528.   "rx_error_inx",
  529.   "rx_num_instructions"
  530. };
  531.  
  532.  
  533. #ifdef __STDC__
  534. static void
  535. re_seprint (struct rx * rx, void * effect, FILE * fp)
  536. #else
  537. static void
  538. re_seprint (rx, effect, fp)
  539.      struct rx * rx;
  540.      void * effect;
  541.      FILE * fp;
  542. #endif
  543. {
  544.   if ((int)effect < 0)
  545.     fputs (efnames[-(int)effect], fp);
  546.   else if (dbug_rxb)
  547.     {
  548.       struct re_se_params * p = &dbug_rxb->se_params[(int)effect];
  549.       fprintf (fp, "%s(%d,%d)", efnames2[p->se], p->op1, p->op2);
  550.     }
  551.   else
  552.     fprintf (fp, "[complex op # %d]", (int)effect);
  553. }
  554.  
  555.  
  556. /* These are for so the regex.c regression tests will compile. */
  557. void
  558. print_compiled_pattern (rxb)
  559.      struct re_pattern_buffer * rxb;
  560. {
  561. }
  562.  
  563. void
  564. print_fastmap (fm)
  565.      char * fm;
  566. {
  567. }
  568.  
  569.  
  570.  
  571. #endif /* RX_DEBUG */
  572.  
  573.  
  574.  
  575. /* This page: Bitsets.  Completely unintersting. */
  576.  
  577. #if 0
  578. #ifdef __STDC__
  579. RX_DECL int
  580. rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b)
  581. #else
  582. RX_DECL int
  583. rx_bitset_is_equal (size, a, b)
  584.      int size;
  585.      rx_Bitset a;
  586.      rx_Bitset b;
  587. #endif
  588. {
  589.   int x;
  590.   RX_subset s = b[0];
  591.   b[0] = ~a[0];
  592.  
  593.   for (x = rx_bitset_numb_subsets(size) - 1; a[x] == b[x]; --x)
  594.     ;
  595.  
  596.   b[0] = s;
  597.   return !x && s == a[0];
  598. }
  599. #endif
  600.  
  601. #ifdef __STDC__
  602. RX_DECL int
  603. rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b)
  604. #else
  605. RX_DECL int
  606. rx_bitset_is_subset (size, a, b)
  607.      int size;
  608.      rx_Bitset a;
  609.      rx_Bitset b;
  610. #endif
  611. {
  612.   int x = rx_bitset_numb_subsets(size) - 1;
  613.   while (x-- && (a[x] & b[x]) == a[x]);
  614.   return x == -1;
  615. }
  616.  
  617.  
  618. #if 0
  619. #ifdef __STDC__
  620. RX_DECL int
  621. rx_bitset_empty (int size, rx_Bitset set)
  622. #else
  623. RX_DECL int
  624. rx_bitset_empty (size, set)
  625.      int size;
  626.      rx_Bitset set;
  627. #endif
  628. {
  629.   int x;
  630.   RX_subset s = set[0];
  631.   set[0] = 1;
  632.   for (x = rx_bitset_numb_subsets(size) - 1; !set[x]; --x)
  633.     ;
  634.   set[0] = s;
  635.   return !s;
  636. }
  637. #endif
  638.  
  639. #ifdef __STDC__
  640. RX_DECL void
  641. rx_bitset_null (int size, rx_Bitset b)
  642. #else
  643. RX_DECL void
  644. rx_bitset_null (size, b)
  645.      int size;
  646.      rx_Bitset b;
  647. #endif
  648. {
  649.   bzero (b, rx_sizeof_bitset(size));
  650. }
  651.  
  652.  
  653. #ifdef __STDC__
  654. RX_DECL void
  655. rx_bitset_universe (int size, rx_Bitset b)
  656. #else
  657. RX_DECL void
  658. rx_bitset_universe (size, b)
  659.      int size;
  660.      rx_Bitset b;
  661. #endif
  662. {
  663.   int x = rx_bitset_numb_subsets (size);
  664.   while (x--)
  665.     *b++ = ~(RX_subset)0;
  666. }
  667.  
  668.  
  669. #ifdef __STDC__
  670. RX_DECL void
  671. rx_bitset_complement (int size, rx_Bitset b)
  672. #else
  673. RX_DECL void
  674. rx_bitset_complement (size, b)
  675.      int size;
  676.      rx_Bitset b;
  677. #endif
  678. {
  679.   int x = rx_bitset_numb_subsets (size);
  680.   while (x--)
  681.     {
  682.       *b = ~*b;
  683.       ++b;
  684.     }
  685. }
  686.  
  687.  
  688. #ifdef __STDC__
  689. RX_DECL void
  690. rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b)
  691. #else
  692. RX_DECL void
  693. rx_bitset_assign (size, a, b)
  694.      int size;
  695.      rx_Bitset a;
  696.      rx_Bitset b;
  697. #endif
  698. {
  699.   int x;
  700.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  701.     a[x] = b[x];
  702. }
  703.  
  704.  
  705. #ifdef __STDC__
  706. RX_DECL void
  707. rx_bitset_union (int size, rx_Bitset a, rx_Bitset b)
  708. #else
  709. RX_DECL void
  710. rx_bitset_union (size, a, b)
  711.      int size;
  712.      rx_Bitset a;
  713.      rx_Bitset b;
  714. #endif
  715. {
  716.   int x;
  717.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  718.     a[x] |= b[x];
  719. }
  720.  
  721.  
  722. #ifdef __STDC__
  723. RX_DECL void
  724. rx_bitset_intersection (int size,
  725.             rx_Bitset a, rx_Bitset b)
  726. #else
  727. RX_DECL void
  728. rx_bitset_intersection (size, a, b)
  729.      int size;
  730.      rx_Bitset a;
  731.      rx_Bitset b;
  732. #endif
  733. {
  734.   int x;
  735.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  736.     a[x] &= b[x];
  737. }
  738.  
  739.  
  740. #ifdef __STDC__
  741. RX_DECL void
  742. rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b)
  743. #else
  744. RX_DECL void
  745. rx_bitset_difference (size, a, b)
  746.      int size;
  747.      rx_Bitset a;
  748.      rx_Bitset b;
  749. #endif
  750. {
  751.   int x;
  752.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  753.     a[x] &=  ~ b[x];
  754. }
  755.  
  756.  
  757. #if 0
  758. #ifdef __STDC__
  759. RX_DECL void
  760. rx_bitset_revdifference (int size,
  761.              rx_Bitset a, rx_Bitset b)
  762. #else
  763. RX_DECL void
  764. rx_bitset_revdifference (size, a, b)
  765.      int size;
  766.      rx_Bitset a;
  767.      rx_Bitset b;
  768. #endif
  769. {
  770.   int x;
  771.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  772.     a[x] = ~a[x] & b[x];
  773. }
  774.  
  775. #ifdef __STDC__
  776. RX_DECL void
  777. rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b)
  778. #else
  779. RX_DECL void
  780. rx_bitset_xor (size, a, b)
  781.      int size;
  782.      rx_Bitset a;
  783.      rx_Bitset b;
  784. #endif
  785. {
  786.   int x;
  787.   for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x)
  788.     a[x] ^= b[x];
  789. }
  790. #endif
  791.  
  792.  
  793. #ifdef __STDC__
  794. RX_DECL unsigned long
  795. rx_bitset_hash (int size, rx_Bitset b)
  796. #else
  797. RX_DECL unsigned long
  798. rx_bitset_hash (size, b)
  799.      int size;
  800.      rx_Bitset b;
  801. #endif
  802. {
  803.   int x;
  804.   unsigned long hash = (unsigned long)rx_bitset_hash;
  805.  
  806.   for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  807.     hash ^= rx_bitset_subset_val(b, x);
  808.  
  809.   return hash;
  810. }
  811.  
  812.  
  813. RX_DECL RX_subset rx_subset_singletons [RX_subset_bits] = 
  814. {
  815.   0x1,
  816.   0x2,
  817.   0x4,
  818.   0x8,
  819.   0x10,
  820.   0x20,
  821.   0x40,
  822.   0x80,
  823.   0x100,
  824.   0x200,
  825.   0x400,
  826.   0x800,
  827.   0x1000,
  828.   0x2000,
  829.   0x4000,
  830.   0x8000,
  831.   0x10000,
  832.   0x20000,
  833.   0x40000,
  834.   0x80000,
  835.   0x100000,
  836.   0x200000,
  837.   0x400000,
  838.   0x800000,
  839.   0x1000000,
  840.   0x2000000,
  841.   0x4000000,
  842.   0x8000000,
  843.   0x10000000,
  844.   0x20000000,
  845.   0x40000000,
  846.   0x80000000
  847. };
  848.  
  849. #ifdef RX_DEBUG
  850.  
  851. #ifdef __STDC__
  852. static void
  853. print_cset (struct rx *rx, rx_Bitset cset, FILE * fp)
  854. #else
  855. static void
  856. print_cset (rx, cset, fp)
  857.      struct rx *rx;
  858.      rx_Bitset cset;
  859.      FILE * fp;
  860. #endif
  861. {
  862.   int x;
  863.   fputc ('[', fp);
  864.   for (x = 0; x < rx->local_cset_size; ++x)
  865.     if (isprint(x) && RX_bitset_member (cset, x))
  866.       fputc (x, fp);
  867.   fputc (']', fp);
  868. }
  869.  
  870. #endif /*  RX_DEBUG */
  871.  
  872.  
  873.  
  874. /* This page: small object pools.
  875.  */
  876.  
  877. struct freelist
  878. {
  879.   struct freelist * next;
  880. };
  881.  
  882. struct chunked_pool
  883. {
  884.   int size;
  885.   struct freelist * freelist;
  886.   char * chunk;
  887.   int num_left;
  888. #if RX_DEBUG
  889.   int leakiness;
  890. #endif
  891. };
  892.  
  893. #define DEF_POOL(NAME,TYPE) \
  894.   struct chunked_pool NAME = { sizeof(TYPE), 0, 0, 0 }
  895.  
  896.  
  897. #ifdef __STDC__
  898. static char *
  899. chunk_malloc (struct chunked_pool * pool)
  900. #else
  901. static char *
  902. chunk_malloc (pool)
  903.      struct chunked_pool * pool;
  904. #endif
  905. {
  906.   struct freelist * it;
  907.   if (pool->freelist)
  908.     {
  909.       it = pool->freelist;
  910.       pool->freelist = it->next;
  911.     }
  912.   else
  913.     {
  914.       if (!pool->num_left)
  915.     {
  916.       pool->chunk = (char *)malloc (pool->size * 128);
  917.       if (!pool->chunk)
  918.         return 0;
  919.       pool->num_left = 128;
  920.     }
  921.       it = (struct freelist *)pool->chunk;
  922.       pool->chunk += pool->size;
  923.       --pool->num_left;
  924.     }
  925. #if RX_DEBUG
  926.   if (it)
  927.     ++pool->leakiness;
  928. #endif
  929.   return (char *)it;
  930. }
  931.  
  932.  
  933. #ifdef __STDC__
  934. static void
  935. chunk_free (struct chunked_pool * pool, char * mem)
  936. #else
  937. static void
  938. chunk_free (pool, mem)
  939.      struct chunked_pool * pool;
  940.      char * mem;
  941. #endif
  942. {
  943.   struct freelist * it = (struct freelist *)mem;
  944.   it->next = pool->freelist;
  945.   pool->freelist = it;
  946. #if RX_DEBUG
  947.   --pool->leakiness;
  948. #endif
  949. }
  950.  
  951. /* This is for pools of variable size objects where there are only a few
  952.  * sizes and where we tend to burst on one size at a time.
  953.  */
  954.  
  955. struct linked_chunk
  956. {
  957.   struct chunked_pool pool;
  958.   struct linked_chunk * next;
  959. };
  960.  
  961. struct chunk_group
  962. {
  963.   struct linked_chunk * list;
  964.   struct linked_chunk * current;
  965. };
  966.  
  967.  
  968. #ifdef __STDC__
  969. static int
  970. cg_find_pool (struct chunk_group * group, int size)
  971. #else
  972. static int
  973. cg_find_pool (group, size)
  974.      struct chunk_group * group;
  975.      int size;
  976. #endif
  977. {
  978.   if (!(group->current && (group->current->pool.size == size)))
  979.     {
  980.       struct linked_chunk * lc = group->list;
  981.       while (lc)
  982.     if (lc->pool.size == size)
  983.       {
  984.         group->current = lc;
  985.         return 1;
  986.       }
  987.       else
  988.     lc = lc->next;
  989.       lc = (struct linked_chunk *)malloc (sizeof (*lc));
  990.       if (!lc)
  991.     return 0;
  992.       bzero (lc, sizeof (*lc));
  993.       lc->pool.size = size;
  994.       lc->next = group->list;
  995.       group->list = lc;
  996.       group->current = lc;
  997.     }
  998.   return 1;
  999. }
  1000.  
  1001.  
  1002. #ifdef __STDC__
  1003. static char *
  1004. cg_malloc (struct chunk_group * group, int size)
  1005. #else
  1006. static char *
  1007. cg_malloc (group, size)
  1008.      struct chunk_group * group;
  1009.      int size;
  1010. #endif
  1011. {
  1012.   return (cg_find_pool (group, size)
  1013.       ?  chunk_malloc (&group->current->pool)
  1014.       : 0);
  1015. }
  1016.  
  1017.  
  1018. #ifdef __STDC__
  1019. static void
  1020. cg_free (struct chunk_group * group, int size, char * mem)
  1021. #else
  1022. static void
  1023. cg_free (group, size, mem)
  1024.      struct chunk_group * group;
  1025.      int size;
  1026.      char * mem;
  1027. #endif
  1028. {
  1029.   if (cg_find_pool (group, size))
  1030.     chunk_free (&group->current->pool, mem);
  1031. }
  1032.  
  1033.  
  1034.  
  1035. static unsigned long rx_hash_masks[4] =
  1036. {
  1037.   0x12488421,
  1038.   0x96699669,
  1039.   0xbe7dd7eb,
  1040.   0xffffffff
  1041. };
  1042.  
  1043.  
  1044. /* Hash tables */
  1045. #ifdef __STDC__
  1046. RX_DECL struct rx_hash_item * 
  1047. rx_hash_find (struct rx_hash * table,
  1048.           unsigned long hash,
  1049.           void * value,
  1050.           struct rx_hash_rules * rules)
  1051. #else
  1052. RX_DECL struct rx_hash_item * 
  1053. rx_hash_find (table, hash, value, rules)
  1054.      struct rx_hash * table;
  1055.      unsigned long hash;
  1056.      void * value;
  1057.      struct rx_hash_rules * rules;
  1058. #endif
  1059. {
  1060.   rx_hash_eq eq = rules->eq;
  1061.   int maskc = 0;
  1062.   int mask = rx_hash_masks [0];
  1063.   int bucket = (hash & mask) % 13;
  1064.  
  1065.   while (table->children [bucket])
  1066.     {
  1067.       table = table->children [bucket];
  1068.       ++maskc;
  1069.       mask = rx_hash_masks[maskc];
  1070.       bucket = (hash & mask) % 13;
  1071.     }
  1072.  
  1073.   {
  1074.     struct rx_hash_item * it = table->buckets[bucket];
  1075.     while (it)
  1076.       if (eq (it->data, value))
  1077.     return it;
  1078.       else
  1079.     it = it->next_same_hash;
  1080.   }
  1081.  
  1082.   return 0;
  1083. }
  1084.  
  1085. #ifdef __STDC__
  1086. RX_DECL struct rx_hash_item *
  1087. rx_hash_store (struct rx_hash * table,
  1088.            unsigned long hash,
  1089.            void * value,
  1090.            struct rx_hash_rules * rules)
  1091. #else
  1092. RX_DECL struct rx_hash_item *
  1093. rx_hash_store (table, hash, value, rules)
  1094.      struct rx_hash * table;
  1095.      unsigned long hash;
  1096.      void * value;
  1097.      struct rx_hash_rules * rules;
  1098. #endif
  1099. {
  1100.   rx_hash_eq eq = rules->eq;
  1101.   int maskc = 0;
  1102.   int mask = rx_hash_masks[0];
  1103.   int bucket = (hash & mask) % 13;
  1104.   int depth = 0;
  1105.   
  1106.   while (table->children [bucket])
  1107.     {
  1108.       table = table->children [bucket];
  1109.       ++maskc;
  1110.       mask = rx_hash_masks[maskc];
  1111.       bucket = (hash & mask) % 13;
  1112.       ++depth;
  1113.     }
  1114.   
  1115.   {
  1116.     struct rx_hash_item * it = table->buckets[bucket];
  1117.     while (it)
  1118.       if (eq (it->data, value))
  1119.     return it;
  1120.       else
  1121.     it = it->next_same_hash;
  1122.   }
  1123.   
  1124.   {
  1125.     if (   (depth < 3)
  1126.     && (table->bucket_size [bucket] >= 4))
  1127.       {
  1128.     struct rx_hash * newtab = ((struct rx_hash *)
  1129.                    rules->hash_alloc (rules));
  1130.     if (!newtab)
  1131.       goto add_to_bucket;
  1132.     bzero (newtab, sizeof (*newtab));
  1133.     newtab->parent = table;
  1134.     {
  1135.       struct rx_hash_item * them = table->buckets[bucket];
  1136.       unsigned long newmask = rx_hash_masks[maskc + 1];
  1137.       while (them)
  1138.         {
  1139.           struct rx_hash_item * save = them->next_same_hash;
  1140.           int new_buck = (them->hash & newmask) % 13;
  1141.           them->next_same_hash = newtab->buckets[new_buck];
  1142.           newtab->buckets[new_buck] = them;
  1143.           them->table = newtab;
  1144.           them = save;
  1145.           ++newtab->bucket_size[new_buck];
  1146.           ++newtab->refs;
  1147.         }
  1148.       table->refs = (table->refs - table->bucket_size[bucket] + 1);
  1149.       table->bucket_size[bucket] = 0;
  1150.       table->buckets[bucket] = 0;
  1151.       table->children[bucket] = newtab;
  1152.       table = newtab;
  1153.       bucket = (hash & newmask) % 13;
  1154.     }
  1155.       }
  1156.   }
  1157.  add_to_bucket:
  1158.   {
  1159.     struct rx_hash_item  * it = ((struct rx_hash_item *)
  1160.                  rules->hash_item_alloc (rules, value));
  1161.     if (!it)
  1162.       return 0;
  1163.     it->hash = hash;
  1164.     it->table = table;
  1165.     /* DATA and BINDING are to be set in hash_item_alloc */
  1166.     it->next_same_hash = table->buckets [bucket];
  1167.     table->buckets[bucket] = it;
  1168.     ++table->bucket_size [bucket];
  1169.     ++table->refs;
  1170.     return it;
  1171.   }
  1172. }
  1173.  
  1174. #ifdef __STDC__
  1175. RX_DECL void
  1176. rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules)
  1177. #else
  1178. RX_DECL void
  1179. rx_hash_free (it, rules)
  1180.      struct rx_hash_item * it;
  1181.      struct rx_hash_rules * rules;
  1182. #endif
  1183. {
  1184.   if (it)
  1185.     {
  1186.       struct rx_hash * table = it->table;
  1187.       unsigned long hash = it->hash;
  1188.       int depth = (table->parent
  1189.            ? (table->parent->parent
  1190.               ? (table->parent->parent->parent
  1191.              ? 3
  1192.              : 2)
  1193.               : 1)
  1194.            : 0);
  1195.       int bucket = (hash & rx_hash_masks [depth]) % 13;
  1196.       struct rx_hash_item ** pos = &table->buckets [bucket];
  1197.       
  1198.       while (*pos != it)
  1199.     pos = &(*pos)->next_same_hash;
  1200.       *pos = it->next_same_hash;
  1201.       rules->free_hash_item (it, rules);
  1202.       --table->bucket_size[bucket];
  1203.       --table->refs;
  1204.       while (!table->refs && depth)
  1205.     {
  1206.       struct rx_hash * save = table;
  1207.       table = table->parent;
  1208.       --depth;
  1209.       bucket = (hash & rx_hash_masks [depth]) % 13;
  1210.       --table->refs;
  1211.       table->children[bucket] = 0;
  1212.       rules->free_hash (save, rules);
  1213.     }
  1214.     }
  1215. }
  1216.  
  1217. #ifdef __STDC__
  1218. typedef void (*rx_hash_freefn) (struct rx_hash_item * it);
  1219. #else /* ndef __STDC__ */
  1220. typedef void (*rx_hash_freefn) ();
  1221. #endif /* ndef __STDC__ */
  1222.  
  1223. #ifdef __STDC__
  1224. RX_DECL void
  1225. rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
  1226.             struct rx_hash_rules * rules)
  1227. #else
  1228. RX_DECL void
  1229. rx_free_hash_table (tab, freefn, rules)
  1230.      struct rx_hash * tab;
  1231.      rx_hash_freefn freefn;
  1232.      struct rx_hash_rules * rules;
  1233. #endif
  1234. {
  1235.   int x;
  1236.  
  1237.   for (x = 0; x < 13; ++x)
  1238.     if (tab->children[x])
  1239.       {
  1240.     rx_free_hash_table (tab->children[x], freefn, rules);
  1241.     rules->free_hash (tab->children[x], rules);
  1242.       }
  1243.     else
  1244.       {
  1245.     struct rx_hash_item * them = tab->buckets[x];
  1246.     while (them)
  1247.       {
  1248.         struct rx_hash_item * that = them;
  1249.         them = that->next_same_hash;
  1250.         freefn (that);
  1251.         rules->free_hash_item (that, rules);
  1252.       }
  1253.       }
  1254. }
  1255.  
  1256.  
  1257.  
  1258. /* Utilities for manipulating bitset represntations of characters sets. */
  1259.  
  1260. static struct chunk_group cset_chunks = {0, 0};
  1261.  
  1262. #ifdef __STDC__
  1263. RX_DECL rx_Bitset
  1264. rx_cset (struct rx *rx)
  1265. #else
  1266. RX_DECL rx_Bitset
  1267. rx_cset (rx)
  1268.      struct rx *rx;
  1269. #endif
  1270. {
  1271.   rx_Bitset b = (rx_Bitset) cg_malloc (&cset_chunks,
  1272.                        rx_sizeof_bitset (rx->local_cset_size));
  1273.   if (b)
  1274.     rx_bitset_null (rx->local_cset_size, b);
  1275.   return b;
  1276. }
  1277.  
  1278.  
  1279. #ifdef __STDC__
  1280. RX_DECL rx_Bitset
  1281. rx_copy_cset (struct rx *rx, rx_Bitset a)
  1282. #else
  1283. RX_DECL rx_Bitset
  1284. rx_copy_cset (rx, a)
  1285.      struct rx *rx;
  1286.      rx_Bitset a;
  1287. #endif
  1288. {
  1289.   rx_Bitset cs = rx_cset (rx);
  1290.  
  1291.   if (cs)
  1292.     rx_bitset_union (rx->local_cset_size, cs, a);
  1293.  
  1294.   return cs;
  1295. }
  1296.  
  1297.  
  1298. #ifdef __STDC__
  1299. RX_DECL void
  1300. rx_free_cset (struct rx * rx, rx_Bitset c)
  1301. #else
  1302. RX_DECL void
  1303. rx_free_cset (rx, c)
  1304.      struct rx * rx;
  1305.      rx_Bitset c;
  1306. #endif
  1307. {
  1308.   if (c)
  1309.     cg_free (&cset_chunks, rx_sizeof_bitset (rx->local_cset_size), (char *)c);
  1310. }
  1311.  
  1312.  
  1313. /* Hash table memory allocation policy for the regexp compiler */
  1314.  
  1315. static DEF_POOL(hash_tabs, struct rx_hash);
  1316. static DEF_POOL(hash_items, struct rx_hash_item);
  1317.  
  1318. #ifdef __STDC__
  1319. struct rx_hash *
  1320. compiler_hash_alloc (struct rx_hash_rules * rules)
  1321. #else
  1322. struct rx_hash *
  1323. compiler_hash_alloc (rules)
  1324.      struct rx_hash_rules * rules;
  1325. #endif
  1326. {
  1327.   return (struct rx_hash *)chunk_malloc (&hash_tabs);
  1328. }
  1329.  
  1330. #ifdef __STDC__
  1331. struct rx_hash_item *
  1332. compiler_hash_item_alloc (struct rx_hash_rules * rules, void * value)
  1333. #else
  1334. struct rx_hash_item *
  1335. compiler_hash_item_alloc (rules, value)
  1336.      struct rx_hash_rules * rules;
  1337.      void * value;
  1338. #endif
  1339. {
  1340.   struct rx_hash_item * it = (struct rx_hash_item *)chunk_malloc (&hash_items);
  1341.   if (it)
  1342.     {
  1343.       it->data = value;
  1344.       it->binding = 0;
  1345.     }
  1346.   return it;
  1347. }
  1348.  
  1349. #ifdef __STDC__
  1350. void
  1351. compiler_free_hash (struct rx_hash * tab,
  1352.             struct rx_hash_rules * rules)
  1353. #else
  1354. void
  1355. compiler_free_hash (tab, rules)
  1356.      struct rx_hash * tab;
  1357.      struct rx_hash_rules * rules;
  1358. #endif
  1359. {
  1360.   chunk_free (&hash_tabs, (char *)tab);
  1361. }
  1362.  
  1363. #ifdef __STDC__
  1364. void
  1365. compiler_free_hash_item (struct rx_hash_item * item,
  1366.              struct rx_hash_rules * rules)
  1367. #else
  1368. void
  1369. compiler_free_hash_item (item, rules)
  1370.      struct rx_hash_item * item;
  1371.      struct rx_hash_rules * rules;
  1372. #endif
  1373. {
  1374.   chunk_free (&hash_items, (char *)item);
  1375. }
  1376.  
  1377.  
  1378. /* This page: REXP_NODE (expression tree) structures. */
  1379.  
  1380. static DEF_POOL (rexp_pool, struct rexp_node);
  1381.  
  1382. #ifdef __STDC__
  1383. RX_DECL struct rexp_node *
  1384. rexp_node (struct rx *rx,
  1385.        enum rexp_node_type type)
  1386. #else
  1387. RX_DECL struct rexp_node *
  1388. rexp_node (rx, type)
  1389.      struct rx *rx;
  1390.      enum rexp_node_type type;
  1391. #endif
  1392. {
  1393.   struct rexp_node *n;
  1394.  
  1395.   n = (struct rexp_node *)chunk_malloc (&rexp_pool);
  1396.   bzero (n, sizeof (*n));
  1397.   if (n)
  1398.     n->type = type;
  1399.   return n;
  1400. }
  1401.  
  1402.  
  1403. /* free_rexp_node assumes that the bitset passed to rx_mk_r_cset
  1404.  * can be freed using rx_free_cset.
  1405.  */
  1406. #ifdef __STDC__
  1407. RX_DECL struct rexp_node *
  1408. rx_mk_r_cset (struct rx * rx,
  1409.           rx_Bitset b)
  1410. #else
  1411. RX_DECL struct rexp_node *
  1412. rx_mk_r_cset (rx, b)
  1413.      struct rx * rx;
  1414.      rx_Bitset b;
  1415. #endif
  1416. {
  1417.   struct rexp_node * n = rexp_node (rx, r_cset);
  1418.   if (n)
  1419.     n->params.cset = b;
  1420.   return n;
  1421. }
  1422.  
  1423.  
  1424. #ifdef __STDC__
  1425. RX_DECL struct rexp_node *
  1426. rx_mk_r_concat (struct rx * rx,
  1427.         struct rexp_node * a,
  1428.         struct rexp_node * b)
  1429. #else
  1430. RX_DECL struct rexp_node *
  1431. rx_mk_r_concat (rx, a, b)
  1432.      struct rx * rx;
  1433.      struct rexp_node * a;
  1434.      struct rexp_node * b;
  1435. #endif
  1436. {
  1437.   struct rexp_node * n = rexp_node (rx, r_concat);
  1438.   if (n)
  1439.     {
  1440.       n->params.pair.left = a;
  1441.       n->params.pair.right = b;
  1442.     }
  1443.   return n;
  1444. }
  1445.  
  1446.  
  1447. #ifdef __STDC__
  1448. RX_DECL struct rexp_node *
  1449. rx_mk_r_alternate (struct rx * rx,
  1450.            struct rexp_node * a,
  1451.            struct rexp_node * b)
  1452. #else
  1453. RX_DECL struct rexp_node *
  1454. rx_mk_r_alternate (rx, a, b)
  1455.      struct rx * rx;
  1456.      struct rexp_node * a;
  1457.      struct rexp_node * b;
  1458. #endif
  1459. {
  1460.   struct rexp_node * n = rexp_node (rx, r_alternate);
  1461.   if (n)
  1462.     {
  1463.       n->params.pair.left = a;
  1464.       n->params.pair.right = b;
  1465.     }
  1466.   return n;
  1467. }
  1468.  
  1469.  
  1470. #ifdef __STDC__
  1471. RX_DECL struct rexp_node *
  1472. rx_mk_r_opt (struct rx * rx,
  1473.          struct rexp_node * a)
  1474. #else
  1475. RX_DECL struct rexp_node *
  1476. rx_mk_r_opt (rx, a)
  1477.      struct rx * rx;
  1478.      struct rexp_node * a;
  1479. #endif
  1480. {
  1481.   struct rexp_node * n = rexp_node (rx, r_opt);
  1482.   if (n)
  1483.     {
  1484.       n->params.pair.left = a;
  1485.       n->params.pair.right = 0;
  1486.     }
  1487.   return n;
  1488. }
  1489.  
  1490.  
  1491. #ifdef __STDC__
  1492. RX_DECL struct rexp_node *
  1493. rx_mk_r_star (struct rx * rx,
  1494.           struct rexp_node * a)
  1495. #else
  1496. RX_DECL struct rexp_node *
  1497. rx_mk_r_star (rx, a)
  1498.      struct rx * rx;
  1499.      struct rexp_node * a;
  1500. #endif
  1501. {
  1502.   struct rexp_node * n = rexp_node (rx, r_star);
  1503.   if (n)
  1504.     {
  1505.       n->params.pair.left = a;
  1506.       n->params.pair.right = 0;
  1507.     }
  1508.   return n;
  1509. }
  1510.  
  1511.  
  1512. #ifdef __STDC__
  1513. RX_DECL struct rexp_node *
  1514. rx_mk_r_2phase_star (struct rx * rx,
  1515.              struct rexp_node * a,
  1516.              struct rexp_node * b)
  1517. #else
  1518. RX_DECL struct rexp_node *
  1519. rx_mk_r_2phase_star (rx, a, b)
  1520.      struct rx * rx;
  1521.      struct rexp_node * a;
  1522.      struct rexp_node * b;
  1523. #endif
  1524. {
  1525.   struct rexp_node * n = rexp_node (rx, r_2phase_star);
  1526.   if (n)
  1527.     {
  1528.       n->params.pair.left = a;
  1529.       n->params.pair.right = b;
  1530.     }
  1531.   return n;
  1532. }
  1533.  
  1534.  
  1535.  
  1536. #ifdef __STDC__
  1537. RX_DECL struct rexp_node *
  1538. rx_mk_r_side_effect (struct rx * rx,
  1539.              rx_side_effect a)
  1540. #else
  1541. RX_DECL struct rexp_node *
  1542. rx_mk_r_side_effect (rx, a)
  1543.      struct rx * rx;
  1544.      rx_side_effect a;
  1545. #endif
  1546. {
  1547.   struct rexp_node * n = rexp_node (rx, r_side_effect);
  1548.   if (n)
  1549.     {
  1550.       n->params.side_effect = a;
  1551.       n->params.pair.right = 0;
  1552.     }
  1553.   return n;
  1554. }
  1555.  
  1556.  
  1557. #ifdef __STDC__
  1558. RX_DECL struct rexp_node *
  1559. rx_mk_r_data  (struct rx * rx,
  1560.            void * a)
  1561. #else
  1562. RX_DECL struct rexp_node *
  1563. rx_mk_r_data  (rx, a)
  1564.      struct rx * rx;
  1565.      void * a;
  1566. #endif
  1567. {
  1568.   struct rexp_node * n = rexp_node (rx, r_data);
  1569.   if (n)
  1570.     {
  1571.       n->params.pair.left = a;
  1572.       n->params.pair.right = 0;
  1573.     }
  1574.   return n;
  1575. }
  1576.  
  1577.  
  1578. #ifdef __STDC__
  1579. RX_DECL void
  1580. rx_free_rexp (struct rx * rx, struct rexp_node * node)
  1581. #else
  1582. RX_DECL void
  1583. rx_free_rexp (rx, node)
  1584.      struct rx * rx;
  1585.      struct rexp_node * node;
  1586. #endif
  1587. {
  1588.   if (node)
  1589.     {
  1590.       switch (node->type)
  1591.     {
  1592.     case r_cset:
  1593.       if (node->params.cset)
  1594.         rx_free_cset (rx, node->params.cset);
  1595.  
  1596.     case r_side_effect:
  1597.       break;
  1598.       
  1599.     case r_concat:
  1600.     case r_alternate:
  1601.     case r_2phase_star:
  1602.     case r_opt:
  1603.     case r_star:
  1604.       rx_free_rexp (rx, node->params.pair.left);
  1605.       rx_free_rexp (rx, node->params.pair.right);
  1606.       break;
  1607.  
  1608.     case r_data:
  1609.       /* This shouldn't occur. */
  1610.       break;
  1611.     }
  1612.       chunk_free (&rexp_pool, (char *)node);
  1613.     }
  1614. }
  1615.  
  1616.  
  1617. #ifdef __STDC__
  1618. RX_DECL struct rexp_node * 
  1619. rx_copy_rexp (struct rx *rx,
  1620.        struct rexp_node *node)
  1621. #else
  1622. RX_DECL struct rexp_node * 
  1623. rx_copy_rexp (rx, node)
  1624.      struct rx *rx;
  1625.      struct rexp_node *node;
  1626. #endif
  1627. {
  1628.   if (!node)
  1629.     return 0;
  1630.   else
  1631.     {
  1632.       struct rexp_node *n = rexp_node (rx, node->type);
  1633.       if (!n)
  1634.     return 0;
  1635.       switch (node->type)
  1636.     {
  1637.     case r_cset:
  1638.       n->params.cset = rx_copy_cset (rx, node->params.cset);
  1639.       if (!n->params.cset)
  1640.         {
  1641.           rx_free_rexp (rx, n);
  1642.           return 0;
  1643.         }
  1644.       break;
  1645.  
  1646.     case r_side_effect:
  1647.       n->params.side_effect = node->params.side_effect;
  1648.       break;
  1649.  
  1650.     case r_concat:
  1651.     case r_alternate:
  1652.     case r_opt:
  1653.     case r_2phase_star:
  1654.     case r_star:
  1655.       n->params.pair.left =
  1656.         rx_copy_rexp (rx, node->params.pair.left);
  1657.       n->params.pair.right =
  1658.         rx_copy_rexp (rx, node->params.pair.right);
  1659.       if (   (node->params.pair.left && !n->params.pair.left)
  1660.           || (node->params.pair.right && !n->params.pair.right))
  1661.         {
  1662.           rx_free_rexp  (rx, n);
  1663.           return 0;
  1664.         }
  1665.       break;
  1666.     case r_data:
  1667.       /* shouldn't happen */
  1668.       break;
  1669.     }
  1670.       return n;
  1671.     }
  1672. }
  1673.  
  1674.  
  1675.  
  1676. /* This page: functions to build and destroy graphs that describe nfa's */
  1677.  
  1678. static DEF_POOL(state_pool, struct rx_nfa_state);
  1679.  
  1680. /* Constructs a new nfa node. */
  1681. #ifdef __STDC__
  1682. RX_DECL struct rx_nfa_state *
  1683. rx_nfa_state (struct rx *rx)
  1684. #else
  1685. RX_DECL struct rx_nfa_state *
  1686. rx_nfa_state (rx)
  1687.      struct rx *rx;
  1688. #endif
  1689. {
  1690.   struct rx_nfa_state * n = (struct rx_nfa_state *)chunk_malloc (&state_pool);
  1691.   if (!n)
  1692.     return 0;
  1693.   bzero (n, sizeof (*n));
  1694.   n->next = rx->nfa_states;
  1695.   rx->nfa_states = n;
  1696.   return n;
  1697. }
  1698.  
  1699.  
  1700. #ifdef __STDC__
  1701. RX_DECL void
  1702. rx_free_nfa_state (struct rx_nfa_state * n)
  1703. #else
  1704. RX_DECL void
  1705. rx_free_nfa_state (n)
  1706.   struct rx_nfa_state * n;
  1707. #endif
  1708. {
  1709.   chunk_free (&state_pool, (char *)n);
  1710. }
  1711.  
  1712.  
  1713. /* This looks up an nfa node, given a numeric id.  Numeric id's are
  1714.  * assigned after the nfa has been built.
  1715.  */
  1716. #ifdef __STDC__
  1717. RX_DECL struct rx_nfa_state * 
  1718. rx_id_to_nfa_state (struct rx * rx,
  1719.             int id)
  1720. #else
  1721. RX_DECL struct rx_nfa_state * 
  1722. rx_id_to_nfa_state (rx, id)
  1723.      struct rx * rx;
  1724.      int id;
  1725. #endif
  1726. {
  1727.   struct rx_nfa_state * n;
  1728.   for (n = rx->nfa_states; n; n = n->next)
  1729.     if (n->id == id)
  1730.       return n;
  1731.   return 0;
  1732. }
  1733.  
  1734.  
  1735. /* This adds an edge between two nodes, but doesn't initialize the 
  1736.  * edge label.
  1737.  */
  1738.  
  1739. static DEF_POOL(edge_pool, struct rx_nfa_edge);
  1740.  
  1741. #ifdef __STDC__
  1742. RX_DECL struct rx_nfa_edge * 
  1743. rx_nfa_edge (struct rx *rx,
  1744.          enum rx_nfa_etype type,
  1745.          struct rx_nfa_state *start,
  1746.          struct rx_nfa_state *dest)
  1747. #else
  1748. RX_DECL struct rx_nfa_edge * 
  1749. rx_nfa_edge (rx, type, start, dest)
  1750.      struct rx *rx;
  1751.      enum rx_nfa_etype type;
  1752.      struct rx_nfa_state *start;
  1753.      struct rx_nfa_state *dest;
  1754. #endif
  1755. {
  1756.   struct rx_nfa_edge *e = (struct rx_nfa_edge *)chunk_malloc (&edge_pool);
  1757.   if (!e)
  1758.     return 0;
  1759.   e->next = start->edges;
  1760.   start->edges = e;
  1761.   e->type = type;
  1762.   e->dest = dest;
  1763.   return e;
  1764. }
  1765.  
  1766.  
  1767. #ifdef __STDC__
  1768. RX_DECL void
  1769. rx_free_nfa_edge (struct rx_nfa_edge * e)
  1770. #else
  1771. RX_DECL void
  1772. rx_free_nfa_edge (e)
  1773.      struct rx_nfa_edge * e;
  1774. #endif
  1775. {
  1776.   chunk_free (&edge_pool, (char *)e);
  1777. }
  1778.  
  1779.  
  1780. /* This constructs a POSSIBLE_FUTURE, which is a kind epsilon-closure
  1781.  * of an NFA.  These are added to an nfa automaticly by eclose_nfa.
  1782.  */  
  1783.  
  1784. static DEF_POOL(pf_pool, struct rx_possible_future);
  1785.  
  1786. #ifdef __STDC__
  1787. static struct rx_possible_future * 
  1788. rx_possible_future (struct rx * rx,
  1789.          struct rx_se_list * effects)
  1790. #else
  1791. static struct rx_possible_future * 
  1792. rx_possible_future (rx, effects)
  1793.      struct rx * rx;
  1794.      struct rx_se_list * effects;
  1795. #endif
  1796. {
  1797.   struct rx_possible_future *ec = ((struct rx_possible_future *)
  1798.                 chunk_malloc (&pf_pool));
  1799.   if (!ec)
  1800.     return 0;
  1801.   ec->destset = 0;
  1802.   ec->next = 0;
  1803.   ec->effects = effects;
  1804.   return ec;
  1805. }
  1806.  
  1807.  
  1808. #ifdef __STDC__
  1809. static void
  1810. rx_free_possible_future (struct rx_possible_future * pf)
  1811. #else
  1812. static void
  1813. rx_free_possible_future (pf)
  1814.      struct rx_possible_future * pf;
  1815. #endif
  1816. {
  1817.   chunk_free (&pf_pool, (char *)pf);
  1818. }
  1819.  
  1820.  
  1821. #ifdef __STDC__
  1822. RX_DECL void
  1823. rx_free_nfa (struct rx *rx)
  1824. #else
  1825. RX_DECL void
  1826. rx_free_nfa (rx)
  1827.      struct rx *rx;
  1828. #endif
  1829. {
  1830.   while (rx->nfa_states)
  1831.     {
  1832.       while (rx->nfa_states->edges)
  1833.     {
  1834.       switch (rx->nfa_states->edges->type)
  1835.         {
  1836.         case ne_cset:
  1837.           rx_free_cset (rx, rx->nfa_states->edges->params.cset);
  1838.           break;
  1839.         default:
  1840.           break;
  1841.         }
  1842.       {
  1843.         struct rx_nfa_edge * e;
  1844.         e = rx->nfa_states->edges;
  1845.         rx->nfa_states->edges = rx->nfa_states->edges->next;
  1846.         rx_free_nfa_edge (e);
  1847.       }
  1848.     } /* while (rx->nfa_states->edges) */
  1849.       {
  1850.     /* Iterate over the partial epsilon closures of rx->nfa_states */
  1851.     struct rx_possible_future * pf = rx->nfa_states->futures;
  1852.     while (pf)
  1853.       {
  1854.         struct rx_possible_future * pft = pf;
  1855.         pf = pf->next;
  1856.         rx_free_possible_future (pft);
  1857.       }
  1858.       }
  1859.       {
  1860.     struct rx_nfa_state *n;
  1861.     n = rx->nfa_states;
  1862.     rx->nfa_states = rx->nfa_states->next;
  1863.     rx_free_nfa_state (n);
  1864.       }
  1865.     }
  1866. }
  1867.  
  1868.  
  1869.  
  1870. /* This page: translating a pattern expression in to an nfa and doing the 
  1871.  * static part of the nfa->super-nfa translation.
  1872.  */
  1873.  
  1874. /* This is the thompson regexp->nfa algorithm. */
  1875. #ifdef __STDC__
  1876. RX_DECL int
  1877. rx_build_nfa (struct rx *rx,
  1878.           struct rexp_node *rexp,
  1879.           struct rx_nfa_state **start,
  1880.           struct rx_nfa_state **end)
  1881. #else
  1882. RX_DECL int
  1883. rx_build_nfa (rx, rexp, start, end)
  1884.      struct rx *rx;
  1885.      struct rexp_node *rexp;
  1886.      struct rx_nfa_state **start;
  1887.      struct rx_nfa_state **end;
  1888. #endif
  1889. {
  1890.   struct rx_nfa_edge *edge;
  1891.  
  1892.   /* Start & end nodes may have been allocated by the caller. */
  1893.   *start = *start ? *start : rx_nfa_state (rx);
  1894.  
  1895.   if (!*start)
  1896.     return 0;
  1897.  
  1898.   if (!rexp)
  1899.     {
  1900.       *end = *start;
  1901.       return 1;
  1902.     }
  1903.  
  1904.   *end = *end ? *end : rx_nfa_state (rx);
  1905.  
  1906.   if (!*end)
  1907.     {
  1908.       rx_free_nfa_state (*start);
  1909.       return 0;
  1910.     }
  1911.  
  1912.   switch (rexp->type)
  1913.     {
  1914.     case r_data:
  1915.       return 0;
  1916.       break;
  1917.     case r_cset:
  1918.       edge = rx_nfa_edge (rx, ne_cset, *start, *end);
  1919.       if (!edge)
  1920.     return 0;
  1921.       edge->params.cset = rx_copy_cset (rx, rexp->params.cset);
  1922.       if (!edge->params.cset)
  1923.     {
  1924.       rx_free_nfa_edge (edge);
  1925.       return 0;
  1926.     }
  1927.       return 1;
  1928.  
  1929.     case r_opt:
  1930.       return (rx_build_nfa (rx, rexp->params.pair.left, start, end)
  1931.           && rx_nfa_edge (rx, ne_epsilon, *start, *end));
  1932.  
  1933.     case r_star:
  1934.       {
  1935.     struct rx_nfa_state * star_start = 0;
  1936.     struct rx_nfa_state * star_end = 0;
  1937.     return (rx_build_nfa (rx, rexp->params.pair.left,
  1938.                   &star_start, &star_end)
  1939.         && star_start
  1940.         && star_end
  1941.         && rx_nfa_edge (rx, ne_epsilon, star_start, star_end)
  1942.         && rx_nfa_edge (rx, ne_epsilon, *start, star_start)
  1943.         && rx_nfa_edge (rx, ne_epsilon, star_end, *end)
  1944.  
  1945.         && rx_nfa_edge (rx, ne_epsilon, star_end, star_start));
  1946.       }
  1947.  
  1948.     case r_2phase_star:
  1949.       {
  1950.     struct rx_nfa_state * star_start = 0;
  1951.     struct rx_nfa_state * star_end = 0;
  1952.     struct rx_nfa_state * loop_exp_start = 0;
  1953.     struct rx_nfa_state * loop_exp_end = 0;
  1954.  
  1955.     return (rx_build_nfa (rx, rexp->params.pair.left,
  1956.                   &star_start, &star_end)
  1957.         && rx_build_nfa (rx, rexp->params.pair.right,
  1958.                  &loop_exp_start, &loop_exp_end)
  1959.         && star_start
  1960.         && star_end
  1961.         && loop_exp_end
  1962.         && loop_exp_start
  1963.         && rx_nfa_edge (rx, ne_epsilon, star_start, *end)
  1964.         && rx_nfa_edge (rx, ne_epsilon, *start, star_start)
  1965.         && rx_nfa_edge (rx, ne_epsilon, star_end, *end)
  1966.  
  1967.         && rx_nfa_edge (rx, ne_epsilon, star_end, loop_exp_start)
  1968.         && rx_nfa_edge (rx, ne_epsilon, loop_exp_end, star_start));
  1969.       }
  1970.  
  1971.  
  1972.     case r_concat:
  1973.       {
  1974.     struct rx_nfa_state *shared = 0;
  1975.     return
  1976.       (rx_build_nfa (rx, rexp->params.pair.left, start, &shared)
  1977.        && rx_build_nfa (rx, rexp->params.pair.right, &shared, end));
  1978.       }
  1979.  
  1980.     case r_alternate:
  1981.       {
  1982.     struct rx_nfa_state *ls = 0;
  1983.     struct rx_nfa_state *le = 0;
  1984.     struct rx_nfa_state *rs = 0;
  1985.     struct rx_nfa_state *re = 0;
  1986.     return (rx_build_nfa (rx, rexp->params.pair.left, &ls, &le)
  1987.         && rx_build_nfa (rx, rexp->params.pair.right, &rs, &re)
  1988.         && rx_nfa_edge (rx, ne_epsilon, *start, ls)
  1989.         && rx_nfa_edge (rx, ne_epsilon, *start, rs)
  1990.         && rx_nfa_edge (rx, ne_epsilon, le, *end)
  1991.         && rx_nfa_edge (rx, ne_epsilon, re, *end));
  1992.       }
  1993.  
  1994.     case r_side_effect:
  1995.       edge = rx_nfa_edge (rx, ne_side_effect, *start, *end);
  1996.       if (!edge)
  1997.     return 0;
  1998.       edge->params.side_effect = rexp->params.side_effect;
  1999.       return 1;
  2000.     };
  2001. }
  2002.  
  2003.  
  2004. /* NAME_RX->NFA_STATES identifies all nodes with non-epsilon transitions.
  2005.  * These nodes can occur in super-states.  All nodes are given an integer id.
  2006.  * The id is non-negative if the node has non-epsilon out-transitions, negative
  2007.  * otherwise (this is because we want the non-negative ids to be used as 
  2008.  * array indexes in a few places).
  2009.  */
  2010.  
  2011. #ifdef __STDC__
  2012. RX_DECL void
  2013. rx_name_nfa_states (struct rx *rx)
  2014. #else
  2015. RX_DECL void
  2016. rx_name_nfa_states (rx)
  2017.      struct rx *rx;
  2018. #endif
  2019. {
  2020.   struct rx_nfa_state *n = rx->nfa_states;
  2021.  
  2022.   rx->nodec = 0;
  2023.   rx->epsnodec = -1;
  2024.  
  2025.   while (n)
  2026.     {
  2027.       struct rx_nfa_edge *e = n->edges;
  2028.  
  2029.       if (n->is_start)
  2030.     n->eclosure_needed = 1;
  2031.  
  2032.       while (e)
  2033.     {
  2034.       switch (e->type)
  2035.         {
  2036.         case ne_epsilon:
  2037.         case ne_side_effect:
  2038.           break;
  2039.  
  2040.         case ne_cset:
  2041.           n->id = rx->nodec++;
  2042.           {
  2043.         struct rx_nfa_edge *from_n = n->edges;
  2044.         while (from_n)
  2045.           {
  2046.             from_n->dest->eclosure_needed = 1;
  2047.             from_n = from_n->next;
  2048.           }
  2049.           }
  2050.           goto cont;
  2051.         }
  2052.       e = e->next;
  2053.     }
  2054.       n->id = rx->epsnodec--;
  2055.     cont:
  2056.       n = n->next;
  2057.     }
  2058.   rx->epsnodec = -rx->epsnodec;
  2059. }
  2060.  
  2061.  
  2062. /* This page: data structures for the static part of the nfa->supernfa
  2063.  * translation.
  2064.  */
  2065.  
  2066. /* The next several functions compare, construct, etc. lists of side
  2067.  * effects.  See ECLOSE_NFA (below) for details.
  2068.  */
  2069.  
  2070. /* Ordering of rx_se_list
  2071.  * (-1, 0, 1 return value convention).
  2072.  */
  2073.  
  2074. #ifdef __STDC__
  2075. static int 
  2076. se_list_cmp (void * va, void * vb)
  2077. #else
  2078. static int 
  2079. se_list_cmp (va, vb)
  2080.      void * va;
  2081.      void * vb;
  2082. #endif
  2083. {
  2084.   struct rx_se_list * a = (struct rx_se_list *)va;
  2085.   struct rx_se_list * b = (struct rx_se_list *)vb;
  2086.  
  2087.   return ((va == vb)
  2088.       ? 0
  2089.       : (!va
  2090.          ? -1
  2091.          : (!vb
  2092.         ? 1
  2093.         : ((long)a->car < (long)b->car
  2094.            ? 1
  2095.            : ((long)a->car > (long)b->car
  2096.               ? -1
  2097.               : se_list_cmp ((void *)a->cdr, (void *)b->cdr))))));
  2098. }
  2099.  
  2100.  
  2101. #ifdef __STDC__
  2102. static int 
  2103. se_list_equal (void * va, void * vb)
  2104. #else
  2105. static int 
  2106. se_list_equal (va, vb)
  2107.      void * va;
  2108.      void * vb;
  2109. #endif
  2110. {
  2111.   return !(se_list_cmp (va, vb));
  2112. }
  2113.  
  2114. static struct rx_hash_rules se_list_hash_rules =
  2115. {
  2116.   se_list_equal,
  2117.   compiler_hash_alloc,
  2118.   compiler_free_hash,
  2119.   compiler_hash_item_alloc,
  2120.   compiler_free_hash_item
  2121. };
  2122.  
  2123. static DEF_POOL(sel_pool, struct rx_se_list);
  2124.  
  2125. #ifdef __STDC__
  2126. static struct rx_se_list * 
  2127. side_effect_cons (struct rx * rx,
  2128.           void * se, struct rx_se_list * list)
  2129. #else
  2130. static struct rx_se_list * 
  2131. side_effect_cons (rx, se, list)
  2132.      struct rx * rx;
  2133.      void * se;
  2134.      struct rx_se_list * list;
  2135. #endif
  2136. {
  2137.   struct rx_se_list * l = ((struct rx_se_list *)
  2138.                chunk_malloc (&sel_pool));
  2139.   if (!l)
  2140.     return 0;
  2141.   l->car = se;
  2142.   l->cdr = list;
  2143.   return l;
  2144. }
  2145.  
  2146.  
  2147. #ifdef __STDC__
  2148. static struct rx_se_list *
  2149. hash_cons_se_prog (struct rx * rx,
  2150.            struct rx_hash * memo,
  2151.            void * car, struct rx_se_list * cdr)
  2152. #else
  2153. static struct rx_se_list *
  2154. hash_cons_se_prog (rx, memo, car, cdr)
  2155.      struct rx * rx;
  2156.      struct rx_hash * memo;
  2157.      void * car;
  2158.      struct rx_se_list * cdr;
  2159. #endif
  2160. {
  2161.   long hash = (long)car ^ (long)cdr;
  2162.   struct rx_se_list template;
  2163.  
  2164.   template.car = car;
  2165.   template.cdr = cdr;
  2166.   {
  2167.     struct rx_hash_item * it = rx_hash_store (memo, hash,
  2168.                           (void *)&template,
  2169.                           &se_list_hash_rules);
  2170.     if (!it)
  2171.       return 0;
  2172.     if (it->data == (void *)&template)
  2173.       {
  2174.     struct rx_se_list * consed = ((struct rx_se_list *)
  2175.                       chunk_malloc (&sel_pool));
  2176.     *consed = template;
  2177.     it->data = (void *)consed;
  2178.       }
  2179.     return (struct rx_se_list *)it->data;
  2180.   }
  2181. }
  2182.      
  2183.  
  2184. #ifdef __STDC__
  2185. static struct rx_se_list *
  2186. hash_se_prog (struct rx * rx, struct rx_hash * memo, struct rx_se_list * prog)
  2187. #else
  2188. static struct rx_se_list *
  2189. hash_se_prog (rx, memo, prog)
  2190.      struct rx * rx;
  2191.      struct rx_hash * memo;
  2192.      struct rx_se_list * prog;
  2193. #endif
  2194. {
  2195.   struct rx_se_list * answer = 0;
  2196.   while (prog)
  2197.     {
  2198.       answer = hash_cons_se_prog (rx, memo, prog->car, answer);
  2199.       if (!answer)
  2200.     return 0;
  2201.       prog = prog->cdr;
  2202.     }
  2203.   return answer;
  2204. }
  2205.  
  2206.  
  2207. /* This page: more data structures for nfa->supernfa.  Specificly,
  2208.  * sets of nfa states.
  2209.  */
  2210.  
  2211. #ifdef __STDC__
  2212. static int 
  2213. nfa_set_cmp (void * va, void * vb)
  2214. #else
  2215. static int 
  2216. nfa_set_cmp (va, vb)
  2217.      void * va;
  2218.      void * vb;
  2219. #endif
  2220. {
  2221.   struct rx_nfa_state_set * a = (struct rx_nfa_state_set *)va;
  2222.   struct rx_nfa_state_set * b = (struct rx_nfa_state_set *)vb;
  2223.  
  2224.   return ((va == vb)
  2225.       ? 0
  2226.       : (!va
  2227.          ? -1
  2228.          : (!vb
  2229.         ? 1
  2230.         : (a->car->id < b->car->id
  2231.            ? 1
  2232.            : (a->car->id > b->car->id
  2233.               ? -1
  2234.               : nfa_set_cmp ((void *)a->cdr, (void *)b->cdr))))));
  2235. }
  2236.  
  2237. #ifdef __STDC__
  2238. static int 
  2239. nfa_set_equal (void * va, void * vb)
  2240. #else
  2241. static int 
  2242. nfa_set_equal (va, vb)
  2243.      void * va;
  2244.      void * vb;
  2245. #endif
  2246. {
  2247.   return !nfa_set_cmp (va, vb);
  2248. }
  2249.  
  2250. static struct rx_hash_rules nfa_set_hash_rules =
  2251. {
  2252.   nfa_set_equal,
  2253.   compiler_hash_alloc,
  2254.   compiler_free_hash,
  2255.   compiler_hash_item_alloc,
  2256.   compiler_free_hash_item
  2257. };
  2258.  
  2259.  
  2260. /* CONS -- again, sets with == elements are ==. */
  2261.  
  2262. static DEF_POOL(nfa_sets, struct rx_nfa_state_set);
  2263.  
  2264. #ifdef __STDC__
  2265. static struct rx_nfa_state_set * 
  2266. nfa_set_cons (struct rx * rx,
  2267.           struct rx_hash * memo, struct rx_nfa_state * state,
  2268.           struct rx_nfa_state_set * set)
  2269. #else
  2270. static struct rx_nfa_state_set * 
  2271. nfa_set_cons (rx, memo, state, set)
  2272.      struct rx * rx;
  2273.      struct rx_hash * memo;
  2274.      struct rx_nfa_state * state;
  2275.      struct rx_nfa_state_set * set;
  2276. #endif
  2277. {
  2278.   struct rx_nfa_state_set template;
  2279.   struct rx_hash_item * node;
  2280.   template.car = state;
  2281.   template.cdr = set;
  2282.   node = rx_hash_store (memo,
  2283.             (((long)state) >> 8) ^ (long)set,
  2284.             &template, &nfa_set_hash_rules);
  2285.   if (!node)
  2286.     return 0;
  2287.   if (node->data == &template)
  2288.     {
  2289.       struct rx_nfa_state_set * l = ((struct rx_nfa_state_set *)
  2290.                   chunk_malloc (&nfa_sets));
  2291.       node->data = (void *) l;
  2292.       if (!l)
  2293.     return 0;
  2294.       *l = template;
  2295.     }
  2296.   return (struct rx_nfa_state_set *)node->data;
  2297. }
  2298.  
  2299.  
  2300. #ifdef __STDC__
  2301. static struct rx_nfa_state_set * 
  2302. nfa_set_enjoin (struct rx * rx,
  2303.         struct rx_hash * memo, struct rx_nfa_state * state,
  2304.         struct rx_nfa_state_set * set)
  2305. #else
  2306. static struct rx_nfa_state_set * 
  2307. nfa_set_enjoin (rx, memo, state, set)
  2308.      struct rx * rx;
  2309.      struct rx_hash * memo;
  2310.      struct rx_nfa_state * state;
  2311.      struct rx_nfa_state_set * set;
  2312. #endif
  2313. {
  2314.   if (!set || state->id < set->car->id)
  2315.     return nfa_set_cons (rx, memo, state, set);
  2316.   if (state->id == set->car->id)
  2317.     return set;
  2318.   else
  2319.     {
  2320.       struct rx_nfa_state_set * newcdr
  2321.     = nfa_set_enjoin (rx, memo, state, set->cdr);
  2322.       if (newcdr != set->cdr)
  2323.     set = nfa_set_cons (rx, memo, set->car, newcdr);
  2324.       return set;
  2325.     }
  2326. }
  2327.  
  2328.  
  2329.  
  2330. /* This page: computing epsilon closures.  The closures aren't total.
  2331.  * Each node's closures are partitioned according to the side effects entailed
  2332.  * along the epsilon edges.  Return true on success.
  2333.  */ 
  2334.  
  2335. struct eclose_frame
  2336. {
  2337.   struct rx_se_list *prog_backwards;
  2338. };
  2339.  
  2340.  
  2341. #ifdef __STDC__
  2342. static int 
  2343. eclose_node (struct rx *rx, struct rx_nfa_state *outnode,
  2344.          struct rx_nfa_state *node, struct eclose_frame *frame)
  2345. #else
  2346. static int 
  2347. eclose_node (rx, outnode, node, frame)
  2348.      struct rx *rx;
  2349.      struct rx_nfa_state *outnode;
  2350.      struct rx_nfa_state *node;
  2351.      struct eclose_frame *frame;
  2352. #endif
  2353. {
  2354.   struct rx_nfa_edge *e = node->edges;
  2355.  
  2356.   /* For each node, we follow all epsilon paths to build the closure.
  2357.    * The closure omits nodes that have only epsilon edges.
  2358.    * The closure is split into partial closures -- all the states in
  2359.    * a partial closure are reached by crossing the same list of
  2360.    * of side effects (though not necessarily the same path).
  2361.    */
  2362.   if (node->mark)
  2363.     return 1;
  2364.   node->mark = 1;
  2365.  
  2366.   if (node->id >= 0 || node->is_final)
  2367.     {
  2368.       struct rx_possible_future **ec;
  2369.       struct rx_se_list * prog_in_order
  2370.     = ((struct rx_se_list *)hash_se_prog (rx,
  2371.                           &rx->se_list_memo,
  2372.                           frame->prog_backwards));
  2373.       int cmp;
  2374.  
  2375.       ec = &outnode->futures;
  2376.  
  2377.       while (*ec)
  2378.     {
  2379.       cmp = se_list_cmp ((void *)(*ec)->effects, (void *)prog_in_order);
  2380.       if (cmp <= 0)
  2381.         break;
  2382.       ec = &(*ec)->next;
  2383.     }
  2384.       if (!*ec || (cmp < 0))
  2385.     {
  2386.       struct rx_possible_future * saved = *ec;
  2387.       *ec = rx_possible_future (rx, prog_in_order);
  2388.       (*ec)->next = saved;
  2389.       if (!*ec)
  2390.         return 0;
  2391.     }
  2392.       if (node->id >= 0)
  2393.     {
  2394.       (*ec)->destset = nfa_set_enjoin (rx, &rx->set_list_memo,
  2395.                        node, (*ec)->destset);
  2396.       if (!(*ec)->destset)
  2397.         return 0;
  2398.     }
  2399.     }
  2400.  
  2401.   while (e)
  2402.     {
  2403.       switch (e->type)
  2404.     {
  2405.     case ne_epsilon:
  2406.       if (!eclose_node (rx, outnode, e->dest, frame))
  2407.         return 0;
  2408.       break;
  2409.     case ne_side_effect:
  2410.       {
  2411.         frame->prog_backwards = side_effect_cons (rx, 
  2412.                               e->params.side_effect,
  2413.                               frame->prog_backwards);
  2414.         if (!frame->prog_backwards)
  2415.           return 0;
  2416.         if (!eclose_node (rx, outnode, e->dest, frame))
  2417.           return 0;
  2418.         {
  2419.           struct rx_se_list * dying = frame->prog_backwards;
  2420.           frame->prog_backwards = frame->prog_backwards->cdr;
  2421.           chunk_free (&sel_pool, (char *)dying);
  2422.         }
  2423.         break;
  2424.       }
  2425.     default:
  2426.       break;
  2427.     }
  2428.       e = e->next;
  2429.     }
  2430.   node->mark = 0;
  2431.   return 1;
  2432. }
  2433.  
  2434.  
  2435. #ifdef __STDC__
  2436. RX_DECL int 
  2437. rx_eclose_nfa (struct rx *rx)
  2438. #else
  2439. RX_DECL int 
  2440. rx_eclose_nfa (rx)
  2441.      struct rx *rx;
  2442. #endif
  2443. {
  2444.   struct rx_nfa_state *n = rx->nfa_states;
  2445.   struct eclose_frame frame;
  2446.   static int rx_id = 0;
  2447.   
  2448.   frame.prog_backwards = 0;
  2449.   rx->rx_id = rx_id++;
  2450.   bzero (&rx->se_list_memo, sizeof (rx->se_list_memo));
  2451.   bzero (&rx->set_list_memo, sizeof (rx->set_list_memo));
  2452.   while (n)
  2453.     {
  2454.       n->futures = 0;
  2455.       if (n->eclosure_needed && !eclose_node (rx, n, n, &frame))
  2456.     return 0;
  2457.       /* clear_marks (rx); */
  2458.       n = n->next;
  2459.     }
  2460.   return 1;
  2461. }
  2462.  
  2463.  
  2464. /* This deletes epsilon edges from an NFA.  After running eclose_node,
  2465.  * we have no more need for these edges.  They are removed to simplify
  2466.  * further operations on the NFA.
  2467.  */
  2468.  
  2469. #ifdef __STDC__
  2470. RX_DECL void 
  2471. rx_delete_epsilon_transitions (struct rx *rx)
  2472. #else
  2473. RX_DECL void 
  2474. rx_delete_epsilon_transitions (rx)
  2475.      struct rx *rx;
  2476. #endif
  2477. {
  2478.   struct rx_nfa_state *n = rx->nfa_states;
  2479.   struct rx_nfa_edge **e;
  2480.  
  2481.   while (n)
  2482.     {
  2483.       e = &n->edges;
  2484.       while (*e)
  2485.     {
  2486.       struct rx_nfa_edge *t;
  2487.       switch ((*e)->type)
  2488.         {
  2489.         case ne_epsilon:
  2490.         case ne_side_effect:
  2491.           t = *e;
  2492.           *e = t->next;
  2493.           rx_free_nfa_edge (t);
  2494.           break;
  2495.  
  2496.         default:
  2497.           e = &(*e)->next;
  2498.           break;
  2499.         }
  2500.     }
  2501.       n = n->next;
  2502.     }
  2503. }
  2504.  
  2505.  
  2506. /* This page: storing the nfa in a contiguous region of memory for
  2507.  * subsequent conversion to a super-nfa.
  2508.  */
  2509.  
  2510.  
  2511. /* This is for qsort on an array of nfa_states. The order
  2512.  * is based on state ids and goes 
  2513.  *        [0...MAX][MIN..-1] where (MAX>=0) and (MIN<0)
  2514.  * This way, positive ids double as array indices.
  2515.  */
  2516.  
  2517. #ifdef __STDC__
  2518. static int 
  2519. nfacmp (void * va, void * vb)
  2520. #else
  2521. static int 
  2522. nfacmp (va, vb)
  2523.      void * va;
  2524.      void * vb;
  2525. #endif
  2526. {
  2527.   struct rx_nfa_state **a = (struct rx_nfa_state **)va;
  2528.   struct rx_nfa_state **b = (struct rx_nfa_state **)vb;
  2529.   return (*a == *b        /* &&&& 3.18 */
  2530.       ? 0
  2531.       : (((*a)->id < 0) == ((*b)->id < 0)
  2532.          ? (((*a)->id  < (*b)->id) ? -1 : 1)
  2533.          : (((*a)->id < 0)
  2534.         ? 1 : -1)));
  2535. }
  2536.  
  2537. #ifdef __STDC__
  2538. static int 
  2539. count_hash_nodes (struct rx_hash * st)
  2540. #else
  2541. static int 
  2542. count_hash_nodes (st)
  2543.      struct rx_hash * st;
  2544. #endif
  2545. {
  2546.   int x;
  2547.   int count = 0;
  2548.   for (x = 0; x < 13; ++x)
  2549.     count += ((st->children[x])
  2550.           ? count_hash_nodes (st->children[x])
  2551.           : st->bucket_size[x]);
  2552.   
  2553.   return count;
  2554. }
  2555.  
  2556.  
  2557. #ifdef __STDC__
  2558. static void 
  2559. se_memo_freer (struct rx_hash_item * node)
  2560. #else
  2561. static void 
  2562. se_memo_freer (node)
  2563.      struct rx_hash_item * node;
  2564. #endif
  2565. {
  2566.   chunk_free (&sel_pool, (char *)node->data);
  2567. }
  2568.  
  2569.  
  2570. #ifdef __STDC__
  2571. static void 
  2572. nfa_set_freer (struct rx_hash_item * node)
  2573. #else
  2574. static void 
  2575. nfa_set_freer (node)
  2576.      struct rx_hash_item * node;
  2577. #endif
  2578. {
  2579.   chunk_free (&nfa_sets, (char *)node->data);
  2580. }
  2581.  
  2582.  
  2583. /* This copies an entire NFA into a single malloced block of memory.
  2584.  * Mostly this is for compatability with regex.c, though it is convenient
  2585.  * to have the nfa nodes in an array.
  2586.  */
  2587.  
  2588. #ifdef __STDC__
  2589. RX_DECL int 
  2590. rx_compactify_nfa (struct rx *rx,
  2591.            void **mem, unsigned long *size)
  2592. #else
  2593. RX_DECL int 
  2594. rx_compactify_nfa (rx, mem, size)
  2595.      struct rx *rx;
  2596.      void **mem;
  2597.      unsigned long *size;
  2598. #endif
  2599. {
  2600.   int total_nodec;
  2601.   struct rx_nfa_state *n;
  2602.   int edgec = 0;
  2603.   int eclosec = 0;
  2604.   int se_list_consc = count_hash_nodes (&rx->se_list_memo);
  2605.   int nfa_setc = count_hash_nodes (&rx->set_list_memo);
  2606.   unsigned long total_size;
  2607.  
  2608.   /* This takes place in two stages.   First, the total size of the
  2609.    * nfa is computed, then structures are copied.  
  2610.    */   
  2611.   n = rx->nfa_states;
  2612.   total_nodec = 0;
  2613.   while (n)
  2614.     {
  2615.       struct rx_nfa_edge *e = n->edges;
  2616.       struct rx_possible_future *ec = n->futures;
  2617.       ++total_nodec;
  2618.       while (e)
  2619.     {
  2620.       ++edgec;
  2621.       e = e->next;
  2622.     }
  2623.       while (ec)
  2624.     {
  2625.       ++eclosec;
  2626.       ec = ec->next;
  2627.     }
  2628.       n = n->next;
  2629.     }
  2630.  
  2631.   total_size = (total_nodec * sizeof (struct rx_nfa_state)
  2632.         + edgec * rx_sizeof_bitset (rx->local_cset_size)
  2633.         + edgec * sizeof (struct rx_nfa_edge)
  2634.         + nfa_setc * sizeof (struct rx_nfa_state_set)
  2635.         + eclosec * sizeof (struct rx_possible_future)
  2636.         + se_list_consc * sizeof (struct rx_se_list)
  2637.         + rx->reserved);
  2638.  
  2639.   if (total_size > *size)
  2640.     {
  2641.       *mem = remalloc (*mem, total_size);
  2642.       if (*mem)
  2643.     *size = total_size;
  2644.       else
  2645.     return 0;
  2646.     }
  2647.   /* Now we've allocated the memory; this copies the NFA. */
  2648.   {
  2649.     static struct rx_nfa_state **scratch = 0;
  2650.     static int scratch_alloc = 0;
  2651.     struct rx_nfa_state *state_base = (struct rx_nfa_state *) * mem;
  2652.     struct rx_nfa_state *new_state = state_base;
  2653.     struct rx_nfa_edge *new_edge =
  2654.       (struct rx_nfa_edge *)
  2655.     ((char *) state_base + total_nodec * sizeof (struct rx_nfa_state));
  2656.     struct rx_se_list * new_se_list =
  2657.       (struct rx_se_list *)
  2658.     ((char *)new_edge + edgec * sizeof (struct rx_nfa_edge));
  2659.     struct rx_possible_future *new_close =
  2660.       ((struct rx_possible_future *)
  2661.        ((char *) new_se_list
  2662.     + se_list_consc * sizeof (struct rx_se_list)));
  2663.     struct rx_nfa_state_set * new_nfa_set =
  2664.       ((struct rx_nfa_state_set *)
  2665.        ((char *)new_close + eclosec * sizeof (struct rx_possible_future)));
  2666.     char *new_bitset =
  2667.       ((char *) new_nfa_set + nfa_setc * sizeof (struct rx_nfa_state_set));
  2668.     int x;
  2669.     struct rx_nfa_state *n;
  2670.  
  2671.     if (scratch_alloc < total_nodec)
  2672.       {
  2673.     scratch = ((struct rx_nfa_state **)
  2674.            remalloc (scratch, total_nodec * sizeof (*scratch)));
  2675.     if (scratch)
  2676.       scratch_alloc = total_nodec;
  2677.     else
  2678.       {
  2679.         scratch_alloc = 0;
  2680.         return 0;
  2681.       }
  2682.       }
  2683.  
  2684.     for (x = 0, n = rx->nfa_states; n; n = n->next)
  2685.       scratch[x++] = n;
  2686.  
  2687.     qsort (scratch, total_nodec,
  2688.        sizeof (struct rx_nfa_state *), (int (*)())nfacmp);
  2689.  
  2690.     for (x = 0; x < total_nodec; ++x)
  2691.       {
  2692.     struct rx_possible_future *eclose = scratch[x]->futures;
  2693.     struct rx_nfa_edge *edge = scratch[x]->edges;
  2694.     struct rx_nfa_state *cn = new_state++;
  2695.     cn->futures = 0;
  2696.     cn->edges = 0;
  2697.     cn->next = (x == total_nodec - 1) ? 0 : (cn + 1);
  2698.     cn->id = scratch[x]->id;
  2699.     cn->is_final = scratch[x]->is_final;
  2700.     cn->is_start = scratch[x]->is_start;
  2701.     cn->mark = 0;
  2702.     while (edge)
  2703.       {
  2704.         int indx = (edge->dest->id < 0
  2705.              ? (total_nodec + edge->dest->id)
  2706.              : edge->dest->id);
  2707.         struct rx_nfa_edge *e = new_edge++;
  2708.         rx_Bitset cset = (rx_Bitset) new_bitset;
  2709.         new_bitset += rx_sizeof_bitset (rx->local_cset_size);
  2710.         rx_bitset_null (rx->local_cset_size, cset);
  2711.         rx_bitset_union (rx->local_cset_size, cset, edge->params.cset);
  2712.         e->next = cn->edges;
  2713.         cn->edges = e;
  2714.         e->type = edge->type;
  2715.         e->dest = state_base + indx;
  2716.         e->params.cset = cset;
  2717.         edge = edge->next;
  2718.       }
  2719.     while (eclose)
  2720.       {
  2721.         struct rx_possible_future *ec = new_close++;
  2722.         struct rx_hash_item * sp;
  2723.         struct rx_se_list ** sepos;
  2724.         struct rx_se_list * sesrc;
  2725.         struct rx_nfa_state_set * destlst;
  2726.         struct rx_nfa_state_set ** destpos;
  2727.         ec->next = cn->futures;
  2728.         cn->futures = ec;
  2729.         for (sepos = &ec->effects, sesrc = eclose->effects;
  2730.          sesrc;
  2731.          sesrc = sesrc->cdr, sepos = &(*sepos)->cdr)
  2732.           {
  2733.         sp = rx_hash_find (&rx->se_list_memo,
  2734.                    (long)sesrc->car ^ (long)sesrc->cdr,
  2735.                    sesrc, &se_list_hash_rules);
  2736.         if (sp->binding)
  2737.           {
  2738.             sesrc = (struct rx_se_list *)sp->binding;
  2739.             break;
  2740.           }
  2741.         *new_se_list = *sesrc;
  2742.         sp->binding = (void *)new_se_list;
  2743.         *sepos = new_se_list;
  2744.         ++new_se_list;
  2745.           }
  2746.         *sepos = sesrc;
  2747.         for (destpos = &ec->destset, destlst = eclose->destset;
  2748.          destlst;
  2749.          destpos = &(*destpos)->cdr, destlst = destlst->cdr)
  2750.           {
  2751.         sp = rx_hash_find (&rx->set_list_memo,
  2752.                    ((((long)destlst->car) >> 8)
  2753.                     ^ (long)destlst->cdr),
  2754.                    destlst, &nfa_set_hash_rules);
  2755.         if (sp->binding)
  2756.           {
  2757.             destlst = (struct rx_nfa_state_set *)sp->binding;
  2758.             break;
  2759.           }
  2760.         *new_nfa_set = *destlst;
  2761.         new_nfa_set->car = state_base + destlst->car->id;
  2762.         sp->binding = (void *)new_nfa_set;
  2763.         *destpos = new_nfa_set;
  2764.         ++new_nfa_set;
  2765.           }
  2766.         *destpos = destlst;
  2767.         eclose = eclose->next;
  2768.       }
  2769.       }
  2770.   }
  2771.   rx_free_hash_table (&rx->se_list_memo, se_memo_freer, &se_list_hash_rules);
  2772.   bzero (&rx->se_list_memo, sizeof (rx->se_list_memo));
  2773.   rx_free_hash_table (&rx->set_list_memo, nfa_set_freer, &nfa_set_hash_rules);
  2774.   bzero (&rx->set_list_memo, sizeof (rx->set_list_memo));
  2775.  
  2776.   rx_free_nfa (rx);
  2777.   rx->nfa_states = (struct rx_nfa_state *)*mem;
  2778.   return 1;
  2779. }
  2780.  
  2781.  
  2782. /* The functions in the next several pages define the lazy-NFA-conversion used
  2783.  * by matchers.  The input to this construction is an NFA such as 
  2784.  * is built by compactify_nfa (rx.c).  The output is the superNFA.
  2785.  */
  2786.  
  2787.  
  2788. /* Match engines can use arbitrary values for opcodes.  So, the parse tree 
  2789.  * is built using instructions names (enum rx_opcode), but the superstate
  2790.  * nfa is populated with mystery opcodes (void *).
  2791.  *
  2792.  * For convenience, here is an id table.  The opcodes are == to their inxs
  2793.  *
  2794.  * The lables in re_search_2 would make good values for instructions.
  2795.  */
  2796.  
  2797. void * rx_id_instruction_table[rx_num_instructions] =
  2798. {
  2799.   (void *) rx_backtrack_point,
  2800.   (void *) rx_do_side_effects,
  2801.   (void *) rx_cache_miss,
  2802.   (void *) rx_next_char,
  2803.   (void *) rx_backtrack,
  2804.   (void *) rx_error_inx
  2805. };
  2806.  
  2807.  
  2808.  
  2809. /* Memory mgt. for superstate graphs. */
  2810.  
  2811. #ifdef __STDC__
  2812. static char *
  2813. rx_cache_malloc (struct rx_cache * cache, int bytes)
  2814. #else
  2815. static char *
  2816. rx_cache_malloc (cache, bytes)
  2817.      struct rx_cache * cache;
  2818.      int bytes;
  2819. #endif
  2820. {
  2821.   while (cache->bytes_left < bytes)
  2822.     {
  2823.       if (cache->memory_pos)
  2824.     cache->memory_pos = cache->memory_pos->next;
  2825.       if (!cache->memory_pos)
  2826.     {
  2827.       cache->morecore (cache);
  2828.       if (!cache->memory_pos)
  2829.         return 0;
  2830.     }
  2831.       cache->bytes_left = cache->memory_pos->bytes;
  2832.       cache->memory_addr = ((char *)cache->memory_pos
  2833.                 + sizeof (struct rx_blocklist));
  2834.     }
  2835.   cache->bytes_left -= bytes;
  2836.   {
  2837.     char * addr = cache->memory_addr;
  2838.     cache->memory_addr += bytes;
  2839.     return addr;
  2840.   }
  2841. }
  2842.  
  2843. #ifdef __STDC__
  2844. static void
  2845. rx_cache_free (struct rx_cache * cache,
  2846.            struct rx_freelist ** freelist, char * mem)
  2847. #else
  2848. static void
  2849. rx_cache_free (cache, freelist, mem)
  2850.      struct rx_cache * cache;
  2851.      struct rx_freelist ** freelist;
  2852.      char * mem;
  2853. #endif
  2854. {
  2855.   struct rx_freelist * it = (struct rx_freelist *)mem;
  2856.   it->next = *freelist;
  2857.   *freelist = it;
  2858. }
  2859.  
  2860.  
  2861. /* The partially instantiated superstate graph has a transition 
  2862.  * table at every node.  There is one entry for every character.
  2863.  * This fills in the transition for a set.
  2864.  */
  2865. #ifdef __STDC__
  2866. static void 
  2867. install_transition (struct rx_superstate *super,
  2868.             struct rx_inx *answer, rx_Bitset trcset) 
  2869. #else
  2870. static void 
  2871. install_transition (super, answer, trcset)
  2872.      struct rx_superstate *super;
  2873.      struct rx_inx *answer;
  2874.      rx_Bitset trcset;
  2875. #endif
  2876. {
  2877.   struct rx_inx * transitions = super->transitions;
  2878.   int chr;
  2879.   for (chr = 0; chr < 256; )
  2880.     if (!*trcset)
  2881.       {
  2882.     ++trcset;
  2883.     chr += 32;
  2884.       }
  2885.     else
  2886.       {
  2887.     RX_subset sub = *trcset;
  2888.     RX_subset mask = 1;
  2889.     int bound = chr + 32;
  2890.     while (chr < bound)
  2891.       {
  2892.         if (sub & mask)
  2893.           transitions [chr] = *answer;
  2894.         ++chr;
  2895.         mask <<= 1;
  2896.       }
  2897.     ++trcset;
  2898.       }
  2899. }
  2900.  
  2901.  
  2902. #if 1
  2903. static int
  2904. qlen (q)
  2905.      struct rx_superstate * q;
  2906. {
  2907.   int count = 1;
  2908.   struct rx_superstate * it;
  2909.   if (!q)
  2910.     return 0;
  2911.   for (it = q->next_recyclable; it != q; it = it->next_recyclable)
  2912.     ++count;
  2913.   return count;
  2914. }
  2915.  
  2916. static void
  2917. check_cache (cache)
  2918.      struct rx_cache * cache;
  2919. {
  2920.   struct rx_cache * you_fucked_up = 0;
  2921.   int total = cache->superstates;
  2922.   int semi = cache->semifree_superstates;
  2923.   if (semi != qlen (cache->semifree_superstate))
  2924.     check_cache (you_fucked_up);
  2925.   if ((total - semi) != qlen (cache->lru_superstate))
  2926.     check_cache (you_fucked_up);
  2927. }
  2928. #endif
  2929.  
  2930. #ifdef __STDC__
  2931. static void
  2932. semifree_superstate (struct rx_cache * cache)
  2933. #else
  2934. static void
  2935. semifree_superstate (cache)
  2936.      struct rx_cache * cache;
  2937. #endif
  2938. {
  2939.   int disqualified = cache->semifree_superstates;
  2940.   if (disqualified == cache->superstates)
  2941.     return;
  2942.   while (cache->lru_superstate->locks)
  2943.     {
  2944.       cache->lru_superstate = cache->lru_superstate->next_recyclable;
  2945.       ++disqualified;
  2946.       if (disqualified == cache->superstates)
  2947.     return;
  2948.     }
  2949.   {
  2950.     struct rx_superstate * it = cache->lru_superstate;
  2951.     it->next_recyclable->prev_recyclable = it->prev_recyclable;
  2952.     it->prev_recyclable->next_recyclable = it->next_recyclable;
  2953.     cache->lru_superstate = (it == it->next_recyclable
  2954.                  ? 0
  2955.                  : it->next_recyclable);
  2956.     if (!cache->semifree_superstate)
  2957.       {
  2958.     cache->semifree_superstate = it;
  2959.     it->next_recyclable = it;
  2960.     it->prev_recyclable = it;
  2961.       }
  2962.     else
  2963.       {
  2964.     it->prev_recyclable = cache->semifree_superstate->prev_recyclable;
  2965.     it->next_recyclable = cache->semifree_superstate;
  2966.     it->prev_recyclable->next_recyclable = it;
  2967.     it->next_recyclable->prev_recyclable = it;
  2968.       }
  2969.     {
  2970.       struct rx_distinct_future *df;
  2971.       it->is_semifree = 1;
  2972.       ++cache->semifree_superstates;
  2973.       df = it->transition_refs;
  2974.       if (df)
  2975.     {
  2976.       df->prev_same_dest->next_same_dest = 0;
  2977.       for (df = it->transition_refs; df; df = df->next_same_dest)
  2978.         {
  2979.           df->future_frame.inx = cache->instruction_table[rx_cache_miss];
  2980.           df->future_frame.data = 0;
  2981.           df->future_frame.data_2 = (void *) df;
  2982.           /* If there are any NEXT-CHAR instruction frames that
  2983.            * refer to this state, we convert them to CACHE-MISS frames.
  2984.            */
  2985.           if (!df->effects
  2986.           && (df->edge->options->next_same_super_edge[0]
  2987.               == df->edge->options))
  2988.         install_transition (df->present, &df->future_frame,
  2989.                     df->edge->cset);
  2990.         }
  2991.       df = it->transition_refs;
  2992.       df->prev_same_dest->next_same_dest = df;
  2993.     }
  2994.     }
  2995.   }
  2996. }
  2997.  
  2998.  
  2999. #ifdef __STDC__
  3000. static void 
  3001. refresh_semifree_superstate (struct rx_cache * cache,
  3002.                  struct rx_superstate * super)
  3003. #else
  3004. static void 
  3005. refresh_semifree_superstate (cache, super)
  3006.      struct rx_cache * cache;
  3007.      struct rx_superstate * super;
  3008. #endif
  3009. {
  3010.   struct rx_distinct_future *df;
  3011.  
  3012.   if (super->transition_refs)
  3013.     {
  3014.       super->transition_refs->prev_same_dest->next_same_dest = 0; 
  3015.       for (df = super->transition_refs; df; df = df->next_same_dest)
  3016.     {
  3017.       df->future_frame.inx = cache->instruction_table[rx_next_char];
  3018.       df->future_frame.data = (void *) super->transitions;
  3019.       /* CACHE-MISS instruction frames that refer to this state,
  3020.        * must be converted to NEXT-CHAR frames.
  3021.        */
  3022.       if (!df->effects
  3023.           && (df->edge->options->next_same_super_edge[0]
  3024.           == df->edge->options))
  3025.         install_transition (df->present, &df->future_frame,
  3026.                 df->edge->cset);
  3027.     }
  3028.       super->transition_refs->prev_same_dest->next_same_dest
  3029.     = super->transition_refs;
  3030.     }
  3031.   if (cache->semifree_superstate == super)
  3032.     cache->semifree_superstate = (super->prev_recyclable == super
  3033.                   ? 0
  3034.                   : super->prev_recyclable);
  3035.   super->next_recyclable->prev_recyclable = super->prev_recyclable;
  3036.   super->prev_recyclable->next_recyclable = super->next_recyclable;
  3037.  
  3038.   if (!cache->lru_superstate)
  3039.     (cache->lru_superstate
  3040.      = super->next_recyclable
  3041.      = super->prev_recyclable
  3042.      = super);
  3043.   else
  3044.     {
  3045.       super->next_recyclable = cache->lru_superstate;
  3046.       super->prev_recyclable = cache->lru_superstate->prev_recyclable;
  3047.       super->next_recyclable->prev_recyclable = super;
  3048.       super->prev_recyclable->next_recyclable = super;
  3049.     }
  3050.   super->is_semifree = 0;
  3051.   --cache->semifree_superstates;
  3052. }
  3053.  
  3054. #ifdef __STDC__
  3055. static void
  3056. rx_refresh_this_superstate (struct rx_cache * cache, struct rx_superstate * superstate)
  3057. #else
  3058. static void
  3059. rx_refresh_this_superstate (cache, superstate)
  3060.      struct rx_cache * cache;
  3061.      struct rx_superstate * superstate;
  3062. #endif
  3063. {
  3064.   if (superstate->is_semifree)
  3065.     refresh_semifree_superstate (cache, superstate);
  3066.   else if (cache->lru_superstate == superstate)
  3067.     cache->lru_superstate = superstate->next_recyclable;
  3068.   else if (superstate != cache->lru_superstate->prev_recyclable)
  3069.     {
  3070.       superstate->next_recyclable->prev_recyclable
  3071.     = superstate->prev_recyclable;
  3072.       superstate->prev_recyclable->next_recyclable
  3073.     = superstate->next_recyclable;
  3074.       superstate->next_recyclable = cache->lru_superstate;
  3075.       superstate->prev_recyclable = cache->lru_superstate->prev_recyclable;
  3076.       superstate->next_recyclable->prev_recyclable = superstate;
  3077.       superstate->prev_recyclable->next_recyclable = superstate;
  3078.     }
  3079. }
  3080.  
  3081. #ifdef __STDC__
  3082. static void 
  3083. release_superset_low (struct rx_cache * cache,
  3084.              struct rx_superset *set)
  3085. #else
  3086. static void 
  3087. release_superset_low (cache, set)
  3088.      struct rx_cache * cache;
  3089.      struct rx_superset *set;
  3090. #endif
  3091. {
  3092.   if (!--set->refs)
  3093.     {
  3094.       if (set->cdr)
  3095.     release_superset_low (cache, set->cdr);
  3096.  
  3097.       set->starts_for = 0;
  3098.  
  3099.       rx_hash_free
  3100.     (rx_hash_find
  3101.      (&cache->superset_table,
  3102.       (unsigned long)set->car ^ set->id ^ (unsigned long)set->cdr,
  3103.       (void *)set,
  3104.       &cache->superset_hash_rules),
  3105.      &cache->superset_hash_rules);
  3106.       rx_cache_free (cache, &cache->free_supersets, (char *)set);
  3107.     }
  3108. }
  3109.  
  3110. /* This tries to add a new superstate to the superstate freelist.
  3111.  * It might, as a result, free some edge pieces or hash tables.
  3112.  * If nothing can be freed because too many locks are being held, fail.
  3113.  */
  3114.  
  3115. #ifdef __STDC__
  3116. static int
  3117. rx_really_free_superstate (struct rx_cache * cache)
  3118. #else
  3119. static int
  3120. rx_really_free_superstate (cache)
  3121.      struct rx_cache * cache;
  3122. #endif
  3123. {
  3124.   int locked_superstates = 0;
  3125.   struct rx_superstate * it;
  3126.  
  3127.   if (!cache->superstates)
  3128.     return 0;
  3129.  
  3130.   {
  3131.     /* This is a total guess.  The idea is that we should expect as
  3132.      * many misses as we've recently experienced.  I.e., cache->misses
  3133.      * should be the same as cache->semifree_superstates.
  3134.      */
  3135.     while ((cache->hits + cache->misses) > cache->superstates_allowed)
  3136.       {
  3137.     cache->hits >>= 1;
  3138.     cache->misses >>= 1;
  3139.       }
  3140.     if (  ((cache->hits + cache->misses) * cache->semifree_superstates)
  3141.     < (cache->superstates         * cache->misses))
  3142.       {
  3143.     semifree_superstate (cache);
  3144.     semifree_superstate (cache);
  3145.       }
  3146.   }
  3147.  
  3148.   while (cache->semifree_superstate && cache->semifree_superstate->locks)
  3149.     {
  3150.       refresh_semifree_superstate (cache, cache->semifree_superstate);
  3151.       ++locked_superstates;
  3152.       if (locked_superstates == cache->superstates)
  3153.     return 0;
  3154.     }
  3155.  
  3156.   if (cache->semifree_superstate)
  3157.     {
  3158.       it = cache->semifree_superstate;
  3159.       it->next_recyclable->prev_recyclable = it->prev_recyclable;
  3160.       it->prev_recyclable->next_recyclable = it->next_recyclable;
  3161.       cache->semifree_superstate = ((it == it->next_recyclable)
  3162.                     ? 0
  3163.                     : it->next_recyclable);
  3164.       --cache->semifree_superstates;
  3165.     }
  3166.   else
  3167.     {
  3168.       while (cache->lru_superstate->locks)
  3169.     {
  3170.       cache->lru_superstate = cache->lru_superstate->next_recyclable;
  3171.       ++locked_superstates;
  3172.       if (locked_superstates == cache->superstates)
  3173.         return 0;
  3174.     }
  3175.       it = cache->lru_superstate;
  3176.       it->next_recyclable->prev_recyclable = it->prev_recyclable;
  3177.       it->prev_recyclable->next_recyclable = it->next_recyclable;
  3178.       cache->lru_superstate = ((it == it->next_recyclable)
  3179.                     ? 0
  3180.                     : it->next_recyclable);
  3181.     }
  3182.  
  3183.   if (it->transition_refs)
  3184.     {
  3185.       struct rx_distinct_future *df;
  3186.       for (df = it->transition_refs,
  3187.        df->prev_same_dest->next_same_dest = 0;
  3188.        df;
  3189.        df = df->next_same_dest)
  3190.     {
  3191.       df->future_frame.inx = cache->instruction_table[rx_cache_miss];
  3192.       df->future_frame.data = 0;
  3193.       df->future_frame.data_2 = (void *) df;
  3194.       df->future = 0;
  3195.     }
  3196.       it->transition_refs->prev_same_dest->next_same_dest =
  3197.     it->transition_refs;
  3198.     }
  3199.   {
  3200.     struct rx_super_edge *tc = it->edges;
  3201.     while (tc)
  3202.       {
  3203.     struct rx_distinct_future * df;
  3204.     struct rx_super_edge *tct = tc->next;
  3205.     df = tc->options;
  3206.     df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  3207.     while (df)
  3208.       {
  3209.         struct rx_distinct_future *dft = df;
  3210.         df = df->next_same_super_edge[0];
  3211.         
  3212.         
  3213.         if (dft->future && dft->future->transition_refs == dft)
  3214.           {
  3215.         dft->future->transition_refs = dft->next_same_dest;
  3216.         if (dft->future->transition_refs == dft)
  3217.           dft->future->transition_refs = 0;
  3218.           }
  3219.         dft->next_same_dest->prev_same_dest = dft->prev_same_dest;
  3220.         dft->prev_same_dest->next_same_dest = dft->next_same_dest;
  3221.         rx_cache_free (cache, &cache->free_discernable_futures,
  3222.                (char *)dft);
  3223.       }
  3224.     rx_cache_free (cache, &cache->free_transition_classes, (char *)tc);
  3225.     tc = tct;
  3226.       }
  3227.   }
  3228.   
  3229.   if (it->contents->superstate == it)
  3230.     it->contents->superstate = 0;
  3231.   release_superset_low (cache, it->contents);
  3232.   rx_cache_free (cache, &cache->free_superstates, (char *)it);
  3233.   --cache->superstates;
  3234.   return 1;
  3235. }
  3236.  
  3237. #ifdef __STDC__
  3238. static char *
  3239. rx_cache_get (struct rx_cache * cache,
  3240.           struct rx_freelist ** freelist)
  3241. #else
  3242. static char *
  3243. rx_cache_get (cache, freelist)
  3244.      struct rx_cache * cache;
  3245.      struct rx_freelist ** freelist;
  3246. #endif
  3247. {
  3248.   while (!*freelist && rx_really_free_superstate (cache))
  3249.     ;
  3250.   if (!*freelist)
  3251.     return 0;
  3252.   {
  3253.     struct rx_freelist * it = *freelist;
  3254.     *freelist = it->next;
  3255.     return (char *)it;
  3256.   }
  3257. }
  3258.  
  3259. #ifdef __STDC__
  3260. static char *
  3261. rx_cache_malloc_or_get (struct rx_cache * cache,
  3262.             struct rx_freelist ** freelist, int bytes)
  3263. #else
  3264. static char *
  3265. rx_cache_malloc_or_get (cache, freelist, bytes)
  3266.      struct rx_cache * cache;
  3267.      struct rx_freelist ** freelist;
  3268.      int bytes;
  3269. #endif
  3270. {
  3271.   if (!*freelist)
  3272.     {
  3273.       char * answer = rx_cache_malloc (cache, bytes);
  3274.       if (answer)
  3275.     return answer;
  3276.     }
  3277.  
  3278.   return rx_cache_get (cache, freelist);
  3279. }
  3280.  
  3281. #ifdef __STDC__
  3282. static char *
  3283. rx_cache_get_superstate (struct rx_cache * cache)
  3284. #else
  3285. static char *
  3286. rx_cache_get_superstate (cache)
  3287.       struct rx_cache * cache;
  3288. #endif
  3289. {
  3290.   char * answer;
  3291.   int bytes = (   sizeof (struct rx_superstate)
  3292.            +  cache->local_cset_size * sizeof (struct rx_inx));
  3293.   if (!cache->free_superstates
  3294.       && (cache->superstates < cache->superstates_allowed))
  3295.     {
  3296.       answer = rx_cache_malloc (cache, bytes);
  3297.       if (answer)
  3298.     {
  3299.       ++cache->superstates;
  3300.       return answer;
  3301.     }
  3302.     }
  3303.   answer = rx_cache_get (cache, &cache->free_superstates);
  3304.   if (!answer)
  3305.     {
  3306.       answer = rx_cache_malloc (cache, bytes);
  3307.       if (answer)
  3308.     ++cache->superstates_allowed;
  3309.     }
  3310.   ++cache->superstates;
  3311.   return answer;
  3312. }
  3313.  
  3314.  
  3315.  
  3316. static int
  3317. supersetcmp (va, vb)
  3318.      void * va;
  3319.      void * vb;
  3320. {
  3321.   struct rx_superset * a = (struct rx_superset *)va;
  3322.   struct rx_superset * b = (struct rx_superset *)vb;
  3323.   return (   (a == b)
  3324.       || (a && b && (a->car == b->car) && (a->cdr == b->cdr)));
  3325. }
  3326.  
  3327.  
  3328. #ifdef __STDC__
  3329. static struct rx_hash_item *
  3330. superset_allocator (struct rx_hash_rules * rules, void * val)
  3331. #else
  3332. static struct rx_hash_item *
  3333. superset_allocator (rules, val)
  3334.      struct rx_hash_rules * rules;
  3335.      void * val;
  3336. #endif
  3337. {
  3338.   struct rx_cache * cache
  3339.     = ((struct rx_cache *)
  3340.        ((char *)rules
  3341.     - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules)));
  3342.   struct rx_superset * template = (struct rx_superset *)val;
  3343.   struct rx_superset * newset
  3344.     = ((struct rx_superset *)
  3345.        rx_cache_malloc_or_get (cache,
  3346.                    &cache->free_supersets,
  3347.                    sizeof (*template)));
  3348.   newset->refs = 0;
  3349.   newset->car = template->car;
  3350.   newset->id = template->car->id;
  3351.   newset->cdr = template->cdr;
  3352.   newset->superstate = 0;
  3353.   rx_protect_superset (rx, template->cdr);
  3354.   newset->hash_item.data = (void *)newset;
  3355.   newset->hash_item.binding = 0;
  3356.   return &newset->hash_item;
  3357. }
  3358.  
  3359. #ifdef __STDC__
  3360. static struct rx_hash * 
  3361. super_hash_allocator (struct rx_hash_rules * rules)
  3362. #else
  3363. static struct rx_hash * 
  3364. super_hash_allocator (rules)
  3365.      struct rx_hash_rules * rules;
  3366. #endif
  3367. {
  3368.   struct rx_cache * cache
  3369.     = ((struct rx_cache *)
  3370.        ((char *)rules
  3371.     - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules)));
  3372.   return ((struct rx_hash *)
  3373.       rx_cache_malloc_or_get (cache,
  3374.                   &cache->free_hash, sizeof (struct rx_hash)));
  3375. }
  3376.  
  3377.  
  3378. #ifdef __STDC__
  3379. static void
  3380. super_hash_liberator (struct rx_hash * hash, struct rx_hash_rules * rules)
  3381. #else
  3382. static void
  3383. super_hash_liberator (hash, rules)
  3384.      struct rx_hash * hash;
  3385.      struct rx_hash_rules * rules;
  3386. #endif
  3387. {
  3388.   struct rx_cache * cache
  3389.     = ((struct rx_cache *)
  3390.        (char *)rules - (long)(&((struct rx_cache *)0)->superset_hash_rules));
  3391.   rx_cache_free (cache, &cache->free_hash, (char *)hash);
  3392. }
  3393.  
  3394. #ifdef __STDC__
  3395. static void
  3396. superset_hash_item_liberator (struct rx_hash_item * it,
  3397.                   struct rx_hash_rules * rules)
  3398. #else
  3399. static void
  3400. superset_hash_item_liberator (it, rules) /* Well, it does ya know. */
  3401.      struct rx_hash_item * it;
  3402.      struct rx_hash_rules * rules;
  3403. #endif
  3404. {
  3405. }
  3406.  
  3407. int rx_cache_bound = 128;
  3408. static int rx_default_cache_got = 0;
  3409.  
  3410. #ifdef __STDC__
  3411. static int
  3412. bytes_for_cache_size (int supers, int cset_size)
  3413. #else
  3414. static int
  3415. bytes_for_cache_size (supers, cset_size)
  3416.      int supers;
  3417.      int cset_size;
  3418. #endif
  3419. {
  3420.   return (int)
  3421.     ((float)supers *
  3422.      (  (1.03 * (float) (  rx_sizeof_bitset (cset_size)
  3423.              + sizeof (struct rx_super_edge)))
  3424.       + (1.80 * (float) sizeof (struct rx_possible_future))
  3425.       + (float) (  sizeof (struct rx_superstate)
  3426.          + cset_size * sizeof (struct rx_inx))));
  3427. }
  3428.  
  3429. #ifdef __STDC__
  3430. static void
  3431. rx_morecore (struct rx_cache * cache)
  3432. #else
  3433. static void
  3434. rx_morecore (cache)
  3435.      struct rx_cache * cache;
  3436. #endif
  3437. {
  3438.   if (rx_default_cache_got >= rx_cache_bound)
  3439.     return;
  3440.  
  3441.   rx_default_cache_got += 16;
  3442.   cache->superstates_allowed = rx_cache_bound;
  3443.   {
  3444.     struct rx_blocklist ** pos = &cache->memory;
  3445.     int size = bytes_for_cache_size (16, cache->local_cset_size);
  3446.     while (*pos)
  3447.       pos = &(*pos)->next;
  3448.     *pos = ((struct rx_blocklist *)
  3449.         malloc (size + sizeof (struct rx_blocklist))); 
  3450.     if (!*pos)
  3451.       return;
  3452.  
  3453.     (*pos)->next = 0;
  3454.     (*pos)->bytes = size;
  3455.     cache->memory_pos = *pos;
  3456.     cache->memory_addr = (char *)*pos + sizeof (**pos);
  3457.     cache->bytes_left = size;
  3458.   }
  3459. }
  3460.  
  3461. static struct rx_cache default_cache = 
  3462. {
  3463.   {
  3464.     supersetcmp,
  3465.     super_hash_allocator,
  3466.     super_hash_liberator,
  3467.     superset_allocator,
  3468.     superset_hash_item_liberator,
  3469.   },
  3470.   0,
  3471.   0,
  3472.   0,
  3473.   0,
  3474.   rx_morecore,
  3475.  
  3476.   0,
  3477.   0,
  3478.   0,
  3479.   0,
  3480.   0,
  3481.  
  3482.   0,
  3483.   0,
  3484.  
  3485.   0,
  3486.  
  3487.   0,
  3488.   0,
  3489.   0,
  3490.   0,
  3491.   128,
  3492.  
  3493.   256,
  3494.   rx_id_instruction_table,
  3495.  
  3496.   {
  3497.     0,
  3498.     0,
  3499.     {0},
  3500.     {0},
  3501.     {0}
  3502.   }
  3503. };
  3504.  
  3505. /* This adds an element to a superstate set.  These sets are lists, such
  3506.  * that lists with == elements are ==.  The empty set is returned by
  3507.  * superset_cons (rx, 0, 0) and is NOT equivelent to 
  3508.  * (struct rx_superset)0.
  3509.  */
  3510.  
  3511. #ifdef __STDC__
  3512. RX_DECL struct rx_superset *
  3513. rx_superset_cons (struct rx * rx,
  3514.           struct rx_nfa_state *car, struct rx_superset *cdr)
  3515. #else
  3516. RX_DECL struct rx_superset *
  3517. rx_superset_cons (rx, car, cdr)
  3518.      struct rx * rx;
  3519.      struct rx_nfa_state *car;
  3520.      struct rx_superset *cdr;
  3521. #endif
  3522. {
  3523.   struct rx_cache * cache = rx->cache;
  3524.   if (!car && !cdr)
  3525.     {
  3526.       if (!cache->empty_superset)
  3527.     {
  3528.       cache->empty_superset
  3529.         = ((struct rx_superset *)
  3530.            rx_cache_malloc_or_get (cache, &cache->free_supersets,
  3531.                        sizeof (struct rx_superset)));
  3532.       if (!cache->empty_superset)
  3533.         return 0;
  3534.       bzero (cache->empty_superset, sizeof (struct rx_superset));
  3535.       cache->empty_superset->refs = 1000;
  3536.     }
  3537.       return cache->empty_superset;
  3538.     }
  3539.   {
  3540.     struct rx_superset template;
  3541.     struct rx_hash_item * hit;
  3542.     template.car = car;
  3543.     template.cdr = cdr;
  3544.     template.id = car->id;
  3545.     hit = rx_hash_store (&cache->superset_table,
  3546.              (unsigned long)car ^ car->id ^ (unsigned long)cdr,
  3547.              (void *)&template,
  3548.              &cache->superset_hash_rules);
  3549.     return (hit
  3550.         ?  (struct rx_superset *)hit->data
  3551.         : 0);
  3552.   }
  3553. }
  3554.  
  3555. /* This computes a union of two NFA state sets.  The sets do not have the
  3556.  * same representation though.  One is a RX_SUPERSET structure (part
  3557.  * of the superstate NFA) and the other is an NFA_STATE_SET (part of the NFA).
  3558.  */
  3559.  
  3560. #ifdef __STDC__
  3561. RX_DECL struct rx_superset *
  3562. rx_superstate_eclosure_union
  3563.   (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl) 
  3564. #else
  3565. RX_DECL struct rx_superset *
  3566. rx_superstate_eclosure_union (rx, set, ecl)
  3567.      struct rx * rx;
  3568.      struct rx_superset *set;
  3569.      struct rx_nfa_state_set *ecl;
  3570. #endif
  3571. {
  3572.   if (!ecl)
  3573.     return set;
  3574.   if (!set->car)
  3575.     return rx_superset_cons (rx, ecl->car,
  3576.                 rx_superstate_eclosure_union (rx, set, ecl->cdr));
  3577.   {
  3578.     if (set->car == ecl->car)
  3579.       return rx_superstate_eclosure_union (rx, set, ecl->cdr);
  3580.     if (set->car > ecl->car)
  3581.       return rx_superset_cons (rx, set->car,
  3582.                   rx_superstate_eclosure_union (rx,
  3583.                                 set->cdr, ecl));
  3584.     return rx_superset_cons (rx, ecl->car,
  3585.                 rx_superstate_eclosure_union (rx, set, ecl->cdr));
  3586.   }
  3587. }
  3588.  
  3589.  
  3590. #ifdef __STDC__
  3591. RX_DECL void 
  3592. rx_release_superset (struct rx *rx,
  3593.              struct rx_superset *set)
  3594. #else
  3595. RX_DECL void 
  3596. rx_release_superset (rx, set)
  3597.      struct rx *rx;
  3598.      struct rx_superset *set;
  3599. #endif
  3600. {
  3601.   release_superset_low (rx->cache, set);
  3602. }
  3603.  
  3604.  
  3605.  
  3606. /*
  3607.  * This makes sure that a list of rx_distinct_futures contains
  3608.  * a future for each possible set of side effects in the eclosure
  3609.  * of a given state.  This is some of the work of filling in a
  3610.  * superstate transition. 
  3611.  */
  3612.  
  3613. #ifdef __STDC__
  3614. static struct rx_distinct_future *
  3615. include_futures (struct rx *rx,
  3616.          struct rx_distinct_future *df, struct rx_nfa_state
  3617.          *state, struct rx_superstate *superstate) 
  3618. #else
  3619. static struct rx_distinct_future *
  3620. include_futures (rx, df, state, superstate)
  3621.      struct rx *rx;
  3622.      struct rx_distinct_future *df;
  3623.      struct rx_nfa_state *state;
  3624.      struct rx_superstate *superstate;
  3625. #endif
  3626. {
  3627.   struct rx_possible_future *future;
  3628.   struct rx_cache * cache = rx->cache;
  3629.   for (future = state->futures; future; future = future->next)
  3630.     {
  3631.       struct rx_distinct_future *dfp;
  3632.       struct rx_distinct_future *insert_before = 0;
  3633.       if (df)
  3634.     df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  3635.       for (dfp = df; dfp; dfp = dfp->next_same_super_edge[0])
  3636.     if (dfp->effects == future->effects)
  3637.       break;
  3638.     else
  3639.       {
  3640.         int order = rx->se_list_cmp (rx, dfp->effects, future->effects);
  3641.         if (order > 0)
  3642.           {
  3643.         insert_before = dfp;
  3644.         dfp = 0;
  3645.         break;
  3646.           }
  3647.       }
  3648.       if (df)
  3649.     df->next_same_super_edge[1]->next_same_super_edge[0] = df;
  3650.       if (!dfp)
  3651.     {
  3652.       dfp
  3653.         = ((struct rx_distinct_future *)
  3654.            rx_cache_malloc_or_get (cache, &cache->free_discernable_futures,
  3655.                        sizeof (struct rx_distinct_future)));
  3656.       if (!dfp)
  3657.         return 0;
  3658.       if (!df)
  3659.         {
  3660.           df = insert_before = dfp;
  3661.           df->next_same_super_edge[0] = df->next_same_super_edge[1] = df;
  3662.         }
  3663.       else if (!insert_before)
  3664.         insert_before = df;
  3665.       else if (insert_before == df)
  3666.         df = dfp;
  3667.  
  3668.       dfp->next_same_super_edge[0] = insert_before;
  3669.       dfp->next_same_super_edge[1]
  3670.         = insert_before->next_same_super_edge[1];
  3671.       dfp->next_same_super_edge[1]->next_same_super_edge[0] = dfp;
  3672.       dfp->next_same_super_edge[0]->next_same_super_edge[1] = dfp;
  3673.       dfp->next_same_dest = dfp->prev_same_dest = dfp;
  3674.       dfp->future = 0;
  3675.       dfp->present = superstate;
  3676.       dfp->future_frame.inx = rx->instruction_table[rx_cache_miss];
  3677.       dfp->future_frame.data = 0;
  3678.       dfp->future_frame.data_2 = (void *) dfp;
  3679.       dfp->side_effects_frame.inx
  3680.         = rx->instruction_table[rx_do_side_effects];
  3681.       dfp->side_effects_frame.data = 0;
  3682.       dfp->side_effects_frame.data_2 = (void *) dfp;
  3683.       dfp->effects = future->effects;
  3684.     }
  3685.     }
  3686.   return df;
  3687. }
  3688.  
  3689.  
  3690.  
  3691.  
  3692. /* This constructs a new superstate from its state set.  The only 
  3693.  * complexity here is memory management.
  3694.  */
  3695. #ifdef __STDC__
  3696. RX_DECL struct rx_superstate *
  3697. rx_superstate (struct rx *rx,
  3698.            struct rx_superset *set)
  3699. #else
  3700. RX_DECL struct rx_superstate *
  3701. rx_superstate (rx, set)
  3702.      struct rx *rx;
  3703.      struct rx_superset *set;
  3704. #endif
  3705. {
  3706.   struct rx_cache * cache = rx->cache;
  3707.   struct rx_superstate * superstate = 0;
  3708.  
  3709.   /* Does the superstate already exist in the cache? */
  3710.   if (set->superstate)
  3711.     {
  3712.       if (set->superstate->rx_id != rx->rx_id)
  3713.     {
  3714.       /* Aha.  It is in the cache, but belongs to a superstate
  3715.        * that refers to an NFA that no longer exists.
  3716.        * (We know it no longer exists because it was evidently
  3717.        *  stored in the same region of memory as the current nfa
  3718.        *  yet it has a different id.)
  3719.        */
  3720.       superstate = set->superstate;
  3721.       if (!superstate->is_semifree)
  3722.         {
  3723.           if (cache->lru_superstate == superstate)
  3724.         {
  3725.           cache->lru_superstate = superstate->next_recyclable;
  3726.           if (cache->lru_superstate == superstate)
  3727.             cache->lru_superstate = 0;
  3728.         }
  3729.           {
  3730.         superstate->next_recyclable->prev_recyclable
  3731.           = superstate->prev_recyclable;
  3732.         superstate->prev_recyclable->next_recyclable
  3733.           = superstate->next_recyclable;
  3734.         if (!cache->semifree_superstate)
  3735.           {
  3736.             (cache->semifree_superstate
  3737.              = superstate->next_recyclable
  3738.              = superstate->prev_recyclable
  3739.              = superstate);
  3740.           }
  3741.         else
  3742.           {
  3743.             superstate->next_recyclable = cache->semifree_superstate;
  3744.             superstate->prev_recyclable
  3745.               = cache->semifree_superstate->prev_recyclable;
  3746.             superstate->next_recyclable->prev_recyclable
  3747.               = superstate;
  3748.             superstate->prev_recyclable->next_recyclable
  3749.               = superstate;
  3750.             cache->semifree_superstate = superstate;
  3751.           }
  3752.         ++cache->semifree_superstates;
  3753.           }
  3754.         }
  3755.       set->superstate = 0;
  3756.       goto handle_cache_miss;
  3757.     }
  3758.       ++cache->hits;
  3759.       superstate = set->superstate;
  3760.  
  3761.       rx_refresh_this_superstate (cache, superstate);
  3762.       return superstate;
  3763.     }
  3764.  
  3765.  handle_cache_miss:
  3766.  
  3767.   /* This point reached only for cache misses. */
  3768.   ++cache->misses;
  3769. #if RX_DEBUG
  3770.   if (rx_debug_trace > 1)
  3771.     {
  3772.       struct rx_superset * setp = set;
  3773.       fprintf (stderr, "Building a superstet %d(%d): ", rx->rx_id, set);
  3774.       while (setp)
  3775.     {
  3776.       fprintf (stderr, "%d ", setp->id);
  3777.       setp = setp->cdr;
  3778.     }
  3779.       fprintf (stderr, "(%d)\n", set);
  3780.     }
  3781. #endif
  3782.   superstate = (struct rx_superstate *)rx_cache_get_superstate (cache);
  3783.   if (!superstate)
  3784.     return 0;
  3785.  
  3786.   if (!cache->lru_superstate)
  3787.     (cache->lru_superstate
  3788.      = superstate->next_recyclable
  3789.      = superstate->prev_recyclable
  3790.      = superstate);
  3791.   else
  3792.     {
  3793.       superstate->next_recyclable = cache->lru_superstate;
  3794.       superstate->prev_recyclable = cache->lru_superstate->prev_recyclable;
  3795.       (  superstate->prev_recyclable->next_recyclable
  3796.        = superstate->next_recyclable->prev_recyclable
  3797.        = superstate);
  3798.     }
  3799.   superstate->rx_id = rx->rx_id;
  3800.   superstate->transition_refs = 0;
  3801.   superstate->locks = 0;
  3802.   superstate->is_semifree = 0;
  3803.   set->superstate = superstate;
  3804.   superstate->contents = set;
  3805.   rx_protect_superset (rx, set);
  3806.   superstate->edges = 0;
  3807.   {
  3808.     int x;
  3809.     /* None of the transitions from this superstate are known yet. */
  3810.     for (x = 0; x < rx->local_cset_size; ++x) /* &&&&& 3.8 % */
  3811.       {
  3812.     struct rx_inx * ifr = &superstate->transitions[x];
  3813.     ifr->inx = rx->instruction_table [rx_cache_miss];
  3814.     ifr->data = ifr->data_2 = 0;
  3815.       }
  3816.   }
  3817.   return superstate;
  3818. }
  3819.  
  3820.  
  3821. /* This computes the destination set of one edge of the superstate NFA.
  3822.  * Note that a RX_DISTINCT_FUTURE is a superstate edge.
  3823.  * Returns 0 on an allocation failure.
  3824.  */
  3825.  
  3826. #ifdef __STDC__
  3827. static int 
  3828. solve_destination (struct rx *rx, struct rx_distinct_future *df)
  3829. #else
  3830. static int 
  3831. solve_destination (rx, df)
  3832.      struct rx *rx;
  3833.      struct rx_distinct_future *df;
  3834. #endif
  3835. {
  3836.   struct rx_super_edge *tc = df->edge;
  3837.   struct rx_superset *nfa_state;
  3838.   struct rx_superset *nil_set = rx_superset_cons (rx, 0, 0);
  3839.   struct rx_superset *solution = nil_set;
  3840.   struct rx_superstate *dest;
  3841.  
  3842.   /* Iterate over all NFA states in the state set of this superstate. */
  3843.   for (nfa_state = df->present->contents;
  3844.        nfa_state->car;
  3845.        nfa_state = nfa_state->cdr)
  3846.     {
  3847.       struct rx_nfa_edge *e;
  3848.       /* Iterate over all edges of each NFA state. */
  3849.       for (e = nfa_state->car->edges; e; e = e->next)
  3850.         /* If we find an edge that is labeled with 
  3851.      * the characters we are solving for.....
  3852.      */
  3853.     if (rx_bitset_is_subset (rx->local_cset_size,
  3854.                  tc->cset, e->params.cset))
  3855.       {
  3856.         struct rx_nfa_state *n = e->dest;
  3857.         struct rx_possible_future *pf;
  3858.         /* ....search the partial epsilon closures of the destination
  3859.          * of that edge for a path that involves the same set of
  3860.          * side effects we are solving for.
  3861.          * If we find such a RX_POSSIBLE_FUTURE, we add members to the
  3862.          * stateset we are computing.
  3863.          */
  3864.         for (pf = n->futures; pf; pf = pf->next)
  3865.           if (pf->effects == df->effects)
  3866.         {
  3867.           struct rx_superset * old_sol = solution;
  3868.           rx_protect_superset (rx, solution);
  3869.           solution =
  3870.             rx_superstate_eclosure_union (rx, solution, pf->destset);
  3871.           if (!solution)
  3872.             return 0;
  3873.           rx_release_superset (rx, old_sol);
  3874.         }
  3875.       }
  3876.     }
  3877.   /* It is possible that the RX_DISTINCT_FUTURE we are working on has 
  3878.    * the empty set of NFA states as its definition.  In that case, this
  3879.    * is a failure point.
  3880.    */
  3881.   if (solution == nil_set)
  3882.     {
  3883.       df->future_frame.inx = (void *) rx_backtrack;
  3884.       df->future_frame.data = 0;
  3885.       df->future_frame.data_2 = 0;
  3886.       return 1;
  3887.     }
  3888.   rx_protect_superset (rx, solution);
  3889.   dest = rx_superstate (rx, solution);
  3890.   rx_release_superset (rx, solution);
  3891.   if (!dest)
  3892.     return 0;
  3893.  
  3894.   {
  3895.     struct rx_distinct_future *dft;
  3896.     dft = df;
  3897.     df->prev_same_dest->next_same_dest = 0;
  3898.     while (dft)
  3899.       {
  3900.     dft->future = dest;
  3901.     dft->future_frame.inx = rx->instruction_table[rx_next_char];
  3902.     dft->future_frame.data = (void *) dest->transitions;
  3903.     dft = dft->next_same_dest;
  3904.       }
  3905.     df->prev_same_dest->next_same_dest = df;
  3906.   }
  3907.   if (!dest->transition_refs)
  3908.     dest->transition_refs = df;
  3909.   else
  3910.     {
  3911.       struct rx_distinct_future *dft = dest->transition_refs->next_same_dest;
  3912.       dest->transition_refs->next_same_dest = df->next_same_dest;
  3913.       df->next_same_dest->prev_same_dest = dest->transition_refs;
  3914.       df->next_same_dest = dft;
  3915.       dft->prev_same_dest = df;
  3916.     }
  3917.   return 1;
  3918. }
  3919.  
  3920.  
  3921. /* This takes a superstate and a character, and computes some edges
  3922.  * from the superstate NFA.  In particular, this computes all edges
  3923.  * that lead from SUPERSTATE given CHR.   This function also 
  3924.  * computes the set of characters that share this edge set.
  3925.  * This returns 0 on allocation error.
  3926.  * The character set and list of edges are returned through 
  3927.  * the paramters CSETOUT and DFOUT.
  3928. } */
  3929.  
  3930. #ifdef __STDC__
  3931. static int 
  3932. compute_super_edge (struct rx *rx, struct rx_distinct_future **dfout,
  3933.               rx_Bitset csetout, struct rx_superstate *superstate,
  3934.               unsigned char chr)  
  3935. #else
  3936. static int 
  3937. compute_super_edge (rx, dfout, csetout, superstate, chr)
  3938.      struct rx *rx;
  3939.      struct rx_distinct_future **dfout;
  3940.      rx_Bitset csetout;
  3941.      struct rx_superstate *superstate;
  3942.      unsigned char chr;
  3943. #endif
  3944. {
  3945.   struct rx_superset *stateset = superstate->contents;
  3946.  
  3947.   /* To compute the set of characters that share edges with CHR, 
  3948.    * we start with the full character set, and subtract.
  3949.    */
  3950.   rx_bitset_universe (rx->local_cset_size, csetout);
  3951.   *dfout = 0;
  3952.  
  3953.   /* Iterate over the NFA states in the superstate state-set. */
  3954.   while (stateset->car)
  3955.     {
  3956.       struct rx_nfa_edge *e;
  3957.       for (e = stateset->car->edges; e; e = e->next)
  3958.     if (RX_bitset_member (e->params.cset, chr))
  3959.       {
  3960.         /* If we find an NFA edge that applies, we make sure there
  3961.          * are corresponding edges in the superstate NFA.
  3962.          */
  3963.         *dfout = include_futures (rx, *dfout, e->dest, superstate);
  3964.         if (!*dfout)
  3965.           return 0;
  3966.         /* We also trim the character set a bit. */
  3967.         rx_bitset_intersection (rx->local_cset_size,
  3968.                     csetout, e->params.cset);
  3969.       }
  3970.     else
  3971.       /* An edge that doesn't apply at least tells us some characters
  3972.        * that don't share the same edge set as CHR.
  3973.        */
  3974.       rx_bitset_difference (rx->local_cset_size, csetout, e->params.cset);
  3975.       stateset = stateset->cdr;
  3976.     }
  3977.   return 1;
  3978. }
  3979.  
  3980.  
  3981. /* This is a constructor for RX_SUPER_EDGE structures.  These are
  3982.  * wrappers for lists of superstate NFA edges that share character sets labels.
  3983.  * If a transition class contains more than one rx_distinct_future (superstate
  3984.  * edge), then it represents a non-determinism in the superstate NFA.
  3985.  */
  3986.  
  3987. #ifdef __STDC__
  3988. static struct rx_super_edge *
  3989. rx_super_edge (struct rx *rx,
  3990.            struct rx_superstate *super, rx_Bitset cset,
  3991.            struct rx_distinct_future *df) 
  3992. #else
  3993. static struct rx_super_edge *
  3994. rx_super_edge (rx, super, cset, df)
  3995.      struct rx *rx;
  3996.      struct rx_superstate *super;
  3997.      rx_Bitset cset;
  3998.      struct rx_distinct_future *df;
  3999. #endif
  4000. {
  4001.   struct rx_super_edge *tc =
  4002.     (struct rx_super_edge *)rx_cache_malloc_or_get
  4003.       (rx->cache, &rx->cache->free_transition_classes,
  4004.        sizeof (struct rx_super_edge) + rx_sizeof_bitset (rx->local_cset_size));
  4005.  
  4006.   if (!tc)
  4007.     return 0;
  4008.   tc->next = super->edges;
  4009.   super->edges = tc;
  4010.   tc->rx_backtrack_frame.inx = rx->instruction_table[rx_backtrack_point];
  4011.   tc->rx_backtrack_frame.data = 0;
  4012.   tc->rx_backtrack_frame.data_2 = (void *) tc;
  4013.   tc->options = df;
  4014.   tc->cset = (rx_Bitset) ((char *) tc + sizeof (*tc));
  4015.   rx_bitset_assign (rx->local_cset_size, tc->cset, cset);
  4016.   if (df)
  4017.     {
  4018.       struct rx_distinct_future * dfp = df;
  4019.       df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  4020.       while (dfp)
  4021.     {
  4022.       dfp->edge = tc;
  4023.       dfp = dfp->next_same_super_edge[0];
  4024.     }
  4025.       df->next_same_super_edge[1]->next_same_super_edge[0] = df;
  4026.     }
  4027.   return tc;
  4028. }
  4029.  
  4030.  
  4031. /* There are three kinds of cache miss.  The first occurs when a
  4032.  * transition is taken that has never been computed during the
  4033.  * lifetime of the source superstate.  That cache miss is handled by
  4034.  * calling COMPUTE_SUPER_EDGE.  The second kind of cache miss
  4035.  * occurs when the destination superstate of a transition doesn't
  4036.  * exist.  SOLVE_DESTINATION is used to construct the destination superstate.
  4037.  * Finally, the third kind of cache miss occurs when the destination
  4038.  * superstate of a transition is in a `semi-free state'.  That case is
  4039.  * handled by UNFREE_SUPERSTATE.
  4040.  *
  4041.  * The function of HANDLE_CACHE_MISS is to figure out which of these
  4042.  * cases applies.
  4043.  */
  4044.  
  4045. #ifdef __STDC__
  4046. static void
  4047. install_partial_transition  (struct rx_superstate *super,
  4048.                  struct rx_inx *answer,
  4049.                  RX_subset set, int offset)
  4050. #else
  4051. static void
  4052. install_partial_transition  (super, answer, set, offset)
  4053.      struct rx_superstate *super;
  4054.      struct rx_inx *answer;
  4055.      RX_subset set;
  4056.      int offset;
  4057. #endif
  4058. {
  4059.   int start = offset;
  4060.   int end = start + 32;
  4061.   RX_subset pos = 1;
  4062.   struct rx_inx * transitions = super->transitions;
  4063.   
  4064.   while (start < end)
  4065.     {
  4066.       if (set & pos)
  4067.     transitions[start] = *answer;
  4068.       pos <<= 1;
  4069.       ++start;
  4070.     }
  4071. }
  4072.  
  4073.  
  4074. #ifdef __STDC__
  4075. RX_DECL struct rx_inx *
  4076. rx_handle_cache_miss
  4077.   (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data) 
  4078. #else
  4079. RX_DECL struct rx_inx *
  4080. rx_handle_cache_miss (rx, super, chr, data)
  4081.      struct rx *rx;
  4082.      struct rx_superstate *super;
  4083.      unsigned char chr;
  4084.      void *data;
  4085. #endif
  4086. {
  4087.   int offset = chr / RX_subset_bits;
  4088.   struct rx_distinct_future *df = data;
  4089.  
  4090.   if (!df)            /* must be the shared_cache_miss_frame */
  4091.     {
  4092.       /* Perhaps this is just a transition waiting to be filled. */
  4093.       struct rx_super_edge *tc;
  4094.       RX_subset mask = rx_subset_singletons [chr % RX_subset_bits];
  4095.  
  4096.       for (tc = super->edges; tc; tc = tc->next)
  4097.     if (tc->cset[offset] & mask)
  4098.       {
  4099.         struct rx_inx * answer;
  4100.         df = tc->options;
  4101.         answer = ((tc->options->next_same_super_edge[0] != tc->options)
  4102.               ? &tc->rx_backtrack_frame
  4103.               : (df->effects
  4104.              ? &df->side_effects_frame
  4105.              : &df->future_frame));
  4106.         install_partial_transition (super, answer,
  4107.                     tc->cset [offset], offset * 32);
  4108.         return answer;
  4109.       }
  4110.       /* Otherwise, it's a flushed or  newly encountered edge. */
  4111.       {
  4112.     rx_Bitset trcset = rx_cset (rx);
  4113.     struct rx_inx *answer;
  4114.     if (!trcset)
  4115.       return 0;
  4116.     rx_lock_superstate (rx, super);
  4117.     if (!compute_super_edge (rx, &df, trcset, super, chr))
  4118.       {
  4119.         rx_unlock_superstate (rx, super);
  4120.         return 0;
  4121.       }
  4122.     if (!df)        /* We just computed the fail transition. */
  4123.       {
  4124.         static struct rx_inx
  4125.           shared_fail_frame = { (void *)rx_backtrack, 0, 0 };
  4126.         answer = &shared_fail_frame;
  4127.       }
  4128.     else
  4129.       {
  4130.         tc = rx_super_edge (rx, super, trcset, df);
  4131.         if (!tc)
  4132.           {
  4133.         rx_unlock_superstate (rx, super);
  4134.         return 0;
  4135.           }
  4136.         answer = ((tc->options->next_same_super_edge[0] != tc->options)
  4137.               ? &tc->rx_backtrack_frame
  4138.               : (df->effects
  4139.              ? &df->side_effects_frame
  4140.              : &df->future_frame));
  4141.       }
  4142.     install_partial_transition (super, answer,
  4143.                     trcset[offset], offset * 32);
  4144.     rx_free_cset (rx, trcset);
  4145.     rx_unlock_superstate (rx, super);
  4146.     return answer;
  4147.       }
  4148.     }
  4149.   else if (df->future) /* A cache miss on an edge with a future? Must be
  4150.             * a semi-free destination. */
  4151.     {                
  4152.       if (df->future->is_semifree)
  4153.     refresh_semifree_superstate (rx->cache, df->future);
  4154.       return &df->future_frame;
  4155.     }
  4156.   else
  4157.     /* no future superstate on an existing edge */
  4158.     {
  4159.       rx_lock_superstate (rx, super);
  4160.       if (!solve_destination (rx, df))
  4161.     {
  4162.       rx_unlock_superstate (rx, super);
  4163.       return 0;
  4164.     }
  4165.       if (!df->effects
  4166.       && (df->edge->options->next_same_super_edge[0] == df->edge->options))
  4167.     install_partial_transition (super, &df->future_frame,
  4168.                     df->edge->cset[offset], offset * 32);
  4169.       rx_unlock_superstate (rx, super);
  4170.       return &df->future_frame;
  4171.     }
  4172. }
  4173.  
  4174.  
  4175.  
  4176.  
  4177. /* The rest of the code provides a regex.c compatable interface. */
  4178.  
  4179.  
  4180. const char *re_error_msg[] =
  4181. {
  4182.   0,                        /* REG_NOUT */
  4183.   "No match",                    /* REG_NOMATCH */
  4184.   "Invalid regular expression",            /* REG_BADPAT */
  4185.   "Invalid collation character",        /* REG_ECOLLATE */
  4186.   "Invalid character class name",        /* REG_ECTYPE */
  4187.   "Trailing backslash",                /* REG_EESCAPE */
  4188.   "Invalid back reference",            /* REG_ESUBREG */
  4189.   "Unmatched [ or [^",                /* REG_EBRACK */
  4190.   "Unmatched ( or \\(",                /* REG_EPAREN */
  4191.   "Unmatched \\{",                /* REG_EBRACE */
  4192.   "Invalid content of \\{\\}",            /* REG_BADBR */
  4193.   "Invalid range end",                /* REG_ERANGE */
  4194.   "Memory exhausted",                /* REG_ESPACE */
  4195.   "Invalid preceding regular expression",    /* REG_BADRPT */
  4196.   "Premature end of regular expression",    /* REG_EEND */
  4197.   "Regular expression too big",            /* REG_ESIZE */
  4198.   "Unmatched ) or \\)",                /* REG_ERPAREN */
  4199. };
  4200.  
  4201.  
  4202.  
  4203. /* 
  4204.  * Macros used while compiling patterns.
  4205.  *
  4206.  * By convention, PEND points just past the end of the uncompiled pattern,
  4207.  * P points to the read position in the pattern.  `translate' is the name
  4208.  * of the translation table (`TRANSLATE' is the name of a macro that looks
  4209.  * things up in `translate').
  4210.  */
  4211.  
  4212.  
  4213. /*
  4214.  * Fetch the next character in the uncompiled pattern---translating it 
  4215.  * if necessary. *Also cast from a signed character in the constant
  4216.  * string passed to us by the user to an unsigned char that we can use
  4217.  * as an array index (in, e.g., `translate').
  4218.  */
  4219. #define PATFETCH(c)                            \
  4220.  do {if (p == pend) return REG_EEND;                    \
  4221.     c = (unsigned char) *p++;                        \
  4222.     c = translate[c];                             \
  4223.  } while (0)
  4224.  
  4225. /* 
  4226.  * Fetch the next character in the uncompiled pattern, with no
  4227.  * translation.
  4228.  */
  4229. #define PATFETCH_RAW(c)                            \
  4230.   do {if (p == pend) return REG_EEND;                    \
  4231.     c = (unsigned char) *p++;                         \
  4232.   } while (0)
  4233.  
  4234. /* Go backwards one character in the pattern.  */
  4235. #define PATUNFETCH p--
  4236.  
  4237.  
  4238. #define TRANSLATE(d) translate[(unsigned char) (d)]
  4239.  
  4240. typedef unsigned regnum_t;
  4241.  
  4242. /* Since offsets can go either forwards or backwards, this type needs to
  4243.  * be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.
  4244.  */
  4245. typedef int pattern_offset_t;
  4246.  
  4247. typedef struct
  4248. {
  4249.   struct rexp_node ** top_expression; /* was begalt */
  4250.   struct rexp_node ** last_expression; /* was laststart */
  4251.   pattern_offset_t inner_group_offset;
  4252.   regnum_t regnum;
  4253. } compile_stack_elt_t;
  4254.  
  4255. typedef struct
  4256. {
  4257.   compile_stack_elt_t *stack;
  4258.   unsigned size;
  4259.   unsigned avail;            /* Offset of next open position.  */
  4260. } compile_stack_type;
  4261.  
  4262.  
  4263. #define INIT_COMPILE_STACK_SIZE 32
  4264.  
  4265. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  4266. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  4267.  
  4268. /* The next available element.  */
  4269. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  4270.  
  4271.  
  4272. /* Set the bit for character C in a list.  */
  4273. #define SET_LIST_BIT(c)                               \
  4274.   (b[((unsigned char) (c)) / BYTEWIDTH]               \
  4275.    |= 1 << (((unsigned char) c) % BYTEWIDTH))
  4276.  
  4277. /* Get the next unsigned number in the uncompiled pattern.  */
  4278. #define GET_UNSIGNED_NUMBER(num)                     \
  4279.   { if (p != pend)                            \
  4280.      {                                    \
  4281.        PATFETCH (c);                             \
  4282.        while (isdigit (c))                         \
  4283.          {                                 \
  4284.            if (num < 0)                            \
  4285.               num = 0;                            \
  4286.            num = num * 10 + c - '0';                     \
  4287.            if (p == pend)                         \
  4288.               break;                             \
  4289.            PATFETCH (c);                        \
  4290.          }                                 \
  4291.        }                                 \
  4292.     }        
  4293.  
  4294. #define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
  4295.  
  4296. #define IS_CHAR_CLASS(string)                        \
  4297.    (!strcmp (string, "alpha") || !strcmp (string, "upper")        \
  4298.     || !strcmp (string, "lower") || !strcmp (string, "digit")        \
  4299.     || !strcmp (string, "alnum") || !strcmp (string, "xdigit")        \
  4300.     || !strcmp (string, "space") || !strcmp (string, "print")        \
  4301.     || !strcmp (string, "punct") || !strcmp (string, "graph")        \
  4302.     || !strcmp (string, "cntrl") || !strcmp (string, "blank"))
  4303.  
  4304.  
  4305. /* These predicates are used in regex_compile. */
  4306.  
  4307. /* P points to just after a ^ in PATTERN.  Return true if that ^ comes
  4308.  * after an alternative or a begin-subexpression.  We assume there is at
  4309.  * least one character before the ^.  
  4310.  */
  4311.  
  4312. #ifdef __STDC__
  4313. static boolean
  4314. at_begline_loc_p (const char *pattern, const char * p, reg_syntax_t syntax)
  4315. #else
  4316. static boolean
  4317. at_begline_loc_p (pattern, p, syntax)
  4318.      const char *pattern;
  4319.      const char * p;
  4320.      reg_syntax_t syntax;
  4321. #endif
  4322. {
  4323.   const char *prev = p - 2;
  4324.   boolean prev_prev_backslash = ((prev > pattern) && (prev[-1] == '\\'));
  4325.   
  4326.     return
  4327.       
  4328.       (/* After a subexpression?  */
  4329.        ((*prev == '(') && ((syntax & RE_NO_BK_PARENS) || prev_prev_backslash))
  4330.        ||
  4331.        /* After an alternative?  */
  4332.        ((*prev == '|') && ((syntax & RE_NO_BK_VBAR) || prev_prev_backslash))
  4333.        );
  4334. }
  4335.  
  4336. /* The dual of at_begline_loc_p.  This one is for $.  We assume there is
  4337.  * at least one character after the $, i.e., `P < PEND'.
  4338.  */
  4339.  
  4340. #ifdef __STDC__
  4341. static boolean
  4342. at_endline_loc_p (const char *p, const char *pend, int syntax)
  4343. #else
  4344. static boolean
  4345. at_endline_loc_p (p, pend, syntax)
  4346.      const char *p;
  4347.      const char *pend;
  4348.      int syntax;
  4349. #endif
  4350. {
  4351.   const char *next = p;
  4352.   boolean next_backslash = (*next == '\\');
  4353.   const char *next_next = (p + 1 < pend) ? (p + 1) : 0;
  4354.   
  4355.   return
  4356.     (
  4357.      /* Before a subexpression?  */
  4358.      ((syntax & RE_NO_BK_PARENS)
  4359.       ? (*next == ')')
  4360.       : (next_backslash && next_next && (*next_next == ')')))
  4361.     ||
  4362.      /* Before an alternative?  */
  4363.      ((syntax & RE_NO_BK_VBAR)
  4364.       ? (*next == '|')
  4365.       : (next_backslash && next_next && (*next_next == '|')))
  4366.      );
  4367. }
  4368.  
  4369.  
  4370. static char id_translation[256] =
  4371. {
  4372.   0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
  4373.  10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  4374.  20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  4375.  30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  4376.  40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  4377.  50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  4378.  60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  4379.  70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  4380.  80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  4381.  90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  4382.  
  4383.  100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  4384.  110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  4385.  120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
  4386.  130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
  4387.  140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
  4388.  150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  4389.  160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
  4390.  170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
  4391.  180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
  4392.  190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
  4393.  
  4394.  200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
  4395.  210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  4396.  220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
  4397.  230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  4398.  240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
  4399.  250, 251, 252, 253, 254, 255
  4400. };
  4401.  
  4402. /* The compiler keeps an inverted translation table.
  4403.  * This looks up/inititalize elements.
  4404.  * VALID is an array of booleans that validate CACHE.
  4405.  */
  4406.  
  4407. #ifdef __STDC__
  4408. static rx_Bitset
  4409. inverse_translation (struct re_pattern_buffer * rxb,
  4410.              char * valid, rx_Bitset cache,
  4411.              char * translate, int c)
  4412. #else
  4413. static rx_Bitset
  4414. inverse_translation (rxb, valid, cache, translate, c)
  4415.      struct re_pattern_buffer * rxb;
  4416.      char * valid;
  4417.      rx_Bitset cache;
  4418.      char * translate;
  4419.      int c;
  4420. #endif
  4421. {
  4422.   rx_Bitset cs
  4423.     = cache + c * rx_bitset_numb_subsets (rxb->rx.local_cset_size); 
  4424.  
  4425.   if (!valid[c])
  4426.     {
  4427.       int x;
  4428.       int c_tr = TRANSLATE(c);
  4429.       rx_bitset_null (rxb->rx.local_cset_size, cs);
  4430.       for (x = 0; x < 256; ++x)    /* &&&& 13.37 */
  4431.     if (TRANSLATE(x) == c_tr)
  4432.       RX_bitset_enjoin (cs, x);
  4433.       valid[c] = 1;
  4434.     }
  4435.   return cs;
  4436. }
  4437.  
  4438.  
  4439.  
  4440.  
  4441. /* More subroutine declarations and macros for regex_compile.  */
  4442.  
  4443. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  4444.    false if it's not.  */
  4445.  
  4446. #ifdef __STDC__
  4447. static boolean
  4448. group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
  4449. #else
  4450. static boolean
  4451. group_in_compile_stack (compile_stack, regnum)
  4452.     compile_stack_type compile_stack;
  4453.     regnum_t regnum;
  4454. #endif
  4455. {
  4456.   int this_element;
  4457.  
  4458.   for (this_element = compile_stack.avail - 1;  
  4459.        this_element >= 0; 
  4460.        this_element--)
  4461.     if (compile_stack.stack[this_element].regnum == regnum)
  4462.       return true;
  4463.  
  4464.   return false;
  4465. }
  4466.  
  4467.  
  4468. /*
  4469.  * Read the ending character of a range (in a bracket expression) from the
  4470.  * uncompiled pattern *P_PTR (which ends at PEND).  We assume the
  4471.  * starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
  4472.  * Then we set the translation of all bits between the starting and
  4473.  * ending characters (inclusive) in the compiled pattern B.
  4474.  * 
  4475.  * Return an error code.
  4476.  * 
  4477.  * We use these short variable names so we can use the same macros as
  4478.  * `regex_compile' itself.  
  4479.  */
  4480.  
  4481. #ifdef __STDC__
  4482. static reg_errcode_t
  4483. compile_range (struct re_pattern_buffer * rxb, rx_Bitset cs,
  4484.            const char ** p_ptr, const char * pend,
  4485.            char * translate, reg_syntax_t syntax,
  4486.            rx_Bitset inv_tr,  char * valid_inv_tr)
  4487. #else
  4488. static reg_errcode_t
  4489. compile_range (rxb, cs, p_ptr, pend, translate, syntax, inv_tr, valid_inv_tr)
  4490.      struct re_pattern_buffer * rxb;
  4491.      rx_Bitset cs;
  4492.      const char ** p_ptr;
  4493.      const char * pend;
  4494.      char * translate;
  4495.      reg_syntax_t syntax;
  4496.      rx_Bitset inv_tr;
  4497.      char * valid_inv_tr;
  4498. #endif
  4499. {
  4500.   unsigned this_char;
  4501.  
  4502.   const char *p = *p_ptr;
  4503.  
  4504.   unsigned char range_end;
  4505.   unsigned char range_start = TRANSLATE(p[-2]);
  4506.  
  4507.   if (p == pend)
  4508.     return REG_ERANGE;
  4509.  
  4510.   PATFETCH (range_end);
  4511.  
  4512.   (*p_ptr)++;
  4513.  
  4514.   if (range_start > range_end)
  4515.     return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  4516.  
  4517.   for (this_char = range_start; this_char <= range_end; this_char++)
  4518.     {
  4519.       rx_Bitset it =
  4520.     inverse_translation (rxb, valid_inv_tr, inv_tr, translate, this_char);
  4521.       rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  4522.     }
  4523.   
  4524.   return REG_NOERROR;
  4525. }
  4526.  
  4527.  
  4528. /* This searches a regexp for backreference side effects.
  4529.  * It fills in the array OUT with 1 at the index of every register pair
  4530.  * referenced by a backreference.
  4531.  *
  4532.  * This is used to help optimize patterns for searching.  The information is
  4533.  * useful because, if the caller doesn't want register values, backreferenced
  4534.  * registers are the only registers for which we need rx_backtrack.
  4535.  */
  4536.  
  4537. #ifdef __STDC__
  4538. static void
  4539. find_backrefs (char * out, struct rexp_node * rexp,
  4540.            struct re_se_params * params)
  4541. #else
  4542. static void
  4543. find_backrefs (out, rexp, params)
  4544.      char * out;
  4545.      struct rexp_node * rexp;
  4546.      struct re_se_params * params;
  4547. #endif
  4548. {
  4549.   if (rexp)
  4550.     switch (rexp->type)
  4551.       {
  4552.       case r_cset:
  4553.       case r_data:
  4554.     return;
  4555.       case r_alternate:
  4556.       case r_concat:
  4557.       case r_opt:
  4558.       case r_star:
  4559.       case r_2phase_star:
  4560.     find_backrefs (out, rexp->params.pair.left, params);
  4561.     find_backrefs (out, rexp->params.pair.right, params);
  4562.     return;
  4563.       case r_side_effect:
  4564.     if (   ((int)rexp->params.side_effect >= 0)
  4565.         && (params [(int)rexp->params.side_effect].se == re_se_backref))
  4566.       out[ params [(int)rexp->params.side_effect].op1] = 1;
  4567.     return;
  4568.       }
  4569. }
  4570.  
  4571.  
  4572.  
  4573. /* Returns 0 unless the pattern can match the empty string. */
  4574.  
  4575. #ifdef __STDC__
  4576. static int
  4577. compute_fastset (struct re_pattern_buffer * rxb, struct rexp_node * rexp)
  4578. #else
  4579. static int
  4580. compute_fastset (rxb, rexp)
  4581.      struct re_pattern_buffer * rxb;
  4582.      struct rexp_node * rexp;
  4583. #endif
  4584. {
  4585.   if (!rexp)
  4586.     return 1;
  4587.   switch (rexp->type)
  4588.     {
  4589.     case r_data:
  4590.       return 1;
  4591.     case r_cset:
  4592.       {
  4593.     rx_bitset_union (rxb->rx.local_cset_size,
  4594.              rxb->fastset, rexp->params.cset);
  4595.       }
  4596.       return 0;
  4597.     case r_concat:
  4598.       return (compute_fastset (rxb, rexp->params.pair.left)
  4599.           && compute_fastset (rxb, rexp->params.pair.right));
  4600.     case r_2phase_star:
  4601.       compute_fastset (rxb, rexp->params.pair.left);
  4602.       /* compute_fastset (rxb, rexp->params.pair.right);  nope... */
  4603.       return 1;
  4604.     case r_alternate:
  4605.       return !!(compute_fastset (rxb, rexp->params.pair.left)
  4606.         + compute_fastset (rxb, rexp->params.pair.right));
  4607.     case r_opt:
  4608.     case r_star:
  4609.       compute_fastset (rxb, rexp->params.pair.left);
  4610.       return 1;
  4611.     case r_side_effect:
  4612.       return 1;
  4613.     }
  4614. }
  4615.  
  4616.  
  4617. /* returns
  4618.  *  1 -- yes, definately anchored by the given side effect.
  4619.  *  2 -- maybe anchored, maybe the empty string.
  4620.  *  0 -- definately not anchored
  4621.  *  There is simply no other possibility.
  4622.  */
  4623.  
  4624. #ifdef __STDC__
  4625. static int
  4626. is_anchored (struct rexp_node * rexp, rx_side_effect se)
  4627. #else
  4628. static int
  4629. is_anchored (rexp, se)
  4630.      struct rexp_node * rexp;
  4631.      rx_side_effect se;
  4632. #endif
  4633. {
  4634.   if (!rexp)
  4635.     return 2;
  4636.   switch (rexp->type)
  4637.     {
  4638.     case r_cset:
  4639.     case r_data:
  4640.       return 0;
  4641.     case r_concat:
  4642.     case r_2phase_star:
  4643.       {
  4644.     int l = is_anchored (rexp->params.pair.left, se);
  4645.     return (l == 2 ? is_anchored (rexp->params.pair.right, se) : l);
  4646.       }
  4647.     case r_alternate:
  4648.       {
  4649.     int l = is_anchored (rexp->params.pair.left, se);
  4650.     int r = l ? is_anchored (rexp->params.pair.right, se) : 0;
  4651.     return MAX (l, r);
  4652.       }
  4653.     case r_opt:
  4654.     case r_star:
  4655.       return is_anchored (rexp->params.pair.left, se) ? 2 : 0;
  4656.       
  4657.     case r_side_effect:
  4658.       return ((rexp->params.side_effect == se)
  4659.           ? 1 : 2);
  4660.     }
  4661. }
  4662.  
  4663.  
  4664. /* This removes register assignments that aren't required by backreferencing.
  4665.  * This can speed up explore_future, especially if it eliminates
  4666.  * non-determinism in the superstate NFA.
  4667.  * 
  4668.  * NEEDED is an array of characters, presumably filled in by FIND_BACKREFS.
  4669.  * The non-zero elements of the array indicate which register assignments
  4670.  * can NOT be removed from the expression.
  4671.  */
  4672.  
  4673. #ifdef __STDC__
  4674. static struct rexp_node *
  4675. remove_unecessary_side_effects (struct rx * rx, char * needed,
  4676.                 struct rexp_node * rexp,
  4677.                 struct re_se_params * params)
  4678. #else
  4679. static struct rexp_node *
  4680. remove_unecessary_side_effects (rx, needed, rexp, params)
  4681.      struct rx * rx;
  4682.      char * needed;
  4683.      struct rexp_node * rexp;
  4684.      struct re_se_params * params;
  4685. #endif
  4686. {
  4687.   struct rexp_node * l;
  4688.   struct rexp_node * r;
  4689.   if (!rexp)
  4690.     return 0;
  4691.   else
  4692.     switch (rexp->type)
  4693.       {
  4694.       case r_cset:
  4695.       case r_data:
  4696.     return rexp;
  4697.       case r_alternate:
  4698.       case r_concat:
  4699.       case r_2phase_star:
  4700.     l = remove_unecessary_side_effects (rx, needed,
  4701.                         rexp->params.pair.left, params);
  4702.     r = remove_unecessary_side_effects (rx, needed,
  4703.                         rexp->params.pair.right, params);
  4704.     if ((l && r) || (rexp->type != r_concat))
  4705.       {
  4706.         rexp->params.pair.left = l;
  4707.         rexp->params.pair.right = r;
  4708.         return rexp;
  4709.       }
  4710.     else
  4711.       {
  4712.         rexp->params.pair.left = rexp->params.pair.right = 0;
  4713.         rx_free_rexp (rx, rexp);
  4714.         return l ? l : r;
  4715.       }
  4716.       case r_opt:
  4717.       case r_star:
  4718.     l = remove_unecessary_side_effects (rx, needed,
  4719.                         rexp->params.pair.left, params);
  4720.     if (l)
  4721.       {
  4722.         rexp->params.pair.left = l;
  4723.         return rexp;
  4724.       }
  4725.     else
  4726.       {
  4727.         rexp->params.pair.left = 0;
  4728.         rx_free_rexp (rx, rexp);
  4729.         return 0;
  4730.       }
  4731.       case r_side_effect:
  4732.     {
  4733.       int se = (int)rexp->params.side_effect;
  4734.       if (   (se >= 0)
  4735.           && (   ((enum re_side_effects)params[se].se == re_se_lparen)
  4736.           || ((enum re_side_effects)params[se].se == re_se_rparen))
  4737.           && (params [se].op1 > 0)
  4738.           && (!needed [params [se].op1]))
  4739.         {
  4740.           rx_free_rexp (rx, rexp);
  4741.           return 0;
  4742.         }
  4743.       else
  4744.         return rexp;
  4745.     }
  4746.       }
  4747. }
  4748.  
  4749.  
  4750.  
  4751. #ifdef __STDC__
  4752. static int
  4753. pointless_if_repeated (struct rexp_node * node, struct re_se_params * params)
  4754. #else
  4755. static int
  4756. pointless_if_repeated (node, params)
  4757.      struct rexp_node * node;
  4758.      struct re_se_params * params;
  4759. #endif
  4760. {
  4761.   if (!node)
  4762.     return 1;
  4763.   switch (node->type)
  4764.     {
  4765.     case r_cset:
  4766.       return 0;
  4767.     case r_alternate:
  4768.     case r_concat:
  4769.     case r_2phase_star:
  4770.       return (pointless_if_repeated (node->params.pair.left, params)
  4771.           && pointless_if_repeated (node->params.pair.right, params));
  4772.     case r_opt:
  4773.     case r_star:
  4774.       return pointless_if_repeated (node->params.pair.left, params);
  4775.     case r_side_effect:
  4776.       switch (((int)node->params.side_effect < 0)
  4777.           ? (enum re_side_effects)node->params.side_effect
  4778.           : (enum re_side_effects)params[(int)node->params.side_effect].se)
  4779.     {
  4780.     case re_se_try:
  4781.     case re_se_at_dot:
  4782.     case re_se_begbuf:
  4783.     case re_se_hat:
  4784.     case re_se_wordbeg:
  4785.     case re_se_wordbound:
  4786.     case re_se_notwordbound:
  4787.     case re_se_wordend:
  4788.     case re_se_endbuf:
  4789.     case re_se_dollar:
  4790.     case re_se_fail:
  4791.     case re_se_win:
  4792.       return 1;
  4793.     case re_se_lparen:
  4794.     case re_se_rparen:
  4795.     case re_se_iter:
  4796.     case re_se_end_iter:
  4797.     case re_se_syntax:
  4798.     case re_se_not_syntax:
  4799.     case re_se_backref:
  4800.       return 0;
  4801.     }
  4802.     case r_data:
  4803.     default:
  4804.       return 0;
  4805.     }
  4806. }
  4807.  
  4808.  
  4809.  
  4810. #ifdef __STDC__
  4811. static int
  4812. registers_on_stack (struct re_pattern_buffer * rxb,
  4813.             struct rexp_node * rexp, int in_danger,
  4814.             struct re_se_params * params)
  4815. #else
  4816. static int
  4817. registers_on_stack (rxb, rexp, in_danger, params)
  4818.      struct re_pattern_buffer * rxb;
  4819.      struct rexp_node * rexp;
  4820.      int in_danger;
  4821.      struct re_se_params * params;
  4822. #endif
  4823. {
  4824.   if (!rexp)
  4825.     return 0;
  4826.   else
  4827.     switch (rexp->type)
  4828.       {
  4829.       case r_cset:
  4830.       case r_data:
  4831.     return 0;
  4832.       case r_alternate:
  4833.       case r_concat:
  4834.     return (   registers_on_stack (rxb, rexp->params.pair.left,
  4835.                        in_danger, params)
  4836.         || (registers_on_stack
  4837.             (rxb, rexp->params.pair.right,
  4838.              in_danger, params)));
  4839.       case r_opt:
  4840.     return registers_on_stack (rxb, rexp->params.pair.left, 0, params);
  4841.       case r_star:
  4842.     return registers_on_stack (rxb, rexp->params.pair.left, 1, params);
  4843.       case r_2phase_star:
  4844.     return
  4845.       (   registers_on_stack (rxb, rexp->params.pair.left, 1, params)
  4846.        || registers_on_stack (rxb, rexp->params.pair.right, 1, params));
  4847.       case r_side_effect:
  4848.     {
  4849.       int se = (int)rexp->params.side_effect;
  4850.       if (   in_danger
  4851.           && (se >= 0)
  4852.           && (params [se].op1 > 0)
  4853.           && (   ((enum re_side_effects)params[se].se == re_se_lparen)
  4854.           || ((enum re_side_effects)params[se].se == re_se_rparen)))
  4855.         return 1;
  4856.       else
  4857.         return 0;
  4858.     }
  4859.       }
  4860. }
  4861.  
  4862.  
  4863.  
  4864. static char idempotent_complex_se[] =
  4865. {
  4866. #define RX_WANT_SE_DEFS 1
  4867. #undef RX_DEF_SE
  4868. #undef RX_DEF_CPLX_SE
  4869. #define RX_DEF_SE(IDEM, NAME, VALUE)          
  4870. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     IDEM,
  4871. #include "rx.h"
  4872. #undef RX_DEF_SE
  4873. #undef RX_DEF_CPLX_SE
  4874. #undef RX_WANT_SE_DEFS
  4875.   23
  4876. };
  4877.  
  4878. static char idempotent_se[] =
  4879. {
  4880.   13,
  4881. #define RX_WANT_SE_DEFS 1
  4882. #undef RX_DEF_SE
  4883. #undef RX_DEF_CPLX_SE
  4884. #define RX_DEF_SE(IDEM, NAME, VALUE)          IDEM,
  4885. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     
  4886. #include "rx.h"
  4887. #undef RX_DEF_SE
  4888. #undef RX_DEF_CPLX_SE
  4889. #undef RX_WANT_SE_DEFS
  4890.   42
  4891. };
  4892.  
  4893.  
  4894.  
  4895.  
  4896. #ifdef __STDC__
  4897. static int
  4898. has_any_se (struct rx * rx,
  4899.         struct rexp_node * rexp)
  4900. #else
  4901. static int
  4902. has_any_se (rx, rexp)
  4903.      struct rx * rx;
  4904.      struct rexp_node * rexp;
  4905. #endif
  4906. {
  4907.   if (!rexp)
  4908.     return 0;
  4909.  
  4910.   switch (rexp->type)
  4911.     {
  4912.     case r_cset:
  4913.     case r_data:
  4914.       return 0;
  4915.  
  4916.     case r_side_effect:
  4917.       return 1;
  4918.       
  4919.     case r_2phase_star:
  4920.     case r_concat:
  4921.     case r_alternate:
  4922.       return
  4923.     (   has_any_se (rx, rexp->params.pair.left)
  4924.      || has_any_se (rx, rexp->params.pair.right));
  4925.  
  4926.     case r_opt:
  4927.     case r_star:
  4928.       return has_any_se (rx, rexp->params.pair.left);
  4929.     }
  4930. }
  4931.  
  4932.  
  4933.  
  4934. /* This must be called AFTER `convert_hard_loops' for a given REXP. */
  4935. #ifdef __STDC__
  4936. static int
  4937. has_non_idempotent_epsilon_path (struct rx * rx,
  4938.                  struct rexp_node * rexp,
  4939.                  struct re_se_params * params)
  4940. #else
  4941. static int
  4942. has_non_idempotent_epsilon_path (rx, rexp, params)
  4943.      struct rx * rx;
  4944.      struct rexp_node * rexp;
  4945.      struct re_se_params * params;
  4946. #endif
  4947. {
  4948.   if (!rexp)
  4949.     return 0;
  4950.  
  4951.   switch (rexp->type)
  4952.     {
  4953.     case r_cset:
  4954.     case r_data:
  4955.     case r_star:
  4956.       return 0;
  4957.  
  4958.     case r_side_effect:
  4959.       return
  4960.     !((int)rexp->params.side_effect > 0
  4961.       ? idempotent_complex_se [ params [(int)rexp->params.side_effect].se ]
  4962.       : idempotent_se [-(int)rexp->params.side_effect]);
  4963.       
  4964.     case r_alternate:
  4965.       return
  4966.     (   has_non_idempotent_epsilon_path (rx,
  4967.                          rexp->params.pair.left, params)
  4968.      || has_non_idempotent_epsilon_path (rx,
  4969.                          rexp->params.pair.right, params));
  4970.  
  4971.     case r_2phase_star:
  4972.     case r_concat:
  4973.       return
  4974.     (   has_non_idempotent_epsilon_path (rx,
  4975.                          rexp->params.pair.left, params)
  4976.      && has_non_idempotent_epsilon_path (rx,
  4977.                          rexp->params.pair.right, params));
  4978.  
  4979.     case r_opt:
  4980.       return has_non_idempotent_epsilon_path (rx,
  4981.                           rexp->params.pair.left, params);
  4982.     }
  4983.  
  4984. }
  4985.  
  4986.  
  4987.  
  4988. /* This computes rougly what it's name suggests.   It can (and does) go wrong 
  4989.  * in the direction of returning spurious 0 without causing disasters.
  4990.  */
  4991. #ifdef __STDC__
  4992. static int
  4993. begins_with_complex_se (struct rx * rx, struct rexp_node * rexp)
  4994. #else
  4995. static int
  4996. begins_with_complex_se (rx, rexp)
  4997.      struct rx * rx;
  4998.      struct rexp_node * rexp;
  4999. #endif
  5000. {
  5001.   if (!rexp)
  5002.     return 0;
  5003.  
  5004.   switch (rexp->type)
  5005.     {
  5006.     case r_cset:
  5007.     case r_data:
  5008.       return 0;
  5009.  
  5010.     case r_side_effect:
  5011.       return ((int)rexp->params.side_effect >= 0);
  5012.       
  5013.     case r_alternate:
  5014.       return
  5015.     (   begins_with_complex_se (rx, rexp->params.pair.left)
  5016.      && begins_with_complex_se (rx, rexp->params.pair.right));
  5017.  
  5018.  
  5019.     case r_concat:
  5020.       return has_any_se (rx, rexp->params.pair.left);
  5021.     case r_opt:
  5022.     case r_star:
  5023.     case r_2phase_star:
  5024.       return 0;
  5025.     }
  5026. }
  5027.  
  5028.  
  5029. /* This destructively removes some of the re_se_tv side effects from 
  5030.  * a rexp tree.  In particular, during parsing re_se_tv was inserted on the
  5031.  * right half of every | to guarantee that posix path preference could be 
  5032.  * honored.  This function removes some which it can be determined aren't 
  5033.  * needed.  
  5034.  */
  5035.  
  5036. #ifdef __STDC__
  5037. static void
  5038. speed_up_alt (struct rx * rx,
  5039.           struct rexp_node * rexp,
  5040.           int unposix)
  5041. #else
  5042. static void
  5043. speed_up_alt (rx, rexp, unposix)
  5044.      struct rx * rx;
  5045.      struct rexp_node * rexp;
  5046.      int unposix;
  5047. #endif
  5048. {
  5049.   if (!rexp)
  5050.     return;
  5051.  
  5052.   switch (rexp->type)
  5053.     {
  5054.     case r_cset:
  5055.     case r_data:
  5056.     case r_side_effect:
  5057.       return;
  5058.  
  5059.     case r_opt:
  5060.     case r_star:
  5061.       speed_up_alt (rx, rexp->params.pair.left, unposix);
  5062.       return;
  5063.  
  5064.     case r_2phase_star:
  5065.     case r_concat:
  5066.       speed_up_alt (rx, rexp->params.pair.left, unposix);
  5067.       speed_up_alt (rx, rexp->params.pair.right, unposix);
  5068.       return;
  5069.  
  5070.     case r_alternate:
  5071.       /* the right child is guaranteed to be (concat re_se_tv <subexp>) */
  5072.  
  5073.       speed_up_alt (rx, rexp->params.pair.left, unposix);
  5074.       speed_up_alt (rx, rexp->params.pair.right->params.pair.right, unposix);
  5075.       
  5076.       if (   unposix
  5077.       || (begins_with_complex_se
  5078.           (rx, rexp->params.pair.right->params.pair.right))
  5079.       || !(   has_any_se (rx, rexp->params.pair.right->params.pair.right)
  5080.            || has_any_se (rx, rexp->params.pair.left)))
  5081.     {
  5082.       struct rexp_node * conc = rexp->params.pair.right;
  5083.       rexp->params.pair.right = conc->params.pair.right;
  5084.       conc->params.pair.right = 0;
  5085.       rx_free_rexp (rx, conc);
  5086.     }
  5087.     }
  5088. }
  5089.  
  5090.  
  5091.  
  5092.  
  5093.  
  5094. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  5095.    Returns one of error codes defined in `regex.h', or zero for success.
  5096.  
  5097.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  5098.    fields are set in BUFP on entry.
  5099.  
  5100.    If it succeeds, results are put in BUFP (if it returns an error, the
  5101.    contents of BUFP are undefined):
  5102.      `buffer' is the compiled pattern;
  5103.      `syntax' is set to SYNTAX;
  5104.      `used' is set to the length of the compiled pattern;
  5105.      `fastmap_accurate' is set to zero;
  5106.      `re_nsub' is set to the number of groups in PATTERN;
  5107.      `not_bol' and `not_eol' are set to zero.
  5108.    
  5109.    The `fastmap' and `newline_anchor' fields are neither
  5110.    examined nor set.  */
  5111.  
  5112.  
  5113.  
  5114. #ifdef __STDC__
  5115. reg_errcode_t
  5116. rx_compile (const char *pattern, int size,
  5117.         reg_syntax_t syntax,
  5118.         struct re_pattern_buffer * rxb) 
  5119. #else
  5120. reg_errcode_t
  5121. rx_compile (pattern, size, syntax, rxb)
  5122.      const char *pattern;
  5123.      int size;
  5124.      reg_syntax_t syntax;
  5125.      struct re_pattern_buffer * rxb;
  5126. #endif
  5127. {
  5128.   RX_subset
  5129.     inverse_translate [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  5130.   char
  5131.     validate_inv_tr [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  5132.  
  5133.   /* We fetch characters from PATTERN here.  Even though PATTERN is
  5134.      `char *' (i.e., signed), we declare these variables as unsigned, so
  5135.      they can be reliably used as array indices.  */
  5136.   register unsigned char c, c1;
  5137.   
  5138.   /* A random tempory spot in PATTERN.  */
  5139.   const char *p1;
  5140.   
  5141.   /* Keeps track of unclosed groups.  */
  5142.   compile_stack_type compile_stack;
  5143.  
  5144.   /* Points to the current (ending) position in the pattern.  */
  5145.   const char *p = pattern;
  5146.   const char *pend = pattern + size;
  5147.   
  5148.   /* How to translate the characters in the pattern.  */
  5149.   char *translate = rxb->translate ? rxb->translate : id_translation;
  5150.  
  5151.   /* When parsing is done, this will hold the expression tree. */
  5152.   struct rexp_node * rexp = 0;
  5153.  
  5154.   /* In the midst of compilation, this holds onto the regexp 
  5155.    * first parst while rexp goes on to aquire additional constructs.
  5156.    */
  5157.   struct rexp_node * orig_rexp = 0;
  5158.   struct rexp_node * fewer_side_effects = 0;
  5159.  
  5160.   /* This and top_expression are saved on the compile stack. */
  5161.   struct rexp_node ** top_expression = &rexp;
  5162.   struct rexp_node ** last_expression = top_expression;
  5163.   
  5164.   /* Parameter to `goto append_node' */
  5165.   struct rexp_node * append;
  5166.  
  5167.   /* Counts open-groups as they are encountered.  This is the index of the
  5168.    * innermost group being compiled.
  5169.    */
  5170.   regnum_t regnum = 0;
  5171.  
  5172.   /* Place in the uncompiled pattern (i.e., the {) to
  5173.    * which to go back if the interval is invalid.  
  5174.    */
  5175.   const char *beg_interval;
  5176.  
  5177.   struct re_se_params * params = 0;
  5178.   int paramc = 0;        /* How many complex side effects so far? */
  5179.  
  5180.   rx_side_effect side;        /* param to `goto add_side_effect' */
  5181.  
  5182.   bzero (validate_inv_tr, sizeof (validate_inv_tr));
  5183.  
  5184.   rxb->rx.instruction_table = rx_id_instruction_table;
  5185.  
  5186.  
  5187.   /* Initialize the compile stack.  */
  5188.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  5189.   if (compile_stack.stack == 0)
  5190.     return REG_ESPACE;
  5191.  
  5192.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  5193.   compile_stack.avail = 0;
  5194.  
  5195.   /* Initialize the pattern buffer.  */
  5196.   rxb->rx.cache = &default_cache;
  5197.   rxb->syntax = syntax;
  5198.   rxb->fastmap_accurate = 0;
  5199.   rxb->not_bol = rxb->not_eol = 0;
  5200.   rxb->least_subs = 0;
  5201.   
  5202.   /* Always count groups, whether or not rxb->no_sub is set.  
  5203.    * The whole pattern is implicitly group 0, so counting begins
  5204.    * with 1.
  5205.    */
  5206.   rxb->re_nsub = 0;
  5207.  
  5208. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  5209.   /* Initialize the syntax table.  */
  5210.    init_syntax_once ();
  5211. #endif
  5212.  
  5213.   /* Loop through the uncompiled pattern until we're at the end.  */
  5214.   while (p != pend)
  5215.     {
  5216.       PATFETCH (c);
  5217.  
  5218.       switch (c)
  5219.         {
  5220.         case '^':
  5221.           {
  5222.             if (   /* If at start of pattern, it's an operator.  */
  5223.                    p == pattern + 1
  5224.                    /* If context independent, it's an operator.  */
  5225.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  5226.                    /* Otherwise, depends on what's come before.  */
  5227.                 || at_begline_loc_p (pattern, p, syntax))
  5228.           {
  5229.         struct rexp_node * n
  5230.           = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_hat);
  5231.         if (!n)
  5232.           return REG_ESPACE;
  5233.         append = n;
  5234.         goto append_node;
  5235.           }
  5236.             else
  5237.               goto normal_char;
  5238.           }
  5239.           break;
  5240.  
  5241.  
  5242.         case '$':
  5243.           {
  5244.             if (   /* If at end of pattern, it's an operator.  */
  5245.                    p == pend 
  5246.                    /* If context independent, it's an operator.  */
  5247.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  5248.                    /* Otherwise, depends on what's next.  */
  5249.                 || at_endline_loc_p (p, pend, syntax))
  5250.           {
  5251.         struct rexp_node * n
  5252.           = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_dollar);
  5253.         if (!n)
  5254.           return REG_ESPACE;
  5255.         append = n;
  5256.         goto append_node;
  5257.           }
  5258.              else
  5259.                goto normal_char;
  5260.            }
  5261.            break;
  5262.  
  5263.  
  5264.     case '+':
  5265.         case '?':
  5266.           if ((syntax & RE_BK_PLUS_QM)
  5267.               || (syntax & RE_LIMITED_OPS))
  5268.             goto normal_char;
  5269.  
  5270.         handle_plus:
  5271.         case '*':
  5272.           /* If there is no previous pattern... */
  5273.           if (pointless_if_repeated (*last_expression, params))
  5274.             {
  5275.               if (syntax & RE_CONTEXT_INVALID_OPS)
  5276.                 return REG_BADRPT;
  5277.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  5278.                 goto normal_char;
  5279.             }
  5280.  
  5281.           {
  5282.             /* 1 means zero (many) matches is allowed.  */
  5283.             char zero_times_ok = 0, many_times_ok = 0;
  5284.  
  5285.             /* If there is a sequence of repetition chars, collapse it
  5286.                down to just one (the right one).  We can't combine
  5287.                interval operators with these because of, e.g., `a{2}*',
  5288.                which should only match an even number of `a's.  */
  5289.  
  5290.             for (;;)
  5291.               {
  5292.                 zero_times_ok |= c != '+';
  5293.                 many_times_ok |= c != '?';
  5294.  
  5295.                 if (p == pend)
  5296.                   break;
  5297.  
  5298.                 PATFETCH (c);
  5299.  
  5300.                 if (c == '*'
  5301.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  5302.                   ;
  5303.  
  5304.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  5305.                   {
  5306.                     if (p == pend) return REG_EESCAPE;
  5307.  
  5308.                     PATFETCH (c1);
  5309.                     if (!(c1 == '+' || c1 == '?'))
  5310.                       {
  5311.                         PATUNFETCH;
  5312.                         PATUNFETCH;
  5313.                         break;
  5314.                       }
  5315.  
  5316.                     c = c1;
  5317.                   }
  5318.                 else
  5319.                   {
  5320.                     PATUNFETCH;
  5321.                     break;
  5322.                   }
  5323.  
  5324.                 /* If we get here, we found another repeat character.  */
  5325.                }
  5326.  
  5327.             /* Star, etc. applied to an empty pattern is equivalent
  5328.                to an empty pattern.  */
  5329.             if (!last_expression)
  5330.               break;
  5331.  
  5332.         /* Now we know whether or not zero matches is allowed
  5333.          * and also whether or not two or more matches is allowed.
  5334.          */
  5335.  
  5336.         {
  5337.           struct rexp_node * inner_exp = *last_expression;
  5338.           int need_sync = 0;
  5339.  
  5340.           if (many_times_ok
  5341.           && has_non_idempotent_epsilon_path (&rxb->rx,
  5342.                               inner_exp, params))
  5343.         {
  5344.           struct rexp_node * pusher
  5345.             = rx_mk_r_side_effect (&rxb->rx,
  5346.                        (rx_side_effect)re_se_pushpos);
  5347.           struct rexp_node * checker
  5348.             = rx_mk_r_side_effect (&rxb->rx,
  5349.                        (rx_side_effect)re_se_chkpos);
  5350.           struct rexp_node * pushback
  5351.             = rx_mk_r_side_effect (&rxb->rx,
  5352.                        (rx_side_effect)re_se_pushback);
  5353.           rx_Bitset cs = rx_cset (&rxb->rx);
  5354.           struct rexp_node * lit_t = rx_mk_r_cset (&rxb->rx, cs);
  5355.           struct rexp_node * fake_state
  5356.             = rx_mk_r_concat (&rxb->rx, pushback, lit_t);
  5357.           struct rexp_node * phase2
  5358.             = rx_mk_r_concat (&rxb->rx, checker, fake_state);
  5359.           struct rexp_node * popper
  5360.             = rx_mk_r_side_effect (&rxb->rx,
  5361.                        (rx_side_effect)re_se_poppos);
  5362.           struct rexp_node * star
  5363.             = rx_mk_r_2phase_star (&rxb->rx, inner_exp, phase2);
  5364.           struct rexp_node * a
  5365.             = rx_mk_r_concat (&rxb->rx, pusher, star);
  5366.           struct rexp_node * whole_thing
  5367.             = rx_mk_r_concat (&rxb->rx, a, popper);
  5368.           if (!(pusher && star && pushback && lit_t && fake_state
  5369.             && lit_t && phase2 && checker && popper
  5370.             && a && whole_thing))
  5371.             return REG_ESPACE;
  5372.           RX_bitset_enjoin (cs, 't');
  5373.           *last_expression = whole_thing;
  5374.         }
  5375.           else
  5376.         {
  5377.           struct rexp_node * star =
  5378.             (many_times_ok ? rx_mk_r_star : rx_mk_r_opt)
  5379.               (&rxb->rx, *last_expression);
  5380.           if (!star)
  5381.             return REG_ESPACE;
  5382.           *last_expression = star;
  5383.           need_sync = has_any_se (&rxb->rx, *last_expression);
  5384.         }
  5385.           if (!zero_times_ok)
  5386.         {
  5387.           struct rexp_node * concat
  5388.             = rx_mk_r_concat (&rxb->rx, inner_exp,
  5389.                       rx_copy_rexp (&rxb->rx,
  5390.                             *last_expression));
  5391.           if (!concat)
  5392.             return REG_ESPACE;
  5393.           *last_expression = concat;
  5394.         }
  5395.           if (need_sync)
  5396.         {
  5397.           int sync_se = paramc;
  5398.           params = (params
  5399.                 ? ((struct re_se_params *)
  5400.                    realloc (params,
  5401.                     sizeof (*params) * (1 + paramc)))
  5402.                 : ((struct re_se_params *)
  5403.                    malloc (sizeof (*params))));
  5404.           if (!params)
  5405.             return REG_ESPACE;
  5406.           ++paramc;
  5407.           params [sync_se].se = re_se_tv;
  5408.           side = (rx_side_effect)sync_se;
  5409.           goto add_side_effect;
  5410.         }
  5411.         }
  5412.         /* The old regex.c used to optimize `.*\n'.  
  5413.          * Maybe rx should too?
  5414.          */
  5415.       }
  5416.       break;
  5417.  
  5418.  
  5419.     case '.':
  5420.       {
  5421.         rx_Bitset cs = rx_cset (&rxb->rx);
  5422.         struct rexp_node * n = rx_mk_r_cset (&rxb->rx, cs);
  5423.         if (!(cs && n))
  5424.           return REG_ESPACE;
  5425.  
  5426.         rx_bitset_universe (rxb->rx.local_cset_size, cs);
  5427.         if (!(rxb->syntax & RE_DOT_NEWLINE))
  5428.           RX_bitset_remove (cs, '\n');
  5429.         if (!(rxb->syntax & RE_DOT_NOT_NULL))
  5430.           RX_bitset_remove (cs, 0);
  5431.  
  5432.         append = n;
  5433.         goto append_node;
  5434.         break;
  5435.       }
  5436.  
  5437.  
  5438.         case '[':
  5439.       if (p == pend) return REG_EBRACK;
  5440.           {
  5441.             boolean had_char_class = false;
  5442.         rx_Bitset cs = rx_cset (&rxb->rx);
  5443.         struct rexp_node * node = rx_mk_r_cset (&rxb->rx, cs);
  5444.         int is_inverted = *p == '^';
  5445.         
  5446.         if (!(node && cs))
  5447.           return REG_ESPACE;
  5448.         
  5449.         /* This branch of the switch is normally exited with
  5450.          *`goto append_node'
  5451.          */
  5452.         append = node;
  5453.         
  5454.             if (is_inverted)
  5455.           p++;
  5456.         
  5457.             /* Remember the first position in the bracket expression.  */
  5458.             p1 = p;
  5459.         
  5460.             /* Read in characters and ranges, setting map bits.  */
  5461.             for (;;)
  5462.               {
  5463.                 if (p == pend) return REG_EBRACK;
  5464.         
  5465.                 PATFETCH (c);
  5466.         
  5467.                 /* \ might escape characters inside [...] and [^...].  */
  5468.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  5469.                   {
  5470.                     if (p == pend) return REG_EESCAPE;
  5471.             
  5472.                     PATFETCH (c1);
  5473.             {
  5474.               rx_Bitset it = inverse_translation (rxb, 
  5475.                               validate_inv_tr,
  5476.                               inverse_translate,
  5477.                               translate,
  5478.                               c1);
  5479.               rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  5480.             }
  5481.                     continue;
  5482.                   }
  5483.         
  5484.                 /* Could be the end of the bracket expression.  If it's
  5485.                    not (i.e., when the bracket expression is `[]' so
  5486.                    far), the ']' character bit gets set way below.  */
  5487.                 if (c == ']' && p != p1 + 1)
  5488.                   goto finalize_class_and_append;
  5489.         
  5490.                 /* Look ahead to see if it's a range when the last thing
  5491.                    was a character class.  */
  5492.                 if (had_char_class && c == '-' && *p != ']')
  5493.                   return REG_ERANGE;
  5494.         
  5495.                 /* Look ahead to see if it's a range when the last thing
  5496.                    was a character: if this is a hyphen not at the
  5497.                    beginning or the end of a list, then it's the range
  5498.                    operator.  */
  5499.                 if (c == '-' 
  5500.                     && !(p - 2 >= pattern && p[-2] == '[') 
  5501.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  5502.                     && *p != ']')
  5503.                   {
  5504.                     reg_errcode_t ret
  5505.                       = compile_range (rxb, cs, &p, pend, translate, syntax,
  5506.                        inverse_translate, validate_inv_tr);
  5507.                     if (ret != REG_NOERROR) return ret;
  5508.                   }
  5509.         
  5510.                 else if (p[0] == '-' && p[1] != ']')
  5511.                   { /* This handles ranges made up of characters only.  */
  5512.                     reg_errcode_t ret;
  5513.             
  5514.             /* Move past the `-'.  */
  5515.                     PATFETCH (c1);
  5516.                     
  5517.                     ret = compile_range (rxb, cs, &p, pend, translate, syntax,
  5518.                      inverse_translate, validate_inv_tr);
  5519.                     if (ret != REG_NOERROR) return ret;
  5520.                   }
  5521.         
  5522.                 /* See if we're at the beginning of a possible character
  5523.                    class.  */
  5524.         
  5525.         else if ((syntax & RE_CHAR_CLASSES)
  5526.              && (c == '[') && (*p == ':'))
  5527.                   {
  5528.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  5529.             
  5530.                     PATFETCH (c);
  5531.                     c1 = 0;
  5532.             
  5533.                     /* If pattern is `[[:'.  */
  5534.                     if (p == pend) return REG_EBRACK;
  5535.             
  5536.                     for (;;)
  5537.                       {
  5538.                         PATFETCH (c);
  5539.                         if (c == ':' || c == ']' || p == pend
  5540.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  5541.               break;
  5542.                         str[c1++] = c;
  5543.                       }
  5544.                     str[c1] = '\0';
  5545.             
  5546.                     /* If isn't a word bracketed by `[:' and:`]':
  5547.                        undo the ending character, the letters, and leave 
  5548.                        the leading `:' and `[' (but set bits for them).  */
  5549.                     if (c == ':' && *p == ']')
  5550.                       {
  5551.                         int ch;
  5552.                         boolean is_alnum = !strcmp (str, "alnum");
  5553.                         boolean is_alpha = !strcmp (str, "alpha");
  5554.                         boolean is_blank = !strcmp (str, "blank");
  5555.                         boolean is_cntrl = !strcmp (str, "cntrl");
  5556.                         boolean is_digit = !strcmp (str, "digit");
  5557.                         boolean is_graph = !strcmp (str, "graph");
  5558.                         boolean is_lower = !strcmp (str, "lower");
  5559.                         boolean is_print = !strcmp (str, "print");
  5560.                         boolean is_punct = !strcmp (str, "punct");
  5561.                         boolean is_space = !strcmp (str, "space");
  5562.                         boolean is_upper = !strcmp (str, "upper");
  5563.                         boolean is_xdigit = !strcmp (str, "xdigit");
  5564.                         
  5565.                         if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  5566.             
  5567.                         /* Throw away the ] at the end of the character
  5568.                            class.  */
  5569.                         PATFETCH (c);                    
  5570.             
  5571.                         if (p == pend) return REG_EBRACK;
  5572.             
  5573.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  5574.                           {
  5575.                             if (   (is_alnum  && isalnum (ch))
  5576.                                 || (is_alpha  && isalpha (ch))
  5577.                                 || (is_blank  && isblank (ch))
  5578.                                 || (is_cntrl  && iscntrl (ch))
  5579.                                 || (is_digit  && isdigit (ch))
  5580.                                 || (is_graph  && isgraph (ch))
  5581.                                 || (is_lower  && islower (ch))
  5582.                                 || (is_print  && isprint (ch))
  5583.                                 || (is_punct  && ispunct (ch))
  5584.                                 || (is_space  && isspace (ch))
  5585.                                 || (is_upper  && isupper (ch))
  5586.                                 || (is_xdigit && isxdigit (ch)))
  5587.                   {
  5588.                 rx_Bitset it =
  5589.                   inverse_translation (rxb, 
  5590.                                validate_inv_tr,
  5591.                                inverse_translate,
  5592.                                translate,
  5593.                                ch);
  5594.                 rx_bitset_union (rxb->rx.local_cset_size,
  5595.                          cs, it);
  5596.                   }
  5597.                           }
  5598.                         had_char_class = true;
  5599.                       }
  5600.                     else
  5601.                       {
  5602.                         c1++;
  5603.                         while (c1--)    
  5604.                           PATUNFETCH;
  5605.             {
  5606.               rx_Bitset it =
  5607.                 inverse_translation (rxb, 
  5608.                          validate_inv_tr,
  5609.                          inverse_translate,
  5610.                          translate,
  5611.                          '[');
  5612.               rx_bitset_union (rxb->rx.local_cset_size,
  5613.                        cs, it);
  5614.             }
  5615.             {
  5616.               rx_Bitset it =
  5617.                 inverse_translation (rxb, 
  5618.                          validate_inv_tr,
  5619.                          inverse_translate,
  5620.                          translate,
  5621.                          ':');
  5622.               rx_bitset_union (rxb->rx.local_cset_size,
  5623.                        cs, it);
  5624.             }
  5625.                         had_char_class = false;
  5626.                       }
  5627.                   }
  5628.                 else
  5629.                   {
  5630.                     had_char_class = false;
  5631.             {
  5632.               rx_Bitset it = inverse_translation (rxb, 
  5633.                               validate_inv_tr,
  5634.                               inverse_translate,
  5635.                               translate,
  5636.                               c);
  5637.               rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  5638.             }
  5639.                   }
  5640.               }
  5641.  
  5642.       finalize_class_and_append:
  5643.         if (is_inverted)
  5644.           {
  5645.         rx_bitset_complement (rxb->rx.local_cset_size, cs);
  5646.         if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  5647.           RX_bitset_remove (cs, '\n');
  5648.           }
  5649.         goto append_node;
  5650.           }
  5651.           break;
  5652.  
  5653.  
  5654.     case '(':
  5655.           if (syntax & RE_NO_BK_PARENS)
  5656.             goto handle_open;
  5657.           else
  5658.             goto normal_char;
  5659.  
  5660.  
  5661.         case ')':
  5662.           if (syntax & RE_NO_BK_PARENS)
  5663.             goto handle_close;
  5664.           else
  5665.             goto normal_char;
  5666.  
  5667.  
  5668.         case '\n':
  5669.           if (syntax & RE_NEWLINE_ALT)
  5670.             goto handle_alt;
  5671.           else
  5672.             goto normal_char;
  5673.  
  5674.  
  5675.     case '|':
  5676.           if (syntax & RE_NO_BK_VBAR)
  5677.             goto handle_alt;
  5678.           else
  5679.             goto normal_char;
  5680.  
  5681.  
  5682.         case '{':
  5683.       if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  5684.         goto handle_interval;
  5685.       else
  5686.         goto normal_char;
  5687.  
  5688.  
  5689.         case '\\':
  5690.           if (p == pend) return REG_EESCAPE;
  5691.  
  5692.           /* Do not translate the character after the \, so that we can
  5693.              distinguish, e.g., \B from \b, even if we normally would
  5694.              translate, e.g., B to b.  */
  5695.           PATFETCH_RAW (c);
  5696.  
  5697.           switch (c)
  5698.             {
  5699.             case '(':
  5700.               if (syntax & RE_NO_BK_PARENS)
  5701.                 goto normal_backslash;
  5702.  
  5703.             handle_open:
  5704.               rxb->re_nsub++;
  5705.               regnum++;
  5706.               if (COMPILE_STACK_FULL)
  5707.                 { 
  5708.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  5709.                             compile_stack_elt_t);
  5710.                   if (compile_stack.stack == 0) return REG_ESPACE;
  5711.  
  5712.                   compile_stack.size <<= 1;
  5713.                 }
  5714.  
  5715.           if (*last_expression)
  5716.         {
  5717.           struct rexp_node * concat
  5718.             = rx_mk_r_concat (&rxb->rx, *last_expression, 0);
  5719.           if (!concat)
  5720.             return REG_ESPACE;
  5721.           *last_expression = concat;
  5722.           last_expression = &concat->params.pair.right;
  5723.         }
  5724.  
  5725.               /*
  5726.            * These are the values to restore when we hit end of this
  5727.                * group.  
  5728.            */
  5729.           COMPILE_STACK_TOP.top_expression = top_expression;
  5730.           COMPILE_STACK_TOP.last_expression = last_expression;
  5731.               COMPILE_STACK_TOP.regnum = regnum;
  5732.           
  5733.               compile_stack.avail++;
  5734.           
  5735.           top_expression = last_expression;
  5736.           break;
  5737.  
  5738.  
  5739.             case ')':
  5740.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  5741.  
  5742.             handle_close:
  5743.               /* See similar code for backslashed left paren above.  */
  5744.               if (COMPILE_STACK_EMPTY)
  5745.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  5746.                   goto normal_char;
  5747.                 else
  5748.                   return REG_ERPAREN;
  5749.  
  5750.               /* Since we just checked for an empty stack above, this
  5751.                  ``can't happen''.  */
  5752.  
  5753.               {
  5754.                 /* We don't just want to restore into `regnum', because
  5755.                    later groups should continue to be numbered higher,
  5756.                    as in `(ab)c(de)' -- the second group is #2.  */
  5757.                 regnum_t this_group_regnum;
  5758.         struct rexp_node ** inner = top_expression;
  5759.  
  5760.                 compile_stack.avail--;
  5761.         top_expression = COMPILE_STACK_TOP.top_expression;
  5762.         last_expression = COMPILE_STACK_TOP.last_expression;
  5763.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  5764.         {
  5765.           int left_se = paramc;
  5766.           int right_se = paramc + 1;
  5767.  
  5768.           params = (params
  5769.                 ? ((struct re_se_params *)
  5770.                    realloc (params,
  5771.                     (paramc + 2) * sizeof (params[0])))
  5772.                 : ((struct re_se_params *)
  5773.                    malloc (2 * sizeof (params[0]))));
  5774.           if (!params)
  5775.             return REG_ESPACE;
  5776.           paramc += 2;
  5777.  
  5778.           params[left_se].se = re_se_lparen;
  5779.           params[left_se].op1 = this_group_regnum;
  5780.           params[right_se].se = re_se_rparen;
  5781.           params[right_se].op1 = this_group_regnum;
  5782.           {
  5783.             struct rexp_node * left
  5784.               = rx_mk_r_side_effect (&rxb->rx,
  5785.                          (rx_side_effect)left_se);
  5786.             struct rexp_node * right
  5787.               = rx_mk_r_side_effect (&rxb->rx,
  5788.                          (rx_side_effect)right_se);
  5789.             struct rexp_node * c1
  5790.               = (*inner
  5791.              ? rx_mk_r_concat (&rxb->rx, left, *inner) : left);
  5792.             struct rexp_node * c2
  5793.               = rx_mk_r_concat (&rxb->rx, c1, right);
  5794.             if (!(left && right && c1 && c2))
  5795.               return REG_ESPACE;
  5796.             *inner = c2;
  5797.           }
  5798.         }
  5799.         break;
  5800.           }
  5801.  
  5802.             case '|':                    /* `\|'.  */
  5803.               if ((syntax & RE_LIMITED_OPS) || (syntax & RE_NO_BK_VBAR))
  5804.                 goto normal_backslash;
  5805.             handle_alt:
  5806.               if (syntax & RE_LIMITED_OPS)
  5807.                 goto normal_char;
  5808.  
  5809.           {
  5810.         struct rexp_node * alt
  5811.           = rx_mk_r_alternate (&rxb->rx, *top_expression, 0);
  5812.         if (!alt)
  5813.           return REG_ESPACE;
  5814.         *top_expression = alt;
  5815.         last_expression = &alt->params.pair.right;
  5816.         {
  5817.           int sync_se = paramc;
  5818.  
  5819.           params = (params
  5820.                 ? ((struct re_se_params *)
  5821.                    realloc (params,
  5822.                     (paramc + 1) * sizeof (params[0])))
  5823.                 : ((struct re_se_params *)
  5824.                    malloc (sizeof (params[0]))));
  5825.           if (!params)
  5826.             return REG_ESPACE;
  5827.           ++paramc;
  5828.  
  5829.           params[sync_se].se = re_se_tv;
  5830.           {
  5831.             struct rexp_node * sync
  5832.               = rx_mk_r_side_effect (&rxb->rx,
  5833.                          (rx_side_effect)sync_se);
  5834.             struct rexp_node * conc
  5835.               = rx_mk_r_concat (&rxb->rx, sync, 0);
  5836.  
  5837.             if (!sync || !conc)
  5838.               return REG_ESPACE;
  5839.  
  5840.             *last_expression = conc;
  5841.             last_expression = &conc->params.pair.right;
  5842.           }
  5843.         }
  5844.           }
  5845.               break;
  5846.  
  5847.  
  5848.             case '{': 
  5849.               /* If \{ is a literal.  */
  5850.               if (!(syntax & RE_INTERVALS)
  5851.                      /* If we're at `\{' and it's not the open-interval 
  5852.                         operator.  */
  5853.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  5854.                   || (p - 2 == pattern  &&  p == pend))
  5855.                 goto normal_backslash;
  5856.  
  5857.             handle_interval:
  5858.               {
  5859.                 /* If got here, then the syntax allows intervals.  */
  5860.  
  5861.                 /* At least (most) this many matches must be made.  */
  5862.                 int lower_bound = -1, upper_bound = -1;
  5863.  
  5864.                 beg_interval = p - 1;
  5865.  
  5866.                 if (p == pend)
  5867.                   {
  5868.                     if (syntax & RE_NO_BK_BRACES)
  5869.                       goto unfetch_interval;
  5870.                     else
  5871.                       return REG_EBRACE;
  5872.                   }
  5873.  
  5874.                 GET_UNSIGNED_NUMBER (lower_bound);
  5875.  
  5876.                 if (c == ',')
  5877.                   {
  5878.                     GET_UNSIGNED_NUMBER (upper_bound);
  5879.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  5880.                   }
  5881.                 else
  5882.                   /* Interval such as `{1}' => match exactly once. */
  5883.                   upper_bound = lower_bound;
  5884.  
  5885.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  5886.                     || lower_bound > upper_bound)
  5887.                   {
  5888.                     if (syntax & RE_NO_BK_BRACES)
  5889.                       goto unfetch_interval;
  5890.                     else 
  5891.                       return REG_BADBR;
  5892.                   }
  5893.  
  5894.                 if (!(syntax & RE_NO_BK_BRACES)) 
  5895.                   {
  5896.                     if (c != '\\') return REG_EBRACE;
  5897.                     PATFETCH (c);
  5898.                   }
  5899.  
  5900.                 if (c != '}')
  5901.                   {
  5902.                     if (syntax & RE_NO_BK_BRACES)
  5903.                       goto unfetch_interval;
  5904.                     else 
  5905.                       return REG_BADBR;
  5906.                   }
  5907.  
  5908.                 /* We just parsed a valid interval.  */
  5909.  
  5910.                 /* If it's invalid to have no preceding re.  */
  5911.                 if (pointless_if_repeated (*last_expression, params))
  5912.                   {
  5913.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  5914.                       return REG_BADRPT;
  5915.                     else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  5916.                       goto unfetch_interval;
  5917.             /* was: else laststart = b; */
  5918.                   }
  5919.  
  5920.                 /* If the upper bound is zero, don't want to iterate
  5921.                  * at all.
  5922.          */
  5923.                  if (upper_bound == 0)
  5924.            {
  5925.              if (*last_expression)
  5926.                {
  5927.              rx_free_rexp (&rxb->rx, *last_expression);
  5928.              *last_expression = 0;
  5929.                }
  5930.            }
  5931.         else
  5932.           /* Otherwise, we have a nontrivial interval. */
  5933.           {
  5934.             int iter_se = paramc;
  5935.             int end_se = paramc + 1;
  5936.             params = (params
  5937.                   ? ((struct re_se_params *)
  5938.                  realloc (params,
  5939.                       sizeof (*params) * (2 + paramc)))
  5940.                   : ((struct re_se_params *)
  5941.                  malloc (2 * sizeof (*params))));
  5942.             if (!params)
  5943.               return REG_ESPACE;
  5944.             paramc += 2;
  5945.             params [iter_se].se = re_se_iter;
  5946.             params [iter_se].op1 = lower_bound;
  5947.             params[iter_se].op2 = upper_bound;
  5948.  
  5949.             params[end_se].se = re_se_end_iter;
  5950.             params[end_se].op1 = lower_bound;
  5951.             params[end_se].op2 = upper_bound;
  5952.             {
  5953.               struct rexp_node * push0
  5954.             = rx_mk_r_side_effect (&rxb->rx,
  5955.                            (rx_side_effect)re_se_push0);
  5956.               struct rexp_node * start_one_iter
  5957.             = rx_mk_r_side_effect (&rxb->rx,
  5958.                            (rx_side_effect)iter_se);
  5959.               struct rexp_node * phase1
  5960.             = rx_mk_r_concat (&rxb->rx, start_one_iter,
  5961.                       *last_expression);
  5962.               struct rexp_node * pushback
  5963.             = rx_mk_r_side_effect (&rxb->rx,
  5964.                            (rx_side_effect)re_se_pushback);
  5965.               rx_Bitset cs = rx_cset (&rxb->rx);
  5966.               struct rexp_node * lit_t
  5967.             = rx_mk_r_cset (&rxb->rx, cs);
  5968.               struct rexp_node * phase2
  5969.             = rx_mk_r_concat (&rxb->rx, pushback, lit_t);
  5970.               struct rexp_node * loop
  5971.             = rx_mk_r_2phase_star (&rxb->rx, phase1, phase2);
  5972.               struct rexp_node * push_n_loop
  5973.             = rx_mk_r_concat (&rxb->rx, push0, loop);
  5974.               struct rexp_node * final_test
  5975.             = rx_mk_r_side_effect (&rxb->rx,
  5976.                            (rx_side_effect)end_se);
  5977.               struct rexp_node * full_exp
  5978.             = rx_mk_r_concat (&rxb->rx, push_n_loop, final_test);
  5979.  
  5980.               if (!(push0 && start_one_iter && phase1
  5981.                 && pushback && lit_t && phase2
  5982.                 && loop && push_n_loop && final_test && full_exp))
  5983.             return REG_ESPACE;
  5984.  
  5985.               RX_bitset_enjoin(cs, 't');
  5986.  
  5987.               *last_expression = full_exp;
  5988.             }
  5989.           }
  5990.                 beg_interval = 0;
  5991.               }
  5992.               break;
  5993.  
  5994.             unfetch_interval:
  5995.               /* If an invalid interval, match the characters as literals.  */
  5996.                p = beg_interval;
  5997.                beg_interval = NULL;
  5998.  
  5999.                /* normal_char and normal_backslash need `c'.  */
  6000.                PATFETCH (c);    
  6001.  
  6002.                if (!(syntax & RE_NO_BK_BRACES))
  6003.                  {
  6004.                    if (p > pattern  &&  p[-1] == '\\')
  6005.                      goto normal_backslash;
  6006.                  }
  6007.                goto normal_char;
  6008.  
  6009. #ifdef emacs
  6010.             /* There is no way to specify the before_dot and after_dot
  6011.                operators.  rms says this is ok.  --karl  */
  6012.             case '=':
  6013.           side = at_dot;
  6014.           goto add_side_effect;
  6015.               break;
  6016.  
  6017.             case 's':
  6018.         case 'S':
  6019.           {
  6020.         rx_Bitset cs = cset (&rxb->rx);
  6021.         struct rexp_node * set = rx_mk_r_cset (&rxb->rx, cs);
  6022.         if (!(cs && set))
  6023.           return REG_ESPACE;
  6024.         if (c == 'S')
  6025.           rx_bitset_universe (rxb->rx.local_cset_size, cs);
  6026.  
  6027.         PATFETCH (c);
  6028.         {
  6029.           int x;
  6030.           char code = syntax_spec_code (c);
  6031.           for (x = 0; x < 256; ++x)
  6032.             {
  6033.               
  6034.               if (SYNTAX (x) & code)
  6035.             {
  6036.               rx_Bitset it =
  6037.                 inverse_translation (rxb, validate_inv_tr,
  6038.                          inverse_translate,
  6039.                          translate, x);
  6040.               rx_bitset_xor (rxb->rx.local_cset_size, cs, it);
  6041.             }
  6042.             }
  6043.         }
  6044.         goto append_node;
  6045.           }
  6046.               break;
  6047. #endif /* emacs */
  6048.  
  6049.  
  6050.             case 'w':
  6051.             case 'W':
  6052.           {
  6053.         rx_Bitset cs = rx_cset (&rxb->rx);
  6054.         struct rexp_node * n = (cs ? rx_mk_r_cset (&rxb->rx, cs) : 0);
  6055.         if (!(cs && n))
  6056.           return REG_ESPACE;
  6057.         if (c == 'W')
  6058.           rx_bitset_universe (rxb->rx.local_cset_size ,cs);
  6059.         {
  6060.           int x;
  6061.           for (x = rxb->rx.local_cset_size - 1; x > 0; --x)
  6062.             if (re_syntax_table[x] & Sword)
  6063.               RX_bitset_toggle (cs, x);
  6064.         }
  6065.         append = n;
  6066.         goto append_node;
  6067.           }
  6068.               break;
  6069.  
  6070. /* With a little extra work, some of these side effects could be optimized
  6071.  * away (basicly by looking at what we already know about the surrounding
  6072.  * chars).  
  6073.  */
  6074.             case '<':
  6075.           side = (rx_side_effect)re_se_wordbeg;
  6076.           goto add_side_effect;
  6077.               break;
  6078.  
  6079.             case '>':
  6080.               side = (rx_side_effect)re_se_wordend;
  6081.           goto add_side_effect;
  6082.               break;
  6083.  
  6084.             case 'b':
  6085.               side = (rx_side_effect)re_se_wordbound;
  6086.           goto add_side_effect;
  6087.               break;
  6088.  
  6089.             case 'B':
  6090.               side = (rx_side_effect)re_se_notwordbound;
  6091.           goto add_side_effect;
  6092.               break;
  6093.  
  6094.             case '`':
  6095.           side = (rx_side_effect)re_se_begbuf;
  6096.           goto add_side_effect;
  6097.           break;
  6098.           
  6099.             case '\'':
  6100.           side = (rx_side_effect)re_se_endbuf;
  6101.           goto add_side_effect;
  6102.               break;
  6103.  
  6104.         add_side_effect:
  6105.           {
  6106.         struct rexp_node * se
  6107.           = rx_mk_r_side_effect (&rxb->rx, side);
  6108.         if (!se)
  6109.           return REG_ESPACE;
  6110.         append = se;
  6111.         goto append_node;
  6112.           }
  6113.           break;
  6114.  
  6115.             case '1': case '2': case '3': case '4': case '5':
  6116.             case '6': case '7': case '8': case '9':
  6117.               if (syntax & RE_NO_BK_REFS)
  6118.                 goto normal_char;
  6119.  
  6120.               c1 = c - '0';
  6121.  
  6122.               if (c1 > regnum)
  6123.                 return REG_ESUBREG;
  6124.  
  6125.               /* Can't back reference to a subexpression if inside of it.  */
  6126.               if (group_in_compile_stack (compile_stack, c1))
  6127.                 goto normal_char;
  6128.           {
  6129.         int backref_se = paramc;
  6130.         params = (params
  6131.               ? ((struct re_se_params *)
  6132.                  realloc (params,
  6133.                       sizeof (*params) * (1 + paramc)))
  6134.               : ((struct re_se_params *)
  6135.                  malloc (sizeof (*params))));
  6136.         if (!params)
  6137.           return REG_ESPACE;
  6138.         ++paramc;
  6139.         params[backref_se].se = re_se_backref;
  6140.         params[backref_se].op1 = c1;
  6141.         side = (rx_side_effect)backref_se;
  6142.         goto add_side_effect;
  6143.           }
  6144.               break;
  6145.  
  6146.             case '+':
  6147.             case '?':
  6148.               if (syntax & RE_BK_PLUS_QM)
  6149.                 goto handle_plus;
  6150.               else
  6151.                 goto normal_backslash;
  6152.  
  6153.             default:
  6154.             normal_backslash:
  6155.               /* You might think it would be useful for \ to mean
  6156.                  not to translate; but if we don't translate it
  6157.                  it will never match anything.  */
  6158.               c = TRANSLATE (c);
  6159.               goto normal_char;
  6160.             }
  6161.           break;
  6162.  
  6163.  
  6164.     default:
  6165.         /* Expects the character in `c'.  */
  6166.     normal_char:
  6167.         {
  6168.           rx_Bitset cs = rx_cset(&rxb->rx);
  6169.           struct rexp_node * match = rx_mk_r_cset (&rxb->rx, cs);
  6170.           rx_Bitset it;
  6171.           if (!(cs && match))
  6172.         return REG_ESPACE;
  6173.           it = inverse_translation (rxb, validate_inv_tr,
  6174.                     inverse_translate, translate, c);
  6175.           rx_bitset_union (CHAR_SET_SIZE, cs, it);
  6176.           append = match;
  6177.  
  6178.         append_node:
  6179.           /* This genericly appends the rexp APPEND to *LAST_EXPRESSION
  6180.            * and then parses the next character normally.
  6181.            */
  6182.           if (*last_expression)
  6183.         {
  6184.           struct rexp_node * concat
  6185.             = rx_mk_r_concat (&rxb->rx, *last_expression, append);
  6186.           if (!concat)
  6187.             return REG_ESPACE;
  6188.           *last_expression = concat;
  6189.           last_expression = &concat->params.pair.right;
  6190.         }
  6191.           else
  6192.         *last_expression = append;
  6193.         }
  6194.     } /* switch (c) */
  6195.     } /* while p != pend */
  6196.  
  6197.   
  6198.   {
  6199.     int win_se = paramc;
  6200.     params = (params
  6201.           ? ((struct re_se_params *)
  6202.          realloc (params,
  6203.               sizeof (*params) * (1 + paramc)))
  6204.           : ((struct re_se_params *)
  6205.          malloc (sizeof (*params))));
  6206.     if (!params)
  6207.       return REG_ESPACE;
  6208.     ++paramc;
  6209.     params[win_se].se = re_se_win;
  6210.     {
  6211.       struct rexp_node * se
  6212.     = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)win_se);
  6213.       struct rexp_node * concat
  6214.     = rx_mk_r_concat (&rxb->rx, rexp, se);
  6215.       if (!(se && concat))
  6216.     return REG_ESPACE;
  6217.       rexp = concat;
  6218.     }
  6219.   }
  6220.  
  6221.  
  6222.   /* Through the pattern now.  */
  6223.  
  6224.   if (!COMPILE_STACK_EMPTY) 
  6225.     return REG_EPAREN;
  6226.  
  6227.       free (compile_stack.stack);
  6228.  
  6229.   orig_rexp = rexp;
  6230. #ifdef RX_DEBUG
  6231.   if (rx_debug_compile)
  6232.     {
  6233.       dbug_rxb = rxb;
  6234.       fputs ("\n\nCompiling ", stdout);
  6235.       fwrite (pattern, 1, size, stdout);
  6236.       fputs (":\n", stdout);
  6237.       rxb->se_params = params;
  6238.       print_rexp (&rxb->rx, orig_rexp, 2, re_seprint, stdout);
  6239.     }
  6240. #endif
  6241.   {
  6242.     rx_Bitset cs = rx_cset(&rxb->rx);
  6243.     rx_Bitset cs2 = rx_cset(&rxb->rx);
  6244.     char * se_map = (char *) alloca (paramc);
  6245.     struct rexp_node * new_rexp = 0;
  6246.  
  6247.  
  6248.     bzero (se_map, paramc);
  6249.     find_backrefs (se_map, rexp, params);
  6250.     fewer_side_effects =
  6251.       remove_unecessary_side_effects (&rxb->rx, se_map,
  6252.                       rx_copy_rexp (&rxb->rx, rexp), params);
  6253.  
  6254.     speed_up_alt (&rxb->rx, rexp, 0);
  6255.     speed_up_alt (&rxb->rx, fewer_side_effects, 1);
  6256.  
  6257.     {
  6258.       char * syntax_parens = rxb->syntax_parens;
  6259.       if (syntax_parens == (char *)0x1)
  6260.     rexp = remove_unecessary_side_effects
  6261.       (&rxb->rx, se_map, rexp, params);
  6262.       else if (syntax_parens)
  6263.     {
  6264.       int x;
  6265.       for (x = 0; x < paramc; ++x)
  6266.         if ((   (params[x].se == re_se_lparen)
  6267.          || (params[x].se == re_se_rparen))
  6268.         && (!syntax_parens [params[x].op1]))
  6269.           se_map [x] = 1;
  6270.       rexp = remove_unecessary_side_effects
  6271.         (&rxb->rx, se_map, rexp, params);
  6272.     }
  6273.     }
  6274.  
  6275.     /* At least one more optimization would be nice to have here but i ran out 
  6276.      * of time.  The idea would be to delay side effects.  
  6277.      * For examle, `(abc)' is the same thing as `abc()' except that the
  6278.      * left paren is offset by 3 (which we know at compile time).
  6279.      * (In this comment, write that second pattern `abc(:3:)' 
  6280.      * where `(:3:' is a syntactic unit.)
  6281.      *
  6282.      * Trickier:  `(abc|defg)'  is the same as `(abc(:3:|defg(:4:))'
  6283.      * (The paren nesting may be hard to follow -- that's an alternation
  6284.      *    of `abc(:3:' and `defg(:4:' inside (purely syntactic) parens
  6285.      *  followed by the closing paren from the original expression.)
  6286.      *
  6287.      * Neither the expression tree representation nor the the nfa make
  6288.      * this very easy to write. :(
  6289.      */
  6290.  
  6291.   /* What we compile is different than what the parser returns.
  6292.    * Suppose the parser returns expression R.
  6293.    * Let R' be R with unnecessary register assignments removed 
  6294.    * (see REMOVE_UNECESSARY_SIDE_EFFECTS, above).
  6295.    *
  6296.    * What we will compile is the expression:
  6297.    *
  6298.    *    m{try}R{win}\|s{try}R'{win}
  6299.    *
  6300.    * {try} and {win} denote side effect epsilons (see EXPLORE_FUTURE).
  6301.    * 
  6302.    * When trying a match, we insert an `m' at the beginning of the 
  6303.    * string if the user wants registers to be filled, `s' if not.
  6304.    */
  6305.     new_rexp =
  6306.       rx_mk_r_alternate
  6307.     (&rxb->rx,
  6308.      rx_mk_r_concat (&rxb->rx, rx_mk_r_cset (&rxb->rx, cs2), rexp),
  6309.      rx_mk_r_concat (&rxb->rx,
  6310.              rx_mk_r_cset (&rxb->rx, cs), fewer_side_effects));
  6311.  
  6312.     if (!(new_rexp && cs && cs2))
  6313.       return REG_ESPACE;
  6314.     RX_bitset_enjoin (cs2, '\0'); /* prefixed to the rexp used for matching. */
  6315.     RX_bitset_enjoin (cs, '\1'); /* prefixed to the rexp used for searching. */
  6316.     rexp = new_rexp;
  6317.   }
  6318.  
  6319. #ifdef RX_DEBUG
  6320.   if (rx_debug_compile)
  6321.     {
  6322.       fputs ("\n...which is compiled as:\n", stdout);
  6323.       print_rexp (&rxb->rx, rexp, 2, re_seprint, stdout);
  6324.     }
  6325. #endif
  6326.   {
  6327.     struct rx_nfa_state *start = 0;
  6328.     struct rx_nfa_state *end = 0;
  6329.  
  6330.     if (!rx_build_nfa (&rxb->rx, rexp, &start, &end))
  6331.       return REG_ESPACE;    /*  */
  6332.     else
  6333.       {
  6334.     void * mem = (void *)rxb->buffer;
  6335.     unsigned long size = rxb->allocated;
  6336.     int start_id;
  6337.     char * perm_mem;
  6338.     int iterator_size = paramc * sizeof (params[0]);
  6339.  
  6340.     end->is_final = 1;
  6341.     start->is_start = 1;
  6342.     rx_name_nfa_states (&rxb->rx);
  6343.     start_id = start->id;
  6344. #ifdef RX_DEBUG
  6345.     if (rx_debug_compile)
  6346.       {
  6347.         fputs ("...giving the NFA: \n", stdout);
  6348.         dbug_rxb = rxb;
  6349.         print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  6350.       }
  6351. #endif
  6352.     if (!rx_eclose_nfa (&rxb->rx))
  6353.       return REG_ESPACE;
  6354.     else
  6355.       {
  6356.         rx_delete_epsilon_transitions (&rxb->rx);
  6357.         
  6358.         /* For compatability reasons, we need to shove the
  6359.          * compiled nfa into one chunk of malloced memory.
  6360.          */
  6361.         rxb->rx.reserved = (   sizeof (params[0]) * paramc
  6362.                 +  rx_sizeof_bitset (rxb->rx.local_cset_size));
  6363. #ifdef RX_DEBUG
  6364.         if (rx_debug_compile)
  6365.           {
  6366.         dbug_rxb = rxb;
  6367.         fputs ("...which cooks down (uncompactified) to: \n", stdout);
  6368.         print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  6369.           }
  6370. #endif
  6371.         if (!rx_compactify_nfa (&rxb->rx, &mem, &size))
  6372.           return REG_ESPACE;
  6373.         rxb->buffer = mem;
  6374.         rxb->allocated = size;
  6375.         rxb->rx.buffer = mem;
  6376.         rxb->rx.allocated = size;
  6377.         perm_mem = ((char *)rxb->rx.buffer
  6378.             + rxb->rx.allocated - rxb->rx.reserved);
  6379.         rxb->se_params = ((struct re_se_params *)perm_mem);
  6380.         bcopy (params, rxb->se_params, iterator_size);
  6381.         perm_mem += iterator_size;
  6382.         rxb->fastset = (rx_Bitset) perm_mem;
  6383.         rxb->start = rx_id_to_nfa_state (&rxb->rx, start_id);
  6384.       }
  6385.     rx_bitset_null (rxb->rx.local_cset_size, rxb->fastset);
  6386.     rxb->can_match_empty = compute_fastset (rxb, orig_rexp);
  6387.     rxb->match_regs_on_stack =
  6388.       registers_on_stack (rxb, orig_rexp, 0, params); 
  6389.     rxb->search_regs_on_stack =
  6390.       registers_on_stack (rxb, fewer_side_effects, 0, params);
  6391.     if (rxb->can_match_empty)
  6392.       rx_bitset_universe (rxb->rx.local_cset_size, rxb->fastset);
  6393.     rxb->is_anchored = is_anchored (orig_rexp, (rx_side_effect) re_se_hat);
  6394.     rxb->begbuf_only = is_anchored (orig_rexp,
  6395.                     (rx_side_effect) re_se_begbuf);
  6396.       }
  6397.     rx_free_rexp (&rxb->rx, rexp);
  6398.     if (params)
  6399.       free (params);
  6400. #ifdef RX_DEBUG
  6401.     if (rx_debug_compile)
  6402.       {
  6403.     dbug_rxb = rxb;
  6404.     fputs ("...which cooks down to: \n", stdout);
  6405.     print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  6406.       }
  6407. #endif
  6408.   }
  6409.   return REG_NOERROR;
  6410. }
  6411.  
  6412.  
  6413.  
  6414. /* This table gives an error message for each of the error codes listed
  6415.    in regex.h.  Obviously the order here has to be same as there.  */
  6416.  
  6417. const char * rx_error_msg[] =
  6418. { 0,                        /* REG_NOERROR */
  6419.     "No match",                    /* REG_NOMATCH */
  6420.     "Invalid regular expression",        /* REG_BADPAT */
  6421.     "Invalid collation character",        /* REG_ECOLLATE */
  6422.     "Invalid character class name",        /* REG_ECTYPE */
  6423.     "Trailing backslash",            /* REG_EESCAPE */
  6424.     "Invalid back reference",            /* REG_ESUBREG */
  6425.     "Unmatched [ or [^",            /* REG_EBRACK */
  6426.     "Unmatched ( or \\(",            /* REG_EPAREN */
  6427.     "Unmatched \\{",                /* REG_EBRACE */
  6428.     "Invalid content of \\{\\}",        /* REG_BADBR */
  6429.     "Invalid range end",            /* REG_ERANGE */
  6430.     "Memory exhausted",                /* REG_ESPACE */
  6431.     "Invalid preceding regular expression",    /* REG_BADRPT */
  6432.     "Premature end of regular expression",    /* REG_EEND */
  6433.     "Regular expression too big",        /* REG_ESIZE */
  6434.     "Unmatched ) or \\)",            /* REG_ERPAREN */
  6435. };
  6436.  
  6437.  
  6438.  
  6439. /* Test if at very beginning or at very end of the virtual concatenation
  6440.  *  of `string1' and `string2'.  If only one string, it's `string2'.  
  6441.  */
  6442.  
  6443. #define AT_STRINGS_BEG() \
  6444.   (string1 \
  6445.    ? ((tst_half == 0) \
  6446.       && ((unsigned char *)tst_pos == (unsigned char *)string1 - 1)) \
  6447.    : ((unsigned char *)tst_pos == (unsigned char *)string2 - 1))
  6448.  
  6449. #define AT_STRINGS_END() \
  6450.   (string2 \
  6451.    ? ((tst_half == 1) \
  6452.       && ((unsigned char *)tst_pos \
  6453.       == ((unsigned char *)string2 + size2 - 1))) \
  6454.    : ((unsigned char *)tst_pos == ((unsigned char *)string1 + size1 - 1)))
  6455.  
  6456. /* Test if D points to a character which is word-constituent.  We have
  6457.  * two special cases to check for: if past the end of string1, look at
  6458.  * the first character in string2; and if before the beginning of
  6459.  * string2, look at the last character in string1.
  6460.  *
  6461.  * Assumes `string1' exists, so use in conjunction with AT_STRINGS_BEG ().  
  6462.  */
  6463. #define LETTER_P(d)                            \
  6464.   (SYNTAX ((string2 && (tst_half == 0)                    \
  6465.         && ((d) == ((unsigned char *)string1 + size1)))        \
  6466.        ? *(unsigned char *)string2                    \
  6467.        : ((string1 && (tst_half == 1)                \
  6468.            && ((d) == (unsigned char *)string2 - 1))        \
  6469.           ? *((unsigned char *)string1 + size1 - 1)            \
  6470.           : *(d))) == Sword)
  6471.  
  6472. /* Test if the character at D and the one after D differ with respect
  6473.  * to being word-constituent.  
  6474.  */
  6475. #define AT_WORD_BOUNDARY(d)                        \
  6476.   (AT_STRINGS_BEG () || AT_STRINGS_END () || LETTER_P (d) != LETTER_P (d + 1))
  6477.  
  6478.  
  6479. static char slowmap [256] =
  6480. {
  6481.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6482.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6483.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6484.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6485.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6486.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6487.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6488.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6489.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6490.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6491.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6492.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6493.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6494.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6495.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6496.   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6497. };
  6498.  
  6499. #ifdef __STDC__
  6500. static void
  6501. rx_blow_up_fastmap (struct re_pattern_buffer * rxb)
  6502. #else
  6503. static void
  6504. rx_blow_up_fastmap (rxb)
  6505.      struct re_pattern_buffer * rxb;
  6506. #endif
  6507. {
  6508.   int x;
  6509.   for (x = 0; x < 256; ++x)    /* &&&& 3.6 % */
  6510.     rxb->fastmap [x] = !!RX_bitset_member (rxb->fastset, x);
  6511.   rxb->fastmap_accurate = 1;
  6512. }
  6513.  
  6514.  
  6515.  
  6516.  
  6517. struct stack_chunk
  6518. {
  6519.   struct stack_chunk * next_chunk;
  6520.   int bytes_left;
  6521.   char * sp;
  6522. };
  6523.  
  6524. #define PUSH(CHUNK_VAR,BYTES)   \
  6525.   if (!CHUNK_VAR || (CHUNK_VAR->bytes_left < (BYTES)))  \
  6526.     {                    \
  6527.       struct stack_chunk * new_chunk;    \
  6528.       if (free_chunks)            \
  6529.     {                \
  6530.       new_chunk = free_chunks;    \
  6531.       free_chunks = free_chunks->next_chunk; \
  6532.     }                \
  6533.       else                \
  6534.     {                \
  6535.       new_chunk = (struct stack_chunk *)alloca (chunk_bytes); \
  6536.       if (!new_chunk)        \
  6537.         {                \
  6538.           ret_val = 0;        \
  6539.           goto test_do_return;    \
  6540.         }                \
  6541.     }                \
  6542.       new_chunk->sp = (char *)new_chunk + sizeof (struct stack_chunk); \
  6543.       new_chunk->bytes_left = (chunk_bytes \
  6544.                    - (BYTES) \
  6545.                    - sizeof (struct stack_chunk)); \
  6546.       new_chunk->next_chunk = CHUNK_VAR; \
  6547.       CHUNK_VAR = new_chunk;        \
  6548.     } \
  6549.   else \
  6550.     (CHUNK_VAR->sp += (BYTES)), (CHUNK_VAR->bytes_left -= (BYTES))
  6551.  
  6552. #define POP(CHUNK_VAR,BYTES) \
  6553.   if (CHUNK_VAR->sp == ((char *)CHUNK_VAR + sizeof(*CHUNK_VAR))) \
  6554.     { \
  6555.       struct stack_chunk * new_chunk = CHUNK_VAR->next_chunk; \
  6556.       CHUNK_VAR->next_chunk = free_chunks; \
  6557.       free_chunks = CHUNK_VAR; \
  6558.       CHUNK_VAR = new_chunk; \
  6559.     } \
  6560.   else \
  6561.     (CHUNK_VAR->sp -= BYTES), (CHUNK_VAR->bytes_left += BYTES)
  6562.  
  6563. struct counter_frame
  6564. {
  6565.   int tag;
  6566.   int val;
  6567.   struct counter_frame * inherited_from; /* If this is a copy. */
  6568.   struct counter_frame * cdr;
  6569. };
  6570.  
  6571. struct backtrack_frame
  6572. {
  6573.   char * counter_stack_sp;
  6574.  
  6575.   /* A frame is used to save the matchers state when it crosses a 
  6576.    * backtracking point.  The `stk_' fields correspond to variables
  6577.    * in re_search_2 (just strip off thes `stk_').  They are documented
  6578.    * tere.
  6579.    */
  6580.   struct rx_superstate * stk_super;
  6581.   const unsigned char * stk_tst_pos;
  6582.   int stk_tst_half;
  6583.   unsigned int stk_c;
  6584.   const unsigned char * stk_tst_str_half;
  6585.   const unsigned char * stk_tst_end_half;
  6586.   int stk_last_l;
  6587.   int stk_last_r;
  6588.   int stk_test_ret;
  6589.  
  6590.   /* This is the list of options left to explore at the backtrack
  6591.    * point for which this frame was created. 
  6592.    */
  6593.   struct rx_distinct_future * df;
  6594.   struct rx_distinct_future * first_df;
  6595.  
  6596. #ifdef RX_DEBUG
  6597.    int stk_line_no;
  6598. #endif
  6599. };
  6600.  
  6601.  
  6602.  
  6603. #if !defined(REGEX_MALLOC) && !defined(__GNUC__)
  6604. #define RE_SEARCH_2_FN    inner_re_search_2
  6605. #else
  6606. #define RE_SEARCH_2_FN    re_search_2
  6607. #endif
  6608.  
  6609. #ifdef __STDC__
  6610. int
  6611. RE_SEARCH_2_FN (struct re_pattern_buffer *rxb,
  6612.         const char * string1, int size1,
  6613.         const char * string2, int size2,
  6614.         int startpos, int range,
  6615.         struct re_registers *regs,
  6616.         int stop)
  6617. #else
  6618. int
  6619. RE_SEARCH_2_FN (rxb,
  6620.         string1, size1, string2, size2, startpos, range, regs, stop)
  6621.      struct re_pattern_buffer *rxb;
  6622.      const char * string1;
  6623.      int size1;
  6624.      const char * string2;
  6625.      int size2;
  6626.      int startpos;
  6627.      int range;
  6628.      struct re_registers *regs;
  6629.      int stop;
  6630. #endif
  6631. {
  6632.   /* Two groups of registers are kept.  The group with the register state
  6633.    * of the current test match, and the group that holds the state at the end
  6634.    * of the best known match, if any.
  6635.    *
  6636.    * For some patterns, there may also be registers saved on the stack.
  6637.    */
  6638.   regoff_t * lparen = 0; /* scratch space for register returns */
  6639.   regoff_t * rparen = 0;
  6640.   regoff_t * best_lpspace = 0; /* in case the user doesn't want these */
  6641.   regoff_t * best_rpspace = 0; /* values, we still need space to store
  6642.                 * them.  Normally, this memoryis unused
  6643.                 * and the space pointed to by REGS is 
  6644.                 * used instead.
  6645.                 */
  6646.   
  6647.   int last_l;            /* Highest index of a valid lparen. */
  6648.   int last_r;            /* It's dual. */
  6649.  
  6650.   int * best_lparen;        /* This contains the best known register */
  6651.   int * best_rparen;        /* assignments. 
  6652.                  * This may point to the same mem as
  6653.                  * best_lpspace, or it might point to memory
  6654.                  * passed by the caller.
  6655.                  */
  6656.   int best_last_l;        /* best_last_l:best_lparen::last_l:lparen */
  6657.   int best_last_r;
  6658.   
  6659.   
  6660.  
  6661.   /* Figure the number of registers we may need for use in backreferences.
  6662.    * The number here includes an element for register zero.  
  6663.    */
  6664.   unsigned num_regs = rxb->re_nsub + 1;
  6665.  
  6666.   int total_size = size1 + size2;
  6667.  
  6668.  
  6669.   /***** INIT re_search_2 */
  6670.   
  6671.   /* Check for out-of-range STARTPOS.  */
  6672.   if ((startpos < 0) || (startpos > total_size))
  6673.     return -1;
  6674.  
  6675.   /* Fix up RANGE if it might eventually take us outside
  6676.    * the virtual concatenation of STRING1 and STRING2.
  6677.    */
  6678.   {
  6679.     int endpos = startpos + range;
  6680.     if (endpos < -1)
  6681.       range = (-1 - startpos);
  6682.     else if (endpos > total_size)
  6683.       range = total_size - startpos;
  6684.   }
  6685.  
  6686.   /* If the search isn't to be a backwards one, don't waste time in a
  6687.    * long search for a pattern that says it is anchored.
  6688.    */
  6689.   if (rxb->begbuf_only && (range > 0))
  6690.     {
  6691.       if (startpos > 0)
  6692.     return -1;
  6693.       else
  6694.     range = 1;
  6695.     }
  6696.  
  6697.   /* Then, decide whether to use internal or user-provided reg buffers. */
  6698.   if (!regs || rxb->no_sub)
  6699.     {
  6700.       best_lpspace = (regoff_t *)REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  6701.       best_rpspace = (regoff_t *)REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  6702.       best_lparen = best_lpspace;
  6703.       best_rparen = best_rpspace;
  6704.     }
  6705.   else
  6706.     {    
  6707.       /* Have the register data arrays been allocated?  */
  6708.       if (rxb->regs_allocated == REGS_UNALLOCATED)
  6709.     { /* No.  So allocate them with malloc.  We need one
  6710.          extra element beyond `num_regs' for the `-1' marker
  6711.          GNU code uses.  */
  6712.       regs->num_regs = MAX (RE_NREGS, rxb->re_nsub + 1);
  6713.       regs->start = TALLOC (regs->num_regs, regoff_t);
  6714.       regs->end = TALLOC (regs->num_regs, regoff_t);
  6715.       if (regs->start == 0 || regs->end == 0)
  6716.         return -2;
  6717.       rxb->regs_allocated = REGS_REALLOCATE;
  6718.     }
  6719.       else if (rxb->regs_allocated == REGS_REALLOCATE)
  6720.     { /* Yes.  If we need more elements than were already
  6721.          allocated, reallocate them.  If we need fewer, just
  6722.          leave it alone.  */
  6723.       if (regs->num_regs < num_regs + 1)
  6724.         {
  6725.           regs->num_regs = num_regs + 1;
  6726.           RETALLOC (regs->start, regs->num_regs, regoff_t);
  6727.           RETALLOC (regs->end, regs->num_regs, regoff_t);
  6728.           if (regs->start == 0 || regs->end == 0)
  6729.         return -2;
  6730.         }
  6731.     }
  6732.       else if (rxb->regs_allocated != REGS_FIXED)
  6733.     return -2;
  6734.  
  6735.       if (regs->num_regs < num_regs + 1)
  6736.     {
  6737.       best_lpspace = ((regoff_t *)
  6738.               REGEX_ALLOCATE (num_regs * sizeof(regoff_t)));
  6739.       best_rpspace = ((regoff_t *)
  6740.               REGEX_ALLOCATE (num_regs * sizeof(regoff_t)));
  6741.       best_lparen = best_lpspace;
  6742.       best_rparen = best_rpspace;
  6743.     }
  6744.       else
  6745.     {
  6746.       best_lparen = regs->start;
  6747.       best_rparen = regs->end;
  6748.     }
  6749.     }
  6750.   
  6751.   lparen = (regoff_t *) REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  6752.   rparen = (regoff_t *) REGEX_ALLOCATE (num_regs * sizeof(regoff_t)); 
  6753.   
  6754.   if (!(best_rparen && best_lparen && lparen && rparen))
  6755.     return -2;
  6756.   
  6757.   best_last_l = best_last_r = -1;
  6758.  
  6759.  
  6760.  
  6761.   /***** fastmap/search loop, initialization */
  6762.  
  6763.   /* This is the loop that scans using the fastmap, and sometimes tries to 
  6764.    * match. From this point on, don't return.  Instead, assign to ret_val
  6765.    * and goto fail.
  6766.    */
  6767.   {
  6768.     const char * translate = rxb->translate ? rxb->translate : id_translation;
  6769.  
  6770.     /** This is state associated with returning to the caller. */
  6771.  
  6772.     int ret_val = -1;
  6773.  
  6774.     /*   A sentinal is sometimes installed in the fastmap.  This records
  6775.      *   where so it can be removed before returning.
  6776.      */
  6777.     int fastmap_chr = -1;
  6778.     int fastmap_val;
  6779.  
  6780.     /** End of state associated with returning to the caller. */
  6781.  
  6782.     /** Start of variables associated with the fastmap based search: */
  6783.  
  6784.     char * fastmap = rxb->fastmap ? (char *)rxb->fastmap : (char *)slowmap;
  6785.     int search_direction;    /* 1 or -1 */
  6786.     int search_end;        /* first position to not try */
  6787.     int offset;            /* either size1 or 0 as string == string2 */
  6788.  
  6789.     /* The string-pair position of the fastmap/search loop: */
  6790.     const unsigned char * pos;    /* The current pos. */
  6791.     const unsigned char * string; /* The current string half. */
  6792.     const unsigned char * end;    /* End of current string. */
  6793.     int size;            /* Current string's size */
  6794.     int half;            /* 0 means string1, 1 means string2 */
  6795.  
  6796.     /** End of variables associated with the fastmap based search: */
  6797.  
  6798.  
  6799.     /** Start of variables associated with trying a match
  6800.      *  after the fastmap has found a plausible starting point.
  6801.      */
  6802.  
  6803.     struct rx_superstate * start_super = 0; /* The superNFA start state. */
  6804.  
  6805.     /*
  6806.      * Two nfa's were compiled.  
  6807.      * `0' is complete.
  6808.      * `1' faster but gets registers wrong and ends too soon.
  6809.      */
  6810.     int nfa_choice = ((regs && !rxb->least_subs) ? '\0' : '\1');
  6811.  
  6812.     const unsigned char * abs_end; /* Don't fetch a character from here. */
  6813.     int first_found;        /* If true, return after finding any match. */
  6814.  
  6815.     /** End of variables associated with trying a match. */
  6816.  
  6817.     /* Update the fastmap now if not correct already. 
  6818.      * When the regexp was compiled, the fastmap was computed
  6819.      * and stored in a bitset.  This expands the bitset into a
  6820.      * character array containing 1s and 0s.
  6821.      */
  6822.     if ((fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
  6823.       rx_blow_up_fastmap (rxb);
  6824.  
  6825.     /* Now we build the starting state of the supernfa. */
  6826.     {
  6827.       struct rx_superset * start_contents;
  6828.       struct rx_nfa_state_set * start_nfa_set;
  6829.       
  6830.       /* We presume here that the nfa start state has only one
  6831.        * possible future with no side effects.  
  6832.        */
  6833.       start_nfa_set = rxb->start->futures->destset;
  6834.       if (   rxb->rx.start_set
  6835.       && (rxb->rx.start_set->starts_for == &rxb->rx))
  6836.     start_contents = rxb->rx.start_set;
  6837.       else
  6838.     {
  6839.       start_contents =
  6840.         rx_superstate_eclosure_union (&rxb->rx,
  6841.                       rx_superset_cons (&rxb->rx, 0, 0),
  6842.                       start_nfa_set);
  6843.       
  6844.       if (!start_contents)
  6845.         return -1;
  6846.  
  6847.       start_contents->starts_for = &rxb->rx;
  6848.       rxb->rx.start_set = start_contents;
  6849.     }
  6850.       if (   start_contents->superstate
  6851.       && (start_contents->superstate->rx_id == rxb->rx.rx_id))
  6852.     {
  6853.       start_super = start_contents->superstate;
  6854.       rx_lock_superstate (&rxb->rx, start_super);
  6855.     }
  6856.       else
  6857.     {
  6858.       rx_protect_superset (&rxb->rx, start_contents);
  6859.       
  6860.       start_super = rx_superstate (&rxb->rx, start_contents);
  6861.       if (!start_super)
  6862.         return -1;
  6863.       rx_lock_superstate (&rxb->rx, start_super);
  6864.       rx_release_superset (&rxb->rx, start_contents);
  6865.     }
  6866.     }
  6867.     
  6868.     /* This computes an upper bound on string addresses for use by
  6869.      * the match-test.
  6870.      */
  6871.     abs_end = ((const unsigned char *) ((stop <= size1)
  6872.                     ? string1 + stop
  6873.                     : string2 + stop - size1));
  6874.  
  6875.     /* We have the option to look for the best match or the first
  6876.      * one we can find.  If the user isn't asking for register information,
  6877.      * we don't need to find the best match.
  6878.      */
  6879.     first_found = !regs;
  6880.  
  6881.     /* Compute search_end & search_direction for the fastmap loop. */
  6882.     if (range >= 0)
  6883.       {
  6884.     search_end = MIN (size1 + size2, startpos + range) + 1;
  6885.     search_direction = 1;
  6886.       }
  6887.     else
  6888.       {
  6889.     search_end = MAX(-1, startpos + range);
  6890.     search_direction = -1;
  6891.       }
  6892.  
  6893.     /* The vacuous search always turns up nothing. */
  6894.     if ((search_direction == 1)
  6895.     ? (startpos > search_end)
  6896.     : (startpos < search_end))
  6897.       return -1;
  6898.  
  6899.     /* Set string/size/offset/end -- the state that tells the fastmap
  6900.      * loop which half of the string we're in.  Also set pos, which
  6901.      * is the addr of the current fastmap scan position.
  6902.      */
  6903.     if (!string2 || (startpos < size1))
  6904.       {
  6905.     string = (const unsigned char *)string1;
  6906.     size = size1;
  6907.     offset = 0;
  6908.     pos = (const unsigned char *)(string1 + startpos);
  6909.     half = 0;
  6910.     end = (const unsigned char *)MIN(string1 + size1, string1 + stop);
  6911.       }
  6912.     else
  6913.       {
  6914.     string = (const unsigned char *)string2;
  6915.     size = size2;
  6916.     offset = size1;
  6917.     pos = (const unsigned char *)(string2 + startpos - size1);
  6918.     half = 1;
  6919.     end = (const unsigned char *)MIN(string2 + size2,
  6920.                      string2 + stop - size1);
  6921.       }
  6922.  
  6923.  
  6924.  
  6925.  
  6926.     /***** fastmap/search loop,  body */
  6927.  
  6928.  
  6929.   init_fastmap_sentinal:
  6930.  
  6931.     /* For the sake of fast fastmapping, set a sentinal in the fastmap.
  6932.      * This sentinal will trap the fastmap loop when it reaches the last
  6933.      * valid character in a string half.
  6934.      *
  6935.      * This must be reset when the fastmap/search loop crosses a string 
  6936.      * boundry, and before returning to the caller.  So sometimes,
  6937.      * the fastmap loop is restarted with `continue', othertimes by
  6938.      * `goto init_fastmap_sentinal'.
  6939.      */
  6940.     if (size)
  6941.       {
  6942.     fastmap_chr = ((search_direction == 1)
  6943.                ? *(end - 1)
  6944.                : *string);
  6945.     fastmap_val = fastmap[fastmap_chr];
  6946.     fastmap[fastmap_chr] = 1;
  6947.       }
  6948.     else
  6949.       fastmap_chr = -1;
  6950.  
  6951.     do
  6952.       {
  6953.     /* If we haven't reached the end of a string half, and if the
  6954.      * pattern can't match the empty string, then the fastmap 
  6955.      * optimization applies.  This conditional scans using the 
  6956.      * fastmap -- stoping when a string half ends, or when a 
  6957.      * plausible starting point for a match is found.
  6958.      * It updates HIT_BOUND to tell which case occured.
  6959.      */
  6960.     if (pos == end)
  6961.       goto fastmap_hit_bound;
  6962.     else
  6963.       {
  6964.         if (search_direction == 1)
  6965.           {
  6966.         if (fastmap_val)
  6967.           {
  6968.             for (;;)
  6969.               {
  6970.             while (!fastmap[*pos])
  6971.               ++pos;
  6972.             goto commence_a_matchin;
  6973.               }
  6974.           }
  6975.         else
  6976.           {
  6977.             for (;;)
  6978.               {
  6979.             while (!fastmap[*pos])
  6980.               ++pos;
  6981.             if (*pos != fastmap_chr)
  6982.               goto commence_a_matchin;
  6983.             else 
  6984.               {
  6985.                 ++pos;
  6986.                 if (pos == end)
  6987.                   goto fastmap_hit_bound;
  6988.               }
  6989.               }
  6990.           }
  6991.           }
  6992.         else
  6993.           {
  6994.         const unsigned char * bound = string - 1;
  6995.         for (;;)
  6996.           {
  6997.             while (!fastmap[*pos])
  6998.               --pos;
  6999.             if ((*pos != fastmap_chr) || fastmap_val)
  7000.               goto commence_a_matchin;
  7001.             else 
  7002.               {
  7003.             --pos;
  7004.             if (pos == bound)
  7005.               goto fastmap_hit_bound;
  7006.               }
  7007.           }
  7008.           }
  7009.       }
  7010.     
  7011.       fastmap_hit_bound:
  7012.     {
  7013.       /* If we hit a bound, it may simply be time to switch sides
  7014.        * between strings.
  7015.        */
  7016.       if ((search_direction == 1) && string2 && (half == 0))
  7017.         {
  7018.           string = (const unsigned char *)string2;
  7019.           size = size2;
  7020.           offset = size1;
  7021.           half = 1;
  7022.           end = (const unsigned char *)MIN(string2 + size2,
  7023.                            string2 + stop - size1);
  7024.           startpos = size1;
  7025.           pos = (const unsigned char *)string2;
  7026.           goto init_fastmap_sentinal;
  7027.         }
  7028.       else if (   string1
  7029.            && (search_direction == -1)
  7030.            && (half == 1))
  7031.         {
  7032.           string = (const unsigned char *)string1;
  7033.           size = size1;
  7034.           offset = 0;
  7035.           end = (const unsigned char *)string1 + size1;
  7036.           half = 0;
  7037.           startpos = size1 - 1;
  7038.           pos = (const unsigned char *)string1 + size1 - 1;
  7039.           goto init_fastmap_sentinal;
  7040.         }
  7041.       /* ...not a string split, simply no more string. 
  7042.        *
  7043.        * When searching backward, running out of string
  7044.        * is reason to quit.
  7045.        */
  7046.       else if (search_direction == -1)
  7047.         goto finish;
  7048.       
  7049.       /* ...when searching forward, we allow the possibility
  7050.        * of an (empty) match after the last character in the
  7051.        * virtual string.  So, fall through to the matcher
  7052.        */
  7053.     }
  7054.  
  7055.  
  7056.       commence_a_matchin:
  7057.  
  7058.     /***** fastmap/search loop body
  7059.      *          test for a match that begins at pos
  7060.      */
  7061.  
  7062.     /* Now the fastmap loop has brought us to a plausible 
  7063.      * starting point for a match.  So, it's time to run the
  7064.      * NFA and see if a match occured.
  7065.      */
  7066.  
  7067.     startpos = pos - string + offset;
  7068.     if (startpos == search_end)
  7069.       goto finish;
  7070.     
  7071.     last_l = last_r = 0;
  7072.     lparen[0] = startpos;    /* We know match-begin for this test... */
  7073.  
  7074.     /* The test matcher is essentially a recursive function
  7075.      * that does an exhaustive run of the superNFA at the 
  7076.      * test position.  For performance, that function has 
  7077.      * been in-lined by hand.
  7078.      */
  7079.  
  7080. #undef OF
  7081. #ifndef HAVE_GNUC_LABELS
  7082. #define OF(A,B)    A
  7083. #else
  7084. #define OF(A,B)    A: B
  7085.       static void * rx_labels_instruction_table[] =
  7086.         {
  7087.           [rx_backtrack_point] &&backtrack_point,
  7088.           [rx_backtrack] &&backtrack,
  7089.           [rx_do_side_effects] &&do_side_effects,
  7090.           [rx_cache_miss] &&cache_miss,
  7091.           [rx_next_char] 0,
  7092.           [rx_error_inx] 0
  7093.         };
  7094. #endif
  7095.     {      
  7096.       /* The current superNFA position of the matcher. */
  7097.       struct rx_superstate * super = start_super;
  7098.       
  7099.       /* The matcher interprets a series of instruction frames.
  7100.        * This is the `instruction counter' for the interpretation.
  7101.        */
  7102.       struct rx_inx * ifr;
  7103.       
  7104.       /* We insert a ghost character in the string to prime
  7105.        * the nfa.  tst_pos, tst_str_half, and tst_end_half
  7106.        * keep track of the test-match position and string-half.
  7107.        */
  7108.       const unsigned char * tst_pos = pos - 1;
  7109.       int tst_half = half;
  7110.       unsigned char c = nfa_choice;
  7111.       
  7112.       const unsigned char * tst_str_half = string;
  7113.       const unsigned char * tst_end_half = end;
  7114.       
  7115.       struct stack_chunk * counter_stack = 0;
  7116.       struct stack_chunk * backtrack_stack = 0;
  7117.       int backtrack_frame_bytes =
  7118.         (sizeof (struct backtrack_frame)
  7119.          + (rxb->match_regs_on_stack
  7120.         ? sizeof (regoff_t) * (num_regs + 1) * 2
  7121.         : 0));
  7122.       int chunk_bytes = backtrack_frame_bytes * 64;
  7123.       struct stack_chunk * free_chunks = 0;
  7124.  
  7125. #ifdef RX_DEBUG
  7126.       int backtrack_depth = 0;
  7127. #endif
  7128.  
  7129.       /* To return from this function, set test_ret and 
  7130.        * `goto test_do_return'.
  7131.        *
  7132.        * Possible return values are:
  7133.        *     1   --- end of string while the superNFA is still going
  7134.        *     0   --- internal error (out of memory)
  7135.        *    -1   --- search completed by reaching the superNFA fail state
  7136.        *    -2   --- a match was found, maybe not the longest.
  7137.        *
  7138.        * When the search is complete (-1), best_last_r indicates whether
  7139.        * a match was found.
  7140.        *
  7141.        * -2 is return only if first_found is non-zero.
  7142.        *
  7143.        * if first_found is non-zero, a return of -1 indicates no match,
  7144.        * otherwise, best_last_r has to be checked.
  7145.        */
  7146.       int test_ret = -1;
  7147.       
  7148.       while (1)
  7149.         {
  7150.           int inx;
  7151. #ifdef RX_DEBUG
  7152.           /* There is a search tree with every node as set of deterministic
  7153.            * transitions in the super nfa.  For every branch of a 
  7154.            * backtrack point is an edge in the tree.
  7155.            * This counts up a pre-order of nodes in that tree.
  7156.            * It's saved on the search stack and printed when debugging. 
  7157.            */
  7158.           int line_no = 0;
  7159.           int lines_found = 0;
  7160. #endif
  7161.  
  7162.  
  7163.         top_of_cycle:
  7164.           /* A superstate is basicly a transition table, indexed by 
  7165.            * characters from the string being tested, and containing 
  7166.            * RX_INX structures.
  7167.            */
  7168.           ifr = &super->transitions [c];
  7169.           
  7170.         recurse_test_match:
  7171.           /* This is the point to which control is sent when the
  7172.            * test matcher recurses.  Before jumping here, some variables
  7173.            * need to be saved on the stack and setup for the recursion.
  7174.            */
  7175.  
  7176.         restart:
  7177.           /* Some instructions don't advance the matcher, but just
  7178.            * carry out some side effects and fetch a new instruction.
  7179.            * To dispatch that new instruction, `goto restart'.
  7180.            */
  7181.           
  7182.           {
  7183.         struct rx_inx * next_tr_table = (struct rx_inx *)ifr->data;
  7184.         struct rx_inx * this_tr_table = super->transitions;
  7185.         /* The fastest route through the loop is when the instruction 
  7186.          * is RX_NEXT_CHAR.  This case is detected when IFR->DATA
  7187.          * is non-zero.  In that case, it points to the next
  7188.          * superstate. 
  7189.          *
  7190.          * This allows us to not bother fetching the bytecode.
  7191.          */
  7192.         while (next_tr_table)
  7193.           {
  7194. #ifdef RX_DEBUG
  7195.             if (rx_debug_trace)
  7196.               {
  7197.             struct rx_superset * setp;
  7198.  
  7199.             fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
  7200.                  line_no,
  7201.                  backtrack_depth,
  7202.                  (tst_pos - tst_str_half
  7203.                   + (tst_half == 0
  7204.                      ? 0 : size1)), c);
  7205.             
  7206.             super =
  7207.               ((struct rx_superstate *)
  7208.                ((char *)this_tr_table
  7209.                 - ((unsigned long)
  7210.                    ((struct rx_superstate *)0)->transitions)));
  7211.  
  7212.             setp = super->contents;
  7213.             fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  7214.                  rxb->rx.rx_id, setp);
  7215.             while (setp)
  7216.               {
  7217.                 fprintf (stderr, "%d ", setp->id);
  7218.                 setp = setp->cdr;
  7219.               }
  7220.             fprintf (stderr, "\n");
  7221.               }
  7222. #endif
  7223.             this_tr_table = next_tr_table;
  7224.             ++tst_pos;
  7225.             if (tst_pos == tst_end_half)
  7226.               {
  7227.             if (   (tst_pos != abs_end)
  7228.                 && string2
  7229.                 && half == 0)
  7230.               {
  7231.                 /* Here we are crossing the break 
  7232.                  * in a split string. 
  7233.                  */
  7234.                 tst_str_half = (const unsigned char *)string2;
  7235.                 tst_end_half = abs_end;
  7236.                 tst_pos = (const unsigned char *)string2;
  7237.                 tst_half = 1;
  7238.               }
  7239.             else
  7240.               {
  7241.                 test_ret = 1;
  7242.                 goto test_do_return;
  7243.               }
  7244.               }
  7245.             c = *tst_pos;
  7246.             ifr = this_tr_table + c;
  7247.             next_tr_table = (struct rx_inx *)ifr->data;
  7248.           }
  7249.         
  7250.         /* Here when we ran out cached next-char transitions. 
  7251.          * So, it will be necessary to do a more expensive
  7252.          * dispatch on the current instruction.  The superstate
  7253.          * pointer is allowed to become invalid during next-char
  7254.          * transitions -- now we must bring it up to date.
  7255.          */
  7256.         super =
  7257.           ((struct rx_superstate *)
  7258.            ((char *)this_tr_table
  7259.             - ((unsigned long)
  7260.                ((struct rx_superstate *)0)->transitions)));
  7261.           }
  7262.           
  7263.           /* We've encountered an instruction other than next-char.
  7264.            * Dispatch that instruction:
  7265.            */
  7266.           inx = (int)ifr->inx;
  7267. #ifdef HAVE_GNUC_LABELS
  7268.           goto *rx_labels_instruction_table[inx];
  7269. #endif
  7270. #ifdef RX_DEBUG
  7271.           if (rx_debug_trace)
  7272.         {
  7273.           struct rx_superset * setp = super->contents;
  7274.           
  7275.           fprintf (stderr, "%d %d>> %s @ %d (%d)", line_no,
  7276.                backtrack_depth,
  7277.                inx_names[inx],
  7278.                (tst_pos - tst_str_half
  7279.                 + (tst_half == 0 ? 0 : size1)), c);
  7280.           
  7281.           fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  7282.                rxb->rx.rx_id, setp);
  7283.           while (setp)
  7284.             {
  7285.               fprintf (stderr, "%d ", setp->id);
  7286.               setp = setp->cdr;
  7287.             }
  7288.           fprintf (stderr, "\n");
  7289.         }
  7290. #endif
  7291.           switch ((enum rx_opcode)inx)
  7292.         {
  7293.         case OF(rx_do_side_effects,do_side_effects):
  7294.  
  7295.           /*  RX_DO_SIDE_EFFECTS occurs when we cross epsilon 
  7296.            *  edges associated with parentheses, backreferencing, etc.
  7297.            */
  7298.           {
  7299.             struct rx_distinct_future * df =
  7300.               (struct rx_distinct_future *)ifr->data_2;
  7301.             struct rx_se_list * el = df->effects;
  7302.             /* Side effects come in lists.  This walks down
  7303.              * a list, dispatching.
  7304.              */
  7305.             while (el)
  7306.               {
  7307. #ifdef HAVE_GNUC_LABELS
  7308.             static void * se_labels[] =
  7309.               {
  7310.                 [-re_se_try] &&se_try,
  7311.                 [-re_se_pushback] &&se_pushback,
  7312.                 [-re_se_push0] &&se_push0,
  7313.                 [-re_se_pushpos] &&se_pushpos,
  7314.                 [-re_se_chkpos] &&se_chkpos,
  7315.                 [-re_se_poppos] &&se_poppos,
  7316. #ifdef emacs
  7317.                 [-re_se_at_dot] &&se_at_dot,
  7318.                 [-re_se_syntax] &&se_syntax,
  7319.                 [-re_se_not_syntax] &&se_not_syntax,
  7320. #endif
  7321.                 [-re_se_begbuf] &&se_begbuf,
  7322.                 [-re_se_hat] &&se_hat,
  7323.                 [-re_se_wordbeg] &&se_wordbeg,
  7324.                 [-re_se_wordbound] &&se_wordbound,
  7325.                 [-re_se_notwordbound] &&se_notwordbound,
  7326.                 [-re_se_wordend] &&se_wordend,
  7327.                 [-re_se_endbuf] &&se_endbuf,
  7328.                 [-re_se_dollar] &&se_dollar,
  7329.                 [-re_se_fail] &&se_fail,
  7330.               };
  7331.             static void * se_lables2[] =
  7332.               {
  7333.                 [re_se_win] &&se_win
  7334.                 [re_se_lparen] &&se_lparen,
  7335.                 [re_se_rparen] &&se_rparen,
  7336.                 [re_se_backref] &&se_backref,
  7337.                 [re_se_iter] &&se_iter,
  7338.                 [re_se_end_iter] &&se_end_iter,
  7339.                 [re_se_tv] &&se_tv
  7340.               };
  7341. #endif
  7342.             int effect = (int)el->car;
  7343.             if (effect < 0)
  7344.               {
  7345. #ifdef HAVE_GNUC_LABELS
  7346.                 goto *se_labels[-effect];
  7347. #endif
  7348. #ifdef RX_DEBUG
  7349.                 if (rx_debug_trace)
  7350.                   {
  7351.                 struct rx_superset * setp = super->contents;
  7352.                 
  7353.                 fprintf (stderr, "....%d %d>> %s\n", line_no,
  7354.                      backtrack_depth,
  7355.                      efnames[-effect]);
  7356.                   }
  7357. #endif
  7358.                 switch ((enum re_side_effects) effect)
  7359.                   {
  7360.                   case OF(re_se_pushback,se_pushback):
  7361.                 ifr = &df->future_frame;
  7362.                 if (!ifr->data)
  7363.                   {
  7364.                     struct rx_superstate * sup = super;
  7365.                     rx_lock_superstate (rx, sup);
  7366.                     if (!rx_handle_cache_miss (&rxb->rx,
  7367.                                    super, c,
  7368.                                    ifr->data_2))
  7369.                       {
  7370.                     rx_unlock_superstate (rx, sup);
  7371.                     test_ret = 0;
  7372.                     goto test_do_return;
  7373.                       }
  7374.                     rx_unlock_superstate (rx, sup);
  7375.                   }
  7376.                 /* --tst_pos; */
  7377.                 c = 't';
  7378.                 super
  7379.                   = ((struct rx_superstate *)
  7380.                      ((char *)ifr->data
  7381.                       - (long)(((struct rx_superstate *)0)
  7382.                            ->transitions)));
  7383.                 goto top_of_cycle;
  7384.                 break;
  7385.                   case OF(re_se_push0,se_push0):
  7386.                 {
  7387.                   struct counter_frame * old_cf
  7388.                      = (counter_stack
  7389.                     ? ((struct counter_frame *)
  7390.                        counter_stack->sp)
  7391.                     : 0);
  7392.                   struct counter_frame * cf;
  7393.                   PUSH (counter_stack,
  7394.                     sizeof (struct counter_frame));
  7395.                   cf = ((struct counter_frame *)
  7396.                     counter_stack->sp);
  7397.                   cf->tag = re_se_iter;
  7398.                   cf->val = 0;
  7399.                   cf->inherited_from = 0;
  7400.                   cf->cdr = old_cf;
  7401.                   break;
  7402.                 }
  7403.                   case OF(re_se_fail,se_fail):
  7404.                 goto test_do_return;
  7405.                   case OF(re_se_begbuf,se_begbuf):
  7406.                 if (!AT_STRINGS_BEG ())
  7407.                   goto test_do_return;
  7408.                 break;
  7409.                   case OF(re_se_endbuf,se_endbuf):
  7410.                 if (!AT_STRINGS_END ())
  7411.                   goto test_do_return;
  7412.                 break;
  7413.                   case OF(re_se_wordbeg,se_wordbeg):
  7414.                 if (   LETTER_P (tst_pos + 1)
  7415.                     && (   AT_STRINGS_BEG()
  7416.                     || !LETTER_P (tst_pos)))
  7417.                   break;
  7418.                 else
  7419.                   goto test_do_return;
  7420.                   case OF(re_se_wordend,se_wordend):
  7421.                 if (   !AT_STRINGS_BEG ()
  7422.                     && LETTER_P (tst_pos)
  7423.                     && (AT_STRINGS_END ()
  7424.                     || !LETTER_P (tst_pos + 1)))
  7425.                   break;
  7426.                 else
  7427.                   goto test_do_return;
  7428.                   case OF(re_se_wordbound,se_wordbound):
  7429.                 if (AT_WORD_BOUNDARY (tst_pos))
  7430.                   break;
  7431.                 else
  7432.                   goto test_do_return;
  7433.                   case OF(re_se_notwordbound,se_notwordbound):
  7434.                 if (!AT_WORD_BOUNDARY (tst_pos))
  7435.                   break;
  7436.                 else
  7437.                   goto test_do_return;
  7438.                   case OF(re_se_hat,se_hat):
  7439.                 if (AT_STRINGS_BEG ())
  7440.                   {
  7441.                     if (rxb->not_bol)
  7442.                       goto test_do_return;
  7443.                     else
  7444.                       break;
  7445.                   }
  7446.                 else
  7447.                   {
  7448.                     char pos_c = *tst_pos;
  7449.                     if (   (TRANSLATE (pos_c)
  7450.                         == TRANSLATE('\n'))
  7451.                     && rxb->newline_anchor)
  7452.                       break;
  7453.                     else
  7454.                       goto test_do_return;
  7455.                   }
  7456.                   case OF(re_se_dollar,se_dollar):
  7457.                 if (AT_STRINGS_END ())
  7458.                   {
  7459.                     if (rxb->not_eol)
  7460.                       goto test_do_return;
  7461.                     else
  7462.                       break;
  7463.                   }
  7464.                 else
  7465.                   {
  7466.                     const unsigned char * next_pos
  7467.                       = ((string2 && (tst_half == 0) &&
  7468.                       (tst_pos
  7469.                        == ((unsigned char *)
  7470.                            string1 + size1 - 1)))
  7471.                      ? (unsigned char *)string2
  7472.                      : tst_pos + 1);
  7473.                     
  7474.                     if (   (TRANSLATE (*next_pos)
  7475.                         == TRANSLATE ('\n'))
  7476.                     && rxb->newline_anchor)
  7477.                       break;
  7478.                     else
  7479.                       goto test_do_return;
  7480.                   }
  7481.                 
  7482.                   case OF(re_se_try,se_try):
  7483.                 /* This is the first side effect in every
  7484.                  * expression.
  7485.                  *
  7486.                  *  FOR NO GOOD REASON...get rid of it...
  7487.                  */
  7488.                 break;
  7489.  
  7490.                   case OF(re_se_pushpos,se_pushpos):
  7491.                 {
  7492.                   int urhere =
  7493.                     ((int)(tst_pos - tst_str_half)
  7494.                      + ((tst_half == 0) ? 0 : size1));
  7495.                   struct counter_frame * old_cf
  7496.                     = (counter_stack
  7497.                        ? ((struct counter_frame *)
  7498.                       counter_stack->sp)
  7499.                        : 0);
  7500.                   struct counter_frame * cf;
  7501.                   PUSH(counter_stack,
  7502.                        sizeof (struct counter_frame));
  7503.                   cf = ((struct counter_frame *)
  7504.                     counter_stack->sp);
  7505.                   cf->tag = re_se_pushpos;
  7506.                   cf->val = urhere;
  7507.                   cf->inherited_from = 0;
  7508.                   cf->cdr = old_cf;
  7509.                   break;
  7510.                 }
  7511.                 
  7512.                   case OF(re_se_chkpos,se_chkpos):
  7513.                 {
  7514.                   int urhere =
  7515.                     ((int)(tst_pos - tst_str_half)
  7516.                      + ((tst_half == 0) ? 0 : size1));
  7517.                   struct counter_frame * cf
  7518.                     = ((struct counter_frame *)
  7519.                        counter_stack->sp);
  7520.                   if (cf->val == urhere)
  7521.                     goto test_do_return;
  7522.                   cf->val = urhere;
  7523.                   break;
  7524.                 }
  7525.                 break;
  7526.  
  7527.                   case OF(re_se_poppos,se_poppos):
  7528.                 POP(counter_stack,
  7529.                     sizeof (struct counter_frame));
  7530.                 break;
  7531.                 
  7532.                 
  7533.                   case OF(re_se_at_dot,se_at_dot):
  7534.                   case OF(re_se_syntax,se_syntax):
  7535.                   case OF(re_se_not_syntax,se_not_syntax):
  7536. #ifdef emacs
  7537.                 this release lacks emacs support;
  7538.                 (coming soon);
  7539. #endif
  7540.                 break;
  7541.                   case re_se_win:
  7542.                   case re_se_lparen:
  7543.                   case re_se_rparen:
  7544.                   case re_se_backref:
  7545.                   case re_se_iter:
  7546.                   case re_se_end_iter:
  7547.                   case re_se_tv:
  7548.                   case re_floogle_flap:
  7549.                 ret_val = 0;
  7550.                 goto test_do_return;
  7551.                   }
  7552.               }
  7553.             else
  7554.               {
  7555. #ifdef HAVE_GNUC_LABELS
  7556.                 goto *se_lables2[(rxb->se_params [effect].se)];
  7557. #endif
  7558. #ifdef RX_DEBUG
  7559.               if (rx_debug_trace)
  7560.                 fprintf (stderr, "....%d %d>> %s %d %d\n", line_no,
  7561.                      backtrack_depth,
  7562.                      efnames2[rxb->se_params [effect].se],
  7563.                      rxb->se_params [effect].op1,
  7564.                      rxb->se_params [effect].op2);
  7565. #endif
  7566.                 switch (rxb->se_params [effect].se)
  7567.                   {
  7568.                   case OF(re_se_win,se_win):
  7569.                 /* This side effect indicates that we've 
  7570.                  * found a match, though not necessarily the 
  7571.                  * best match.  This is a fancy assignment to 
  7572.                  * register 0 unless the caller didn't 
  7573.                  * care about registers.  In which case,
  7574.                  * this stops the match.
  7575.                  */
  7576.                 {
  7577.                   int urhere =
  7578.                     ((int)(tst_pos - tst_str_half)
  7579.                      + ((tst_half == 0)
  7580.                     ? 0 : size1));
  7581.  
  7582.                   if (   (best_last_r < 0)
  7583.                       || (urhere + 1 > best_rparen[0]))
  7584.                     {
  7585.                       /* Record the best known and keep
  7586.                        * looking.
  7587.                        */
  7588.                       int x;
  7589.                       for (x = 0; x <= last_l; ++x)
  7590.                     best_lparen[x] = lparen[x];
  7591.                       best_last_l = last_l;
  7592.                       for (x = 0; x <= last_r; ++x)
  7593.                     best_rparen[x] = rparen[x];
  7594.                       best_rparen[0] = urhere + 1;
  7595.                       best_last_r = last_r;
  7596.                     }
  7597.                   /* If we're not reporting the match-length 
  7598.                    * or other register info, we need look no
  7599.                    * further.
  7600.                    */
  7601.                   if (first_found)
  7602.                     {
  7603.                       test_ret = -2;
  7604.                       goto test_do_return;
  7605.                     }
  7606.                 }
  7607.                 break;
  7608.                   case OF(re_se_lparen,se_lparen):
  7609.                 {
  7610.                   int urhere =
  7611.                     ((int)(tst_pos - tst_str_half)
  7612.                      + ((tst_half == 0) ? 0 : size1));
  7613.                   
  7614.                   int reg = rxb->se_params [effect].op1;
  7615. #if 0
  7616.                   if (reg > last_l)
  7617. #endif
  7618.                     {
  7619.                       lparen[reg] = urhere + 1;
  7620.                       /* In addition to making this assignment,
  7621.                        * we now know that lower numbered regs
  7622.                        * that haven't already been assigned,
  7623.                        * won't be.  We make sure they're
  7624.                        * filled with -1, so they can be
  7625.                        * recognized as unassigned.
  7626.                        */
  7627.                       if (last_l < reg)
  7628.                     while (++last_l < reg)
  7629.                       lparen[last_l] = -1;
  7630.                     }
  7631.                   break;
  7632.                 }
  7633.                 
  7634.                   case OF(re_se_rparen,se_rparen):
  7635.                 {
  7636.                   int urhere =
  7637.                     ((int)(tst_pos - tst_str_half)
  7638.                      + ((tst_half == 0) ? 0 : size1));
  7639.                   int reg = rxb->se_params [effect].op1;
  7640.                   rparen[reg] = urhere + 1;
  7641.                   if (last_r < reg)
  7642.                     {
  7643.                       while (++last_r < reg)
  7644.                     rparen[last_r] = -1;
  7645.                     }
  7646.                   break;
  7647.                 }
  7648.                 
  7649.                   case OF(re_se_backref,se_backref):
  7650.                 {
  7651.                   int reg = rxb->se_params [effect].op1;
  7652.                   if (reg > last_r || rparen[reg] < 0)
  7653.                     goto test_do_return;
  7654.                   {
  7655.                     /* fixme */
  7656.                     const unsigned char * there
  7657.                       = tst_str_half + lparen[reg];
  7658.                     const unsigned char * last
  7659.                       = tst_str_half + rparen[reg];
  7660.                     const unsigned char * here = tst_pos + 1;
  7661.  
  7662.                     if ((here == tst_end_half) && string2
  7663.                     && (tst_str_half
  7664.                         == (unsigned char *) string1)
  7665.                     && (tst_end_half != abs_end))
  7666.                       {
  7667.                     here = (unsigned char *)string2;
  7668.                     tst_end_half = abs_end;
  7669.                       }
  7670.                     
  7671.                     while (there < last && here < tst_end_half)    /* 4% */
  7672.                       if (TRANSLATE(*there) /* &&&& 6% */
  7673.                       != TRANSLATE(*here))
  7674.                     goto test_do_return;
  7675.                       else
  7676.                     {
  7677.                       ++there; ++here;
  7678.                       if ((here == tst_end_half) && string2
  7679.                           && (tst_str_half
  7680.                           == (unsigned char *)string1)
  7681.                           && (tst_end_half != abs_end))
  7682.                         {
  7683.                           here = (unsigned char *)string2;
  7684.                           tst_end_half = abs_end;
  7685.                           tst_half = 1;
  7686.                         }
  7687.                     }
  7688.                     if (there != last)
  7689.                       goto test_do_return;
  7690.                     tst_pos = here - 1;
  7691.                     if ((here == (unsigned char *)string2)
  7692.                     && (unsigned char *)string1)
  7693.                       {
  7694.                     tst_pos = ((unsigned char *)string1
  7695.                            + size1 - 1);
  7696.                     tst_end_half = tst_pos + 1;
  7697.                     tst_half = 0;
  7698.                       }
  7699.                   }
  7700.                   break;
  7701.                 }
  7702.                   case OF(re_se_iter,se_iter):
  7703.                 {
  7704.                   struct counter_frame * csp
  7705.                     = ((struct counter_frame *)
  7706.                        counter_stack->sp);
  7707.                   if (csp->val == rxb->se_params[effect].op2)
  7708.                     goto test_do_return;
  7709.                   else
  7710.                     ++csp->val;
  7711.                   break;
  7712.                 }
  7713.                   case OF(re_se_end_iter,se_end_iter):
  7714.                 {
  7715.                   struct counter_frame * csp
  7716.                     = ((struct counter_frame *)
  7717.                        counter_stack->sp);
  7718.                   if (csp->val < rxb->se_params[effect].op1)
  7719.                     goto test_do_return;
  7720.                   else
  7721.                     {
  7722.                       struct counter_frame * source = csp;
  7723.                       while (source->inherited_from)
  7724.                     source = source->inherited_from;
  7725.                       if (!source || !source->cdr)
  7726.                     {
  7727.                       POP(counter_stack,
  7728.                           sizeof(struct counter_frame));
  7729.                     }
  7730.                       else
  7731.                     {
  7732.                       source = source->cdr;
  7733.                       csp->val = source->val;
  7734.                       csp->tag = source->tag;
  7735.                       csp->cdr = 0;
  7736.                       csp->inherited_from = source;
  7737.                     }
  7738.                     }
  7739.                   break;
  7740.                 }
  7741.                   case OF(re_se_tv, se_tv):
  7742.                 /* is a noop */
  7743.                 break;
  7744.                   case re_se_try:
  7745.                   case re_se_pushback:
  7746.                   case re_se_push0:
  7747.                   case re_se_pushpos:
  7748.                   case re_se_chkpos:
  7749.                   case re_se_poppos:
  7750.                   case re_se_at_dot:
  7751.                   case re_se_syntax:
  7752.                   case re_se_not_syntax:
  7753.                   case re_se_begbuf:
  7754.                   case re_se_hat:
  7755.                   case re_se_wordbeg:
  7756.                   case re_se_wordbound:
  7757.                   case re_se_notwordbound:
  7758.                   case re_se_wordend:
  7759.                   case re_se_endbuf:
  7760.                   case re_se_dollar:
  7761.                   case re_se_fail:
  7762.                   case re_floogle_flap:
  7763.                 ret_val = 0;
  7764.                 goto test_do_return;
  7765.                   }
  7766.               }
  7767.             el = el->cdr;
  7768.               }
  7769.             /* Now the side effects are done,
  7770.              * so get the next instruction.
  7771.              * and move on.
  7772.              */
  7773.             ifr = &df->future_frame;
  7774.             goto restart;
  7775.           }
  7776.           
  7777.         case OF(rx_backtrack_point,backtrack_point):
  7778.           {
  7779.             /* A backtrack point indicates that we've reached a
  7780.              * non-determinism in the superstate NFA.  This is a
  7781.              * loop that exhaustively searches the possibilities.
  7782.              *
  7783.              * A backtracking strategy is used.  We keep track of what
  7784.              * registers are valid so we can erase side effects.
  7785.              *
  7786.              * First, make sure there is some stack space to hold 
  7787.              * our state.
  7788.              */
  7789.  
  7790.             struct backtrack_frame * bf;
  7791.  
  7792.             PUSH(backtrack_stack, backtrack_frame_bytes);
  7793. #ifdef RX_DEBUG
  7794.             ++backtrack_depth;
  7795. #endif
  7796.  
  7797.             bf = ((struct backtrack_frame *)
  7798.               backtrack_stack->sp);
  7799.             {
  7800.               bf->stk_super = super;
  7801.               /* We prevent the current superstate from being
  7802.                * deleted from the superstate cache.
  7803.                */
  7804.               rx_lock_superstate (&rxb->rx, super);
  7805.               bf->stk_tst_pos = tst_pos;
  7806. #ifdef RX_DEBUG
  7807.               bf->stk_line_no = line_no;
  7808. #endif
  7809.               bf->stk_tst_half = tst_half;
  7810.               bf->stk_c = c;
  7811.               bf->stk_tst_str_half = tst_str_half;
  7812.               bf->stk_tst_end_half = tst_end_half;
  7813.               bf->stk_last_l = last_l;
  7814.               bf->stk_last_r = last_r;
  7815.               bf->df = ((struct rx_super_edge *)ifr->data_2)->options;
  7816.               bf->first_df = bf->df;
  7817.               bf->counter_stack_sp = (counter_stack
  7818.                           ? counter_stack->sp
  7819.                           : 0);
  7820.               bf->stk_test_ret = test_ret;
  7821.               if (rxb->match_regs_on_stack)
  7822.             {
  7823.               int x;
  7824.               regoff_t * stk =
  7825.                 (regoff_t *)((char *)bf + sizeof (*bf));
  7826.               for (x = 0; x <= last_l; ++x)
  7827.                 stk[x] = lparen[x];
  7828.               stk += x;
  7829.               for (x = 0; x <= last_r; ++x)
  7830.                 stk[x] = rparen[x];
  7831.             }
  7832.  
  7833.             }
  7834.  
  7835.             /* Here is a while loop whose body is mainly a function
  7836.              * call and some code to handle a return from that
  7837.              * function.
  7838.              *
  7839.              * From here on for the rest of `case backtrack_point' it
  7840.              * is unsafe to assume that the variables saved on the
  7841.              * stack are valid -- so reread their values from the stack
  7842.              * as needed.
  7843.              *
  7844.              * This lets us re-use one generation fewer stack saves in
  7845.              * the call-graph of a search.
  7846.              */
  7847.             
  7848.           while_non_det_options:
  7849. #ifdef RX_DEBUG
  7850.             ++lines_found;
  7851.             if (rx_debug_trace)
  7852.               fprintf (stderr, "@@@ %d calls %d @@@\n",
  7853.                    line_no, lines_found);
  7854.             
  7855.             line_no = lines_found;
  7856. #endif
  7857.             
  7858.             if (bf->df->next_same_super_edge[0] == bf->first_df)
  7859.               {
  7860.             /* This is a tail-call optimization -- we don't recurse
  7861.              * for the last of the possible futures.
  7862.              */
  7863.             ifr = (bf->df->effects
  7864.                    ? &bf->df->side_effects_frame
  7865.                    : &bf->df->future_frame);
  7866.  
  7867.             rx_unlock_superstate (&rxb->rx, super);
  7868.             POP(backtrack_stack, backtrack_frame_bytes);
  7869. #ifdef RX_DEBUG
  7870.             --backtrack_depth;
  7871. #endif
  7872.             goto restart;
  7873.               }
  7874.             else
  7875.               {
  7876.             if (counter_stack)
  7877.               {
  7878.                 struct counter_frame * old_cf
  7879.                   = ((struct counter_frame *)counter_stack->sp);
  7880.                 struct counter_frame * cf;
  7881.                 PUSH(counter_stack, sizeof (struct counter_frame));
  7882.                 cf = ((struct counter_frame *)counter_stack->sp);
  7883.                 cf->tag = old_cf->tag;
  7884.                 cf->val = old_cf->val;
  7885.                 cf->inherited_from = old_cf;
  7886.                 cf->cdr = 0;
  7887.               }            
  7888.             /* `Call' this test-match block */
  7889.             ifr = (bf->df->effects
  7890.                    ? &bf->df->side_effects_frame
  7891.                    : &bf->df->future_frame);
  7892.             goto recurse_test_match;
  7893.               }
  7894.  
  7895.             /* Returns in this block are accomplished by
  7896.              * goto test_do_return.  There are two cases.
  7897.              * If there is some search-stack left,
  7898.              * then it is a return from a `recursive' call.
  7899.              * If there is no search-stack left, then
  7900.              * we should return to the fastmap/search loop.
  7901.              */
  7902.             
  7903.           test_do_return:
  7904.  
  7905.             if (!backtrack_stack)
  7906.               {
  7907. #ifdef RX_DEBUG
  7908.             if (rx_debug_trace)
  7909.               fprintf (stderr, "!!! %d bails returning %d !!!\n",
  7910.                    line_no, test_ret);
  7911. #endif
  7912.  
  7913.             /* No more search-stack -- this test is done. */
  7914.             if (test_ret)
  7915.               goto return_from_test_match;
  7916.             else
  7917.               goto error_in_testing_match;
  7918.               }
  7919.  
  7920.             /* Ok..we're returning from a recursive call to 
  7921.              * the test match block:
  7922.              */
  7923.             
  7924.             bf = ((struct backtrack_frame *)
  7925.               backtrack_stack->sp);
  7926. #ifdef RX_DEBUG
  7927.             if (rx_debug_trace)
  7928.               fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
  7929.                    line_no, test_ret, bf->stk_line_no);
  7930. #endif
  7931.  
  7932.             while (counter_stack
  7933.                && (!bf->counter_stack_sp
  7934.                    || (bf->counter_stack_sp != counter_stack->sp)))
  7935.               {
  7936.             POP(counter_stack, sizeof (struct counter_frame));
  7937.               }
  7938.  
  7939.             if (!test_ret)
  7940.               {
  7941.             POP (backtrack_stack, backtrack_frame_bytes);
  7942.             goto test_do_return;
  7943.               }
  7944.  
  7945.             /* If any possible future reaches the end of the 
  7946.              * string without failing, make sure we propogate 
  7947.              * that information to the caller.
  7948.              */
  7949.             if ((test_ret == -2) && first_found)
  7950.               {
  7951.             rx_unlock_superstate (&rxb->rx, bf->stk_super);
  7952.             POP (backtrack_stack, backtrack_frame_bytes);
  7953.             goto test_do_return;
  7954.               }
  7955.  
  7956.             if (bf->stk_test_ret < 0)
  7957.               test_ret = bf->stk_test_ret;
  7958.  
  7959.             last_l = bf->stk_last_l;
  7960.             last_r = bf->stk_last_r;
  7961.             bf->df = bf->df->next_same_super_edge[0];
  7962.             super = bf->stk_super;
  7963.             tst_pos = bf->stk_tst_pos;
  7964.             tst_half = bf->stk_tst_half;
  7965.             c = bf->stk_c;
  7966.             tst_str_half = bf->stk_tst_str_half;
  7967.             tst_end_half = bf->stk_tst_end_half;
  7968. #ifdef RX_DEBUG
  7969.             line_no = bf->stk_line_no;
  7970. #endif
  7971.  
  7972.             if (rxb->match_regs_on_stack)
  7973.               {
  7974.             int x;
  7975.             regoff_t * stk =
  7976.               (regoff_t *)((char *)bf + sizeof (*bf));
  7977.             for (x = 0; x <= last_l; ++x)
  7978.               lparen[x] = stk[x];
  7979.             stk += x;
  7980.             for (x = 0; x <= last_r; ++x)
  7981.               rparen[x] = stk[x];
  7982.               }
  7983.  
  7984.             goto while_non_det_options;
  7985.           }
  7986.  
  7987.           
  7988.         case OF(rx_cache_miss,cache_miss):
  7989.           /* Because the superstate NFA is lazily constructed,
  7990.            * and in fact may erode from underneath us, we sometimes
  7991.            * have to construct the next instruction from the hard way.
  7992.            * This invokes one step in the lazy-conversion.
  7993.            */
  7994.           ifr = rx_handle_cache_miss (&rxb->rx, super, c, ifr->data_2);
  7995.           if (!ifr)
  7996.             {
  7997.               test_ret = 0;
  7998.               goto test_do_return;
  7999.             }
  8000.           goto restart;
  8001.           
  8002.         case OF(rx_backtrack,backtrack):
  8003.           /* RX_BACKTRACK means that we've reached the empty
  8004.            * superstate, indicating that match can't succeed
  8005.            * from this point.
  8006.            */
  8007.           goto test_do_return;
  8008.         case rx_next_char:
  8009.         case rx_error_inx:
  8010.         case rx_num_instructions:
  8011.           ret_val = 0;
  8012.           goto test_do_return;
  8013.         }
  8014.         }
  8015.     }
  8016.  
  8017.  
  8018.     /* Healthy exists from the test-match loop do a 
  8019.      * `goto return_from_test_match'   On the other hand, 
  8020.      * we might end up here.
  8021.      */
  8022.       error_in_testing_match:
  8023.     ret_val = -2;
  8024.     goto finish;
  8025.  
  8026.  
  8027.     /***** fastmap/search loop body
  8028.      *          considering the results testing for a match
  8029.      */
  8030.  
  8031.       return_from_test_match:
  8032.  
  8033.     if (best_last_l >= 0)
  8034.       {
  8035.         if (regs && (regs->start != best_lparen))
  8036.           {
  8037.         bcopy (best_lparen, regs->start,
  8038.                regs->num_regs * sizeof (int));
  8039.         bcopy (best_rparen, regs->end,
  8040.                regs->num_regs * sizeof (int));
  8041.           }
  8042.         if (regs && !rxb->no_sub)
  8043.           {
  8044.         int q;
  8045.         int bound = (regs->num_regs > num_regs
  8046.                  ? regs->num_regs
  8047.                  : num_regs);
  8048.         regoff_t * s = regs->start;
  8049.         regoff_t * e = regs->end;
  8050.         for (q = best_last_l + 1;  q < bound; ++q)
  8051.           s[q] = e[q] = -1;
  8052.           }
  8053.         ret_val = best_lparen[0];
  8054.         goto finish;
  8055.       }
  8056.  
  8057.     /***** fastmap/search loop,  increment and loop-test */
  8058.  
  8059.     pos += search_direction;
  8060.     startpos += search_direction;
  8061.  
  8062.       } while (startpos < search_end);
  8063.  
  8064.  
  8065.   /**** Exit code for fastmap/searchloop and the entire re_search_2 fn. */
  8066.  
  8067.   finish:
  8068.     /* Unset the fastmap sentinel */
  8069.     if (fastmap_chr >= 0)
  8070.       fastmap[fastmap_chr] = fastmap_val;
  8071.  
  8072.     if (start_super)
  8073.       rx_unlock_superstate (&rxb->rx, start_super);
  8074.  
  8075. #ifdef REGEX_MALLOC
  8076.     if (lparen) free (lparen);
  8077.     if (rparen) free (rparen);
  8078.     if (best_lpspace) free (best_lpspace);
  8079.     if (best_rpspace) free (best_rpspace);
  8080. #endif
  8081.     return ret_val;
  8082.   }
  8083. }
  8084.  
  8085. #if !defined(REGEX_MALLOC) && !defined(__GNUC__)
  8086. #ifdef __STDC__
  8087. int
  8088. re_search_2 (struct re_pattern_buffer *rxb,
  8089.          const char * string1, int size1,
  8090.          const char * string2, int size2,
  8091.          int startpos, int range,
  8092.          struct re_registers *regs,
  8093.          int stop)
  8094. #else
  8095. int
  8096. re_search_2 (rxb, string1, size1, string2, size2, startpos, range, regs, stop)
  8097.      struct re_pattern_buffer *rxb;
  8098.      const char * string1;
  8099.      int size1;
  8100.      const char * string2;
  8101.      int size2;
  8102.      int startpos;
  8103.      int range;
  8104.      struct re_registers *regs;
  8105.      int stop;
  8106. #endif
  8107. {
  8108.   int ret;
  8109.   ret = inner_re_search_2 (rxb, string1, size1, string2, size2, startpos,
  8110.                range, regs, stop);
  8111.   alloca (0);
  8112.   return ret;
  8113. }
  8114. #endif
  8115.  
  8116.  
  8117. /* Like re_search_2, above, but only one string is specified, and
  8118.  * doesn't let you say where to stop matching.
  8119.  */
  8120.  
  8121. #ifdef __STDC__
  8122. int
  8123. re_search (struct re_pattern_buffer * rxb, const char *string,
  8124.        int size, int startpos, int range,
  8125.        struct re_registers *regs)
  8126. #else
  8127. int
  8128. re_search (rxb, string, size, startpos, range, regs)
  8129.      struct re_pattern_buffer * rxb;
  8130.      const char * string;
  8131.      int size;
  8132.      int startpos;
  8133.      int range;
  8134.      struct re_registers *regs;
  8135. #endif
  8136. {
  8137.   return re_search_2 (rxb, 0, 0, string, size, startpos, range, regs, size);
  8138. }
  8139.  
  8140. #ifdef __STDC__
  8141. int
  8142. re_match_2 (struct re_pattern_buffer * rxb,
  8143.         const char * string1, int size1,
  8144.         const char * string2, int size2,
  8145.         int pos, struct re_registers *regs, int stop)
  8146. #else
  8147. int
  8148. re_match_2 (rxb, string1, size1, string2, size2, pos, regs, stop)
  8149.      struct re_pattern_buffer * rxb;
  8150.      const char * string1;
  8151.      int size1;
  8152.      const char * string2;
  8153.      int size2;
  8154.      int pos;
  8155.      struct re_registers *regs;
  8156.      int stop;
  8157. #endif
  8158. {
  8159.   struct re_registers some_regs;
  8160.   regoff_t start;
  8161.   regoff_t end;
  8162.   int srch;
  8163.   int save = rxb->regs_allocated;
  8164.   struct re_registers * regs_to_pass = regs;
  8165.  
  8166.   if (!regs)
  8167.     {
  8168.       some_regs.start = &start;
  8169.       some_regs.end = &end;
  8170.       some_regs.num_regs = 1;
  8171.       regs_to_pass = &some_regs;
  8172.       rxb->regs_allocated = REGS_FIXED;
  8173.     }
  8174.  
  8175.   srch = re_search_2 (rxb, string1, size1, string2, size2,
  8176.               pos, 1, regs_to_pass, stop);
  8177.   if (regs_to_pass != regs)
  8178.     rxb->regs_allocated = save;
  8179.   if (srch < 0)
  8180.     return srch;
  8181.   return regs_to_pass->end[0] - regs_to_pass->start[0];
  8182. }
  8183.  
  8184. /* re_match is like re_match_2 except it takes only a single string.  */
  8185.  
  8186. #ifdef __STDC__
  8187. int
  8188. re_match (struct re_pattern_buffer * rxb,
  8189.       const char * string,
  8190.       int size, int pos,
  8191.       struct re_registers *regs)
  8192. #else
  8193. int
  8194. re_match (rxb, string, size, pos, regs)
  8195.      struct re_pattern_buffer * rxb;
  8196.      const char *string;
  8197.      int size;
  8198.      int pos;
  8199.      struct re_registers *regs;
  8200. #endif
  8201. {
  8202.   return re_match_2 (rxb, string, size, 0, 0, pos, regs, size);
  8203. }
  8204.  
  8205.  
  8206.  
  8207. /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
  8208.    also be assigned to arbitrarily: each pattern buffer stores its own
  8209.    syntax, so it can be changed between regex compilations.  */
  8210. reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
  8211.  
  8212.  
  8213. /* Specify the precise syntax of regexps for compilation.  This provides
  8214.    for compatibility for various utilities which historically have
  8215.    different, incompatible syntaxes.
  8216.  
  8217.    The argument SYNTAX is a bit mask comprised of the various bits
  8218.    defined in regex.h.  We return the old syntax.  */
  8219.  
  8220. #ifdef __STDC__
  8221. reg_syntax_t
  8222. re_set_syntax (reg_syntax_t syntax)
  8223. #else
  8224. reg_syntax_t
  8225. re_set_syntax (syntax)
  8226.     reg_syntax_t syntax;
  8227. #endif
  8228. {
  8229.   reg_syntax_t ret = re_syntax_options;
  8230.  
  8231.   re_syntax_options = syntax;
  8232.   return ret;
  8233. }
  8234.  
  8235.  
  8236. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  8237.    ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
  8238.    this memory for recording register information.  STARTS and ENDS
  8239.    must be allocated using the malloc library routine, and must each
  8240.    be at least NUM_REGS * sizeof (regoff_t) bytes long.
  8241.  
  8242.    If NUM_REGS == 0, then subsequent matches should allocate their own
  8243.    register data.
  8244.  
  8245.    Unless this function is called, the first search or match using
  8246.    PATTERN_BUFFER will allocate its own register data, without
  8247.    freeing the old data.  */
  8248.  
  8249. #ifdef __STDC__
  8250. void
  8251. re_set_registers (struct re_pattern_buffer *bufp,
  8252.           struct re_registers *regs,
  8253.           unsigned num_regs,
  8254.           regoff_t * starts, regoff_t * ends)
  8255. #else
  8256. void
  8257. re_set_registers (bufp, regs, num_regs, starts, ends)
  8258.      struct re_pattern_buffer *bufp;
  8259.      struct re_registers *regs;
  8260.      unsigned num_regs;
  8261.      regoff_t * starts;
  8262.      regoff_t * ends;
  8263. #endif
  8264. {
  8265.   if (num_regs)
  8266.     {
  8267.       bufp->regs_allocated = REGS_REALLOCATE;
  8268.       regs->num_regs = num_regs;
  8269.       regs->start = starts;
  8270.       regs->end = ends;
  8271.     }
  8272.   else
  8273.     {
  8274.       bufp->regs_allocated = REGS_UNALLOCATED;
  8275.       regs->num_regs = 0;
  8276.       regs->start = regs->end = (regoff_t) 0;
  8277.     }
  8278. }
  8279.  
  8280.  
  8281.  
  8282.  
  8283. #ifdef __STDC__
  8284. static int 
  8285. cplx_se_sublist_len (struct rx_se_list * list)
  8286. #else
  8287. static int 
  8288. cplx_se_sublist_len (list)
  8289.      struct rx_se_list * list;
  8290. #endif
  8291. {
  8292.   int x = 0;
  8293.   while (list)
  8294.     {
  8295.       if ((int)list->car >= 0)
  8296.     ++x;
  8297.       list = list->cdr;
  8298.     }
  8299.   return x;
  8300. }
  8301.  
  8302.  
  8303. /* For rx->se_list_cmp */
  8304.  
  8305. #ifdef __STDC__
  8306. static int 
  8307. posix_se_list_order (struct rx * rx,
  8308.              struct rx_se_list * a, struct rx_se_list * b)
  8309. #else
  8310. static int 
  8311. posix_se_list_order (rx, a, b)
  8312.      struct rx * rx;
  8313.      struct rx_se_list * a;
  8314.      struct rx_se_list * b;
  8315. #endif
  8316. {
  8317.   int al = cplx_se_sublist_len (a);
  8318.   int bl = cplx_se_sublist_len (b);
  8319.  
  8320.   if (!al && !bl)
  8321.     return ((a == b)
  8322.         ? 0
  8323.         : ((a < b) ? -1 : 1));
  8324.   
  8325.   else if (!al)
  8326.     return -1;
  8327.  
  8328.   else if (!bl)
  8329.     return 1;
  8330.  
  8331.   else
  8332.     {
  8333.       rx_side_effect * av = ((rx_side_effect *)
  8334.                  alloca (sizeof (rx_side_effect) * (al + 1)));
  8335.       rx_side_effect * bv = ((rx_side_effect *)
  8336.                  alloca (sizeof (rx_side_effect) * (bl + 1)));
  8337.       struct rx_se_list * ap = a;
  8338.       struct rx_se_list * bp = b;
  8339.       int ai, bi;
  8340.       
  8341.       for (ai = al - 1; ai >= 0; --ai)
  8342.     {
  8343.       while ((int)ap->car < 0)
  8344.         ap = ap->cdr;
  8345.       av[ai] = ap->car;
  8346.       ap = ap->cdr;
  8347.     }
  8348.       av[al] = (rx_side_effect)-2;
  8349.       for (bi = bl - 1; bi >= 0; --bi)
  8350.     {
  8351.       while ((int)bp->car < 0)
  8352.         bp = bp->cdr;
  8353.       bv[bi] = bp->car;
  8354.       bp = bp->cdr;
  8355.     }
  8356.       bv[bl] = (rx_side_effect)-1;
  8357.  
  8358.       {
  8359.     int ret;
  8360.     int x = 0;
  8361.     while (av[x] == bv[x])
  8362.       ++x;
  8363.     ret = ((av[x] < bv[x]) ? -1 : 1);
  8364.     return ret;
  8365.       }
  8366.     }
  8367. }
  8368.  
  8369.  
  8370.  
  8371.  
  8372. /* re_compile_pattern is the GNU regular expression compiler: it
  8373.    compiles PATTERN (of length SIZE) and puts the result in RXB.
  8374.    Returns 0 if the pattern was valid, otherwise an error string.
  8375.  
  8376.    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  8377.    are set in RXB on entry.
  8378.  
  8379.    We call rx_compile to do the actual compilation.  */
  8380.  
  8381. #ifdef __STDC__
  8382. const char *
  8383. re_compile_pattern (const char *pattern,
  8384.             int length,
  8385.             struct re_pattern_buffer * rxb)
  8386. #else
  8387. const char *
  8388. re_compile_pattern (pattern, length, rxb)
  8389.      const char *pattern;
  8390.      int length;
  8391.      struct re_pattern_buffer * rxb;
  8392. #endif
  8393. {
  8394.   reg_errcode_t ret;
  8395.  
  8396.   /* GNU code is written to assume at least RE_NREGS registers will be set
  8397.      (and at least one extra will be -1).  */
  8398.   rxb->regs_allocated = REGS_UNALLOCATED;
  8399.  
  8400.   /* And GNU code determines whether or not to get register information
  8401.      by passing null for the REGS argument to re_match, etc., not by
  8402.      setting no_sub.  */
  8403.   rxb->no_sub = 0;
  8404.  
  8405.   rxb->rx.local_cset_size = 256;
  8406.  
  8407.   /* Match anchors at newline.  */
  8408.   rxb->newline_anchor = 1;
  8409.  
  8410.   rxb->re_nsub = 0;
  8411.   rxb->start = 0;
  8412.   rxb->se_params = 0;
  8413.   rxb->rx.nodec = 0;
  8414.   rxb->rx.epsnodec = 0;
  8415.   rxb->rx.instruction_table = 0;
  8416.   rxb->rx.nfa_states = 0;
  8417.   rxb->rx.se_list_cmp = posix_se_list_order;
  8418.   rxb->rx.start_set = 0;
  8419.  
  8420.   ret = rx_compile (pattern, length, re_syntax_options, rxb);
  8421.   alloca (0);
  8422.   return rx_error_msg[(int) ret];
  8423. }
  8424.  
  8425.  
  8426.  
  8427. #ifdef __STDC__
  8428. int
  8429. re_compile_fastmap (struct re_pattern_buffer * rxb)
  8430. #else
  8431. int
  8432. re_compile_fastmap (rxb)
  8433.      struct re_pattern_buffer * rxb;
  8434. #endif
  8435. {
  8436.   rx_blow_up_fastmap (rxb);
  8437.   return 0;
  8438. }
  8439.  
  8440.  
  8441.  
  8442.  
  8443. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  8444.    them if this is an Emacs or POSIX compilation.  */
  8445.  
  8446. #if !defined (emacs) && !defined (_POSIX_SOURCE) && 0
  8447.  
  8448. /* BSD has one and only one pattern buffer.  */
  8449. static struct re_pattern_buffer rx_comp_buf;
  8450.  
  8451. #ifdef __STDC__
  8452. char *
  8453. re_comp (const char *s)
  8454. #else
  8455. char *
  8456. re_comp (s)
  8457.     const char *s;
  8458. #endif
  8459. {
  8460.   reg_errcode_t ret;
  8461.  
  8462.   if (!s)
  8463.     {
  8464.       if (!rx_comp_buf.buffer)
  8465.     return "No previous regular expression";
  8466.       return 0;
  8467.     }
  8468.  
  8469.   if (!rx_comp_buf.fastmap)
  8470.     {
  8471.       rx_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  8472.       if (!rx_comp_buf.fastmap)
  8473.     return "Memory exhausted";
  8474.     }
  8475.  
  8476.   /* Since `rx_exec' always passes NULL for the `regs' argument, we
  8477.      don't need to initialize the pattern buffer fields which affect it.  */
  8478.  
  8479.   /* Match anchors at newlines.  */
  8480.   rx_comp_buf.newline_anchor = 1;
  8481.  
  8482.   rx_comp_buf.re_nsub = 0;
  8483.   rx_comp_buf.start = 0;
  8484.   rx_comp_buf.se_params = 0;
  8485.   rx_comp_buf.rx.nodec = 0;
  8486.   rx_comp_buf.rx.epsnodec = 0;
  8487.   rx_comp_buf.rx.instruction_table = 0;
  8488.   rx_comp_buf.rx.nfa_states = 0;
  8489.   rx_comp_buf.rx.start = 0;
  8490.   rx_comp_buf.rx.se_list_cmp = posix_se_list_order;
  8491.  
  8492.   ret = rx_compile (s, strlen (s), rx_syntax_options, &rx_comp_buf);
  8493.   alloca (0);
  8494.  
  8495.   /* Yes, we're discarding `const' here.  */
  8496.   return (char *) rx_error_msg[(int) ret];
  8497. }
  8498.  
  8499.  
  8500. #ifdef __STDC__
  8501. int
  8502. rx_exec (const char *s)
  8503. #else
  8504. int
  8505. rx_exec (s)
  8506.     const char *s;
  8507. #endif
  8508. {
  8509.   const int len = strlen (s);
  8510.   return
  8511.     0 <= re_search (&rx_comp_buf, s, len, 0, len, (struct rx_registers *) 0);
  8512. }
  8513. #endif /* not emacs and not _POSIX_SOURCE */
  8514.  
  8515.  
  8516.  
  8517. /* POSIX.2 functions.  Don't define these for Emacs.  */
  8518.  
  8519. #if !defined(emacs)
  8520.  
  8521. /* regcomp takes a regular expression as a string and compiles it.
  8522.  
  8523.    PREG is a regex_t *.  We do not expect any fields to be initialized,
  8524.    since POSIX says we shouldn't.  Thus, we set
  8525.  
  8526.      `buffer' to the compiled pattern;
  8527.      `used' to the length of the compiled pattern;
  8528.      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  8529.        REG_EXTENDED bit in CFLAGS is set; otherwise, to
  8530.        RE_SYNTAX_POSIX_BASIC;
  8531.      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  8532.      `fastmap' and `fastmap_accurate' to zero;
  8533.      `re_nsub' to the number of subexpressions in PATTERN.
  8534.  
  8535.    PATTERN is the address of the pattern string.
  8536.  
  8537.    CFLAGS is a series of bits which affect compilation.
  8538.  
  8539.      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  8540.      use POSIX basic syntax.
  8541.  
  8542.      If REG_NEWLINE is set, then . and [^...] don't match newline.
  8543.      Also, regexec will try a match beginning after every newline.
  8544.  
  8545.      If REG_ICASE is set, then we considers upper- and lowercase
  8546.      versions of letters to be equivalent when matching.
  8547.  
  8548.      If REG_NOSUB is set, then when PREG is passed to regexec, that
  8549.      routine will report only success or failure, and nothing about the
  8550.      registers.
  8551.  
  8552.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  8553.    the return codes and their meanings.)  */
  8554.  
  8555.  
  8556. #ifdef __STDC__
  8557. int
  8558. regcomp (regex_t * preg, const char * pattern, int cflags)
  8559. #else
  8560. int
  8561. regcomp (preg, pattern, cflags)
  8562.     regex_t * preg;
  8563.     const char * pattern;
  8564.     int cflags;
  8565. #endif
  8566. {
  8567.   reg_errcode_t ret;
  8568.   unsigned syntax
  8569.     = cflags & REG_EXTENDED ? RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  8570.  
  8571.   /* regex_compile will allocate the space for the compiled pattern.  */
  8572.   preg->buffer = 0;
  8573.   preg->allocated = 0;
  8574.  
  8575.   preg->fastmap = malloc (256);
  8576.   if (!preg->fastmap)
  8577.     return REG_ESPACE;
  8578.   preg->fastmap_accurate = 0;
  8579.  
  8580.   if (cflags & REG_ICASE)
  8581.     {
  8582.       unsigned i;
  8583.  
  8584.       preg->translate = (char *) malloc (256);
  8585.       if (!preg->translate)
  8586.         return (int) REG_ESPACE;
  8587.  
  8588.       /* Map uppercase characters to corresponding lowercase ones.  */
  8589.       for (i = 0; i < CHAR_SET_SIZE; i++)
  8590.         preg->translate[i] = isupper (i) ? tolower (i) : i;
  8591.     }
  8592.   else
  8593.     preg->translate = 0;
  8594.  
  8595.   /* If REG_NEWLINE is set, newlines are treated differently.  */
  8596.   if (cflags & REG_NEWLINE)
  8597.     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
  8598.       syntax &= ~RE_DOT_NEWLINE;
  8599.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  8600.       /* It also changes the matching behavior.  */
  8601.       preg->newline_anchor = 1;
  8602.     }
  8603.   else
  8604.     preg->newline_anchor = 0;
  8605.  
  8606.   preg->no_sub = !!(cflags & REG_NOSUB);
  8607.  
  8608.   /* POSIX says a null character in the pattern terminates it, so we
  8609.      can use strlen here in compiling the pattern.  */
  8610.   preg->re_nsub = 0;
  8611.   preg->start = 0;
  8612.   preg->se_params = 0;
  8613.   preg->rx.nodec = 0;
  8614.   preg->rx.epsnodec = 0;
  8615.   preg->rx.instruction_table = 0;
  8616.   preg->rx.nfa_states = 0;
  8617.   preg->rx.local_cset_size = 256;
  8618.   preg->rx.start = 0;
  8619.   preg->rx.se_list_cmp = posix_se_list_order;
  8620.   preg->rx.start_set = 0;
  8621.   ret = rx_compile (pattern, strlen (pattern), syntax, preg);
  8622.   alloca (0);
  8623.  
  8624.   /* POSIX doesn't distinguish between an unmatched open-group and an
  8625.      unmatched close-group: both are REG_EPAREN.  */
  8626.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  8627.  
  8628.   return (int) ret;
  8629. }
  8630.  
  8631.  
  8632. /* regexec searches for a given pattern, specified by PREG, in the
  8633.    string STRING.
  8634.  
  8635.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  8636.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  8637.    least NMATCH elements, and we set them to the offsets of the
  8638.    corresponding matched substrings.
  8639.  
  8640.    EFLAGS specifies `execution flags' which affect matching: if
  8641.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  8642.    string; if REG_NOTEOL is set, then $ does not match at the end.
  8643.  
  8644.    We return 0 if we find a match and REG_NOMATCH if not.  */
  8645.  
  8646. #ifdef __STDC__
  8647. int
  8648. regexec (const regex_t *preg, const char *string,
  8649.      size_t nmatch, regmatch_t pmatch[],
  8650.      int eflags)
  8651. #else
  8652. int
  8653. regexec (preg, string, nmatch, pmatch, eflags)
  8654.     const regex_t *preg;
  8655.     const char *string;
  8656.     size_t nmatch;
  8657.     regmatch_t pmatch[];
  8658.     int eflags;
  8659. #endif
  8660. {
  8661.   int ret;
  8662.   struct re_registers regs;
  8663.   regex_t private_preg;
  8664.   int len = strlen (string);
  8665.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  8666.  
  8667.   private_preg = *preg;
  8668.  
  8669.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  8670.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  8671.  
  8672.   /* The user has told us exactly how many registers to return
  8673.    * information about, via `nmatch'.  We have to pass that on to the
  8674.    * matching routines.
  8675.    */
  8676.   private_preg.regs_allocated = REGS_FIXED;
  8677.  
  8678.   if (want_reg_info)
  8679.     {
  8680.       regs.num_regs = nmatch;
  8681.       regs.start = TALLOC (nmatch, regoff_t);
  8682.       regs.end = TALLOC (nmatch, regoff_t);
  8683.       if (regs.start == 0 || regs.end == 0)
  8684.         return (int) REG_NOMATCH;
  8685.     }
  8686.  
  8687.   /* Perform the searching operation.  */
  8688.   ret = re_search (&private_preg,
  8689.            string, len,
  8690.                    /* start: */ 0,
  8691.            /* range: */ len,
  8692.                    want_reg_info ? ®s : (struct re_registers *) 0);
  8693.  
  8694.   /* Copy the register information to the POSIX structure.  */
  8695.   if (want_reg_info)
  8696.     {
  8697.       if (ret >= 0)
  8698.         {
  8699.           unsigned r;
  8700.  
  8701.           for (r = 0; r < nmatch; r++)
  8702.             {
  8703.               pmatch[r].rm_so = regs.start[r];
  8704.               pmatch[r].rm_eo = regs.end[r];
  8705.             }
  8706.         }
  8707.  
  8708.       /* If we needed the temporary register info, free the space now.  */
  8709.       free (regs.start);
  8710.       free (regs.end);
  8711.     }
  8712.  
  8713.   /* We want zero return to mean success, unlike `re_search'.  */
  8714.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  8715. }
  8716.  
  8717.  
  8718. /* Returns a message corresponding to an error code, ERRCODE, returned
  8719.    from either regcomp or regexec.   */
  8720.  
  8721. #ifdef __STDC__
  8722. size_t
  8723. regerror (int errcode, const regex_t *preg,
  8724.       char *errbuf, size_t errbuf_size)
  8725. #else
  8726. size_t
  8727. regerror (errcode, preg, errbuf, errbuf_size)
  8728.     int errcode;
  8729.     const regex_t *preg;
  8730.     char *errbuf;
  8731.     size_t errbuf_size;
  8732. #endif
  8733. {
  8734.   const char *msg
  8735.     = rx_error_msg[errcode] == 0 ? "Success" : rx_error_msg[errcode];
  8736.   size_t msg_size = strlen (msg) + 1; /* Includes the 0.  */
  8737.  
  8738.   if (errbuf_size != 0)
  8739.     {
  8740.       if (msg_size > errbuf_size)
  8741.         {
  8742.           strncpy (errbuf, msg, errbuf_size - 1);
  8743.           errbuf[errbuf_size - 1] = 0;
  8744.         }
  8745.       else
  8746.         strcpy (errbuf, msg);
  8747.     }
  8748.  
  8749.   return msg_size;
  8750. }
  8751.  
  8752.  
  8753. /* Free dynamically allocated space used by PREG.  */
  8754.  
  8755. #ifdef __STDC__
  8756. void
  8757. regfree (regex_t *preg)
  8758. #else
  8759. void
  8760. regfree (preg)
  8761.     regex_t *preg;
  8762. #endif
  8763. {
  8764.   if (preg->buffer != 0)
  8765.     free (preg->buffer);
  8766.   preg->buffer = 0;
  8767.   preg->allocated = 0;
  8768.  
  8769.   if (preg->fastmap != 0)
  8770.     free (preg->fastmap);
  8771.   preg->fastmap = 0;
  8772.   preg->fastmap_accurate = 0;
  8773.  
  8774.   if (preg->translate != 0)
  8775.     free (preg->translate);
  8776.   preg->translate = 0;
  8777. }
  8778.  
  8779. #endif /* not emacs  */
  8780.  
  8781.  
  8782.  
  8783.  
  8784.  
  8785.