home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / GREP15AS.ZIP / DFA.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  22.6 KB  |  584 lines

  1. /* dfa.h - declarations for GNU deterministic regexp compiler
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.                       Written June, 1988 by Mike Haertel
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 1, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* MS-DOS port (c) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>
  20.  
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.
  28.  
  29.    $Header: e:/gnu/grep/RCS/dfa.h 1.5.0.5 90/09/21 11:48:25 tho Exp $  */
  30.  
  31. #ifdef MSDOS
  32. #define LONG long
  33. #define ONE 1L
  34. #else /* not MSDOS */
  35. #define LONG int
  36. #define ONE 1
  37. #endif /* not MSDOS */
  38.  
  39. #ifdef USG
  40. #include <string.h>
  41.  
  42. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  43. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  44. #define bzero(s,n)    memset((s),0,(n))
  45. #define index strchr
  46. #define rindex strrchr
  47.  
  48. #else
  49. #include <strings.h>
  50. extern char *strchr(), *strrchr(), *memcpy();
  51. #endif
  52.  
  53. #ifdef __STDC__
  54.  
  55. /* Missing include files for GNU C. */
  56. /* #include <stdlib.h> */
  57. #ifndef MSDOS
  58. typedef int size_t;            /* long !!!! */
  59. extern void *calloc(int, size_t);
  60. extern void *malloc(size_t);
  61. extern void *realloc(void *, size_t);
  62. extern void free(void *);
  63. #endif /* MSDOS */
  64.  
  65. #ifndef USG
  66. extern char *bcopy(), *bzero();
  67. #endif /* not USG */
  68.  
  69. #ifdef SOMEDAY
  70. #define ISALNUM(c) isalnum(c)
  71. #define ISALPHA(c) isalpha(c)
  72. #define ISUPPER(c) isupper(c)
  73. #else
  74. #define ISALNUM(c) (isascii(c) && isalnum(c))
  75. #define ISALPHA(c) (isascii(c) && isalpha(c))
  76. #define ISUPPER(c) (isascii(c) && isupper(c))
  77. #endif
  78.  
  79. #else /* not __STDC__ */
  80.  
  81. #define const
  82. typedef int size_t;
  83. extern char *calloc(), *malloc(), *realloc();
  84. extern void free();
  85.  
  86. extern char *bcopy(), *bzero();
  87.  
  88. #define ISALNUM(c) (isascii(c) && isalnum(c))
  89. #define ISALPHA(c) (isascii(c) && isalpha(c))
  90. #define ISUPPER(c) (isascii(c) && isupper(c))
  91.  
  92. #endif /* not __STDC__ */
  93.  
  94.  
  95. /* If this bit is set, plain parentheses serve as grouping, and backslash
  96.      parentheses are needed for literal searching.
  97.    If not set, backslash-parentheses are grouping, and plain parentheses
  98.      are for literal searching.  */
  99. #define RE_NO_BK_PARENS    ONE
  100.  
  101. /* If this bit is set, plain | serves as the `or'-operator, and \| is a 
  102.      literal.
  103.    If not set, \| serves as the `or'-operator, and | is a literal.  */
  104. #define RE_NO_BK_VBAR (ONE << 1)
  105.  
  106. /* If this bit is not set, plain + or ? serves as an operator, and \+, \? are 
  107.      literals.
  108.    If set, \+, \? are operators and plain +, ? are literals.  */
  109. #define RE_BK_PLUS_QM (ONE << 2)
  110.  
  111. /* If this bit is set, | binds tighter than ^ or $.
  112.    If not set, the contrary.  */
  113. #define RE_TIGHT_VBAR (ONE << 3)
  114.  
  115. /* If this bit is set, then treat newline as an OR operator.
  116.    If not set, treat it as a normal character.  */
  117. #define RE_NEWLINE_OR (ONE << 4)
  118.  
  119. /* If this bit is set, then special characters may act as normal
  120.    characters in some contexts. Specifically, this applies to:
  121.     ^ -- only special at the beginning, or after ( or |;
  122.     $ -- only special at the end, or before ) or |;
  123.     *, +, ? -- only special when not after the beginning, (, or |.
  124.    If this bit is not set, special characters (such as *, ^, and $)
  125.    always have their special meaning regardless of the surrounding
  126.    context.  */
  127. #define RE_CONTEXT_INDEP_OPS (ONE << 5)
  128.  
  129. /* If this bit is not set, then \ before anything inside [ and ] is taken as 
  130.      a real \.
  131.    If set, then such a \ escapes the following character.  This is a
  132.      special case for awk.  */
  133. #define RE_AWK_CLASS_HACK (ONE << 6)
  134.  
  135. /* If this bit is set, then \{ and \} or { and } serve as interval operators.
  136.    If not set, then \{ and \} and { and } are treated as literals.  */
  137. #define RE_INTERVALS (ONE << 7)
  138.  
  139. /* If this bit is not set, then \{ and \} serve as interval operators and 
  140.      { and } are literals.
  141.    If set, then { and } serve as interval operators and \{ and \} are 
  142.      literals.  */
  143. #define RE_NO_BK_CURLY_BRACES (ONE << 8)
  144.  
  145. /* If this bit is set, then character classes are supported; they are:
  146.      [:alpha:],    [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
  147.      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  148.    If not set, then character classes are not supported.  */
  149. #define RE_CHAR_CLASSES (ONE << 9)
  150.  
  151. /* If this bit is set, then the dot re doesn't match a null byte.
  152.    If not set, it does.  */
  153. #define RE_DOT_NOT_NULL (ONE << 10)
  154.  
  155. /* If this bit is set, then [^...] doesn't match a newline.
  156.    If not set, it does.  */
  157. #define RE_HAT_NOT_NEWLINE (ONE << 11)
  158.  
  159. /* If this bit is set, back references are recognized.
  160.    If not set, they aren't.  */
  161. #define RE_NO_BK_REFS (ONE << 12)
  162.  
  163. /* If this bit is set, back references must refer to a preceding
  164.    subexpression.  If not set, a back reference to a nonexistent
  165.    subexpression is treated as literal characters.  */
  166. #define RE_NO_EMPTY_BK_REF (ONE << 13)
  167.  
  168. /* If this bit is set, bracket expressions can't be empty.  
  169.    If it is set, they can be empty.  */
  170. #define RE_NO_EMPTY_BRACKETS (ONE << 14)
  171.  
  172. /* If this bit is set, then *, +, ? and { cannot be first in an re or
  173.    immediately after a |, or a (.  Furthermore, a | cannot be first or
  174.    last in an re, or immediately follow another | or a (.  Also, a ^
  175.    cannot appear in a nonleading position and a $ cannot appear in a
  176.    nontrailing position (outside of bracket expressions, that is).  */
  177. #define RE_CONTEXTUAL_INVALID_OPS (ONE << 15)
  178.  
  179. /* If this bit is set, then +, ? and | aren't recognized as operators.
  180.    If it's not, they are.  */
  181. #define RE_LIMITED_OPS (ONE << 16)
  182.  
  183. /* If this bit is set, then an ending range point has to collate higher
  184.      or equal to the starting range point.
  185.    If it's not set, then when the ending range point collates higher
  186.      than the starting range point, the range is just considered empty.  */
  187. #define RE_NO_EMPTY_RANGES (ONE << 17)
  188.  
  189. /* If this bit is set, then a hyphen (-) can't be an ending range point.
  190.    If it isn't, then it can.  */
  191. #define RE_NO_HYPHEN_RANGE_END (ONE << 18)
  192.  
  193.  
  194. /* Define combinations of bits for the standard possibilities.  */
  195. #define RE_SYNTAX_POSIX_AWK (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
  196.             | RE_CONTEXT_INDEP_OPS)
  197. #define RE_SYNTAX_AWK (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
  198.             | RE_CONTEXT_INDEP_OPS | RE_AWK_CLASS_HACK)
  199. #define RE_SYNTAX_EGREP (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
  200.             | RE_CONTEXT_INDEP_OPS | RE_NEWLINE_OR)
  201. #define RE_SYNTAX_GREP (RE_BK_PLUS_QM | RE_NEWLINE_OR)
  202. #define RE_SYNTAX_EMACS 0
  203. #define RE_SYNTAX_POSIX_BASIC (RE_INTERVALS | RE_BK_PLUS_QM         \
  204.             | RE_CHAR_CLASSES | RE_DOT_NOT_NULL         \
  205.                         | RE_HAT_NOT_NEWLINE | RE_NO_EMPTY_BK_REF     \
  206.                         | RE_NO_EMPTY_BRACKETS | RE_LIMITED_OPS        \
  207.                         | RE_NO_EMPTY_RANGES | RE_NO_HYPHEN_RANGE_END)    
  208.                         
  209. #define RE_SYNTAX_POSIX_EXTENDED (RE_INTERVALS | RE_NO_BK_CURLY_BRACES       \
  210.             | RE_NO_BK_VBAR | RE_NO_BK_PARENS            \
  211.                         | RE_HAT_NOT_NEWLINE | RE_CHAR_CLASSES            \
  212.                         | RE_NO_EMPTY_BRACKETS | RE_CONTEXTUAL_INVALID_OPS \
  213.                         | RE_NO_BK_REFS | RE_NO_EMPTY_RANGES            \
  214.                         | RE_NO_HYPHEN_RANGE_END)
  215.  
  216. /* The NULL pointer. */
  217. #ifndef MSDOS
  218. #define NULL 0                /* we have it in stdio.h */
  219. #endif /* not MSDOS */
  220.  
  221. /* Number of bits in an unsigned char. */
  222. #define CHARBITS 8
  223.  
  224. /* First integer value that is greater than any character code. */
  225. #define _NOTCHAR (1 << CHARBITS)
  226.  
  227. /* INTBITS need not be exact, just a lower bound. */
  228. #define INTBITS (CHARBITS * sizeof (int))
  229.  
  230. /* Number of ints required to hold a bit for every character. */
  231. #define _CHARSET_INTS ((_NOTCHAR + INTBITS - 1) / INTBITS)
  232.  
  233. /* Sets of unsigned characters are stored as bit vectors in arrays of ints. */
  234. typedef int _charset[_CHARSET_INTS];
  235.  
  236. /* The regexp is parsed into an array of tokens in postfix form.  Some tokens
  237.    are operators and others are terminal symbols.  Most (but not all) of these
  238.    codes are returned by the lexical analyzer. */
  239. #ifdef __STDC__
  240.  
  241. typedef enum
  242. {
  243.   _END = -1,            /* _END is a terminal symbol that matches the
  244.                    end of input; any value of _END or less in
  245.                    the parse tree is such a symbol.  Accepting
  246.                    states of the DFA are those that would have
  247.                    a transition on _END. */
  248.  
  249.   /* Ordinary character values are terminal symbols that match themselves. */
  250.  
  251.   _EMPTY = _NOTCHAR,        /* _EMPTY is a terminal symbol that matches
  252.                    the empty string. */
  253.  
  254.   _BACKREF,            /* _BACKREF is generated by \<digit>; it
  255.                    it not completely handled.  If the scanner
  256.                    detects a transition on backref, it returns
  257.                    a kind of "semi-success" indicating that
  258.                    the match will have to be verified with
  259.                    a backtracking matcher. */
  260.  
  261.   _BEGLINE,            /* _BEGLINE is a terminal symbol that matches
  262.                    the empty string if it is at the beginning
  263.                    of a line. */
  264.  
  265.   _ALLBEGLINE,            /* _ALLBEGLINE is a terminal symbol that
  266.                    matches the empty string if it is at the
  267.                    beginning of a line; _ALLBEGLINE applies
  268.                    to the entire regexp and can only occur
  269.                    as the first token thereof.  _ALLBEGLINE
  270.                    never appears in the parse tree; a _BEGLINE
  271.                    is prepended with _CAT to the entire
  272.                    regexp instead. */
  273.  
  274.   _ENDLINE,            /* _ENDLINE is a terminal symbol that matches
  275.                    the empty string if it is at the end of
  276.                    a line. */
  277.  
  278.   _ALLENDLINE,            /* _ALLENDLINE is to _ENDLINE as _ALLBEGLINE
  279.                    is to _BEGLINE. */
  280.  
  281.   _BEGWORD,            /* _BEGWORD is a terminal symbol that matches
  282.                    the empty string if it is at the beginning
  283.                    of a word. */
  284.  
  285.   _ENDWORD,            /* _ENDWORD is a terminal symbol that matches
  286.                    the empty string if it is at the end of
  287.                    a word. */
  288.  
  289.   _LIMWORD,            /* _LIMWORD is a terminal symbol that matches
  290.                    the empty string if it is at the beginning
  291.                    or the end of a word. */
  292.  
  293.   _NOTLIMWORD,            /* _NOTLIMWORD is a terminal symbol that
  294.                    matches the empty string if it is not at
  295.                    the beginning or end of a word. */
  296.  
  297.   _QMARK,            /* _QMARK is an operator of one argument that
  298.                    matches zero or one occurences of its
  299.                    argument. */
  300.  
  301.   _STAR,            /* _STAR is an operator of one argument that
  302.                    matches the Kleene closure (zero or more
  303.                    occurrences) of its argument. */
  304.  
  305.   _PLUS,            /* _PLUS is an operator of one argument that
  306.                    matches the positive closure (one or more
  307.                    occurrences) of its argument. */
  308.  
  309.   _CAT,                /* _CAT is an operator of two arguments that
  310.                    matches the concatenation of its
  311.                    arguments.  _CAT is never returned by the
  312.                    lexical analyzer. */
  313.  
  314.   _OR,                /* _OR is an operator of two arguments that
  315.                    matches either of its arguments. */
  316.  
  317.   _LPAREN,            /* _LPAREN never appears in the parse tree,
  318.                    it is only a lexeme. */
  319.  
  320.   _RPAREN,            /* _RPAREN never appears in the parse tree. */
  321.  
  322.   _SET                /* _SET and (and any value greater) is a
  323.                    terminal symbol that matches any of a
  324.                    class of characters. */
  325. } _token;
  326.  
  327. #else /* not __STDC__ */
  328.  
  329. typedef short _token;
  330.  
  331. #define _END -1
  332. #define _EMPTY _NOTCHAR
  333. #define _BACKREF (_EMPTY + 1)
  334. #define _BEGLINE (_EMPTY + 2)
  335. #define _ALLBEGLINE (_EMPTY + 3)
  336. #define _ENDLINE (_EMPTY + 4)
  337. #define _ALLENDLINE (_EMPTY + 5)
  338. #define _BEGWORD (_EMPTY + 6)
  339. #define _ENDWORD (_EMPTY + 7)
  340. #define _LIMWORD (_EMPTY + 8)
  341. #define _NOTLIMWORD (_EMPTY + 9)
  342. #define _QMARK (_EMPTY + 10)
  343. #define _STAR (_EMPTY + 11)
  344. #define _PLUS (_EMPTY + 12)
  345. #define _CAT (_EMPTY + 13)
  346. #define _OR (_EMPTY + 14)
  347. #define _LPAREN (_EMPTY + 15)
  348. #define _RPAREN (_EMPTY + 16)
  349. #define _SET (_EMPTY + 17)
  350.  
  351. #endif /* not __STDC__ */
  352.  
  353. /* Sets are stored in an array in the compiled regexp; the index of the
  354.    array corresponding to a given set token is given by _SET_INDEX(t). */
  355. #define _SET_INDEX(t) ((t) - _SET)
  356.  
  357. /* Sometimes characters can only be matched depending on the surrounding
  358.    context.  Such context decisions depend on what the previous character
  359.    was, and the value of the current (lookahead) character.  Context
  360.    dependent constraints are encoded as 8 bit integers.  Each bit that
  361.    is set indicates that the constraint succeeds in the corresponding
  362.    context.
  363.  
  364.    bit 7 - previous and current are newlines
  365.    bit 6 - previous was newline, current isn't
  366.    bit 5 - previous wasn't newline, current is
  367.    bit 4 - neither previous nor current is a newline
  368.    bit 3 - previous and current are word-constituents
  369.    bit 2 - previous was word-constituent, current isn't
  370.    bit 1 - previous wasn't word-constituent, current is
  371.    bit 0 - neither previous nor current is word-constituent
  372.  
  373.    Word-constituent characters are those that satisfy isalnum().
  374.  
  375.    The macro _SUCCEEDS_IN_CONTEXT determines whether a a given constraint
  376.    succeeds in a particular context.  Prevn is true if the previous character
  377.    was a newline, currn is true if the lookahead character is a newline.
  378.    Prevl and currl similarly depend upon whether the previous and current
  379.    characters are word-constituent letters. */
  380. #define _MATCHES_NEWLINE_CONTEXT(constraint, prevn, currn) \
  381.   ((constraint) & 1 << ((prevn) ? 2 : 0) + ((currn) ? 1 : 0) + 4)
  382. #define _MATCHES_LETTER_CONTEXT(constraint, prevl, currl) \
  383.   ((constraint) & 1 << ((prevl) ? 2 : 0) + ((currl) ? 1 : 0))
  384. #define _SUCCEEDS_IN_CONTEXT(constraint, prevn, currn, prevl, currl) \
  385.   (_MATCHES_NEWLINE_CONTEXT(constraint, prevn, currn)             \
  386.    && _MATCHES_LETTER_CONTEXT(constraint, prevl, currl))
  387.  
  388. /* The following macros give information about what a constraint depends on. */
  389. #define _PREV_NEWLINE_DEPENDENT(constraint) \
  390.   (((constraint) & 0xc0) >> 2 != ((constraint) & 0x30))
  391. #define _PREV_LETTER_DEPENDENT(constraint) \
  392.   (((constraint) & 0x0c) >> 2 != ((constraint) & 0x03))
  393.  
  394. /* Tokens that match the empty string subject to some constraint actually
  395.    work by applying that constraint to determine what may follow them,
  396.    taking into account what has gone before.  The following values are
  397.    the constraints corresponding to the special tokens previously defined. */
  398. #define _NO_CONSTRAINT 0xff
  399. #define _BEGLINE_CONSTRAINT 0xcf
  400. #define _ENDLINE_CONSTRAINT 0xaf
  401. #define _BEGWORD_CONSTRAINT 0xf2
  402. #define _ENDWORD_CONSTRAINT 0xf4
  403. #define _LIMWORD_CONSTRAINT 0xf6
  404. #define _NOTLIMWORD_CONSTRAINT 0xf9
  405.  
  406. /* States of the recognizer correspond to sets of positions in the parse
  407.    tree, together with the constraints under which they may be matched.
  408.    So a position is encoded as an index into the parse tree together with
  409.    a constraint. */
  410. typedef struct
  411. {
  412.   unsigned index;        /* Index into the parse array. */
  413.   unsigned constraint;        /* Constraint for matching this position. */
  414. } _position;
  415.  
  416. /* Sets of positions are stored as arrays. */
  417. typedef struct
  418. {
  419.   _position *elems;        /* Elements of this position set. */
  420.   int nelem;            /* Number of elements in this set. */
  421. } _position_set;
  422.  
  423. /* A state of the regexp consists of a set of positions, some flags,
  424.    and the token value of the lowest-numbered position of the state that
  425.    contains an _END token. */
  426. typedef struct
  427. {
  428.   int hash;            /* Hash of the positions of this state. */
  429.   _position_set elems;        /* Positions this state could match. */
  430.   char newline;            /* True if previous state matched newline. */
  431.   char letter;            /* True if previous state matched a letter. */
  432.   char backref;            /* True if this state matches a \<digit>. */
  433.   unsigned char constraint;    /* Constraint for this state to accept. */
  434.   int first_end;        /* Token value of the first _END in elems. */
  435. } _dfa_state;
  436.  
  437. /* If an r.e. is at most MUST_MAX characters long, we look for a string which
  438.    must appear in it; whatever's found is dropped into the struct reg. */
  439.  
  440. #define MUST_MAX    50
  441.  
  442. /* A compiled regular expression. */
  443. struct regexp
  444. {
  445.   /* Stuff built by the scanner. */
  446.   _charset *charsets;        /* Array of character sets for _SET tokens. */
  447.   int cindex;            /* Index for adding new charsets. */
  448.   int calloc;            /* Number of charsets currently allocated. */
  449.  
  450.   /* Stuff built by the parser. */
  451.   _token *tokens;        /* Postfix parse array. */
  452.   int tindex;            /* Index for adding new tokens. */
  453.   int talloc;            /* Number of tokens currently allocated. */
  454.   int depth;            /* Depth required of an evaluation stack
  455.                    used for depth-first traversal of the
  456.                    parse tree. */
  457.   int nleaves;            /* Number of leaves on the parse tree. */
  458.   int nregexps;            /* Count of parallel regexps being built
  459.                    with regparse(). */
  460.  
  461.   /* Stuff owned by the state builder. */
  462.   _dfa_state *states;        /* States of the regexp. */
  463.   int sindex;            /* Index for adding new states. */
  464.   int salloc;            /* Number of states currently allocated. */
  465.  
  466.   /* Stuff built by the structure analyzer. */
  467.   _position_set *follows;    /* Array of follow sets, indexed by position
  468.                    index.  The follow of a position is the set
  469.                    of positions containing characters that
  470.                    could conceivably follow a character
  471.                    matching the given position in a string
  472.                    matching the regexp.  Allocated to the
  473.                    maximum possible position index. */
  474.   int searchflag;        /* True if we are supposed to build a searching
  475.                    as opposed to an exact matcher.  A searching
  476.                    matcher finds the first and shortest string
  477.                    matching a regexp anywhere in the buffer,
  478.                    whereas an exact matcher finds the longest
  479.                    string matching, but anchored to the
  480.                    beginning of the buffer. */
  481.  
  482.   /* Stuff owned by the executor. */
  483.   int tralloc;            /* Number of transition tables that have
  484.                    slots so far. */
  485.   int trcount;            /* Number of transition tables that have
  486.                    actually been built. */
  487.   int **trans;            /* Transition tables for states that can
  488.                    never accept.  If the transitions for a
  489.                    state have not yet been computed, or the
  490.                    state could possibly accept, its entry in
  491.                    this table is NULL. */
  492.   int **realtrans;        /* Trans always points to realtrans + 1; this
  493.                    is so trans[-1] can contain NULL. */
  494.   int **fails;            /* Transition tables after failing to accept
  495.                    on a state that potentially could do so. */
  496.   int *success;            /* Table of acceptance conditions used in
  497.                    regexecute and computed in build_state. */
  498.   int *newlines;        /* Transitions on newlines.  The entry for a
  499.                    newline in any transition table is always
  500.                    -1 so we can count lines without wasting
  501.                    too many cycles.  The transition for a
  502.                    newline is stored separately and handled
  503.                    as a special case.  Newline is also used
  504.                    as a sentinel at the end of the buffer. */
  505.   char must[MUST_MAX];
  506.   int mustn;
  507. };
  508.  
  509. /* Some macros for user access to regexp internals. */
  510.  
  511. /* ACCEPTING returns true if s could possibly be an accepting state of r. */
  512. #define ACCEPTING(s, r) ((r).states[s].constraint)
  513.  
  514. /* ACCEPTS_IN_CONTEXT returns true if the given state accepts in the
  515.    specified context. */
  516. #define ACCEPTS_IN_CONTEXT(prevn, currn, prevl, currl, state, reg) \
  517.   _SUCCEEDS_IN_CONTEXT((reg).states[state].constraint,           \
  518.                prevn, currn, prevl, currl)
  519.  
  520. /* FIRST_MATCHING_REGEXP returns the index number of the first of parallel
  521.    regexps that a given state could accept.  Parallel regexps are numbered
  522.    starting at 1. */
  523. #define FIRST_MATCHING_REGEXP(state, reg) (-(reg).states[state].first_end)
  524.  
  525. /* Entry points. */
  526.  
  527. #ifdef __STDC__
  528.  
  529. /* Regsyntax() takes two arguments; the first sets the syntax bits described
  530.    earlier in this file, and the second sets the case-folding flag. */
  531. extern void regsyntax (LONG, int);
  532.  
  533. /* Compile the given string of the given length into the given struct regexp.
  534.    Final argument is a flag specifying whether to build a searching or an
  535.    exact matcher. */
  536. extern void regcompile(const char *, size_t, struct regexp *, int);
  537.  
  538. /* Execute the given struct regexp on the buffer of characters.  The
  539.    first char * points to the beginning, and the second points to the
  540.    first character after the end of the buffer, which must be a writable
  541.    place so a sentinel end-of-buffer marker can be stored there.  The
  542.    second-to-last argument is a flag telling whether to allow newlines to
  543.    be part of a string matching the regexp.  The next-to-last argument,
  544.    if non-NULL, points to a place to increment every time we see a
  545.    newline.  The final argument, if non-NULL, points to a flag that will
  546.    be set if further examination by a backtracking matcher is needed in
  547.    order to verify backreferencing; otherwise the flag will be cleared.
  548.    Returns NULL if no match is found, or a pointer to the first
  549.    character after the first & shortest matching string in the buffer. */
  550. extern char *regexecute(struct regexp *, char *, char *, int, int *, int *);
  551.  
  552. /* Free the storage held by the components of a struct regexp. */
  553. extern void regfree(struct regexp *);
  554.  
  555. /* Entry points for people who know what they're doing. */
  556.  
  557. /* Initialize the components of a struct regexp. */
  558. extern void reginit(struct regexp *);
  559.  
  560. /* Incrementally parse a string of given length into a struct regexp. */
  561. extern void regparse(const char *, size_t, struct regexp *);
  562.  
  563. /* Analyze a parsed regexp; second argument tells whether to build a searching
  564.    or an exact matcher. */
  565. extern void reganalyze(struct regexp *, int);
  566.  
  567. /* Compute, for each possible character, the transitions out of a given
  568.    state, storing them in an array of integers. */
  569. extern void regstate(int, struct regexp *, int []);
  570.  
  571. /* Error handling. */
  572.  
  573. /* Regerror() is called by the regexp routines whenever an error occurs.  It
  574.    takes a single argument, a NUL-terminated string describing the error.
  575.    The default regerror() prints the error message to stderr and exits.
  576.    The user can provide a different regfree() if so desired. */
  577. extern void regerror(const char *);
  578.  
  579. #else /* not __STDC__ */
  580. extern void regsyntax(), regcompile(), regfree(), reginit(), regparse();
  581. extern void reganalyze(), regstate(), regerror();
  582. extern char *regexecute();
  583. #endif /* not __STDC__ */
  584.