2 * Helper to verify the output rendering routines.
3 * Copyright © 2023-2024 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/>.
28 #define PROGNAME "rendertest"
32 static const char sopts[] = "n:ECVH";
33 static const struct option lopts[] = {
34 { "count", 1, NULL, 'n' },
35 { "cdecl", 0, NULL, 'C' },
36 { "english", 0, NULL, 'E' },
37 { "version", 0, NULL, 'V' },
38 { "help", 0, NULL, 'H' },
42 static void print_usage(FILE *f)
44 fprintf(f, "Usage: %s [options]\n", progname);
47 static void print_help(void)
50 puts("Helper application to test output rendering.\n");
51 test_print_options(lopts);
54 enum { MODE_CDECL, MODE_ENGLISH };
56 static int do_test(char *line, unsigned long n, int mode)
62 decl = cdecl_parse_english(line);
64 decl = cdecl_parse_decl(line);
67 print_error("%s", cdecl_get_error()->str);
68 print_error("the failed input was: %s", line);
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 print_error("output is not 0-terminated");
83 if (line[n] != '\a') {
84 print_error("output overflow");
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 (!test_strtoul(&n, optarg) || n > (size_t)-2) {
113 print_error("invalid count: %s", optarg);
118 test_print_version(PROGNAME);
130 * Ensure the preallocated buffer is more than one byte, otherwise we
131 * will hit a bug in AIX 7.2 getline and fall into an infinite loop.
133 line = malloc_nofail((sz = MAX(n+1, 10)));
134 while (do_getline(&line, &sz)) {
135 if (do_test(line, n, mode) < 0)