Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / expand.c
blob2a2fbd35e47b572dcb1ecbdb32447c001c42157d
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <regex.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include "atf-c/dynstr.h"
35 #include "atf-c/error.h"
36 #include "atf-c/expand.h"
37 #include "atf-c/sanity.h"
39 /* REG_BASIC is just a synonym for 0, provided as a counterpart to
40 * REG_EXTENDED to improve readability. It is not provided by all
41 * systems. */
42 #if !defined(REG_BASIC)
43 #define REG_BASIC 0
44 #endif /* !defined(REG_BASIC) */
46 /* ---------------------------------------------------------------------
47 * The "pattern" error type.
48 * --------------------------------------------------------------------- */
50 struct pattern_error_data {
51 int m_errcode;
52 regex_t m_preg;
54 typedef struct pattern_error_data pattern_error_data_t;
56 static
57 void
58 pattern_format(const atf_error_t err, char *buf, size_t buflen)
60 const pattern_error_data_t *data;
61 char tmp[4096];
63 PRE(atf_error_is(err, "pattern"));
65 data = atf_error_data(err);
66 regerror(data->m_errcode, &data->m_preg, tmp, sizeof(tmp));
67 snprintf(buf, buflen, "Regular expression error: %s", tmp);
70 static
71 atf_error_t
72 pattern_error(int errcode, const regex_t* preg)
74 atf_error_t err;
75 pattern_error_data_t data;
77 data.m_errcode = errcode;
78 data.m_preg = *preg;
80 err = atf_error_new("pattern", &data, sizeof(data), pattern_format);
82 return err;
85 /* ---------------------------------------------------------------------
86 * Auxiliary functions.
87 * --------------------------------------------------------------------- */
89 static
90 atf_error_t
91 glob_to_regex(const char *glob, atf_dynstr_t *regex)
93 atf_error_t err;
95 err = atf_dynstr_init_fmt(regex, "^");
97 for (; !atf_is_error(err) && *glob != '\0'; glob++) {
98 if (*glob == '*')
99 err = atf_dynstr_append_fmt(regex, ".*");
100 else if (*glob == '?')
101 err = atf_dynstr_append_fmt(regex, ".");
102 else
103 err = atf_dynstr_append_fmt(regex, "%c", *glob);
106 if (!atf_is_error(err))
107 err = atf_dynstr_append_fmt(regex, "$");
109 if (atf_is_error(err))
110 atf_dynstr_fini(regex);
112 return err;
115 /* ---------------------------------------------------------------------
116 * Free functions.
117 * --------------------------------------------------------------------- */
119 bool
120 atf_expand_is_glob(const char *glob)
122 /* NOTE: Keep this in sync with glob_to_regex! */
123 return (strchr(glob, '*') != NULL) || (strchr(glob, '?') != NULL);
126 atf_error_t
127 atf_expand_matches_glob(const char *glob, const char *candidate,
128 bool *result)
130 atf_dynstr_t regex;
131 atf_error_t err;
132 int res;
133 regex_t preg;
135 /* Special case: regcomp does not like empty patterns. */
136 if (glob[0] == '\0') {
137 *result = candidate[0] == '\0';
138 err = atf_no_error();
139 goto out;
142 /* Convert the glob pattern into a regular expression. */
143 err = glob_to_regex(glob, &regex);
144 if (atf_is_error(err))
145 goto out;
147 /* Compile the regular expression. */
148 res = regcomp(&preg, atf_dynstr_cstring(&regex), REG_BASIC);
149 if (res != 0) {
150 err = pattern_error(res, &preg);
151 goto out_regex;
154 /* Check match. */
155 res = regexec(&preg, candidate, 0, NULL, 0);
156 if (res != 0 && res != REG_NOMATCH)
157 err = pattern_error(res, &preg);
158 else {
159 *result = (res == 0);
160 INV(!atf_is_error(err));
163 regfree(&preg);
164 out_regex:
165 atf_dynstr_fini(&regex);
166 out:
167 return err;