2 * Automated Testing Framework (atf)
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
42 #if !defined(REG_BASIC)
44 #endif /* !defined(REG_BASIC) */
46 /* ---------------------------------------------------------------------
47 * The "pattern" error type.
48 * --------------------------------------------------------------------- */
50 struct pattern_error_data
{
54 typedef struct pattern_error_data pattern_error_data_t
;
58 pattern_format(const atf_error_t err
, char *buf
, size_t buflen
)
60 const pattern_error_data_t
*data
;
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
);
72 pattern_error(int errcode
, const regex_t
* preg
)
75 pattern_error_data_t data
;
77 data
.m_errcode
= errcode
;
80 err
= atf_error_new("pattern", &data
, sizeof(data
), pattern_format
);
85 /* ---------------------------------------------------------------------
86 * Auxiliary functions.
87 * --------------------------------------------------------------------- */
91 glob_to_regex(const char *glob
, atf_dynstr_t
*regex
)
95 err
= atf_dynstr_init_fmt(regex
, "^");
97 for (; !atf_is_error(err
) && *glob
!= '\0'; glob
++) {
99 err
= atf_dynstr_append_fmt(regex
, ".*");
100 else if (*glob
== '?')
101 err
= atf_dynstr_append_fmt(regex
, ".");
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
);
115 /* ---------------------------------------------------------------------
117 * --------------------------------------------------------------------- */
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
);
127 atf_expand_matches_glob(const char *glob
, const char *candidate
,
135 /* Special case: regcomp does not like empty patterns. */
136 if (glob
[0] == '\0') {
137 *result
= candidate
[0] == '\0';
138 err
= atf_no_error();
142 /* Convert the glob pattern into a regular expression. */
143 err
= glob_to_regex(glob
, ®ex
);
144 if (atf_is_error(err
))
147 /* Compile the regular expression. */
148 res
= regcomp(&preg
, atf_dynstr_cstring(®ex
), REG_BASIC
);
150 err
= pattern_error(res
, &preg
);
155 res
= regexec(&preg
, candidate
, 0, NULL
, 0);
156 if (res
!= 0 && res
!= REG_NOMATCH
)
157 err
= pattern_error(res
, &preg
);
159 *result
= (res
== 0);
160 INV(!atf_is_error(err
));
165 atf_dynstr_fini(®ex
);