xref: /netbsd/lib/libform/type_regex.c (revision 13876d4e)
1 /*	$NetBSD: type_regex.c,v 1.8 2021/04/13 13:13:04 christos 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 <sys/cdefs.h>
33 __RCSID("$NetBSD: type_regex.c,v 1.8 2021/04/13 13:13:04 christos Exp $");
34 
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <regex.h>
38 #include "form.h"
39 #include "internals.h"
40 
41 /*
42  * The regex type handling.
43  */
44 
45 typedef struct
46 {
47 	regex_t compiled;
48 	unsigned references;
49 } regex_args;
50 
51 /*
52  * Create the regex arguments structure from the given args.  Return NULL
53  * if the call fails, otherwise return a pointer to the structure allocated.
54  */
55 static char *
create_regex_args(va_list * args)56 create_regex_args(va_list *args)
57 {
58 	regex_args *new;
59 	char *expression;
60 
61 	new = malloc(sizeof(*new));
62 
63 	if (new != NULL) {
64 		new->references = 1;
65 		expression = va_arg(*args, char *);
66 		if ((regcomp(&new->compiled, expression,
67 			     (REG_EXTENDED | REG_NOSUB | REG_NEWLINE))) != 0) {
68 			free(new);
69 			return NULL;
70 		}
71 	}
72 
73 	return (void *) new;
74 }
75 
76 /*
77  * Copy the regex argument structure.
78  */
79 static char *
copy_regex_args(char * args)80 copy_regex_args(char *args)
81 {
82 	((regex_args *) (void *) args)->references++;
83 
84 	return (void *) args;
85 }
86 
87 /*
88  * Free the allocated storage associated with the type arguments.
89  */
90 static void
free_regex_args(char * args)91 free_regex_args(char *args)
92 {
93 	if (args != NULL) {
94 		((regex_args *) (void *) args)->references--;
95 		if (((regex_args *) (void *) args)->references == 0)
96 			free(args);
97 	}
98 }
99 
100 /*
101  * Check the contents of the field buffer match the regex.
102  */
103 static int
regex_check_field(FIELD * field,char * args)104 regex_check_field(FIELD *field, char *args)
105 {
106 	if ((args != NULL) &&
107 	    (regexec(&((regex_args *) (void *) field->args)->compiled,
108 		   args, (size_t) 0, NULL, 0) == 0))
109 		return TRUE;
110 
111 	return FALSE;
112 }
113 
114 static FIELDTYPE builtin_regex = {
115 	_TYPE_HAS_ARGS | _TYPE_IS_BUILTIN,  /* flags */
116 	0,                                  /* refcount */
117 	NULL,                               /* link */
118 	create_regex_args,                  /* make_args */
119 	copy_regex_args,                    /* copy_args */
120 	free_regex_args,                    /* free_args */
121 	regex_check_field,                  /* field_check */
122 	NULL,                               /* char_check */
123 	NULL,                               /* next_choice */
124 	NULL                                /* prev_choice */
125 };
126 
127 FIELDTYPE *TYPE_REGEXP = &builtin_regex;
128 
129 
130