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"
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 print_error("%s", cdecl_get_error()->str);
67 print_error("the failed input was: %s", line);
71 memset(line, '\a', n+1);
73 rc = cdecl_explain(line, n, decl);
75 rc = cdecl_declare(line, n, decl);
77 if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') {
78 print_error("output is not 0-terminated");
82 if (line[n] != '\a') {
83 print_error("output overflow");
87 printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line);
92 int main(int argc, char **argv)
94 int opt, mode = MODE_CDECL, ret = EXIT_SUCCESS;
102 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
111 if (!test_strtoul(&n, optarg) || n > (size_t)-2) {
112 print_error("invalid count: %s", optarg);
117 test_print_version(PROGNAME);
129 * Ensure the preallocated buffer is more than one byte, otherwise we
130 * will hit a bug in AIX 7.2 getline and fall into an infinite loop.
132 line = malloc_nofail((sz = MAX(n+1, 10)));
133 while (test_getline(&line, &sz)) {
134 if (do_test(line, n, mode) < 0)