Add support for POSIX character classes. PR: 116826 (submitted by Roy Marples ) Patch seems to be taken from dash, so probably a copyright 1997-2005 Herbert Xu is in order (license is ok, the only GPL bit in dash is mksignames.c from bash). I also took a bugfix from dash (!!ccmatch because further code assumes true is represented by 1). diff --git a/expand.c b/expand.c --- a/expand.c +++ b/expand.c @@ -1,6 +1,8 @@ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 1997-2005 + * Herbert Xu . All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. @@ -1349,6 +1351,42 @@ patmatch(char *pattern, char *string, in } +STATIC int ccmatch(char *p, int chr, char **r) +{ + static const struct class { + char name[10]; + int (*fn)(int); + } classes[] = { + { .name = ":alnum:]", .fn = isalnum }, + { .name = ":cntrl:]", .fn = iscntrl }, + { .name = ":lower:]", .fn = islower }, + { .name = ":space:]", .fn = isspace }, + { .name = ":alpha:]", .fn = isalpha }, + { .name = ":digit:]", .fn = isdigit }, + { .name = ":print:]", .fn = isprint }, + { .name = ":upper:]", .fn = isupper }, + { .name = ":blank:]", .fn = isblank }, + { .name = ":graph:]", .fn = isgraph }, + { .name = ":punct:]", .fn = ispunct }, + { .name = ":xdigit:]", .fn = isxdigit }, + }; + const struct class *class, *end; + char *q; + + end = classes + sizeof(classes) / sizeof(classes[0]); + for (class = classes; class < end; class++) { + q = prefix(class->name, p); + if (!q) + continue; + *r = q; + return class->fn(chr); + } + + *r = 0; + return 0; +} + + STATIC int pmatch(char *pattern, char *string, int squoted) { @@ -1434,6 +1472,15 @@ pmatch(char *pattern, char *string, int continue; if (c == CTLESC) c = *p++; + else if (c == '[') { + char *r; + + found |= !!ccmatch(p, chr, &r); + if (r) { + p = r; + continue; + } + } if (*p == '-' && p[1] != ']') { p++; while (*p == CTLQUOTEMARK) diff --git a/mystring.c b/mystring.c --- a/mystring.c +++ b/mystring.c @@ -88,14 +88,14 @@ scopyn(const char *from, char *to, int s * prefix -- see if pfx is a prefix of string. */ -int +char * prefix(const char *pfx, const char *string) { while (*pfx) { if (*pfx++ != *string++) return 0; } - return 1; + return (char *)string; } diff --git a/mystring.h b/mystring.h --- a/mystring.h +++ b/mystring.h @@ -36,7 +36,7 @@ #include void scopyn(const char *, char *, int); -int prefix(const char *, const char *); +char *prefix(const char *, const char *); int number(const char *); int is_number(const char *);