]> git.draconx.ca Git - cdecl99.git/blob - t/crossparse.c
Avoid the use of for loop declarations.
[cdecl99.git] / t / crossparse.c
1 /*
2  * Test that libcdecl can parse its own output.
3  * Copyright © 2012, 2020, 2022-2023 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <getopt.h>
26 #include <cdecl.h>
27 #include "test.h"
28
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' },
38         { 0 }
39 };
40
41 static void print_usage(FILE *f)
42 {
43         fprintf(f, "Usage: %s [options]\n", progname);
44 }
45
46 static void print_help(void)
47 {
48         print_usage(stdout);
49         puts("Test that libcdecl can parse its own output.\n");
50         test_print_options(lopts);
51 }
52
53 enum {
54         MODE_CDECL,
55         MODE_ENGLISH,
56 };
57
58 typedef struct cdecl *parse_func(const char *);
59 typedef size_t render_func(char *, size_t, struct cdecl *);
60
61 char *rerender(const char *str, const char *parse_name,  parse_func *parse,
62                                 const char *render_name, render_func *render)
63 {
64         struct cdecl *decl = NULL;
65         char *buf = NULL;
66         size_t len;
67
68         decl = parse(str);
69         if (!decl) {
70                 fprintf(stderr, "%s: %s: failed to parse input: %s\n",
71                                 progname, parse_name, cdecl_get_error()->str);
72                 goto err;
73         }
74
75         len = render(NULL, 0, decl);
76         buf = malloc_nofail(len+1);
77         if (render(buf, len+1, decl) != len) {
78                 fprintf(stderr, "%s: %s: inconsistent length returned\n",
79                                 progname, render_name);
80                 goto err;
81         }
82
83         cdecl_free(decl);
84         return buf;
85 err:
86         cdecl_free(decl);
87         free(buf);
88
89         fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
90         return NULL;
91 }
92 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
93
94 static bool test_crossparse(const char *str, int mode)
95 {
96         char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
97         bool ret = false;
98
99         if (mode == MODE_ENGLISH) {
100                 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
101                 if (!buf1)
102                         goto out;
103                 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
104                 if (!buf2)
105                         goto out;
106                 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
107                 if (!buf3)
108                         goto out;
109         } else {
110                 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
111                 if (!buf1)
112                         goto out;
113                 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
114                 if (!buf2)
115                         goto out;
116                 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
117                 if (!buf3)
118                         goto out;
119         }
120
121         if (!strcmp(buf1, buf3))
122                 ret = true;
123 out:
124         free(buf1);
125         free(buf2);
126         free(buf3);
127
128         if (!ret) {
129                 fprintf(stderr, "%s: failed cross-parse check of: %s\n",
130                                 progname, str);
131         }
132
133         return ret;
134 }
135
136 int main(int argc, char **argv)
137 {
138         int opt, mode = MODE_CDECL;
139         int ret = EXIT_SUCCESS;
140         int i;
141
142         const char *filename = NULL;
143         FILE *infile = NULL;
144
145         if (argc > 0)
146                 progname = argv[0];
147
148         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
149                 switch (opt) {
150                 case 'C':
151                         mode = MODE_CDECL;
152                         break;
153                 case 'E':
154                         mode = MODE_ENGLISH;
155                         break;
156                 case 'f':
157                         infile = stdin;
158                         filename = optarg;
159                         break;
160                 case 'V':
161                         test_print_version(PROGNAME);
162                         return EXIT_SUCCESS;
163                 case 'H':
164                         print_help();
165                         return EXIT_SUCCESS;
166                 default:
167                         print_usage(stderr);
168                         return EXIT_FAILURE;
169                 }
170         }
171
172         if (infile) {
173                 char *line = NULL;
174                 size_t n;
175
176                 if (filename) {
177                         infile = fopen(filename, "r");
178                         if (!infile) {
179                                 fprintf(stderr, "%s: %s: %s\n", progname,
180                                                 filename, strerror(errno));
181                                 return EXIT_FAILURE;
182                         }
183                 }
184
185                 while (getline(&line, &n, infile) >= 0) {
186                         char *c = strchr(line, '\n');
187                         if (c)
188                                 *c = '\0';
189                         if (!test_crossparse(line, mode))
190                                 ret = EXIT_FAILURE;
191                 }
192
193                 free(line);
194                 fclose(infile);
195         } else if (argv[optind]) {
196                 for (i = optind; i < argc; i++) {
197                         if (!test_crossparse(argv[i], mode))
198                                 ret = EXIT_FAILURE;
199                 }
200         } else {
201                 print_usage(stderr);
202                 return EXIT_FAILURE;
203         }
204
205         return ret;
206 }