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");
54 for (opt = lopts; opt->val; opt++) {
55 int w = print_option_start(opt, NULL);
67 typedef struct cdecl *parse_func(const char *);
68 typedef size_t render_func(char *, size_t, struct cdecl *);
70 char *rerender(const char *str, const char *parse_name, parse_func *parse,
71 const char *render_name, render_func *render)
73 struct cdecl *decl = NULL;
79 fprintf(stderr, "%s: %s: failed to parse input: %s\n",
80 progname, parse_name, cdecl_get_error()->str);
84 len = render(NULL, 0, decl);
85 buf = malloc_nofail(len+1);
86 if (render(buf, len+1, decl) != len) {
87 fprintf(stderr, "%s: %s: inconsistent length returned\n",
88 progname, render_name);
98 fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
101 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
103 static bool test_crossparse(const char *str, int mode)
105 char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
108 if (mode == MODE_ENGLISH) {
109 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
112 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
115 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
119 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
122 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
125 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
130 if (!strcmp(buf1, buf3))
138 fprintf(stderr, "%s: failed cross-parse check of: %s\n",
145 int main(int argc, char **argv)
147 int opt, mode = MODE_CDECL;
148 int ret = EXIT_SUCCESS;
150 const char *filename = NULL;
156 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
169 test_print_version(PROGNAME);
185 infile = fopen(filename, "r");
187 fprintf(stderr, "%s: %s: %s\n", progname,
188 filename, strerror(errno));
193 while (getline(&line, &n, infile) >= 0) {
194 char *c = strchr(line, '\n');
197 if (!test_crossparse(line, mode))
200 } else if (argv[optind]) {
201 for (int i = optind; i < argc; i++) {
202 if (!test_crossparse(argv[i], mode))