1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2006-2010 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2016 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * regexpr.h
24  *
25  * Author: Tatu Ylonen <ylo@ngs.fi>
26  *
27  * Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
28  *
29  * Permission to use, copy, modify, distribute, and sell this software
30  * and its documentation for any purpose is hereby granted without fee,
31  * provided that the above copyright notice appear in all copies.  This
32  * software is provided "as is" without express or implied warranty.
33  *
34  * Created: Thu Sep 26 17:15:36 1991 ylo
35  * Last modified: Mon Nov  4 15:49:46 1991 ylo
36  *
37  * Modified to work with C++ for use in BAREOS,
38  * Kern Sibbald April, 2006
39  */
40 /**
41  * @file
42  * regexpr.h
43  * regular expression header file
44  *
45  */
46 
47 #ifndef BAREOS_LIB_BREGEX_H_
48 #define BAREOS_LIB_BREGEX_H_
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #ifndef REGEXPR_H
54 #define REGEXPR_H
55 
56 /* If we pull in this header, make sure we only get our own library
57  *  bregex.c
58  */
59 #define regex_t b_regex_t
60 #define regmatch_t b_regmatch_t
61 #define re_syntax b_re_syntax
62 #define re_syntax_table b_re_syntax_table
63 #define ReCompileInitialize b_re_compile_initialize
64 #define ReSetSyntax b_re_set_syntax
65 #define re_compile_pattern b_re_compile_pattern
66 #define ReMatch b_re_match
67 #define ReSearch b_re_search
68 #define ReCompileFastmap b_re_compile_fastmap
69 #define re_comp b_re_comp
70 #define re_exec b_re_exec
71 #define regcomp b_regcomp
72 #define regexec b_regexec
73 #define regerror b_regerror
74 #define regfree b_regfree
75 
76 
77 #define RE_NREGS 100 /* number of registers available */
78 
79 #define regoff_t int
80 
81 typedef struct {
82   regoff_t rm_so;
83   regoff_t rm_eo;
84 } regmatch_t;
85 
86 
87 #define REG_EXTENDED (1 << 1)
88 #define REG_ICASE (1 << 2)
89 #define REG_NOSUB (1 << 3)
90 #define REG_NEWLINE (1 << 4)
91 #define REG_NOTBOL (1 << 5)
92 
93 #define REG_NOMATCH -1
94 
95 /* clang-format off */
96 struct regex_t {
97   unsigned char* buffer;  /* compiled pattern */
98   int allocated;                /* allocated size of compiled pattern */
99   int used;                     /* actual length of compiled pattern */
100   unsigned char* fastmap; /* fastmap[ch] is true if ch can start pattern */
101   unsigned char* translate; /* translation to apply during compilation/matching */
102   unsigned char fastmap_accurate; /* true if fastmap is valid */
103   unsigned char can_be_null;      /* true if can match empty string */
104   unsigned char uses_registers;   /* registers are used and need to be initialized */
105   int num_registers;    /* number of registers used */
106   unsigned char anchor; /* anchor: 0=none 1=begline 2=begbuf */
107   char* errmsg;
108   int cflags;           /* compilation flags */
109   POOLMEM* lcase; /* used by REG_ICASE */
110 };
111 /* clang-format on */
112 
113 
114 typedef struct re_registers {
115   int start[RE_NREGS]; /* start offset of region */
116   int end[RE_NREGS];   /* end offset of region */
117 } * regexp_registers_t;
118 
119 /* bit definitions for syntax */
120 #define RE_NO_BK_PARENS 1        /* no quoting for parentheses */
121 #define RE_NO_BK_VBAR 2          /* no quoting for vertical bar */
122 #define RE_BK_PLUS_QM 4          /* quoting needed for + and ? */
123 #define RE_TIGHT_VBAR 8          /* | binds tighter than ^ and $ */
124 #define RE_NEWLINE_OR 16         /* treat newline as or */
125 #define RE_CONTEXT_INDEP_OPS 32  /* ^$?*+ are special in all contexts */
126 #define RE_ANSI_HEX 64           /* ansi sequences (\n etc) and \xhh */
127 #define RE_NO_GNU_EXTENSIONS 128 /* no gnu extensions */
128 
129 /* definitions for some common regexp styles */
130 #define RE_SYNTAX_AWK (RE_NO_BK_PARENS | RE_NO_BK_VBAR | RE_CONTEXT_INDEP_OPS)
131 #define RE_SYNTAX_EGREP (RE_SYNTAX_AWK | RE_NEWLINE_OR)
132 #define RE_SYNTAX_GREP (RE_BK_PLUS_QM | RE_NEWLINE_OR)
133 #define RE_SYNTAX_EMACS 0
134 
135 #define Sword 1
136 #define Swhitespace 2
137 #define Sdigit 4
138 #define Soctaldigit 8
139 #define Shexdigit 16
140 
141 /* Rename all exported symbols to avoid conflicts with similarly named
142    symbols in some systems' standard C libraries... */
143 
144 
145 extern int re_syntax;
146 /* This is the actual syntax mask.  It was added so that Python could do
147  * syntax-dependent munging of patterns before compilation. */
148 
149 extern unsigned char re_syntax_table[256];
150 
151 void ReCompileInitialize(void);
152 
153 int ReSetSyntax(int syntax);
154 /* This sets the syntax to use and returns the previous syntax.  The
155  * syntax is specified by a bit mask of the above defined bits. */
156 
157 const char* re_compile_pattern(regex_t* compiled, unsigned char* regex);
158 /* This compiles the regexp (given in regex and length in regex_size).
159  * This returns NULL if the regexp compiled successfully, and an error
160  * message if an error was encountered.  The buffer field must be
161  * initialized to a memory area allocated by malloc (or to NULL) before
162  * use, and the allocated field must be set to its length (or 0 if
163  * buffer is NULL).  Also, the translate field must be set to point to a
164  * valid translation table, or NULL if it is not used. */
165 
166 int ReMatch(regex_t* compiled,
167             unsigned char* string,
168             int size,
169             int pos,
170             regexp_registers_t old_regs);
171 /* This tries to match the regexp against the string.  This returns the
172  * length of the matched portion, or -1 if the pattern could not be
173  * matched and -2 if an error (such as failure stack overflow) is
174  * encountered. */
175 
176 int ReSearch(regex_t* compiled,
177              unsigned char* string,
178              int size,
179              int startpos,
180              int range,
181              regexp_registers_t regs);
182 /* This searches for a substring matching the regexp.  This returns the
183  * first index at which a match is found.  range specifies at how many
184  * positions to try matching; positive values indicate searching
185  * forwards, and negative values indicate searching backwards.  mstop
186  * specifies the offset beyond which a match must not go.  This returns
187  * -1 if no match is found, and -2 if an error (such as failure stack
188  * overflow) is encountered. */
189 
190 void ReCompileFastmap(regex_t* compiled);
191 /* This computes the fastmap for the regexp.  For this to have any effect,
192  * the calling program must have initialized the fastmap field to point
193  * to an array of 256 characters. */
194 
195 
196 int regcomp(regex_t* preg, const char* regex, int cflags);
197 int regexec(regex_t* preg,
198             const char* string,
199             size_t nmatch,
200             regmatch_t pmatch[],
201             int eflags);
202 size_t regerror(int errcode, regex_t* preg, char* errbuf, size_t errbuf_size);
203 void regfree(regex_t* preg);
204 
205 #endif /* REGEXPR_H */
206 
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 #endif /* !BAREOS_LIB_BREGEX_H_ */
212