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