glob.c 6.51 KB
Newer Older
Petr Machata's avatar
Petr Machata committed
1 2
/*
 * This file is part of ltrace.
3
 * Copyright (C) 2007,2008,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata's avatar
Petr Machata committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <sys/types.h>
#include <regex.h>
#include <string.h>
#include <stdlib.h>
25
#include <assert.h>
Petr Machata's avatar
Petr Machata committed
26 27 28 29

static ssize_t
match_character_class(const char *glob, size_t length, size_t from)
{
30 31 32
	assert(length > 0);
	const char *colon = memchr(glob + from + 2, ':', length - 1);
	if (colon == NULL || colon[1] != ']')
Petr Machata's avatar
Petr Machata committed
33
		return -1;
34
	return colon - glob;
Petr Machata's avatar
Petr Machata committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

static ssize_t
match_brack(const char *glob, size_t length, size_t from, int *exclmp)
{
	size_t i = from + 1;

	if (i >= length)
		return -1;

	/* Complement operator.  */
	*exclmp = 0;
	if (glob[i] == '^' || glob[i] == '!') {
		*exclmp = glob[i++] == '!';
		if (i >= length)
			return -1;
	}

	/* On first character, both [ and ] are legal.  But when [ is
	 * followed with :, it's character class.  */
	if (glob[i] == '[' && glob[i + 1] == ':') {
		ssize_t j = match_character_class(glob, length, i);
		if (j < 0)
		fail:
			return -1;
		i = j;
	}
	++i; /* skip any character, including [ or ]  */

	for (; i < length; ++i) {
		char c = glob[i];
66
		if (c == '[' && glob[i + 1] == ':') {
Petr Machata's avatar
Petr Machata committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
			ssize_t j = match_character_class(glob, length, i);
			if (j < 0)
				goto fail;
			i = j;

		} else if (c == ']') {
			return i;
		}
	}
	return -1;
}

static int
append(char **bufp, const char *str, size_t str_size,
       size_t *sizep, size_t *allocp)
{
	if (str_size == 0)
		str_size = strlen(str);
	size_t nsize = *sizep + str_size;
	if (nsize > *allocp) {
		size_t nalloc = nsize * 2;
		char *nbuf = realloc(*bufp, nalloc);
		if (nbuf == NULL)
			return -1;
		*allocp = nalloc;
		*bufp = nbuf;
	}

	memcpy(*bufp + *sizep, str, str_size);
	*sizep = nsize;
	return 0;
}

static int
glob_to_regex(const char *glob, char **retp)
{
	size_t allocd = 0;
	size_t size = 0;
	char *buf = NULL;

	size_t length = strlen(glob);
	int escape = 0;
	size_t i;
	for(i = 0; i < length; ++i) {
		char c = glob[i];
		if (escape) {
			if (c == '\\') {
				if (append(&buf, "\\\\", 0,
					   &size, &allocd) < 0) {
				fail:
					free(buf);
					return REG_ESPACE;
				}

			} else if (c == '*') {
				if (append(&buf, "\\*", 0, &size, &allocd) < 0)
					goto fail;
			} else if (c == '?') {
				if (append(&buf, "?", 0, &size, &allocd) < 0)
					goto fail;
			} else if (append(&buf, (char[]){ '\\', c }, 2,
					  &size, &allocd) < 0)
				goto fail;
			escape = 0;
		} else {
			if (c == '\\')
				escape = 1;
			else if (c == '[') {
				int exclm;
				ssize_t j = match_brack(glob, length, i, &exclm);
137 138
				if (j < 0) {
					free(buf);
Petr Machata's avatar
Petr Machata committed
139
					return REG_EBRACK;
140
				}
Petr Machata's avatar
Petr Machata committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
				if (exclm
				    && append(&buf, "[^", 2,
					      &size, &allocd) < 0)
					goto fail;
				if (append(&buf, glob + i + 2*exclm,
					   j - i + 1 - 2*exclm,
					   &size, &allocd) < 0)
					goto fail;
				i = j;

			} else if (c == '*') {
				if (append(&buf, ".*", 0, &size, &allocd) < 0)
					goto fail;
			} else if (c == '?') {
				if (append(&buf, ".", 0, &size, &allocd) < 0)
					goto fail;
			} else if (c == '.') {
				if (append(&buf, "\\.", 0, &size, &allocd) < 0)
					goto fail;
			} else if (append(&buf, &c, 1, &size, &allocd) < 0)
				goto fail;
		}
	}

	if (escape) {
		free(buf);
		return REG_EESCAPE;
	}

	{
		char c = 0;
		if (append(&buf, &c, 1, &size, &allocd) < 0)
			goto fail;
	}
	*retp = buf;
Petr Machata's avatar
Petr Machata committed
176
	return 0;
Petr Machata's avatar
Petr Machata committed
177 178 179 180 181
}

int
globcomp(regex_t *preg, const char *glob, int cflags)
{
182
	char *regex = NULL;
Petr Machata's avatar
Petr Machata committed
183
	int status = glob_to_regex(glob, &regex);
Petr Machata's avatar
Petr Machata committed
184
	if (status != 0)
Petr Machata's avatar
Petr Machata committed
185
		return status;
186
	assert(regex != NULL);
Petr Machata's avatar
Petr Machata committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	status = regcomp(preg, regex, cflags);
	free(regex);
	return status;
}

#ifdef TEST
#include <stdio.h>

static void
translate(const char *glob, int exp_status, const char *expect)
{
	char *pattern = NULL;
	int status = glob_to_regex(glob, &pattern);
	if (status != exp_status) {
		fprintf(stderr, "translating %s, expected status %d, got %d\n",
			glob, exp_status, status);
		return;
	}

	if (status == 0) {
		assert(pattern != NULL);
		if (strcmp(pattern, expect) != 0)
			fprintf(stderr, "translating %s, expected %s, got %s\n",
				glob, expect, pattern);
		free(pattern);
	} else {
		assert(pattern == NULL);
	}
}

static void
try_match(const char *glob, const char *str, int expect)
{
	regex_t preg;
	int status = globcomp(&preg, glob, 0);
	assert(status == 0);
	status = regexec(&preg, str, 0, NULL, 0);
	assert(status == expect);
	regfree(&preg);
}

int
main(void)
{
        translate("*", 0, ".*");
        translate("?", 0, ".");
        translate(".*", 0, "\\..*");
        translate("*.*", 0, ".*\\..*");
        translate("*a*", 0, ".*a.*");
        translate("[abc]", 0, "[abc]");
        translate("[^abc]", 0, "[^abc]");
        translate("[!abc]", 0, "[^abc]");
        translate("[]]", 0, "[]]");
        translate("[[]", 0, "[[]");
        translate("[^]]", 0, "[^]]");
        translate("[^a-z]", 0, "[^a-z]");
        translate("[abc\\]]", 0, "[abc\\]]");
        translate("[abc\\]def]", 0, "[abc\\]def]");
        translate("[[:space:]]", 0, "[[:space:]]");
        translate("[^[:space:]]", 0, "[^[:space:]]");
        translate("[![:space:]]", 0, "[^[:space:]]");
        translate("[^a-z]*", 0, "[^a-z].*");
        translate("[^a-z]bar*", 0, "[^a-z]bar.*");
	translate("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.", 0,
		  ".*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\."
		  ".*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\.");

        translate("\\", REG_EESCAPE, NULL);
        translate("[^[:naotuh\\", REG_EBRACK, NULL);
        translate("[^[:", REG_EBRACK, NULL);
        translate("[^[", REG_EBRACK, NULL);
        translate("[^", REG_EBRACK, NULL);
        translate("[\\", REG_EBRACK, NULL);
        translate("[", REG_EBRACK, NULL);
261
        translate("abc[", REG_EBRACK, NULL);
Petr Machata's avatar
Petr Machata committed
262 263 264 265 266 267 268 269 270 271 272

	try_match("abc*def", "abc012def", 0);
	try_match("abc*def", "ab012def", REG_NOMATCH);
	try_match("[abc]*def", "a1def", 0);
	try_match("[abc]*def", "b1def", 0);
	try_match("[abc]*def", "d1def", REG_NOMATCH);

	return 0;
}

#endif