/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1995-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* *
***********************************************************************/
/*
* fnmatch(3) test harness
* see testfnmatch --help for a description of the input format
*/
#if _PACKAGE_ast
#include <ast.h>
#include <fnmatch.h>
#else
#endif
#include <stdio.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#if !_PACKAGE_ast
#include <fnmatch.h>
#endif
#ifdef __STDC__
#include <stdlib.h>
#include <locale.h>
#endif
#ifndef NiL
#ifdef __STDC__
#define NiL 0
#else
#define NiL (char*)0
#endif
#endif
static void
help(void)
{
H("NAME\n");
H(" testfnmatch - fnmatch(3) test harness\n");
H("\n");
H("SYNOPSIS\n");
H(" testfnmatch [ options ] < testfnmatch.dat\n");
H("\n");
H("DESCRIPTION\n");
H(" testfnmatch reads fnmatch(3) test specifications, one per line, from\n");
H(" the standard input and writes one output line for each failed test. A\n");
H(" summary line is written after all tests are done. Unsupported features\n");
H(" are noted before the first test, and tests requiring these features\n");
H(" are silently ignored.\n");
H("\n");
H("OPTIONS\n");
H(" -c catch signals and non-terminating calls\n");
H(" -h list help\n");
H(" -v list each test line\n");
H("\n");
H("INPUT FORMAT\n");
H(" Input lines may be blank, a comment beginning with #, or a test\n");
H(" specification. A specification is five fields separated by one\n");
H(" or more tabs. NULL denotes the empty string and NIL denotes the\n");
H(" 0 pointer.\n");
H("\n");
H(" Field 1: the regex(3) flags to apply, one character per FNM_feature\n");
H(" flag. The test is skipped if FNM_feature is not supported by the\n");
H(" implementation. If the first character is not [SK] then the\n");
H(" specification is a global control line. One or more of [SK] may be\n");
H(" specified; the test will be repeated for each mode. Specifications\n");
H(" containing testregex(1) flags are silently ignored.\n");
H("\n");
H(" S 0 SRE (basic sh glob)\n");
H(" K FNM_AUGMENTED KRE (ksh glob)\n");
H("\n");
H(" d FNM_PERIOD explicit leading . match\n");
H(" g FNM_LEADING_DIR testfnmatch only -- match until /\n");
H(" i FNM_ICASE ignore case\n");
H(" p FNM_PATHNAME explicit / match\n");
H(" s FNM_NOESCAPE \\ not special\n");
H(" u standard unspecified behavior -- errors not counted\n");
H(" $ expand C \\c escapes in fields 2 and 3\n");
H("\n");
H(" Field 1 control lines:\n");
H("\n");
H(" C set LC_COLLATE and LC_CTYPE to locale in field 2\n");
H("\n");
H(" { silent skip if failed until }\n");
H(" } end of skip\n");
H("\n");
H(" : comment comment copied to output\n");
H("\n");
H(" number use number for nmatch (20 by default)\n");
H("\n");
H(" Field 2: the fnmatch pattern; SAME uses the pattern from the\n");
H(" previous specification.\n");
H("\n");
H(" Field 3: the string to match.\n");
H("\n");
H(" Field 4: the test outcome. This is either OK, NOMATCH, or a testre(1T)\n");
H(" style match list of (m,n) entries that is interpreted as OK.\n");
H("\n");
H(" Field 5: optional comment appended to the report.\n");
H("\n");
H("CONTRIBUTORS\n");
H(" Glenn Fowler <gsf@research.att.com> (ksh strmatch, regex extensions)\n");
H(" David Korn <dgk@research.att.com> (ksh glob matcher)\n");
H(" glibc contributors (fnmatch tests)\n");
}
#ifndef elementsof
#define elementsof(x) (sizeof(x)/sizeof(x[0]))
#endif
#ifndef streq
#endif
#define NOTEST (~0)
static const char* unsupported[] =
{
0,
#ifndef FNM_ICASE
"ICASE",
#endif
#ifndef FNM_NOESCAPE
"NOESCAPE",
#endif
#ifndef FNM_PATHNAME
"PATHNAME",
#endif
#ifndef FNM_PERIOD
"PERIOD",
#endif
};
#ifndef FNM_ICASE
#endif
#ifndef FNM_LEADING_DIR
#endif
#ifndef FNM_NOESCAPE
#endif
#ifndef FNM_PATHNAME
#endif
#ifndef FNM_PERIOD
#endif
static struct
{
int errors;
int lineno;
int ret;
int signals;
int unspecified;
int warnings;
char* file;
char* which;
} state;
static void
{
unsigned char* u = (unsigned char*)s;
int c;
if (!u)
printf("NIL");
else if (!*u)
printf("NULL");
else if (expand)
{
printf("\"");
for (;;)
{
switch (c = *u++)
{
case 0:
break;
case '\\':
printf("\\\\");
continue;
case '"':
printf("\\\"");
continue;
case '\a':
printf("\\a");
continue;
case '\b':
printf("\\b");
continue;
case '\f':
printf("\\f");
continue;
case '\n':
printf("\\n");
continue;
case '\r':
printf("\\r");
continue;
case '\t':
printf("\\t");
continue;
case '\v':
printf("\\v");
continue;
default:
putchar(c);
else
printf("\\x%02x", c);
continue;
}
break;
}
printf("\"");
}
else
printf("%s", s);
}
static void
{
if (re)
{
printf(" ");
if (s)
{
printf(" versus ");
}
}
if (unspecified)
{
state.unspecified++;
printf(" unspecified behavior");
}
else
}
static int
{
if (!skip)
{
printf("NOTE\t");
if (msg)
}
}
static void
{
printf("bad test case ");
exit(1);
}
static void
escape(char* s)
{
char* e;
char* t;
char* q;
int c;
for (t = s; *t = *s; s++, t++)
if (*s == '\\')
switch (*++s)
{
case '\\':
break;
case 'a':
*t = '\a';
break;
case 'b':
*t = '\b';
break;
case 'c':
if (*t = *s)
s++;
*t &= 037;
break;
case 'e':
case 'E':
*t = 033;
break;
case 'f':
*t = '\f';
break;
case 'n':
*t = '\n';
break;
case 'r':
*t = '\r';
break;
case 's':
*t = ' ';
break;
case 't':
*t = '\t';
break;
case 'v':
*t = '\v';
break;
case 'u':
case 'x':
q = *s == 'u' ? (s + 5) : (char*)0;
c = 0;
e = s + 1;
while (!e || !q || s < q)
{
switch (*++s)
{
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
c = (c << 4) + *s - 'a' + 10;
continue;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
c = (c << 4) + *s - 'A' + 10;
continue;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
c = (c << 4) + *s - '0';
continue;
case '{':
case '[':
if (s != e)
{
s--;
break;
}
e = 0;
if (q && *(s + 1) == 'U' && *(s + 2) == '+')
s += 2;
continue;
case '}':
case ']':
if (e)
s--;
break;
default:
s--;
break;
}
break;
}
*t = c;
break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
c = *s - '0';
q = s + 2;
while (s < q)
switch (*++s)
{
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
c = (c << 3) + *s - '0';
break;
default:
q = --s;
break;
}
*t = c;
break;
default:
}
}
static void
sigunblock(int s)
{
#ifdef SIG_SETMASK
int op;
sigemptyset(&mask);
if (s)
{
op = SIG_UNBLOCK;
}
else op = SIG_SETMASK;
#else
#ifdef sigmask
#endif
#endif
}
static void
{
alarm(0);
}
static char*
{
register char* s = buf;
register char* b;
for (;;)
{
return 0;
s += strlen(s) - 1;
if (*s != '\n')
break;
if (s == b || *(s - 1) != '\\')
{
*s = 0;
break;
}
s--;
}
return buf;
}
int
{
int cflags;
int eflags;
int expand;
int query;
int unspecified;
int kre;
int sre;
int eret;
int i;
int j;
int subunitlen;
int locale = 0;
int skip = 0;
int testno = 0;
char* p;
char* spec;
char* re;
char* s;
char* ans;
char* msg;
char* subunit;
char* version;
int catch = 0;
int verbose = 0;
p = unit;
p++;
*p = 0;
while ((p = *++argv) && *p == '-')
for (;;)
{
switch (*++p)
{
case 0:
break;
case 'c':
catch = 1;
continue;
case 'h':
case '?':
case '-':
help();
return 2;
case 'v':
verbose = 1;
continue;
default:
return 1;
}
break;
}
if (catch)
{
}
if (!*argv)
{
{
}
{
return 2;
}
{
subunit = p = 0;
for (;;)
{
switch (*s++)
{
case 0:
break;
case '/':
subunit = s;
continue;
case '.':
p = s - 1;
continue;
default:
continue;
}
break;
}
if (!subunit)
if (p < subunit)
p = s - 1;
subunitlen = p - subunit;
subunit = 0;
else
}
else
subunit = 0;
if (catch)
printf(", catch");
if (verbose)
printf(", verbose");
printf("\n");
level = 1;
{
/* parse: */
if (*p == 0 || *p == '#')
continue;
if (*p == ':')
{
while (*++p == ' ');
printf("NOTE %s\n", p);
continue;
}
i = 0;
field[i++] = p;
for (;;)
{
switch (*p++)
{
case 0:
p--;
goto checkfield;
case '\t':
*(p - 1) = 0;
s = field[i - 1];
if (streq(s, "NIL"))
field[i - 1] = 0;
else if (streq(s, "NULL"))
*s = 0;
while (*p == '\t')
p++;
if (!*p)
break;
if (i >= elementsof(field))
field[i++] = p;
/*FALLTHROUGH*/
default:
continue;
}
break;
}
/* interpret: */
cflags = 0;
eflags = 0;
#if 0
#if FNM_PATHNAME != NOTEST
eflags |= FNM_PATHNAME;
#endif
#if FNM_PERIOD != NOTEST
eflags |= FNM_PERIOD;
#endif
#endif
for (p = spec; *p; p++)
{
if (isdigit(*p))
{
strtol(p, &p, 10);
p--;
continue;
}
switch (*p)
{
case 'A':
continue;
case 'B':
continue;
case 'C':
query = 0;
if (locale)
if (i != 2)
{
#if defined(LC_COLLATE) && defined(LC_CTYPE)
s = field[1];
if (!s || streq(s, "POSIX"))
s = "C";
if (!(ans = setlocale(LC_COLLATE, s)) || streq(ans, "C") || streq(ans, "POSIX") || !(ans = setlocale(LC_CTYPE, s)) || streq(ans, "C") || streq(ans, "POSIX"))
else
{
printf("NOTE \"%s\" locale\n", s);
}
#else
#endif
}
continue;
case 'E':
continue;
case 'K':
kre = 1;
continue;
case 'L':
continue;
case 'S':
sre = 1;
continue;
case 'a':
continue;
case 'b':
continue;
case 'd':
eflags |= FNM_PERIOD;
continue;
case 'e':
continue;
case 'f':
continue;
case 'g':
continue;
case 'h':
continue;
case 'i':
continue;
case 'j':
continue;
case 'k':
continue;
case 'l':
continue;
case 'm':
continue;
case 'n':
continue;
case 'o':
continue;
case 'p':
eflags |= FNM_PATHNAME;
continue;
case 'r':
continue;
case 's':
eflags |= FNM_NOESCAPE;
continue;
case 'u':
unspecified = 1;
continue;
case 'v':
continue;
case 'x':
continue;
case 'y':
continue;
case 'z':
continue;
case '$':
expand = 1;
continue;
case '/':
continue;
case '{':
level <<= 1;
{
}
else
{
query = 1;
}
continue;
case '}':
if (level == 1)
else
{
#if defined(LC_COLLATE) && defined(LC_CTYPE)
{
locale = 0;
{
s = "C";
setlocale(LC_COLLATE, s);
printf("NOTE \"%s\" locale\n", s);
}
}
#endif
level >>= 1;
}
continue;
default:
break;
}
break;
}
continue;
if (i < 4)
while (i < elementsof(field))
field[i++] = 0;
escape(s);
if (*ans == '(')
{
if (*p == ')')
{
s[j] = 0;
s += i;
}
}
if (skip)
continue;
if (sre)
{
sre = 0;
}
#ifdef FNM_AUGMENTED
else if (kre)
{
eflags |= FNM_AUGMENTED;
kre = 0;
}
#endif
else
continue;
if (skip)
continue;
if (!query)
testno++;
if (catch)
{
else
{
alarm(0);
}
}
else
if (eret)
{
if (query)
{
if (eret == FNM_NOMATCH)
printf("OK expected, NOMATCH returned");
else
printf("\n");
}
}
{
if (query)
else
{
printf("\n");
}
}
goto compile;
}
if (subunit)
if (state.unspecified)
}
return 0;
}