Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/gkregex.h
3206 views
/* Definitions for data structures and routines for the regular1expression library.2Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,20063Free Software Foundation, Inc.4This file is part of the GNU C Library.56The GNU C Library is free software; you can redistribute it and/or7modify it under the terms of the GNU Lesser General Public8License as published by the Free Software Foundation; either9version 2.1 of the License, or (at your option) any later version.1011The GNU C Library is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14Lesser General Public License for more details.1516You should have received a copy of the GNU Lesser General Public17License along with the GNU C Library; if not, write to the Free18Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA1902111-1307 USA. */2021#ifndef _REGEX_H22#define _REGEX_H 12324#include <sys/types.h>2526/* Allow the use in C++ code. */27#ifdef __cplusplus28extern "C" {29#endif3031/* The following two types have to be signed and unsigned integer type32wide enough to hold a value of a pointer. For most ANSI compilers33ptrdiff_t and size_t should be likely OK. Still size of these two34types is 2 for Microsoft C. Ugh... */35typedef long int s_reg_t;36typedef unsigned long int active_reg_t;3738/* The following bits are used to determine the regexp syntax we39recognize. The set/not-set meanings are chosen so that Emacs syntax40remains the value 0. The bits are given in alphabetical order, and41the definitions shifted by one from the previous bit; thus, when we42add or remove a bit, only one other definition need change. */43typedef unsigned long int reg_syntax_t;4445/* If this bit is not set, then \ inside a bracket expression is literal.46If set, then such a \ quotes the following character. */47#define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)4849/* If this bit is not set, then + and ? are operators, and \+ and \? are50literals.51If set, then \+ and \? are operators and + and ? are literals. */52#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)5354/* If this bit is set, then character classes are supported. They are:55[:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],56[:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].57If not set, then character classes are not supported. */58#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)5960/* If this bit is set, then ^ and $ are always anchors (outside bracket61expressions, of course).62If this bit is not set, then it depends:63^ is an anchor if it is at the beginning of a regular64expression or after an open-group or an alternation operator;65$ is an anchor if it is at the end of a regular expression, or66before a close-group or an alternation operator.6768This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because69POSIX draft 11.2 says that * etc. in leading positions is undefined.70We already implemented a previous draft which made those constructs71invalid, though, so we haven't changed the code back. */72#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)7374/* If this bit is set, then special characters are always special75regardless of where they are in the pattern.76If this bit is not set, then special characters are special only in77some contexts; otherwise they are ordinary. Specifically,78* + ? and intervals are only special when not after the beginning,79open-group, or alternation operator. */80#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)8182/* If this bit is set, then *, +, ?, and { cannot be first in an re or83immediately after an alternation or begin-group operator. */84#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)8586/* If this bit is set, then . matches newline.87If not set, then it doesn't. */88#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)8990/* If this bit is set, then . doesn't match NUL.91If not set, then it does. */92#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)9394/* If this bit is set, nonmatching lists [^...] do not match newline.95If not set, they do. */96#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)9798/* If this bit is set, either \{...\} or {...} defines an99interval, depending on RE_NO_BK_BRACES.100If not set, \{, \}, {, and } are literals. */101#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)102103/* If this bit is set, +, ? and | aren't recognized as operators.104If not set, they are. */105#define RE_LIMITED_OPS (RE_INTERVALS << 1)106107/* If this bit is set, newline is an alternation operator.108If not set, newline is literal. */109#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)110111/* If this bit is set, then `{...}' defines an interval, and \{ and \}112are literals.113If not set, then `\{...\}' defines an interval. */114#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)115116/* If this bit is set, (...) defines a group, and \( and \) are literals.117If not set, \(...\) defines a group, and ( and ) are literals. */118#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)119120/* If this bit is set, then \<digit> matches <digit>.121If not set, then \<digit> is a back-reference. */122#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)123124/* If this bit is set, then | is an alternation operator, and \| is literal.125If not set, then \| is an alternation operator, and | is literal. */126#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)127128/* If this bit is set, then an ending range point collating higher129than the starting range point, as in [z-a], is invalid.130If not set, then when ending range point collates higher than the131starting range point, the range is ignored. */132#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)133134/* If this bit is set, then an unmatched ) is ordinary.135If not set, then an unmatched ) is invalid. */136#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)137138/* If this bit is set, succeed as soon as we match the whole pattern,139without further backtracking. */140#define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)141142/* If this bit is set, do not process the GNU regex operators.143If not set, then the GNU regex operators are recognized. */144#define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)145146/* If this bit is set, turn on internal regex debugging.147If not set, and debugging was on, turn it off.148This only works if regex.c is compiled -DDEBUG.149We define this bit always, so that all that's needed to turn on150debugging is to recompile regex.c; the calling code can always have151this bit set, and it won't affect anything in the normal case. */152#define RE_DEBUG (RE_NO_GNU_OPS << 1)153154/* If this bit is set, a syntactically invalid interval is treated as155a string of ordinary characters. For example, the ERE 'a{1' is156treated as 'a\{1'. */157#define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)158159/* If this bit is set, then ignore case when matching.160If not set, then case is significant. */161#define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)162163/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only164for ^, because it is difficult to scan the regex backwards to find165whether ^ should be special. */166#define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)167168/* If this bit is set, then \{ cannot be first in an bre or169immediately after an alternation or begin-group operator. */170#define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)171172/* If this bit is set, then no_sub will be set to 1 during173re_compile_pattern. */174#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)175176/* This global variable defines the particular regexp syntax to use (for177some interfaces). When a regexp is compiled, the syntax used is178stored in the pattern buffer, so changing this does not affect179already-compiled regexps. */180extern reg_syntax_t re_syntax_options;181182/* Define combinations of the above bits for the standard possibilities.183(The [[[ comments delimit what gets put into the Texinfo file, so184don't delete them!) */185/* [[[begin syntaxes]]] */186#define RE_SYNTAX_EMACS 0187188#define RE_SYNTAX_AWK \189(RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \190| RE_NO_BK_PARENS | RE_NO_BK_REFS \191| RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \192| RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \193| RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)194195#define RE_SYNTAX_GNU_AWK \196((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \197& ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS \198| RE_CONTEXT_INVALID_OPS ))199200#define RE_SYNTAX_POSIX_AWK \201(RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \202| RE_INTERVALS | RE_NO_GNU_OPS)203204#define RE_SYNTAX_GREP \205(RE_BK_PLUS_QM | RE_CHAR_CLASSES \206| RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \207| RE_NEWLINE_ALT)208209#define RE_SYNTAX_EGREP \210(RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \211| RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \212| RE_NEWLINE_ALT | RE_NO_BK_PARENS \213| RE_NO_BK_VBAR)214215#define RE_SYNTAX_POSIX_EGREP \216(RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \217| RE_INVALID_INTERVAL_ORD)218219/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */220#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC221222#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC223224/* Syntax bits common to both basic and extended POSIX regex syntax. */225#define _RE_SYNTAX_POSIX_COMMON \226(RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \227| RE_INTERVALS | RE_NO_EMPTY_RANGES)228229#define RE_SYNTAX_POSIX_BASIC \230(_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)231232/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes233RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this234isn't minimal, since other operators, such as \`, aren't disabled. */235#define RE_SYNTAX_POSIX_MINIMAL_BASIC \236(_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)237238#define RE_SYNTAX_POSIX_EXTENDED \239(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \240| RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \241| RE_NO_BK_PARENS | RE_NO_BK_VBAR \242| RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)243244/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is245removed and RE_NO_BK_REFS is added. */246#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \247(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \248| RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \249| RE_NO_BK_PARENS | RE_NO_BK_REFS \250| RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)251/* [[[end syntaxes]]] */252253/* Maximum number of duplicates an interval can allow. Some systems254(erroneously) define this in other header files, but we want our255value, so remove any previous define. */256#ifdef RE_DUP_MAX257# undef RE_DUP_MAX258#endif259/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */260#define RE_DUP_MAX (0x7fff)261262263/* POSIX `cflags' bits (i.e., information for `regcomp'). */264265/* If this bit is set, then use extended regular expression syntax.266If not set, then use basic regular expression syntax. */267#define REG_EXTENDED 1268269/* If this bit is set, then ignore case when matching.270If not set, then case is significant. */271#define REG_ICASE (REG_EXTENDED << 1)272273/* If this bit is set, then anchors do not match at newline274characters in the string.275If not set, then anchors do match at newlines. */276#define REG_NEWLINE (REG_ICASE << 1)277278/* If this bit is set, then report only success or fail in regexec.279If not set, then returns differ between not matching and errors. */280#define REG_NOSUB (REG_NEWLINE << 1)281282283/* POSIX `eflags' bits (i.e., information for regexec). */284285/* If this bit is set, then the beginning-of-line operator doesn't match286the beginning of the string (presumably because it's not the287beginning of a line).288If not set, then the beginning-of-line operator does match the289beginning of the string. */290#define REG_NOTBOL 1291292/* Like REG_NOTBOL, except for the end-of-line. */293#define REG_NOTEOL (1 << 1)294295/* Use PMATCH[0] to delimit the start and end of the search in the296buffer. */297#define REG_STARTEND (1 << 2)298299300/* If any error codes are removed, changed, or added, update the301`re_error_msg' table in regex.c. */302typedef enum303{304#ifdef _XOPEN_SOURCE305REG_ENOSYS = -1, /* This will never happen for this implementation. */306#endif307308REG_NOERROR = 0, /* Success. */309REG_NOMATCH, /* Didn't find a match (for regexec). */310311/* POSIX regcomp return error codes. (In the order listed in the312standard.) */313REG_BADPAT, /* Invalid pattern. */314REG_ECOLLATE, /* Inalid collating element. */315REG_ECTYPE, /* Invalid character class name. */316REG_EESCAPE, /* Trailing backslash. */317REG_ESUBREG, /* Invalid back reference. */318REG_EBRACK, /* Unmatched left bracket. */319REG_EPAREN, /* Parenthesis imbalance. */320REG_EBRACE, /* Unmatched \{. */321REG_BADBR, /* Invalid contents of \{\}. */322REG_ERANGE, /* Invalid range end. */323REG_ESPACE, /* Ran out of memory. */324REG_BADRPT, /* No preceding re for repetition op. */325326/* Error codes we've added. */327REG_EEND, /* Premature end. */328REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */329REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */330} reg_errcode_t;331332/* This data structure represents a compiled pattern. Before calling333the pattern compiler, the fields `buffer', `allocated', `fastmap',334`translate', and `no_sub' can be set. After the pattern has been335compiled, the `re_nsub' field is available. All other fields are336private to the regex routines. */337338#ifndef RE_TRANSLATE_TYPE339# define RE_TRANSLATE_TYPE unsigned char *340#endif341342struct re_pattern_buffer343{344/* Space that holds the compiled pattern. It is declared as345`unsigned char *' because its elements are sometimes used as346array indexes. */347unsigned char *buffer;348349/* Number of bytes to which `buffer' points. */350unsigned long int allocated;351352/* Number of bytes actually used in `buffer'. */353unsigned long int used;354355/* Syntax setting with which the pattern was compiled. */356reg_syntax_t syntax;357358/* Pointer to a fastmap, if any, otherwise zero. re_search uses the359fastmap, if there is one, to skip over impossible starting points360for matches. */361char *fastmap;362363/* Either a translate table to apply to all characters before364comparing them, or zero for no translation. The translation is365applied to a pattern when it is compiled and to a string when it366is matched. */367RE_TRANSLATE_TYPE translate;368369/* Number of subexpressions found by the compiler. */370size_t re_nsub;371372/* Zero if this pattern cannot match the empty string, one else.373Well, in truth it's used only in `re_search_2', to see whether or374not we should use the fastmap, so we don't set this absolutely375perfectly; see `re_compile_fastmap' (the `duplicate' case). */376unsigned can_be_null : 1;377378/* If REGS_UNALLOCATED, allocate space in the `regs' structure379for `max (RE_NREGS, re_nsub + 1)' groups.380If REGS_REALLOCATE, reallocate space if necessary.381If REGS_FIXED, use what's there. */382#define REGS_UNALLOCATED 0383#define REGS_REALLOCATE 1384#define REGS_FIXED 2385unsigned regs_allocated : 2;386387/* Set to zero when `regex_compile' compiles a pattern; set to one388by `re_compile_fastmap' if it updates the fastmap. */389unsigned fastmap_accurate : 1;390391/* If set, `re_match_2' does not return information about392subexpressions. */393unsigned no_sub : 1;394395/* If set, a beginning-of-line anchor doesn't match at the beginning396of the string. */397unsigned not_bol : 1;398399/* Similarly for an end-of-line anchor. */400unsigned not_eol : 1;401402/* If true, an anchor at a newline matches. */403unsigned newline_anchor : 1;404};405406typedef struct re_pattern_buffer regex_t;407408/* Type for byte offsets within the string. POSIX mandates this. */409typedef int regoff_t;410411412/* This is the structure we store register match data in. See413regex.texinfo for a full description of what registers match. */414struct re_registers415{416unsigned num_regs;417regoff_t *start;418regoff_t *end;419};420421422/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,423`re_match_2' returns information about at least this many registers424the first time a `regs' structure is passed. */425#ifndef RE_NREGS426# define RE_NREGS 30427#endif428429430/* POSIX specification for registers. Aside from the different names than431`re_registers', POSIX uses an array of structures, instead of a432structure of arrays. */433typedef struct434{435regoff_t rm_so; /* Byte offset from string's start to substring's start. */436regoff_t rm_eo; /* Byte offset from string's start to substring's end. */437} regmatch_t;438439/* Declarations for routines. */440441/* Sets the current default syntax to SYNTAX, and return the old syntax.442You can also simply assign to the `re_syntax_options' variable. */443extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);444445/* Compile the regular expression PATTERN, with length LENGTH446and syntax given by the global `re_syntax_options', into the buffer447BUFFER. Return NULL if successful, and an error string if not. */448extern const char *re_compile_pattern (const char *__pattern, size_t __length,449struct re_pattern_buffer *__buffer);450451452/* Compile a fastmap for the compiled pattern in BUFFER; used to453accelerate searches. Return 0 if successful and -2 if was an454internal error. */455extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);456457458/* Search in the string STRING (with length LENGTH) for the pattern459compiled into BUFFER. Start searching at position START, for RANGE460characters. Return the starting position of the match, -1 for no461match, or -2 for an internal error. Also return register462information in REGS (if REGS and BUFFER->no_sub are nonzero). */463extern int re_search (struct re_pattern_buffer *__buffer, const char *__string,464int __length, int __start, int __range,465struct re_registers *__regs);466467468/* Like `re_search', but search in the concatenation of STRING1 and469STRING2. Also, stop searching at index START + STOP. */470extern int re_search_2 (struct re_pattern_buffer *__buffer,471const char *__string1, int __length1,472const char *__string2, int __length2, int __start,473int __range, struct re_registers *__regs, int __stop);474475476/* Like `re_search', but return how many characters in STRING the regexp477in BUFFER matched, starting at position START. */478extern int re_match (struct re_pattern_buffer *__buffer, const char *__string,479int __length, int __start, struct re_registers *__regs);480481482/* Relates to `re_match' as `re_search_2' relates to `re_search'. */483extern int re_match_2 (struct re_pattern_buffer *__buffer,484const char *__string1, int __length1,485const char *__string2, int __length2, int __start,486struct re_registers *__regs, int __stop);487488489/* Set REGS to hold NUM_REGS registers, storing them in STARTS and490ENDS. Subsequent matches using BUFFER and REGS will use this memory491for recording register information. STARTS and ENDS must be492allocated with malloc, and must each be at least `NUM_REGS * sizeof493(regoff_t)' bytes long.494495If NUM_REGS == 0, then subsequent matches should allocate their own496register data.497498Unless this function is called, the first search or match using499PATTERN_BUFFER will allocate its own register data, without500freeing the old data. */501extern void re_set_registers (struct re_pattern_buffer *__buffer,502struct re_registers *__regs,503unsigned int __num_regs,504regoff_t *__starts, regoff_t *__ends);505506#if defined _REGEX_RE_COMP || defined _LIBC507# ifndef _CRAY508/* 4.2 bsd compatibility. */509extern char *re_comp (const char *);510extern int re_exec (const char *);511# endif512#endif513514/* GCC 2.95 and later have "__restrict"; C99 compilers have515"restrict", and "configure" may have defined "restrict". */516#ifndef __restrict517# if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))518# if defined restrict || 199901L <= __STDC_VERSION__519# define __restrict restrict520# else521# define __restrict522# endif523# endif524#endif525/* gcc 3.1 and up support the [restrict] syntax. */526#ifndef __restrict_arr527# if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) \528&& !defined __GNUG__529# define __restrict_arr __restrict530# else531# define __restrict_arr532# endif533#endif534535/* POSIX compatibility. */536extern int regcomp (regex_t *__restrict __preg,537const char *__restrict __pattern,538int __cflags);539540extern int regexec (const regex_t *__restrict __preg,541const char *__restrict __string, size_t __nmatch,542regmatch_t __pmatch[__restrict_arr],543int __eflags);544545extern size_t regerror (int __errcode, const regex_t *__restrict __preg,546char *__restrict __errbuf, size_t __errbuf_size);547548extern void regfree (regex_t *__preg);549550551#ifdef __cplusplus552}553#endif /* C++ */554555#endif /* regex.h */556557558