3 * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
4 * You may freely copy it for use as a template for your own field types.
5 * If you develop a field type that might be of general use, please send
6 * it back to the ncurses maintainers for inclusion in the next version.
8 /***************************************************************************
10 * Author : Juergen Pfeifer *
12 ***************************************************************************/
14 #include "form.priv.h"
16 MODULE_ID("$Id: fty_regex.c,v 1.18 2004/05/29 19:19:09 tom Exp $")
18 #if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */
24 unsigned long *refCount;
28 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
33 RegEx_Init(char *instring)
46 #define INIT register char *sp = RegEx_Init(instring);
47 #define GETC() (*sp++)
49 #define UNGETC(c) (--sp)
50 #define RETURN(c) return(c)
51 #define ERROR(c) return RegEx_Error(c)
53 #if HAVE_REGEXP_H_FUNCS
61 char *compiled_expression;
62 unsigned long *refCount;
66 /* Maximum Length we allow for a compiled regular expression */
67 #define MAX_RX_LEN (2048)
68 #define RX_INCREMENT (256)
72 /*---------------------------------------------------------------------------
74 | Function : static void *Make_RegularExpression_Type(va_list * ap)
76 | Description : Allocate structure for regex type argument.
78 | Return Values : Pointer to argument structure or NULL on error
79 +--------------------------------------------------------------------------*/
81 Make_RegularExpression_Type(va_list *ap)
83 #if HAVE_REGEX_H_FUNCS
84 char *rx = va_arg(*ap, char *);
87 preg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg));
91 if (((preg->pRegExp = (regex_t *) malloc(sizeof(regex_t))) != 0)
92 && !regcomp(preg->pRegExp, rx,
93 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE)))
95 preg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
97 *(preg->refCount) = 1;
104 preg = (RegExp_Arg *)0;
107 return ((void *)preg);
108 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
109 char *rx = va_arg(*ap, char *);
112 pArg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg));
116 int blen = RX_INCREMENT;
118 pArg->compiled_expression = NULL;
119 pArg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
121 *(pArg->refCount) = 1;
125 char *buf = (char *)malloc(blen);
129 #if HAVE_REGEXP_H_FUNCS
130 char *last_pos = compile(rx, buf, &buf[blen], '\0');
132 #else /* HAVE_REGEXPR_H_FUNCS */
133 char *last_pos = compile(rx, buf, &buf[blen]);
139 blen += RX_INCREMENT;
149 pArg->compiled_expression = buf;
154 while (blen <= MAX_RX_LEN);
156 if (pArg && !pArg->compiled_expression)
167 /*---------------------------------------------------------------------------
168 | Facility : libnform
169 | Function : static void *Copy_RegularExpression_Type(
172 | Description : Copy structure for regex type argument.
174 | Return Values : Pointer to argument structure or NULL on error.
175 +--------------------------------------------------------------------------*/
177 Copy_RegularExpression_Type(const void *argp)
179 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
180 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
181 const RegExp_Arg *result = (const RegExp_Arg *)0;
185 *(ap->refCount) += 1;
188 return (void *)result;
194 /*---------------------------------------------------------------------------
195 | Facility : libnform
196 | Function : static void Free_RegularExpression_Type(void * argp)
198 | Description : Free structure for regex type argument.
201 +--------------------------------------------------------------------------*/
203 Free_RegularExpression_Type(void *argp)
205 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
206 RegExp_Arg *ap = (RegExp_Arg *)argp;
210 if (--(*(ap->refCount)) == 0)
212 #if HAVE_REGEX_H_FUNCS
216 regfree(ap->pRegExp);
218 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
219 if (ap->compiled_expression)
222 free(ap->compiled_expression);
231 /*---------------------------------------------------------------------------
232 | Facility : libnform
233 | Function : static bool Check_RegularExpression_Field(
237 | Description : Validate buffer content to be a valid regular expression
239 | Return Values : TRUE - field is valid
240 | FALSE - field is invalid
241 +--------------------------------------------------------------------------*/
243 Check_RegularExpression_Field(FIELD *field, const void *argp)
247 #if HAVE_REGEX_H_FUNCS
248 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
250 if (ap && ap->pRegExp)
251 match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0)
254 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
255 RegExp_Arg *ap = (RegExp_Arg *)argp;
257 if (ap && ap->compiled_expression)
258 match = (step(field_buffer(field, 0), ap->compiled_expression)
265 static FIELDTYPE typeREGEXP =
267 _HAS_ARGS | _RESIDENT,
268 1, /* this is mutable, so we can't be const */
271 Make_RegularExpression_Type,
272 Copy_RegularExpression_Type,
273 Free_RegularExpression_Type,
274 Check_RegularExpression_Field,
280 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP;
282 /* fty_regex.c ends here */