xref: /freebsd/lib/libc/tests/gen/test-fnmatch.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*79346959SEnji Cooper /*-
2*79346959SEnji Cooper  * Copyright (c) 2010 Jilles Tjoelker
3*79346959SEnji Cooper  * All rights reserved.
4*79346959SEnji Cooper  *
5*79346959SEnji Cooper  * Redistribution and use in source and binary forms, with or without
6*79346959SEnji Cooper  * modification, are permitted provided that the following conditions
7*79346959SEnji Cooper  * are met:
8*79346959SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
9*79346959SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
10*79346959SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
11*79346959SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
12*79346959SEnji Cooper  *    documentation and/or other materials provided with the distribution.
13*79346959SEnji Cooper  *
14*79346959SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*79346959SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*79346959SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*79346959SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*79346959SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*79346959SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*79346959SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*79346959SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*79346959SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*79346959SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*79346959SEnji Cooper  * SUCH DAMAGE.
25*79346959SEnji Cooper  */
26fbf5b9f8SEnji Cooper 
27fbf5b9f8SEnji Cooper #include <sys/param.h>
28fbf5b9f8SEnji Cooper #include <stdio.h>
29fbf5b9f8SEnji Cooper #include <stdlib.h>
30fbf5b9f8SEnji Cooper #include <string.h>
31fbf5b9f8SEnji Cooper #include <unistd.h>
32fbf5b9f8SEnji Cooper 
33fbf5b9f8SEnji Cooper #include "fnmatch_testcases.h"
34fbf5b9f8SEnji Cooper 
35fbf5b9f8SEnji Cooper static int
write_sh_tests(const char * progname,int num)36fbf5b9f8SEnji Cooper write_sh_tests(const char *progname, int num)
37fbf5b9f8SEnji Cooper {
38fbf5b9f8SEnji Cooper 	size_t i;
39fbf5b9f8SEnji Cooper 	struct testcase *t;
40fbf5b9f8SEnji Cooper 
41fbf5b9f8SEnji Cooper 	printf("# Generated by %s -s %d, do not edit.\n", progname, num);
42fbf5b9f8SEnji Cooper 	printf("# $" "FreeBSD$\n");
43fbf5b9f8SEnji Cooper 	printf("failures=\n");
44fbf5b9f8SEnji Cooper 	printf("failed() { printf '%%s\\n' \"Failed: $1 '$2' '$3'\"; failures=x$failures; }\n");
45fbf5b9f8SEnji Cooper 	if (num == 1) {
46fbf5b9f8SEnji Cooper 		printf("testmatch() { eval \"case \\$2 in ''$1) ;; *) failed testmatch \\\"\\$@\\\";; esac\"; }\n");
47fbf5b9f8SEnji Cooper 		printf("testnomatch() { eval \"case \\$2 in ''$1) failed testnomatch \\\"\\$@\\\";; esac\"; }\n");
48fbf5b9f8SEnji Cooper 	} else if (num == 2) {
49fbf5b9f8SEnji Cooper 		printf("# We do not treat a backslash specially in this case,\n");
50fbf5b9f8SEnji Cooper 		printf("# but this is not the case in all shells.\n");
51fbf5b9f8SEnji Cooper 		printf("netestmatch() { case $2 in $1) ;; *) failed netestmatch \"$@\";; esac; }\n");
52fbf5b9f8SEnji Cooper 		printf("netestnomatch() { case $2 in $1) failed netestnomatch \"$@\";; esac; }\n");
53fbf5b9f8SEnji Cooper 	}
54fbf5b9f8SEnji Cooper 
55fbf5b9f8SEnji Cooper 	for (i = 0; i < nitems(testcases); i++) {
56fbf5b9f8SEnji Cooper 		t = &testcases[i];
57fbf5b9f8SEnji Cooper 		if (strchr(t->pattern, '\'') != NULL ||
58fbf5b9f8SEnji Cooper 		    strchr(t->string, '\'') != NULL)
59fbf5b9f8SEnji Cooper 			continue;
60fbf5b9f8SEnji Cooper 		if (t->flags == 0 && strcmp(t->pattern, "\\") == 0)
61fbf5b9f8SEnji Cooper 			continue;
62fbf5b9f8SEnji Cooper 		if (num == 1 && t->flags == 0)
63fbf5b9f8SEnji Cooper 			printf("test%smatch '%s' '%s'\n",
64fbf5b9f8SEnji Cooper 			    t->result == FNM_NOMATCH ? "no" : "",
65fbf5b9f8SEnji Cooper 			    t->pattern, t->string);
66fbf5b9f8SEnji Cooper 		if (num == 2 && (t->flags == FNM_NOESCAPE ||
67fbf5b9f8SEnji Cooper 		    (t->flags == 0 && strchr(t->pattern, '\\') == NULL)))
68fbf5b9f8SEnji Cooper 			printf("netest%smatch '%s' '%s'\n",
69fbf5b9f8SEnji Cooper 			    t->result == FNM_NOMATCH ? "no" : "",
70fbf5b9f8SEnji Cooper 			    t->pattern, t->string);
71fbf5b9f8SEnji Cooper 	}
72fbf5b9f8SEnji Cooper 	printf("[ -z \"$failures\" ]\n");
73fbf5b9f8SEnji Cooper 	return 0;
74fbf5b9f8SEnji Cooper }
75fbf5b9f8SEnji Cooper 
76fbf5b9f8SEnji Cooper static void
usage(char * progname)77fbf5b9f8SEnji Cooper usage(char *progname)
78fbf5b9f8SEnji Cooper {
79fbf5b9f8SEnji Cooper 	fprintf(stderr, "usage: %s [-s num]\n", progname);
80fbf5b9f8SEnji Cooper 	fprintf(stderr, "-s option writes tests for sh(1), num is 1 or 2\n");
81fbf5b9f8SEnji Cooper }
82fbf5b9f8SEnji Cooper 
83fbf5b9f8SEnji Cooper int
main(int argc,char * argv[])84fbf5b9f8SEnji Cooper main(int argc, char *argv[])
85fbf5b9f8SEnji Cooper {
86fbf5b9f8SEnji Cooper 	int opt;
87fbf5b9f8SEnji Cooper 
88fbf5b9f8SEnji Cooper 	while ((opt = getopt(argc, argv, "s:")) != -1) {
89fbf5b9f8SEnji Cooper 		switch (opt) {
90fbf5b9f8SEnji Cooper 		case 's':
91fbf5b9f8SEnji Cooper 			return (write_sh_tests(argv[0], atoi(optarg)));
92fbf5b9f8SEnji Cooper 		default:
93fbf5b9f8SEnji Cooper 			usage(argv[0]);
94fbf5b9f8SEnji Cooper 			exit(1);
95fbf5b9f8SEnji Cooper 		}
96fbf5b9f8SEnji Cooper 	}
97fbf5b9f8SEnji Cooper 	usage(argv[0]);
98fbf5b9f8SEnji Cooper 	exit(1);
99fbf5b9f8SEnji Cooper }
100