]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Start implementing proper error handling.
[cdecl99.git] / src / cdecl99.c
1 /*
2  *  Command line utility for making sense of C declarations.
3  *  Copyright © 2011 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 <http://www.gnu.org/licenses/>.
17  */
18 #include <config.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <getopt.h>
26 #include <gettext.h>
27 #include "readline.h"
28 #include "cdecl.h"
29
30 static const char *progname = "cdecl99";
31 static const char sopts[] = "qbif:e:VH";
32 static const struct option lopts[] = {
33         { "quiet",       0, NULL, 'q' },
34         { "batch",       0, NULL, 'b' },
35         { "interactive", 0, NULL, 'i' },
36         { "file",        1, NULL, 'f' },
37         { "execute",     1, NULL, 'e' },
38         { "version",     0, NULL, 'V' },
39         { "help",        0, NULL, 'H' },
40         { 0 }
41 };
42
43 static void print_version(void)
44 {
45         puts(PACKAGE_STRING);
46         /* TRANSLATORS: (C) must *always* be translated as ©. */
47         printf("Copyright %s 2011 Nick Bowler.\n", gettext("(C)"));
48         puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
49         puts("This is free software: you are free to change and redistribute it.");
50         puts("There is NO WARRANTY, to the extent permitted by law.");
51 }
52
53 static void print_usage(FILE *f)
54 {
55         fprintf(f, "Usage: %s [options]\n", progname);
56 }
57
58 static void print_help(void)
59 {
60         print_usage(stdout);
61         puts("Detailed help coming soon.");
62 }
63
64 /*
65  * Format a declaration according to the given function and return a pointer
66  * to the formatted string.  The returned pointer remains valid until the
67  * next call, after which it must not be re-used.
68  *
69  * Returns NULL on failure.
70  */
71 static const char *
72 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
73 {
74         static size_t bufsz;
75         static char *buf;
76
77         size_t rc;
78
79 retry:
80         rc = func(buf, bufsz, decl);
81         if (rc >= bufsz) {
82                 char *tmp;
83
84                 tmp = realloc(buf, rc + 1);
85                 if (!tmp) {
86                         fprintf(stderr, "failed to allocate memory\n");
87                         return NULL;
88                 }
89
90                 buf = tmp;
91                 bufsz = rc + 1;
92                 goto retry;
93         }
94
95         return buf;
96 }
97
98 static int cmd_explain(const char *cmd, const char *arg)
99 {
100         const struct cdecl_error *err;
101         struct cdecl *decl;
102         const char *str;
103         int ret = -1;
104
105         decl = cdecl_parse_decl(arg);
106         if (!decl) {
107                 err = cdecl_get_error();
108                 fprintf(stderr, "%s\n", err->str);
109                 goto out;
110         }
111
112         for (struct cdecl *i = decl; i; i = i->next) {
113                 str = do_format(cdecl_explain, i);
114                 if (!str)
115                         goto out;
116
117                 printf("%s\n", str);
118         }
119
120         ret = 1;
121 out:
122         cdecl_free(decl);
123         return ret;
124 }
125
126 static int cmd_simplify(const char *cmd, const char *arg)
127 {
128         const struct cdecl_error *err;
129         struct cdecl *decl;
130         const char *str;
131         int ret = -1;
132
133         decl = cdecl_parse_decl(arg);
134         if (!decl) {
135                 err = cdecl_get_error();
136                 fprintf(stderr, "%s\n", err->str);
137                 goto out;
138         }
139
140         for (struct cdecl *i = decl; i; i = i->next) {
141                 struct cdecl_declspec *s = i->specifiers;
142
143                 if (i != decl) {
144                         i->specifiers = NULL;
145                         printf(", ");
146                 }
147
148                 str = do_format(cdecl_declare, i);
149                 i->specifiers = s;
150
151                 if (!str)
152                         goto out;
153
154                 printf("%s", str);
155         }
156
157         putchar('\n');
158
159         ret = 1;
160 out:
161         cdecl_free(decl);
162         return ret;
163 }
164
165 static int cmd_declare(const char *cmd, const char *arg)
166 {
167         const struct cdecl_error *err;
168         struct cdecl *decl;
169         const char *str;
170         int ret = -1;
171
172         /* The name of the command is significant here. */
173         decl = cdecl_parse_english(cmd);
174         if (!decl) {
175                 err = cdecl_get_error();
176                 fprintf(stderr, "%s\n", err->str);
177                 goto out;
178         }
179
180         /*
181          * English parses have at most one full declarator, so no loop is
182          * needed here.
183          */
184         str = do_format(cdecl_declare, decl);
185         if (!str)
186                 goto out;
187
188         printf("%s\n", str);
189         ret = 1;
190 out:
191         cdecl_free(decl);
192         return ret;
193 }
194
195 static int cmd_quit(const char *cmd, const char *arg)
196 {
197         return 0;
198 }
199
200 static int cmd_help(const char *cmd, const char *arg);
201
202 static const struct command {
203         char name[16];
204         int (*func)(const char *cmd, const char *arg);
205         const char *blurb;
206 } commands[] = {
207         { "explain",  cmd_explain,  "Explain a C declaration." },
208         { "simplify", cmd_simplify, "Simplify a C declaration." },
209         { "declare",  cmd_declare,  "Construct a C declaration." },
210         { "type",     cmd_declare,  "Construct a C type name." },
211         { "help",     cmd_help,     "Print this list of commands." },
212         { "quit",     cmd_quit,     "Quit the program." },
213         { "exit",     cmd_quit,     NULL }
214 };
215 static const size_t ncommands = sizeof commands / sizeof commands[0];
216
217 static int cmd_help(const char *cmd, const char *arg)
218 {
219         for (size_t i = 0; i < ncommands; i++) {
220                 if (!commands[i].blurb)
221                         continue;
222
223                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
224         }
225
226         return 1;
227 }
228
229 /*
230  * Ensure that the first n characters of str equal the entire (null-terminated)
231  * string cmd.
232  */
233 static int cmd_cmp(const char *str, const char *cmd, size_t n)
234 {
235         size_t cmdlen = strlen(cmd);
236
237         if (n < cmdlen)
238                 return -1;
239         if (n > cmdlen)
240                 return 1;
241         return memcmp(str, cmd, n);
242 }
243
244 static int run_command(const char *line)
245 {
246         const char *cmd = line + strspn(line, " \t");
247         const char *arg = cmd  + strcspn(cmd, " \t");
248
249         if (cmd[0] == '\0')
250                 return 1;
251
252         for (size_t i = 0; i < ncommands; i++) {
253                 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
254                         continue;
255
256                 return commands[i].func(cmd, arg);
257         }
258
259         fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
260         return -1;
261 }
262
263 static int repl(void)
264 {
265         char *line;
266
267         for (; (line = readline("> ")); free(line)) {
268                 if (!run_command(line))
269                         break;
270         }
271
272         free(line);
273         return 0;
274 }
275
276 static int repl_cmdline(int argc, char **argv)
277 {
278         int opt, rc, ret = 0;
279
280         optind = 1;
281         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
282                 if (opt != 'e')
283                         continue;
284
285                 rc = run_command(optarg);
286                 if (rc < 0)
287                         ret = -1;
288                 else if (rc == 0)
289                         break;
290         }
291
292         return ret;
293 }
294
295 static int repl_noninteractive(void)
296 {
297         int rc, ret = 0, saved_errno;
298         char *line = NULL;
299         size_t n;
300
301         while (getline(&line, &n, stdin) >= 0) {
302                 char *c = strchr(line, '\n');
303                 if (c)
304                         *c = '\0';
305
306                 rc = run_command(line);
307                 if (rc < 0)
308                         ret = -1;
309                 else if (rc == 0)
310                         break;
311         }
312
313         saved_errno = errno;
314         free(line);
315
316         if (ferror(stdin)) {
317                 errno = saved_errno;
318                 perror("read error");
319                 return -1;
320         }
321
322         return ret;
323 }
324
325 int main(int argc, char **argv)
326 {
327         bool show_intro = true, interactive = true, execute = false;
328         const char *filename = NULL;
329         int opt, rc;
330
331         if (argc > 0)
332                 progname = argv[0];
333
334         /* Initialize gettext. */
335         setlocale(LC_ALL, "");
336         bindtextdomain(PACKAGE, LOCALEDIR);
337         textdomain(PACKAGE);
338
339         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
340                 switch (opt) {
341                 case 'q':
342                         show_intro = false;
343                         break;
344                 case 'b':
345                         interactive = false;
346                         break;
347                 case 'i':
348                         interactive = true;
349                         break;
350                 case 'f':
351                         filename = optarg;
352                         break;
353                 case 'e':
354                         execute = true;
355                         break;
356                 case 'V':
357                         print_version();
358                         return EXIT_SUCCESS;
359                 case 'H':
360                         print_help();
361                         return EXIT_SUCCESS;
362                 default:
363                         print_usage(stderr);
364                         return EXIT_FAILURE;
365                 }
366         }
367
368         /* --filename and --execute imply --batch. */
369         if (filename || execute)
370                 interactive = false;
371
372         /* --batch implies --quiet */
373         if (interactive && show_intro)
374                 print_version();
375
376         /* --execute supersedes --filename */
377         if (filename && !execute) {
378                 if (!freopen(filename, "r", stdin)) {
379                         perror(filename);
380                         return EXIT_FAILURE;
381                 }
382         }
383
384         if (interactive)
385                 rc = repl();
386         else if (execute)
387                 rc = repl_cmdline(argc, argv);
388         else
389                 rc = repl_noninteractive();
390
391         if (rc != 0)
392                 return EXIT_FAILURE;
393         return EXIT_SUCCESS;
394 }