/***********************************************************************
* *
* 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> *
* *
***********************************************************************/
#pragma prototyped
/*
* glob(3) test harness
* see help() for details
*/
#if _PACKAGE_ast
#include <ast.h>
#else
static int
{
register char** ao;
register char** an;
register char** ae;
}
}
#endif
#include <stdio.h>
#include <glob.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#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(" testglob - glob(3) test harness\n");
H("\n");
H("SYNOPSIS\n");
H(" testglob [ options ] < testglob.dat\n");
H("\n");
H("DESCRIPTION\n");
H(" testglob reads glob(3) test specifications, one per line, from the\n");
H(" standard input and writes one output line for each failed test. A\n");
H(" summary line is written after all tests are done. Unsupported\n");
H(" features are noted before the first test, and tests requiring these\n");
H(" features are silently ignored. Each test is repeated with GLOB_LIST,\n");
H(" GLOB_STACK, and GLOB_LIST|GLOB_STACK set.\n");
H("\n");
H("OPTIONS\n");
H(" -c catch signals and non-terminating calls\n");
H(" -e ignore error code mismatches\n");
H(" -n do not repeat tests with GLOB_LIST, GLOB_STACK, or GLOB_LIST|GLOB_STACK\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 glob(3) flags to apply, one character per GLOB_feature\n");
H(" flag. The test is skipped if GLOB_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.\n");
H("\n");
H(" K GLOB_AUGMENTED augmented (ksh) patterns\n");
H(" S 0 basic shell patterns\n");
H("\n");
H(" a GLOB_APPEND append to previous result\n");
H(" b GLOB_BRACE enable {...} expansion\n");
H(" c GLOB_COMPLETE shell file completion\n");
H(" e GLOB_NOESCAPE \\ not special\n");
H(" E GLOB_ERR abort on error\n");
H(" i GLOB_ICASE ignore case\n");
H(" m GLOB_MARK append / to directories\n");
H(" n GLOB_NOCHECK no match returns original pattern\n");
H(" r GLOB_STARSTAR enable /**/ expansion\n");
H(" s GLOB_NOSORT don't sort\n");
H(" u standard unspecified behavior -- errors not counted\n");
H("\n");
H(" Field 1 control lines:\n");
H("\n");
H(" C set LC_COLLATE and LC_CTYPE to the locale in field 2\n");
H(" I set gl_fignore to the pattern 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(" Field 2: a shell filename expansion pattern\n");
H("\n");
H(" Field 3: the expected glob() return value, OK for success, glob\n");
H(" return codes otherwise, with the GLOB_ prefix omitted\n");
H("\n");
H(" Field 4: optional space-separated matched file list\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");
}
#ifndef elementsof
#define elementsof(x) (sizeof(x)/sizeof(x[0]))
#endif
#ifndef streq
#endif
#define NOTEST (~0)
static const char* unsupported[] = {
#ifndef GLOB_AUGMENTED
"AUGMENTED",
#endif
#ifndef GLOB_BRACE
"BRACE",
#endif
#ifndef GLOB_COMPLETE
"COMPLETE",
#endif
#ifndef GLOB_DISC
"DISC",
#endif
#ifndef GLOB_ICASE
"ICASE",
#endif
#ifndef GLOB_LIST
"LIST",
#endif
#ifndef GLOB_STACK
"STACK",
#endif
#ifndef GLOB_STARSTAR
"STARSTAR",
#endif
0
};
#ifndef GLOB_BRACE
#endif
#ifndef GLOB_COMPLETE
#endif
#ifndef GLOB_DISC
#define GLOB_DISC 0
#endif
#ifndef GLOB_ICASE
#endif
#ifndef GLOB_LIST
#define GLOB_LIST 0
#endif
#ifndef GLOB_STACK
#define GLOB_STACK 0
#endif
#ifndef GLOB_STARSTAR
#define GLOB_STARSTAR 0
#endif
#ifndef GLOB_ABORTED
#endif
#ifndef GLOB_NOMATCH
#endif
#ifndef GLOB_NOSPACE
#endif
#ifndef GLOB_INTR
#endif
#ifndef GLOB_APPERR
#endif
#ifndef GLOB_NOSYS
#endif
#ifndef GLOB_ELOOP
#endif
#ifndef GLOB_EBUS
#endif
#ifndef GLOB_EFAULT
#endif
0, "OK", "ok",
};
static struct {
int errors;
struct {
int count;
int error;
int position;
} ignore;
int lineno;
int ret;
int signals;
int unspecified;
int warnings;
char* stack;
char* which;
} state;
static void
quote(char* s)
{
unsigned char* u = (unsigned char*)s;
int c;
if (!u)
printf("NIL");
else if (!*u)
printf("NULL");
else {
printf("\"");
for (;;) {
switch (c = *u++) {
case 0:
break;;
case '\\':
printf("\\\\");
continue;
case '"':
printf("\\\"");
continue;
case '\n':
printf("\\n");
continue;
case '\r':
printf("\\r");
continue;
case '\t':
printf("\\t");
continue;
default:
putchar(c);
else
printf("\\x%02x", c);
continue;
}
break;
}
printf("\"");
}
}
static void
{
#if GLOB_LIST
printf("LIST: ");
#endif
#if GLOB_STACK
if (flags & GLOB_STACK)
printf("STACK: ");
#endif
if (unspecified) {
state.unspecified++;
printf(" unspecified behavior");
}
else
if (pat)
}
static int
{
if (!skip) {
printf("NOTE\t");
if (msg)
}
}
static void
{
printf("bad test case ");
exit(1);
}
static int
hex(int c)
{
}
static void
escape(char* s)
{
char* t;
for (t = s; *t = *s; s++, t++) {
if (*s != '\\')
continue;
switch (*++s) {
case 0:
*++t = 0;
break;
case '\\':
*t = '\\';
break;
case 'n':
*t = '\n';
break;
case 'r':
*t = '\r';
break;
case 't':
*t = '\t';
break;
case 'x':
*t = hex(*++s) << 4;
*t |= hex(*++s);
break;
default:
s--;
break;
}
}
}
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);
switch (sig) {
case SIGALRM:
break;
case SIGBUS:
break;
case SIGSEGV:
break;
}
}
static int
qstrcmp(const void* a, const void* b)
{
return strcmp(*(char**)a, *(char**)b);
}
static char*
getline(void)
{
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 flags;
int query;
int unspecified;
int kre;
int sre;
int okre;
int osre;
int ret;
int i;
int m;
int n;
int expected;
int got;
int ok;
int nmodes;
int extra;
char* spec;
char* pat;
char* p;
char* bp;
char* k;
char* s;
char* bs;
char* ans;
char* err;
char* msg;
char** v;
char* buf;
char* path;
char* pathmax;
#if GLOB_DISC
#endif
#if GLOB_LIST
#endif
int catch = 0;
int cwd = 0;
int locale = 0;
int skip = 0;
int testno = 0;
int verbose = 0;
int working = 0;
static int modes[] = {
0,
#if GLOB_LIST
#endif
#if GLOB_STACK
#endif
#if GLOB_LIST && GLOB_STACK
#endif
};
p = unit;
p++;
*p = 0;
while ((p = *++argv) && *p == '-')
for (;;) {
switch (*++p) {
case 0:
break;
case 'c':
catch = 1;
printf(", catch");
continue;
case 'e':
printf(", ignore error code mismatches");
continue;
case 'h':
case '?':
case '-':
printf(", help\n\n");
help();
return 2;
case 'n':
nmodes = 1;
continue;
case 'v':
verbose = 1;
printf(", verbose");
continue;
default:
printf(", invalid option %c", *p);
continue;
}
break;
}
if (p)
printf(", argument(s) ignored");
printf("\n");
printf("NOTE\tunsupported:");
got = ' ';
got = ',';
}
printf("\n");
}
if (catch) {
}
path[0] = 0;
ok = 0;
extra = 0;
/* parse: */
if (*p == 0 || *p == '#')
continue;
if (*p == ':') {
while (*++p == ' ');
printf("NOTE %s\n", p);
continue;
}
if (*p == 'W') {
while (*++p == '\t');
if (*p) {
i = p - buf;
if (working) {
working = 0;
cwd = 1;
if (chdir("../.."))
else if (verbose)
}
if (i > cwd) {
if (verbose) {
printf("\n");
}
}
}
else {
if (k) {
if (verbose) {
printf(" ");
quote(k);
printf("\n");
}
k = 0;
}
if (verbose) {
printf("\n");
}
}
}
cwd = i - 1;
}
*(s - 1) = '/';
k = 0;
while (s < pathmax && *p) {
if (*p == '\t') {
k = linkbuf;
k++;
*k = 0;
k = linkbuf;
break;
}
*s++ = *p++;
}
*s++ = 0;
}
continue;
}
if (work[2]) {
if (k) {
if (verbose) {
printf(" ");
quote(k);
printf("\n");
}
}
else {
if (verbose) {
printf("\n");
}
}
if (!working)
working = 1;
else if (chdir("../.."))
else if (verbose)
else if (verbose) {
printf("\n");
}
work[2] = 0;
}
#if GLOB_DISC
fignore[0] = 0;
#endif
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: */
flags = 0;
for (p = spec; *p; p++) {
switch (*p) {
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 'I':
#if GLOB_DISC
if (field[1])
else
fignore[0] = 0;
#endif
continue;
case 'K':
kre = 1;
continue;
case 'S':
sre = 1;
continue;
case 'a':
flags |= GLOB_APPEND;
continue;
case 'b':
flags |= GLOB_BRACE;
continue;
case 'c':
flags |= GLOB_COMPLETE;
continue;
case 'e':
flags |= GLOB_NOESCAPE;
continue;
case 'i':
flags |= GLOB_ICASE;
continue;
case 'm':
continue;
case 'n':
flags |= GLOB_NOCHECK;
continue;
case 'r':
flags |= GLOB_STARSTAR;
continue;
case 's':
flags |= GLOB_NOSORT;
continue;
case 'u':
unspecified = 1;
continue;
case 'x':
#if GLOB_VERSION >= 20060717L
extra = 15;
#endif
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 < 3)
continue;
while (i < elementsof(field))
field[i++] = 0;
for (m = 0; m < nmodes; m++) {
if (modes[m]) {
continue;
}
/* execute: */
if (sre) {
sre = 0;
#ifdef GLOB_AUGMENTED
flags &= ~GLOB_AUGMENTED;
#endif
}
#ifdef GLOB_AUGMENTED
else if (kre) {
kre = 0;
flags |= GLOB_AUGMENTED;
}
#endif
else
continue;
printf("\n");
}
if (ok) {
ok = 0;
}
}
#if GLOB_DISC
if (fignore[0]) {
}
else
gl.gl_fignore = 0;
#if GLOB_VERSION >= 20060717L
#endif
#endif
if (!query)
testno++;
if (catch) {
else {
alarm(0);
}
}
else
if (ret == 0) {
ok = 1;
ret = GLOB_NOMATCH;
}
expected = i;
got = i;
}
if (query)
else {
if (!ret)
printf("\n");
}
}
else if (ret != GLOB_NOMATCH) {
#if GLOB_LIST
n = 0;
break;
}
av[n] = 0;
v = av;
}
else
#endif
{
}
if (verbose) {
printf(" ");
for (i = 0; i < n; i++)
printf(" %s", v[i]);
printf("\n");
}
v[n = quniq(v, n)] = 0;
}
if (!ans)
ans = "";
bp = p = "";
for (i = 0; i < n; i++) {
bp = p = v[i];
bs = s;
while (*p == *s && *s && *s != ' ') {
p++;
s++;
}
if (*p || *s && *s != ' ')
break;
while (*s == ' ')
s++;
}
if (*p || *s) {
if (!*p && i >= n)
bp = 0;
if (!*s)
bs = s = 0;
else {
while (*s && *s != ' ')
s++;
if (*s == ' ')
*s = 0;
else
s = 0;
}
printf(" expected, ");
printf(" returned\n");
if (s)
*s = 0;
}
}
if (flags & GLOB_APPEND)
break;
}
}
if (state.unspecified)
return 0;
}