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 *
11 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
13 ***************************************************************************/
15 #include "form.priv.h"
17 MODULE_ID("$Id: fty_regex.c,v 1.16 2002/07/06 15:33:27 juergen Exp $")
19 #if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */
25 unsigned long *refCount;
28 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
32 static char *RegEx_Init(char *instring)
38 static char *RegEx_Error(int code)
44 #define INIT register char *sp = RegEx_Init(instring);
45 #define GETC() (*sp++)
47 #define UNGETC(c) (--sp)
48 #define RETURN(c) return(c)
49 #define ERROR(c) return RegEx_Error(c)
51 #if HAVE_REGEXP_H_FUNCS
59 char *compiled_expression;
60 unsigned long *refCount;
63 /* Maximum Length we allow for a compiled regular expression */
64 #define MAX_RX_LEN (2048)
65 #define RX_INCREMENT (256)
69 /*---------------------------------------------------------------------------
71 | Function : static void *Make_RegularExpression_Type(va_list * ap)
73 | Description : Allocate structure for regex type argument.
75 | Return Values : Pointer to argument structure or NULL on error
76 +--------------------------------------------------------------------------*/
77 static void *Make_RegularExpression_Type(va_list * ap)
79 #if HAVE_REGEX_H_FUNCS
80 char *rx = va_arg(*ap,char *);
83 preg = (RegExp_Arg*)malloc(sizeof(RegExp_Arg));
86 if (((preg->pRegExp = (regex_t*)malloc(sizeof(regex_t))) != (regex_t*)0)
87 && !regcomp(preg->pRegExp,rx,
88 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE) ))
90 preg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
91 *(preg->refCount) = 1;
98 preg = (RegExp_Arg*)0;
101 return((void *)preg);
102 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
103 char *rx = va_arg(*ap,char *);
106 pArg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg));
110 int blen = RX_INCREMENT;
111 pArg->compiled_expression = NULL;
112 pArg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
113 *(pArg->refCount) = 1;
116 char *buf = (char *)malloc(blen);
119 #if HAVE_REGEXP_H_FUNCS
120 char *last_pos = compile (rx, buf, &buf[blen], '\0');
121 #else /* HAVE_REGEXPR_H_FUNCS */
122 char *last_pos = compile (rx, buf, &buf[blen]);
128 blen += RX_INCREMENT;
138 pArg->compiled_expression = buf;
142 } while( blen <= MAX_RX_LEN );
144 if (pArg && !pArg->compiled_expression)
155 /*---------------------------------------------------------------------------
156 | Facility : libnform
157 | Function : static void *Copy_RegularExpression_Type(
160 | Description : Copy structure for regex type argument.
162 | Return Values : Pointer to argument structure or NULL on error.
163 +--------------------------------------------------------------------------*/
164 static void *Copy_RegularExpression_Type(const void * argp)
166 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
167 const RegExp_Arg *ap = (const RegExp_Arg *)argp;
168 const RegExp_Arg *result = (const RegExp_Arg *)0;
172 *(ap->refCount) += 1;
175 return (void *)result;
181 /*---------------------------------------------------------------------------
182 | Facility : libnform
183 | Function : static void Free_RegularExpression_Type(void * argp)
185 | Description : Free structure for regex type argument.
188 +--------------------------------------------------------------------------*/
189 static void Free_RegularExpression_Type(void * argp)
191 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
192 RegExp_Arg *ap = (RegExp_Arg *)argp;
195 if (--(*(ap->refCount)) == 0)
197 #if HAVE_REGEX_H_FUNCS
201 regfree(ap->pRegExp);
203 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
204 if (ap->compiled_expression)
207 free(ap->compiled_expression);
216 /*---------------------------------------------------------------------------
217 | Facility : libnform
218 | Function : static bool Check_RegularExpression_Field(
222 | Description : Validate buffer content to be a valid regular expression
224 | Return Values : TRUE - field is valid
225 | FALSE - field is invalid
226 +--------------------------------------------------------------------------*/
227 static bool Check_RegularExpression_Field(FIELD * field, const void * argp)
230 #if HAVE_REGEX_H_FUNCS
231 const RegExp_Arg *ap = (const RegExp_Arg*)argp;
232 if (ap && ap->pRegExp)
233 match = (regexec(ap->pRegExp,field_buffer(field,0),0,NULL,0) ? FALSE:TRUE);
234 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
235 RegExp_Arg *ap = (RegExp_Arg *)argp;
236 if (ap && ap->compiled_expression)
237 match = (step(field_buffer(field,0),ap->compiled_expression) ? TRUE:FALSE);
242 static FIELDTYPE typeREGEXP = {
243 _HAS_ARGS | _RESIDENT,
244 1, /* this is mutable, so we can't be const */
247 Make_RegularExpression_Type,
248 Copy_RegularExpression_Type,
249 Free_RegularExpression_Type,
250 Check_RegularExpression_Field,
256 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP;
258 /* fty_regex.c ends here */