2 * This file is part of ltrace.
3 * Copyright (C) 2007,2008,2012,2013 Petr Machata, Red Hat Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include <sys/types.h>
28 match_character_class(const char *glob, size_t length, size_t from)
31 const char *colon = memchr(glob + from + 2, ':', length - 1);
32 if (colon == NULL || colon[1] != ']')
38 match_brack(const char *glob, size_t length, size_t from, int *exclmp)
45 /* Complement operator. */
47 if (glob[i] == '^' || glob[i] == '!') {
48 *exclmp = glob[i++] == '!';
53 /* On first character, both [ and ] are legal. But when [ is
54 * followed with :, it's character class. */
55 if (glob[i] == '[' && glob[i + 1] == ':') {
56 ssize_t j = match_character_class(glob, length, i);
62 ++i; /* skip any character, including [ or ] */
64 for (; i < length; ++i) {
66 if (c == '[' && glob[i + 1] == ':') {
67 ssize_t j = match_character_class(glob, length, i);
72 } else if (c == ']') {
80 append(char **bufp, const char *str, size_t str_size,
81 size_t *sizep, size_t *allocp)
84 str_size = strlen(str);
85 size_t nsize = *sizep + str_size;
86 if (nsize > *allocp) {
87 size_t nalloc = nsize * 2;
88 char *nbuf = realloc(*bufp, nalloc);
95 memcpy(*bufp + *sizep, str, str_size);
101 glob_to_regex(const char *glob, char **retp)
107 size_t length = strlen(glob);
110 for(i = 0; i < length; ++i) {
114 if (append(&buf, "\\\\", 0,
115 &size, &allocd) < 0) {
121 } else if (c == '*') {
122 if (append(&buf, "\\*", 0, &size, &allocd) < 0)
124 } else if (c == '?') {
125 if (append(&buf, "?", 0, &size, &allocd) < 0)
127 } else if (append(&buf, (char[]){ '\\', c }, 2,
136 ssize_t j = match_brack(glob, length, i, &exclm);
142 && append(&buf, "[^", 2,
145 if (append(&buf, glob + i + 2*exclm,
151 } else if (c == '*') {
152 if (append(&buf, ".*", 0, &size, &allocd) < 0)
154 } else if (c == '?') {
155 if (append(&buf, ".", 0, &size, &allocd) < 0)
157 } else if (c == '.') {
158 if (append(&buf, "\\.", 0, &size, &allocd) < 0)
160 } else if (append(&buf, &c, 1, &size, &allocd) < 0)
172 if (append(&buf, &c, 1, &size, &allocd) < 0)
180 globcomp(regex_t *preg, const char *glob, int cflags)
183 int status = glob_to_regex(glob, ®ex);
186 assert(regex != NULL);
187 status = regcomp(preg, regex, cflags);
196 translate(const char *glob, int exp_status, const char *expect)
198 char *pattern = NULL;
199 int status = glob_to_regex(glob, &pattern);
200 if (status != exp_status) {
201 fprintf(stderr, "translating %s, expected status %d, got %d\n",
202 glob, exp_status, status);
207 assert(pattern != NULL);
208 if (strcmp(pattern, expect) != 0)
209 fprintf(stderr, "translating %s, expected %s, got %s\n",
210 glob, expect, pattern);
213 assert(pattern == NULL);
218 try_match(const char *glob, const char *str, int expect)
221 int status = globcomp(&preg, glob, 0);
223 status = regexec(&preg, str, 0, NULL, 0);
224 assert(status == expect);
231 translate("*", 0, ".*");
232 translate("?", 0, ".");
233 translate(".*", 0, "\\..*");
234 translate("*.*", 0, ".*\\..*");
235 translate("*a*", 0, ".*a.*");
236 translate("[abc]", 0, "[abc]");
237 translate("[^abc]", 0, "[^abc]");
238 translate("[!abc]", 0, "[^abc]");
239 translate("[]]", 0, "[]]");
240 translate("[[]", 0, "[[]");
241 translate("[^]]", 0, "[^]]");
242 translate("[^a-z]", 0, "[^a-z]");
243 translate("[abc\\]]", 0, "[abc\\]]");
244 translate("[abc\\]def]", 0, "[abc\\]def]");
245 translate("[[:space:]]", 0, "[[:space:]]");
246 translate("[^[:space:]]", 0, "[^[:space:]]");
247 translate("[![:space:]]", 0, "[^[:space:]]");
248 translate("[^a-z]*", 0, "[^a-z].*");
249 translate("[^a-z]bar*", 0, "[^a-z]bar.*");
250 translate("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.", 0,
251 ".*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\."
252 ".*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\.");
254 translate("\\", REG_EESCAPE, NULL);
255 translate("[^[:naotuh\\", REG_EBRACK, NULL);
256 translate("[^[:", REG_EBRACK, NULL);
257 translate("[^[", REG_EBRACK, NULL);
258 translate("[^", REG_EBRACK, NULL);
259 translate("[\\", REG_EBRACK, NULL);
260 translate("[", REG_EBRACK, NULL);
261 translate("abc[", REG_EBRACK, NULL);
263 try_match("abc*def", "abc012def", 0);
264 try_match("abc*def", "ab012def", REG_NOMATCH);
265 try_match("[abc]*def", "a1def", 0);
266 try_match("[abc]*def", "b1def", 0);
267 try_match("[abc]*def", "d1def", REG_NOMATCH);