386BSD 0.1 development
[unix-history] / usr / src / usr.bin / diff / regex.h
CommitLineData
89dd82fa
WJ
1/* Definitions for data structures callers pass the regex library.
2
3 Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
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
20#ifdef __GNUC__
21 #pragma once
22#endif
23
24#ifndef __REGEXP_LIBRARY
25#define __REGEXP_LIBRARY
26
27/* Define number of parens for which we record the beginnings and ends.
28 This affects how much space the `struct re_registers' type takes up. */
29#ifndef RE_NREGS
30#define RE_NREGS 10
31#endif
32
33#define BYTEWIDTH 8
34
35
36/* Maximum number of duplicates an interval can allow. */
37#define RE_DUP_MAX ((1 << 15) - 1)
38
39
40/* This defines the various regexp syntaxes. */
41extern int obscure_syntax;
42
43
44/* The following bits are used in the obscure_syntax variable to choose among
45 alternative regexp syntaxes. */
46
47/* If this bit is set, plain parentheses serve as grouping, and backslash
48 parentheses are needed for literal searching.
49 If not set, backslash-parentheses are grouping, and plain parentheses
50 are for literal searching. */
51#define RE_NO_BK_PARENS 1
52
53/* If this bit is set, plain | serves as the `or'-operator, and \| is a
54 literal.
55 If not set, \| serves as the `or'-operator, and | is a literal. */
56#define RE_NO_BK_VBAR (1 << 1)
57
58/* If this bit is not set, plain + or ? serves as an operator, and \+, \? are
59 literals.
60 If set, \+, \? are operators and plain +, ? are literals. */
61#define RE_BK_PLUS_QM (1 << 2)
62
63/* If this bit is set, | binds tighter than ^ or $.
64 If not set, the contrary. */
65#define RE_TIGHT_VBAR (1 << 3)
66
67/* If this bit is set, then treat newline as an OR operator.
68 If not set, treat it as a normal character. */
69#define RE_NEWLINE_OR (1 << 4)
70
71/* If this bit is set, then special characters may act as normal
72 characters in some contexts. Specifically, this applies to:
73 ^ -- only special at the beginning, or after ( or |;
74 $ -- only special at the end, or before ) or |;
75 *, +, ? -- only special when not after the beginning, (, or |.
76 If this bit is not set, special characters (such as *, ^, and $)
77 always have their special meaning regardless of the surrounding
78 context. */
79#define RE_CONTEXT_INDEP_OPS (1 << 5)
80
81/* If this bit is not set, then \ before anything inside [ and ] is taken as
82 a real \.
83 If set, then such a \ escapes the following character. This is a
84 special case for awk. */
85#define RE_AWK_CLASS_HACK (1 << 6)
86
87/* If this bit is set, then \{ and \} or { and } serve as interval operators.
88 If not set, then \{ and \} and { and } are treated as literals. */
89#define RE_INTERVALS (1 << 7)
90
91/* If this bit is not set, then \{ and \} serve as interval operators and
92 { and } are literals.
93 If set, then { and } serve as interval operators and \{ and \} are
94 literals. */
95#define RE_NO_BK_CURLY_BRACES (1 << 8)
96
97/* If this bit is set, then character classes are supported; they are:
98 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
99 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
100 If not set, then character classes are not supported. */
101#define RE_CHAR_CLASSES (1 << 9)
102
103/* If this bit is set, then the dot re doesn't match a null byte.
104 If not set, it does. */
105#define RE_DOT_NOT_NULL (1 << 10)
106
107/* If this bit is set, then [^...] doesn't match a newline.
108 If not set, it does. */
109#define RE_HAT_NOT_NEWLINE (1 << 11)
110
111/* If this bit is set, back references are recognized.
112 If not set, they aren't. */
113#define RE_NO_BK_REFS (1 << 12)
114
115/* If this bit is set, back references must refer to a preceding
116 subexpression. If not set, a back reference to a nonexistent
117 subexpression is treated as literal characters. */
118#define RE_NO_EMPTY_BK_REF (1 << 13)
119
120/* If this bit is set, bracket expressions can't be empty.
121 If it is set, they can be empty. */
122#define RE_NO_EMPTY_BRACKETS (1 << 14)
123
124/* If this bit is set, then *, +, ? and { cannot be first in an re or
125 immediately after a |, or a (. Furthermore, a | cannot be first or
126 last in an re, or immediately follow another | or a (. Also, a ^
127 cannot appear in a nonleading position and a $ cannot appear in a
128 nontrailing position (outside of bracket expressions, that is). */
129#define RE_CONTEXTUAL_INVALID_OPS (1 << 15)
130
131/* If this bit is set, then +, ? and | aren't recognized as operators.
132 If it's not, they are. */
133#define RE_LIMITED_OPS (1 << 16)
134
135/* If this bit is set, then an ending range point has to collate higher
136 or equal to the starting range point.
137 If it's not set, then when the ending range point collates higher
138 than the starting range point, the range is just considered empty. */
139#define RE_NO_EMPTY_RANGES (1 << 17)
140
141/* If this bit is set, then a hyphen (-) can't be an ending range point.
142 If it isn't, then it can. */
143#define RE_NO_HYPHEN_RANGE_END (1 << 18)
144
145
146/* Define combinations of bits for the standard possibilities. */
147#define RE_SYNTAX_POSIX_AWK (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
148 | RE_CONTEXT_INDEP_OPS)
149#define RE_SYNTAX_AWK (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
150 | RE_CONTEXT_INDEP_OPS | RE_AWK_CLASS_HACK)
151#define RE_SYNTAX_EGREP (RE_NO_BK_PARENS | RE_NO_BK_VBAR \
152 | RE_CONTEXT_INDEP_OPS | RE_NEWLINE_OR)
153#define RE_SYNTAX_GREP (RE_BK_PLUS_QM | RE_NEWLINE_OR)
154#define RE_SYNTAX_EMACS 0
155#define RE_SYNTAX_POSIX_BASIC (RE_INTERVALS | RE_BK_PLUS_QM \
156 | RE_CHAR_CLASSES | RE_DOT_NOT_NULL \
157 | RE_HAT_NOT_NEWLINE | RE_NO_EMPTY_BK_REF \
158 | RE_NO_EMPTY_BRACKETS | RE_LIMITED_OPS \
159 | RE_NO_EMPTY_RANGES | RE_NO_HYPHEN_RANGE_END)
160
161#define RE_SYNTAX_POSIX_EXTENDED (RE_INTERVALS | RE_NO_BK_CURLY_BRACES \
162 | RE_NO_BK_VBAR | RE_NO_BK_PARENS \
163 | RE_HAT_NOT_NEWLINE | RE_CHAR_CLASSES \
164 | RE_NO_EMPTY_BRACKETS | RE_CONTEXTUAL_INVALID_OPS \
165 | RE_NO_BK_REFS | RE_NO_EMPTY_RANGES \
166 | RE_NO_HYPHEN_RANGE_END)
167
168
169/* This data structure is used to represent a compiled pattern. */
170
171struct re_pattern_buffer
172 {
173 char *buffer; /* Space holding the compiled pattern commands. */
174 long allocated; /* Size of space that `buffer' points to. */
175 long used; /* Length of portion of buffer actually occupied */
176 char *fastmap; /* Pointer to fastmap, if any, or zero if none. */
177 /* re_search uses the fastmap, if there is one,
178 to skip over totally implausible characters. */
179 char *translate; /* Translate table to apply to all characters before
180 comparing, or zero for no translation.
181 The translation is applied to a pattern when it is
182 compiled and to data when it is matched. */
183 char fastmap_accurate;
184 /* Set to zero when a new pattern is stored,
185 set to one when the fastmap is updated from it. */
186 char can_be_null; /* Set to one by compiling fastmap
187 if this pattern might match the null string.
188 It does not necessarily match the null string
189 in that case, but if this is zero, it cannot.
190 2 as value means can match null string
191 but at end of range or before a character
192 listed in the fastmap. */
193 };
194
195
196/* search.c (search_buffer) needs this one value. It is defined both in
197 regex.c and here. */
198#define RE_EXACTN_VALUE 1
199
200
201/* Structure to store register contents data in.
202
203 Pass the address of such a structure as an argument to re_match, etc.,
204 if you want this information back.
205
206 For i from 1 to RE_NREGS - 1, start[i] records the starting index in
207 the string of where the ith subexpression matched, and end[i] records
208 one after the ending index. start[0] and end[0] are analogous, for
209 the entire pattern. */
210
211struct re_registers
212 {
213 int start[RE_NREGS];
214 int end[RE_NREGS];
215 };
216
217
218\f
219#ifdef __STDC__
220
221extern char *re_compile_pattern (char *, int, struct re_pattern_buffer *);
222/* Is this really advertised? */
223extern void re_compile_fastmap (struct re_pattern_buffer *);
224extern int re_search (struct re_pattern_buffer *, char*, int, int, int,
225 struct re_registers *);
226extern int re_search_2 (struct re_pattern_buffer *, char *, int,
227 char *, int, int, int,
228 struct re_registers *, int);
229extern int re_match (struct re_pattern_buffer *, char *, int, int,
230 struct re_registers *);
231extern int re_match_2 (struct re_pattern_buffer *, char *, int,
232 char *, int, int, struct re_registers *, int);
233
234/* 4.2 bsd compatibility. */
235extern char *re_comp (char *);
236extern int re_exec (char *);
237
238#else /* !__STDC__ */
239
240extern char *re_compile_pattern ();
241/* Is this really advertised? */
242extern void re_compile_fastmap ();
243extern int re_search (), re_search_2 ();
244extern int re_match (), re_match_2 ();
245
246/* 4.2 bsd compatibility. */
247extern char *re_comp ();
248extern int re_exec ();
249
250#endif /* __STDC__ */
251
252
253#ifdef SYNTAX_TABLE
254extern char *re_syntax_table;
255#endif
256
257#endif /* !__REGEXP_LIBRARY */