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