2 * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
28 #endif /* STDC_HEADERS */
31 #endif /* HAVE_STRING_H */
34 #endif /* HAVE_STRINGS_H */
57 * In "normal" fill, anything can be escaped and hex chars are expanded.
59 static struct fill_test txt_data[] = {
60 { "Embedded\\x20Space", "Embedded Space", 0 },
61 { "\\x20Leading", " Leading", 0 },
62 { "Trailing\\x20", "Trailing ", 0 },
63 { "Multiple\\x20\\x20Spaces", "Multiple Spaces", 0 },
64 { "Hexparse\\x200Check", "Hexparse 0Check", 0 },
65 { "Escaped\\\\Escape", "Escaped\\Escape", 0 },
66 { "LongGroupName", "LongGrou", 8 }
70 * The only escaped chars in a command should be [,:= \t#]
71 * The rest are done by glob() or fnmatch().
73 static struct fill_test cmd_data[] = {
74 { "foo\\,bar", "foo,bar", 0 },
75 { "this\\:that", "this:that", 0 },
76 { "foo\\=bar", "foo=bar", 0 },
77 { "tab\\\tstop", "tab\tstop", 0 },
78 { "not a \\#comment", "not a #comment", 0 }
82 * No escaped characters in command line args.
83 * Arguments get appended.
85 static struct fill_test args_data[] = {
87 { "-type", "/ -type", 0, 1 },
88 { "f", "/ -type f", 0, 1 },
89 { "-exec", "/ -type f -exec", 0, 1 },
90 { "ls", "/ -type f -exec ls", 0, 1 },
91 { "{}", "/ -type f -exec ls {}", 0, 1 }
95 check_fill(const char *input, int len, int addspace, const char *expect, char **resultp)
97 if (!fill(input, len))
99 *resultp = yylval.string;
100 return !strcmp(yylval.string, expect);
104 check_fill_cmnd(const char *input, int len, int addspace, const char *expect, char **resultp)
106 if (!fill_cmnd(input, len))
108 *resultp = yylval.command.cmnd;
109 return !strcmp(yylval.command.cmnd, expect);
113 check_fill_args(const char *input, int len, int addspace, const char *expect, char **resultp)
115 if (!fill_args(input, len, addspace))
117 *resultp = yylval.command.args;
118 return !strcmp(yylval.command.args, expect);
122 do_tests(int (*checker)(const char *, int, int, const char *, char **),
123 struct fill_test *data, size_t ntests)
129 for (i = 0; i < ntests; i++) {
130 if (data[i].len == 0)
131 len = strlen(data[i].input);
135 switch ((*checker)(data[i].input, len, data[i].addspace, data[i].output, &result)) {
138 fprintf(stderr, "Failed parsing %.*s: expected [%s], got [%s]\n",
139 (int)data[i].len, data[i].input, data[i].output, result);
147 fprintf(stderr, "Failed parsing %.*s: fill function failure\n",
148 (int)data[i].len, data[i].input);
158 main(int argc, char *argv[])
160 int ntests, errors = 0;
162 errors += do_tests(check_fill, txt_data, sizeof(txt_data) / sizeof(txt_data[0]));
163 errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0]));
164 errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0]));
166 ntests = sizeof(txt_data) / sizeof(txt_data[0]) +
167 sizeof(cmd_data) / sizeof(cmd_data[0]) +
168 sizeof(args_data) / sizeof(args_data[0]);
169 printf("check_fill: %d tests run, %d errors, %d%% success rate\n",
170 ntests, errors, (ntests - errors) * 100 / ntests);
184 yyerror(const char *s)