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