1 /* $NetBSD: type_regex.c,v 1.4 2002/06/19 14:12:57 blymn Exp $ */ 2 3 /*- 4 * Copyright (c) 1998-1999 Brett Lymn 5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au) 6 * All rights reserved. 7 * 8 * This code has been donated to The NetBSD Foundation by the Author. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * 30 */ 31 32 #include <stdlib.h> 33 #include <sys/types.h> 34 #include <regex.h> 35 #include "form.h" 36 #include "internals.h" 37 38 /* 39 * The regex type handling. 40 */ 41 42 typedef struct 43 { 44 regex_t compiled; 45 unsigned references; 46 } regex_args; 47 48 /* 49 * Create the regex arguments structure from the given args. Return NULL 50 * if the call fails, otherwise return a pointer to the structure allocated. 51 */ 52 static char * 53 create_regex_args(va_list *args) 54 { 55 regex_args *new; 56 char *expression; 57 58 new = (regex_args *) malloc(sizeof(regex_args)); 59 60 if (new != NULL) { 61 new->references = 1; 62 expression = va_arg(*args, char *); 63 if ((regcomp(&new->compiled, expression, 64 (REG_EXTENDED | REG_NOSUB | REG_NEWLINE))) != 0) { 65 free(new); 66 return NULL; 67 } 68 } 69 70 return (void *) new; 71 } 72 73 /* 74 * Copy the the regex argument structure. 75 */ 76 static char * 77 copy_regex_args(char *args) 78 { 79 ((regex_args *) (void *) args)->references++; 80 81 return (void *) args; 82 } 83 84 /* 85 * Free the allocated storage associated with the type arguments. 86 */ 87 static void 88 free_regex_args(char *args) 89 { 90 if (args != NULL) { 91 ((regex_args *) (void *) args)->references--; 92 if (((regex_args *) (void *) args)->references == 0) 93 free(args); 94 } 95 } 96 97 /* 98 * Check the contents of the field buffer match the regex. 99 */ 100 static int 101 regex_check_field(FIELD *field, char *args) 102 { 103 if ((args != NULL) && 104 (regexec(&((regex_args *) (void *) field->args)->compiled, 105 args, 0, NULL, 0) == 0)) 106 return TRUE; 107 108 return FALSE; 109 } 110 111 static FIELDTYPE builtin_regex = { 112 _TYPE_HAS_ARGS | _TYPE_IS_BUILTIN, /* flags */ 113 0, /* refcount */ 114 NULL, /* link */ 115 create_regex_args, /* make_args */ 116 copy_regex_args, /* copy_args */ 117 free_regex_args, /* free_args */ 118 regex_check_field, /* field_check */ 119 NULL, /* char_check */ 120 NULL, /* next_choice */ 121 NULL /* prev_choice */ 122 }; 123 124 FIELDTYPE *TYPE_REGEXP = &builtin_regex; 125 126 127