Import grep-2.7
[dragonfly.git] / contrib / grep / src / dfa.h
1 /* dfa.h - declarations for GNU deterministic regexp compiler
2    Copyright (C) 1988, 1998, 2007, 2009-2010 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc.,
17    51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA */
18
19 /* Written June, 1988 by Mike Haertel */
20
21 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__
22 # define __attribute__(x)
23 #endif
24
25 /* Element of a list of strings, at least one of which is known to
26    appear in any R.E. matching the DFA. */
27 struct dfamust
28 {
29   int exact;
30   char *must;
31   struct dfamust *next;
32 };
33
34 /* The dfa structure. It is completely opaque. */
35 struct dfa;
36
37 /* Entry points. */
38
39 /* Allocate a struct dfa.  The struct dfa is completely opaque.
40    The returned pointer should be passed directly to free() after
41    calling dfafree() on it. */
42 extern struct dfa *dfaalloc (void);
43
44 /* Return the dfamusts associated with a dfa. */
45 extern struct dfamust *dfamusts (struct dfa const *);
46
47 /* dfasyntax() takes three arguments; the first sets the syntax bits described
48    earlier in this file, the second sets the case-folding flag, and the
49    third specifies the line terminator. */
50 extern void dfasyntax (reg_syntax_t, int, unsigned char);
51
52 /* Compile the given string of the given length into the given struct dfa.
53    Final argument is a flag specifying whether to build a searching or an
54    exact matcher. */
55 extern void dfacomp (char const *, size_t, struct dfa *, int);
56
57 /* Search through a buffer looking for a match to the given struct dfa.
58    Find the first occurrence of a string matching the regexp in the
59    buffer, and the shortest possible version thereof.  Return a pointer to
60    the first character after the match, or NULL if none is found.  BEGIN
61    points to the beginning of the buffer, and END points to the first byte
62    after its end.  Note however that we store a sentinel byte (usually
63    newline) in *END, so the actual buffer must be one byte longer.
64    When NEWLINE is nonzero, newlines may appear in the matching string.
65    If COUNT is non-NULL, increment *COUNT once for each newline processed.
66    Finally, if BACKREF is non-NULL set *BACKREF to indicate whether we
67    encountered a back-reference (1) or not (0).  The caller may use this
68    to decide whether to fall back on a backtracking matcher. */
69 extern char *dfaexec (struct dfa *d, char const *begin, char *end,
70                       int newline, int *count, int *backref);
71
72 /* Free the storage held by the components of a struct dfa. */
73 extern void dfafree (struct dfa *);
74
75 /* Entry points for people who know what they're doing. */
76
77 /* Initialize the components of a struct dfa. */
78 extern void dfainit (struct dfa *);
79
80 /* Incrementally parse a string of given length into a struct dfa. */
81 extern void dfaparse (char const *, size_t, struct dfa *);
82
83 /* Analyze a parsed regexp; second argument tells whether to build a searching
84    or an exact matcher. */
85 extern void dfaanalyze (struct dfa *, int);
86
87 /* Compute, for each possible character, the transitions out of a given
88    state, storing them in an array of integers. */
89 extern void dfastate (int, struct dfa *, int []);
90
91 /* Error handling. */
92
93 /* dfawarn() is called by the regexp routines whenever a regex is compiled
94    that likely doesn't do what the user wanted.  It takes a single
95    argument, a NUL-terminated string describing the situation.  The user
96    must supply a dfawarn.  */
97 extern void dfawarn (const char *);
98
99 /* dfaerror() is called by the regexp routines whenever an error occurs.  It
100    takes a single argument, a NUL-terminated string describing the error.
101    The user must supply a dfaerror.  */
102 extern void dfaerror (const char *) __attribute__ ((noreturn));