1290646Sngie/*-
2290646Sngie * Copyright (c) 2010 Jilles Tjoelker
3290646Sngie * All rights reserved.
4290646Sngie *
5290646Sngie * Redistribution and use in source and binary forms, with or without
6290646Sngie * modification, are permitted provided that the following conditions
7290646Sngie * are met:
8290646Sngie * 1. Redistributions of source code must retain the above copyright
9290646Sngie *    notice, this list of conditions and the following disclaimer.
10290646Sngie * 2. Redistributions in binary form must reproduce the above copyright
11290646Sngie *    notice, this list of conditions and the following disclaimer in the
12290646Sngie *    documentation and/or other materials provided with the distribution.
13290646Sngie *
14290646Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15290646Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16290646Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17290646Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18290646Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19290646Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20290646Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21290646Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22290646Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23290646Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24290646Sngie * SUCH DAMAGE.
25290646Sngie */
26290572Sngie
27290572Sngie#include <sys/cdefs.h>
28290572Sngie__FBSDID("$FreeBSD$");
29290572Sngie
30290572Sngie#include <sys/param.h>
31290572Sngie#include <stdio.h>
32290572Sngie#include <stdlib.h>
33290572Sngie#include <string.h>
34290572Sngie#include <unistd.h>
35290572Sngie
36290572Sngie#include "fnmatch_testcases.h"
37290572Sngie
38290572Sngiestatic int
39290572Sngiewrite_sh_tests(const char *progname, int num)
40290572Sngie{
41290572Sngie	size_t i;
42290572Sngie	struct testcase *t;
43290572Sngie
44290572Sngie	printf("# Generated by %s -s %d, do not edit.\n", progname, num);
45290572Sngie	printf("# $" "FreeBSD$\n");
46290572Sngie	printf("failures=\n");
47290572Sngie	printf("failed() { printf '%%s\\n' \"Failed: $1 '$2' '$3'\"; failures=x$failures; }\n");
48290572Sngie	if (num == 1) {
49290572Sngie		printf("testmatch() { eval \"case \\$2 in ''$1) ;; *) failed testmatch \\\"\\$@\\\";; esac\"; }\n");
50290572Sngie		printf("testnomatch() { eval \"case \\$2 in ''$1) failed testnomatch \\\"\\$@\\\";; esac\"; }\n");
51290572Sngie	} else if (num == 2) {
52290572Sngie		printf("# We do not treat a backslash specially in this case,\n");
53290572Sngie		printf("# but this is not the case in all shells.\n");
54290572Sngie		printf("netestmatch() { case $2 in $1) ;; *) failed netestmatch \"$@\";; esac; }\n");
55290572Sngie		printf("netestnomatch() { case $2 in $1) failed netestnomatch \"$@\";; esac; }\n");
56290572Sngie	}
57290572Sngie
58290572Sngie	for (i = 0; i < nitems(testcases); i++) {
59290572Sngie		t = &testcases[i];
60290572Sngie		if (strchr(t->pattern, '\'') != NULL ||
61290572Sngie		    strchr(t->string, '\'') != NULL)
62290572Sngie			continue;
63290572Sngie		if (t->flags == 0 && strcmp(t->pattern, "\\") == 0)
64290572Sngie			continue;
65290572Sngie		if (num == 1 && t->flags == 0)
66290572Sngie			printf("test%smatch '%s' '%s'\n",
67290572Sngie			    t->result == FNM_NOMATCH ? "no" : "",
68290572Sngie			    t->pattern, t->string);
69290572Sngie		if (num == 2 && (t->flags == FNM_NOESCAPE ||
70290572Sngie		    (t->flags == 0 && strchr(t->pattern, '\\') == NULL)))
71290572Sngie			printf("netest%smatch '%s' '%s'\n",
72290572Sngie			    t->result == FNM_NOMATCH ? "no" : "",
73290572Sngie			    t->pattern, t->string);
74290572Sngie	}
75290572Sngie	printf("[ -z \"$failures\" ]\n");
76290572Sngie	return 0;
77290572Sngie}
78290572Sngie
79290572Sngiestatic void
80290572Sngieusage(char *progname)
81290572Sngie{
82290572Sngie	fprintf(stderr, "usage: %s [-s num]\n", progname);
83290572Sngie	fprintf(stderr, "-s option writes tests for sh(1), num is 1 or 2\n");
84290572Sngie}
85290572Sngie
86290572Sngieint
87290572Sngiemain(int argc, char *argv[])
88290572Sngie{
89290572Sngie	int opt;
90290572Sngie
91290572Sngie	while ((opt = getopt(argc, argv, "s:")) != -1) {
92290572Sngie		switch (opt) {
93290572Sngie		case 's':
94290572Sngie			return (write_sh_tests(argv[0], atoi(optarg)));
95290572Sngie		default:
96290572Sngie			usage(argv[0]);
97290572Sngie			exit(1);
98290572Sngie		}
99290572Sngie	}
100290572Sngie	usage(argv[0]);
101290572Sngie	exit(1);
102290572Sngie}
103