-
Notifications
You must be signed in to change notification settings - Fork 37
/
cligen_regex.c
355 lines (323 loc) · 9.31 KB
/
cligen_regex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
CLI generator regular expressions
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2001-2022 Olof Hagsand
This file is part of CLIgen.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2, indicate
your decision by deleting the provisions above and replace them with the
notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
This file includes support for regular expressions
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <regex.h>
#ifdef HAVE_LIBXML_XMLREGEXP_H
#include <libxml/xmlregexp.h>
#endif
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_parsetree.h"
#include "cligen_pt_head.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_handle.h"
#include "cligen_regex.h"
/*-------------------------- POSIX -------------------------*/
/*! Compile a regexp according to posix regexps
*
* It is implicitly assumed that the match should be done at the beginning and
* the end,
* therefore, the pattern is prefixed with a ^, and postfixed with a $.
* So, a match is made if <pattern> matches the beginning of <string>.
* For example <pattern> "foobar" will not match <string> "foo".
*
* @param[in] regexp Regular expression string in XSD regex format
* @param[out] recomp Compiled regular expression (malloc:d, should be freed)
* @retval 1 OK
* @retval 0 Invalid regular expression (syntax error?)
* @retval -1 Error
*/
int
cligen_regex_posix_compile(char *regexp,
void **recomp)
{
int retval = -1;
cbuf *cb = NULL;
regex_t *re = NULL;
int len0;
int err;
len0 = strlen(regexp);
if ((cb = cbuf_new()) == NULL)
goto done;
/* Check if prepended by ^ */
if (len0 > 0 && regexp[0] == '^'){
if (regexp[len0-1] == '$')
cprintf(cb, "%s", regexp);
else if (len0 > 1 && regexp[1] == '(')
cprintf(cb, "%s)$", regexp);
else
cprintf(cb, "%s$", regexp);
}
/* Check if terminated by $ only */
else if (len0 > 0 && regexp[len0-1] == '$'){
if (len0 > 1 && regexp[len0-2] == ')')
cprintf(cb, "^(%s", regexp);
else
cprintf(cb, "^%s", regexp);
}
else /* Neither ^or $ */
cprintf(cb, "^(%s)$", regexp);
if ((re = malloc(sizeof(regex_t))) == NULL)
goto done;
memset(re, 0, sizeof(regex_t));
if ((err = regcomp(re, cbuf_get(cb), REG_NOSUB|REG_EXTENDED)) != 0) {
char errbuf[1024] = {0,};
if (regerror(err, re, errbuf, 1000) < 0){
perror("regerror");
goto done;
}
goto fail;
}
*recomp = re;
re = NULL;
retval = 1;
done:
if (re)
free(re);
if (cb)
cbuf_free(cb);
return retval;
fail:
retval = 0;
goto done;
}
/*! Exec a regexp according to posix regexp
*
* @param[in] recomp Compiled regular expression (malloc:d, should be freed)
* @param[in] string Content string to match
* @retval 1 Match
* @retval 0 No match
* @retval -1 Error
*/
int
cligen_regex_posix_exec(void *recomp,
char *string)
{
int retval = -1;
int status;
regex_t *re;
char errbuf[1024];
re = (regex_t*)recomp;
status = regexec(re, string, (size_t) 0, NULL, 0);
if (status == 0)
retval = 1;
else {
regerror(status, re, errbuf, sizeof(errbuf)); /* XXX error is ignored */
retval = 0;
}
return retval;
}
/*! Free compiled regular expression i
*
* @param[in] recomp Compiled regular expression
*/
int
cligen_regex_posix_free(void *recomp)
{
if (recomp){
regfree(recomp);
}
return 0;
}
/*-------------------------- Libxml2 -----------------------------------*/
/*! Compile a regexp according to libxml regex
*
* @param[in] regexp Regular expression string in XSD regex format
* @param[out] recomp Compiled regular expression (malloc:d, should be freed)
* @retval 1 OK
* @retval 0 Invalid regular expression (syntax error?)
* @retval -1 Error
*/
int
cligen_regex_libxml2_compile(char *regexp0,
void **recomp)
{
int retval = -1;
#ifdef HAVE_LIBXML_XMLREGEXP_H
xmlChar *regexp = (xmlChar*)regexp0;
xmlRegexp *xrp = NULL;
if ((xrp = xmlRegexpCompile(regexp)) == NULL)
goto fail;
*recomp = xrp;
retval = 1;
done:
return retval;
fail:
retval = 0;
goto done;
#endif
return retval;
}
/*! Exec a regexp according to libxml2
*
* @param[in] recomp Compiled regular expression (malloc:d, should be freed)
* @param[in] string Content string to match
* @retval 1 Match
* @retval 0 No match
* @retval -1 Error
*/
int
cligen_regex_libxml2_exec(void *recomp,
char *string0)
{
int retval = -1;
#ifdef HAVE_LIBXML_XMLREGEXP_H
xmlChar *content = (xmlChar*)string0;
xmlRegexp *xrp = (xmlRegexp *)recomp;
/* Returns 1 if matches, 0 if not and a negative value in case of error */
if ((retval = xmlRegexpExec(xrp, content)) < 0)
goto done;
done:
#endif
return retval;
}
/*! Free compiled regular expression
*
* @param[in] recomp Compiled regular expression (malloc:d, should be freed)
*/
int
cligen_regex_libxml2_free(void *recomp)
{
#ifdef HAVE_LIBXML_XMLREGEXP_H
if (recomp)
xmlRegFreeRegexp(recomp);
#endif
return 0;
}
/*-------------------------- Generic -----------------------------------*/
/*! Compilation of regular expression / pattern
*
* @param[in] h Clicon handle
* @param[in] regexp Regular expression string in XSD regex format
* @param[out] recomp Compiled regular expression (malloc:d, should be freed)
* @retval 1 OK
* @retval 0 Invalid regular expression (syntax error?)
* @retval -1 Error
*/
int
cligen_regex_compile(cligen_handle h,
char *regexp,
void **recomp)
{
int retval = -1;
if (cligen_regex_xsd(h) == 0)
retval = cligen_regex_posix_compile(regexp, recomp);
else
retval = cligen_regex_libxml2_compile(regexp, recomp);
return retval;
}
/*! Execution of (pre-compiled) regular expression / pattern
*
* @param[in] h Clicon handle
* @param[in] recomp Compiled regexp
*/
int
cligen_regex_exec(cligen_handle h,
void *recomp,
char *string)
{
int retval = -1;
if (cligen_regex_xsd(h) == 0)
retval = cligen_regex_posix_exec(recomp, string);
else
retval = cligen_regex_libxml2_exec(recomp, string);
return retval;
}
/*! Free compiled regular expression
*
* @param[in] h Clicon handle
* @param[in] recomp Compiled regular expression
*/
int
cligen_regex_free(cligen_handle h,
void *recomp)
{
int retval = -1;
if (cligen_regex_xsd(h) == 0) {
retval = cligen_regex_posix_free(recomp);
free(recomp);
}
else
retval = cligen_regex_libxml2_free(recomp);
return retval;
}
/*! Makes a regexp check of <string> with <pattern>.
*
* @param[in] h Clicon handle
* @param[in] string Content string to match
* @param[in] pattern Pattern string to match
* @param[in] invert Invert match
* @retval 1 Match
* @retval 0 No match
* @retval -1 Error,
*/
int
match_regexp(cligen_handle h,
char *string,
char *pattern,
int invert)
{
int retval = -1;
int ret;
void *re = NULL;
if (string == NULL || pattern == NULL){
errno = EINVAL;
goto done;
}
if ((ret = cligen_regex_compile(h, pattern, &re)) < 0)
goto done;
if (ret == 0)
goto fail;
if ((ret = cligen_regex_exec(h, re, string)) < 0)
goto done;
if (invert)
ret = !ret;
if (ret == 0)
goto fail;
retval = 1;
done:
if (re != NULL)
cligen_regex_free(h, re);
return retval;
fail:
retval = 0;
goto done;
}