1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2012,2015 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /***************************************************************************
32 * Author : Juergen Pfeifer *
34 ***************************************************************************/
36 #include "form.priv.h"
38 MODULE_ID("$Id: fty_regex.c,v 1.33 2021/08/14 15:01:52 tom Exp $")
40 #if HAVE_REGEX_H_FUNCS || HAVE_LIB_PCRE2 /* We prefer POSIX regex */
43 #include <pcre2posix.h>
45 /* pcre2 used to provide its "POSIX" entrypoints using the same names as the
46 * standard ones in the C runtime, but that never worked because the linker
47 * would use the C runtime. Debian patched the library to fix this symbol
48 * conflict, but overlooked the header file, and Debian's patch was made
49 * obsolete when pcre2 was changed early in 2019 to provide different names.
51 * Here is a workaround to make the older version of Debian's package work.
53 #if !defined(PCRE2regcomp) && defined(HAVE_PCRE2REGCOMP)
63 PCRE2POSIX_EXP_DECL int PCRE2regcomp(regex_t *, const char *, int);
64 PCRE2POSIX_EXP_DECL int PCRE2regexec(const regex_t *, const char *, size_t,
66 PCRE2POSIX_EXP_DECL void PCRE2regfree(regex_t *);
70 #define regcomp(r,s,n) PCRE2regcomp(r,s,n)
71 #define regexec(r,s,n,m,x) PCRE2regexec(r,s,n,m,x)
72 #define regfree(r) PCRE2regfree(r)
74 /* end workaround... */
75 #elif HAVE_PCREPOSIX_H
76 #include <pcreposix.h>
84 unsigned long *refCount;
88 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
93 RegEx_Init(char *instring)
100 RegEx_Error(int code)
106 #define INIT register char *sp = RegEx_Init(instring);
107 #define GETC() (*sp++)
108 #define PEEKC() (*sp)
109 #define UNGETC(c) (--sp)
110 #define RETURN(c) return(c)
111 #define ERROR(c) return RegEx_Error(c)
113 #if HAVE_REGEXP_H_FUNCS
121 char *compiled_expression;
122 unsigned long *refCount;
126 /* Maximum Length we allow for a compiled regular expression */
127 #define MAX_RX_LEN (2048)
128 #define RX_INCREMENT (256)
132 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
133 # define MAYBE_UNUSED
135 # define MAYBE_UNUSED GCC_UNUSED
138 /*---------------------------------------------------------------------------
139 | Facility : libnform
140 | Function : static void *Generic_RegularExpression_Type(void * arg)
142 | Description : Allocate structure for regex type argument.
144 | Return Values : Pointer to argument structure or NULL on error
145 +--------------------------------------------------------------------------*/
147 Generic_RegularExpression_Type(void *arg MAYBE_UNUSED)
149 #if HAVE_REGEX_H_FUNCS
150 char *rx = (char *)arg;
151 RegExp_Arg *preg = (RegExp_Arg *)0;
155 preg = typeCalloc(RegExp_Arg, 1);
159 T((T_CREATE("RegExp_Arg %p"), (void *)preg));
160 if (((preg->pRegExp = typeMalloc(regex_t, 1)) != 0)
161 && !regcomp(preg->pRegExp, rx,
162 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE)))
164 T((T_CREATE("regex_t %p"), (void *)preg->pRegExp));
165 if ((preg->refCount = typeMalloc(unsigned long, 1)) != 0)
166 *(preg->refCount) = 1;
173 preg = (RegExp_Arg *)0;
177 return ((void *)preg);
178 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
179 char *rx = (char *)arg;
180 RegExp_Arg *pArg = (RegExp_Arg *)0;
184 pArg = typeMalloc(RegExp_Arg, 1);
188 int blen = RX_INCREMENT;
190 T((T_CREATE("RegExp_Arg %p"), pArg));
191 pArg->compiled_expression = NULL;
192 if ((pArg->refCount = typeMalloc(unsigned long, 1)) != 0)
193 *(pArg->refCount) = 1;
197 char *buf = typeMalloc(char, blen);
201 #if HAVE_REGEXP_H_FUNCS
202 char *last_pos = compile(rx, buf, &buf[blen], '\0');
204 #else /* HAVE_REGEXPR_H_FUNCS */
205 char *last_pos = compile(rx, buf, &buf[blen]);
211 blen += RX_INCREMENT;
221 pArg->compiled_expression = buf;
226 while (blen <= MAX_RX_LEN);
228 if (pArg && !pArg->compiled_expression)
240 /*---------------------------------------------------------------------------
241 | Facility : libnform
242 | Function : static void *Make_RegularExpression_Type(va_list * ap)
244 | Description : Allocate structure for regex type argument.
246 | Return Values : Pointer to argument structure or NULL on error
247 +--------------------------------------------------------------------------*/
249 Make_RegularExpression_Type(va_list *ap)
251 char *rx = va_arg(*ap, char *);
253 return Generic_RegularExpression_Type((void *)rx);
256 /*---------------------------------------------------------------------------
257 | Facility : libnform
258 | Function : static void *Copy_RegularExpression_Type(
261 | Description : Copy structure for regex type argument.
263 | Return Values : Pointer to argument structure or NULL on error.
264 +--------------------------------------------------------------------------*/
266 Copy_RegularExpression_Type(const void *argp MAYBE_UNUSED)
268 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
269 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
270 const RegExp_Arg *result = (const RegExp_Arg *)0;
274 *(ap->refCount) += 1;
277 return (void *)result;
283 /*---------------------------------------------------------------------------
284 | Facility : libnform
285 | Function : static void Free_RegularExpression_Type(void * argp)
287 | Description : Free structure for regex type argument.
290 +--------------------------------------------------------------------------*/
292 Free_RegularExpression_Type(void *argp MAYBE_UNUSED)
294 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
295 RegExp_Arg *ap = (RegExp_Arg *)argp;
299 if (--(*(ap->refCount)) == 0)
301 #if HAVE_REGEX_H_FUNCS
305 regfree(ap->pRegExp);
308 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
309 if (ap->compiled_expression)
312 free(ap->compiled_expression);
321 /*---------------------------------------------------------------------------
322 | Facility : libnform
323 | Function : static bool Check_RegularExpression_Field(
327 | Description : Validate buffer content to be a valid regular expression
329 | Return Values : TRUE - field is valid
330 | FALSE - field is invalid
331 +--------------------------------------------------------------------------*/
333 Check_RegularExpression_Field(FIELD *field MAYBE_UNUSED,
334 const void *argp MAYBE_UNUSED)
338 #if HAVE_REGEX_H_FUNCS
339 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
341 if (ap && ap->pRegExp)
342 match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0)
345 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
346 RegExp_Arg *ap = (RegExp_Arg *)argp;
348 if (ap && ap->compiled_expression)
349 match = (step(field_buffer(field, 0), ap->compiled_expression)
356 static FIELDTYPE typeREGEXP =
358 _HAS_ARGS | _RESIDENT,
359 1, /* this is mutable, so we can't be const */
362 Make_RegularExpression_Type,
363 Copy_RegularExpression_Type,
364 Free_RegularExpression_Type,
365 INIT_FT_FUNC(Check_RegularExpression_Field),
369 #if NCURSES_INTEROP_FUNCS
370 Generic_RegularExpression_Type
374 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_REGEXP = &typeREGEXP;
376 #if NCURSES_INTEROP_FUNCS
377 /* The next routines are to simplify the use of ncurses from
378 programming languages with restrictions on interop with C level
379 constructs (e.g. variable access or va_list + ellipsis constructs)
381 FORM_EXPORT(FIELDTYPE *)
382 _nc_TYPE_REGEXP(void)
388 /* fty_regex.c ends here */