116b8cff5SYuri Pankov #pragma prototyped noticed
216b8cff5SYuri Pankov 
316b8cff5SYuri Pankov /*
416b8cff5SYuri Pankov  * regex(3) test harness
516b8cff5SYuri Pankov  *
616b8cff5SYuri Pankov  * build:	cc -o testregex testregex.c
716b8cff5SYuri Pankov  * help:	testregex --man
816b8cff5SYuri Pankov  * note:	REG_* features are detected by #ifdef; if REG_* are enums
916b8cff5SYuri Pankov  *		then supply #define REG_foo REG_foo for each enum REG_foo
1016b8cff5SYuri Pankov  *
1116b8cff5SYuri Pankov  *	Glenn Fowler <glenn.s.fowler@gmail.com>
1216b8cff5SYuri Pankov  *	AT&T Research
1316b8cff5SYuri Pankov  *
1416b8cff5SYuri Pankov  * PLEASE: publish your tests so everyone can benefit
1516b8cff5SYuri Pankov  *
1616b8cff5SYuri Pankov  * The following license covers testregex.c and all associated test data.
1716b8cff5SYuri Pankov  *
1816b8cff5SYuri Pankov  * Permission is hereby granted, free of charge, to any person obtaining a
1916b8cff5SYuri Pankov  * copy of THIS SOFTWARE FILE (the "Software"), to deal in the Software
2016b8cff5SYuri Pankov  * without restriction, including without limitation the rights to use,
2116b8cff5SYuri Pankov  * copy, modify, merge, publish, distribute, and/or sell copies of the
2216b8cff5SYuri Pankov  * Software, and to permit persons to whom the Software is furnished to do
2316b8cff5SYuri Pankov  * so, subject to the following disclaimer:
2416b8cff5SYuri Pankov  *
3516b8cff5SYuri Pankov  */
3616b8cff5SYuri Pankov 
3716b8cff5SYuri Pankov static const char id[] = "\n@(#)$Id: testregex (AT&T Research) 2010-06-10 $\0\n";
3816b8cff5SYuri Pankov 
3916b8cff5SYuri Pankov #if _PACKAGE_ast
4016b8cff5SYuri Pankov #include <ast.h>
4116b8cff5SYuri Pankov #else
4216b8cff5SYuri Pankov #include <sys/types.h>
4316b8cff5SYuri Pankov #endif
4416b8cff5SYuri Pankov 
4516b8cff5SYuri Pankov #include <stdio.h>
4616b8cff5SYuri Pankov #include <regex.h>
4716b8cff5SYuri Pankov #include <ctype.h>
4816b8cff5SYuri Pankov #include <setjmp.h>
4916b8cff5SYuri Pankov #include <signal.h>
5016b8cff5SYuri Pankov #include <string.h>
5116b8cff5SYuri Pankov #include <unistd.h>
5216b8cff5SYuri Pankov 
5316b8cff5SYuri Pankov #ifdef	__STDC__
5416b8cff5SYuri Pankov #include <stdlib.h>
5516b8cff5SYuri Pankov #include <locale.h>
5616b8cff5SYuri Pankov #endif
5716b8cff5SYuri Pankov 
5816b8cff5SYuri Pankov #ifndef RE_DUP_MAX
5916b8cff5SYuri Pankov #define RE_DUP_MAX	32767
6016b8cff5SYuri Pankov #endif
6116b8cff5SYuri Pankov 
6216b8cff5SYuri Pankov #if !_PACKAGE_ast
6316b8cff5SYuri Pankov #undef	REG_DISCIPLINE
6416b8cff5SYuri Pankov #endif
6516b8cff5SYuri Pankov 
6616b8cff5SYuri Pankov #ifndef REG_DELIMITED
6716b8cff5SYuri Pankov #undef	_REG_subcomp
6816b8cff5SYuri Pankov #endif
6916b8cff5SYuri Pankov 
7016b8cff5SYuri Pankov #define TEST_ARE		0x00000001
7116b8cff5SYuri Pankov #define TEST_BRE		0x00000002
7216b8cff5SYuri Pankov #define TEST_ERE		0x00000004
7316b8cff5SYuri Pankov #define TEST_KRE		0x00000008
7416b8cff5SYuri Pankov #define TEST_LRE		0x00000010
7516b8cff5SYuri Pankov #define TEST_SRE		0x00000020
7616b8cff5SYuri Pankov 
7716b8cff5SYuri Pankov #define TEST_EXPAND		0x00000100
7816b8cff5SYuri Pankov #define TEST_LENIENT		0x00000200
7916b8cff5SYuri Pankov 
8016b8cff5SYuri Pankov #define TEST_QUERY		0x00000400
8116b8cff5SYuri Pankov #define TEST_SUB		0x00000800
8216b8cff5SYuri Pankov #define TEST_UNSPECIFIED	0x00001000
8316b8cff5SYuri Pankov #define TEST_VERIFY		0x00002000
8416b8cff5SYuri Pankov #define TEST_AND		0x00004000
8516b8cff5SYuri Pankov #define TEST_OR			0x00008000
8616b8cff5SYuri Pankov 
8716b8cff5SYuri Pankov #define TEST_DELIMIT		0x00010000
8816b8cff5SYuri Pankov #define TEST_OK			0x00020000
8916b8cff5SYuri Pankov #define TEST_SAME		0x00040000
9016b8cff5SYuri Pankov 
9116b8cff5SYuri Pankov #define TEST_ACTUAL		0x00100000
9216b8cff5SYuri Pankov #define TEST_BASELINE		0x00200000
9316b8cff5SYuri Pankov #define TEST_FAIL		0x00400000
9416b8cff5SYuri Pankov #define TEST_PASS		0x00800000
9516b8cff5SYuri Pankov #define TEST_SUMMARY		0x01000000
9616b8cff5SYuri Pankov 
9716b8cff5SYuri Pankov #define TEST_IGNORE_ERROR	0x02000000
9816b8cff5SYuri Pankov #define TEST_IGNORE_OVER	0x04000000
9916b8cff5SYuri Pankov #define TEST_IGNORE_POSITION	0x08000000
10016b8cff5SYuri Pankov 
10116b8cff5SYuri Pankov #define TEST_CATCH		0x10000000
10216b8cff5SYuri Pankov #define TEST_VERBOSE		0x20000000
10316b8cff5SYuri Pankov 
10416b8cff5SYuri Pankov #define TEST_DECOMP		0x40000000
10516b8cff5SYuri Pankov 
10716b8cff5SYuri Pankov 
10816b8cff5SYuri Pankov #ifdef REG_DISCIPLINE
10916b8cff5SYuri Pankov 
11016b8cff5SYuri Pankov 
11116b8cff5SYuri Pankov #include <stk.h>
11216b8cff5SYuri Pankov 
11316b8cff5SYuri Pankov typedef struct Disc_s
11416b8cff5SYuri Pankov {
11516b8cff5SYuri Pankov 	regdisc_t	disc;
11616b8cff5SYuri Pankov 	int		ordinal;
11716b8cff5SYuri Pankov 	Sfio_t*		sp;
11816b8cff5SYuri Pankov } Disc_t;
11916b8cff5SYuri Pankov 
12016b8cff5SYuri Pankov static void*
compf(const regex_t * re,const char * xstr,size_t xlen,regdisc_t * disc)12116b8cff5SYuri Pankov compf(const regex_t* re, const char* xstr, size_t xlen, regdisc_t* disc)
12216b8cff5SYuri Pankov {
12316b8cff5SYuri Pankov 	Disc_t*		dp = (Disc_t*)disc;
12416b8cff5SYuri Pankov 
12516b8cff5SYuri Pankov 	return (void*)((char*)0 + ++dp->ordinal);
12616b8cff5SYuri Pankov }
12716b8cff5SYuri Pankov 
12816b8cff5SYuri Pankov static int
execf(const regex_t * re,void * data,const char * xstr,size_t xlen,const char * sstr,size_t slen,char ** snxt,regdisc_t * disc)12916b8cff5SYuri Pankov execf(const regex_t* re, void* data, const char* xstr, size_t xlen, const char* sstr, size_t slen, char** snxt, regdisc_t* disc)
13016b8cff5SYuri Pankov {
13116b8cff5SYuri Pankov 	Disc_t*		dp = (Disc_t*)disc;
13216b8cff5SYuri Pankov 
13316b8cff5SYuri Pankov 	sfprintf(dp->sp, "{%-.*s}(%lu:%d)", xlen, xstr, (char*)data - (char*)0, slen);
13416b8cff5SYuri Pankov 	return atoi(xstr);
13516b8cff5SYuri Pankov }
13616b8cff5SYuri Pankov 
13716b8cff5SYuri Pankov static void*
resizef(void * handle,void * data,size_t size)13816b8cff5SYuri Pankov resizef(void* handle, void* data, size_t size)
13916b8cff5SYuri Pankov {
14016b8cff5SYuri Pankov 	if (!size)
14116b8cff5SYuri Pankov 		return 0;
14216b8cff5SYuri Pankov 	return stkalloc((Sfio_t*)handle, size);
14316b8cff5SYuri Pankov }
14416b8cff5SYuri Pankov 
14516b8cff5SYuri Pankov #endif
14616b8cff5SYuri Pankov 
14716b8cff5SYuri Pankov #ifndef NiL
14816b8cff5SYuri Pankov #ifdef	__STDC__
14916b8cff5SYuri Pankov #define NiL		0
15016b8cff5SYuri Pankov #else
15116b8cff5SYuri Pankov #define NiL		(char*)0
15216b8cff5SYuri Pankov #endif
15316b8cff5SYuri Pankov #endif
15416b8cff5SYuri Pankov 
15516b8cff5SYuri Pankov #define H(x)		do{if(html)fprintf(stderr,x);}while(0)
15616b8cff5SYuri Pankov #define T(x)		fprintf(stderr,x)
15716b8cff5SYuri Pankov 
15816b8cff5SYuri Pankov static void
help(int html)15916b8cff5SYuri Pankov help(int html)
16016b8cff5SYuri Pankov {
16116b8cff5SYuri Pankov H("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n");
16216b8cff5SYuri Pankov H("<HTML>\n");
16316b8cff5SYuri Pankov H("<HEAD>\n");
16416b8cff5SYuri Pankov H("<TITLE>testregex man document</TITLE>\n");
16516b8cff5SYuri Pankov H("</HEAD>\n");
16616b8cff5SYuri Pankov H("<BODY bgcolor=white>\n");
16716b8cff5SYuri Pankov H("<PRE>\n");
16816b8cff5SYuri Pankov T("NAME\n");
16916b8cff5SYuri Pankov T("  testregex - regex(3) test harness\n");
17016b8cff5SYuri Pankov T("\n");
17116b8cff5SYuri Pankov T("SYNOPSIS\n");
17216b8cff5SYuri Pankov T("  testregex [ options ]\n");
17316b8cff5SYuri Pankov T("\n");
17416b8cff5SYuri Pankov T("DESCRIPTION\n");
17516b8cff5SYuri Pankov T("  testregex reads regex(3) test specifications, one per line, from the\n");
17616b8cff5SYuri Pankov T("  standard input and writes one output line for each failed test. A\n");
17716b8cff5SYuri Pankov T("  summary line is written after all tests are done. Each successful\n");
17816b8cff5SYuri Pankov T("  test is run again with REG_NOSUB. Unsupported features are noted\n");
17916b8cff5SYuri Pankov T("  before the first test, and tests requiring these features are\n");
18016b8cff5SYuri Pankov T("  silently ignored.\n");
18116b8cff5SYuri Pankov T("\n");
18216b8cff5SYuri Pankov T("OPTIONS\n");
18316b8cff5SYuri Pankov T("  -c	catch signals and non-terminating calls\n");
18416b8cff5SYuri Pankov T("  -e	ignore error return mismatches\n");
18516b8cff5SYuri Pankov T("  -h	list help on standard error\n");
18616b8cff5SYuri Pankov T("  -n	do not repeat successful tests with regnexec()\n");
18716b8cff5SYuri Pankov T("  -o	ignore match[] overrun errors\n");
18816b8cff5SYuri Pankov T("  -p	ignore negative position mismatches\n");
18916b8cff5SYuri Pankov T("  -s	use stack instead of malloc\n");
19016b8cff5SYuri Pankov T("  -x	do not repeat successful tests with REG_NOSUB\n");
19116b8cff5SYuri Pankov T("  -v	list each test line\n");
19216b8cff5SYuri Pankov T("  -A	list failed test lines with actual answers\n");
19316b8cff5SYuri Pankov T("  -B	list all test lines with actual answers\n");
19416b8cff5SYuri Pankov T("  -F	list failed test lines\n");
19516b8cff5SYuri Pankov T("  -P	list passed test lines\n");
19616b8cff5SYuri Pankov T("  -S	output one summary line\n");
19716b8cff5SYuri Pankov T("\n");
19816b8cff5SYuri Pankov T("INPUT FORMAT\n");
19916b8cff5SYuri Pankov T("  Input lines may be blank, a comment beginning with #, or a test\n");
20016b8cff5SYuri Pankov T("  specification. A specification is five fields separated by one\n");
20116b8cff5SYuri Pankov T("  or more tabs. NULL denotes the empty string and NIL denotes the\n");
20216b8cff5SYuri Pankov T("  0 pointer.\n");
20316b8cff5SYuri Pankov T("\n");
20416b8cff5SYuri Pankov T("  Field 1: the regex(3) flags to apply, one character per REG_feature\n");
20516b8cff5SYuri Pankov T("  flag. The test is skipped if REG_feature is not supported by the\n");
20616b8cff5SYuri Pankov T("  implementation. If the first character is not [BEASKLP] then the\n");
20716b8cff5SYuri Pankov T("  specification is a global control line. One or more of [BEASKLP] may be\n");
20816b8cff5SYuri Pankov T("  specified; the test will be repeated for each mode.\n");
20916b8cff5SYuri Pankov T("\n");
21016b8cff5SYuri Pankov T("    B 	basic			BRE	(grep, ed, sed)\n");
21116b8cff5SYuri Pankov T("    E 	REG_EXTENDED		ERE	(egrep)\n");
21216b8cff5SYuri Pankov T("    A	REG_AUGMENTED		ARE	(egrep with negation)\n");
21316b8cff5SYuri Pankov T("    S	REG_SHELL		SRE	(sh glob)\n");
21416b8cff5SYuri Pankov T("    K	REG_SHELL|REG_AUGMENTED	KRE	(ksh glob)\n");
21516b8cff5SYuri Pankov T("    L	REG_LITERAL		LRE	(fgrep)\n");
21616b8cff5SYuri Pankov T("\n");
21716b8cff5SYuri Pankov T("    a	REG_LEFT|REG_RIGHT	implicit ^...$\n");
21816b8cff5SYuri Pankov T("    b	REG_NOTBOL		lhs does not match ^\n");
21916b8cff5SYuri Pankov T("    c	REG_COMMENT		ignore space and #...\\n\n");
22016b8cff5SYuri Pankov T("    d	REG_SHELL_DOT		explicit leading . match\n");
22116b8cff5SYuri Pankov T("    e	REG_NOTEOL		rhs does not match $\n");
22216b8cff5SYuri Pankov T("    f	REG_MULTIPLE		multiple \\n separated patterns\n");
22316b8cff5SYuri Pankov T("    g	FNM_LEADING_DIR		testfnmatch only -- match until /\n");
22416b8cff5SYuri Pankov T("    h	REG_MULTIREF		multiple digit backref\n");
22516b8cff5SYuri Pankov T("    i	REG_ICASE		ignore case\n");
22616b8cff5SYuri Pankov T("    j	REG_SPAN		. matches \\n\n");
22716b8cff5SYuri Pankov T("    k	REG_ESCAPE		\\ to ecape [...] delimiter\n");
22816b8cff5SYuri Pankov T("    l	REG_LEFT		implicit ^...\n");
22916b8cff5SYuri Pankov T("    m	REG_MINIMAL		minimal match\n");
23016b8cff5SYuri Pankov T("    n	REG_NEWLINE		explicit \\n match\n");
23116b8cff5SYuri Pankov T("    o	REG_ENCLOSED		(|&) magic inside [@|&](...)\n");
23216b8cff5SYuri Pankov T("    p	REG_SHELL_PATH		explicit / match\n");
23316b8cff5SYuri Pankov T("    q	REG_DELIMITED		delimited pattern\n");
23416b8cff5SYuri Pankov T("    r	REG_RIGHT		implicit ...$\n");
23516b8cff5SYuri Pankov T("    s	REG_SHELL_ESCAPED	\\ not special\n");
23616b8cff5SYuri Pankov T("    t	REG_MUSTDELIM		all delimiters must be specified\n");
23716b8cff5SYuri Pankov T("    u	standard unspecified behavior -- errors not counted\n");
23816b8cff5SYuri Pankov T("    v	REG_CLASS_ESCAPE	\\ special inside [...]\n");
23916b8cff5SYuri Pankov T("    w	REG_NOSUB		no subexpression match array\n");
24016b8cff5SYuri Pankov T("    x	REG_LENIENT		let some errors slide\n");
24116b8cff5SYuri Pankov T("    y	REG_LEFT		regexec() implicit ^...\n");
24216b8cff5SYuri Pankov T("    z	REG_NULL		NULL subexpressions ok\n");
24316b8cff5SYuri Pankov T("    $	                        expand C \\c escapes in fields 2 and 3\n");
24416b8cff5SYuri Pankov T("    /	                        field 2 is a regsubcomp() expression\n");
24516b8cff5SYuri Pankov T("    =	                        field 3 is a regdecomp() expression\n");
24616b8cff5SYuri Pankov T("\n");
24716b8cff5SYuri Pankov T("  Field 1 control lines:\n");
24816b8cff5SYuri Pankov T("\n");
24916b8cff5SYuri Pankov T("    C		set LC_COLLATE and LC_CTYPE to locale in field 2\n");
25016b8cff5SYuri Pankov T("\n");
25116b8cff5SYuri Pankov T("    ?test ...	output field 5 if passed and != EXPECTED, silent otherwise\n");
25216b8cff5SYuri Pankov T("    &test ...	output field 5 if current and previous passed\n");
25316b8cff5SYuri Pankov T("    |test ...	output field 5 if current passed and previous failed\n");
25416b8cff5SYuri Pankov T("    ; ...	output field 2 if previous failed\n");
25516b8cff5SYuri Pankov T("    {test ...	skip if failed until }\n");
25616b8cff5SYuri Pankov T("    }		end of skip\n");
25716b8cff5SYuri Pankov T("\n");
25816b8cff5SYuri Pankov T("    : comment		comment copied as output NOTE\n");
25916b8cff5SYuri Pankov T("    :comment:test	:comment: ignored\n");
26016b8cff5SYuri Pankov T("    N[OTE] comment	comment copied as output NOTE\n");
26116b8cff5SYuri Pankov T("    T[EST] comment	comment\n");
26216b8cff5SYuri Pankov T("\n");
26316b8cff5SYuri Pankov T("    number		use number for nmatch (20 by default)\n");
26416b8cff5SYuri Pankov T("\n");
26516b8cff5SYuri Pankov T("  Field 2: the regular expression pattern; SAME uses the pattern from\n");
26616b8cff5SYuri Pankov T("    the previous specification. RE_DUP_MAX inside {...} expands to the\n");
26716b8cff5SYuri Pankov T("    value from <limits.h>.\n");
26816b8cff5SYuri Pankov T("\n");
26916b8cff5SYuri Pankov T("  Field 3: the string to match. X...{RE_DUP_MAX} expands to RE_DUP_MAX\n");
27016b8cff5SYuri Pankov T("    copies of X.\n");
27116b8cff5SYuri Pankov T("\n");
27216b8cff5SYuri Pankov T("  Field 4: the test outcome. This is either one of the posix error\n");
27316b8cff5SYuri Pankov T("    codes (with REG_ omitted) or the match array, a list of (m,n)\n");
27416b8cff5SYuri Pankov T("    entries with m and n being first and last+1 positions in the\n");
27516b8cff5SYuri Pankov T("    field 3 string, or NULL if REG_NOSUB is in effect and success\n");
27616b8cff5SYuri Pankov T("    is expected. BADPAT is acceptable in place of any regcomp(3)\n");
27716b8cff5SYuri Pankov T("    error code. The match[] array is initialized to (-2,-2) before\n");
27816b8cff5SYuri Pankov T("    each test. All array elements from 0 to nmatch-1 must be specified\n");
27916b8cff5SYuri Pankov T("    in the outcome. Unspecified endpoints (offset -1) are denoted by ?.\n");
28016b8cff5SYuri Pankov T("    Unset endpoints (offset -2) are denoted by X. {x}(o:n) denotes a\n");
28116b8cff5SYuri Pankov T("    matched (?{...}) expression, where x is the text enclosed by {...},\n");
28216b8cff5SYuri Pankov T("    o is the expression ordinal counting from 1, and n is the length of\n");
28316b8cff5SYuri Pankov T("    the unmatched portion of the subject string. If x starts with a\n");
28416b8cff5SYuri Pankov T("    number then that is the return value of re_execf(), otherwise 0 is\n");
28516b8cff5SYuri Pankov T("    returned. RE_DUP_MAX[-+]N expands to the <limits.h> value -+N.\n");
28616b8cff5SYuri Pankov T("\n");
28716b8cff5SYuri Pankov T("  Field 5: optional comment appended to the report.\n");
28816b8cff5SYuri Pankov T("\n");
28916b8cff5SYuri Pankov T("CAVEAT\n");
29016b8cff5SYuri Pankov T("    If a regex implementation misbehaves with memory then all bets are off.\n");
29116b8cff5SYuri Pankov T("\n");
29216b8cff5SYuri Pankov T("CONTRIBUTORS\n");
29316b8cff5SYuri Pankov T("  Glenn Fowler    glenn.s.fowler@gmail.com        (ksh strmatch, regex extensions)\n");
29416b8cff5SYuri Pankov T("  David Korn      dgkorn@gmail.com        (ksh glob matcher)\n");
29516b8cff5SYuri Pankov T("  Doug McIlroy    mcilroy@dartmouth.edu       (ast regex/testre in C++)\n");
29616b8cff5SYuri Pankov T("  Tom Lord        lord@regexps.com            (rx tests)\n");
29716b8cff5SYuri Pankov T("  Henry Spencer   henry@zoo.toronto.edu       (original public regex)\n");
29816b8cff5SYuri Pankov T("  Andrew Hume     andrew@research.att.com     (gre tests)\n");
29916b8cff5SYuri Pankov T("  John Maddock    John_Maddock@compuserve.com (regex++ tests)\n");
30016b8cff5SYuri Pankov T("  Philip Hazel    ph10@cam.ac.uk              (pcre tests)\n");
30116b8cff5SYuri Pankov T("  Ville Laurikari vl@iki.fi                   (libtre tests)\n");
30216b8cff5SYuri Pankov H("</PRE>\n");
30316b8cff5SYuri Pankov H("</BODY>\n");
30416b8cff5SYuri Pankov H("</HTML>\n");
30516b8cff5SYuri Pankov }
30616b8cff5SYuri Pankov 
30716b8cff5SYuri Pankov #ifndef elementsof
30816b8cff5SYuri Pankov #define elementsof(x)	(sizeof(x)/sizeof(x[0]))
30916b8cff5SYuri Pankov #endif
31016b8cff5SYuri Pankov 
31116b8cff5SYuri Pankov #ifndef streq
31216b8cff5SYuri Pankov #define streq(a,b)	(*(a)==*(b)&&!strcmp(a,b))
31316b8cff5SYuri Pankov #endif
31416b8cff5SYuri Pankov 
31516b8cff5SYuri Pankov #define HUNG		2
31616b8cff5SYuri Pankov #define NOTEST		(~0)
31716b8cff5SYuri Pankov 
31816b8cff5SYuri Pankov #ifndef REG_TEST_DEFAULT
31916b8cff5SYuri Pankov #define REG_TEST_DEFAULT	0
32016b8cff5SYuri Pankov #endif
32116b8cff5SYuri Pankov 
32216b8cff5SYuri Pankov #ifndef REG_EXEC_DEFAULT
32316b8cff5SYuri Pankov #define REG_EXEC_DEFAULT	0
32416b8cff5SYuri Pankov #endif
32516b8cff5SYuri Pankov 
32616b8cff5SYuri Pankov static const char* unsupported[] =
32716b8cff5SYuri Pankov {
32816b8cff5SYuri Pankov 	"BASIC",
32916b8cff5SYuri Pankov #ifndef REG_EXTENDED
33016b8cff5SYuri Pankov 	"EXTENDED",
33116b8cff5SYuri Pankov #endif
33216b8cff5SYuri Pankov #ifndef REG_AUGMENTED
33316b8cff5SYuri Pankov 	"AUGMENTED",
33416b8cff5SYuri Pankov #endif
33516b8cff5SYuri Pankov #ifndef REG_SHELL
33616b8cff5SYuri Pankov 	"SHELL",
33716b8cff5SYuri Pankov #endif
33816b8cff5SYuri Pankov 
33916b8cff5SYuri Pankov #ifndef REG_CLASS_ESCAPE
34016b8cff5SYuri Pankov 	"CLASS_ESCAPE",
34116b8cff5SYuri Pankov #endif
34216b8cff5SYuri Pankov #ifndef REG_COMMENT
34316b8cff5SYuri Pankov 	"COMMENT",
34416b8cff5SYuri Pankov #endif
34516b8cff5SYuri Pankov #ifndef REG_DELIMITED
34616b8cff5SYuri Pankov 	"DELIMITED",
34716b8cff5SYuri Pankov #endif
34816b8cff5SYuri Pankov #ifndef REG_DISCIPLINE
34916b8cff5SYuri Pankov 	"DISCIPLINE",
35016b8cff5SYuri Pankov #endif
35116b8cff5SYuri Pankov #ifndef REG_ESCAPE
35216b8cff5SYuri Pankov 	"ESCAPE",
35316b8cff5SYuri Pankov #endif
35416b8cff5SYuri Pankov #ifndef REG_ICASE
35516b8cff5SYuri Pankov 	"ICASE",
35616b8cff5SYuri Pankov #endif
35716b8cff5SYuri Pankov #ifndef REG_LEFT
35816b8cff5SYuri Pankov 	"LEFT",
35916b8cff5SYuri Pankov #endif
36016b8cff5SYuri Pankov #ifndef REG_LENIENT
36116b8cff5SYuri Pankov 	"LENIENT",
36216b8cff5SYuri Pankov #endif
36316b8cff5SYuri Pankov #ifndef REG_LITERAL
36416b8cff5SYuri Pankov 	"LITERAL",
36516b8cff5SYuri Pankov #endif
36616b8cff5SYuri Pankov #ifndef REG_MINIMAL
36716b8cff5SYuri Pankov 	"MINIMAL",
36816b8cff5SYuri Pankov #endif
36916b8cff5SYuri Pankov #ifndef REG_MULTIPLE
37016b8cff5SYuri Pankov 	"MULTIPLE",
37116b8cff5SYuri Pankov #endif
37216b8cff5SYuri Pankov #ifndef REG_MULTIREF
37316b8cff5SYuri Pankov 	"MULTIREF",
37416b8cff5SYuri Pankov #endif
37516b8cff5SYuri Pankov #ifndef REG_MUSTDELIM
37616b8cff5SYuri Pankov 	"MUSTDELIM",
37716b8cff5SYuri Pankov #endif
37816b8cff5SYuri Pankov #ifndef REG_NEWLINE
37916b8cff5SYuri Pankov 	"NEWLINE",
38016b8cff5SYuri Pankov #endif
38116b8cff5SYuri Pankov #ifndef REG_NOTBOL
38216b8cff5SYuri Pankov 	"NOTBOL",
38316b8cff5SYuri Pankov #endif
38416b8cff5SYuri Pankov #ifndef REG_NOTEOL
38516b8cff5SYuri Pankov 	"NOTEOL",
38616b8cff5SYuri Pankov #endif
38716b8cff5SYuri Pankov #ifndef REG_NULL
38816b8cff5SYuri Pankov 	"NULL",
38916b8cff5SYuri Pankov #endif
39016b8cff5SYuri Pankov #ifndef REG_RIGHT
39116b8cff5SYuri Pankov 	"RIGHT",
39216b8cff5SYuri Pankov #endif
39316b8cff5SYuri Pankov #ifndef REG_SHELL_DOT
39416b8cff5SYuri Pankov 	"SHELL_DOT",
39516b8cff5SYuri Pankov #endif
39616b8cff5SYuri Pankov #ifndef REG_SHELL_ESCAPED
39716b8cff5SYuri Pankov 	"SHELL_ESCAPED",
39816b8cff5SYuri Pankov #endif
39916b8cff5SYuri Pankov #ifndef REG_SHELL_GROUP
40016b8cff5SYuri Pankov 	"SHELL_GROUP",
40116b8cff5SYuri Pankov #endif
40216b8cff5SYuri Pankov #ifndef REG_SHELL_PATH
40316b8cff5SYuri Pankov 	"SHELL_PATH",
40416b8cff5SYuri Pankov #endif
40516b8cff5SYuri Pankov #ifndef REG_SPAN
40616b8cff5SYuri Pankov 	"SPAN",
40716b8cff5SYuri Pankov #endif
40816b8cff5SYuri Pankov #if REG_NOSUB & REG_TEST_DEFAULT
40916b8cff5SYuri Pankov 	"SUBMATCH",
41016b8cff5SYuri Pankov #endif
41116b8cff5SYuri Pankov #if !_REG_nexec
41216b8cff5SYuri Pankov 	"regnexec",
41316b8cff5SYuri Pankov #endif
41416b8cff5SYuri Pankov #if !_REG_subcomp
41516b8cff5SYuri Pankov 	"regsubcomp",
41616b8cff5SYuri Pankov #endif
41716b8cff5SYuri Pankov #if !_REG_decomp
41816b8cff5SYuri Pankov 	"redecomp",
41916b8cff5SYuri Pankov #endif
42016b8cff5SYuri Pankov 	0
42116b8cff5SYuri Pankov };
42216b8cff5SYuri Pankov 
42316b8cff5SYuri Pankov #ifndef REG_CLASS_ESCAPE
42416b8cff5SYuri Pankov #define REG_CLASS_ESCAPE	NOTEST
42516b8cff5SYuri Pankov #endif
42616b8cff5SYuri Pankov #ifndef REG_COMMENT
42716b8cff5SYuri Pankov #define REG_COMMENT	NOTEST
42816b8cff5SYuri Pankov #endif
42916b8cff5SYuri Pankov #ifndef REG_DELIMITED
43016b8cff5SYuri Pankov #define REG_DELIMITED	NOTEST
43116b8cff5SYuri Pankov #endif
43216b8cff5SYuri Pankov #ifndef REG_ESCAPE
43316b8cff5SYuri Pankov #define REG_ESCAPE	NOTEST
43416b8cff5SYuri Pankov #endif
43516b8cff5SYuri Pankov #ifndef REG_ICASE
43616b8cff5SYuri Pankov #define REG_ICASE	NOTEST
43716b8cff5SYuri Pankov #endif
43816b8cff5SYuri Pankov #ifndef REG_LEFT
43916b8cff5SYuri Pankov #define REG_LEFT	NOTEST
44016b8cff5SYuri Pankov #endif
44116b8cff5SYuri Pankov #ifndef REG_LENIENT
44216b8cff5SYuri Pankov #define REG_LENIENT	0
44316b8cff5SYuri Pankov #endif
44416b8cff5SYuri Pankov #ifndef REG_MINIMAL
44516b8cff5SYuri Pankov #define REG_MINIMAL	NOTEST
44616b8cff5SYuri Pankov #endif
44716b8cff5SYuri Pankov #ifndef REG_MULTIPLE
44816b8cff5SYuri Pankov #define REG_MULTIPLE	NOTEST
44916b8cff5SYuri Pankov #endif
45016b8cff5SYuri Pankov #ifndef REG_MULTIREF
45116b8cff5SYuri Pankov #define REG_MULTIREF	NOTEST
45216b8cff5SYuri Pankov #endif
45316b8cff5SYuri Pankov #ifndef REG_MUSTDELIM
45416b8cff5SYuri Pankov #define REG_MUSTDELIM	NOTEST
45516b8cff5SYuri Pankov #endif
45616b8cff5SYuri Pankov #ifndef REG_NEWLINE
45716b8cff5SYuri Pankov #define REG_NEWLINE	NOTEST
45816b8cff5SYuri Pankov #endif
45916b8cff5SYuri Pankov #ifndef REG_NOTBOL
46016b8cff5SYuri Pankov #define REG_NOTBOL	NOTEST
46116b8cff5SYuri Pankov #endif
46216b8cff5SYuri Pankov #ifndef REG_NOTEOL
46316b8cff5SYuri Pankov #define REG_NOTEOL	NOTEST
46416b8cff5SYuri Pankov #endif
46516b8cff5SYuri Pankov #ifndef REG_NULL
46616b8cff5SYuri Pankov #define REG_NULL	NOTEST
46716b8cff5SYuri Pankov #endif
46816b8cff5SYuri Pankov #ifndef REG_RIGHT
46916b8cff5SYuri Pankov #define REG_RIGHT	NOTEST
47016b8cff5SYuri Pankov #endif
47116b8cff5SYuri Pankov #ifndef REG_SHELL_DOT
47216b8cff5SYuri Pankov #define REG_SHELL_DOT	NOTEST
47316b8cff5SYuri Pankov #endif
47416b8cff5SYuri Pankov #ifndef REG_SHELL_ESCAPED
47516b8cff5SYuri Pankov #define REG_SHELL_ESCAPED	NOTEST
47616b8cff5SYuri Pankov #endif
47716b8cff5SYuri Pankov #ifndef REG_SHELL_GROUP
47816b8cff5SYuri Pankov #define REG_SHELL_GROUP	NOTEST
47916b8cff5SYuri Pankov #endif
48016b8cff5SYuri Pankov #ifndef REG_SHELL_PATH
48116b8cff5SYuri Pankov #define REG_SHELL_PATH	NOTEST
48216b8cff5SYuri Pankov #endif
48316b8cff5SYuri Pankov #ifndef REG_SPAN
48416b8cff5SYuri Pankov #define REG_SPAN	NOTEST
48516b8cff5SYuri Pankov #endif
48616b8cff5SYuri Pankov 
48716b8cff5SYuri Pankov #define REG_UNKNOWN	(-1)
48816b8cff5SYuri Pankov 
48916b8cff5SYuri Pankov #ifndef REG_ENEWLINE
49016b8cff5SYuri Pankov #define REG_ENEWLINE	(REG_UNKNOWN-1)
49116b8cff5SYuri Pankov #endif
49216b8cff5SYuri Pankov #ifndef REG_ENULL
49316b8cff5SYuri Pankov #ifndef REG_EMPTY
49416b8cff5SYuri Pankov #define REG_ENULL	(REG_UNKNOWN-2)
49516b8cff5SYuri Pankov #else
49616b8cff5SYuri Pankov #define REG_ENULL	REG_EMPTY
49716b8cff5SYuri Pankov #endif
49816b8cff5SYuri Pankov #endif
49916b8cff5SYuri Pankov #ifndef REG_ECOUNT
50016b8cff5SYuri Pankov #define REG_ECOUNT	(REG_UNKNOWN-3)
50116b8cff5SYuri Pankov #endif
50216b8cff5SYuri Pankov #ifndef REG_BADESC
50316b8cff5SYuri Pankov #define REG_BADESC	(REG_UNKNOWN-4)
50416b8cff5SYuri Pankov #endif
50516b8cff5SYuri Pankov #ifndef REG_EMEM
50616b8cff5SYuri Pankov #define REG_EMEM	(REG_UNKNOWN-5)
50716b8cff5SYuri Pankov #endif
50816b8cff5SYuri Pankov #ifndef REG_EHUNG
50916b8cff5SYuri Pankov #define REG_EHUNG	(REG_UNKNOWN-6)
51016b8cff5SYuri Pankov #endif
51116b8cff5SYuri Pankov #ifndef REG_EBUS
51216b8cff5SYuri Pankov #define REG_EBUS	(REG_UNKNOWN-7)
51316b8cff5SYuri Pankov #endif
51416b8cff5SYuri Pankov #ifndef REG_EFAULT
51516b8cff5SYuri Pankov #define REG_EFAULT	(REG_UNKNOWN-8)
51616b8cff5SYuri Pankov #endif
51716b8cff5SYuri Pankov #ifndef REG_EFLAGS
51816b8cff5SYuri Pankov #define REG_EFLAGS	(REG_UNKNOWN-9)
51916b8cff5SYuri Pankov #endif
52016b8cff5SYuri Pankov #ifndef REG_EDELIM
52116b8cff5SYuri Pankov #define REG_EDELIM	(REG_UNKNOWN-9)
52216b8cff5SYuri Pankov #endif
52316b8cff5SYuri Pankov 
52416b8cff5SYuri Pankov static const struct { int code; char* name; } codes[] =
52516b8cff5SYuri Pankov {
52616b8cff5SYuri Pankov 	REG_UNKNOWN,	"UNKNOWN",
52716b8cff5SYuri Pankov 	REG_NOMATCH,	"NOMATCH",
52816b8cff5SYuri Pankov 	REG_BADPAT,	"BADPAT",
52916b8cff5SYuri Pankov 	REG_ECOLLATE,	"ECOLLATE",
53016b8cff5SYuri Pankov 	REG_ECTYPE,	"ECTYPE",
53116b8cff5SYuri Pankov 	REG_EESCAPE,	"EESCAPE",
53216b8cff5SYuri Pankov 	REG_ESUBREG,	"ESUBREG",
53316b8cff5SYuri Pankov 	REG_EBRACK,	"EBRACK",
53416b8cff5SYuri Pankov 	REG_EPAREN,	"EPAREN",
53516b8cff5SYuri Pankov 	REG_EBRACE,	"EBRACE",
53616b8cff5SYuri Pankov 	REG_BADBR,	"BADBR",
53716b8cff5SYuri Pankov 	REG_ERANGE,	"ERANGE",
53816b8cff5SYuri Pankov 	REG_ESPACE,	"ESPACE",
53916b8cff5SYuri Pankov 	REG_BADRPT,	"BADRPT",
54016b8cff5SYuri Pankov 	REG_ENEWLINE,	"ENEWLINE",
54116b8cff5SYuri Pankov 	REG_ENULL,	"ENULL",
54216b8cff5SYuri Pankov 	REG_ECOUNT,	"ECOUNT",
54316b8cff5SYuri Pankov 	REG_BADESC,	"BADESC",
54416b8cff5SYuri Pankov 	REG_EMEM,	"EMEM",
54516b8cff5SYuri Pankov 	REG_EHUNG,	"EHUNG",
54616b8cff5SYuri Pankov 	REG_EBUS,	"EBUS",
54716b8cff5SYuri Pankov 	REG_EFAULT,	"EFAULT",
54816b8cff5SYuri Pankov 	REG_EFLAGS,	"EFLAGS",
54916b8cff5SYuri Pankov 	REG_EDELIM,	"EDELIM",
55016b8cff5SYuri Pankov };
55116b8cff5SYuri Pankov 
55216b8cff5SYuri Pankov static struct
55316b8cff5SYuri Pankov {
55416b8cff5SYuri Pankov 	regmatch_t	NOMATCH;
55516b8cff5SYuri Pankov 	int		errors;
55616b8cff5SYuri Pankov 	int		extracted;
55716b8cff5SYuri Pankov 	int		ignored;
55816b8cff5SYuri Pankov 	int		lineno;
55916b8cff5SYuri Pankov 	int		passed;
56016b8cff5SYuri Pankov 	int		signals;
56116b8cff5SYuri Pankov 	int		unspecified;
56216b8cff5SYuri Pankov 	int		verify;
56316b8cff5SYuri Pankov 	int		warnings;
56416b8cff5SYuri Pankov 	char*		file;
56516b8cff5SYuri Pankov 	char*		stack;
56616b8cff5SYuri Pankov 	char*		which;
56716b8cff5SYuri Pankov 	jmp_buf		gotcha;
56816b8cff5SYuri Pankov #ifdef REG_DISCIPLINE
56916b8cff5SYuri Pankov 	Disc_t		disc;
57016b8cff5SYuri Pankov #endif
57116b8cff5SYuri Pankov } state;
57216b8cff5SYuri Pankov 
57316b8cff5SYuri Pankov static void
quote(char * s,int len,unsigned long test)57416b8cff5SYuri Pankov quote(char* s, int len, unsigned long test)
57516b8cff5SYuri Pankov {
57616b8cff5SYuri Pankov 	unsigned char*	u = (unsigned char*)s;
57716b8cff5SYuri Pankov 	unsigned char*	e;
57816b8cff5SYuri Pankov 	int		c;
57916b8cff5SYuri Pankov #ifdef MB_CUR_MAX
58016b8cff5SYuri Pankov 	int		w;
58116b8cff5SYuri Pankov #endif
58216b8cff5SYuri Pankov 
58316b8cff5SYuri Pankov 	if (!u)
58416b8cff5SYuri Pankov 		printf("NIL");
58516b8cff5SYuri Pankov 	else if (!*u && len <= 1)
58616b8cff5SYuri Pankov 		printf("NULL");
58716b8cff5SYuri Pankov 	else if (test & TEST_EXPAND)
58816b8cff5SYuri Pankov 	{
58916b8cff5SYuri Pankov 		if (len < 0)
59016b8cff5SYuri Pankov 			len = strlen((char*)u);
59116b8cff5SYuri Pankov 		e = u + len;
59216b8cff5SYuri Pankov 		if (test & TEST_DELIMIT)
59316b8cff5SYuri Pankov 			printf("\"");
59416b8cff5SYuri Pankov 		while (u < e)
59516b8cff5SYuri Pankov 			switch (c = *u++)
59616b8cff5SYuri Pankov 			{
59716b8cff5SYuri Pankov 			case '\\':
59816b8cff5SYuri Pankov 				printf("\\\\");
59916b8cff5SYuri Pankov 				break;
60016b8cff5SYuri Pankov 			case '"':
60116b8cff5SYuri Pankov 				if (test & TEST_DELIMIT)
60216b8cff5SYuri Pankov 					printf("\\\"");
60316b8cff5SYuri Pankov 				else
60416b8cff5SYuri Pankov 					printf("\"");
60516b8cff5SYuri Pankov 				break;
60616b8cff5SYuri Pankov 			case '\a':
60716b8cff5SYuri Pankov 				printf("\\a");
60816b8cff5SYuri Pankov 				break;
60916b8cff5SYuri Pankov 			case '\b':
61016b8cff5SYuri Pankov 				printf("\\b");
61116b8cff5SYuri Pankov 				break;
61216b8cff5SYuri Pankov 			case 033:
61316b8cff5SYuri Pankov 				printf("\\e");
61416b8cff5SYuri Pankov 				break;
61516b8cff5SYuri Pankov 			case '\f':
61616b8cff5SYuri Pankov 				printf("\\f");
61716b8cff5SYuri Pankov 				break;
61816b8cff5SYuri Pankov 			case '\n':
61916b8cff5SYuri Pankov 				printf("\\n");
62016b8cff5SYuri Pankov 				break;
62116b8cff5SYuri Pankov 			case '\r':
62216b8cff5SYuri Pankov 				printf("\\r");
62316b8cff5SYuri Pankov 				break;
62416b8cff5SYuri Pankov 			case '\t':
62516b8cff5SYuri Pankov 				printf("\\t");
62616b8cff5SYuri Pankov 				break;
62716b8cff5SYuri Pankov 			case '\v':
62816b8cff5SYuri Pankov 				printf("\\v");
62916b8cff5SYuri Pankov 				break;
63016b8cff5SYuri Pankov 			default:
63116b8cff5SYuri Pankov #ifdef MB_CUR_MAX
63216b8cff5SYuri Pankov 				s = (char*)u - 1;
63316b8cff5SYuri Pankov 				if ((w = mblen(s, (char*)e - s)) > 1)
63416b8cff5SYuri Pankov 				{
63516b8cff5SYuri Pankov 					u += w - 1;
63616b8cff5SYuri Pankov 					fwrite(s, 1, w, stdout);
63716b8cff5SYuri Pankov 				}
63816b8cff5SYuri Pankov 				else
63916b8cff5SYuri Pankov #endif
64016b8cff5SYuri Pankov 				if (!iscntrl(c) && isprint(c))
64116b8cff5SYuri Pankov 					putchar(c);
64216b8cff5SYuri Pankov 				else
64316b8cff5SYuri Pankov 					printf("\\x%02x", c);
64416b8cff5SYuri Pankov 				break;
64516b8cff5SYuri Pankov 			}
64616b8cff5SYuri Pankov 		if (test & TEST_DELIMIT)
64716b8cff5SYuri Pankov 			printf("\"");
64816b8cff5SYuri Pankov 	}
64916b8cff5SYuri Pankov 	else
65016b8cff5SYuri Pankov 		printf("%s", s);
65116b8cff5SYuri Pankov }
65216b8cff5SYuri Pankov 
65316b8cff5SYuri Pankov static void
report(char * comment,char * fun,char * re,char * s,int len,char * msg,int flags,unsigned long test)65416b8cff5SYuri Pankov report(char* comment, char* fun, char* re, char* s, int len, char* msg, int flags, unsigned long test)
65516b8cff5SYuri Pankov {
65616b8cff5SYuri Pankov 	if (state.file)
65716b8cff5SYuri Pankov 		printf("%s:", state.file);
65816b8cff5SYuri Pankov 	printf("%d:", state.lineno);
65916b8cff5SYuri Pankov 	if (re)
66016b8cff5SYuri Pankov 	{
66116b8cff5SYuri Pankov 		printf(" ");
66216b8cff5SYuri Pankov 		quote(re, -1, test|TEST_DELIMIT);
66316b8cff5SYuri Pankov 		if (s)
66416b8cff5SYuri Pankov 		{
66516b8cff5SYuri Pankov 			printf(" versus ");
66616b8cff5SYuri Pankov 			quote(s, len, test|TEST_DELIMIT);
66716b8cff5SYuri Pankov 		}
66816b8cff5SYuri Pankov 	}
66916b8cff5SYuri Pankov 	if (test & TEST_UNSPECIFIED)
67016b8cff5SYuri Pankov 	{
67116b8cff5SYuri Pankov 		state.unspecified++;
67216b8cff5SYuri Pankov 		printf(" unspecified behavior");
67316b8cff5SYuri Pankov 	}
67416b8cff5SYuri Pankov 	else
67516b8cff5SYuri Pankov 		state.errors++;
67616b8cff5SYuri Pankov 	if (state.which)
67716b8cff5SYuri Pankov 		printf(" %s", state.which);
67816b8cff5SYuri Pankov 	if (flags & REG_NOSUB)
67916b8cff5SYuri Pankov 		printf(" NOSUB");
68016b8cff5SYuri Pankov 	if (fun)
68116b8cff5SYuri Pankov 		printf(" %s", fun);
68216b8cff5SYuri Pankov 	if (comment[strlen(comment)-1] == '\n')
68316b8cff5SYuri Pankov 		printf(" %s", comment);
68416b8cff5SYuri Pankov 	else
68516b8cff5SYuri Pankov 	{
68616b8cff5SYuri Pankov 		printf(" %s: ", comment);
68716b8cff5SYuri Pankov 		if (msg)
68816b8cff5SYuri Pankov 			printf("%s: ", msg);
68916b8cff5SYuri Pankov 	}
69016b8cff5SYuri Pankov }
69116b8cff5SYuri Pankov 
69216b8cff5SYuri Pankov static void
error(regex_t * preg,int code)69316b8cff5SYuri Pankov error(regex_t* preg, int code)
69416b8cff5SYuri Pankov {
69516b8cff5SYuri Pankov 	char*	msg;
69616b8cff5SYuri Pankov 	char	buf[256];
69716b8cff5SYuri Pankov 
69816b8cff5SYuri Pankov 	switch (code)
69916b8cff5SYuri Pankov 	{
70016b8cff5SYuri Pankov 	case REG_EBUS:
70116b8cff5SYuri Pankov 		msg = "bus error";
70216b8cff5SYuri Pankov 		break;
70316b8cff5SYuri Pankov 	case REG_EFAULT:
70416b8cff5SYuri Pankov 		msg = "memory fault";
70516b8cff5SYuri Pankov 		break;
70616b8cff5SYuri Pankov 	case REG_EHUNG:
70716b8cff5SYuri Pankov 		msg = "did not terminate";
70816b8cff5SYuri Pankov 		break;
70916b8cff5SYuri Pankov 	default:
71016b8cff5SYuri Pankov 		regerror(code, preg, msg = buf, sizeof buf);
71116b8cff5SYuri Pankov 		break;
71216b8cff5SYuri Pankov 	}
71316b8cff5SYuri Pankov 	printf("%s\n", msg);
71416b8cff5SYuri Pankov }
71516b8cff5SYuri Pankov 
71616b8cff5SYuri Pankov static void
bad(char * comment,char * re,char * s,int len,unsigned long test)71716b8cff5SYuri Pankov bad(char* comment, char* re, char* s, int len, unsigned long test)
71816b8cff5SYuri Pankov {
71916b8cff5SYuri Pankov 	printf("bad test case ");
72016b8cff5SYuri Pankov 	report(comment, NiL, re, s, len, NiL, 0, test);
72116b8cff5SYuri Pankov 	exit(1);
72216b8cff5SYuri Pankov }
72316b8cff5SYuri Pankov 
72416b8cff5SYuri Pankov static int
escape(char * s)72516b8cff5SYuri Pankov escape(char* s)
72616b8cff5SYuri Pankov {
72716b8cff5SYuri Pankov 	char*	b;
72816b8cff5SYuri Pankov 	char*	t;
72916b8cff5SYuri Pankov 	char*	q;
73016b8cff5SYuri Pankov 	char*	e;
73116b8cff5SYuri Pankov 	int	c;
73216b8cff5SYuri Pankov 
73316b8cff5SYuri Pankov 	for (b = t = s; *t = *s; s++, t++)
73416b8cff5SYuri Pankov 		if (*s == '\\')
73516b8cff5SYuri Pankov 			switch (*++s)
73616b8cff5SYuri Pankov 			{
73716b8cff5SYuri Pankov 			case '\\':
73816b8cff5SYuri Pankov 				break;
73916b8cff5SYuri Pankov 			case 'a':
74016b8cff5SYuri Pankov 				*t = '\a';
74116b8cff5SYuri Pankov 				break;
74216b8cff5SYuri Pankov 			case 'b':
74316b8cff5SYuri Pankov 				*t = '\b';
74416b8cff5SYuri Pankov 				break;
74516b8cff5SYuri Pankov 			case 'c':
74616b8cff5SYuri Pankov 				if (*t = *++s)
74716b8cff5SYuri Pankov 					*t &= 037;
74816b8cff5SYuri Pankov 				else
74916b8cff5SYuri Pankov 					s--;
75016b8cff5SYuri Pankov 				break;
75116b8cff5SYuri Pankov 			case 'e':
75216b8cff5SYuri Pankov 			case 'E':
75316b8cff5SYuri Pankov 				*t = 033;
75416b8cff5SYuri Pankov 				break;
75516b8cff5SYuri Pankov 			case 'f':
75616b8cff5SYuri Pankov 				*t = '\f';
75716b8cff5SYuri Pankov 				break;
75816b8cff5SYuri Pankov 			case 'n':
75916b8cff5SYuri Pankov 				*t = '\n';
76016b8cff5SYuri Pankov 				break;
76116b8cff5SYuri Pankov 			case 'r':
76216b8cff5SYuri Pankov 				*t = '\r';
76316b8cff5SYuri Pankov 				break;
76416b8cff5SYuri Pankov 			case 's':
76516b8cff5SYuri Pankov 				*t = ' ';
76616b8cff5SYuri Pankov 				break;
76716b8cff5SYuri Pankov 			case 't':
76816b8cff5SYuri Pankov 				*t = '\t';
76916b8cff5SYuri Pankov 				break;
77016b8cff5SYuri Pankov 			case 'v':
77116b8cff5SYuri Pankov 				*t = '\v';
77216b8cff5SYuri Pankov 				break;
77316b8cff5SYuri Pankov 			case 'u':
77416b8cff5SYuri Pankov 			case 'x':
77516b8cff5SYuri Pankov 				c = 0;
77616b8cff5SYuri Pankov 				q = c == 'u' ? (s + 5) : (char*)0;
77716b8cff5SYuri Pankov 				e = s + 1;
77816b8cff5SYuri Pankov 				while (!e || !q || s < q)
77916b8cff5SYuri Pankov 				{
78016b8cff5SYuri Pankov 					switch (*++s)
78116b8cff5SYuri Pankov 					{
78216b8cff5SYuri Pankov 					case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
78316b8cff5SYuri Pankov 						c = (c << 4) + *s - 'a' + 10;
78416b8cff5SYuri Pankov 						continue;
78516b8cff5SYuri Pankov 					case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
78616b8cff5SYuri Pankov 						c = (c << 4) + *s - 'A' + 10;
78716b8cff5SYuri Pankov 						continue;
78816b8cff5SYuri Pankov 					case '0': case '1': case '2': case '3': case '4':
78916b8cff5SYuri Pankov 					case '5': case '6': case '7': case '8': case '9':
79016b8cff5SYuri Pankov 						c = (c << 4) + *s - '0';
79116b8cff5SYuri Pankov 						continue;
79216b8cff5SYuri Pankov 					case '{':
79316b8cff5SYuri Pankov 					case '[':
79416b8cff5SYuri Pankov 						if (s != e)
79516b8cff5SYuri Pankov 						{
79616b8cff5SYuri Pankov 							s--;
79716b8cff5SYuri Pankov 							break;
79816b8cff5SYuri Pankov 						}
79916b8cff5SYuri Pankov 						e = 0;
80016b8cff5SYuri Pankov 						continue;
80116b8cff5SYuri Pankov 					case '}':
80216b8cff5SYuri Pankov 					case ']':
80316b8cff5SYuri Pankov 						if (e)
80416b8cff5SYuri Pankov 							s--;
80516b8cff5SYuri Pankov 						break;
80616b8cff5SYuri Pankov 					default:
80716b8cff5SYuri Pankov 						s--;
80816b8cff5SYuri Pankov 						break;
80916b8cff5SYuri Pankov 					}
81016b8cff5SYuri Pankov 					break;
81116b8cff5SYuri Pankov 				}
81216b8cff5SYuri Pankov 				*t = c;
81316b8cff5SYuri Pankov 				break;
81416b8cff5SYuri Pankov 			case '0': case '1': case '2': case '3':
81516b8cff5SYuri Pankov 			case '4': case '5': case '6': case '7':
81616b8cff5SYuri Pankov 				c = *s - '0';
81716b8cff5SYuri Pankov 				q = s + 2;
81816b8cff5SYuri Pankov 				while (s < q)
81916b8cff5SYuri Pankov 				{
82016b8cff5SYuri Pankov 					switch (*++s)
82116b8cff5SYuri Pankov 					{
82216b8cff5SYuri Pankov 					case '0': case '1': case '2': case '3':
82316b8cff5SYuri Pankov 					case '4': case '5': case '6': case '7':
82416b8cff5SYuri Pankov 						c = (c << 3) + *s - '0';
82516b8cff5SYuri Pankov 						break;
82616b8cff5SYuri Pankov 					default:
82716b8cff5SYuri Pankov 						q = --s;
82816b8cff5SYuri Pankov 						break;
82916b8cff5SYuri Pankov 					}
83016b8cff5SYuri Pankov 				}
83116b8cff5SYuri Pankov 				*t = c;
83216b8cff5SYuri Pankov 				break;
83316b8cff5SYuri Pankov 			default:
83416b8cff5SYuri Pankov 				*(s + 1) = 0;
83516b8cff5SYuri Pankov 				bad("invalid C \\ escape\n", s - 1, NiL, 0, 0);
83616b8cff5SYuri Pankov 			}
83716b8cff5SYuri Pankov 	return t - b;
83816b8cff5SYuri Pankov }
83916b8cff5SYuri Pankov 
84016b8cff5SYuri Pankov static void
matchoffprint(int off)84116b8cff5SYuri Pankov matchoffprint(int off)
84216b8cff5SYuri Pankov {
84316b8cff5SYuri Pankov 	switch (off)
84416b8cff5SYuri Pankov 	{
84516b8cff5SYuri Pankov 	case -2:
84616b8cff5SYuri Pankov 		printf("X");
84716b8cff5SYuri Pankov 		break;
84816b8cff5SYuri Pankov 	case -1:
84916b8cff5SYuri Pankov 		printf("?");
85016b8cff5SYuri Pankov 		break;
85116b8cff5SYuri Pankov 	default:
85216b8cff5SYuri Pankov 		printf("%d", off);
85316b8cff5SYuri Pankov 		break;
85416b8cff5SYuri Pankov 	}
85516b8cff5SYuri Pankov }
85616b8cff5SYuri Pankov 
85716b8cff5SYuri Pankov static void
matchprint(regmatch_t * match,int nmatch,int nsub,char * ans,unsigned long test)85816b8cff5SYuri Pankov matchprint(regmatch_t* match, int nmatch, int nsub, char* ans, unsigned long test)
85916b8cff5SYuri Pankov {
86016b8cff5SYuri Pankov 	int	i;
86116b8cff5SYuri Pankov 
86216b8cff5SYuri Pankov 	for (; nmatch > nsub + 1; nmatch--)
86316b8cff5SYuri Pankov 		if ((match[nmatch-1].rm_so != -1 || match[nmatch-1].rm_eo != -1) && (!(test & TEST_IGNORE_POSITION) || match[nmatch-1].rm_so >= 0 && match[nmatch-1].rm_eo >= 0))
86416b8cff5SYuri Pankov 			break;
86516b8cff5SYuri Pankov 	for (i = 0; i < nmatch; i++)
86616b8cff5SYuri Pankov 	{
86716b8cff5SYuri Pankov 		printf("(");
86816b8cff5SYuri Pankov 		matchoffprint(match[i].rm_so);
86916b8cff5SYuri Pankov 		printf(",");
87016b8cff5SYuri Pankov 		matchoffprint(match[i].rm_eo);
87116b8cff5SYuri Pankov 		printf(")");
87216b8cff5SYuri Pankov 	}
87316b8cff5SYuri Pankov&nb