2 * Test that libcdecl can parse its own output.
3 * Copyright © 2012, 2020 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/>.
29 #define PROGNAME "crossparse"
30 static const char *progname = PROGNAME;
31 static const char sopts[] = "f:ECVH";
32 static const struct option lopts[] = {
33 { "cdecl", 0, NULL, 'C' },
34 { "english", 0, NULL, 'E' },
35 { "file", 2, NULL, 'f' },
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)
48 const struct option *opt;
51 puts("Test that libcdecl can parse its own output.\n");
52 test_print_options(lopts);
60 typedef struct cdecl *parse_func(const char *);
61 typedef size_t render_func(char *, size_t, struct cdecl *);
63 char *rerender(const char *str, const char *parse_name, parse_func *parse,
64 const char *render_name, render_func *render)
66 struct cdecl *decl = NULL;
72 fprintf(stderr, "%s: %s: failed to parse input: %s\n",
73 progname, parse_name, cdecl_get_error()->str);
77 len = render(NULL, 0, decl);
78 buf = malloc_nofail(len+1);
79 if (render(buf, len+1, decl) != len) {
80 fprintf(stderr, "%s: %s: inconsistent length returned\n",
81 progname, render_name);
91 fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
94 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
96 static bool test_crossparse(const char *str, int mode)
98 char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
101 if (mode == MODE_ENGLISH) {
102 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
105 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
108 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
112 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
115 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
118 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
123 if (!strcmp(buf1, buf3))
131 fprintf(stderr, "%s: failed cross-parse check of: %s\n",
138 int main(int argc, char **argv)
140 int opt, mode = MODE_CDECL;
141 int ret = EXIT_SUCCESS;
143 const char *filename = NULL;
149 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
162 test_print_version(PROGNAME);
178 infile = fopen(filename, "r");
180 fprintf(stderr, "%s: %s: %s\n", progname,
181 filename, strerror(errno));
186 while (getline(&line, &n, infile) >= 0) {
187 char *c = strchr(line, '\n');
190 if (!test_crossparse(line, mode))
193 } else if (argv[optind]) {
194 for (int i = optind; i < argc; i++) {
195 if (!test_crossparse(argv[i], mode))