/* * Helper to verify the output rendering routines. * Copyright © 2023-2024 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include "cdecl.h" #define PROGNAME "rendertest" #include "test.h" static const char sopts[] = "n:ECVH"; static const struct option lopts[] = { { "count", 1, NULL, 'n' }, { "cdecl", 0, NULL, 'C' }, { "english", 0, NULL, 'E' }, { "version", 0, NULL, 'V' }, { "help", 0, NULL, 'H' }, { 0 } }; static void print_usage(FILE *f) { fprintf(f, "Usage: %s [options]\n", progname); } static void print_help(void) { print_usage(stdout); puts("Helper application to test output rendering.\n"); test_print_options(lopts); } enum { MODE_CDECL, MODE_ENGLISH }; static int do_test(char *line, unsigned long n, int mode) { struct cdecl *decl; size_t rc; if (mode) decl = cdecl_parse_english(line); else decl = cdecl_parse_decl(line); if (!decl) { print_error("%s", cdecl_get_error()->str); print_error("the failed input was: %s", line); return -1; } memset(line, '\a', n+1); if (mode) rc = cdecl_explain(line, n, decl); else rc = cdecl_declare(line, n, decl); if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') { print_error("output is not 0-terminated"); return -1; } if (line[n] != '\a') { print_error("output overflow"); return -1; } printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line); cdecl_free(decl); return 0; } int main(int argc, char **argv) { int opt, mode = MODE_CDECL, ret = EXIT_SUCCESS; unsigned long n = 60; char *line; size_t sz; if (argc > 0) progname = argv[0]; while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { switch (opt) { case 'C': mode = MODE_CDECL; break; case 'E': mode = MODE_ENGLISH; break; case 'n': if (!test_strtoul(&n, optarg) || n > (size_t)-2) { print_error("invalid count: %s", optarg); return EXIT_FAILURE; } break; case 'V': test_print_version(PROGNAME); return EXIT_SUCCESS; case 'H': print_help(); return EXIT_SUCCESS; default: print_usage(stderr); return EXIT_FAILURE; } } /* * Ensure the preallocated buffer is more than one byte, otherwise we * will hit a bug in AIX 7.2 getline and fall into an infinite loop. */ line = malloc_nofail((sz = MAX(n+1, 10))); while (test_getline(&line, &sz)) { if (do_test(line, n, mode) < 0) ret = EXIT_FAILURE; } free(line); return ret; }