2 * regex.h -- regular expression definitions for lsof
4 * This header file is used only when the dialect has no POSIX-conformant
5 * regular expression function set. When that is the case, the dialect's
6 * machine.h will define USE_LIB_REGEX.
8 * When the dialect has a POSIX-conformant regular expression function set,
9 * USE_LIB_REGEX is not defined and this header file #include's <regex.h>.
11 * V. Abell <abe@purdue.edu>
17 * Copyright 2000 Purdue Research Foundation, West Lafayette, Indiana
18 * 47907. All rights reserved.
20 * Written by Victor A. Abell
22 * This software is not subject to any license of the American Telephone
23 * and Telegraph Company or the Regents of the University of California.
25 * This software has been adapted from snprintf.c in sendmail 8.9.3. It
26 * is subject to the sendmail copyright statements listed below, and the
27 * sendmail licensing terms stated in the sendmail LICENSE file comment
28 * section of this file.
30 * Permission is granted to anyone to use this software for any purpose on
31 * any computer system, and to alter it and redistribute it freely, subject
32 * to the following restrictions:
34 * 1. Neither the authors nor Purdue University are responsible for any
35 * consequences of the use of this software.
37 * 2. The origin of this software must not be misrepresented, either by
38 * explicit claim or by omission. Credit to the authors and Purdue
39 * University must appear in documentation and sources.
41 * 3. Altered versions must be plainly marked as such, and must not be
42 * misrepresented as being the original software.
44 * 4. This notice may not be removed or altered.
50 * This section comes from GLIBC 2.2. It is used only when the dialect
51 * has no POSIX-conformant regular expression function set. When that is
52 * the case, the dialect's machine.h will define USE_LIB_REGEX.
55 /* Definitions for data structures and routines for the regular
56 expression library, version 0.12.
57 Copyright (C) 1985,1989-1993,1995-1998, 2000 Free Software Foundation, Inc.
59 This file is part of the GNU C Library. Its master source is NOT part of
60 the C library, however. The master source lives in /gd/gnu/lib.
62 The GNU C Library is free software; you can redistribute it and/or
63 modify it under the terms of the GNU Library General Public License as
64 published by the Free Software Foundation; either version 2 of the
65 License, or (at your option) any later version.
67 The GNU C Library is distributed in the hope that it will be useful,
68 but WITHOUT ANY WARRANTY; without even the implied warranty of
69 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
70 Library General Public License for more details.
72 You should have received a copy of the GNU Library General Public
73 License along with the GNU C Library; see the file COPYING.LIB. If not,
74 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
75 Boston, MA 02111-1307, USA. */
80 /* Allow the use in C++ code. */
85 /* POSIX says that <sys/types.h> must be included (by the caller) before
88 #if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
89 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
94 /* The following two types have to be signed and unsigned integer type
95 wide enough to hold a value of a pointer. For most ANSI compilers
96 ptrdiff_t and size_t should be likely OK. Still size of these two
97 types is 2 for Microsoft C. Ugh... */
98 typedef long int s_reg_t;
99 typedef unsigned long int active_reg_t;
101 /* The following bits are used to determine the regexp syntax we
102 recognize. The set/not-set meanings are chosen so that Emacs syntax
103 remains the value 0. The bits are given in alphabetical order, and
104 the definitions shifted by one from the previous bit; thus, when we
105 add or remove a bit, only one other definition need change. */
106 typedef unsigned long int reg_syntax_t;
108 /* If this bit is not set, then \ inside a bracket expression is literal.
109 If set, then such a \ quotes the following character. */
110 #define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
112 /* If this bit is not set, then + and ? are operators, and \+ and \? are
114 If set, then \+ and \? are operators and + and ? are literals. */
115 #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
117 /* If this bit is set, then character classes are supported. They are:
118 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
119 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
120 If not set, then character classes are not supported. */
121 #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
123 /* If this bit is set, then ^ and $ are always anchors (outside bracket
124 expressions, of course).
125 If this bit is not set, then it depends:
126 ^ is an anchor if it is at the beginning of a regular
127 expression or after an open-group or an alternation operator;
128 $ is an anchor if it is at the end of a regular expression, or
129 before a close-group or an alternation operator.
131 This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
132 POSIX draft 11.2 says that * etc. in leading positions is undefined.
133 We already implemented a previous draft which made those constructs
134 invalid, though, so we haven't changed the code back. */
135 #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
137 /* If this bit is set, then special characters are always special
138 regardless of where they are in the pattern.
139 If this bit is not set, then special characters are special only in
140 some contexts; otherwise they are ordinary. Specifically,
141 * + ? and intervals are only special when not after the beginning,
142 open-group, or alternation operator. */
143 #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
145 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
146 immediately after an alternation or begin-group operator. */
147 #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
149 /* If this bit is set, then . matches newline.
150 If not set, then it doesn't. */
151 #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
153 /* If this bit is set, then . doesn't match NUL.
154 If not set, then it does. */
155 #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
157 /* If this bit is set, nonmatching lists [^...] do not match newline.
158 If not set, they do. */
159 #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
161 /* If this bit is set, either \{...\} or {...} defines an
162 interval, depending on RE_NO_BK_BRACES.
163 If not set, \{, \}, {, and } are literals. */
164 #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
166 /* If this bit is set, +, ? and | aren't recognized as operators.
167 If not set, they are. */
168 #define RE_LIMITED_OPS (RE_INTERVALS << 1)
170 /* If this bit is set, newline is an alternation operator.
171 If not set, newline is literal. */
172 #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
174 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
176 If not set, then `\{...\}' defines an interval. */
177 #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
179 /* If this bit is set, (...) defines a group, and \( and \) are literals.
180 If not set, \(...\) defines a group, and ( and ) are literals. */
181 #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
183 /* If this bit is set, then \<digit> matches <digit>.
184 If not set, then \<digit> is a back-reference. */
185 #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
187 /* If this bit is set, then | is an alternation operator, and \| is literal.
188 If not set, then \| is an alternation operator, and | is literal. */
189 #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
191 /* If this bit is set, then an ending range point collating higher
192 than the starting range point, as in [z-a], is invalid.
193 If not set, then when ending range point collates higher than the
194 starting range point, the range is ignored. */
195 #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
197 /* If this bit is set, then an unmatched ) is ordinary.
198 If not set, then an unmatched ) is invalid. */
199 #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
201 /* If this bit is set, succeed as soon as we match the whole pattern,
202 without further backtracking. */
203 #define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
205 /* If this bit is set, do not process the GNU regex operators.
206 If not set, then the GNU regex operators are recognized. */
207 #define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
209 /* If this bit is set, turn on internal regex debugging.
210 If not set, and debugging was on, turn it off.
211 This only works if regex.c is compiled -DDEBUG.
212 We define this bit always, so that all that's needed to turn on
213 debugging is to recompile regex.c; the calling code can always have
214 this bit set, and it won't affect anything in the normal case. */
215 #define RE_DEBUG (RE_NO_GNU_OPS << 1)
217 /* This global variable defines the particular regexp syntax to use (for
218 some interfaces). When a regexp is compiled, the syntax used is
219 stored in the pattern buffer, so changing this does not affect
220 already-compiled regexps. */
221 extern reg_syntax_t re_syntax_options;
223 /* Define combinations of the above bits for the standard possibilities.
224 (The [[[ comments delimit what gets put into the Texinfo file, so
225 don't delete them!) */
226 /* [[[begin syntaxes]]] */
227 #define RE_SYNTAX_EMACS 0
229 #define RE_SYNTAX_AWK \
230 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
231 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
232 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
233 | RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
234 | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
236 #define RE_SYNTAX_GNU_AWK \
237 ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \
238 & ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS))
240 #define RE_SYNTAX_POSIX_AWK \
241 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
242 | RE_INTERVALS | RE_NO_GNU_OPS)
244 #define RE_SYNTAX_GREP \
245 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
246 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
249 #define RE_SYNTAX_EGREP \
250 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
251 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
252 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
255 #define RE_SYNTAX_POSIX_EGREP \
256 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
258 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
259 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
261 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
263 /* Syntax bits common to both basic and extended POSIX regex syntax. */
264 #define _RE_SYNTAX_POSIX_COMMON \
265 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
266 | RE_INTERVALS | RE_NO_EMPTY_RANGES)
268 #define RE_SYNTAX_POSIX_BASIC \
269 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
271 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
272 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
273 isn't minimal, since other operators, such as \`, aren't disabled. */
274 #define RE_SYNTAX_POSIX_MINIMAL_BASIC \
275 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
277 #define RE_SYNTAX_POSIX_EXTENDED \
278 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
279 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
280 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
281 | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
283 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
284 removed and RE_NO_BK_REFS is added. */
285 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
286 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
287 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
288 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
289 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
290 /* [[[end syntaxes]]] */
292 /* Maximum number of duplicates an interval can allow. Some systems
293 (erroneously) define this in other header files, but we want our
294 value, so remove any previous define. */
298 /* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
299 #define RE_DUP_MAX (0x7fff)
302 /* POSIX `cflags' bits (i.e., information for `regcomp'). */
304 /* If this bit is set, then use extended regular expression syntax.
305 If not set, then use basic regular expression syntax. */
306 #define REG_EXTENDED 1
308 /* If this bit is set, then ignore case when matching.
309 If not set, then case is significant. */
310 #define REG_ICASE (REG_EXTENDED << 1)
312 /* If this bit is set, then anchors do not match at newline
313 characters in the string.
314 If not set, then anchors do match at newlines. */
315 #define REG_NEWLINE (REG_ICASE << 1)
317 /* If this bit is set, then report only success or fail in regexec.
318 If not set, then returns differ between not matching and errors. */
319 #define REG_NOSUB (REG_NEWLINE << 1)
322 /* POSIX `eflags' bits (i.e., information for regexec). */
324 /* If this bit is set, then the beginning-of-line operator doesn't match
325 the beginning of the string (presumably because it's not the
326 beginning of a line).
327 If not set, then the beginning-of-line operator does match the
328 beginning of the string. */
331 /* Like REG_NOTBOL, except for the end-of-line. */
332 #define REG_NOTEOL (1 << 1)
335 /* If any error codes are removed, changed, or added, update the
336 `re_error_msg' table in regex.c. */
340 REG_ENOSYS = -1, /* This will never happen for this implementation. */
343 REG_NOERROR = 0, /* Success. */
344 REG_NOMATCH, /* Didn't find a match (for regexec). */
346 /* POSIX regcomp return error codes. (In the order listed in the
348 REG_BADPAT, /* Invalid pattern. */
349 REG_ECOLLATE, /* Not implemented. */
350 REG_ECTYPE, /* Invalid character class name. */
351 REG_EESCAPE, /* Trailing backslash. */
352 REG_ESUBREG, /* Invalid back reference. */
353 REG_EBRACK, /* Unmatched left bracket. */
354 REG_EPAREN, /* Parenthesis imbalance. */
355 REG_EBRACE, /* Unmatched \{. */
356 REG_BADBR, /* Invalid contents of \{\}. */
357 REG_ERANGE, /* Invalid range end. */
358 REG_ESPACE, /* Ran out of memory. */
359 REG_BADRPT, /* No preceding re for repetition op. */
361 /* Error codes we've added. */
362 REG_EEND, /* Premature end. */
363 REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
364 REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
367 /* This data structure represents a compiled pattern. Before calling
368 the pattern compiler, the fields `buffer', `allocated', `fastmap',
369 `translate', and `no_sub' can be set. After the pattern has been
370 compiled, the `re_nsub' field is available. All other fields are
371 private to the regex routines. */
373 #ifndef RE_TRANSLATE_TYPE
374 # define RE_TRANSLATE_TYPE char *
377 struct re_pattern_buffer
379 /* [[[begin pattern_buffer]]] */
380 /* Space that holds the compiled pattern. It is declared as
381 `unsigned char *' because its elements are
382 sometimes used as array indexes. */
383 unsigned char *buffer;
385 /* Number of bytes to which `buffer' points. */
386 unsigned long int allocated;
388 /* Number of bytes actually used in `buffer'. */
389 unsigned long int used;
391 /* Syntax setting with which the pattern was compiled. */
394 /* Pointer to a fastmap, if any, otherwise zero. re_search uses
395 the fastmap, if there is one, to skip over impossible
396 starting points for matches. */
399 /* Either a translate table to apply to all characters before
400 comparing them, or zero for no translation. The translation
401 is applied to a pattern when it is compiled and to a string
402 when it is matched. */
403 RE_TRANSLATE_TYPE translate;
405 /* Number of subexpressions found by the compiler. */
408 /* Zero if this pattern cannot match the empty string, one else.
409 Well, in truth it's used only in `re_search_2', to see
410 whether or not we should use the fastmap, so we don't set
411 this absolutely perfectly; see `re_compile_fastmap' (the
412 `duplicate' case). */
413 unsigned can_be_null : 1;
415 /* If REGS_UNALLOCATED, allocate space in the `regs' structure
416 for `max (RE_NREGS, re_nsub + 1)' groups.
417 If REGS_REALLOCATE, reallocate space if necessary.
418 If REGS_FIXED, use what's there. */
419 #define REGS_UNALLOCATED 0
420 #define REGS_REALLOCATE 1
422 unsigned regs_allocated : 2;
424 /* Set to zero when `regex_compile' compiles a pattern; set to one
425 by `re_compile_fastmap' if it updates the fastmap. */
426 unsigned fastmap_accurate : 1;
428 /* If set, `re_match_2' does not return information about
432 /* If set, a beginning-of-line anchor doesn't match at the
433 beginning of the string. */
434 unsigned not_bol : 1;
436 /* Similarly for an end-of-line anchor. */
437 unsigned not_eol : 1;
439 /* If true, an anchor at a newline matches. */
440 unsigned newline_anchor : 1;
442 /* [[[end pattern_buffer]]] */
445 typedef struct re_pattern_buffer regex_t;
447 /* Type for byte offsets within the string. POSIX mandates this. */
448 typedef int regoff_t;
451 /* This is the structure we store register match data in. See
452 regex.texinfo for a full description of what registers match. */
461 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
462 `re_match_2' returns information about at least this many registers
463 the first time a `regs' structure is passed. */
469 /* POSIX specification for registers. Aside from the different names than
470 `re_registers', POSIX uses an array of structures, instead of a
471 structure of arrays. */
474 regoff_t rm_so; /* Byte offset from string's start to substring's start. */
475 regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
478 /* Declarations for routines. */
480 /* To avoid duplicating every routine declaration -- once with a
481 prototype (if we are ANSI), and once without (if we aren't) -- we
482 use the following macro to declare argument types. This
483 unfortunately clutters up the declarations a bit, but I think it's
488 # define _RE_ARGS(args) args
490 #else /* not __STDC__ */
492 # define _RE_ARGS(args) ()
494 #endif /* not __STDC__ */
496 /* Sets the current default syntax to SYNTAX, and return the old syntax.
497 You can also simply assign to the `re_syntax_options' variable. */
498 extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
500 /* Compile the regular expression PATTERN, with length LENGTH
501 and syntax given by the global `re_syntax_options', into the buffer
502 BUFFER. Return NULL if successful, and an error string if not. */
503 extern const char *re_compile_pattern
504 _RE_ARGS ((const char *pattern, size_t length,
505 struct re_pattern_buffer *buffer));
508 /* Compile a fastmap for the compiled pattern in BUFFER; used to
509 accelerate searches. Return 0 if successful and -2 if was an
511 extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
514 /* Search in the string STRING (with length LENGTH) for the pattern
515 compiled into BUFFER. Start searching at position START, for RANGE
516 characters. Return the starting position of the match, -1 for no
517 match, or -2 for an internal error. Also return register
518 information in REGS (if REGS and BUFFER->no_sub are nonzero). */
520 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
521 int length, int start, int range, struct re_registers *regs));
524 /* Like `re_search', but search in the concatenation of STRING1 and
525 STRING2. Also, stop searching at index START + STOP. */
526 extern int re_search_2
527 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
528 int length1, const char *string2, int length2,
529 int start, int range, struct re_registers *regs, int stop));
532 /* Like `re_search', but return how many characters in STRING the regexp
533 in BUFFER matched, starting at position START. */
535 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
536 int length, int start, struct re_registers *regs));
539 /* Relates to `re_match' as `re_search_2' relates to `re_search'. */
540 extern int re_match_2
541 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
542 int length1, const char *string2, int length2,
543 int start, struct re_registers *regs, int stop));
546 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
547 ENDS. Subsequent matches using BUFFER and REGS will use this memory
548 for recording register information. STARTS and ENDS must be
549 allocated with malloc, and must each be at least `NUM_REGS * sizeof
550 (regoff_t)' bytes long.
552 If NUM_REGS == 0, then subsequent matches should allocate their own
555 Unless this function is called, the first search or match using
556 PATTERN_BUFFER will allocate its own register data, without
557 freeing the old data. */
558 extern void re_set_registers
559 _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
560 unsigned num_regs, regoff_t *starts, regoff_t *ends));
562 #if defined _REGEX_RE_COMP || defined _LIBC
564 /* 4.2 bsd compatibility. */
565 extern char *re_comp _RE_ARGS ((const char *));
566 extern int re_exec _RE_ARGS ((const char *));
570 /* GCC 2.95 and later have "__restrict"; C99 compilers have
571 "restrict", and "configure" may have defined "restrict". */
573 # if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
574 # if defined restrict || 199901L <= __STDC_VERSION__
575 # define __restrict restrict
581 /* For now unconditionally define __restrict_arr to expand to nothing.
582 Ideally we would have a test for the compiler which allows defining
584 #define __restrict_arr
586 /* POSIX compatibility. */
587 extern int regcomp _RE_ARGS ((regex_t *__restrict __preg,
588 const char *__restrict __pattern,
591 extern int regexec _RE_ARGS ((const regex_t *__restrict __preg,
592 const char *__restrict __string, size_t __nmatch,
593 regmatch_t __pmatch[__restrict_arr],
596 extern size_t regerror _RE_ARGS ((int __errcode, const regex_t *__preg,
597 char *__errbuf, size_t __errbuf_size));
599 extern void regfree _RE_ARGS ((regex_t *__preg));
611 trim-versions-without-asking: nil
615 #else /* !defined(USE_LIB_REGEX) */
617 #endif /* defined(USE_LIB_REGEX) */