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.17 2003/10/25 14:54:48 tom Exp $")
18 #if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */
24 unsigned long *refCount
;
27 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
31 static char *RegEx_Init(char *instring
)
37 static char *RegEx_Error(int code
)
43 #define INIT register char *sp = RegEx_Init(instring);
44 #define GETC() (*sp++)
46 #define UNGETC(c) (--sp)
47 #define RETURN(c) return(c)
48 #define ERROR(c) return RegEx_Error(c)
50 #if HAVE_REGEXP_H_FUNCS
58 char *compiled_expression
;
59 unsigned long *refCount
;
62 /* Maximum Length we allow for a compiled regular expression */
63 #define MAX_RX_LEN (2048)
64 #define RX_INCREMENT (256)
68 /*---------------------------------------------------------------------------
70 | Function : static void *Make_RegularExpression_Type(va_list * ap)
72 | Description : Allocate structure for regex type argument.
74 | Return Values : Pointer to argument structure or NULL on error
75 +--------------------------------------------------------------------------*/
76 static void *Make_RegularExpression_Type(va_list * ap
)
78 #if HAVE_REGEX_H_FUNCS
79 char *rx
= va_arg(*ap
,char *);
82 preg
= (RegExp_Arg
*)malloc(sizeof(RegExp_Arg
));
85 if (((preg
->pRegExp
= (regex_t
*)malloc(sizeof(regex_t
))) != (regex_t
*)0)
86 && !regcomp(preg
->pRegExp
,rx
,
87 (REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
) ))
89 preg
->refCount
= (unsigned long *)malloc(sizeof(unsigned long));
90 *(preg
->refCount
) = 1;
97 preg
= (RegExp_Arg
*)0;
100 return((void *)preg
);
101 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
102 char *rx
= va_arg(*ap
,char *);
105 pArg
= (RegExp_Arg
*)malloc(sizeof(RegExp_Arg
));
109 int blen
= RX_INCREMENT
;
110 pArg
->compiled_expression
= NULL
;
111 pArg
->refCount
= (unsigned long *)malloc(sizeof(unsigned long));
112 *(pArg
->refCount
) = 1;
115 char *buf
= (char *)malloc(blen
);
118 #if HAVE_REGEXP_H_FUNCS
119 char *last_pos
= compile (rx
, buf
, &buf
[blen
], '\0');
120 #else /* HAVE_REGEXPR_H_FUNCS */
121 char *last_pos
= compile (rx
, buf
, &buf
[blen
]);
127 blen
+= RX_INCREMENT
;
137 pArg
->compiled_expression
= buf
;
141 } while( blen
<= MAX_RX_LEN
);
143 if (pArg
&& !pArg
->compiled_expression
)
154 /*---------------------------------------------------------------------------
155 | Facility : libnform
156 | Function : static void *Copy_RegularExpression_Type(
159 | Description : Copy structure for regex type argument.
161 | Return Values : Pointer to argument structure or NULL on error.
162 +--------------------------------------------------------------------------*/
163 static void *Copy_RegularExpression_Type(const void * argp
)
165 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
166 const RegExp_Arg
*ap
= (const RegExp_Arg
*)argp
;
167 const RegExp_Arg
*result
= (const RegExp_Arg
*)0;
171 *(ap
->refCount
) += 1;
174 return (void *)result
;
180 /*---------------------------------------------------------------------------
181 | Facility : libnform
182 | Function : static void Free_RegularExpression_Type(void * argp)
184 | Description : Free structure for regex type argument.
187 +--------------------------------------------------------------------------*/
188 static void Free_RegularExpression_Type(void * argp
)
190 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
191 RegExp_Arg
*ap
= (RegExp_Arg
*)argp
;
194 if (--(*(ap
->refCount
)) == 0)
196 #if HAVE_REGEX_H_FUNCS
200 regfree(ap
->pRegExp
);
202 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
203 if (ap
->compiled_expression
)
206 free(ap
->compiled_expression
);
215 /*---------------------------------------------------------------------------
216 | Facility : libnform
217 | Function : static bool Check_RegularExpression_Field(
221 | Description : Validate buffer content to be a valid regular expression
223 | Return Values : TRUE - field is valid
224 | FALSE - field is invalid
225 +--------------------------------------------------------------------------*/
226 static bool Check_RegularExpression_Field(FIELD
* field
, const void * argp
)
229 #if HAVE_REGEX_H_FUNCS
230 const RegExp_Arg
*ap
= (const RegExp_Arg
*)argp
;
231 if (ap
&& ap
->pRegExp
)
232 match
= (regexec(ap
->pRegExp
,field_buffer(field
,0),0,NULL
,0) ? FALSE
:TRUE
);
233 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
234 RegExp_Arg
*ap
= (RegExp_Arg
*)argp
;
235 if (ap
&& ap
->compiled_expression
)
236 match
= (step(field_buffer(field
,0),ap
->compiled_expression
) ? TRUE
:FALSE
);
241 static FIELDTYPE typeREGEXP
= {
242 _HAS_ARGS
| _RESIDENT
,
243 1, /* this is mutable, so we can't be const */
246 Make_RegularExpression_Type
,
247 Copy_RegularExpression_Type
,
248 Free_RegularExpression_Type
,
249 Check_RegularExpression_Field
,
255 NCURSES_EXPORT_VAR(FIELDTYPE
*) TYPE_REGEXP
= &typeREGEXP
;
257 /* fty_regex.c ends here */