1 /* $NetBSD: type_regex.c,v 1.7 2004/11/24 11:57:09 blymn Exp $ */
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
8 * This code has been donated to The NetBSD Foundation by the Author.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: type_regex.c,v 1.7 2004/11/24 11:57:09 blymn Exp $");
36 #include <sys/types.h>
39 #include "internals.h"
42 * The regex type handling.
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.
56 create_regex_args(va_list *args
)
61 new = (regex_args
*) malloc(sizeof(regex_args
));
65 expression
= va_arg(*args
, char *);
66 if ((regcomp(&new->compiled
, expression
,
67 (REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
))) != 0) {
77 * Copy the regex argument structure.
80 copy_regex_args(char *args
)
82 ((regex_args
*) (void *) args
)->references
++;
88 * Free the allocated storage associated with the type arguments.
91 free_regex_args(char *args
)
94 ((regex_args
*) (void *) args
)->references
--;
95 if (((regex_args
*) (void *) args
)->references
== 0)
101 * Check the contents of the field buffer match the regex.
104 regex_check_field(FIELD
*field
, char *args
)
106 if ((args
!= NULL
) &&
107 (regexec(&((regex_args
*) (void *) field
->args
)->compiled
,
108 args
, (size_t) 0, NULL
, 0) == 0))
114 static FIELDTYPE builtin_regex
= {
115 _TYPE_HAS_ARGS
| _TYPE_IS_BUILTIN
, /* flags */
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 */
127 FIELDTYPE
*TYPE_REGEXP
= &builtin_regex
;