home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / LESS177.ZIP / src / regex.h < prev    next >
C/C++ Source or Header  |  1992-07-18  |  18KB  |  476 lines

  1. /* Definitions for data structures and routines for the regular
  2.    expression library, version REPLACE-WITH-VERSION.
  3.  
  4.    Copyright (C) 1985, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #ifndef __REGEXP_LIBRARY_H__
  21. #define __REGEXP_LIBRARY_H__
  22.  
  23. /* POSIX says that <sys/types.h> must be included before <regex.h>.  */
  24.  
  25. /* The following bits are used to determine the regexp syntax we
  26.    recognize.  The set/not-set meanings are chosen so that Emacs syntax
  27.    remains the value 0.  The bits are given in alphabetical order, and
  28.    the definitions shifted by one from the previous bit; thus, when we
  29.    add or remove a bit, only one other definition need change.  */
  30. typedef unsigned reg_syntax_t;
  31.  
  32. /* If this bit is not set, then \ inside a bracket expression is literal.
  33.    If set, then such a \ quotes the following character.  */
  34. #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
  35.  
  36. /* If this bit is not set, then + and ? are operators, and \+ and \? are
  37.      literals. 
  38.    If set, then \+ and \? are operators and + and ? are literals.  */
  39. #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
  40.  
  41. /* If this bit is set, then character classes are supported.  They are:
  42.      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
  43.      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  44.    If not set, then character classes are not supported.  */
  45. #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
  46.  
  47. /* If this bit is set, then ^ and $ are always anchors (outside bracket
  48.      expressions).
  49.    If this bit is not set, then it depends:
  50.         ^  is an anchor if it is at the beginning of a regular
  51.            expression or after an open-group or an alternation operator;
  52.         $  is an anchor if it is at the end of a regular expression, or
  53.            before a close-group or an alternation operator.  
  54.    This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
  55.    POSIX now says that the behavior of * etc. in leading positions is
  56.    undefined.  We have already implemented a previous draft which
  57.    made those constructs invalid, so we may as well not change the code
  58.    back.  */
  59. #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
  60.  
  61. /* If this bit is set, then special characters are always special
  62.      regardless of where they are in the pattern.
  63.    If this bit is not set, then special characters are special only in
  64.      some contexts; otherwise they are ordinary.  Specifically, 
  65.      * + ? and intervals are only special when not after the beginning,
  66.      open-group, or alternation operator.  */
  67. #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
  68.  
  69. /* If this bit is set, then *, +, ?, and { cannot be first in an re or
  70.    immediately after an alternation or begin-group operator.
  71.    Furthermore, alternation cannot be first or last in an re, or
  72.    immediately follow another alternation or begin-group.  */
  73. #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
  74.  
  75. /* If this bit is set, then . matches a newline.
  76.    If not set, then it doesn't.  */
  77. #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
  78.  
  79. /* If this bit is set, then period doesn't match a null.
  80.    If not set, then it does.  */
  81. #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
  82.  
  83. /* If this bit is set, nonmatching lists [^...] do not match newline.
  84.    If not set, they do.  */
  85. #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
  86.  
  87. /* If this bit is set, either \{...\} or {...} defines an
  88.      interval, depending on RE_NO_BK_BRACES. 
  89.    If not set, \{, \}, {, and } are literals.  */
  90. #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
  91.  
  92. /* If this bit is set,  +, ? and | aren't recognized as operators.
  93.    If not set, they are.  */
  94. #define RE_LIMITED_OPS (RE_INTERVALS << 1)
  95.  
  96. /* If this bit is set, newline is an alternation operator.
  97.    If not set, newline is literal.  */
  98. #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
  99.  
  100. /* If this bit is set, newline in the pattern is an ordinary character.
  101.    If not set, newline before ^ or after $ allows the ^ or $ to be an
  102.      anchor.  */
  103. #define RE_NEWLINE_ORDINARY (RE_NEWLINE_ALT << 1)
  104.  
  105. /* If this bit is not set, then \{ and \} defines an interval,
  106.      and { and } are literals.
  107.    If set, then { and } defines an interval, and \{ and \} are literals.  */
  108. #define RE_NO_BK_BRACES (RE_NEWLINE_ORDINARY << 1)
  109.  
  110. /* If this bit is set, (...) defines a group, and \( and \) are literals.
  111.    If not set, \(...\) defines a group, and ( and ) are literals.  */
  112. #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
  113.  
  114. /* If this bit is set, then back references (i.e., \<digit>) are not
  115.      recognized.
  116.    If not set, then they are.  */
  117. #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
  118.  
  119. /* If this bit is set, then | is an alternation operator, and \| is literal. 
  120.    If not set, then \| is an alternation operator, and | is literal.  */
  121. #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
  122.  
  123. /* If this bit is set, then you can't have empty alternatives.
  124.    If not set, then you can.  */
  125. #define RE_NO_EMPTY_ALTS (RE_NO_BK_VBAR << 1)
  126.  
  127. /* If this bit is set, then you can't have empty groups.
  128.    If not set, then you can.  */
  129. #define RE_NO_EMPTY_GROUPS (RE_NO_EMPTY_ALTS << 1)
  130.  
  131. /* If this bit is set, then an ending range point has to collate higher
  132.      than or equal to the starting range point.
  133.    If not set, then when the ending range point collates higher than the
  134.      starting range point, we consider such a range to be empty.  */ 
  135. #define RE_NO_EMPTY_RANGES (RE_NO_EMPTY_GROUPS << 1)
  136.  
  137. /* If this bit is set, then all back references must refer to a preceding
  138.      subexpression.
  139.    If not set, then a back reference to a nonexistent subexpression is
  140.      treated as literal characters.  */ 
  141. #define RE_NO_MISSING_BK_REF (RE_NO_EMPTY_RANGES << 1)
  142.  
  143. /* If this bit is set, then Regex considers an unmatched close-group
  144.      operator to be the ordinary character parenthesis.
  145.    If not set, then an unmatched close-group operator is invalid.  */
  146. #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_MISSING_BK_REF << 1)
  147.  
  148. /* This global variable defines the particular regexp syntax to use (for
  149.    some interfaces).  When a regexp is compiled, the syntax used is
  150.    stored in the pattern buffer, so changing this does not affect
  151.    already-compiled regexps.  */
  152. extern reg_syntax_t obscure_syntax;
  153.  
  154.  
  155.  
  156. /* Define combinations of the above bits for the standard possibilities.
  157.    (The [[[ comments delimit what gets put into the Texinfo file.) */ 
  158. /* [[[begin syntaxes]]] */
  159. #define RE_SYNTAX_EMACS 0
  160.  
  161. #define RE_SYNTAX_POSIX_AWK                         \
  162.   (RE_CONTEXT_INDEP_ANCHORS | RE_CONTEXT_INDEP_OPS | RE_NO_BK_PARENS    \
  163.    | RE_NO_BK_VBAR) 
  164.  
  165. #define RE_SYNTAX_AWK                            \
  166.   (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_SYNTAX_POSIX_AWK)
  167.  
  168. #define RE_SYNTAX_GREP                            \
  169.   (RE_BK_PLUS_QM | RE_NEWLINE_ALT)
  170.  
  171. #define RE_SYNTAX_EGREP                            \
  172.   (RE_CONTEXT_INDEP_ANCHORS | RE_CONTEXT_INDEP_OPS            \
  173.    | RE_NEWLINE_ALT | RE_NO_BK_PARENS | RE_NO_BK_VBAR)
  174.  
  175. #define RE_SYNTAX_POSIX_BASIC                        \
  176.   (RE_CHAR_CLASSES | RE_DOT_NEWLINE                    \
  177.    | RE_DOT_NOT_NULL | RE_INTERVALS | RE_LIMITED_OPS            \
  178.    | RE_NEWLINE_ORDINARY | RE_NO_EMPTY_RANGES | RE_NO_MISSING_BK_REF)
  179.  
  180. #define RE_SYNTAX_POSIX_EXTENDED                    \
  181.   (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS                \
  182.    | RE_CONTEXT_INVALID_OPS | RE_DOT_NEWLINE | RE_DOT_NOT_NULL        \
  183.    | RE_INTERVALS | RE_NEWLINE_ORDINARY | RE_NO_BK_BRACES        \
  184.    | RE_NO_BK_PARENS | RE_NO_BK_REFS | RE_NO_BK_VBAR            \
  185.    | RE_NO_EMPTY_ALTS | RE_NO_EMPTY_GROUPS | RE_NO_EMPTY_RANGES        \
  186.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  187. /* [[[end syntaxes]]] */
  188.  
  189.  
  190.  
  191.  
  192. /* Maximum number of duplicates an interval can allow.  */
  193. #undef RE_DUP_MAX
  194. #define RE_DUP_MAX  ((1 << 15) - 1) 
  195.  
  196.  
  197. /* POSIX `cflags' bits (i.e., information for regcomp).  */
  198.  
  199. /* If this bit is set, then use extended regular expression syntax.
  200.    If not set, then use basic regular expression syntax.  */
  201. #define REG_EXTENDED 1
  202.  
  203. /* If this bit is set, then ignore case when matching.
  204.    If not set, then case is significant.  */
  205. #define REG_ICASE (REG_EXTENDED << 1)
  206.  
  207. /* If this bit is set, then anchors do not match at newline
  208.      characters in the string.
  209.    If not set, then anchors do match at newlines.  */
  210. #define REG_NEWLINE (REG_ICASE << 1)
  211.  
  212. /* If this bit is set, then report only success or fail in regexec.
  213.    If not set, then returns differ between not matching and errors.  */
  214. #define REG_NOSUB (REG_NEWLINE << 1)
  215.  
  216.  
  217. /* POSIX `eflags' bits (i.e., information for regexec).  */
  218.  
  219. /* If this bit is set, then the beginning-of-line operator doesn't match
  220.      the beginning of the string (presumably because it's not the
  221.      beginning of a line).
  222.    If not set, then the beginning-of-line operator does match the
  223.      beginning of the string.  */
  224. #define REG_NOTBOL 1
  225.  
  226. /* Like REG_NOTBOL, except for the end-of-line.  */
  227. #define REG_NOTEOL (1 << 1)
  228.  
  229.  
  230. /* If any error codes are removed, changed, or added, update the
  231.    `re_error_msg' table in regex.c.  */
  232. typedef enum
  233. {
  234.   REG_NOERROR = 0,    /* Success.  */
  235.   REG_NOMATCH,        /* Didn't find a match (for regexec).  */
  236.  
  237.   /* POSIX regcomp return error codes.  (In the order listed in the
  238.      standard.)  */
  239.   REG_BADPAT,        /* Invalid pattern.  */
  240.   REG_ECOLLATE,        /* Not implemented.  */
  241.   REG_ECTYPE,        /* Invalid character class name.  */
  242.   REG_EESCAPE,        /* Trailing backslash.  */
  243.   REG_ESUBREG,        /* Invalid back reference.  */
  244.   REG_EBRACK,        /* Unmatched left bracket.  */
  245.   REG_EPAREN,        /* Parenthesis imbalance.  */ 
  246.   REG_EBRACE,        /* Unmatched \{.  */
  247.   REG_BADBR,        /* Invalid contents of \{\}.  */
  248.   REG_ERANGE,        /* Invalid range end.  */
  249.   REG_ESPACE,        /* Ran out of memory.  */
  250.   REG_BADRPT,        /* No preceding re for repetition op.  */
  251.  
  252.   /* Error codes we've added.  */
  253.   REG_EEND,        /* Premature end.  */
  254.   REG_ESIZE,        /* Compiled pattern bigger than 2^16 bytes.  */
  255.   REG_ERPAREN        /* Unmatched ) or \); not returned from regcomp.  */
  256. } reg_errcode_t;
  257.  
  258.  
  259.  
  260.  
  261. /* This data structure represents a compiled pattern.  Before calling
  262.    the pattern compiler, the fields `buffer', `allocated', `fastmap',
  263.    `translate', and `no_sub' can be set.  After the pattern has been
  264.    compiled, the `re_nsub' field is available.  All other fields are
  265.    private to the regex routines.  */
  266.  
  267. struct re_pattern_buffer
  268. {
  269. /* [[[begin pattern_buffer]]] */
  270.     /* Space that holds the compiled pattern.  It is declared as
  271.           `unsigned char *' because its elements are
  272.            sometimes used as array indexes.  */
  273.   unsigned char *buffer;
  274.  
  275.     /* Number of bytes to which `buffer' points.  */
  276.   unsigned long allocated;
  277.  
  278.     /* Number of bytes actually used in `buffer'.  */
  279.   unsigned long used;    
  280.  
  281.         /* Syntax setting with which the pattern was compiled.  */
  282.   reg_syntax_t syntax;
  283.  
  284.         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
  285.            the fastmap, if there is one, to skip over impossible
  286.            starting points for matches.  */
  287.   char *fastmap;
  288.  
  289.         /* Either a translate table to apply to all characters before
  290.            comparing them, or zero for no translation.  The translation
  291.            is applied to a pattern when it is compiled and to a string
  292.            when it is matched.  */
  293.   char *translate;
  294.  
  295.     /* Number of subexpressions found by the compiler.  */
  296.   size_t re_nsub;
  297.  
  298.         /* Set to 1 by re_compile_fastmap if this pattern can match the
  299.            null string; 0 prevents the searcher from matching it with
  300.            the null string.  Set to 2 if it might match the null string
  301.            either at the end of a search range or just before a
  302.            character listed in the fastmap.  */
  303.   unsigned can_be_null : 2;
  304.  
  305.         /* Set to zero when regex_compile compiles a pattern; set to one
  306.            by re_compile_fastmap when it updates the fastmap, if any.  */
  307.   unsigned fastmap_accurate : 1;
  308.  
  309.         /* If set, regexec reports only success or failure and does not
  310.            return anything in pmatch.  */
  311.   unsigned no_sub : 1;
  312.  
  313.         /* If set, a beginning-of-line anchor doesn't match at the
  314.            beginning of the string.  */ 
  315.   unsigned not_bol : 1;
  316.  
  317.         /* Similarly for an end-of-line anchor.  */
  318.   unsigned not_eol : 1;
  319.  
  320.         /* If true, an anchor at a newline matches.  */
  321.   unsigned newline_anchor : 1;
  322.  
  323.         /* If set, re_match_2 assumes a non-null REGS argument is
  324.            initialized.  If not set, REGS is initialized to the max of
  325.            RE_NREGS and re_nsub + 1 registers.  */
  326.   unsigned caller_allocated_regs : 1;
  327. /* [[[end pattern_buffer]]] */
  328. };
  329.  
  330. typedef struct re_pattern_buffer regex_t;
  331.  
  332.  
  333. /* search.c (search_buffer) in Emacs needs this one opcode value.  It is
  334.    defined both in `regex.c' and here.  */
  335. #define RE_EXACTN_VALUE 1
  336.  
  337.  
  338.  
  339.  
  340. /* Type for byte offsets within the string.  POSIX mandates us defining
  341.    this.  */
  342. typedef int regoff_t;
  343.  
  344.  
  345. /* This is the structure we store register match data in.  See
  346.    regex.texinfo for a full description of what registers match.  */
  347. struct re_registers
  348. {
  349.   unsigned num_regs;
  350.   regoff_t *start;
  351.   regoff_t *end;
  352. };
  353.  
  354.  
  355. /* If `caller_allocated_regs' is zero in the pattern buffer, re_match_2
  356.    returns information about this many registers.  */
  357. #ifndef RE_NREGS
  358. #define RE_NREGS 30
  359. #endif
  360.  
  361.  
  362. /* POSIX specification for registers.  Aside from the different names than
  363.    `re_registers', POSIX uses an array of structures, instead of a
  364.    structure of arrays.  */
  365. typedef struct
  366. {
  367.   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
  368.   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
  369. } regmatch_t;
  370.  
  371.  
  372.  
  373.  
  374. /* Declarations for routines.  */
  375.  
  376. #if __STDC__
  377.  
  378. /* Sets the current syntax to SYNTAX.  You can also simply assign to the
  379.    `obscure_syntax' variable.  */
  380. extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
  381.  
  382. /* Compile the regular expression PATTERN, with length LENGTH
  383.    and syntax given by the global `obscure_syntax', into the buffer
  384.    BUFFER.  Return NULL if successful, and an error string if not.  */
  385. extern const char *re_compile_pattern (const char *pattern, int length,
  386.                                        struct re_pattern_buffer *buffer);
  387.  
  388.  
  389. /* Compile a fastmap for the compiled pattern in BUFFER; used to
  390.    accelerate searches.  Return 0 if successful and -2 if was an
  391.    internal error.  */
  392. extern int re_compile_fastmap (struct re_pattern_buffer *buffer);
  393.  
  394.  
  395. /* Search in the string STRING (with length LENGTH) for the pattern
  396.    compiled into BUFFER.  Start searching at position START, for RANGE
  397.    characters.  Return the starting position of the match, -1 for no
  398.    match, or -2 for an internal error.  Also return register
  399.    information in REGS (if REGS and BUFFER->no_sub are nonzero).  */
  400. extern int re_search (struct re_pattern_buffer *buffer,
  401.                       const char *string, int length,
  402.                       int start, int range, 
  403.               struct re_registers *regs);
  404.  
  405.  
  406. /* Like `re_search', but search in the concatenation of STRING1 and
  407.    STRING2.  Also, stop searching at index START + STOP.  */
  408. extern int re_search_2 (struct re_pattern_buffer *buffer,
  409.                         const char *string1, int length1,
  410.                 const char *string2, int length2,
  411.                         int start, int range, 
  412.                         struct re_registers *regs,
  413.                         int stop);
  414.  
  415.  
  416. /* Like `re_search', but return how many characters in STRING the regexp
  417.    in BUFFER matched, starting at position START.  */
  418. extern int re_match (const struct re_pattern_buffer *buffer,
  419.                      const char *string, int length,
  420.                      int start, struct re_registers *regs);
  421.  
  422.  
  423. /* Relates to `re_match' as `re_search_2' relates to `re_search'.  */
  424. extern int re_match_2 (const struct re_pattern_buffer *buffer,
  425.                        const char *string1, int length1,
  426.                    const char *string2, int length2,
  427.                        int start,
  428.                        struct re_registers *regs,
  429.                        int stop);
  430.  
  431.  
  432. /* 4.2 bsd compatibility.  */
  433. extern const char *re_comp (const char *);
  434. extern int re_exec (const char *);
  435.  
  436. /* POSIX compatibility.  */
  437. extern int regcomp (regex_t *preg, const char *pattern, int cflags);
  438. extern int regexec (const regex_t *preg, const char *string, size_t nmatch,
  439.             regmatch_t pmatch[], int eflags);
  440. extern size_t regerror (int errcode, const regex_t *preg, char *errbuf, 
  441.             size_t errbuf_size);
  442. extern void regfree (regex_t *preg);
  443.  
  444. #else /* not __STDC__ */
  445.  
  446. /* Support old C compilers.  */
  447. #define const
  448.  
  449. extern reg_syntax_t re_set_syntax ();
  450. extern char *re_compile_pattern ();
  451. extern int re_search (), re_search_2 ();
  452. extern int re_match (), re_match_2 ();
  453.  
  454. /* 4.2 BSD compatibility.  */
  455. extern char *re_comp ();
  456. extern int re_exec ();
  457.  
  458. /* POSIX compatibility.  */
  459. extern int regcomp ();
  460. extern int regexec ();
  461. extern size_t regerror ();
  462. extern void regfree ();
  463.  
  464. #endif /* not __STDC__ */
  465. #endif /* not __REGEXP_LIBRARY_H__ */
  466.  
  467.  
  468.  
  469. /*
  470. Local variables:
  471. make-backup-files: t
  472. version-control: t
  473. trim-versions-without-asking: nil
  474. End:
  475. */
  476.