Merged silc_1_0_branch to trunk.
[silc.git] / lib / contrib / regexpr.h
1 /*
2
3 regexpr.h
4
5 Author: Tatu Ylonen <ylo@ngs.fi>
6
7 Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
8
9 Permission to use, copy, modify, distribute, and sell this software
10 and its documentation is hereby granted without fee, provided that the
11 above copyright notice appears in all source code copies, the name of
12 Tatu Ylonen is not used to advertise products containing this software
13 or a derivation thereof, and all modified versions are clearly marked
14 as such.
15
16 This software is provided "as is" without express or implied warranty.
17
18 Created: Thu Sep 26 17:15:36 1991 ylo
19 Last modified: Fri Jan  3 12:05:45 1992 ylo
20
21 */
22
23 /* $Id$ */
24
25 #ifndef REGEXPR_H
26 #define REGEXPR_H
27
28 #define RE_NREGS        10  /* number of registers available */
29
30 typedef struct re_pattern_buffer
31 {
32   char *buffer;          /* compiled pattern */
33   int allocated;         /* allocated size of compiled pattern */
34   int used;              /* actual length of compiled pattern */
35   char *fastmap;         /* fastmap[ch] is true if ch can start pattern */
36   char *translate;       /* translation to apply during compilation/matching */
37   char fastmap_accurate; /* true if fastmap is valid */
38   char can_be_null;      /* true if can match empty string */
39   char uses_registers;   /* registers are used and need to be initialized */
40   char anchor;           /* anchor: 0=none 1=begline 2=begbuf */
41 } *regexp_t;
42
43 typedef struct re_registers
44 {
45   int start[RE_NREGS];  /* start offset of region */
46   int end[RE_NREGS];    /* end offset of region */
47 } *regexp_registers_t;
48
49 /* bit definitions for syntax */
50 #define RE_NO_BK_PARENS         1    /* no quoting for parentheses */
51 #define RE_NO_BK_VBAR           2    /* no quoting for vertical bar */
52 #define RE_BK_PLUS_QM           4    /* quoting needed for + and ? */
53 #define RE_TIGHT_VBAR           8    /* | binds tighter than ^ and $ */
54 #define RE_NEWLINE_OR           16   /* treat newline as or */
55 #define RE_CONTEXT_INDEP_OPS    32   /* ^$?*+ are special in all contexts */
56 #define RE_ANSI_HEX             64   /* ansi sequences (\n etc) and \xhh */
57 #define RE_NO_GNU_EXTENSIONS   128   /* no gnu extensions */
58
59 /* definitions for some common regexp styles */
60 #define RE_SYNTAX_AWK   (RE_NO_BK_PARENS|RE_NO_BK_VBAR|RE_CONTEXT_INDEP_OPS)
61 #define RE_SYNTAX_EGREP (RE_SYNTAX_AWK|RE_NEWLINE_OR)
62 #define RE_SYNTAX_GREP  (RE_BK_PLUS_QM|RE_NEWLINE_OR)
63 #define RE_SYNTAX_EMACS 0
64
65 int re_set_syntax(int syntax);
66 /* This sets the syntax to use and returns the previous syntax.  The
67    syntax is specified by a bit mask of the above defined bits. */
68
69 char *re_compile_pattern(char *regex, int regex_size, regexp_t compiled);
70 /* This compiles the regexp (given in regex and length in regex_size).
71    This returns NULL if the regexp compiled successfully, and an error
72    message if an error was encountered.  The buffer field must be
73    initialized to a memory area allocated by malloc (or to NULL) before
74    use, and the allocated field must be set to its length (or 0 if buffer is
75    NULL).  Also, the translate field must be set to point to a valid
76    translation table, or NULL if it is not used. */
77
78 int re_match(regexp_t compiled, char *string, int size, int pos,
79              regexp_registers_t regs);
80 /* This tries to match the regexp against the string.  This returns the
81    length of the matched portion, or -1 if the pattern could not be
82    matched and -2 if an error (such as failure stack overflow) is
83    encountered. */
84
85 int re_match_2(regexp_t compiled, char *string1, int size1,
86               char *string2, int size2, int pos, regexp_registers_t regs,
87                int mstop);
88 /* This tries to match the regexp to the concatenation of string1 and
89    string2.  This returns the length of the matched portion, or -1 if the
90    pattern could not be matched and -2 if an error (such as failure stack
91    overflow) is encountered. */
92
93 int re_search(regexp_t compiled, char *string, int size, int startpos,
94               int range, regexp_registers_t regs);
95 /* This rearches for a substring matching the regexp.  This returns the first
96    index at which a match is found.  range specifies at how many positions to
97    try matching; positive values indicate searching forwards, and negative
98    values indicate searching backwards.  mstop specifies the offset beyond
99    which a match must not go.  This returns -1 if no match is found, and
100    -2 if an error (such as failure stack overflow) is encountered. */
101
102 int re_search_2(regexp_t compiled, char *string1, int size1,
103                 char *string2, int size2, int startpos, int range,
104                 regexp_registers_t regs, int mstop);
105 /* This is like re_search, but search from the concatenation of string1 and
106    string2.  */
107
108 void re_compile_fastmap(regexp_t compiled);
109 /* This computes the fastmap for the regexp.  For this to have any effect,
110    the calling program must have initialized the fastmap field to point
111    to an array of 256 characters. */
112
113 char *re_comp(char *s);
114 /* BSD 4.2 regex library routine re_comp.  This compiles the regexp into
115    an internal buffer.  This returns NULL if the regexp was compiled
116    successfully, and an error message if there was an error. */
117
118 int re_exec(char *s);
119 /* BSD 4.2 regexp library routine re_exec.  This returns true if the string
120    matches the regular expression (that is, a matching part is found
121    anywhere in the string). */
122
123 /* POSIX Compatibility */
124 #define regex_t struct re_pattern_buffer
125 #define regmatch_t struct re_registers
126 #define REG_EXTENDED 1
127 #define REG_ICASE (REG_EXTENDED << 1)
128 #define REG_NEWLINE (REG_ICASE << 1)
129 #define REG_NOSUB (REG_NEWLINE << 1)
130 #define REG_NOTBOL 1
131 #define REG_NOTEOL (REG_NOTBOL << 1)
132 int regcomp(regex_t *preg, const char *regex, int cflags);
133 int regexec(const regex_t *preg, const char *string, size_t nmatch,
134             regmatch_t pmatch[], int eflags);
135 size_t regerror(int errcode, const regex_t *preg, char *errbuf,
136                 size_t errbuf_size);
137 void regfree(regex_t *preg);
138
139 #endif /* REGEXPR_H */