OSDN Git Service

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