patch(1) problems workaround
[minix3.git] / lib / libform / type_regex.c
blob866f59004bd1dfc95548c3374877e7a581a348e6
1 /* $NetBSD: type_regex.c,v 1.7 2004/11/24 11:57:09 blymn Exp $ */
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6 * All rights reserved.
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
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
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 $");
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <regex.h>
38 #include "form.h"
39 #include "internals.h"
42 * The regex type handling.
45 typedef struct
47 regex_t compiled;
48 unsigned references;
49 } regex_args;
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.
55 static char *
56 create_regex_args(va_list *args)
58 regex_args *new;
59 char *expression;
61 new = (regex_args *) malloc(sizeof(regex_args));
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;
73 return (void *) new;
77 * Copy the regex argument structure.
79 static char *
80 copy_regex_args(char *args)
82 ((regex_args *) (void *) args)->references++;
84 return (void *) args;
88 * Free the allocated storage associated with the type arguments.
90 static void
91 free_regex_args(char *args)
93 if (args != NULL) {
94 ((regex_args *) (void *) args)->references--;
95 if (((regex_args *) (void *) args)->references == 0)
96 free(args);
101 * Check the contents of the field buffer match the regex.
103 static int
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))
109 return TRUE;
111 return FALSE;
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 */
127 FIELDTYPE *TYPE_REGEXP = &builtin_regex;