/***********************************************************************
* *
* 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
/*
* strmatch(3) test harness
* see testmatch --help for a description of the input format
*/
#if OLD
#else
#endif
#if _PACKAGE_ast
#include <ast.h>
#else
#endif
#include <stdio.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(" testmatch - strgrpmatch(3) test harness\n");
H("\n");
H("SYNOPSIS\n");
H(" testmatch [ options ] testmatch.dat\n");
H("\n");
H("DESCRIPTION\n");
H(" testmatch reads strgrpmatch(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. Each successful\n");
H(" test is run again with no subexpression pointer array. Unsupported\n");
H(" features are noted before the first test, and tests requiring these\n");
H(" features are silently ignored.\n");
H("\n");
#if OLD
H(" This version tests the legacy ad-hoc implementation with roots in the\n");
H(" Bourne sh implementation.\n");
#else
H(" This version tests the AST <regex.h> implementation.\n");
#endif
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 strgrpmatch(3) flags to apply, one character per\n");
H(" STR_feature flag. The test is skipped if STR_feature is not supported\n");
H(" by the implementation. If the first character is not [SK] then the\n");
H(" specification is a global control line. Note that no distinction\n");
H(" is made between SRE and KRE tests. STR_MAXIMAL is set by default.\n");
H(" Specifications containing testre(1T) flags are silently ignored.\n");
H("\n");
H(" S 0 SRE (sh glob)\n");
H(" K 0 KRE (ksh glob)\n");
H("\n");
H(" a STR_LEFT|STR_RIGHT implicit ^...$\n");
H(" i STR_ICASE ignore case\n");
H(" l STR_LEFT implicit ^...\n");
H(" m ~STR_MAXIMAL minimal match (default is STR_MAXIMAL)\n");
H(" r STR_RIGHT implicit ...$\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 strgrpmatch expression pattern; SAME uses the pattern\n");
H(" from the 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, BADPAT, or\n");
H(" the match array, a list of (m,n) entries with m and n being first\n");
H(" and last+1 positions in the field 3 string, or NULL if subexpression\n");
H(" array is specified and success is expected. The match[]\n");
H(" subexpression pointer array is initialized to (-2,-2).\n");
H(" All array elements not equal to (-2,-2) must be specified\n");
H(" in the outcome. Unspecified endpoints are denoted by ?.\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 strgrpmatch)\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)
#endif
static const char* unsupported[] =
{
0,
#if NOT_SUPPORTED
"strmatch",
"strgrpmatch",
#endif
#ifndef STR_ICASE
"ICASE",
#endif
#ifndef STR_LEFT
"LEFT",
#endif
#ifndef STR_MAXIMAL
"MAXIMAL",
#endif
#ifndef STR_RIGHT
"RIGHT",
#endif
};
#ifndef STR_ICASE
#endif
#ifndef STR_LEFT
#endif
#ifndef STR_MAXIMAL
#endif
#ifndef STR_RIGHT
#endif
#endif
static struct
{
int errors;
struct
{
int count;
int error;
int position;
} ignore;
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
report(char* comment, char* fun, char* re, char* s, char* msg, int flags, int unspecified, int expand)
{
if (re)
{
printf(" ");
if (s)
{
printf(" versus ");
}
}
if (unspecified)
{
state.unspecified++;
printf(" unspecified behavior");
}
else
if (fun)
else
{
if (msg)
}
}
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
{
int i;
if (match[nmatch-2] != -2 && (!state.ignore.position || match[nmatch-2] >= 0 && match[nmatch-2] >= 0))
break;
for (i = 0; i < nmatch; i += 2)
{
printf("(");
if (match[i] == -1)
printf("?");
else
printf(",");
printf("?");
else
printf(")");
}
if (ans)
printf("\n");
}
static int
matchcheck(int nmatch, ssize_t* match, char* ans, char* re, char* s, int flags, int query, int unspecified, int expand)
{
char* p;
int i;
ssize_t m;
ssize_t n;
{
if (*p++ != '(')
if (*p == '?')
{
m = -1;
p++;
}
else
m = strtol(p, &p, 10);
if (*p++ != ',')
if (*p == '?')
{
n = -1;
p++;
}
else
n = strtol(p, &p, 10);
if (*p++ != ')')
{
if (!query)
{
}
return 0;
}
}
for (; i < nmatch; i += 2)
{
{
if (!query)
{
{
return 0;
}
}
return 0;
}
}
{
}
return 1;
}
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 query;
int expand;
int unspecified;
int kre;
int sre;
int nmatch;
int rmatch;
int eret;
int i;
int sub;
int subunitlen;
int locale = 0;
int skip = 0;
int testno = 0;
char* p;
char* spec;
char* re;
char* s;
char* ans;
char* msg;
char* fun;
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 'p':
continue;
case 'v':
verbose = 1;
continue;
default:
continue;
}
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;
#if STR_MAXIMAL != NOTEST
#else
eflags = 0;
#endif
nmatch = 20;
for (p = spec; *p; p++)
{
if (isdigit(*p))
{
p--;
continue;
}
switch (*p)
{
case 'A':
continue;
case 'B':
continue;
case 'C':
query = 0;
#if OLD
#else
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
}
#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 'c':
continue;
case 'd':
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':
#if STR_MAXIMAL != NOTEST
eflags &= ~STR_MAXIMAL;
#else
#endif
continue;
case 'n':
continue;
case 'o':
continue;
case 'p':
continue;
case 'r':
continue;
case 's':
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 (skip)
continue;
#if NOT_SUPPORTED == 0
if (sre)
{
sre = 0;
}
else if (kre)
{
kre = 0;
}
else
continue;
sub = 1;
if (skip)
continue;
if (!query)
testno++;
for (i = 0; i < elementsof(match); i++)
match[i] = -2;
if (catch)
{
else
{
if (sub)
{
fun = "strgrpmatch";
if (verbose)
}
else
{
fun = "strmatch";
}
alarm(0);
}
}
else if (sub)
{
fun = "strgrpmatch";
if (verbose)
}
else
{
fun = "strmatch";
}
#if OLD
{
printf("-EOF\n");
goto skip;
}
#endif
if (!sub)
{
if (eret)
{
{
if (query)
else
if (eret == 1)
printf("OK expected, NOMATCH returned");
else
printf("\n");
}
}
{
if (query)
else
{
printf("\n");
}
}
}
else if (eret)
{
{
if (query)
else
{
if (eret == 1)
printf("OK expected, NOMATCH returned");
else
printf("\n");
}
}
}
{
if (query)
else
{
}
}
else if (!*ans)
{
if (match[0] != -2)
{
if (query)
else
{
}
}
}
{
continue;
sub = 0;
goto nosub;
}
else if (query)
goto compile;
#endif
}
#if OLD
skip:
#endif
if (subunit)
if (state.unspecified)
}
return 0;
}