1 /**************************************************************************** 2 * Copyright (c) 1998-2004,2006 Free Software Foundation, Inc. * 3 * * 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: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 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. * 22 * * 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 * 26 * authorization. * 27 ****************************************************************************/ 28 29 /*************************************************************************** 30 * * 31 * Author : Juergen Pfeifer * 32 * * 33 ***************************************************************************/ 34 35 #include "form.priv.h" 36 37 MODULE_ID("$Id: fty_regex.c,v 1.19 2006/04/22 21:33:05 tom Exp $") 38 39 #if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */ 40 #include <regex.h> 41 42 typedef struct 43 { 44 regex_t *pRegExp; 45 unsigned long *refCount; 46 } 47 RegExp_Arg; 48 49 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS 50 #undef RETURN 51 static int reg_errno; 52 53 static char * 54 RegEx_Init(char *instring) 55 { 56 reg_errno = 0; 57 return instring; 58 } 59 60 static char * 61 RegEx_Error(int code) 62 { 63 reg_errno = code; 64 return 0; 65 } 66 67 #define INIT register char *sp = RegEx_Init(instring); 68 #define GETC() (*sp++) 69 #define PEEKC() (*sp) 70 #define UNGETC(c) (--sp) 71 #define RETURN(c) return(c) 72 #define ERROR(c) return RegEx_Error(c) 73 74 #if HAVE_REGEXP_H_FUNCS 75 #include <regexp.h> 76 #else 77 #include <regexpr.h> 78 #endif 79 80 typedef struct 81 { 82 char *compiled_expression; 83 unsigned long *refCount; 84 } 85 RegExp_Arg; 86 87 /* Maximum Length we allow for a compiled regular expression */ 88 #define MAX_RX_LEN (2048) 89 #define RX_INCREMENT (256) 90 91 #endif 92 93 /*--------------------------------------------------------------------------- 94 | Facility : libnform 95 | Function : static void *Make_RegularExpression_Type(va_list * ap) 96 | 97 | Description : Allocate structure for regex type argument. 98 | 99 | Return Values : Pointer to argument structure or NULL on error 100 +--------------------------------------------------------------------------*/ 101 static void * 102 Make_RegularExpression_Type(va_list *ap) 103 { 104 #if HAVE_REGEX_H_FUNCS 105 char *rx = va_arg(*ap, char *); 106 RegExp_Arg *preg; 107 108 preg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg)); 109 110 if (preg) 111 { 112 if (((preg->pRegExp = (regex_t *) malloc(sizeof(regex_t))) != 0) 113 && !regcomp(preg->pRegExp, rx, 114 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE))) 115 { 116 preg->refCount = (unsigned long *)malloc(sizeof(unsigned long)); 117 118 *(preg->refCount) = 1; 119 } 120 else 121 { 122 if (preg->pRegExp) 123 free(preg->pRegExp); 124 free(preg); 125 preg = (RegExp_Arg *)0; 126 } 127 } 128 return ((void *)preg); 129 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS 130 char *rx = va_arg(*ap, char *); 131 RegExp_Arg *pArg; 132 133 pArg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg)); 134 135 if (pArg) 136 { 137 int blen = RX_INCREMENT; 138 139 pArg->compiled_expression = NULL; 140 pArg->refCount = (unsigned long *)malloc(sizeof(unsigned long)); 141 142 *(pArg->refCount) = 1; 143 144 do 145 { 146 char *buf = (char *)malloc(blen); 147 148 if (buf) 149 { 150 #if HAVE_REGEXP_H_FUNCS 151 char *last_pos = compile(rx, buf, &buf[blen], '\0'); 152 153 #else /* HAVE_REGEXPR_H_FUNCS */ 154 char *last_pos = compile(rx, buf, &buf[blen]); 155 #endif 156 if (reg_errno) 157 { 158 free(buf); 159 if (reg_errno == 50) 160 blen += RX_INCREMENT; 161 else 162 { 163 free(pArg); 164 pArg = NULL; 165 break; 166 } 167 } 168 else 169 { 170 pArg->compiled_expression = buf; 171 break; 172 } 173 } 174 } 175 while (blen <= MAX_RX_LEN); 176 } 177 if (pArg && !pArg->compiled_expression) 178 { 179 free(pArg); 180 pArg = NULL; 181 } 182 return (void *)pArg; 183 #else 184 return 0; 185 #endif 186 } 187 188 /*--------------------------------------------------------------------------- 189 | Facility : libnform 190 | Function : static void *Copy_RegularExpression_Type( 191 | const void * argp) 192 | 193 | Description : Copy structure for regex type argument. 194 | 195 | Return Values : Pointer to argument structure or NULL on error. 196 +--------------------------------------------------------------------------*/ 197 static void * 198 Copy_RegularExpression_Type(const void *argp) 199 { 200 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS) 201 const RegExp_Arg *ap = (const RegExp_Arg *)argp; 202 const RegExp_Arg *result = (const RegExp_Arg *)0; 203 204 if (ap) 205 { 206 *(ap->refCount) += 1; 207 result = ap; 208 } 209 return (void *)result; 210 #else 211 return 0; 212 #endif 213 } 214 215 /*--------------------------------------------------------------------------- 216 | Facility : libnform 217 | Function : static void Free_RegularExpression_Type(void * argp) 218 | 219 | Description : Free structure for regex type argument. 220 | 221 | Return Values : - 222 +--------------------------------------------------------------------------*/ 223 static void 224 Free_RegularExpression_Type(void *argp) 225 { 226 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS 227 RegExp_Arg *ap = (RegExp_Arg *)argp; 228 229 if (ap) 230 { 231 if (--(*(ap->refCount)) == 0) 232 { 233 #if HAVE_REGEX_H_FUNCS 234 if (ap->pRegExp) 235 { 236 free(ap->refCount); 237 regfree(ap->pRegExp); 238 } 239 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS 240 if (ap->compiled_expression) 241 { 242 free(ap->refCount); 243 free(ap->compiled_expression); 244 } 245 #endif 246 free(ap); 247 } 248 } 249 #endif 250 } 251 252 /*--------------------------------------------------------------------------- 253 | Facility : libnform 254 | Function : static bool Check_RegularExpression_Field( 255 | FIELD * field, 256 | const void * argp) 257 | 258 | Description : Validate buffer content to be a valid regular expression 259 | 260 | Return Values : TRUE - field is valid 261 | FALSE - field is invalid 262 +--------------------------------------------------------------------------*/ 263 static bool 264 Check_RegularExpression_Field(FIELD *field, const void *argp) 265 { 266 bool match = FALSE; 267 268 #if HAVE_REGEX_H_FUNCS 269 const RegExp_Arg *ap = (const RegExp_Arg *)argp; 270 271 if (ap && ap->pRegExp) 272 match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0) 273 ? FALSE 274 : TRUE); 275 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS 276 RegExp_Arg *ap = (RegExp_Arg *)argp; 277 278 if (ap && ap->compiled_expression) 279 match = (step(field_buffer(field, 0), ap->compiled_expression) 280 ? TRUE 281 : FALSE); 282 #endif 283 return match; 284 } 285 286 static FIELDTYPE typeREGEXP = 287 { 288 _HAS_ARGS | _RESIDENT, 289 1, /* this is mutable, so we can't be const */ 290 (FIELDTYPE *)0, 291 (FIELDTYPE *)0, 292 Make_RegularExpression_Type, 293 Copy_RegularExpression_Type, 294 Free_RegularExpression_Type, 295 Check_RegularExpression_Field, 296 NULL, 297 NULL, 298 NULL 299 }; 300 301 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP; 302 303 /* fty_regex.c ends here */ 304