1 /****************************************************************************
2 * Copyright 2018,2020 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.29 2020/02/29 15:46:00 anonymous.maarten Exp $")
40 #if HAVE_REGEX_H_FUNCS || HAVE_LIB_PCRE2 /* We prefer POSIX regex */
43 #include <pcre2posix.h>
44 #elif HAVE_PCREPOSIX_H
45 #include <pcreposix.h>
53 unsigned long *refCount;
57 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
62 RegEx_Init(char *instring)
75 #define INIT register char *sp = RegEx_Init(instring);
76 #define GETC() (*sp++)
78 #define UNGETC(c) (--sp)
79 #define RETURN(c) return(c)
80 #define ERROR(c) return RegEx_Error(c)
82 #if HAVE_REGEXP_H_FUNCS
90 char *compiled_expression;
91 unsigned long *refCount;
95 /* Maximum Length we allow for a compiled regular expression */
96 #define MAX_RX_LEN (2048)
97 #define RX_INCREMENT (256)
101 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
102 # define MAYBE_UNUSED
104 # define MAYBE_UNUSED GCC_UNUSED
107 /*---------------------------------------------------------------------------
108 | Facility : libnform
109 | Function : static void *Generic_RegularExpression_Type(void * arg)
111 | Description : Allocate structure for regex type argument.
113 | Return Values : Pointer to argument structure or NULL on error
114 +--------------------------------------------------------------------------*/
116 Generic_RegularExpression_Type(void *arg MAYBE_UNUSED)
118 #if HAVE_REGEX_H_FUNCS
119 char *rx = (char *)arg;
120 RegExp_Arg *preg = (RegExp_Arg *)0;
124 preg = typeCalloc(RegExp_Arg, 1);
128 T((T_CREATE("RegExp_Arg %p"), (void *)preg));
129 if (((preg->pRegExp = typeMalloc(regex_t, 1)) != 0)
130 && !regcomp(preg->pRegExp, rx,
131 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE)))
133 T((T_CREATE("regex_t %p"), (void *)preg->pRegExp));
134 if ((preg->refCount = typeMalloc(unsigned long, 1)) != 0)
135 *(preg->refCount) = 1;
142 preg = (RegExp_Arg *)0;
146 return ((void *)preg);
147 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
148 char *rx = (char *)arg;
149 RegExp_Arg *pArg = (RegExp_Arg *)0;
153 pArg = typeMalloc(RegExp_Arg, 1);
157 int blen = RX_INCREMENT;
159 T((T_CREATE("RegExp_Arg %p"), pArg));
160 pArg->compiled_expression = NULL;
161 if ((pArg->refCount = typeMalloc(unsigned long, 1)) != 0)
162 *(pArg->refCount) = 1;
166 char *buf = typeMalloc(char, blen);
170 #if HAVE_REGEXP_H_FUNCS
171 char *last_pos = compile(rx, buf, &buf[blen], '\0');
173 #else /* HAVE_REGEXPR_H_FUNCS */
174 char *last_pos = compile(rx, buf, &buf[blen]);
180 blen += RX_INCREMENT;
190 pArg->compiled_expression = buf;
195 while (blen <= MAX_RX_LEN);
197 if (pArg && !pArg->compiled_expression)
209 /*---------------------------------------------------------------------------
210 | Facility : libnform
211 | Function : static void *Make_RegularExpression_Type(va_list * ap)
213 | Description : Allocate structure for regex type argument.
215 | Return Values : Pointer to argument structure or NULL on error
216 +--------------------------------------------------------------------------*/
218 Make_RegularExpression_Type(va_list *ap)
220 char *rx = va_arg(*ap, char *);
222 return Generic_RegularExpression_Type((void *)rx);
225 /*---------------------------------------------------------------------------
226 | Facility : libnform
227 | Function : static void *Copy_RegularExpression_Type(
230 | Description : Copy structure for regex type argument.
232 | Return Values : Pointer to argument structure or NULL on error.
233 +--------------------------------------------------------------------------*/
235 Copy_RegularExpression_Type(const void *argp MAYBE_UNUSED)
237 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
238 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
239 const RegExp_Arg *result = (const RegExp_Arg *)0;
243 *(ap->refCount) += 1;
246 return (void *)result;
252 /*---------------------------------------------------------------------------
253 | Facility : libnform
254 | Function : static void Free_RegularExpression_Type(void * argp)
256 | Description : Free structure for regex type argument.
259 +--------------------------------------------------------------------------*/
261 Free_RegularExpression_Type(void *argp MAYBE_UNUSED)
263 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
264 RegExp_Arg *ap = (RegExp_Arg *)argp;
268 if (--(*(ap->refCount)) == 0)
270 #if HAVE_REGEX_H_FUNCS
274 regfree(ap->pRegExp);
277 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
278 if (ap->compiled_expression)
281 free(ap->compiled_expression);
290 /*---------------------------------------------------------------------------
291 | Facility : libnform
292 | Function : static bool Check_RegularExpression_Field(
296 | Description : Validate buffer content to be a valid regular expression
298 | Return Values : TRUE - field is valid
299 | FALSE - field is invalid
300 +--------------------------------------------------------------------------*/
302 Check_RegularExpression_Field(FIELD *field MAYBE_UNUSED,
303 const void *argp MAYBE_UNUSED)
307 #if HAVE_REGEX_H_FUNCS
308 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
310 if (ap && ap->pRegExp)
311 match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0)
314 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
315 RegExp_Arg *ap = (RegExp_Arg *)argp;
317 if (ap && ap->compiled_expression)
318 match = (step(field_buffer(field, 0), ap->compiled_expression)
325 static FIELDTYPE typeREGEXP =
327 _HAS_ARGS | _RESIDENT,
328 1, /* this is mutable, so we can't be const */
331 Make_RegularExpression_Type,
332 Copy_RegularExpression_Type,
333 Free_RegularExpression_Type,
334 INIT_FT_FUNC(Check_RegularExpression_Field),
338 #if NCURSES_INTEROP_FUNCS
339 Generic_RegularExpression_Type
343 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP;
345 #if NCURSES_INTEROP_FUNCS
346 /* The next routines are to simplify the use of ncurses from
347 programming languages with restictions on interop with C level
348 constructs (e.g. variable access or va_list + ellipsis constructs)
350 NCURSES_EXPORT(FIELDTYPE *)
351 _nc_TYPE_REGEXP(void)
357 /* fty_regex.c ends here */