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