/*
* Helper to verify the output rendering routines.
* Copyright © 2023 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
#include "test.h"
#define PROGNAME "rendertest"
static const char *progname = PROGNAME;
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) {
fprintf(stderr, "%s: %s\n", progname, cdecl_get_error()->str);
fprintf(stderr, "%s: the failed input was: %s\n",
progname, 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') {
fprintf(stderr, "%s: output is not 0-terminated\n", progname);
return -1;
}
if (line[n] != '\a') {
fprintf(stderr, "%s: output overflow\n", progname);
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 (!strict_strtoul(&n, optarg, 0) || n > (size_t)-2) {
fprintf(stderr, "%s: invalid count: %s\n",
progname, 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;
}
}
line = malloc_nofail((sz = n+1));
while (getline(&line, &sz, stdin) >= 0) {
char *c = strchr(line, '\n');
if (c)
*c = '\0';
if (do_test(line, n, mode) < 0)
ret = EXIT_FAILURE;
}
free(line);
return ret;
}