2 * Helper to verify the output rendering routines.
3 * Copyright © 2023 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 #define PROGNAME "rendertest"
30 static const char *progname = PROGNAME;
31 static const char sopts[] = "n:ECVH";
32 static const struct option lopts[] = {
33 { "count", 1, NULL, 'n' },
34 { "cdecl", 0, NULL, 'C' },
35 { "english", 0, NULL, 'E' },
36 { "version", 0, NULL, 'V' },
37 { "help", 0, NULL, 'H' },
41 static void print_usage(FILE *f)
43 fprintf(f, "Usage: %s [options]\n", progname);
46 static void print_help(void)
49 puts("Helper application to test output rendering.\n");
50 test_print_options(lopts);
53 enum { MODE_CDECL, MODE_ENGLISH };
55 static int do_test(char *line, unsigned long n, int mode)
61 decl = cdecl_parse_english(line);
63 decl = cdecl_parse_decl(line);
66 fprintf(stderr, "%s: %s\n", progname, cdecl_get_error()->str);
67 fprintf(stderr, "%s: the failed input was: %s\n",
72 memset(line, '\a', n+1);
74 rc = cdecl_explain(line, n, decl);
76 rc = cdecl_declare(line, n, decl);
78 if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') {
79 fprintf(stderr, "%s: output is not 0-terminated\n", progname);
83 if (line[n] != '\a') {
84 fprintf(stderr, "%s: output overflow\n", progname);
88 printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line);
93 int main(int argc, char **argv)
95 int opt, mode = MODE_CDECL, ret = EXIT_SUCCESS;
103 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
112 if (!strict_strtoul(&n, optarg, 0) || n > (size_t)-2) {
113 fprintf(stderr, "%s: invalid count: %s\n",
119 test_print_version(PROGNAME);
130 line = malloc_nofail((sz = n+1));
131 while (getline(&line, &sz, stdin) >= 0) {
132 char *c = strchr(line, '\n');
136 if (do_test(line, n, mode) < 0)