1314818Sngie/*	$NetBSD: t_regex_att.c,v 1.3 2017/01/14 20:59:23 christos Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Christos Zoulas.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie * 3. All advertising materials mentioning features or use of this software
19272343Sngie *    must display the following acknowledgement:
20272343Sngie *        This product includes software developed by the NetBSD
21272343Sngie *        Foundation, Inc. and its contributors.
22272343Sngie * 4. Neither the name of The NetBSD Foundation nor the names of its
23272343Sngie *    contributors may be used to endorse or promote products derived
24272343Sngie *    from this software without specific prior written permission.
25272343Sngie *
26272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36272343Sngie * POSSIBILITY OF SUCH DAMAGE.
37272343Sngie */
38272343Sngie
39272343Sngie#include <sys/cdefs.h>
40314818Sngie__RCSID("$NetBSD: t_regex_att.c,v 1.3 2017/01/14 20:59:23 christos Exp $");
41272343Sngie
42272343Sngie#include <sys/param.h>
43272343Sngie
44314818Sngie#include <atf-c.h>
45314818Sngie#include <ctype.h>
46314818Sngie#include <regex.h>
47272343Sngie#include <stdio.h>
48314818Sngie#include <stdlib.h>
49272343Sngie#include <string.h>
50314818Sngie#include <util.h>
51272343Sngie#include <vis.h>
52272343Sngie
53272343Sngiestatic const char sep[] = "\r\n\t";
54272343Sngiestatic const char delim[3] = "\\\\\0";
55272343Sngie
56272343Sngie
57272343Sngiestatic void
58272343Sngiefail(const char *pattern, const char *input, size_t lineno) {
59272343Sngie	fprintf(stderr,
60272343Sngie	    "skipping failed test at line %zu (pattern=%s, input=%s)\n",
61272343Sngie	    lineno, pattern, input);
62272343Sngie}
63272343Sngie
64272343Sngiestatic int
65272343Sngiebug(const char *pattern, const char *input, size_t lineno) {
66272343Sngie	static const struct {
67272343Sngie		const char *p;
68272343Sngie		const char *i;
69272343Sngie	} b[] = {
70272343Sngie#if defined(REGEX_SPENCER)
71272343Sngie		/*
72272343Sngie		 * The default libc implementation by Henry Spencer
73272343Sngie		 */
74272343Sngie		{ "a[-]?c", "ac" },			// basic.dat
75272343Sngie		{ "(a*)*", "a" },			// categorization.dat
76272343Sngie		{ "(aba|a*b)*", "ababa" },		// categorization.dat
77272343Sngie		{ "\\(a\\(b\\)*\\)*\\2", "abab" },	// categorization.dat
78272343Sngie		{ "(a*)*", "aaaaaa" },			// nullsubexpression.dat
79272343Sngie		{ "(a*)*", "aaaaaax" },			// nullsubexpression.dat
80272343Sngie		{ "(a*)+", "a" },			// nullsubexpression.dat
81272343Sngie		{ "(a*)+", "aaaaaa" },			// nullsubexpression.dat
82272343Sngie		{ "(a*)+", "aaaaaax" },			// nullsubexpression.dat
83272343Sngie		{ "([a]*)*", "a" },			// nullsubexpression.dat
84272343Sngie		{ "([a]*)*", "aaaaaa" },		// nullsubexpression.dat
85272343Sngie		{ "([a]*)*", "aaaaaax" },		// nullsubexpression.dat
86272343Sngie		{ "([a]*)+", "a" },			// nullsubexpression.dat
87272343Sngie		{ "([a]*)+", "aaaaaa" },		// nullsubexpression.dat
88272343Sngie		{ "([a]*)+", "aaaaaax" },		// nullsubexpression.dat
89272343Sngie		{ "([^b]*)*", "a" },			// nullsubexpression.dat
90272343Sngie		{ "([^b]*)*", "aaaaaa" },		// nullsubexpression.dat
91272343Sngie		{ "([^b]*)*", "aaaaaab" },		// nullsubexpression.dat
92272343Sngie		{ "([ab]*)*", "a" },			// nullsubexpression.dat
93272343Sngie		{ "([ab]*)*", "aaaaaa" },		// nullsubexpression.dat
94272343Sngie		{ "([ab]*)*", "ababab" },		// nullsubexpression.dat
95272343Sngie		{ "([ab]*)*", "bababa" },		// nullsubexpression.dat
96272343Sngie		{ "([ab]*)*", "b" },			// nullsubexpression.dat
97272343Sngie		{ "([ab]*)*", "bbbbbb" },		// nullsubexpression.dat
98272343Sngie		{ "([ab]*)*", "aaaabcde" },		// nullsubexpression.dat
99272343Sngie		{ "([^a]*)*", "b" },			// nullsubexpression.dat
100272343Sngie		{ "([^a]*)*", "bbbbbb" },		// nullsubexpression.dat
101272343Sngie		{ "([^ab]*)*", "ccccxx" },		// nullsubexpression.dat
102272343Sngie		{ "\\(a*\\)*\\(x\\)", "ax" },		// nullsubexpression.dat
103272343Sngie		{ "\\(a*\\)*\\(x\\)", "axa" },		// nullsubexpression.dat
104272343Sngie		{ "\\(a*\\)*\\(x\\)\\(\\1\\)", "x" },	// nullsubexpression.dat
105272343Sngie/* crash! */	{ "\\(a*\\)*\\(x\\)\\(\\1\\)", "ax" },	// nullsubexpression.dat
106272343Sngie/* crash! */	{ "\\(a*\\)*\\(x\\)\\(\\1\\)\\(x\\)", "axxa" },	// ""
107272343Sngie		{ "(a*)*(x)",  "ax" },			// nullsubexpression.dat
108272343Sngie		{ "(a*)*(x)",  "axa" },			// nullsubexpression.dat
109272343Sngie		{ "(a*)+(x)",  "ax" },			// nullsubexpression.dat
110272343Sngie		{ "(a*)+(x)",  "axa" },			// nullsubexpression.dat
111272343Sngie		{ "((a|ab)(c|bcd))(d*)", "abcd" },	// forcedassoc.dat
112272343Sngie		{ "((a|ab)(bcd|c))(d*)", "abcd" },	// forcedassoc.dat
113272343Sngie		{ "((ab|a)(c|bcd))(d*)", "abcd" },	// forcedassoc.dat
114272343Sngie		{ "((ab|a)(bcd|c))(d*)", "abcd" },	// forcedassoc.dat
115272343Sngie		{ "((a*)(b|abc))(c*)", "abc" },		// forcedassoc.dat
116272343Sngie		{ "((a*)(abc|b))(c*)", "abc" },		// forcedassoc.dat
117272343Sngie		{ "((..)|(.)){2}", "aaa" },		// repetition.dat
118272343Sngie		{ "((..)|(.)){3}", "aaa" },		// repetition.dat
119272343Sngie		{ "((..)|(.)){3}", "aaaa" },		// repetition.dat
120272343Sngie		{ "((..)|(.)){3}", "aaaaa" },		// repetition.dat
121272343Sngie		{ "X(.?){0,}Y", "X1234567Y" },		// repetition.dat
122272343Sngie		{ "X(.?){1,}Y", "X1234567Y" },		// repetition.dat
123272343Sngie		{ "X(.?){2,}Y", "X1234567Y" },		// repetition.dat
124272343Sngie		{ "X(.?){3,}Y", "X1234567Y" },		// repetition.dat
125272343Sngie		{ "X(.?){4,}Y", "X1234567Y" },		// repetition.dat
126272343Sngie		{ "X(.?){5,}Y", "X1234567Y" },		// repetition.dat
127272343Sngie		{ "X(.?){6,}Y", "X1234567Y" },		// repetition.dat
128272343Sngie		{ "X(.?){7,}Y", "X1234567Y" },		// repetition.dat
129272343Sngie		{ "X(.?){0,8}Y", "X1234567Y" },		// repetition.dat
130272343Sngie		{ "X(.?){1,8}Y", "X1234567Y" },		// repetition.dat
131272343Sngie		{ "X(.?){2,8}Y", "X1234567Y" },		// repetition.dat
132272343Sngie		{ "X(.?){3,8}Y", "X1234567Y" },		// repetition.dat
133272343Sngie		{ "X(.?){4,8}Y", "X1234567Y" },		// repetition.dat
134272343Sngie		{ "X(.?){5,8}Y", "X1234567Y" },		// repetition.dat
135272343Sngie		{ "X(.?){6,8}Y", "X1234567Y" },		// repetition.dat
136272343Sngie		{ "X(.?){7,8}Y", "X1234567Y" },		// repetition.dat
137272343Sngie		{ "(a|ab|c|bcd){0,}(d*)", "ababcd" },	// repetition.dat
138272343Sngie		{ "(a|ab|c|bcd){1,}(d*)", "ababcd" },	// repetition.dat
139272343Sngie		{ "(a|ab|c|bcd){2,}(d*)", "ababcd" },	// repetition.dat
140272343Sngie		{ "(a|ab|c|bcd){3,}(d*)", "ababcd" },	// repetition.dat
141272343Sngie		{ "(a|ab|c|bcd){1,10}(d*)", "ababcd" },	// repetition.dat
142272343Sngie		{ "(a|ab|c|bcd){2,10}(d*)", "ababcd" },	// repetition.dat
143272343Sngie		{ "(a|ab|c|bcd){3,10}(d*)", "ababcd" },	// repetition.dat
144272343Sngie		{ "(a|ab|c|bcd)*(d*)", "ababcd" },	// repetition.dat
145272343Sngie		{ "(a|ab|c|bcd)+(d*)", "ababcd" },	// repetition.dat
146272343Sngie		{ "(ab|a|c|bcd){0,}(d*)", "ababcd" },	// repetition.dat
147272343Sngie		{ "(ab|a|c|bcd){1,}(d*)", "ababcd" },	// repetition.dat
148272343Sngie		{ "(ab|a|c|bcd){2,}(d*)", "ababcd" },	// repetition.dat
149272343Sngie		{ "(ab|a|c|bcd){3,}(d*)", "ababcd" },	// repetition.dat
150272343Sngie		{ "(ab|a|c|bcd){1,10}(d*)", "ababcd" },	// repetition.dat
151272343Sngie		{ "(ab|a|c|bcd){2,10}(d*)", "ababcd" },	// repetition.dat
152272343Sngie		{ "(ab|a|c|bcd){3,10}(d*)", "ababcd" },	// repetition.dat
153272343Sngie		{ "(ab|a|c|bcd)*(d*)", "ababcd" },	// repetition.dat
154272343Sngie		{ "(ab|a|c|bcd)+(d*)", "ababcd" },	// repetition.dat
155272343Sngie#elif defined(REGEX_TRE)
156272343Sngie		{ "a[-]?c", "ac" },			// basic.dat
157272343Sngie		{ "a\\(b\\)*\\1", "a" },		// categorization.dat
158272343Sngie		{ "a\\(b\\)*\\1", "abab" },		// categorization.dat
159272343Sngie		{ "\\(a\\(b\\)*\\)*\\2", "abab" },	// categorization.dat
160272343Sngie		{ "\\(a*\\)*\\(x\\)\\(\\1\\)", "ax" },	// categorization.dat
161272343Sngie		{ "\\(a*\\)*\\(x\\)\\(\\1\\)\\(x\\)", "axxa" },	// ""
162272343Sngie		{ "((..)|(.))*", "aa" },		// repetition.dat
163272343Sngie		{ "((..)|(.))*", "aaa" },		// repetition.dat
164272343Sngie		{ "((..)|(.))*", "aaaaa" },		// repetition.dat
165272343Sngie		{ "X(.?){7,}Y", "X1234567Y" },		// repetition.dat
166272343Sngie#else
167272343Sngie		{ "", "" }
168272343Sngie#endif
169272343Sngie	};
170272343Sngie
171272343Sngie	for (size_t i = 0; i < __arraycount(b); i++) {
172272343Sngie		if (strcmp(pattern, b[i].p) == 0 &&
173272343Sngie		    strcmp(input, b[i].i) == 0) {
174272343Sngie			fail(pattern, input, lineno);
175272343Sngie			return 1;
176272343Sngie		}
177272343Sngie	}
178272343Sngie	return 0;
179272343Sngie}
180272343Sngie
181272343Sngie#ifdef REGEX_SPENCER
182272343Sngie#define HAVE_BRACES	1
183272343Sngie#define HAVE_MINIMAL	0
184272343Sngie#endif
185272343Sngie#ifndef HAVE_BRACES
186272343Sngie#define HAVE_BRACES	1
187272343Sngie#endif
188272343Sngie#ifndef HAVE_MINIMAL
189272343Sngie#define HAVE_MINIMAL	1
190272343Sngie#endif
191272343Sngie
192272343Sngiestatic int
193272343Sngieoptional(const char *s)
194272343Sngie{
195272343Sngie	static const struct{
196272343Sngie		const char *n;
197272343Sngie		int v;
198272343Sngie	} nv[]= {
199272343Sngie		{ "[[<element>]] not supported", HAVE_BRACES },
200272343Sngie		{ "no *? +? mimimal match ops", HAVE_MINIMAL },
201272343Sngie	};
202272343Sngie
203272343Sngie	for (size_t i = 0; i < __arraycount(nv); i++)
204272343Sngie		if (strcmp(nv[i].n, s) == 0) {
205272343Sngie			if (nv[i].v)
206272343Sngie				return 0;
207272343Sngie			fprintf(stderr, "skipping unsupported [%s] tests\n", s);
208272343Sngie			return 1;
209272343Sngie		}
210272343Sngie
211272343Sngie	ATF_REQUIRE_MSG(0, "Unknown feature: %s", s);
212272343Sngie	return 0;
213272343Sngie}
214272343Sngie
215272343Sngiestatic int
216272343Sngieunsupported(const char *s)
217272343Sngie{
218272343Sngie	static const char *we[] = {
219272343Sngie#if defined(REGEX_SPENCER)
220272343Sngie		"ASSOCIATIVITY=left",		// have right associativity
221272343Sngie		"SUBEXPRESSION=precedence",	// have grouping subexpression
222272343Sngie		"REPEAT_LONGEST=last",		// have first repeat longest
223272343Sngie		"BUG=alternation-order",	// don't have it
224272343Sngie		"BUG=first-match",		// don't have it
225272343Sngie		"BUG=nomatch-match",		// don't have it
226272343Sngie		"BUG=repeat-any",		// don't have it
227272343Sngie		"BUG=range-null",		// don't have it
228272343Sngie		"BUG=repeat-null-unknown",	// don't have it
229272343Sngie		"BUG=repeat-null",		// don't have it
230272343Sngie		"BUG=repeat-artifact",		// don't have it
231272343Sngie		"BUG=subexpression-first",	// don't have it
232272343Sngie#elif defined(REGEX_TRE)
233272343Sngie		"ASSOCIATIVITY=right",		// have left associativity
234272343Sngie		"SUBEXPRESSION=grouping",	// have precedence subexpression
235272343Sngie		"REPEAT_LONGEST=first",		// have last repeat longest
236272343Sngie		"LENGTH=first",			// have last length
237272343Sngie		"BUG=alternation-order",	// don't have it
238272343Sngie		"BUG=first-match",		// don't have it
239272343Sngie		"BUG=range-null",		// don't have it
240272343Sngie		"BUG=repeat-null",		// don't have it
241272343Sngie		"BUG=repeat-artifact",		// don't have it
242272343Sngie		"BUG=subexpression-first",	// don't have it
243272343Sngie		"BUG=repeat-short",		// don't have it
244272343Sngie#endif
245272343Sngie	};
246272343Sngie
247272343Sngie	if (s == NULL)
248272343Sngie		return 0;
249272343Sngie
250272343Sngie	while (*s == '#' || isspace((unsigned char)*s))
251272343Sngie		s++;
252272343Sngie
253272343Sngie	for (size_t i = 0; i < __arraycount(we); i++)
254272343Sngie		if (strcmp(we[i], s) == 0)
255272343Sngie			return 1;
256272343Sngie	return 0;
257272343Sngie}
258272343Sngie
259272343Sngiestatic void
260272343Sngiegeterror(const char *s, int *comp, int *exec)
261272343Sngie{
262272343Sngie	static const struct {
263272343Sngie		const char *n;
264272343Sngie		int v;
265272343Sngie		int ce;
266272343Sngie	} nv[] = {
267272343Sngie#define COMP 1
268272343Sngie#define EXEC 2
269272343Sngie		{ "OK", 0, COMP|EXEC },
270272343Sngie#define _DO(a, b)	{ # a, REG_ ## a, b },
271272343Sngie		_DO(NOMATCH, EXEC)
272272343Sngie		_DO(BADPAT, COMP)
273272343Sngie		_DO(ECOLLATE, COMP)
274272343Sngie		_DO(ECTYPE, COMP)
275272343Sngie		_DO(EESCAPE, COMP)
276272343Sngie		_DO(ESUBREG, COMP)
277272343Sngie		_DO(EBRACK, COMP)
278272343Sngie		_DO(EPAREN, COMP)
279272343Sngie		_DO(EBRACE, COMP)
280272343Sngie		_DO(BADBR, COMP)
281272343Sngie		_DO(ERANGE, COMP)
282272343Sngie		_DO(ESPACE, EXEC)
283272343Sngie		_DO(BADRPT, COMP)
284272343Sngie		_DO(EMPTY, COMP)
285272343Sngie		_DO(ASSERT, COMP)
286272343Sngie		_DO(INVARG, COMP)
287272343Sngie		_DO(ENOSYS, COMP)
288272343Sngie#undef _DO
289272343Sngie	};
290272343Sngie	*comp = 0;
291272343Sngie	*exec = 0;
292272343Sngie	for (size_t i = 0; i < __arraycount(nv); i++)
293272343Sngie		if (strcmp(s, nv[i].n) == 0) {
294272343Sngie			if (nv[i].ce & COMP)
295272343Sngie				*comp = nv[i].v;
296272343Sngie			if (nv[i].ce & EXEC)
297272343Sngie				*exec = nv[i].v;
298272343Sngie			return;
299272343Sngie		}
300272343Sngie	ATF_REQUIRE_MSG(0, "Unknown error %s", s);
301272343Sngie	return;
302272343Sngie}
303272343Sngie
304272343Sngiestatic int
305272343Sngiegetflags(char *s)
306272343Sngie{
307272343Sngie	int flags = 0;
308272343Sngie
309272343Sngie	for (;; s++)
310272343Sngie		switch (*s) {
311272343Sngie		case '0': case '1': case '2': case '3': case '4':
312272343Sngie		case '5': case '6': case '7': case '8': case '9':
313272343Sngie			*s = '\0';
314272343Sngie			break;
315272343Sngie		case '\0':
316272343Sngie			return flags;
317272343Sngie		case 'B':
318272343Sngie		case 'E':
319272343Sngie		case 'F':
320272343Sngie		case 'L':
321272343Sngie			break;
322272343Sngie		case 'i':
323272343Sngie			flags |= REG_ICASE;
324272343Sngie			*s = '\0';
325272343Sngie			break;
326272343Sngie		case '$':
327272343Sngie			*s = '\0';
328272343Sngie			break;
329272343Sngie		case 'n':
330272343Sngie			*s = '\0';
331272343Sngie			break;
332272343Sngie		default:
333272343Sngie			ATF_REQUIRE_MSG(0, "Unknown char %c", *s);
334272343Sngie			break;
335272343Sngie		}
336272343Sngie}
337272343Sngie
338272343Sngiestatic size_t
339272343Sngiegetmatches(const char *s)
340272343Sngie{
341272343Sngie	size_t i;
342272343Sngie	char *q;
343272343Sngie	for (i = 0; (q = strchr(s, '(')) != NULL; i++, s = q + 1)
344272343Sngie		continue;
345272343Sngie	ATF_REQUIRE_MSG(i != 0, "No parentheses found");
346272343Sngie	return i;
347272343Sngie}
348272343Sngie
349272343Sngiestatic void
350272343Sngiecheckcomment(const char *s, size_t lineno)
351272343Sngie{
352272343Sngie	if (s && strstr(s, "BUG") != NULL)
353272343Sngie		fprintf(stderr, "Expected %s at line %zu\n", s, lineno);
354272343Sngie}
355272343Sngie
356272343Sngiestatic void
357272343Sngiecheckmatches(const char *matches, size_t nm, const regmatch_t *pm,
358272343Sngie    size_t lineno)
359272343Sngie{
360272343Sngie	if (nm == 0)
361272343Sngie		return;
362272343Sngie
363272343Sngie	char *res;
364272343Sngie	size_t len = strlen(matches) + 1, off = 0;
365272343Sngie
366272343Sngie	ATF_REQUIRE((res = strdup(matches)) != NULL);
367272343Sngie	for (size_t i = 0; i < nm; i++) {
368272343Sngie		int l;
369272343Sngie		if (pm[i].rm_so == -1 && pm[i].rm_eo == -1)
370272343Sngie			l = snprintf(res + off, len - off, "(?,?)");
371272343Sngie		else
372272343Sngie			l = snprintf(res + off, len - off, "(%lld,%lld)",
373272343Sngie			    (long long)pm[i].rm_so, (long long)pm[i].rm_eo);
374272343Sngie		ATF_REQUIRE_MSG((size_t) l < len - off, "String too long %s"
375272343Sngie		    " cur=%d, max=%zu", res, l, len - off);
376272343Sngie		off += l;
377272343Sngie	}
378273012Sngie	ATF_CHECK_STREQ_MSG(res, matches, " at line %zu", lineno);
379272343Sngie	free(res);
380272343Sngie}
381272343Sngie
382272343Sngiestatic void
383272343Sngieatt_test(const struct atf_tc *tc, const char *data_name)
384272343Sngie{
385272343Sngie	regex_t re;
386272343Sngie	char *line, *lastpattern = NULL, data_path[MAXPATHLEN];
387272343Sngie	size_t len, lineno = 0;
388272343Sngie	int skipping = 0;
389272343Sngie	FILE *input_file;
390272343Sngie
391272343Sngie	snprintf(data_path, sizeof(data_path), "%s/data/%s.dat",
392272343Sngie	    atf_tc_get_config_var(tc, "srcdir"), data_name);
393272343Sngie
394272343Sngie	input_file = fopen(data_path, "r");
395272343Sngie	if (input_file == NULL)
396272343Sngie		atf_tc_fail("Failed to open input file %s", data_path);
397272343Sngie
398272343Sngie	for (; (line = fparseln(input_file, &len, &lineno, delim, 0))
399272343Sngie	    != NULL; free(line)) {
400272343Sngie		char *name, *pattern, *input, *matches, *comment;
401272343Sngie		regmatch_t *pm;
402272343Sngie		size_t nm;
403272343Sngie#ifdef DEBUG
404272343Sngie		fprintf(stderr, "[%s]\n", line);
405272343Sngie#endif
406272343Sngie		if ((name = strtok(line, sep)) == NULL)
407272343Sngie			continue;
408272343Sngie
409272343Sngie		/*
410272343Sngie		 * We check these early so that we skip the lines quickly
411272343Sngie		 * in order to do more strict testing on the other arguments
412272343Sngie		 * The same characters are also tested in the switch below
413272343Sngie		 */
414272343Sngie		if (*name == '}') {
415272343Sngie			skipping = 0;
416272343Sngie			continue;
417272343Sngie		}
418272343Sngie		if (skipping)
419272343Sngie			continue;
420272343Sngie		if (*name == ';' || *name == '#' || strcmp(name, "NOTE") == 0)
421272343Sngie			continue;
422272343Sngie		if (*name == ':') {
423272343Sngie			/* Skip ":HA#???:" prefix */
424272343Sngie			while (*++name && *name != ':')
425272343Sngie				continue;
426272343Sngie			if (*name)
427272343Sngie				name++;
428272343Sngie		}
429272343Sngie
430272343Sngie		ATF_REQUIRE_MSG((pattern = strtok(NULL, sep)) != NULL,
431272343Sngie			"Missing pattern at line %zu", lineno);
432272343Sngie		ATF_REQUIRE_MSG((input = strtok(NULL, sep)) != NULL,
433272343Sngie			"Missing input at line %zu", lineno);
434272343Sngie
435272343Sngie		if (strchr(name, '$')) {
436272343Sngie			ATF_REQUIRE(strunvis(pattern, pattern) != -1);
437272343Sngie			ATF_REQUIRE(strunvis(input, input) != -1);
438272343Sngie		}
439272343Sngie
440272343Sngie
441272343Sngie		if (strcmp(input, "NULL") == 0)
442272343Sngie			*input = '\0';
443272343Sngie
444272343Sngie		if (strcmp(pattern, "SAME") == 0) {
445272343Sngie			ATF_REQUIRE(lastpattern != NULL);
446272343Sngie			pattern = lastpattern;
447272343Sngie		} else {
448272343Sngie			free(lastpattern);
449272343Sngie			ATF_REQUIRE((lastpattern = strdup(pattern)) != NULL);
450272343Sngie		}
451272343Sngie
452272343Sngie		ATF_REQUIRE_MSG((matches = strtok(NULL, sep)) != NULL,
453272343Sngie		    "Missing matches at line %zu", lineno);
454272343Sngie
455272343Sngie		comment = strtok(NULL, sep);
456272343Sngie		switch (*name) {
457272343Sngie		case '{':	/* Begin optional implementation */
458272343Sngie			if (optional(comment)) {
459272343Sngie				skipping++;
460272343Sngie				continue;
461272343Sngie			}
462272343Sngie			name++;	/* We have it, so ignore */
463272343Sngie			break;
464272343Sngie		case '}':	/* End optional implementation */
465272343Sngie			skipping = 0;
466272343Sngie			continue;
467272343Sngie		case '?':	/* Optional */
468272343Sngie		case '|':	/* Alternative */
469272343Sngie			if (unsupported(comment))
470272343Sngie				continue;
471272343Sngie			name++;	/* We have it, so ignore */
472272343Sngie			break;
473272343Sngie		case '#':	/* Comment */
474272343Sngie		case ';':	/* Skip */
475272343Sngie			continue;
476272343Sngie		default:
477272343Sngie			break;
478272343Sngie		}
479272343Sngie
480272343Sngie		/* XXX: Our bug */
481272343Sngie		if (bug(pattern, input, lineno))
482272343Sngie			continue;
483272343Sngie
484272343Sngie		int comp, exec;
485272343Sngie		if (*matches != '(') {
486272343Sngie			geterror(matches, &comp, &exec);
487272343Sngie			pm = NULL;
488272343Sngie			nm = 0;
489272343Sngie		} else {
490272343Sngie			comp = exec = 0;
491272343Sngie			nm = getmatches(matches);
492272343Sngie			ATF_REQUIRE((pm = calloc(nm, sizeof(*pm))) != NULL);
493272343Sngie		}
494272343Sngie
495272343Sngie
496272343Sngie
497272343Sngie		int iflags = getflags(name);
498272343Sngie		for (; *name; name++) {
499272343Sngie			int flags;
500272343Sngie			switch (*name) {
501272343Sngie			case 'B':
502272343Sngie				flags = REG_BASIC;
503272343Sngie				break;
504272343Sngie			case 'E':
505272343Sngie				flags = REG_EXTENDED;
506272343Sngie				break;
507272343Sngie			case 'L':
508272343Sngie				flags = REG_NOSPEC;
509272343Sngie				break;
510272343Sngie			default:
511272343Sngie				ATF_REQUIRE_MSG(0, "Bad name %c", *name);
512272343Sngie				continue;
513272343Sngie			}
514272343Sngie			int c = regcomp(&re, pattern, flags | iflags);
515272343Sngie			ATF_REQUIRE_MSG(c == comp,
516272343Sngie			    "regcomp returned %d for pattern %s at line %zu",
517272343Sngie			    c, pattern, lineno);
518272343Sngie			if (c)
519272343Sngie				continue;
520272343Sngie			int e = regexec(&re, input, nm, pm, 0);
521272343Sngie			ATF_REQUIRE_MSG(e == exec, "Expected error %d,"
522272343Sngie			    " got %d at line %zu", exec, e, lineno);
523272343Sngie			checkmatches(matches, nm, pm, lineno);
524272343Sngie			checkcomment(comment, lineno);
525272343Sngie			regfree(&re);
526272343Sngie		}
527272343Sngie		free(pm);
528272343Sngie	}
529272343Sngie
530272343Sngie	fclose(input_file);
531272343Sngie}
532272343Sngie
533272343SngieATF_TC(basic);
534272343SngieATF_TC_HEAD(basic, tc)
535272343Sngie{
536272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests basic functionality");
537272343Sngie}
538272343SngieATF_TC_BODY(basic, tc)
539272343Sngie{
540272343Sngie	att_test(tc, "basic");
541272343Sngie}
542272343Sngie
543272343SngieATF_TC(categorization);
544272343SngieATF_TC_HEAD(categorization, tc)
545272343Sngie{
546272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests implementation categorization");
547272343Sngie}
548272343SngieATF_TC_BODY(categorization, tc)
549272343Sngie{
550272343Sngie	att_test(tc, "categorization");
551272343Sngie}
552272343Sngie
553272343SngieATF_TC(nullsubexpr);
554272343SngieATF_TC_HEAD(nullsubexpr, tc)
555272343Sngie{
556272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests (...)*");
557272343Sngie}
558272343SngieATF_TC_BODY(nullsubexpr, tc)
559272343Sngie{
560272343Sngie	att_test(tc, "nullsubexpr");
561272343Sngie}
562272343Sngie
563272343SngieATF_TC(leftassoc);
564272343SngieATF_TC_HEAD(leftassoc, tc)
565272343Sngie{
566272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests left-associative "
567272343Sngie	    "implementations");
568272343Sngie}
569272343SngieATF_TC_BODY(leftassoc, tc)
570272343Sngie{
571272343Sngie#if SKIP_LEFTASSOC
572272343Sngie	/* jmmv: I converted the original shell-based tests to C and they
573272343Sngie	 * disabled this test in a very unconventional way without giving
574272343Sngie	 * any explation.  Mark as broken here, but I don't know why. */
575272343Sngie	atf_tc_expect_fail("Reason for breakage unknown");
576272343Sngie#endif
577272343Sngie	att_test(tc, "leftassoc");
578272343Sngie}
579272343Sngie
580272343SngieATF_TC(rightassoc);
581272343SngieATF_TC_HEAD(rightassoc, tc)
582272343Sngie{
583272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests right-associative "
584272343Sngie	    "implementations");
585272343Sngie}
586272343SngieATF_TC_BODY(rightassoc, tc)
587272343Sngie{
588272343Sngie#if SKIP_RIGHTASSOC
589272343Sngie	/* jmmv: I converted the original shell-based tests to C and they
590272343Sngie	 * disabled this test in a very unconventional way without giving
591272343Sngie	 * any explation.  Mark as broken here, but I don't know why. */
592272343Sngie	atf_tc_expect_fail("Reason for breakage unknown");
593272343Sngie#endif
594272343Sngie	att_test(tc, "rightassoc");
595272343Sngie}
596272343Sngie
597272343SngieATF_TC(forcedassoc);
598272343SngieATF_TC_HEAD(forcedassoc, tc)
599272343Sngie{
600272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests subexpression grouping to "
601272343Sngie	    "force association");
602272343Sngie}
603272343SngieATF_TC_BODY(forcedassoc, tc)
604272343Sngie{
605272343Sngie	att_test(tc, "forcedassoc");
606272343Sngie}
607272343Sngie
608272343SngieATF_TC(repetition);
609272343SngieATF_TC_HEAD(repetition, tc)
610272343Sngie{
611272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests implicit vs. explicit "
612272343Sngie	    "repetition");
613272343Sngie}
614272343SngieATF_TC_BODY(repetition, tc)
615272343Sngie{
616272343Sngie	att_test(tc, "repetition");
617272343Sngie}
618272343Sngie
619272343SngieATF_TP_ADD_TCS(tp)
620272343Sngie{
621272343Sngie
622272343Sngie	ATF_TP_ADD_TC(tp, basic);
623272343Sngie	ATF_TP_ADD_TC(tp, categorization);
624272343Sngie	ATF_TP_ADD_TC(tp, nullsubexpr);
625286638Sbapt	ATF_TP_ADD_TC(tp, leftassoc);
626272343Sngie	ATF_TP_ADD_TC(tp, rightassoc);
627272343Sngie	ATF_TP_ADD_TC(tp, forcedassoc);
628272343Sngie	ATF_TP_ADD_TC(tp, repetition);
629272343Sngie	return atf_no_error();
630272343Sngie}
631