X-Git-Url: http://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/93f0fff49d45d0cb36d7f7d9109c4fb56e6ebeab..4ef59e97312cf3e8f537643c65bc2fef057b786b:/t/crossparse.c diff --git a/t/crossparse.c b/t/crossparse.c index 520b08a..5f7eddc 100644 --- a/t/crossparse.c +++ b/t/crossparse.c @@ -1,6 +1,6 @@ /* * Test that libcdecl can parse its own output. - * Copyright © 2012, 2020, 2022-2023 Nick Bowler + * Copyright © 2012, 2020, 2022-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 @@ -23,11 +23,13 @@ #include #include #include -#include -#include "test.h" + +#include "cdecl.h" #define PROGNAME "crossparse" -static const char *progname = PROGNAME; +#include "test.h" +#include "getline.h" + static const char sopts[] = "f:ECVH"; static const struct option lopts[] = { { "cdecl", 0, NULL, 'C' }, @@ -45,8 +47,6 @@ static void print_usage(FILE *f) static void print_help(void) { - const struct option *opt; - print_usage(stdout); puts("Test that libcdecl can parse its own output.\n"); test_print_options(lopts); @@ -54,7 +54,7 @@ static void print_help(void) enum { MODE_CDECL, - MODE_ENGLISH, + MODE_ENGLISH }; typedef struct cdecl *parse_func(const char *); @@ -69,16 +69,15 @@ char *rerender(const char *str, const char *parse_name, parse_func *parse, decl = parse(str); if (!decl) { - fprintf(stderr, "%s: %s: failed to parse input: %s\n", - progname, parse_name, cdecl_get_error()->str); + print_error("%s: failed to parse input: %s", parse_name, + cdecl_get_error()->str); goto err; } len = render(NULL, 0, decl); buf = malloc_nofail(len+1); if (render(buf, len+1, decl) != len) { - fprintf(stderr, "%s: %s: inconsistent length returned\n", - progname, render_name); + print_error("%s: inconsistent length returned", render_name); goto err; } @@ -88,7 +87,7 @@ err: cdecl_free(decl); free(buf); - fprintf(stderr, "%s: the failed input was: %s\n", progname, str); + print_error("the failed input was: %s", str); return NULL; } #define rerender(str, p, r) (rerender(str, #p, p, #r, r)) @@ -128,8 +127,7 @@ out: free(buf3); if (!ret) { - fprintf(stderr, "%s: failed cross-parse check of: %s\n", - progname, str); + print_error("failed cross-parse check of: %s", str); } return ret; @@ -139,9 +137,9 @@ int main(int argc, char **argv) { int opt, mode = MODE_CDECL; int ret = EXIT_SUCCESS; + int i; const char *filename = NULL; - FILE *infile = NULL; if (argc > 0) progname = argv[0]; @@ -155,7 +153,6 @@ int main(int argc, char **argv) mode = MODE_ENGLISH; break; case 'f': - infile = stdin; filename = optarg; break; case 'V': @@ -170,31 +167,21 @@ int main(int argc, char **argv) } } - if (infile) { + if (filename && !freopen(filename, "r", stdin)) { + print_error("%s: %s", filename, strerror(errno)); + return EXIT_FAILURE; + } else if (filename) { char *line = NULL; - size_t n; - - if (filename) { - infile = fopen(filename, "r"); - if (!infile) { - fprintf(stderr, "%s: %s: %s\n", progname, - filename, strerror(errno)); - return EXIT_FAILURE; - } - } + size_t n = 0; - while (getline(&line, &n, infile) >= 0) { - char *c = strchr(line, '\n'); - if (c) - *c = '\0'; + while (do_getline(&line, &n)) { if (!test_crossparse(line, mode)) ret = EXIT_FAILURE; } free(line); - fclose(infile); } else if (argv[optind]) { - for (int i = optind; i < argc; i++) { + for (i = optind; i < argc; i++) { if (!test_crossparse(argv[i], mode)) ret = EXIT_FAILURE; }