1 /****************************************************************************
2 * Copyright (c) 1998-2015,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /***************************************************************************
31 * Author : Juergen Pfeifer *
33 ***************************************************************************/
35 #include "form.priv.h"
37 MODULE_ID("$Id: fty_regex.c,v 1.27 2018/07/14 21:41:39 tom Exp $")
39 #if HAVE_REGEX_H_FUNCS || HAVE_LIB_PCRE2 /* We prefer POSIX regex */
41 #if HAVE_PCRE2_POSIX_H
42 #include <pcre2-posix.h>
43 #elif HAVE_PCREPOSIX_H
44 #include <pcreposix.h>
52 unsigned long *refCount;
56 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
61 RegEx_Init(char *instring)
74 #define INIT register char *sp = RegEx_Init(instring);
75 #define GETC() (*sp++)
77 #define UNGETC(c) (--sp)
78 #define RETURN(c) return(c)
79 #define ERROR(c) return RegEx_Error(c)
81 #if HAVE_REGEXP_H_FUNCS
89 char *compiled_expression;
90 unsigned long *refCount;
94 /* Maximum Length we allow for a compiled regular expression */
95 #define MAX_RX_LEN (2048)
96 #define RX_INCREMENT (256)
100 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
101 # define MAYBE_UNUSED
103 # define MAYBE_UNUSED GCC_UNUSED
106 /*---------------------------------------------------------------------------
107 | Facility : libnform
108 | Function : static void *Generic_RegularExpression_Type(void * arg)
110 | Description : Allocate structure for regex type argument.
112 | Return Values : Pointer to argument structure or NULL on error
113 +--------------------------------------------------------------------------*/
115 Generic_RegularExpression_Type(void *arg MAYBE_UNUSED)
117 #if HAVE_REGEX_H_FUNCS
118 char *rx = (char *)arg;
119 RegExp_Arg *preg = (RegExp_Arg *)0;
123 preg = typeCalloc(RegExp_Arg, 1);
127 T((T_CREATE("RegExp_Arg %p"), (void *)preg));
128 if (((preg->pRegExp = typeMalloc(regex_t, 1)) != 0)
129 && !regcomp(preg->pRegExp, rx,
130 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE)))
132 T((T_CREATE("regex_t %p"), (void *)preg->pRegExp));
133 if ((preg->refCount = typeMalloc(unsigned long, 1)) != 0)
134 *(preg->refCount) = 1;
141 preg = (RegExp_Arg *)0;
145 return ((void *)preg);
146 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
147 char *rx = (char *)arg;
148 RegExp_Arg *pArg = (RegExp_Arg *)0;
152 pArg = typeMalloc(RegExp_Arg, 1);
156 int blen = RX_INCREMENT;
158 T((T_CREATE("RegExp_Arg %p"), pArg));
159 pArg->compiled_expression = NULL;
160 if ((pArg->refCount = typeMalloc(unsigned long, 1)) != 0)
161 *(pArg->refCount) = 1;
165 char *buf = typeMalloc(char, blen);
169 #if HAVE_REGEXP_H_FUNCS
170 char *last_pos = compile(rx, buf, &buf[blen], '\0');
172 #else /* HAVE_REGEXPR_H_FUNCS */
173 char *last_pos = compile(rx, buf, &buf[blen]);
179 blen += RX_INCREMENT;
189 pArg->compiled_expression = buf;
194 while (blen <= MAX_RX_LEN);
196 if (pArg && !pArg->compiled_expression)
208 /*---------------------------------------------------------------------------
209 | Facility : libnform
210 | Function : static void *Make_RegularExpression_Type(va_list * ap)
212 | Description : Allocate structure for regex type argument.
214 | Return Values : Pointer to argument structure or NULL on error
215 +--------------------------------------------------------------------------*/
217 Make_RegularExpression_Type(va_list *ap)
219 char *rx = va_arg(*ap, char *);
221 return Generic_RegularExpression_Type((void *)rx);
224 /*---------------------------------------------------------------------------
225 | Facility : libnform
226 | Function : static void *Copy_RegularExpression_Type(
229 | Description : Copy structure for regex type argument.
231 | Return Values : Pointer to argument structure or NULL on error.
232 +--------------------------------------------------------------------------*/
234 Copy_RegularExpression_Type(const void *argp MAYBE_UNUSED)
236 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
237 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
238 const RegExp_Arg *result = (const RegExp_Arg *)0;
242 *(ap->refCount) += 1;
245 return (void *)result;
251 /*---------------------------------------------------------------------------
252 | Facility : libnform
253 | Function : static void Free_RegularExpression_Type(void * argp)
255 | Description : Free structure for regex type argument.
258 +--------------------------------------------------------------------------*/
260 Free_RegularExpression_Type(void *argp MAYBE_UNUSED)
262 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
263 RegExp_Arg *ap = (RegExp_Arg *)argp;
267 if (--(*(ap->refCount)) == 0)
269 #if HAVE_REGEX_H_FUNCS
273 regfree(ap->pRegExp);
276 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
277 if (ap->compiled_expression)
280 free(ap->compiled_expression);
289 /*---------------------------------------------------------------------------
290 | Facility : libnform
291 | Function : static bool Check_RegularExpression_Field(
295 | Description : Validate buffer content to be a valid regular expression
297 | Return Values : TRUE - field is valid
298 | FALSE - field is invalid
299 +--------------------------------------------------------------------------*/
301 Check_RegularExpression_Field(FIELD *field MAYBE_UNUSED,
302 const void *argp MAYBE_UNUSED)
306 #if HAVE_REGEX_H_FUNCS
307 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
309 if (ap && ap->pRegExp)
310 match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0)
313 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
314 RegExp_Arg *ap = (RegExp_Arg *)argp;
316 if (ap && ap->compiled_expression)
317 match = (step(field_buffer(field, 0), ap->compiled_expression)
324 static FIELDTYPE typeREGEXP =
326 _HAS_ARGS | _RESIDENT,
327 1, /* this is mutable, so we can't be const */
330 Make_RegularExpression_Type,
331 Copy_RegularExpression_Type,
332 Free_RegularExpression_Type,
333 INIT_FT_FUNC(Check_RegularExpression_Field),
337 #if NCURSES_INTEROP_FUNCS
338 Generic_RegularExpression_Type
342 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP;
344 #if NCURSES_INTEROP_FUNCS
345 /* The next routines are to simplify the use of ncurses from
346 programming languages with restictions on interop with C level
347 constructs (e.g. variable access or va_list + ellipsis constructs)
349 NCURSES_EXPORT(FIELDTYPE *)
350 _nc_TYPE_REGEXP(void)
356 /* fty_regex.c ends here */