home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / lib / g++-include / rx.h < prev    next >
C/C++ Source or Header  |  1994-12-22  |  116KB  |  3,721 lines

  1. #if !defined(RXH) || defined(RX_WANT_SE_DEFS)
  2. #define RXH
  3.  
  4. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  5.  
  6. This file is part of the librx library.
  7.  
  8. Librx is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU Library General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. Librx is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with this software; see the file COPYING.LIB.  If not,
  20. write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA
  21. 02139, USA.  */
  22. /*  t. lord    Wed Sep 23 18:20:57 1992    */
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. #ifndef RX_WANT_SE_DEFS
  32.  
  33. /* This page: Bitsets */
  34.  
  35. #ifndef RX_subset
  36. typedef unsigned int RX_subset;
  37. #define RX_subset_bits    (32)
  38. #define RX_subset_mask    (RX_subset_bits - 1)
  39. #endif
  40.  
  41. typedef RX_subset * rx_Bitset;
  42.  
  43. #ifdef __STDC__
  44. typedef void (*rx_bitset_iterator) (void *, int member_index);
  45. #else
  46. typedef void (*rx_bitset_iterator) ();
  47. #endif
  48.  
  49. #define rx_bitset_subset(N)  ((N) / RX_subset_bits)
  50. #define rx_bitset_subset_val(B,N)  ((B)[rx_bitset_subset(N)])
  51. #define RX_bitset_access(B,N,OP) \
  52.   ((B)[rx_bitset_subset(N)] OP rx_subset_singletons[(N) & RX_subset_mask])
  53. #define RX_bitset_member(B,N)   RX_bitset_access(B, N, &)
  54. #define RX_bitset_enjoin(B,N)   RX_bitset_access(B, N, |=)
  55. #define RX_bitset_remove(B,N)   RX_bitset_access(B, N, &= ~)
  56. #define RX_bitset_toggle(B,N)   RX_bitset_access(B, N, ^= )
  57. #define rx_bitset_numb_subsets(N) (((N) + RX_subset_bits - 1) / RX_subset_bits)
  58. #define rx_sizeof_bitset(N)    (rx_bitset_numb_subsets(N) * sizeof(RX_subset))
  59.  
  60.  
  61.  
  62. /* This page: Splay trees. */
  63.  
  64. #ifdef __STDC__
  65. typedef int (*rx_sp_comparer) (void * a, void * b);
  66. #else
  67. typedef int (*rx_sp_comparer) ();
  68. #endif
  69.  
  70. struct rx_sp_node 
  71. {
  72.   void * key;
  73.   void * data;
  74.   struct rx_sp_node * kids[2];
  75. };
  76.  
  77. #ifdef __STDC__
  78. typedef void (*rx_sp_key_data_freer) (struct rx_sp_node *);
  79. #else
  80. typedef void (*rx_sp_key_data_freer) ();
  81. #endif
  82.  
  83.  
  84. /* giant inflatable hash trees */
  85.  
  86. struct rx_hash_item
  87. {
  88.   struct rx_hash_item * next_same_hash;
  89.   struct rx_hash * table;
  90.   unsigned long hash;
  91.   void * data;
  92.   void * binding;
  93. };
  94.  
  95. struct rx_hash
  96. {
  97.   struct rx_hash * parent;
  98.   int refs;
  99.   struct rx_hash * children[13];
  100.   struct rx_hash_item * buckets [13];
  101.   int bucket_size [13];
  102. };
  103.  
  104. struct rx_hash_rules;
  105.  
  106. #ifdef __STDC__
  107. /* should return like == */
  108. typedef int (*rx_hash_eq)(void *, void *);
  109. typedef struct rx_hash * (*rx_alloc_hash)(struct rx_hash_rules *);
  110. typedef void (*rx_free_hash)(struct rx_hash *,
  111.                 struct rx_hash_rules *);
  112. typedef struct rx_hash_item * (*rx_alloc_hash_item)(struct rx_hash_rules *,
  113.                             void *);
  114. typedef void (*rx_free_hash_item)(struct rx_hash_item *,
  115.                  struct rx_hash_rules *);
  116. #else
  117. typedef int (*rx_hash_eq)();
  118. typedef struct rx_hash * (*rx_alloc_hash)();
  119. typedef void (*rx_free_hash)();
  120. typedef struct rx_hash_item * (*rx_alloc_hash_item)();
  121. typedef void (*rx_free_hash_item)();
  122. #endif
  123.  
  124. struct rx_hash_rules
  125. {
  126.   rx_hash_eq eq;
  127.   rx_alloc_hash hash_alloc;
  128.   rx_free_hash free_hash;
  129.   rx_alloc_hash_item hash_item_alloc;
  130.   rx_free_hash_item free_hash_item;
  131. };
  132.  
  133.  
  134. /* Forward declarations */
  135.  
  136. struct rx_cache;
  137. struct rx_superset;
  138. struct rx;
  139. struct rx_se_list;
  140.  
  141.  
  142.  
  143. /* 
  144.  * GLOSSARY
  145.  *
  146.  * regexp
  147.  * regular expression
  148.  * expression
  149.  * pattern - a `regular' expression.  The expression
  150.  *       need not be formally regular -- it can contain
  151.  *       constructs that don't correspond to purely regular
  152.  *       expressions.
  153.  *
  154.  * buffer
  155.  * string - the string (or strings) being searched or matched.
  156.  *
  157.  * pattern buffer - a structure of type `struct re_pattern_buffer'
  158.  *       This in turn contains a `struct rx', which holds the
  159.  *       NFA compiled from a pattern, as well as some of the state
  160.  *       of a matcher using the pattern.
  161.  *
  162.  * NFA - nondeterministic finite automata.  Some people
  163.  *       use this term to a member of the class of 
  164.  *       regular automata (those corresponding to a regular
  165.  *       language).  However, in this code, the meaning is
  166.  *       more general.  The automata used by Rx are comperable
  167.  *       in power to what are usually called `push down automata'.
  168.  *
  169.  *       Two NFA are built by rx for every pattern.  One is built
  170.  *       by the compiler.  The other is built from the first, on
  171.  *       the fly, by the matcher.  The latter is called the `superstate
  172.  *       NFA' because its states correspond to sets of states from
  173.  *       the first NFA.  (Joe Keane gets credit for the name
  174.  *       `superstate NFA').
  175.  *
  176.  * NFA edges
  177.  * epsilon edges
  178.  * side-effect edges - The NFA compiled from a pattern can have three
  179.  *       kinds of edges.  Epsilon edges can be taken freely anytime
  180.  *       their source state is reached.  Character set edges can be
  181.  *       taken when their source state is reached and when the next 
  182.  *       character in the buffer is a member of the set.  Side effect
  183.  *       edges imply a transition that can only be taken after the
  184.  *       indicated side effect has been successfully accomplished.
  185.  *       Some examples of side effects are:
  186.  *
  187.  *        Storing the current match position to record the
  188.  *              location of a parentesized subexpression.
  189.  *
  190.  *              Advancing the matcher over N characters if they
  191.  *              match the N characters previously matched by a 
  192.  *              parentesized subexpression.
  193.  *
  194.  *       Both of those kinds of edges occur in the NFA generated
  195.  *       by the pattern:  \(.\)\1
  196.  *
  197.  *       Epsilon and side effect edges are similar.  Unfortunately,
  198.  *       some of the code uses the name `epsilon edge' to mean
  199.  *       both epsilon and side effect edges.  For example,  the
  200.  *       function has_non_idempotent_epsilon_path computes the existance
  201.  *       of a non-trivial path containing only a mix of epsilon and
  202.  *       side effect edges.  In that case `nonidempotent epsilon' is being
  203.  *       used to mean `side effect'.
  204.  */
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /* LOW LEVEL PATTERN BUFFERS */
  211.  
  212. /* Suppose that from some NFA state, more than one path through
  213.  * side-effect edges is possible.  In what order should the paths
  214.  * be tried?  A function of type rx_se_list_order answers that
  215.  * question.  It compares two lists of side effects, and says
  216.  * which list comes first.
  217.  */
  218.  
  219. #ifdef __STDC__
  220. typedef int (*rx_se_list_order) (struct rx *,
  221.                  struct rx_se_list *, 
  222.                  struct rx_se_list *);
  223. #else
  224. typedef int (*rx_se_list_order) ();
  225. #endif
  226.  
  227.  
  228.  
  229. /* Struct RX holds a compiled regular expression - that is, an nfa
  230.  * ready to be converted on demand to a more efficient superstate nfa.
  231.  * This is for the low level interface.  The high-level interfaces enclose
  232.  * this in a `struct re_pattern_buffer'.  
  233.  */
  234. struct rx
  235. {
  236.   /* The compiler assigns a unique id to every pattern.
  237.    * Like sequence numbers in X, there is a subtle bug here
  238.    * if you use Rx in a system that runs for a long time.
  239.    * But, because of the way the caches work out, it is almost
  240.    * impossible to trigger the Rx version of this bug.
  241.    *
  242.    * The id is used to validate superstates found in a cache
  243.    * of superstates.  It isn't sufficient to let a superstate
  244.    * point back to the rx for which it was compiled -- the caller
  245.    * may be re-using a `struct rx' in which case the superstate
  246.    * is not really valid.  So instead, superstates are validated
  247.    * by checking the sequence number of the pattern for which
  248.    * they were built.
  249.    */
  250.   int rx_id;
  251.  
  252.   /* This is memory mgt. state for superstates.  This may be 
  253.    * shared by more than one struct rx.
  254.    */
  255.   struct rx_cache * cache;
  256.  
  257.   /* Every regex defines the size of its own character set. 
  258.    * A superstate has an array of this size, with each element
  259.    * a `struct rx_inx'.  So, don't make this number too large.
  260.    * In particular, don't make it 2^16.
  261.    */
  262.   int local_cset_size;
  263.  
  264.   /* After the NFA is built, it is copied into a contiguous region
  265.    * of memory (mostly for compatability with GNU regex).
  266.    * Here is that region, and it's size:
  267.    */
  268.   void * buffer;
  269.   unsigned long allocated;
  270.  
  271.   /* Clients of RX can ask for some extra storage in the space pointed
  272.    * to by BUFFER.  The field RESERVED is an input parameter to the
  273.    * compiler.  After compilation, this much space will be available 
  274.    * at (buffer + allocated - reserved)
  275.    */
  276.   unsigned long reserved;
  277.  
  278.   /* --------- The remaining fields are for internal use only. --------- */
  279.   /* --------- But! they must be initialized to 0.           --------- */
  280.  
  281.   /* NODEC is the number of nodes in the NFA with non-epsilon
  282.    * transitions. 
  283.    */
  284.   int nodec;
  285.  
  286.   /* EPSNODEC is the number of nodes with only epsilon transitions. */
  287.   int epsnodec;
  288.  
  289.   /* The sum (NODEC + EPSNODEC) is the total number of states in the
  290.    * compiled NFA.
  291.    */
  292.  
  293.   /* Lists of side effects as stored in the NFA are `hash consed'..meaning
  294.    * that lists with the same elements are ==.  During compilation, 
  295.    * this table facilitates hash-consing.
  296.    */
  297.   struct rx_hash se_list_memo;
  298.  
  299.   /* Lists of NFA states are also hashed. 
  300.    */
  301.   struct rx_hash set_list_memo;
  302.  
  303.  
  304.  
  305.  
  306.   /* The compiler and matcher must build a number of instruction frames.
  307.    * The format of these frames is fixed (c.f. struct rx_inx).  The values
  308.    * of the instructions is not fixed.
  309.    *
  310.    * An enumerated type (enum rx_opcode) defines the set of instructions
  311.    * that the compiler or matcher might generate.  When filling an instruction
  312.    * frame, the INX field is found by indexing this instruction table
  313.    * with an opcode:
  314.    */
  315.   void ** instruction_table;
  316.  
  317.   /* The list of all states in an NFA.
  318.    * During compilation, the NEXT field of NFA states links this list.
  319.    * After compilation, all the states are compacted into an array,
  320.    * ordered by state id numbers.  At that time, this points to the base 
  321.    * of that array.
  322.    */
  323.   struct rx_nfa_state *nfa_states;
  324.  
  325.   /* Every nfa begins with one distinguished starting state:
  326.    */
  327.   struct rx_nfa_state *start;
  328.  
  329.   /* This orders the search through super-nfa paths.
  330.    * See the comment near the typedef of rx_se_list_order.
  331.    */
  332.   rx_se_list_order se_list_cmp;
  333.  
  334.   struct rx_superset * start_set;
  335. };
  336.  
  337.  
  338.  
  339.  
  340. /* SYNTAX TREES */
  341.  
  342. /* Compilation is in stages.  
  343.  *
  344.  * In the first stage, a pattern specified by a string is 
  345.  * translated into a syntax tree.  Later stages will convert
  346.  * the syntax tree into an NFA optimized for conversion to a
  347.  * superstate-NFA.
  348.  *
  349.  * This page is about syntax trees.
  350.  */
  351.  
  352. enum rexp_node_type
  353. {
  354.   r_cset,            /* Match from a character set. `a' or `[a-z]'*/
  355.   r_concat,            /* Concat two subexpressions.   `ab' */
  356.   r_alternate,            /* Choose one of two subexpressions. `a\|b' */
  357.   r_opt,            /* Optional subexpression. `a?' */
  358.   r_star,            /* Repeated subexpression. `a*' */
  359.  
  360.  
  361.   /* A 2phase-star is a variation on a repeated subexpression.
  362.    * In this case, there are two subexpressions.  The first, if matched,
  363.    * begins a repitition (otherwise, the whole expression is matches the
  364.    * empth string).  
  365.    * 
  366.    * After matching the first subexpression, a 2phase star either finishes,
  367.    * or matches the second subexpression.  If the second subexpression is
  368.    * matched, then the whole construct repeats.
  369.    *
  370.    * 2phase stars are used in two circumstances.  First, they
  371.    * are used as part of the implementation of POSIX intervals (counted
  372.    * repititions).  Second, they are used to implement proper star
  373.    * semantics when the repeated subexpression contains paths of
  374.    * only side effects.  See rx_compile for more information.
  375.    */
  376.   r_2phase_star,
  377.  
  378.  
  379.   /* c.f. "typedef void * rx_side_effect" */
  380.   r_side_effect,
  381.  
  382.   /* This is an extension type:  It is for transient use in source->source
  383.    * transformations (implemented over syntax trees).
  384.    */
  385.   r_data
  386. };
  387.  
  388. /* A side effect is a matcher-specific action associated with
  389.  * transitions in the NFA.  The details of side effects are up
  390.  * to the matcher.  To the compiler and superstate constructors
  391.  * side effects are opaque:
  392.  */
  393.  
  394. typedef void * rx_side_effect;
  395.  
  396. /* Nodes in a syntax tree are of this type:
  397.  */
  398. struct rexp_node
  399. {
  400.   enum rexp_node_type type;
  401.   union
  402.   {
  403.     rx_Bitset cset;
  404.     rx_side_effect side_effect;
  405.     struct
  406.       {
  407.     struct rexp_node *left;
  408.     struct rexp_node *right;
  409.       } pair;
  410.     void * data;
  411.   } params;
  412. };
  413.  
  414.  
  415.  
  416. /* NFA
  417.  *
  418.  * A syntax tree is compiled into an NFA.  This page defines the structure
  419.  * of that NFA.
  420.  */
  421.  
  422. struct rx_nfa_state
  423. {
  424.   /* These are kept in a list as the NFA is being built. */
  425.   struct rx_nfa_state *next;
  426.  
  427.   /* After the NFA is built, states are given integer id's.
  428.    * States whose outgoing transitions are all either epsilon or 
  429.    * side effect edges are given ids less than 0.  Other states
  430.    * are given successive non-negative ids starting from 0.
  431.    */
  432.   int id;
  433.  
  434.   /* The list of NFA edges that go from this state to some other. */
  435.   struct rx_nfa_edge *edges;
  436.  
  437.   /* If you land in this state, then you implicitly land
  438.    * in all other states reachable by only epsilon translations.
  439.    * Call the set of maximal paths to such states the epsilon closure
  440.    * of this state.
  441.    *
  442.    * There may be other states that are reachable by a mixture of
  443.    * epsilon and side effect edges.  Consider the set of maximal paths
  444.    * of that sort from this state.  Call it the epsilon-side-effect
  445.    * closure of the state.
  446.    * 
  447.    * The epsilon closure of the state is a subset of the epsilon-side-
  448.    * effect closure.  It consists of all the paths that contain 
  449.    * no side effects -- only epsilon edges.
  450.    * 
  451.    * The paths in the epsilon-side-effect closure  can be partitioned
  452.    * into equivalance sets. Two paths are equivalant if they have the
  453.    * same set of side effects, in the same order.  The epsilon-closure
  454.    * is one of these equivalance sets.  Let's call these equivalance
  455.    * sets: observably equivalant path sets.  That name is chosen
  456.    * because equivalance of two paths means they cause the same side
  457.    * effects -- so they lead to the same subsequent observations other
  458.    * than that they may wind up in different target states.
  459.    *
  460.    * The superstate nfa, which is derived from this nfa, is based on
  461.    * the observation that all of the paths in an observably equivalant
  462.    * path set can be explored at the same time, provided that the
  463.    * matcher keeps track not of a single nfa state, but of a set of
  464.    * states.   In particular, after following all the paths in an
  465.    * observably equivalant set, you wind up at a set of target states.
  466.    * That set of target states corresponds to one state in the
  467.    * superstate NFA.
  468.    *
  469.    * Staticly, before matching begins, it is convenient to analyze the
  470.    * nfa.  Each state is labeled with a list of the observably
  471.    * equivalant path sets who's union covers all the
  472.    * epsilon-side-effect paths beginning in this state.  This list is
  473.    * called the possible futures of the state.
  474.    *
  475.    * A trivial example is this NFA:
  476.    *             s1
  477.    *         A  --->  B
  478.    *
  479.    *             s2  
  480.    *            --->  C
  481.    *
  482.    *             epsilon           s1
  483.    *            --------->  D   ------> E
  484.    * 
  485.    * 
  486.    * In this example, A has two possible futures.
  487.    * One invokes the side effect `s1' and contains two paths,
  488.    * one ending in state B, the other in state E.
  489.    * The other invokes the side effect `s2' and contains only
  490.    * one path, landing in state C.
  491.    */
  492.   struct rx_possible_future *futures;
  493.  
  494.  
  495.   /* There are exactly two distinguished states in every NFA: */
  496.   unsigned int is_final:1;
  497.   unsigned int is_start:1;
  498.  
  499.   /* These are used during NFA construction... */
  500.   unsigned int eclosure_needed:1;
  501.   unsigned int mark:1;
  502. };
  503.  
  504.  
  505. /* An edge in an NFA is typed: */
  506. enum rx_nfa_etype
  507. {
  508.   /* A cset edge is labled with a set of characters one of which
  509.    * must be matched for the edge to be taken.
  510.    */
  511.   ne_cset,
  512.  
  513.   /* An epsilon edge is taken whenever its starting state is
  514.    * reached. 
  515.    */
  516.   ne_epsilon,
  517.  
  518.   /* A side effect edge is taken whenever its starting state is
  519.    * reached.  Side effects may cause the match to fail or the
  520.    * position of the matcher to advance.
  521.    */
  522.   ne_side_effect        /* A special kind of epsilon. */
  523. };
  524.  
  525. struct rx_nfa_edge
  526. {
  527.   struct rx_nfa_edge *next;
  528.   enum rx_nfa_etype type;
  529.   struct rx_nfa_state *dest;
  530.   union
  531.   {
  532.     rx_Bitset cset;
  533.     rx_side_effect side_effect;
  534.   } params;
  535. };
  536.  
  537.  
  538.  
  539. /* A possible future consists of a list of side effects
  540.  * and a set of destination states.  Below are their
  541.  * representations.  These structures are hash-consed which
  542.  * means that lists with the same elements share a representation
  543.  * (their addresses are ==).
  544.  */
  545.  
  546. struct rx_nfa_state_set
  547. {
  548.   struct rx_nfa_state * car;
  549.   struct rx_nfa_state_set * cdr;
  550. };
  551.  
  552. struct rx_se_list
  553. {
  554.   rx_side_effect car;
  555.   struct rx_se_list * cdr;
  556. };
  557.  
  558. struct rx_possible_future
  559. {
  560.   struct rx_possible_future *next;
  561.   struct rx_se_list * effects;
  562.   struct rx_nfa_state_set * destset;
  563. };
  564.  
  565.  
  566.  
  567. /* This begins the description of the superstate NFA.
  568.  *
  569.  * The superstate NFA corresponds to the NFA in these ways:
  570.  *
  571.  * Every superstate NFA states SUPER correspond to sets of NFA states,
  572.  * nfa_states(SUPER).
  573.  *
  574.  * Superstate edges correspond to NFA paths.
  575.  *
  576.  * The superstate has no epsilon transitions;
  577.  * every edge has a character label, and a (possibly empty) side
  578.  * effect label.   The side effect label corresponds to a list of
  579.  * side effects that occur in the NFA.  These parts are referred
  580.  * to as:   superedge_character(EDGE) and superedge_sides(EDGE).
  581.  *
  582.  * For a superstate edge EDGE starting in some superstate SUPER,
  583.  * the following is true (in pseudo-notation :-):
  584.  *
  585.  *       exists DEST in nfa_states s.t. 
  586.  *         exists nfaEDGE in nfa_edges s.t.
  587.  *                 origin (nfaEDGE) == DEST
  588.  *              && origin (nfaEDGE) is a member of nfa_states(SUPER)
  589.  *              && exists PF in possible_futures(dest(nfaEDGE)) s.t.
  590.  *                     sides_of_possible_future (PF) == superedge_sides (EDGE)
  591.  *
  592.  * also:
  593.  *
  594.  *      let SUPER2 := superedge_destination(EDGE)
  595.  *          nfa_states(SUPER2)
  596.  *           == union of all nfa state sets S s.t.
  597.  *                          exists PF in possible_futures(dest(nfaEDGE)) s.t.
  598.  *                            sides_of_possible_future (PF) == superedge_sides (EDGE)
  599.  *                          && S == dests_of_possible_future (PF) }
  600.  *
  601.  * Or in english, every superstate is a set of nfa states.  A given
  602.  * character and a superstate implies many transitions in the NFA --
  603.  * those that begin with an edge labeled with that character from a
  604.  * state in the set corresponding to the superstate.
  605.  * 
  606.  * The destinations of those transitions each have a set of possible
  607.  * futures.  A possible future is a list of side effects and a set of
  608.  * destination NFA states.  Two sets of possible futures can be
  609.  * `merged' by combining all pairs of possible futures that have the
  610.  * same side effects.  A pair is combined by creating a new future
  611.  * with the same side effect but the union of the two destination sets.
  612.  * In this way, all the possible futures suggested by a superstate
  613.  * and a character can be merged into a set of possible futures where
  614.  * no two elements of the set have the same set of side effects.
  615.  *
  616.  * The destination of a possible future, being a set of NFA states, 
  617.  * corresponds to a supernfa state.  So, the merged set of possible
  618.  * futures we just created can serve as a set of edges in the
  619.  * supernfa.
  620.  *
  621.  * The representation of the superstate nfa and the nfa is critical.
  622.  * The nfa has to be compact, but has to facilitate the rapid
  623.  * computation of missing superstates.  The superstate nfa has to 
  624.  * be fast to interpret, lazilly constructed, and bounded in space.
  625.  *
  626.  * To facilitate interpretation, the superstate data structures are 
  627.  * peppered with `instruction frames'.  There is an instruction set
  628.  * defined below which matchers using the supernfa must be able to
  629.  * interpret.
  630.  *
  631.  * We'd like to make it possible but not mandatory to use code
  632.  * addresses to represent instructions (c.f. gcc's computed goto).
  633.  * Therefore, we define an enumerated type of opcodes, and when
  634.  * writing one of these instructions into a data structure, use
  635.  * the opcode as an index into a table of instruction values.
  636.  * 
  637.  * Here are the opcodes that occur in the superstate nfa:
  638.  */
  639.  
  640.  
  641. /* Every superstate contains a table of instruction frames indexed 
  642.  * by characters.  A normal `move' in a matcher is to fetch the next
  643.  * character and use it as an index into a superstates transition
  644.  * table.
  645.  *
  646.  * In the fasted case, only one edge follows from that character.
  647.  * In other cases there is more work to do.
  648.  * 
  649.  * The descriptions of the opcodes refer to data structures that are
  650.  * described further below. 
  651.  */
  652.  
  653. enum rx_opcode
  654. {
  655.   /* 
  656.    * BACKTRACK_POINT is invoked when a character transition in 
  657.    * a superstate leads to more than one edge.  In that case,
  658.    * the edges have to be explored independently using a backtracking
  659.    * strategy.
  660.    *
  661.    * A BACKTRACK_POINT instruction is stored in a superstate's 
  662.    * transition table for some character when it is known that that
  663.    * character crosses more than one edge.  On encountering this
  664.    * instruction, the matcher saves enough state to backtrack to this
  665.    * point in the match later.
  666.    */
  667.   rx_backtrack_point = 0,    /* data is (struct transition_class *) */
  668.  
  669.   /* 
  670.    * RX_DO_SIDE_EFFECTS evaluates the side effects of an epsilon path.
  671.    * There is one occurence of this instruction per rx_distinct_future.
  672.    * This instruction is skipped if a rx_distinct_future has no side effects.
  673.    */
  674.   rx_do_side_effects = rx_backtrack_point + 1,
  675.  
  676.   /* data is (struct rx_distinct_future *) */
  677.  
  678.   /* 
  679.    * RX_CACHE_MISS instructions are stored in rx_distinct_futures whose
  680.    * destination superstate has been reclaimed (or was never built).
  681.    * It recomputes the destination superstate.
  682.    * RX_CACHE_MISS is also stored in a superstate transition table before
  683.    * any of its edges have been built.
  684.    */
  685.   rx_cache_miss = rx_do_side_effects + 1,
  686.   /* data is (struct rx_distinct_future *) */
  687.  
  688.   /* 
  689.    * RX_NEXT_CHAR is called to consume the next character and take the
  690.    * corresponding transition.  This is the only instruction that uses 
  691.    * the DATA field of the instruction frame instead of DATA_2.
  692.    * (see EXPLORE_FUTURE in regex.c).
  693.    */
  694.   rx_next_char = rx_cache_miss + 1, /* data is (struct superstate *) */
  695.  
  696.   /* RX_BACKTRACK indicates that a transition fails.
  697.    */
  698.   rx_backtrack = rx_next_char + 1, /* no data */
  699.  
  700.   /* 
  701.    * RX_ERROR_INX is stored only in places that should never be executed.
  702.    */
  703.   rx_error_inx = rx_backtrack + 1, /* Not supposed to occur. */
  704.  
  705.   rx_num_instructions = rx_error_inx + 1
  706. };
  707.  
  708. /* An id_instruction_table holds the values stored in instruction
  709.  * frames.  The table is indexed by the enums declared above.
  710.  */
  711. extern void * rx_id_instruction_table[rx_num_instructions];
  712.  
  713. /* The heart of the matcher is a `word-code-interpreter' 
  714.  * (like a byte-code interpreter, except that instructions
  715.  * are a full word wide).
  716.  *
  717.  * Instructions are not stored in a vector of code, instead,
  718.  * they are scattered throughout the data structures built
  719.  * by the regexp compiler and the matcher.  One word-code instruction,
  720.  * together with the arguments to that instruction, constitute
  721.  * an instruction frame (struct rx_inx).
  722.  *
  723.  * This structure type is padded by hand to a power of 2 because
  724.  * in one of the dominant cases, we dispatch by indexing a table
  725.  * of instruction frames.  If that indexing can be accomplished
  726.  * by just a shift of the index, we're happy.
  727.  *
  728.  * Instructions take at most one argument, but there are two
  729.  * slots in an instruction frame that might hold that argument.
  730.  * These are called data and data_2.  The data slot is only
  731.  * used for one instruction (RX_NEXT_CHAR).  For all other 
  732.  * instructions, data should be set to 0.
  733.  *
  734.  * RX_NEXT_CHAR is the most important instruction by far.
  735.  * By reserving the data field for its exclusive use, 
  736.  * instruction dispatch is sped up in that case.  There is
  737.  * no need to fetch both the instruction and the data,
  738.  * only the data is needed.  In other words, a `cycle' begins
  739.  * by fetching the field data.  If that is non-0, then it must
  740.  * be the destination state of a next_char transition, so
  741.  * make that value the current state, advance the match position
  742.  * by one character, and start a new cycle.  On the other hand,
  743.  * if data is 0, fetch the instruction and do a more complicated
  744.  * dispatch on that.
  745.  */
  746.  
  747. struct rx_inx 
  748. {
  749.   void * data;
  750.   void * data_2;
  751.   void * inx;
  752.   void * fnord;
  753. };
  754.  
  755. #ifndef RX_TAIL_ARRAY
  756. #define RX_TAIL_ARRAY  1
  757. #endif
  758.  
  759. /* A superstate corresponds to a set of nfa states.  Those sets are
  760.  * represented by STRUCT RX_SUPERSET.  The constructors
  761.  * guarantee that only one (shared) structure is created for a given set.
  762.  */
  763. struct rx_superset
  764. {
  765.   int refs;            /* This is a reference counted structure. */
  766.  
  767.   /* We keep these sets in a cache because (in an unpredictable way),
  768.    * the same set is often created again and again.  But that is also
  769.    * problematic -- compatibility with POSIX and GNU regex requires
  770.    * that we not be able to tell when a program discards a particular
  771.    * NFA (thus invalidating the supersets created from it).
  772.    *
  773.    * But when a cache hit appears to occur, we will have in hand the
  774.    * nfa for which it may have happened.  That is why every nfa is given
  775.    * its own sequence number.  On a cache hit, the cache is validated
  776.    * by comparing the nfa sequence number to this field:
  777.    */
  778.   int id;
  779.  
  780.   struct rx_nfa_state * car;    /* May or may not be a valid addr. */
  781.   struct rx_superset * cdr;
  782.  
  783.   /* If the corresponding superstate exists: */
  784.   struct rx_superstate * superstate;
  785.  
  786.  
  787.   /* There is another bookkeeping problem.  It is expensive to 
  788.    * compute the starting nfa state set for an nfa.  So, once computed,
  789.    * it is cached in the `struct rx'.
  790.    *
  791.    * But, the state set can be flushed from the superstate cache.
  792.    * When that happens, we can't know if the corresponding `struct rx'
  793.    * is still alive or if it has been freed or re-used by the program.
  794.    * So, the cached pointer to this set in a struct rx might be invalid
  795.    * and we need a way to validate it.
  796.    *
  797.    * Fortunately, even if this set is flushed from the cache, it is
  798.    * not freed.  It just goes on the free-list of supersets.
  799.    * So we can still examine it.  
  800.    *
  801.    * So to validate a starting set memo, check to see if the
  802.    * starts_for field still points back to the struct rx in question,
  803.    * and if the ID matches the rx sequence number.
  804.    */
  805.   struct rx * starts_for;
  806.  
  807.   /* This is used to link into a hash bucket so these objects can
  808.    * be `hash-consed'.
  809.    */
  810.   struct rx_hash_item hash_item;
  811. };
  812.  
  813. #define rx_protect_superset(RX,CON) (++(CON)->refs)
  814.  
  815. /* The terminology may be confusing (rename this structure?).
  816.  * Every character occurs in at most one rx_super_edge per super-state.
  817.  * But, that structure might have more than one option, indicating a point
  818.  * of non-determinism. 
  819.  *
  820.  * In other words, this structure holds a list of superstate edges
  821.  * sharing a common starting state and character label.  The edges
  822.  * are in the field OPTIONS.  All superstate edges sharing the same
  823.  * starting state and character are in this list.
  824.  */
  825. struct rx_super_edge
  826. {
  827.   struct rx_super_edge *next;
  828.   struct rx_inx rx_backtrack_frame;
  829.   int cset_size;
  830.   rx_Bitset cset;
  831.   struct rx_distinct_future *options;
  832. };
  833.  
  834. /* A superstate is a set of nfa states (RX_SUPERSET) along
  835.  * with a transition table.  Superstates are built on demand and reclaimed
  836.  * without warning.  To protect a superstate from this ghastly fate,
  837.  * use LOCK_SUPERSTATE. 
  838.  */
  839. struct rx_superstate
  840. {
  841.   int rx_id;            /* c.f. the id field of rx_superset */
  842.   int locks;            /* protection from reclamation */
  843.  
  844.   /* Within a superstate cache, all the superstates are kept in a big
  845.    * queue.  The tail of the queue is the state most likely to be
  846.    * reclaimed.  The *recyclable fields hold the queue position of 
  847.    * this state.
  848.    */
  849.   struct rx_superstate * next_recyclable;
  850.   struct rx_superstate * prev_recyclable;
  851.  
  852.   /* The supernfa edges that exist in the cache and that have
  853.    * this state as their destination are kept in this list:
  854.    */
  855.   struct rx_distinct_future * transition_refs;
  856.  
  857.   /* The list of nfa states corresponding to this superstate: */
  858.   struct rx_superset * contents;
  859.  
  860.   /* The list of edges in the cache beginning from this state. */
  861.   struct rx_super_edge * edges;
  862.  
  863.   /* A tail of the recyclable queue is marked as semifree.  A semifree
  864.    * state has no incoming next_char transitions -- any transition
  865.    * into a semifree state causes a complex dispatch with the side
  866.    * effect of rescuing the state from its semifree state.
  867.    *
  868.    * An alternative to this might be to make next_char more expensive,
  869.    * and to move a state to the head of the recyclable queue whenever
  870.    * it is entered.  That way, popular states would never be recycled.
  871.    *
  872.    * But unilaterally making next_char more expensive actually loses.
  873.    * So, incoming transitions are only made expensive for states near
  874.    * the tail of the recyclable queue.  The more cache contention
  875.    * there is, the more frequently a state will have to prove itself
  876.    * and be moved back to the front of the queue.  If there is less 
  877.    * contention, then popular states just aggregate in the front of 
  878.    * the queue and stay there.
  879.    */
  880.   int is_semifree;
  881.  
  882.  
  883.   /* This keeps track of the size of the transition table for this
  884.    * state.  There is a half-hearted attempt to support variable sized
  885.    * superstates.
  886.    */
  887.   int trans_size;
  888.  
  889.   /* Indexed by characters... */
  890.   struct rx_inx transitions[RX_TAIL_ARRAY];
  891. };
  892.  
  893.  
  894. /* A list of distinct futures define the edges that leave from a 
  895.  * given superstate on a given character.  c.f. rx_super_edge.
  896.  */
  897.  
  898. struct rx_distinct_future
  899. {
  900.   struct rx_distinct_future * next_same_super_edge[2];
  901.   struct rx_distinct_future * next_same_dest;
  902.   struct rx_distinct_future * prev_same_dest;
  903.   struct rx_superstate * present;    /* source state */
  904.   struct rx_superstate * future;    /* destination state */
  905.   struct rx_super_edge * edge;
  906.  
  907.  
  908.   /* The future_frame holds the instruction that should be executed
  909.    * after all the side effects are done, when it is time to complete
  910.    * the transition to the next state.
  911.    *
  912.    * Normally this is a next_char instruction, but it may be a
  913.    * cache_miss instruction as well, depending on whether or not
  914.    * the superstate is in the cache and semifree.
  915.    * 
  916.    * If this is the only future for a given superstate/char, and
  917.    * if there are no side effects to be performed, this frame is
  918.    * not used (directly) at all.  Instead, its contents are copied
  919.    * into the transition table of the starting state of this dist. future.
  920.    */
  921.   struct rx_inx future_frame;
  922.  
  923.   struct rx_inx side_effects_frame;
  924.   struct rx_se_list * effects;
  925. };
  926.  
  927. #define rx_lock_superstate(R,S)  ((S)->locks++)
  928. #define rx_unlock_superstate(R,S) (--(S)->locks)
  929.  
  930.  
  931. /* This page destined for rx.h */
  932.  
  933. struct rx_blocklist
  934. {
  935.   struct rx_blocklist * next;
  936.   int bytes;
  937. };
  938.  
  939. struct rx_freelist
  940. {
  941.   struct rx_freelist * next;
  942. };
  943.  
  944. struct rx_cache;
  945.  
  946. #ifdef __STDC__
  947. typedef void (*rx_morecore_fn)(struct rx_cache *);
  948. #else
  949. typedef void (*rx_morecore_fn)();
  950. #endif
  951.  
  952. /* You use this to control the allocation of superstate data 
  953.  * during matching.  Most of it should be initialized to 0.
  954.  *
  955.  * A MORECORE function is necessary.  It should allocate
  956.  * a new block of memory or return 0.
  957.  * A default that uses malloc is called `rx_morecore'.
  958.  *
  959.  * The number of SUPERSTATES_ALLOWED indirectly limits how much memory
  960.  * the system will try to allocate.  The default is 128.  Batch style
  961.  * applications that are very regexp intensive should use as high a number
  962.  * as possible without thrashing.
  963.  * 
  964.  * The LOCAL_CSET_SIZE is the number of characters in a character set.
  965.  * It is therefore the number of entries in a superstate transition table.
  966.  * Generally, it should be 256.  If your character set has 16 bits, 
  967.  * it is better to translate your regexps into equivalent 8 bit patterns.
  968.  */
  969.  
  970. struct rx_cache
  971. {
  972.   struct rx_hash_rules superset_hash_rules;
  973.  
  974.   /* Objects are allocated by incrementing a pointer that 
  975.    * scans across rx_blocklists.
  976.    */
  977.   struct rx_blocklist * memory;
  978.   struct rx_blocklist * memory_pos;
  979.   int bytes_left;
  980.   char * memory_addr;
  981.   rx_morecore_fn morecore;
  982.  
  983.   /* Freelists. */
  984.   struct rx_freelist * free_superstates;
  985.   struct rx_freelist * free_transition_classes;
  986.   struct rx_freelist * free_discernable_futures;
  987.   struct rx_freelist * free_supersets;
  988.   struct rx_freelist * free_hash;
  989.  
  990.   /* Two sets of superstates -- those that are semifreed, and those
  991.    * that are being used.
  992.    */
  993.   struct rx_superstate * lru_superstate;
  994.   struct rx_superstate * semifree_superstate;
  995.  
  996.   struct rx_superset * empty_superset;
  997.  
  998.   int superstates;
  999.   int semifree_superstates;
  1000.   int hits;
  1001.   int misses;
  1002.   int superstates_allowed;
  1003.  
  1004.   int local_cset_size;
  1005.   void ** instruction_table;
  1006.  
  1007.   struct rx_hash superset_table;
  1008. };
  1009.  
  1010.  
  1011.  
  1012. /* The lowest-level search function supports arbitrarily fragmented
  1013.  * strings and (optionally) suspendable/resumable searches.
  1014.  *
  1015.  * Callers have to provide a few hooks.
  1016.  */
  1017.  
  1018. #ifndef __GNUC__
  1019. #ifdef __STDC__
  1020. #define __const__ const
  1021. #else
  1022. #define __const__
  1023. #endif
  1024. #endif
  1025.  
  1026. /* This holds a matcher position */
  1027. struct rx_string_position
  1028. {
  1029.   __const__ unsigned char * pos;    /* The current pos. */
  1030.   __const__ unsigned char * string; /* The current string burst. */
  1031.   __const__ unsigned char * end;    /* First invalid position >= POS. */
  1032.   int offset;            /* Integer address of the current burst. */
  1033.   int size;            /* Current string's size. */
  1034.   int search_direction;        /* 1 or -1 */
  1035.   int search_end;        /* First position to not try. */
  1036. };
  1037.  
  1038.  
  1039. enum rx_get_burst_return
  1040. {
  1041.   rx_get_burst_continuation,
  1042.   rx_get_burst_error,
  1043.   rx_get_burst_ok,
  1044.   rx_get_burst_no_more
  1045. };
  1046.  
  1047.  
  1048. /* A call to get burst should make POS valid.  It might be invalid
  1049.  * if the STRING field doesn't point to a burst that actually
  1050.  * contains POS.
  1051.  *
  1052.  * GET_BURST should take a clue from SEARCH_DIRECTION (1 or -1) as to
  1053.  * whether or not to pad to the left.  Padding to the right is always
  1054.  * appropriate, but need not go past the point indicated by STOP.
  1055.  *
  1056.  * If a continuation is returned, then the reentering call to
  1057.  * a search function will retry the get_burst.
  1058.  */
  1059.  
  1060. #ifdef __STDC__
  1061. typedef enum rx_get_burst_return
  1062.   (*rx_get_burst_fn) (struct rx_string_position * pos,
  1063.               void * app_closure,
  1064.               int stop);
  1065.                            
  1066. #else
  1067. typedef enum rx_get_burst_return (*rx_get_burst_fn) ();
  1068. #endif
  1069.  
  1070.  
  1071. enum rx_back_check_return
  1072. {
  1073.   rx_back_check_continuation,
  1074.   rx_back_check_error,
  1075.   rx_back_check_pass,
  1076.   rx_back_check_fail
  1077. };
  1078.  
  1079. /* Back_check should advance the position it is passed 
  1080.  * over rparen - lparen characters and return pass iff
  1081.  * the characters starting at POS match those indexed
  1082.  * by [LPAREN..RPAREN].
  1083.  *
  1084.  * If a continuation is returned, then the reentering call to
  1085.  * a search function will retry the back_check.
  1086.  */
  1087.  
  1088. #ifdef __STDC__
  1089. typedef enum rx_back_check_return
  1090.   (*rx_back_check_fn) (struct rx_string_position * pos,
  1091.                int lparen,
  1092.                int rparen,
  1093.                unsigned char * translate,
  1094.                void * app_closure,
  1095.                int stop);
  1096.                            
  1097. #else
  1098. typedef enum rx_back_check_return (*rx_back_check_fn) ();
  1099. #endif
  1100.  
  1101.  
  1102.  
  1103.  
  1104. /* A call to fetch_char should return the character at POS or POS + 1.
  1105.  * Returning continuations here isn't supported.  OFFSET is either 0 or 1
  1106.  * and indicates which characters is desired.
  1107.  */
  1108.  
  1109. #ifdef __STDC__
  1110. typedef int (*rx_fetch_char_fn) (struct rx_string_position * pos,
  1111.                  int offset,
  1112.                  void * app_closure,
  1113.                  int stop);
  1114. #else
  1115. typedef int (*rx_fetch_char_fn) ();
  1116. #endif
  1117.  
  1118.  
  1119. enum rx_search_return
  1120. {
  1121.   rx_search_continuation = -4,
  1122.   rx_search_error = -3,
  1123.   rx_search_soft_fail = -2,    /* failed by running out of string */
  1124.   rx_search_fail = -1,        /* failed only by reaching failure states */
  1125.   /* return values >= 0 indicate the position of a successful match */
  1126. };
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133. /* regex.h
  1134.  * 
  1135.  * The remaining declarations replace regex.h.
  1136.  */
  1137.  
  1138. /* This is an array of error messages corresponding to the error codes.
  1139.  */
  1140. extern __const__ char *re_error_msg[];
  1141.  
  1142. /* If any error codes are removed, changed, or added, update the
  1143.    `re_error_msg' table in regex.c.  */
  1144. typedef enum
  1145. {
  1146.   REG_NOERROR = 0,    /* Success.  */
  1147.   REG_NOMATCH,        /* Didn't find a match (for regexec).  */
  1148.  
  1149.   /* POSIX regcomp return error codes.  (In the order listed in the
  1150.      standard.)  */
  1151.   REG_BADPAT,        /* Invalid pattern.  */
  1152.   REG_ECOLLATE,        /* Not implemented.  */
  1153.   REG_ECTYPE,        /* Invalid character class name.  */
  1154.   REG_EESCAPE,        /* Trailing backslash.  */
  1155.   REG_ESUBREG,        /* Invalid back reference.  */
  1156.   REG_EBRACK,        /* Unmatched left bracket.  */
  1157.   REG_EPAREN,        /* Parenthesis imbalance.  */ 
  1158.   REG_EBRACE,        /* Unmatched \{.  */
  1159.   REG_BADBR,        /* Invalid contents of \{\}.  */
  1160.   REG_ERANGE,        /* Invalid range end.  */
  1161.   REG_ESPACE,        /* Ran out of memory.  */
  1162.   REG_BADRPT,        /* No preceding re for repetition op.  */
  1163.  
  1164.   /* Error codes we've added.  */
  1165.   REG_EEND,        /* Premature end.  */
  1166.   REG_ESIZE,        /* Compiled pattern bigger than 2^16 bytes.  */
  1167.   REG_ERPAREN        /* Unmatched ) or \); not returned from regcomp.  */
  1168. } reg_errcode_t;
  1169.  
  1170. /* The regex.c support, as a client of rx, defines a set of possible
  1171.  * side effects that can be added to the edge lables of nfa edges.
  1172.  * Here is the list of sidef effects in use.
  1173.  */
  1174.  
  1175. enum re_side_effects
  1176. {
  1177. #define RX_WANT_SE_DEFS 1
  1178. #undef RX_DEF_SE
  1179. #undef RX_DEF_CPLX_SE
  1180. #define RX_DEF_SE(IDEM, NAME, VALUE)          NAME VALUE,
  1181. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     NAME VALUE,
  1182. #include "rx.h"
  1183. #undef RX_DEF_SE
  1184. #undef RX_DEF_CPLX_SE
  1185. #undef RX_WANT_SE_DEFS
  1186.    re_floogle_flap = 65533
  1187. };
  1188.  
  1189. /* These hold paramaters for the kinds of side effects that are possible
  1190.  * in the supported pattern languages.  These include things like the 
  1191.  * numeric bounds of {} operators and the index of paren registers for 
  1192.  * subexpression measurement or backreferencing.
  1193.  */
  1194. struct re_se_params
  1195. {
  1196.   enum re_side_effects se;
  1197.   int op1;
  1198.   int op2;
  1199. };
  1200.  
  1201. typedef unsigned reg_syntax_t;
  1202.  
  1203. struct re_pattern_buffer
  1204. {
  1205.   struct rx rx;
  1206.   reg_syntax_t syntax;        /* See below for syntax bit definitions. */
  1207.  
  1208.   unsigned int no_sub:1;    /* If set, don't  return register offsets. */
  1209.   unsigned int not_bol:1;    /* If set, the anchors ('^' and '$') don't */
  1210.   unsigned int not_eol:1;    /*     match at the ends of the string.  */  
  1211.   unsigned int newline_anchor:1;/* If true, an anchor at a newline matches.*/
  1212.   unsigned int least_subs:1;    /* If set, and returning registers, return
  1213.                  * as few values as possible.  Only 
  1214.                  * backreferenced groups and group 0 (the whole
  1215.                  * match) will be returned.
  1216.                  */
  1217.  
  1218.   /* If true, this says that the matcher should keep registers on its
  1219.    * backtracking stack.  For many patterns, we can easily determine that
  1220.    * this isn't necessary.
  1221.    */
  1222.   unsigned int match_regs_on_stack:1;
  1223.   unsigned int search_regs_on_stack:1;
  1224.  
  1225.   /* is_anchored and begbuf_only are filled in by rx_compile. */
  1226.   unsigned int is_anchored:1;    /* Anchorded by ^? */
  1227.   unsigned int begbuf_only:1;    /* Anchored to char position 0? */
  1228.  
  1229.   
  1230.   /* If REGS_UNALLOCATED, allocate space in the `regs' structure
  1231.    * for `max (RE_NREGS, re_nsub + 1)' groups.
  1232.    * If REGS_REALLOCATE, reallocate space if necessary.
  1233.    * If REGS_FIXED, use what's there.  
  1234.    */
  1235. #define REGS_UNALLOCATED 0
  1236. #define REGS_REALLOCATE 1
  1237. #define REGS_FIXED 2
  1238.   unsigned int regs_allocated:2;
  1239.  
  1240.   
  1241.   /* Either a translate table to apply to all characters before
  1242.    * comparing them, or zero for no translation.  The translation
  1243.    * is applied to a pattern when it is compiled and to a string
  1244.    * when it is matched.
  1245.    */
  1246.   unsigned char * translate;
  1247.  
  1248.   /* If this is a valid pointer, it tells rx not to store the extents of 
  1249.    * certain subexpressions (those corresponding to non-zero entries).
  1250.    * Passing 0x1 is the same as passing an array of all ones.  Passing 0x0
  1251.    * is the same as passing an array of all zeros.
  1252.    * The array should contain as many entries as their are subexps in the 
  1253.    * regexp.
  1254.    */
  1255.   char * syntax_parens;
  1256.  
  1257.     /* Number of subexpressions found by the compiler.  */
  1258.   size_t re_nsub;
  1259.  
  1260.   void * buffer;        /* Malloced memory for the nfa. */
  1261.   unsigned long allocated;    /* Size of that memory. */
  1262.  
  1263.   /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
  1264.    * the fastmap, if there is one, to skip over impossible
  1265.    * starting points for matches.  */
  1266.   char *fastmap;
  1267.  
  1268.   unsigned int fastmap_accurate:1; /* These three are internal. */
  1269.   unsigned int can_match_empty:1;  
  1270.   struct rx_nfa_state * start;    /* The nfa starting state. */
  1271.  
  1272.   /* This is the list of iterator bounds for {lo,hi} constructs.
  1273.    * The memory pointed to is part of the rx->buffer.
  1274.    */
  1275.   struct re_se_params *se_params;
  1276.  
  1277.   /* This is a bitset representation of the fastmap.
  1278.    * This is a true fastmap that already takes the translate
  1279.    * table into account.
  1280.    */
  1281.   rx_Bitset fastset;
  1282. };
  1283.  
  1284. /* Type for byte offsets within the string.  POSIX mandates this.  */
  1285. typedef int regoff_t;
  1286.  
  1287. /* This is the structure we store register match data in.  See
  1288.    regex.texinfo for a full description of what registers match.  */
  1289. struct re_registers
  1290. {
  1291.   unsigned num_regs;
  1292.   regoff_t *start;
  1293.   regoff_t *end;
  1294. };
  1295.  
  1296. typedef struct re_pattern_buffer regex_t;
  1297.  
  1298. /* POSIX specification for registers.  Aside from the different names than
  1299.    `re_registers', POSIX uses an array of structures, instead of a
  1300.    structure of arrays.  */
  1301. typedef struct
  1302. {
  1303.   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
  1304.   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
  1305. } regmatch_t;
  1306.  
  1307.  
  1308. /* The following bits are used to determine the regexp syntax we
  1309.    recognize.  The set/not-set meanings are chosen so that Emacs syntax
  1310.    remains the value 0.  The bits are given in alphabetical order, and
  1311.    the definitions shifted by one from the previous bit; thus, when we
  1312.    add or remove a bit, only one other definition need change.  */
  1313.  
  1314. /* If this bit is not set, then \ inside a bracket expression is literal.
  1315.    If set, then such a \ quotes the following character.  */
  1316. #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
  1317.  
  1318. /* If this bit is not set, then + and ? are operators, and \+ and \? are
  1319.      literals. 
  1320.    If set, then \+ and \? are operators and + and ? are literals.  */
  1321. #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
  1322.  
  1323. /* If this bit is set, then character classes are supported.  They are:
  1324.      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
  1325.      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  1326.    If not set, then character classes are not supported.  */
  1327. #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
  1328.  
  1329. /* If this bit is set, then ^ and $ are always anchors (outside bracket
  1330.      expressions, of course).
  1331.    If this bit is not set, then it depends:
  1332.         ^  is an anchor if it is at the beginning of a regular
  1333.            expression or after an open-group or an alternation operator;
  1334.         $  is an anchor if it is at the end of a regular expression, or
  1335.            before a close-group or an alternation operator.  
  1336.  
  1337.    This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
  1338.    POSIX draft 11.2 says that * etc. in leading positions is undefined.
  1339.    We already implemented a previous draft which made those constructs
  1340.    invalid, though, so we haven't changed the code back.  */
  1341. #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
  1342.  
  1343. /* If this bit is set, then special characters are always special
  1344.      regardless of where they are in the pattern.
  1345.    If this bit is not set, then special characters are special only in
  1346.      some contexts; otherwise they are ordinary.  Specifically, 
  1347.      * + ? and intervals are only special when not after the beginning,
  1348.      open-group, or alternation operator.  */
  1349. #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
  1350.  
  1351. /* If this bit is set, then *, +, ?, and { cannot be first in an re or
  1352.      immediately after an alternation or begin-group operator.  */
  1353. #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
  1354.  
  1355. /* If this bit is set, then . matches newline.
  1356.    If not set, then it doesn't.  */
  1357. #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
  1358.  
  1359. /* If this bit is set, then . doesn't match NUL.
  1360.    If not set, then it does.  */
  1361. #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
  1362.  
  1363. /* If this bit is set, nonmatching lists [^...] do not match newline.
  1364.    If not set, they do.  */
  1365. #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
  1366.  
  1367. /* If this bit is set, either \{...\} or {...} defines an
  1368.      interval, depending on RE_NO_BK_BRACES. 
  1369.    If not set, \{, \}, {, and } are literals.  */
  1370. #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
  1371.  
  1372. /* If this bit is set, +, ? and | aren't recognized as operators.
  1373.    If not set, they are.  */
  1374. #define RE_LIMITED_OPS (RE_INTERVALS << 1)
  1375.  
  1376. /* If this bit is set, newline is an alternation operator.
  1377.    If not set, newline is literal.  */
  1378. #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
  1379.  
  1380. /* If this bit is set, then `{...}' defines an interval, and \{ and \}
  1381.      are literals.
  1382.   If not set, then `\{...\}' defines an interval.  */
  1383. #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
  1384.  
  1385. /* If this bit is set, (...) defines a group, and \( and \) are literals.
  1386.    If not set, \(...\) defines a group, and ( and ) are literals.  */
  1387. #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
  1388.  
  1389. /* If this bit is set, then \<digit> matches <digit>.
  1390.    If not set, then \<digit> is a back-reference.  */
  1391. #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
  1392.  
  1393. /* If this bit is set, then | is an alternation operator, and \| is literal. 
  1394.    If not set, then \| is an alternation operator, and | is literal.  */
  1395. #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
  1396.  
  1397. /* If this bit is set, then an ending range point collating higher
  1398.      than the starting range point, as in [z-a], is invalid.
  1399.    If not set, then when ending range point collates higher than the
  1400.      starting range point, the range is ignored.  */
  1401. #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
  1402.  
  1403. /* If this bit is set, then an unmatched ) is ordinary.
  1404.    If not set, then an unmatched ) is invalid.  */
  1405. #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
  1406.  
  1407. /* This global variable defines the particular regexp syntax to use (for
  1408.    some interfaces).  When a regexp is compiled, the syntax used is
  1409.    stored in the pattern buffer, so changing this does not affect
  1410.    already-compiled regexps.  */
  1411. extern reg_syntax_t re_syntax_options;
  1412.  
  1413. /* Define combinations of the above bits for the standard possibilities.
  1414.    (The [[[ comments delimit what gets put into the Texinfo file, so
  1415.    don't delete them!)  */ 
  1416. /* [[[begin syntaxes]]] */
  1417. #define RE_SYNTAX_EMACS 0
  1418.  
  1419. #define RE_SYNTAX_AWK                            \
  1420.   (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL            \
  1421.    | RE_NO_BK_PARENS            | RE_NO_BK_REFS                \
  1422.    | RE_NO_BK_VAR               | RE_NO_EMPTY_RANGES            \
  1423.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1424.  
  1425. #define RE_SYNTAX_POSIX_AWK                         \
  1426.   (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
  1427.  
  1428. #define RE_SYNTAX_GREP                            \
  1429.   (RE_BK_PLUS_QM              | RE_CHAR_CLASSES                \
  1430.    | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS                \
  1431.    | RE_NEWLINE_ALT)
  1432.  
  1433. #define RE_SYNTAX_EGREP                            \
  1434.   (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS            \
  1435.    | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE            \
  1436.    | RE_NEWLINE_ALT       | RE_NO_BK_PARENS                \
  1437.    | RE_NO_BK_VBAR)
  1438.  
  1439. #define RE_SYNTAX_POSIX_EGREP                        \
  1440.   (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
  1441.  
  1442. #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
  1443.  
  1444. /* Syntax bits common to both basic and extended POSIX regex syntax.  */
  1445. #define _RE_SYNTAX_POSIX_COMMON                        \
  1446.   (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL        \
  1447.    | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
  1448.  
  1449. #define RE_SYNTAX_POSIX_BASIC                        \
  1450.   (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
  1451.  
  1452. /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
  1453.    RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
  1454.    isn't minimal, since other operators, such as \`, aren't disabled.  */
  1455. #define RE_SYNTAX_POSIX_MINIMAL_BASIC                    \
  1456.   (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
  1457.  
  1458. #define RE_SYNTAX_POSIX_EXTENDED                    \
  1459.   (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS            \
  1460.    | RE_CONTEXT_INDEP_OPS  | RE_NO_BK_BRACES                \
  1461.    | RE_NO_BK_PARENS       | RE_NO_BK_VBAR                \
  1462.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1463.  
  1464. /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
  1465.    replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added.  */
  1466. #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED                \
  1467.   (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS            \
  1468.    | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES                \
  1469.    | RE_NO_BK_PARENS        | RE_NO_BK_REFS                \
  1470.    | RE_NO_BK_VBAR        | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1471. /* [[[end syntaxes]]] */
  1472.  
  1473. /* Maximum number of duplicates an interval can allow.  Some systems
  1474.    (erroneously) define this in other header files, but we want our
  1475.    value, so remove any previous define.  */
  1476. #ifdef RE_DUP_MAX
  1477. #undef RE_DUP_MAX
  1478. #endif
  1479. #define RE_DUP_MAX ((1 << 15) - 1) 
  1480.  
  1481.  
  1482.  
  1483. /* POSIX `cflags' bits (i.e., information for `regcomp').  */
  1484.  
  1485. /* If this bit is set, then use extended regular expression syntax.
  1486.    If not set, then use basic regular expression syntax.  */
  1487. #define REG_EXTENDED 1
  1488.  
  1489. /* If this bit is set, then ignore case when matching.
  1490.    If not set, then case is significant.  */
  1491. #define REG_ICASE (REG_EXTENDED << 1)
  1492.  
  1493. /* If this bit is set, then anchors do not match at newline
  1494.      characters in the string.
  1495.    If not set, then anchors do match at newlines.  */
  1496. #define REG_NEWLINE (REG_ICASE << 1)
  1497.  
  1498. /* If this bit is set, then report only success or fail in regexec.
  1499.    If not set, then returns differ between not matching and errors.  */
  1500. #define REG_NOSUB (REG_NEWLINE << 1)
  1501.  
  1502.  
  1503. /* POSIX `eflags' bits (i.e., information for regexec).  */
  1504.  
  1505. /* If this bit is set, then the beginning-of-line operator doesn't match
  1506.      the beginning of the string (presumably because it's not the
  1507.      beginning of a line).
  1508.    If not set, then the beginning-of-line operator does match the
  1509.      beginning of the string.  */
  1510. #define REG_NOTBOL 1
  1511.  
  1512. /* Like REG_NOTBOL, except for the end-of-line.  */
  1513. #define REG_NOTEOL (1 << 1)
  1514.  
  1515. /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
  1516.  * `re_match_2' returns information about at least this many registers
  1517.  * the first time a `regs' structure is passed. 
  1518.  *
  1519.  * Also, this is the greatest number of backreferenced subexpressions
  1520.  * allowed in a pattern being matched without caller-supplied registers.
  1521.  */
  1522. #ifndef RE_NREGS
  1523. #define RE_NREGS 30
  1524. #endif
  1525.  
  1526. extern int rx_cache_bound;
  1527. extern char rx_version_string[];
  1528.  
  1529.  
  1530.  
  1531. #ifdef RX_WANT_RX_DEFS
  1532.  
  1533. /* This is decls to the interesting subsystems and lower layers
  1534.  * of rx.  Everything which doesn't have a public counterpart in 
  1535.  * regex.c is declared here.
  1536.  */
  1537.  
  1538.  
  1539. #ifdef __STDC__
  1540. typedef void (*rx_hash_freefn) (struct rx_hash_item * it);
  1541. #else /* ndef __STDC__ */
  1542. typedef void (*rx_hash_freefn) ();
  1543. #endif /* ndef __STDC__ */
  1544.  
  1545.  
  1546.  
  1547.  
  1548. #ifdef __STDC__
  1549. RX_DECL int rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b);
  1550. RX_DECL int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
  1551. RX_DECL int rx_bitset_empty (int size, rx_Bitset set);
  1552. RX_DECL void rx_bitset_null (int size, rx_Bitset b);
  1553. RX_DECL void rx_bitset_universe (int size, rx_Bitset b);
  1554. RX_DECL void rx_bitset_complement (int size, rx_Bitset b);
  1555. RX_DECL void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
  1556. RX_DECL void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
  1557. RX_DECL void rx_bitset_intersection (int size,
  1558.                      rx_Bitset a, rx_Bitset b);
  1559. RX_DECL void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
  1560. RX_DECL void rx_bitset_revdifference (int size,
  1561.                       rx_Bitset a, rx_Bitset b);
  1562. RX_DECL void rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b);
  1563. RX_DECL unsigned long rx_bitset_hash (int size, rx_Bitset b);
  1564. RX_DECL struct rx_hash_item * rx_hash_find (struct rx_hash * table,
  1565.                         unsigned long hash,
  1566.                         void * value,
  1567.                         struct rx_hash_rules * rules);
  1568. RX_DECL struct rx_hash_item * rx_hash_store (struct rx_hash * table,
  1569.                          unsigned long hash,
  1570.                          void * value,
  1571.                          struct rx_hash_rules * rules);
  1572. RX_DECL void rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules);
  1573. RX_DECL void rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
  1574.                  struct rx_hash_rules * rules);
  1575. RX_DECL rx_Bitset rx_cset (struct rx *rx);
  1576. RX_DECL rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
  1577. RX_DECL void rx_free_cset (struct rx * rx, rx_Bitset c);
  1578. RX_DECL struct rexp_node * rexp_node (struct rx *rx,
  1579.                       enum rexp_node_type type);
  1580. RX_DECL struct rexp_node * rx_mk_r_cset (struct rx * rx,
  1581.                      rx_Bitset b);
  1582. RX_DECL struct rexp_node * rx_mk_r_concat (struct rx * rx,
  1583.                        struct rexp_node * a,
  1584.                        struct rexp_node * b);
  1585. RX_DECL struct rexp_node * rx_mk_r_alternate (struct rx * rx,
  1586.                           struct rexp_node * a,
  1587.                           struct rexp_node * b);
  1588. RX_DECL struct rexp_node * rx_mk_r_opt (struct rx * rx,
  1589.                     struct rexp_node * a);
  1590. RX_DECL struct rexp_node * rx_mk_r_star (struct rx * rx,
  1591.                      struct rexp_node * a);
  1592. RX_DECL struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
  1593.                         struct rexp_node * a,
  1594.                         struct rexp_node * b);
  1595. RX_DECL struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
  1596.                         rx_side_effect a);
  1597. RX_DECL struct rexp_node * rx_mk_r_data  (struct rx * rx,
  1598.                       void * a);
  1599. RX_DECL void rx_free_rexp (struct rx * rx, struct rexp_node * node);
  1600. RX_DECL struct rexp_node * rx_copy_rexp (struct rx *rx,
  1601.                      struct rexp_node *node);
  1602. RX_DECL struct rx_nfa_state * rx_nfa_state (struct rx *rx);
  1603. RX_DECL void rx_free_nfa_state (struct rx_nfa_state * n);
  1604. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
  1605.                           int id);
  1606. RX_DECL struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
  1607.                       enum rx_nfa_etype type,
  1608.                       struct rx_nfa_state *start,
  1609.                       struct rx_nfa_state *dest);
  1610. RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge * e);
  1611. RX_DECL void rx_free_nfa (struct rx *rx);
  1612. RX_DECL int rx_build_nfa (struct rx *rx,
  1613.               struct rexp_node *rexp,
  1614.               struct rx_nfa_state **start,
  1615.               struct rx_nfa_state **end);
  1616. RX_DECL void rx_name_nfa_states (struct rx *rx);
  1617. RX_DECL int rx_eclose_nfa (struct rx *rx);
  1618. RX_DECL void rx_delete_epsilon_transitions (struct rx *rx);
  1619. RX_DECL int rx_compactify_nfa (struct rx *rx,
  1620.                    void **mem, unsigned long *size);
  1621. RX_DECL void rx_release_superset (struct rx *rx,
  1622.                   struct rx_superset *set);
  1623. RX_DECL struct rx_superset * rx_superset_cons (struct rx * rx,
  1624.                            struct rx_nfa_state *car, struct rx_superset *cdr);
  1625. RX_DECL struct rx_superset * rx_superstate_eclosure_union
  1626.   (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl);
  1627. RX_DECL struct rx_superstate * rx_superstate (struct rx *rx,
  1628.                           struct rx_superset *set);
  1629. RX_DECL struct rx_inx * rx_handle_cache_miss
  1630.   (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data);
  1631. RX_DECL reg_errcode_t rx_compile (__const__ char *pattern, int size,
  1632.                   reg_syntax_t syntax,
  1633.                   struct re_pattern_buffer * rxb);
  1634. RX_DECL void rx_blow_up_fastmap (struct re_pattern_buffer * rxb);
  1635. #else /* STDC */
  1636. RX_DECL int rx_bitset_is_equal ();
  1637. RX_DECL int rx_bitset_is_subset ();
  1638. RX_DECL int rx_bitset_empty ();
  1639. RX_DECL void rx_bitset_null ();
  1640. RX_DECL void rx_bitset_universe ();
  1641. RX_DECL void rx_bitset_complement ();
  1642. RX_DECL void rx_bitset_assign ();
  1643. RX_DECL void rx_bitset_union ();
  1644. RX_DECL void rx_bitset_intersection ();
  1645. RX_DECL void rx_bitset_difference ();
  1646. RX_DECL void rx_bitset_revdifference ();
  1647. RX_DECL void rx_bitset_xor ();
  1648. RX_DECL unsigned long rx_bitset_hash ();
  1649. RX_DECL struct rx_hash_item * rx_hash_find ();
  1650. RX_DECL struct rx_hash_item * rx_hash_store ();
  1651. RX_DECL void rx_hash_free ();
  1652. RX_DECL void rx_free_hash_table ();
  1653. RX_DECL rx_Bitset rx_cset ();
  1654. RX_DECL rx_Bitset rx_copy_cset ();
  1655. RX_DECL void rx_free_cset ();
  1656. RX_DECL struct rexp_node * rexp_node ();
  1657. RX_DECL struct rexp_node * rx_mk_r_cset ();
  1658. RX_DECL struct rexp_node * rx_mk_r_concat ();
  1659. RX_DECL struct rexp_node * rx_mk_r_alternate ();
  1660. RX_DECL struct rexp_node * rx_mk_r_opt ();
  1661. RX_DECL struct rexp_node * rx_mk_r_star ();
  1662. RX_DECL struct rexp_node * rx_mk_r_2phase_star ();
  1663. RX_DECL struct rexp_node * rx_mk_r_side_effect ();
  1664. RX_DECL struct rexp_node * rx_mk_r_data  ();
  1665. RX_DECL void rx_free_rexp ();
  1666. RX_DECL struct rexp_node * rx_copy_rexp ();
  1667. RX_DECL struct rx_nfa_state * rx_nfa_state ();
  1668. RX_DECL void rx_free_nfa_state ();
  1669. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state ();
  1670. RX_DECL struct rx_nfa_edge * rx_nfa_edge ();
  1671. RX_DECL void rx_free_nfa_edge ();
  1672. RX_DECL void rx_free_nfa ();
  1673. RX_DECL int rx_build_nfa ();
  1674. RX_DECL void rx_name_nfa_states ();
  1675. RX_DECL int rx_eclose_nfa ();
  1676. RX_DECL void rx_delete_epsilon_transitions ();
  1677. RX_DECL int rx_compactify_nfa ();
  1678. RX_DECL void rx_release_superset ();
  1679. RX_DECL struct rx_superset * rx_superset_cons ();
  1680. RX_DECL struct rx_superset * rx_superstate_eclosure_union ();
  1681. RX_DECL struct rx_superstate * rx_superstate ();
  1682. RX_DECL struct rx_inx * rx_handle_cache_miss ();
  1683. RX_DECL reg_errcode_t rx_compile ();
  1684. RX_DECL void rx_blow_up_fastmap ();
  1685. #endif /* STDC */
  1686.  
  1687.  
  1688. #endif /* RX_WANT_RX_DEFS */
  1689.  
  1690.  
  1691.  
  1692. #ifdef __STDC__
  1693. extern int re_search_2 (struct re_pattern_buffer *rxb,
  1694.             __const__ char * string1, int size1,
  1695.             __const__ char * string2, int size2,
  1696.             int startpos, int range,
  1697.             struct re_registers *regs,
  1698.             int stop);
  1699. extern int re_search (struct re_pattern_buffer * rxb, __const__ char *string,
  1700.               int size, int startpos, int range,
  1701.               struct re_registers *regs);
  1702. extern int re_match_2 (struct re_pattern_buffer * rxb,
  1703.                __const__ char * string1, int size1,
  1704.                __const__ char * string2, int size2,
  1705.                int pos, struct re_registers *regs, int stop);
  1706. extern int re_match (struct re_pattern_buffer * rxb,
  1707.              __const__ char * string,
  1708.              int size, int pos,
  1709.              struct re_registers *regs);
  1710. extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
  1711. extern void re_set_registers (struct re_pattern_buffer *bufp,
  1712.                   struct re_registers *regs,
  1713.                   unsigned num_regs,
  1714.                   regoff_t * starts, regoff_t * ends);
  1715. extern __const__ char * re_compile_pattern (__const__ char *pattern,
  1716.                     int length,
  1717.                     struct re_pattern_buffer * rxb);
  1718. extern int re_compile_fastmap (struct re_pattern_buffer * rxb);
  1719. extern char * re_comp (__const__ char *s);
  1720. extern int re_exec (__const__ char *s);
  1721. extern int regcomp (regex_t * preg, __const__ char * pattern, int cflags);
  1722. extern int regexec (__const__ regex_t *preg, __const__ char *string,
  1723.             size_t nmatch, regmatch_t pmatch[],
  1724.             int eflags);
  1725. extern size_t regerror (int errcode, __const__ regex_t *preg,
  1726.             char *errbuf, size_t errbuf_size);
  1727. extern void regfree (regex_t *preg);
  1728.  
  1729. #else /* STDC */
  1730. extern int re_search_2 ();
  1731. extern int re_search ();
  1732. extern int re_match_2 ();
  1733. extern int re_match ();
  1734. extern reg_syntax_t re_set_syntax ();
  1735. extern void re_set_registers ();
  1736. extern __const__ char * re_compile_pattern ();
  1737. extern int re_compile_fastmap ();
  1738. extern char * re_comp ();
  1739. extern int re_exec ();
  1740. extern int regcomp ();
  1741. extern int regexec ();
  1742. extern size_t regerror ();
  1743. extern void regfree ();
  1744.  
  1745. #endif /* STDC */
  1746.  
  1747.  
  1748.  
  1749. #ifdef RX_WANT_RX_DEFS
  1750.  
  1751. struct rx_counter_frame
  1752. {
  1753.   int tag;
  1754.   int val;
  1755.   struct rx_counter_frame * inherited_from; /* If this is a copy. */
  1756.   struct rx_counter_frame * cdr;
  1757. };
  1758.  
  1759. struct rx_backtrack_frame
  1760. {
  1761.   char * counter_stack_sp;
  1762.  
  1763.   /* A frame is used to save the matchers state when it crosses a 
  1764.    * backtracking point.  The `stk_' fields correspond to variables
  1765.    * in re_search_2 (just strip off thes `stk_').  They are documented
  1766.    * tere.
  1767.    */
  1768.   struct rx_superstate * stk_super;
  1769.   unsigned int stk_c;
  1770.   struct rx_string_position stk_test_pos;
  1771.   int stk_last_l;
  1772.   int stk_last_r;
  1773.   int stk_test_ret;
  1774.  
  1775.   /* This is the list of options left to explore at the backtrack
  1776.    * point for which this frame was created. 
  1777.    */
  1778.   struct rx_distinct_future * df;
  1779.   struct rx_distinct_future * first_df;
  1780.  
  1781. #ifdef RX_DEBUG
  1782.    int stk_line_no;
  1783. #endif
  1784. };
  1785.  
  1786. struct rx_stack_chunk
  1787. {
  1788.   struct rx_stack_chunk * next_chunk;
  1789.   int bytes_left;
  1790.   char * sp;
  1791. };
  1792.  
  1793. enum rx_outer_entry
  1794. {
  1795.   rx_outer_start,
  1796.   rx_outer_fastmap,
  1797.   rx_outer_test,
  1798.   rx_outer_restore_pos
  1799. };
  1800.  
  1801. enum rx_fastmap_return
  1802. {
  1803.   rx_fastmap_continuation,
  1804.   rx_fastmap_error,
  1805.   rx_fastmap_ok,
  1806.   rx_fastmap_fail
  1807. };
  1808.  
  1809. enum rx_fastmap_entry
  1810. {
  1811.   rx_fastmap_start,
  1812.   rx_fastmap_string_break
  1813. };
  1814.  
  1815. enum rx_test_return
  1816. {
  1817.   rx_test_continuation,
  1818.   rx_test_error,
  1819.   rx_test_fail,
  1820.   rx_test_ok
  1821. };
  1822.  
  1823. enum rx_test_internal_return
  1824. {
  1825.   rx_test_internal_error,
  1826.   rx_test_found_first,
  1827.   rx_test_line_finished
  1828. };
  1829.  
  1830. enum rx_test_match_entry
  1831. {
  1832.   rx_test_start,
  1833.   rx_test_cache_hit_loop,
  1834.   rx_test_backreference_check,
  1835.   rx_test_backtrack_return
  1836. };
  1837.  
  1838. struct rx_search_state
  1839. {
  1840.   /* Two groups of registers are kept.  The group with the register state
  1841.    * of the current test match, and the group that holds the state at the end
  1842.    * of the best known match, if any.
  1843.    *
  1844.    * For some patterns, there may also be registers saved on the stack.
  1845.    */
  1846.   unsigned num_regs;        /* Includes an element for register zero. */
  1847.   regoff_t * lparen;        /* scratch space for register returns */
  1848.   regoff_t * rparen;
  1849.   regoff_t * best_lpspace;    /* in case the user doesn't want these */
  1850.   regoff_t * best_rpspace;    /* values, we still need space to store
  1851.                  * them.  Normally, this memoryis unused
  1852.                  * and the space pointed to by REGS is 
  1853.                  * used instead.
  1854.                  */
  1855.   
  1856.   int last_l;            /* Highest index of a valid lparen. */
  1857.   int last_r;            /* It's dual. */
  1858.   
  1859.   int * best_lparen;        /* This contains the best known register */
  1860.   int * best_rparen;        /* assignments. 
  1861.                  * This may point to the same mem as
  1862.                  * best_lpspace, or it might point to memory
  1863.                  * passed by the caller.
  1864.                  */
  1865.   int best_last_l;        /* best_last_l:best_lparen::last_l:lparen */
  1866.   int best_last_r;
  1867.  
  1868.  
  1869.   unsigned char * translate;  
  1870.  
  1871.   struct rx_string_position outer_pos;
  1872.  
  1873.   struct rx_superstate * start_super;
  1874.   int nfa_choice;
  1875.   int first_found;        /* If true, return after finding any match. */
  1876.   int ret_val;
  1877.  
  1878.   /* For continuations... */
  1879.   enum rx_outer_entry outer_search_resume_pt;
  1880.   struct re_pattern_buffer * saved_rxb;
  1881.   int saved_startpos;
  1882.   int saved_range;
  1883.   int saved_stop;
  1884.   int saved_total_size;
  1885.   rx_get_burst_fn saved_get_burst;
  1886.   rx_back_check_fn saved_back_check;
  1887.   struct re_registers * saved_regs;
  1888.   
  1889.   /**
  1890.    ** state for fastmap
  1891.    **/
  1892.   char * fastmap;
  1893.   int fastmap_chr;
  1894.   int fastmap_val;
  1895.  
  1896.   /* for continuations in the fastmap procedure: */
  1897.   enum rx_fastmap_entry fastmap_resume_pt;
  1898.  
  1899.   /**
  1900.    ** state for test_match 
  1901.    **/
  1902.  
  1903.   /* The current superNFA position of the matcher. */
  1904.   struct rx_superstate * super;
  1905.   
  1906.   /* The matcher interprets a series of instruction frames.
  1907.    * This is the `instruction counter' for the interpretation.
  1908.    */
  1909.   struct rx_inx * ifr;
  1910.   
  1911.   /* We insert a ghost character in the string to prime
  1912.    * the nfa.  test_pos.pos, test_pos.str_half, and test_pos.end_half
  1913.    * keep track of the test-match position and string-half.
  1914.    */
  1915.   unsigned char c;
  1916.   
  1917.   /* Position within the string. */
  1918.   struct rx_string_position test_pos;
  1919.  
  1920.   struct rx_stack_chunk * counter_stack;
  1921.   struct rx_stack_chunk * backtrack_stack;
  1922.   int backtrack_frame_bytes;
  1923.   int chunk_bytes;
  1924.   struct rx_stack_chunk * free_chunks;
  1925.  
  1926.   /* To return from this function, set test_ret and 
  1927.    * `goto test_do_return'.
  1928.    *
  1929.    * Possible return values are:
  1930.    *     1   --- end of string while the superNFA is still going
  1931.    *     0   --- internal error (out of memory)
  1932.    *    -1   --- search completed by reaching the superNFA fail state
  1933.    *    -2   --- a match was found, maybe not the longest.
  1934.    *
  1935.    * When the search is complete (-1), best_last_r indicates whether
  1936.    * a match was found.
  1937.    *
  1938.    * -2 is return only if search_state.first_found is non-zero.
  1939.    *
  1940.    * if search_state.first_found is non-zero, a return of -1 indicates no match,
  1941.    * otherwise, best_last_r has to be checked.
  1942.    */
  1943.   int test_ret;
  1944.  
  1945.   int could_have_continued;
  1946.   
  1947. #ifdef RX_DEBUG
  1948.   int backtrack_depth;
  1949.   /* There is a search tree with every node as set of deterministic
  1950.    * transitions in the super nfa.  For every branch of a 
  1951.    * backtrack point is an edge in the tree.
  1952.    * This counts up a pre-order of nodes in that tree.
  1953.    * It's saved on the search stack and printed when debugging. 
  1954.    */
  1955.   int line_no;
  1956.   int lines_found;
  1957. #endif
  1958.  
  1959.  
  1960.   /* For continuations within the match tester */
  1961.   enum rx_test_match_entry test_match_resume_pt;
  1962.   struct rx_inx * saved_next_tr_table;
  1963.   struct rx_inx * saved_this_tr_table;
  1964.   int saved_reg;
  1965.   struct rx_backtrack_frame * saved_bf;
  1966.   
  1967. };
  1968.  
  1969.  
  1970. extern char rx_slowmap[];
  1971. extern unsigned char rx_id_translation[];
  1972.  
  1973. static __inline__ void
  1974. init_fastmap (rxb, search_state)
  1975.      struct re_pattern_buffer * rxb;
  1976.      struct rx_search_state * search_state;
  1977. {
  1978.   search_state->fastmap = (rxb->fastmap
  1979.                ? (char *)rxb->fastmap
  1980.                : (char *)rx_slowmap);
  1981.   /* Update the fastmap now if not correct already. 
  1982.    * When the regexp was compiled, the fastmap was computed
  1983.    * and stored in a bitset.  This expands the bitset into a
  1984.    * character array containing 1s and 0s.
  1985.    */
  1986.   if ((search_state->fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
  1987.     rx_blow_up_fastmap (rxb);
  1988.   search_state->fastmap_chr = -1;
  1989.   search_state->fastmap_val = 0;
  1990.   search_state->fastmap_resume_pt = rx_fastmap_start;
  1991. }
  1992.  
  1993. static __inline__ void
  1994. uninit_fastmap (rxb, search_state)
  1995.      struct re_pattern_buffer * rxb;
  1996.      struct rx_search_state * search_state;
  1997. {
  1998.   /* Unset the fastmap sentinel */
  1999.   if (search_state->fastmap_chr >= 0)
  2000.     search_state->fastmap[search_state->fastmap_chr]
  2001.       = search_state->fastmap_val;
  2002. }
  2003.  
  2004. static __inline__ int
  2005. fastmap_search (rxb, stop, get_burst, app_closure, search_state)
  2006.      struct re_pattern_buffer * rxb;
  2007.      int stop;
  2008.      rx_get_burst_fn get_burst;
  2009.      void * app_closure;
  2010.      struct rx_search_state * search_state;
  2011. {
  2012.   enum rx_fastmap_entry pc;
  2013.  
  2014.   if (0)
  2015.     {
  2016.     return_continuation:
  2017.       search_state->fastmap_resume_pt = pc;
  2018.       return rx_fastmap_continuation;
  2019.     }
  2020.  
  2021.   pc = search_state->fastmap_resume_pt;
  2022.  
  2023.   switch (pc)
  2024.     {
  2025.     case rx_fastmap_start:
  2026.     init_fastmap_sentinal:
  2027.       /* For the sake of fast fastmapping, set a sentinal in the fastmap.
  2028.        * This sentinal will trap the fastmap loop when it reaches the last
  2029.        * valid character in a string half.
  2030.        *
  2031.        * This must be reset when the fastmap/search loop crosses a string 
  2032.        * boundry, and before returning to the caller.  So sometimes,
  2033.        * the fastmap loop is restarted with `continue', othertimes by
  2034.        * `goto init_fastmap_sentinal'.
  2035.        */
  2036.       if (search_state->outer_pos.size)
  2037.     {
  2038.       search_state->fastmap_chr = ((search_state->outer_pos.search_direction == 1)
  2039.                        ? *(search_state->outer_pos.end - 1)
  2040.                        : *search_state->outer_pos.string);
  2041.       search_state->fastmap_val
  2042.         = search_state->fastmap[search_state->fastmap_chr];
  2043.       search_state->fastmap[search_state->fastmap_chr] = 1;
  2044.     }
  2045.       else
  2046.     {
  2047.       search_state->fastmap_chr = -1;
  2048.       search_state->fastmap_val = 0;
  2049.     }
  2050.       
  2051.       if (search_state->outer_pos.pos >= search_state->outer_pos.end)
  2052.     goto fastmap_hit_bound;
  2053.       else
  2054.     {
  2055.       if (search_state->outer_pos.search_direction == 1)
  2056.         {
  2057.           if (search_state->fastmap_val)
  2058.         {
  2059.           for (;;)
  2060.             {
  2061.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  2062.             ++search_state->outer_pos.pos;
  2063.               return rx_fastmap_ok;
  2064.             }
  2065.         }
  2066.           else
  2067.         {
  2068.           for (;;)
  2069.             {
  2070.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  2071.             ++search_state->outer_pos.pos;
  2072.               if (*search_state->outer_pos.pos != search_state->fastmap_chr)
  2073.             return rx_fastmap_ok;
  2074.               else 
  2075.             {
  2076.               ++search_state->outer_pos.pos;
  2077.               if (search_state->outer_pos.pos == search_state->outer_pos.end)
  2078.                 goto fastmap_hit_bound;
  2079.             }
  2080.             }
  2081.         }
  2082.         }
  2083.       else
  2084.         {
  2085.           __const__ unsigned char * bound;
  2086.           bound = search_state->outer_pos.string - 1;
  2087.           if (search_state->fastmap_val)
  2088.         {
  2089.           for (;;)
  2090.             {
  2091.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  2092.             --search_state->outer_pos.pos;
  2093.               return rx_fastmap_ok;
  2094.             }
  2095.         }
  2096.           else
  2097.         {
  2098.           for (;;)
  2099.             {
  2100.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  2101.             --search_state->outer_pos.pos;
  2102.               if ((*search_state->outer_pos.pos != search_state->fastmap_chr) || search_state->fastmap_val)
  2103.             return rx_fastmap_ok;
  2104.               else 
  2105.             {
  2106.               --search_state->outer_pos.pos;
  2107.               if (search_state->outer_pos.pos == bound)
  2108.                 goto fastmap_hit_bound;
  2109.             }
  2110.             }
  2111.         }
  2112.         }
  2113.     }
  2114.       
  2115.     case rx_fastmap_string_break:
  2116.     fastmap_hit_bound:
  2117.       {
  2118.     /* If we hit a bound, it may be time to fetch another burst
  2119.      * of string, or it may be time to return a continuation to 
  2120.       * the caller, or it might be time to fail.
  2121.      */
  2122.  
  2123.     int burst_state;
  2124.     burst_state = get_burst (&search_state->outer_pos, app_closure, stop);
  2125.     switch (burst_state)
  2126.       {
  2127.       case rx_get_burst_continuation:
  2128.         {
  2129.           pc = rx_fastmap_string_break;
  2130.           goto return_continuation;
  2131.         }
  2132.       case rx_get_burst_error:
  2133.         return rx_fastmap_error;
  2134.       case rx_get_burst_ok:
  2135.         goto init_fastmap_sentinal;
  2136.       case rx_get_burst_no_more:
  2137.         /* ...not a string split, simply no more string. 
  2138.          *
  2139.          * When searching backward, running out of string
  2140.          * is reason to quit.
  2141.          *
  2142.          * When searching forward, we allow the possibility
  2143.          * of an (empty) match after the last character in the
  2144.          * virtual string.  So, fall through to the matcher
  2145.          */
  2146.         return (  (search_state->outer_pos.search_direction == 1)
  2147.             ? rx_fastmap_ok
  2148.             : rx_fastmap_fail);
  2149.       }
  2150.       }
  2151.     }
  2152.  
  2153. }
  2154.  
  2155.  
  2156.  
  2157. #ifdef emacs
  2158. /* The `emacs' switch turns on certain matching commands
  2159.  * that make sense only in Emacs. 
  2160.  */
  2161. #include "config.h"
  2162. #include "lisp.h"
  2163. #include "buffer.h"
  2164. #include "syntax.h"
  2165. /* Emacs uses `NULL' as a predicate.  */
  2166. #undef NULL
  2167. #else  /* not emacs */
  2168. /* Setting RX_MEMDBUG is useful if you have dbmalloc.  Maybe with similar
  2169.  * packages too.
  2170.  */
  2171. #ifdef RX_MEMDBUG
  2172. #include <malloc.h>
  2173. #else /* not RX_RX_MEMDBUG */
  2174.  
  2175. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  2176.  * `BSTRING', as far as I know, and neither of them use this code.  
  2177.  */
  2178. #if HAVE_STRING_H || STDC_HEADERS
  2179. #include <string.h>
  2180.  
  2181. #ifndef bcmp
  2182. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  2183. #endif
  2184.  
  2185. #ifndef bcopy
  2186. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  2187. #endif
  2188.  
  2189. #ifndef bzero
  2190. #define bzero(s, n)    memset ((s), 0, (n))
  2191. #endif
  2192.  
  2193. #else /*  HAVE_STRING_H || STDC_HEADERS */
  2194. #include <strings.h>
  2195. #endif   /* not RX_MEMDBUG */
  2196.  
  2197. #ifdef STDC_HEADERS
  2198. #include <stdlib.h>
  2199. #else /* not STDC_HEADERS */
  2200. char *malloc ();
  2201. char *realloc ();
  2202. #endif /* not STDC_HEADERS */
  2203.  
  2204. #endif /* not emacs */
  2205.  
  2206.  
  2207.  
  2208. /* Define the syntax basics for \<, \>, etc.
  2209.  * This must be nonzero for the wordchar and notwordchar pattern
  2210.  * commands in re_match_2.
  2211.  */
  2212. #ifndef Sword 
  2213. #define Sword 1
  2214. #endif
  2215.  
  2216. /* How many characters in the character set.  */
  2217. #define CHAR_SET_SIZE (1 << CHARBITS)
  2218. #define SYNTAX(c) re_syntax_table[c]
  2219. RX_DECL char re_syntax_table[CHAR_SET_SIZE];
  2220.  
  2221. #endif /* not emacs */
  2222.  
  2223.  
  2224. /* Test if at very beginning or at very end of the virtual concatenation
  2225.  *  of `string1' and `string2'.  If only one string, it's `string2'.  
  2226.  */
  2227.  
  2228. #define AT_STRINGS_BEG() \
  2229.   (   -1         \
  2230.    == ((search_state.test_pos.pos - search_state.test_pos.string) \
  2231.        + search_state.test_pos.offset))
  2232.  
  2233. #define AT_STRINGS_END() \
  2234.   (   (total_size - 1)     \
  2235.    == ((search_state.test_pos.pos - search_state.test_pos.string) \
  2236.        + search_state.test_pos.offset))
  2237.  
  2238.  
  2239. /* Test if POS + 1 points to a character which is word-constituent.  We have
  2240.  * two special cases to check for: if past the end of string1, look at
  2241.  * the first character in string2; and if before the beginning of
  2242.  * string2, look at the last character in string1.
  2243.  *
  2244.  * Assumes `string1' exists, so use in conjunction with AT_STRINGS_BEG ().  
  2245.  */
  2246. #define LETTER_P(POS,OFF)                        \
  2247.   (   SYNTAX (fetch_char(POS, OFF, app_closure, stop))            \
  2248.    == Sword)
  2249.  
  2250. /* Test if the character at D and the one after D differ with respect
  2251.  * to being word-constituent.  
  2252.  */
  2253. #define AT_WORD_BOUNDARY(d)                        \
  2254.   (AT_STRINGS_BEG () || AT_STRINGS_END () || LETTER_P (d,0) != LETTER_P (d, 1))
  2255.  
  2256.  
  2257. #ifdef RX_SUPPORT_CONTINUATIONS
  2258. #define RX_STACK_ALLOC(BYTES) malloc(BYTES)
  2259. #define RX_STACK_FREE(MEM) free(MEM)
  2260. #else
  2261. #define RX_STACK_ALLOC(BYTES) alloca(BYTES)
  2262. #define RX_STACK_FREE(MEM) \
  2263.       ((struct rx_stack_chunk *)MEM)->next_chunk = search_state.free_chunks; \
  2264.       search_state.free_chunks = ((struct rx_stack_chunk *)MEM);
  2265.  
  2266. #endif
  2267.  
  2268. #define PUSH(CHUNK_VAR,BYTES)   \
  2269.   if (!CHUNK_VAR || (CHUNK_VAR->bytes_left < (BYTES)))  \
  2270.     {                    \
  2271.       struct rx_stack_chunk * new_chunk;    \
  2272.       if (search_state.free_chunks)            \
  2273.     {                \
  2274.       new_chunk = search_state.free_chunks;    \
  2275.       search_state.free_chunks = search_state.free_chunks->next_chunk; \
  2276.     }                \
  2277.       else                \
  2278.     {                \
  2279.       new_chunk = (struct rx_stack_chunk *)RX_STACK_ALLOC(search_state.chunk_bytes); \
  2280.       if (!new_chunk)        \
  2281.         {                \
  2282.           search_state.ret_val = 0;        \
  2283.           goto test_do_return;    \
  2284.         }                \
  2285.     }                \
  2286.       new_chunk->sp = (char *)new_chunk + sizeof (struct rx_stack_chunk); \
  2287.       new_chunk->bytes_left = (search_state.chunk_bytes \
  2288.                    - (BYTES) \
  2289.                    - sizeof (struct rx_stack_chunk)); \
  2290.       new_chunk->next_chunk = CHUNK_VAR; \
  2291.       CHUNK_VAR = new_chunk;        \
  2292.     } \
  2293.   else \
  2294.     (CHUNK_VAR->sp += (BYTES)), (CHUNK_VAR->bytes_left -= (BYTES))
  2295.  
  2296. #define POP(CHUNK_VAR,BYTES) \
  2297.   if (CHUNK_VAR->sp == ((char *)CHUNK_VAR + sizeof(*CHUNK_VAR))) \
  2298.     { \
  2299.       struct rx_stack_chunk * new_chunk = CHUNK_VAR->next_chunk; \
  2300.       RX_STACK_FREE(CHUNK_VAR); \
  2301.       CHUNK_VAR = new_chunk; \
  2302.     } \
  2303.   else \
  2304.     (CHUNK_VAR->sp -= BYTES), (CHUNK_VAR->bytes_left += BYTES)
  2305.  
  2306.  
  2307.  
  2308. #define SRCH_TRANSLATE(C)  search_state.translate[(unsigned char) (C)]
  2309.  
  2310.  
  2311.  
  2312.  
  2313. #ifdef __STDC__
  2314. RX_DECL __inline__ int
  2315. rx_search  (struct re_pattern_buffer * rxb,
  2316.         int startpos,
  2317.         int range,
  2318.         int stop,
  2319.         int total_size,
  2320.         rx_get_burst_fn get_burst,
  2321.         rx_back_check_fn back_check,
  2322.         rx_fetch_char_fn fetch_char,
  2323.         void * app_closure,
  2324.         struct re_registers * regs,
  2325.         struct rx_search_state * resume_state,
  2326.         struct rx_search_state * save_state)
  2327. #else
  2328. RX_DECL __inline__ int
  2329. rx_search  (rxb, startpos, range, stop, total_size,
  2330.         get_burst, back_check, fetch_char,
  2331.         app_closure, regs, resume_state, save_state)
  2332.      struct re_pattern_buffer * rxb;
  2333.      int startpos;
  2334.      int range;
  2335.      int stop;
  2336.      int total_size;
  2337.      rx_get_burst_fn get_burst;
  2338.      rx_back_check_fn back_check;
  2339.      rx_fetch_char_fn fetch_char;
  2340.      void * app_closure;
  2341.      struct re_registers * regs;
  2342.      struct rx_search_state * resume_state;
  2343.      struct rx_search_state * save_state;
  2344. #endif
  2345. {
  2346.   int pc;
  2347.   int test_state;
  2348.   struct rx_search_state search_state;
  2349.  
  2350.   if (!resume_state)
  2351.     pc = rx_outer_start;
  2352.   else
  2353.     {
  2354.       search_state = *resume_state;
  2355.       regs = search_state.saved_regs;
  2356.       rxb = search_state.saved_rxb;
  2357.       startpos = search_state.saved_startpos;
  2358.       range = search_state.saved_range;
  2359.       stop = search_state.saved_stop;
  2360.       total_size = search_state.saved_total_size;
  2361.       get_burst = search_state.saved_get_burst;
  2362.       back_check = search_state.saved_back_check;
  2363.       pc = search_state.outer_search_resume_pt;
  2364.       if (0)
  2365.     {
  2366.     return_continuation:
  2367.       if (save_state)
  2368.         {
  2369.           *save_state = search_state;
  2370.           save_state->saved_regs = regs;
  2371.           save_state->saved_rxb = rxb;
  2372.           save_state->saved_startpos = startpos;
  2373.           save_state->saved_range = range;
  2374.           save_state->saved_stop = stop;
  2375.           save_state->saved_total_size = total_size;
  2376.           save_state->saved_get_burst = get_burst;
  2377.           save_state->saved_back_check = back_check;
  2378.           save_state->outer_search_resume_pt = pc;
  2379.         }
  2380.       return rx_search_continuation;
  2381.     }
  2382.     }
  2383.  
  2384.   switch (pc)
  2385.     {
  2386.     case rx_outer_start:
  2387.       search_state.ret_val = rx_search_fail;
  2388.       (  search_state.lparen
  2389.        = search_state.rparen
  2390.        = search_state.best_lpspace
  2391.        = search_state.best_rpspace
  2392.        = 0);
  2393.       
  2394.       /* figure the number of registers we may need for use in backreferences.
  2395.        * the number here includes an element for register zero.  
  2396.        */
  2397.       search_state.num_regs = rxb->re_nsub + 1;
  2398.       
  2399.       
  2400.       /* check for out-of-range startpos.  */
  2401.       if ((startpos < 0) || (startpos > total_size))
  2402.     return rx_search_fail;
  2403.       
  2404.       /* fix up range if it might eventually take us outside the string. */
  2405.       {
  2406.     int endpos;
  2407.     endpos = startpos + range;
  2408.     if (endpos < -1)
  2409.       range = (-1 - startpos);
  2410.     else if (endpos > (total_size + 1))
  2411.       range = total_size - startpos;
  2412.       }
  2413.       
  2414.       /* if the search isn't to be a backwards one, don't waste time in a
  2415.        * long search for a pattern that says it is anchored.
  2416.        */
  2417.       if (rxb->begbuf_only && (range > 0))
  2418.     {
  2419.       if (startpos > 0)
  2420.         return rx_search_fail;
  2421.       else
  2422.         range = 1;
  2423.     }
  2424.       
  2425.       /* decide whether to use internal or user-provided reg buffers. */
  2426.       if (!regs || rxb->no_sub)
  2427.     {
  2428.       search_state.best_lpspace =
  2429.         (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2430.       search_state.best_rpspace =
  2431.         (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2432.       search_state.best_lparen = search_state.best_lpspace;
  2433.       search_state.best_rparen = search_state.best_rpspace;
  2434.     }
  2435.       else
  2436.     {    
  2437.       /* have the register data arrays been allocated?  */
  2438.       if (rxb->regs_allocated == REGS_UNALLOCATED)
  2439.         { /* no.  so allocate them with malloc.  we need one
  2440.          extra element beyond `search_state.num_regs' for the `-1' marker
  2441.          gnu code uses.  */
  2442.           regs->num_regs = MAX (RE_NREGS, rxb->re_nsub + 1);
  2443.           regs->start = ((regoff_t *)
  2444.                  malloc (regs->num_regs * sizeof ( regoff_t)));
  2445.           regs->end = ((regoff_t *)
  2446.                malloc (regs->num_regs * sizeof ( regoff_t)));
  2447.           if (regs->start == 0 || regs->end == 0)
  2448.         return rx_search_error;
  2449.           rxb->regs_allocated = REGS_REALLOCATE;
  2450.         }
  2451.       else if (rxb->regs_allocated == REGS_REALLOCATE)
  2452.         { /* yes.  if we need more elements than were already
  2453.          allocated, reallocate them.  if we need fewer, just
  2454.          leave it alone.  */
  2455.           if (regs->num_regs < search_state.num_regs + 1)
  2456.         {
  2457.           regs->num_regs = search_state.num_regs + 1;
  2458.           regs->start = ((regoff_t *)
  2459.                  realloc (regs->start,
  2460.                       regs->num_regs * sizeof (regoff_t)));
  2461.           regs->end = ((regoff_t *)
  2462.                    realloc (regs->end,
  2463.                     regs->num_regs * sizeof ( regoff_t)));
  2464.           if (regs->start == 0 || regs->end == 0)
  2465.             return rx_search_error;
  2466.         }
  2467.         }
  2468.       else if (rxb->regs_allocated != REGS_FIXED)
  2469.         return rx_search_error;
  2470.       
  2471.       if (regs->num_regs < search_state.num_regs + 1)
  2472.         {
  2473.           search_state.best_lpspace =
  2474.         ((regoff_t *)
  2475.          REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
  2476.           search_state.best_rpspace =
  2477.         ((regoff_t *)
  2478.          REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
  2479.           search_state.best_lparen = search_state.best_lpspace;
  2480.           search_state.best_rparen = search_state.best_rpspace;
  2481.         }
  2482.       else
  2483.         {
  2484.           search_state.best_lparen = regs->start;
  2485.           search_state.best_rparen = regs->end;
  2486.         }
  2487.     }
  2488.       
  2489.       search_state.lparen =
  2490.     (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2491.       search_state.rparen =
  2492.     (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)); 
  2493.       
  2494.       if (! (   search_state.best_rparen
  2495.          && search_state.best_lparen
  2496.          && search_state.lparen && search_state.rparen))
  2497.     return rx_search_error;
  2498.       
  2499.       search_state.best_last_l = search_state.best_last_r = -1;
  2500.       
  2501.       search_state.translate = (rxb->translate
  2502.                 ? rxb->translate
  2503.                 : rx_id_translation);
  2504.       
  2505.       
  2506.       
  2507.       /*
  2508.        * two nfa's were compiled.  
  2509.        * `0' is complete.
  2510.        * `1' faster but gets registers wrong and ends too soon.
  2511.        */
  2512.       search_state.nfa_choice = (regs && !rxb->least_subs) ? '\0' : '\1';
  2513.       
  2514.       /* we have the option to look for the best match or the first
  2515.        * one we can find.  if the user isn't asking for register information,
  2516.        * we don't need to find the best match.
  2517.        */
  2518.       search_state.first_found = !regs;
  2519.       
  2520.       if (range >= 0)
  2521.     {
  2522.       search_state.outer_pos.search_end = startpos + range;
  2523.       search_state.outer_pos.search_direction = 1;
  2524.     }
  2525.       else
  2526.     {
  2527.       search_state.outer_pos.search_end = startpos + range;
  2528.       search_state.outer_pos.search_direction = -1;
  2529.     }
  2530.       
  2531.       /* the vacuous search always turns up nothing. */
  2532.       if ((search_state.outer_pos.search_direction == 1)
  2533.       ? (startpos > search_state.outer_pos.search_end)
  2534.       : (startpos < search_state.outer_pos.search_end))
  2535.     return rx_search_fail;
  2536.       
  2537.       /* now we build the starting state of the supernfa. */
  2538.       {
  2539.     struct rx_superset * start_contents;
  2540.     struct rx_nfa_state_set * start_nfa_set;
  2541.     
  2542.     /* we presume here that the nfa start state has only one
  2543.      * possible future with no side effects.  
  2544.      */
  2545.     start_nfa_set = rxb->start->futures->destset;
  2546.     if (   rxb->rx.start_set
  2547.         && (rxb->rx.start_set->starts_for == &rxb->rx))
  2548.       start_contents = rxb->rx.start_set;
  2549.     else
  2550.       {
  2551.         start_contents =
  2552.           rx_superstate_eclosure_union (&rxb->rx,
  2553.                         rx_superset_cons (&rxb->rx, 0, 0),
  2554.                         start_nfa_set);
  2555.         
  2556.         if (!start_contents)
  2557.           return rx_search_fail;
  2558.         
  2559.         start_contents->starts_for = &rxb->rx;
  2560.         rxb->rx.start_set = start_contents;
  2561.       }
  2562.     if (   start_contents->superstate
  2563.         && (start_contents->superstate->rx_id == rxb->rx.rx_id))
  2564.       {
  2565.         search_state.start_super = start_contents->superstate;
  2566.         rx_lock_superstate (&rxb->rx, search_state.start_super);
  2567.       }
  2568.     else
  2569.       {
  2570.         rx_protect_superset (&rxb->rx, start_contents);
  2571.         
  2572.         search_state.start_super = rx_superstate (&rxb->rx, start_contents);
  2573.         if (!search_state.start_super)
  2574.           return rx_search_fail;
  2575.         rx_lock_superstate (&rxb->rx, search_state.start_super);
  2576.         rx_release_superset (&rxb->rx, start_contents);
  2577.       }
  2578.       }
  2579.       
  2580.       
  2581.       /* The outer_pos tracks the position within the strings
  2582.        * as seen by loop that calls fastmap_search.
  2583.        *
  2584.        * The caller supplied get_burst function actually 
  2585.        * gives us pointers to chars.
  2586.        * 
  2587.        * Communication with the get_burst function is through an
  2588.        * rx_string_position structure.  Here, the structure for
  2589.        * outer_pos is initialized.   It is set to point to the
  2590.        * NULL string, at an offset of STARTPOS.  STARTPOS is out
  2591.        * of range of the NULL string, so the first call to 
  2592.        * getburst will patch up the rx_string_position to point
  2593.        * to valid characters.
  2594.        */
  2595.  
  2596.       (  search_state.outer_pos.string
  2597.        = search_state.outer_pos.end
  2598.        = 0);
  2599.  
  2600.       search_state.outer_pos.offset = 0;
  2601.       search_state.outer_pos.size = 0;
  2602.       search_state.outer_pos.pos = (unsigned char *)startpos;
  2603.       init_fastmap (rxb, &search_state);
  2604.  
  2605.       search_state.fastmap_resume_pt = rx_fastmap_start;
  2606.     case rx_outer_fastmap:
  2607.       /* do { */
  2608.     pseudo_do:
  2609.       {
  2610.     {
  2611.       int fastmap_state;
  2612.       fastmap_state = fastmap_search (rxb, stop, get_burst, app_closure,
  2613.                       &search_state);
  2614.       switch (fastmap_state)
  2615.         {
  2616.         case rx_fastmap_continuation:
  2617.           pc = rx_outer_fastmap;
  2618.           goto return_continuation;
  2619.         case rx_fastmap_fail:
  2620.           goto finish;
  2621.         case rx_fastmap_ok:
  2622.           break;
  2623.         }
  2624.     }
  2625.     
  2626.     /* now the fastmap loop has brought us to a plausible 
  2627.      * starting point for a match.  so, it's time to run the
  2628.      * nfa and see if a match occured.
  2629.      */
  2630.     startpos = (  search_state.outer_pos.pos
  2631.             - search_state.outer_pos.string
  2632.             + search_state.outer_pos.offset);
  2633.     if (startpos == search_state.outer_pos.search_end)
  2634.       goto finish;
  2635.       }
  2636.  
  2637.       search_state.test_match_resume_pt = rx_test_start;
  2638.       /* do interrupted for entry point... */
  2639.     case rx_outer_test:
  2640.       /* ...do continued */
  2641.       {
  2642.     goto test_match;
  2643.       test_returns_to_search:
  2644.     switch (test_state)
  2645.       {
  2646.       case rx_test_continuation:
  2647.         pc = rx_outer_test;
  2648.         goto return_continuation;
  2649.       case rx_test_error:
  2650.         search_state.ret_val = rx_search_error;
  2651.         goto finish;
  2652.       case rx_test_fail:
  2653.         break;
  2654.       case rx_test_ok:
  2655.         goto finish;
  2656.       }
  2657.     search_state.outer_pos.pos += search_state.outer_pos.search_direction;
  2658.     startpos += search_state.outer_pos.search_direction;
  2659.       }
  2660.       /* do interrupted for entry point... */
  2661.     case rx_outer_restore_pos:
  2662.       {
  2663.     int x;
  2664.     x = get_burst (&search_state.outer_pos, app_closure, stop);
  2665.     switch (x)
  2666.       {
  2667.       case rx_get_burst_continuation:
  2668.         pc = rx_outer_restore_pos;
  2669.         goto return_continuation;
  2670.       case rx_get_burst_error:
  2671.         search_state.ret_val = rx_search_error;
  2672.         goto finish;
  2673.       case rx_get_burst_no_more:
  2674.         if (rxb->can_match_empty)
  2675.           break;
  2676.         goto finish;
  2677.       case rx_get_burst_ok:
  2678.         break;
  2679.       }
  2680.       } /* } while (...see below...) */
  2681.  
  2682.       if ((search_state.outer_pos.search_direction == 1)
  2683.       ? (startpos < search_state.outer_pos.search_end)
  2684.       : (startpos > search_state.outer_pos.search_end))
  2685.     goto pseudo_do;
  2686.  
  2687.     
  2688.     finish:
  2689.       uninit_fastmap (rxb, &search_state);
  2690.       if (search_state.start_super)
  2691.     rx_unlock_superstate (&rxb->rx, search_state.start_super);
  2692.       
  2693. #ifdef regex_malloc
  2694.       if (search_state.lparen) free (search_state.lparen);
  2695.       if (search_state.rparen) free (search_state.rparen);
  2696.       if (search_state.best_lpspace) free (search_state.best_lpspace);
  2697.       if (search_state.best_rpspace) free (search_state.best_rpspace);
  2698. #endif
  2699.       return search_state.ret_val;
  2700.     }
  2701.  
  2702.  
  2703.  test_match:
  2704.   {
  2705.     enum rx_test_match_entry test_pc;
  2706.     int inx;
  2707.     test_pc = search_state.test_match_resume_pt;
  2708.     if (test_pc == rx_test_start)
  2709.       {
  2710. #ifdef RX_DEBUG
  2711.     search_state.backtrack_depth = 0;
  2712. #endif
  2713.     search_state.last_l = search_state.last_r = 0;
  2714.     search_state.lparen[0] = startpos;
  2715.     search_state.super = search_state.start_super;
  2716.     search_state.c = search_state.nfa_choice;
  2717.     search_state.test_pos.pos = search_state.outer_pos.pos - 1;    
  2718.     search_state.test_pos.string = search_state.outer_pos.string;
  2719.     search_state.test_pos.end = search_state.outer_pos.end;
  2720.     search_state.test_pos.offset = search_state.outer_pos.offset;
  2721.     search_state.test_pos.size = search_state.outer_pos.size;
  2722.     search_state.test_pos.search_direction = 1;
  2723.     search_state.counter_stack = 0;
  2724.     search_state.backtrack_stack = 0;
  2725.     search_state.backtrack_frame_bytes =
  2726.       (sizeof (struct rx_backtrack_frame)
  2727.        + (rxb->match_regs_on_stack
  2728.           ? sizeof (regoff_t) * (search_state.num_regs + 1) * 2
  2729.           : 0));
  2730.     search_state.chunk_bytes = search_state.backtrack_frame_bytes * 64;
  2731.     search_state.free_chunks = 0;
  2732.     search_state.test_ret = rx_test_line_finished;
  2733.     search_state.could_have_continued = 0;
  2734.       }  
  2735.     /* This is while (1)...except that the body of the loop is interrupted 
  2736.      * by some alternative entry points.
  2737.      */
  2738.   pseudo_while_1:
  2739.     switch (test_pc)
  2740.       {
  2741.       case rx_test_cache_hit_loop:
  2742.     goto resume_continuation_1;
  2743.       case rx_test_backreference_check:
  2744.     goto resume_continuation_2;
  2745.       case rx_test_backtrack_return:
  2746.     goto resume_continuation_3;
  2747.       case rx_test_start:
  2748. #ifdef RX_DEBUG
  2749.     /* There is a search tree with every node as set of deterministic
  2750.      * transitions in the super nfa.  For every branch of a 
  2751.      * backtrack point is an edge in the tree.
  2752.      * This counts up a pre-order of nodes in that tree.
  2753.      * It's saved on the search stack and printed when debugging. 
  2754.      */
  2755.     search_state.line_no = 0;
  2756.     search_state.lines_found = 0;
  2757. #endif
  2758.     
  2759.       top_of_cycle:
  2760.     /* A superstate is basicly a transition table, indexed by 
  2761.      * characters from the string being tested, and containing 
  2762.      * RX_INX (`instruction frame') structures.
  2763.      */
  2764.     search_state.ifr = &search_state.super->transitions [search_state.c];
  2765.     
  2766.       recurse_test_match:
  2767.     /* This is the point to which control is sent when the
  2768.      * test matcher `recurses'.  Before jumping here, some variables
  2769.      * need to be saved on the stack and the next instruction frame
  2770.      * has to be computed.
  2771.      */
  2772.     
  2773.       restart:
  2774.     /* Some instructions don't advance the matcher, but just
  2775.      * carry out some side effects and fetch a new instruction.
  2776.      * To dispatch that new instruction, `goto restart'.
  2777.      */
  2778.     
  2779.     {
  2780.       struct rx_inx * next_tr_table;
  2781.       struct rx_inx * this_tr_table;
  2782.       /* The fastest route through the loop is when the instruction 
  2783.        * is RX_NEXT_CHAR.  This case is detected when SEARCH_STATE.IFR->DATA
  2784.        * is non-zero.  In that case, it points to the next
  2785.        * superstate. 
  2786.        *
  2787.        * This allows us to not bother fetching the bytecode.
  2788.        */
  2789.       next_tr_table = (struct rx_inx *)search_state.ifr->data;
  2790.       this_tr_table = search_state.super->transitions;
  2791.       while (next_tr_table)
  2792.         {
  2793. #ifdef RX_DEBUG_0
  2794.           if (rx_debug_trace)
  2795.         {
  2796.           struct rx_superset * setp;
  2797.           
  2798.           fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
  2799.                search_state.line_no,
  2800.                search_state.backtrack_depth,
  2801.                (search_state.test_pos.pos - search_state.test_pos.string
  2802.                 + search_state.test_pos.offset), search_state.c);
  2803.           
  2804.           search_state.super =
  2805.             ((struct rx_superstate *)
  2806.              ((char *)this_tr_table
  2807.               - ((unsigned long)
  2808.              ((struct rx_superstate *)0)->transitions)));
  2809.           
  2810.           setp = search_state.super->contents;
  2811.           fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  2812.                rxb->rx.rx_id, setp);
  2813.           while (setp)
  2814.             {
  2815.               fprintf (stderr, "%d ", setp->id);
  2816.               setp = setp->cdr;
  2817.             }
  2818.           fprintf (stderr, "\n");
  2819.         }
  2820. #endif
  2821.           this_tr_table = next_tr_table;
  2822.           ++search_state.test_pos.pos;
  2823.           if (search_state.test_pos.pos == search_state.test_pos.end)
  2824.         {
  2825.           int burst_state;
  2826.         try_burst_1:
  2827.           burst_state = get_burst (&search_state.test_pos,
  2828.                        app_closure, stop);
  2829.           switch (burst_state)
  2830.             {
  2831.             case rx_get_burst_continuation:
  2832.               search_state.saved_this_tr_table = this_tr_table;
  2833.               search_state.saved_next_tr_table = next_tr_table;
  2834.               test_pc = rx_test_cache_hit_loop;
  2835.               goto test_return_continuation;
  2836.               
  2837.             resume_continuation_1:
  2838.               /* Continuation one jumps here to do its work: */
  2839.               search_state.saved_this_tr_table = this_tr_table;
  2840.               search_state.saved_next_tr_table = next_tr_table;
  2841.               goto try_burst_1;
  2842.               
  2843.             case rx_get_burst_ok:
  2844.               /* get_burst succeeded...keep going */
  2845.               break;
  2846.               
  2847.             case rx_get_burst_no_more:
  2848.               search_state.test_ret = rx_test_line_finished;
  2849.               search_state.could_have_continued = 1;
  2850.               goto test_do_return;
  2851.               
  2852.             case rx_get_burst_error:
  2853.               /* An error... */
  2854.               search_state.test_ret = rx_test_internal_error;
  2855.               goto test_do_return;
  2856.             }
  2857.         }
  2858.           search_state.c = *search_state.test_pos.pos;
  2859.           search_state.ifr = this_tr_table + search_state.c;
  2860.           next_tr_table = (struct rx_inx *)search_state.ifr->data;
  2861.         } /* Fast loop through cached transition tables */
  2862.       
  2863.       /* Here when we ran out of cached next-char transitions. 
  2864.        * So, it will be necessary to do a more expensive
  2865.        * dispatch on the current instruction.  The superstate
  2866.        * pointer is allowed to become invalid during next-char
  2867.        * transitions -- now we must bring it up to date.
  2868.        */
  2869.       search_state.super =
  2870.         ((struct rx_superstate *)
  2871.          ((char *)this_tr_table
  2872.           - ((unsigned long)
  2873.          ((struct rx_superstate *)0)->transitions)));
  2874.     }
  2875.     
  2876.     /* We've encountered an instruction other than next-char.
  2877.      * Dispatch that instruction:
  2878.      */
  2879.     inx = (int)search_state.ifr->inx;
  2880. #ifdef RX_DEBUG_0
  2881.     if (rx_debug_trace)
  2882.       {
  2883.         struct rx_superset * setp = search_state.super->contents;
  2884.         
  2885.         fprintf (stderr, "%d %d>> %s @ %d (%d)", search_state.line_no,
  2886.              search_state.backtrack_depth,
  2887.              inx_names[inx],
  2888.              (search_state.test_pos.pos - search_state.test_pos.string
  2889.               + (test_pos.half == 0 ? 0 : size1)), search_state.c);
  2890.         
  2891.         fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  2892.              rxb->rx.rx_id, setp);
  2893.         while (setp)
  2894.           {
  2895.         fprintf (stderr, "%d ", setp->id);
  2896.         setp = setp->cdr;
  2897.           }
  2898.         fprintf (stderr, "\n");
  2899.       }
  2900. #endif
  2901.     switch ((enum rx_opcode)inx)
  2902.       {
  2903.       case rx_do_side_effects:
  2904.         
  2905.         /*  RX_DO_SIDE_EFFECTS occurs when we cross epsilon 
  2906.          *  edges associated with parentheses, backreferencing, etc.
  2907.          */
  2908.         {
  2909.           struct rx_distinct_future * df =
  2910.         (struct rx_distinct_future *)search_state.ifr->data_2;
  2911.           struct rx_se_list * el = df->effects;
  2912.           /* Side effects come in lists.  This walks down
  2913.            * a list, dispatching.
  2914.            */
  2915.           while (el)
  2916.         {
  2917.           long effect;
  2918.           effect = (long)el->car;
  2919.           if (effect < 0)
  2920.             {
  2921. #ifdef RX_DEBUG_0
  2922.               if (rx_debug_trace)
  2923.             {
  2924.               struct rx_superset * setp = search_state.super->contents;
  2925.               
  2926.               fprintf (stderr, "....%d %d>> %s\n", search_state.line_no,
  2927.                    search_state.backtrack_depth,
  2928.                    efnames[-effect]);
  2929.             }
  2930. #endif
  2931.               switch ((enum re_side_effects) effect)
  2932.  
  2933.             {
  2934.             case re_se_pushback:
  2935.               search_state.ifr = &df->future_frame;
  2936.               if (!search_state.ifr->data)
  2937.                 {
  2938.                   struct rx_superstate * sup;
  2939.                   sup = search_state.super;
  2940.                   rx_lock_superstate (rx, sup);
  2941.                   if (!rx_handle_cache_miss (&rxb->rx,
  2942.                              search_state.super,
  2943.                              search_state.c,
  2944.                              (search_state.ifr
  2945.                               ->data_2)))
  2946.                 {
  2947.                   rx_unlock_superstate (rx, sup);
  2948.                   search_state.test_ret = rx_test_internal_error;
  2949.                   goto test_do_return;
  2950.                 }
  2951.                   rx_unlock_superstate (rx, sup);
  2952.                 }
  2953.               /* --search_state.test_pos.pos; */
  2954.               search_state.c = 't';
  2955.               search_state.super
  2956.                 = ((struct rx_superstate *)
  2957.                    ((char *)search_state.ifr->data
  2958.                 - (long)(((struct rx_superstate *)0)
  2959.                      ->transitions)));
  2960.               goto top_of_cycle;
  2961.               break;
  2962.             case re_se_push0:
  2963.               {
  2964.                 struct rx_counter_frame * old_cf
  2965.                   = (search_state.counter_stack
  2966.                  ? ((struct rx_counter_frame *)
  2967.                     search_state.counter_stack->sp)
  2968.                  : 0);
  2969.                 struct rx_counter_frame * cf;
  2970.                 PUSH (search_state.counter_stack,
  2971.                   sizeof (struct rx_counter_frame));
  2972.                 cf = ((struct rx_counter_frame *)
  2973.                   search_state.counter_stack->sp);
  2974.                 cf->tag = re_se_iter;
  2975.                 cf->val = 0;
  2976.                 cf->inherited_from = 0;
  2977.                 cf->cdr = old_cf;
  2978.                 break;
  2979.               }
  2980.             case re_se_fail:
  2981.               goto test_do_return;
  2982.             case re_se_begbuf:
  2983.               if (!AT_STRINGS_BEG ())
  2984.                 goto test_do_return;
  2985.               break;
  2986.             case re_se_endbuf:
  2987.               if (!AT_STRINGS_END ())
  2988.                 goto test_do_return;
  2989.               break;
  2990.             case re_se_wordbeg:
  2991.               if (   LETTER_P (&search_state.test_pos, 1)
  2992.                   && (   AT_STRINGS_BEG()
  2993.                   || !LETTER_P (&search_state.test_pos, 0)))
  2994.                 break;
  2995.               else
  2996.                 goto test_do_return;
  2997.             case re_se_wordend:
  2998.               if (   !AT_STRINGS_BEG ()
  2999.                   && LETTER_P (&search_state.test_pos, 0)
  3000.                   && (AT_STRINGS_END ()
  3001.                   || !LETTER_P (&search_state.test_pos, 1)))
  3002.                 break;
  3003.               else
  3004.                 goto test_do_return;
  3005.             case re_se_wordbound:
  3006.               if (AT_WORD_BOUNDARY (&search_state.test_pos))
  3007.                 break;
  3008.               else
  3009.                 goto test_do_return;
  3010.             case re_se_notwordbound:
  3011.               if (!AT_WORD_BOUNDARY (&search_state.test_pos))
  3012.                 break;
  3013.               else
  3014.                 goto test_do_return;
  3015.             case re_se_hat:
  3016.               if (AT_STRINGS_BEG ())
  3017.                 {
  3018.                   if (rxb->not_bol)
  3019.                 goto test_do_return;
  3020.                   else
  3021.                 break;
  3022.                 }
  3023.               else
  3024.                 {
  3025.                   char pos_c = *search_state.test_pos.pos;
  3026.                   if (   (SRCH_TRANSLATE (pos_c)
  3027.                       == SRCH_TRANSLATE('\n'))
  3028.                   && rxb->newline_anchor)
  3029.                 break;
  3030.                   else
  3031.                 goto test_do_return;
  3032.                 }
  3033.             case re_se_dollar:
  3034.               if (AT_STRINGS_END ())
  3035.                 {
  3036.                   if (rxb->not_eol)
  3037.                 goto test_do_return;
  3038.                   else
  3039.                 break;
  3040.                 }
  3041.               else
  3042.                 {
  3043.                   if (   (   SRCH_TRANSLATE (fetch_char
  3044.                             (&search_state.test_pos, 1,
  3045.                              app_closure, stop))
  3046.                       == SRCH_TRANSLATE ('\n'))
  3047.                   && rxb->newline_anchor)
  3048.                 break;
  3049.                   else
  3050.                 goto test_do_return;
  3051.                 }
  3052.               
  3053.             case re_se_try:
  3054.               /* This is the first side effect in every
  3055.                * expression.
  3056.                *
  3057.                *  FOR NO GOOD REASON...get rid of it...
  3058.                */
  3059.               break;
  3060.               
  3061.             case re_se_pushpos:
  3062.               {
  3063.                 int urhere =
  3064.                   ((int)(search_state.test_pos.pos
  3065.                      - search_state.test_pos.string)
  3066.                    + search_state.test_pos.offset);
  3067.                 struct rx_counter_frame * old_cf
  3068.                   = (search_state.counter_stack
  3069.                  ? ((struct rx_counter_frame *)
  3070.                     search_state.counter_stack->sp)
  3071.                  : 0);
  3072.                 struct rx_counter_frame * cf;
  3073.                 PUSH(search_state.counter_stack,
  3074.                  sizeof (struct rx_counter_frame));
  3075.                 cf = ((struct rx_counter_frame *)
  3076.                   search_state.counter_stack->sp);
  3077.                 cf->tag = re_se_pushpos;
  3078.                 cf->val = urhere;
  3079.                 cf->inherited_from = 0;
  3080.                 cf->cdr = old_cf;
  3081.                 break;
  3082.               }
  3083.               
  3084.             case re_se_chkpos:
  3085.               {
  3086.                 int urhere =
  3087.                   ((int)(search_state.test_pos.pos
  3088.                      - search_state.test_pos.string)
  3089.                    + search_state.test_pos.offset);
  3090.                 struct rx_counter_frame * cf
  3091.                   = ((struct rx_counter_frame *)
  3092.                  search_state.counter_stack->sp);
  3093.                 if (cf->val == urhere)
  3094.                   goto test_do_return;
  3095.                 cf->val = urhere;
  3096.                 break;
  3097.               }
  3098.               break;
  3099.               
  3100.             case re_se_poppos:
  3101.               POP(search_state.counter_stack,
  3102.                   sizeof (struct rx_counter_frame));
  3103.               break;
  3104.               
  3105.               
  3106.             case re_se_at_dot:
  3107.             case re_se_syntax:
  3108.             case re_se_not_syntax:
  3109. #ifdef emacs
  3110.               this release lacks emacs support;
  3111.               (coming soon);
  3112. #endif
  3113.               break;
  3114.             case re_se_win:
  3115.             case re_se_lparen:
  3116.             case re_se_rparen:
  3117.             case re_se_backref:
  3118.             case re_se_iter:
  3119.             case re_se_end_iter:
  3120.             case re_se_tv:
  3121.             case re_floogle_flap:
  3122.               search_state.ret_val = 0;
  3123.               goto test_do_return;
  3124.             }
  3125.             }
  3126.           else
  3127.             {
  3128. #ifdef RX_DEBUG_0
  3129.               if (rx_debug_trace)
  3130.             fprintf (stderr, "....%d %d>> %s %d %d\n", search_state.line_no,
  3131.                  search_state.backtrack_depth,
  3132.                  efnames2[rxb->se_params [effect].se],
  3133.                  rxb->se_params [effect].op1,
  3134.                  rxb->se_params [effect].op2);
  3135. #endif
  3136.               switch (rxb->se_params [effect].se)
  3137.             {
  3138.             case re_se_win:
  3139.               /* This side effect indicates that we've 
  3140.                * found a match, though not necessarily the 
  3141.                * best match.  This is a fancy assignment to 
  3142.                * register 0 unless the caller didn't 
  3143.                * care about registers.  In which case,
  3144.                * this stops the match.
  3145.                */
  3146.               {
  3147.                 int urhere =
  3148.                   ((int)(search_state.test_pos.pos
  3149.                      - search_state.test_pos.string)
  3150.                    + search_state.test_pos.offset);
  3151.                 
  3152.                 if (   (search_state.best_last_r < 0)
  3153.                 || (urhere + 1 > search_state.best_rparen[0]))
  3154.                   {
  3155.                 /* Record the best known and keep
  3156.                  * looking.
  3157.                  */
  3158.                 int x;
  3159.                 for (x = 0; x <= search_state.last_l; ++x)
  3160.                   search_state.best_lparen[x] = search_state.lparen[x];
  3161.                 search_state.best_last_l = search_state.last_l;
  3162.                 for (x = 0; x <= search_state.last_r; ++x)
  3163.                   search_state.best_rparen[x] = search_state.rparen[x];
  3164.                 search_state.best_rparen[0] = urhere + 1;
  3165.                 search_state.best_last_r = search_state.last_r;
  3166.                   }
  3167.                 /* If we're not reporting the match-length 
  3168.                  * or other register info, we need look no
  3169.                  * further.
  3170.                  */
  3171.                 if (search_state.first_found)
  3172.                   {
  3173.                 search_state.test_ret = rx_test_found_first;
  3174.                 goto test_do_return;
  3175.                   }
  3176.               }
  3177.               break;
  3178.             case re_se_lparen:
  3179.               {
  3180.                 int urhere =
  3181.                   ((int)(search_state.test_pos.pos
  3182.                      - search_state.test_pos.string)
  3183.                    + search_state.test_pos.offset);
  3184.                 
  3185.                 int reg = rxb->se_params [effect].op1;
  3186. #if 0
  3187.                 if (reg > search_state.last_l)
  3188. #endif
  3189.                   {
  3190.                 search_state.lparen[reg] = urhere + 1;
  3191.                 /* In addition to making this assignment,
  3192.                  * we now know that lower numbered regs
  3193.                  * that haven't already been assigned,
  3194.                  * won't be.  We make sure they're
  3195.                  * filled with -1, so they can be
  3196.                  * recognized as unassigned.
  3197.                  */
  3198.                 if (search_state.last_l < reg)
  3199.                   while (++search_state.last_l < reg)
  3200.                     search_state.lparen[search_state.last_l] = -1;
  3201.                   }
  3202.                 break;
  3203.               }
  3204.               
  3205.             case re_se_rparen:
  3206.               {
  3207.                 int urhere =
  3208.                   ((int)(search_state.test_pos.pos
  3209.                      - search_state.test_pos.string)
  3210.                    + search_state.test_pos.offset);
  3211.                 int reg = rxb->se_params [effect].op1;
  3212.                 search_state.rparen[reg] = urhere + 1;
  3213.                 if (search_state.last_r < reg)
  3214.                   {
  3215.                 while (++search_state.last_r < reg)
  3216.                   search_state.rparen[search_state.last_r]
  3217.                     = -1;
  3218.                   }
  3219.                 break;
  3220.               }
  3221.               
  3222.             case re_se_backref:
  3223.               {
  3224.                 int reg = rxb->se_params [effect].op1;
  3225.                 if (   reg > search_state.last_r
  3226.                 || search_state.rparen[reg] < 0)
  3227.                   goto test_do_return;
  3228.                 
  3229.                 {
  3230.                   int backref_status;
  3231.                 check_backreference:
  3232.                   backref_status
  3233.                 = back_check (&search_state.test_pos,
  3234.                           search_state.lparen[reg],
  3235.                           search_state.rparen[reg],
  3236.                           search_state.translate,
  3237.                           app_closure,
  3238.                           stop);
  3239.                   switch (backref_status)
  3240.                 {
  3241.                 case rx_back_check_continuation:
  3242.                   search_state.saved_reg = reg;
  3243.                   test_pc = rx_test_backreference_check;
  3244.                   goto test_return_continuation;
  3245.                 resume_continuation_2:
  3246.                   reg = search_state.saved_reg;
  3247.                   goto check_backreference;
  3248.                 case rx_back_check_fail:
  3249.                   /* Fail */
  3250.                   goto test_do_return;
  3251.                 case rx_back_check_pass:
  3252.                   /* pass --
  3253.                    * test_pos now advanced to last
  3254.                    * char matched by backref
  3255.                    */
  3256.                   break;
  3257.                 }
  3258.                 }
  3259.                 break;
  3260.               }
  3261.             case re_se_iter:
  3262.               {
  3263.                 struct rx_counter_frame * csp
  3264.                   = ((struct rx_counter_frame *)
  3265.                  search_state.counter_stack->sp);
  3266.                 if (csp->val == rxb->se_params[effect].op2)
  3267.                   goto test_do_return;
  3268.                 else
  3269.                   ++csp->val;
  3270.                 break;
  3271.               }
  3272.             case re_se_end_iter:
  3273.               {
  3274.                 struct rx_counter_frame * csp
  3275.                   = ((struct rx_counter_frame *)
  3276.                  search_state.counter_stack->sp);
  3277.                 if (csp->val < rxb->se_params[effect].op1)
  3278.                   goto test_do_return;
  3279.                 else
  3280.                   {
  3281.                 struct rx_counter_frame * source = csp;
  3282.                 while (source->inherited_from)
  3283.                   source = source->inherited_from;
  3284.                 if (!source || !source->cdr)
  3285.                   {
  3286.                     POP(search_state.counter_stack,
  3287.                     sizeof(struct rx_counter_frame));
  3288.                   }
  3289.                 else
  3290.                   {
  3291.                     source = source->cdr;
  3292.                     csp->val = source->val;
  3293.                     csp->tag = source->tag;
  3294.                     csp->cdr = 0;
  3295.                     csp->inherited_from = source;
  3296.                   }
  3297.                   }
  3298.                 break;
  3299.               }
  3300.             case re_se_tv:
  3301.               /* is a noop */
  3302.               break;
  3303.             case re_se_try:
  3304.             case re_se_pushback:
  3305.             case re_se_push0:
  3306.             case re_se_pushpos:
  3307.             case re_se_chkpos:
  3308.             case re_se_poppos:
  3309.             case re_se_at_dot:
  3310.             case re_se_syntax:
  3311.             case re_se_not_syntax:
  3312.             case re_se_begbuf:
  3313.             case re_se_hat:
  3314.             case re_se_wordbeg:
  3315.             case re_se_wordbound:
  3316.             case re_se_notwordbound:
  3317.             case re_se_wordend:
  3318.             case re_se_endbuf:
  3319.             case re_se_dollar:
  3320.             case re_se_fail:
  3321.             case re_floogle_flap:
  3322.               search_state.ret_val = 0;
  3323.               goto test_do_return;
  3324.             }
  3325.             }
  3326.           el = el->cdr;
  3327.         }
  3328.           /* Now the side effects are done,
  3329.            * so get the next instruction.
  3330.            * and move on.
  3331.            */
  3332.           search_state.ifr = &df->future_frame;
  3333.           goto restart;
  3334.         }
  3335.         
  3336.       case rx_backtrack_point:
  3337.         {
  3338.           /* A backtrack point indicates that we've reached a
  3339.            * non-determinism in the superstate NFA.  This is a
  3340.            * loop that exhaustively searches the possibilities.
  3341.            *
  3342.            * A backtracking strategy is used.  We keep track of what
  3343.            * registers are valid so we can erase side effects.
  3344.            *
  3345.            * First, make sure there is some stack space to hold 
  3346.            * our state.
  3347.            */
  3348.           
  3349.           struct rx_backtrack_frame * bf;
  3350.           
  3351.           PUSH(search_state.backtrack_stack,
  3352.            search_state.backtrack_frame_bytes);
  3353. #ifdef RX_DEBUG_0
  3354.           ++search_state.backtrack_depth;
  3355. #endif
  3356.           
  3357.           bf = ((struct rx_backtrack_frame *)
  3358.             search_state.backtrack_stack->sp);
  3359.           {
  3360.         bf->stk_super = search_state.super;
  3361.         /* We prevent the current superstate from being
  3362.          * deleted from the superstate cache.
  3363.          */
  3364.         rx_lock_superstate (&rxb->rx, search_state.super);
  3365. #ifdef RX_DEBUG_0
  3366.         bf->stk_search_state.line_no = search_state.line_no;
  3367. #endif
  3368.         bf->stk_c = search_state.c;
  3369.         bf->stk_test_pos = search_state.test_pos;
  3370.         bf->stk_last_l = search_state.last_l;
  3371.         bf->stk_last_r = search_state.last_r;
  3372.         bf->df = ((struct rx_super_edge *)
  3373.               search_state.ifr->data_2)->options;
  3374.         bf->first_df = bf->df;
  3375.         bf->counter_stack_sp = (search_state.counter_stack
  3376.                     ? search_state.counter_stack->sp
  3377.                     : 0);
  3378.         bf->stk_test_ret = search_state.test_ret;
  3379.         if (rxb->match_regs_on_stack)
  3380.           {
  3381.             int x;
  3382.             regoff_t * stk =
  3383.               (regoff_t *)((char *)bf + sizeof (*bf));
  3384.             for (x = 0; x <= search_state.last_l; ++x)
  3385.               stk[x] = search_state.lparen[x];
  3386.             stk += x;
  3387.             for (x = 0; x <= search_state.last_r; ++x)
  3388.               stk[x] = search_state.rparen[x];
  3389.           }
  3390.           }
  3391.           
  3392.           /* Here is a while loop whose body is mainly a function
  3393.            * call and some code to handle a return from that
  3394.            * function.
  3395.            *
  3396.            * From here on for the rest of `case backtrack_point' it
  3397.            * is unsafe to assume that the search_state copies of 
  3398.            * variables saved on the backtracking stack are valid
  3399.            * -- so read their values from the backtracking stack.
  3400.            *
  3401.            * This lets us use one generation fewer stack saves in
  3402.            * the call-graph of a search.
  3403.            */
  3404.           
  3405.         while_non_det_options:
  3406. #ifdef RX_DEBUG_0
  3407.           ++search_state.lines_found;
  3408.           if (rx_debug_trace)
  3409.         fprintf (stderr, "@@@ %d calls %d @@@\n",
  3410.              search_state.line_no, search_state.lines_found);
  3411.           
  3412.           search_state.line_no = search_state.lines_found;
  3413. #endif
  3414.           
  3415.           if (bf->df->next_same_super_edge[0] == bf->first_df)
  3416.         {
  3417.           /* This is a tail-call optimization -- we don't recurse
  3418.            * for the last of the possible futures.
  3419.            */
  3420.           search_state.ifr = (bf->df->effects
  3421.                       ? &bf->df->side_effects_frame
  3422.                       : &bf->df->future_frame);
  3423.           
  3424.           rx_unlock_superstate (&rxb->rx, search_state.super);
  3425.           POP(search_state.backtrack_stack,
  3426.               search_state.backtrack_frame_bytes);
  3427. #ifdef RX_DEBUG
  3428.           --search_state.backtrack_depth;
  3429. #endif
  3430.           goto restart;
  3431.         }
  3432.           else
  3433.         {
  3434.           if (search_state.counter_stack)
  3435.             {
  3436.               struct rx_counter_frame * old_cf
  3437.             = ((struct rx_counter_frame *)search_state.counter_stack->sp);
  3438.               struct rx_counter_frame * cf;
  3439.               PUSH(search_state.counter_stack, sizeof (struct rx_counter_frame));
  3440.               cf = ((struct rx_counter_frame *)search_state.counter_stack->sp);
  3441.               cf->tag = old_cf->tag;
  3442.               cf->val = old_cf->val;
  3443.               cf->inherited_from = old_cf;
  3444.               cf->cdr = 0;
  3445.             }            
  3446.           /* `Call' this test-match block */
  3447.           search_state.ifr = (bf->df->effects
  3448.                       ? &bf->df->side_effects_frame
  3449.                       : &bf->df->future_frame);
  3450.           goto recurse_test_match;
  3451.         }
  3452.           
  3453.           /* Returns in this block are accomplished by
  3454.            * goto test_do_return.  There are two cases.
  3455.            * If there is some search-stack left,
  3456.            * then it is a return from a `recursive' call.
  3457.            * If there is no search-stack left, then
  3458.            * we should return to the fastmap/search loop.
  3459.            */
  3460.           
  3461.         test_do_return:
  3462.           
  3463.           if (!search_state.backtrack_stack)
  3464.         {
  3465. #ifdef RX_DEBUG_0
  3466.           if (rx_debug_trace)
  3467.             fprintf (stderr, "!!! %d bails returning %d !!!\n",
  3468.                  search_state.line_no, search_state.test_ret);
  3469. #endif
  3470.           
  3471.           /* No more search-stack -- this test is done. */
  3472.           if (search_state.test_ret)
  3473.             goto return_from_test_match;
  3474.           else
  3475.             goto error_in_testing_match;
  3476.         }
  3477.           
  3478.           /* Returning from a recursive call to 
  3479.            * the test match block:
  3480.            */
  3481.           
  3482.           bf = ((struct rx_backtrack_frame *)
  3483.             search_state.backtrack_stack->sp);
  3484. #ifdef RX_DEBUG_0
  3485.           if (rx_debug_trace)
  3486.         fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
  3487.              search_state.line_no,
  3488.              search_state.test_ret,
  3489.              bf->stk_search_state.line_no);
  3490. #endif
  3491.           
  3492.           while (search_state.counter_stack
  3493.              && (!bf->counter_stack_sp
  3494.              || (bf->counter_stack_sp
  3495.                  != search_state.counter_stack->sp)))
  3496.         {
  3497.           POP(search_state.counter_stack,
  3498.               sizeof (struct rx_counter_frame));
  3499.         }
  3500.           
  3501.           if (search_state.test_ret == rx_test_error)
  3502.         {
  3503.           POP (search_state.backtrack_stack,
  3504.                search_state.backtrack_frame_bytes);
  3505.           goto test_do_return;
  3506.         }
  3507.           
  3508.           /* If a non-longest match was found and that is good 
  3509.            * enough, return immediately.
  3510.            */
  3511.           if (   (search_state.test_ret == rx_test_found_first)
  3512.           && search_state.first_found)
  3513.         {
  3514.           rx_unlock_superstate (&rxb->rx, bf->stk_super);
  3515.           POP (search_state.backtrack_stack,
  3516.                search_state.backtrack_frame_bytes);
  3517.           goto test_do_return;
  3518.         }
  3519.           
  3520.           search_state.test_ret = bf->stk_test_ret;
  3521.           search_state.last_l = bf->stk_last_l;
  3522.           search_state.last_r = bf->stk_last_r;
  3523.           bf->df = bf->df->next_same_super_edge[0];
  3524.           search_state.super = bf->stk_super;
  3525.           search_state.c = bf->stk_c;
  3526. #ifdef RX_DEBUG_0
  3527.           search_state.line_no = bf->stk_search_state.line_no;
  3528. #endif
  3529.           
  3530.           if (rxb->match_regs_on_stack)
  3531.         {
  3532.           int x;
  3533.           regoff_t * stk =
  3534.             (regoff_t *)((char *)bf + sizeof (*bf));
  3535.           for (x = 0; x <= search_state.last_l; ++x)
  3536.             search_state.lparen[x] = stk[x];
  3537.           stk += x;
  3538.           for (x = 0; x <= search_state.last_r; ++x)
  3539.             search_state.rparen[x] = stk[x];
  3540.         }
  3541.           
  3542.           {
  3543.         int x;
  3544.           try_burst_2:
  3545.         x = get_burst (&bf->stk_test_pos, app_closure, stop);
  3546.         switch (x)
  3547.           {
  3548.           case rx_get_burst_continuation:
  3549.             search_state.saved_bf = bf;
  3550.             test_pc = rx_test_backtrack_return;
  3551.             goto test_return_continuation;
  3552.           resume_continuation_3:
  3553.             bf = search_state.saved_bf;
  3554.             goto try_burst_2;
  3555.           case rx_get_burst_no_more:
  3556.             /* Since we've been here before, it is some kind of
  3557.              * error that we can't return.
  3558.              */
  3559.           case rx_get_burst_error:
  3560.             search_state.test_ret = rx_test_internal_error;
  3561.             goto test_do_return;
  3562.           case rx_get_burst_ok:
  3563.             break;
  3564.           }
  3565.           }
  3566.           search_state.test_pos = bf->stk_test_pos;
  3567.           goto while_non_det_options;
  3568.         }
  3569.         
  3570.         
  3571.       case rx_cache_miss:
  3572.         /* Because the superstate NFA is lazily constructed,
  3573.          * and in fact may erode from underneath us, we sometimes
  3574.          * have to construct the next instruction from the hard way.
  3575.          * This invokes one step in the lazy-conversion.
  3576.          */
  3577.         search_state.ifr = rx_handle_cache_miss (&rxb->rx,
  3578.                              search_state.super,
  3579.                              search_state.c,
  3580.                              search_state.ifr->data_2);
  3581.         if (!search_state.ifr)
  3582.           {
  3583.         search_state.test_ret = rx_test_internal_error;
  3584.         goto test_do_return;
  3585.           }
  3586.         goto restart;
  3587.         
  3588.       case rx_backtrack:
  3589.         /* RX_BACKTRACK means that we've reached the empty
  3590.          * superstate, indicating that match can't succeed
  3591.          * from this point.
  3592.          */
  3593.         goto test_do_return;
  3594.         
  3595.       case rx_next_char:
  3596.       case rx_error_inx:
  3597.       case rx_num_instructions:
  3598.         search_state.ret_val = 0;
  3599.         goto test_do_return;
  3600.       }
  3601.     goto pseudo_while_1;
  3602.       }
  3603.     
  3604.     /* Healthy exits from the test-match loop do a 
  3605.      * `goto return_from_test_match'   On the other hand, 
  3606.      * we might end up here.
  3607.      */
  3608.   error_in_testing_match:
  3609.     test_state = rx_test_error;
  3610.     goto test_returns_to_search;
  3611.     
  3612.     /***** fastmap/search loop body
  3613.      *          considering the results testing for a match
  3614.      */
  3615.     
  3616.   return_from_test_match:
  3617.     
  3618.     if (search_state.best_last_l >= 0)
  3619.       {
  3620.     if (regs && (regs->start != search_state.best_lparen))
  3621.       {
  3622.         bcopy (search_state.best_lparen, regs->start,
  3623.            regs->num_regs * sizeof (int));
  3624.         bcopy (search_state.best_rparen, regs->end,
  3625.            regs->num_regs * sizeof (int));
  3626.       }
  3627.     if (regs && !rxb->no_sub)
  3628.       {
  3629.         int q;
  3630.         int bound = (regs->num_regs > search_state.num_regs
  3631.              ? regs->num_regs
  3632.              : search_state.num_regs);
  3633.         regoff_t * s = regs->start;
  3634.         regoff_t * e = regs->end;
  3635.         for (q = search_state.best_last_l + 1;  q < bound; ++q)
  3636.           s[q] = e[q] = -1;
  3637.       }
  3638.     search_state.ret_val = search_state.best_lparen[0];
  3639.     test_state = rx_test_ok;
  3640.     goto test_returns_to_search;
  3641.       }
  3642.     else
  3643.       {
  3644.     test_state = rx_test_fail;
  3645.     goto test_returns_to_search;
  3646.       }
  3647.     
  3648.   test_return_continuation:
  3649.     search_state.test_match_resume_pt = test_pc;
  3650.     test_state = rx_test_continuation;
  3651.     goto test_returns_to_search;
  3652.   }
  3653. }
  3654.  
  3655.  
  3656.  
  3657. #endif /* RX_WANT_RX_DEFS */
  3658.  
  3659.  
  3660.  
  3661. #else /* RX_WANT_SE_DEFS */
  3662.   /* Integers are used to represent side effects.
  3663.    *
  3664.    * Simple side effects are given negative integer names by these enums.
  3665.    * 
  3666.    * Non-negative names are reserved for complex effects.
  3667.    *
  3668.    * Complex effects are those that take arguments.  For example, 
  3669.    * a register assignment associated with a group is complex because
  3670.    * it requires an argument to tell which group is being matched.
  3671.    * 
  3672.    * The integer name of a complex effect is an index into rxb->se_params.
  3673.    */
  3674.  
  3675.   RX_DEF_SE(1, re_se_try, = -1)        /* Epsilon from start state */
  3676.  
  3677.   RX_DEF_SE(0, re_se_pushback, = re_se_try - 1)
  3678.   RX_DEF_SE(0, re_se_push0, = re_se_pushback -1)
  3679.   RX_DEF_SE(0, re_se_pushpos, = re_se_push0 - 1)
  3680.   RX_DEF_SE(0, re_se_chkpos, = re_se_pushpos -1)
  3681.   RX_DEF_SE(0, re_se_poppos, = re_se_chkpos - 1)
  3682.  
  3683.   RX_DEF_SE(1, re_se_at_dot, = re_se_poppos - 1)    /* Emacs only */
  3684.   RX_DEF_SE(0, re_se_syntax, = re_se_at_dot - 1) /* Emacs only */
  3685.   RX_DEF_SE(0, re_se_not_syntax, = re_se_syntax - 1) /* Emacs only */
  3686.  
  3687.   RX_DEF_SE(1, re_se_begbuf, = re_se_not_syntax - 1) /* match beginning of buffer */
  3688.   RX_DEF_SE(1, re_se_hat, = re_se_begbuf - 1) /* match beginning of line */
  3689.  
  3690.   RX_DEF_SE(1, re_se_wordbeg, = re_se_hat - 1) 
  3691.   RX_DEF_SE(1, re_se_wordbound, = re_se_wordbeg - 1)
  3692.   RX_DEF_SE(1, re_se_notwordbound, = re_se_wordbound - 1)
  3693.  
  3694.   RX_DEF_SE(1, re_se_wordend, = re_se_notwordbound - 1)
  3695.   RX_DEF_SE(1, re_se_endbuf, = re_se_wordend - 1)
  3696.  
  3697.   /* This fails except at the end of a line. 
  3698.    * It deserves to go here since it is typicly one of the last steps 
  3699.    * in a match.
  3700.    */
  3701.   RX_DEF_SE(1, re_se_dollar, = re_se_endbuf - 1)
  3702.  
  3703.   /* Simple effects: */
  3704.   RX_DEF_SE(1, re_se_fail, = re_se_dollar - 1)
  3705.  
  3706.   /* Complex effects.  These are used in the 'se' field of 
  3707.    * a struct re_se_params.  Indexes into the se array
  3708.    * are stored as instructions on nfa edges.
  3709.    */
  3710.   RX_DEF_CPLX_SE(1, re_se_win, = 0)
  3711.   RX_DEF_CPLX_SE(1, re_se_lparen, = re_se_win + 1)
  3712.   RX_DEF_CPLX_SE(1, re_se_rparen, = re_se_lparen + 1)
  3713.   RX_DEF_CPLX_SE(0, re_se_backref, = re_se_rparen + 1)
  3714.   RX_DEF_CPLX_SE(0, re_se_iter, = re_se_backref + 1) 
  3715.   RX_DEF_CPLX_SE(0, re_se_end_iter, = re_se_iter + 1)
  3716.   RX_DEF_CPLX_SE(0, re_se_tv, = re_se_end_iter + 1)
  3717.  
  3718. #endif
  3719.  
  3720. #endif
  3721.