]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Avoid modifying the input command string.
[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_quit(const char *cmd, const char *arg)
155 {
156         return 0;
157 }
158
159 static int cmd_help(const char *cmd, const char *arg);
160
161 static const struct command {
162         char name[16];
163         int (*func)(const char *cmd, const char *arg);
164         const char *blurb;
165 } commands[] = {
166         { "explain",  cmd_explain,  "Explain a C declaration." },
167         { "simplify", cmd_simplify, "Simplify a C declaration." },
168         { "help",     cmd_help,     "Print this list of commands." },
169         { "quit",     cmd_quit,     "Quit the program." },
170         { "exit",     cmd_quit,     NULL }
171 };
172 static const size_t ncommands = sizeof commands / sizeof commands[0];
173
174 static int cmd_help(const char *cmd, const char *arg)
175 {
176         for (size_t i = 0; i < ncommands; i++) {
177                 if (!commands[i].blurb)
178                         continue;
179
180                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
181         }
182
183         return 1;
184 }
185
186 /*
187  * Ensure that the first n characters of str equal the entire (null-terminated)
188  * string cmd.
189  */
190 static int cmd_cmp(const char *str, const char *cmd, size_t n)
191 {
192         size_t cmdlen = strlen(cmd);
193
194         if (n < cmdlen)
195                 return -1;
196         if (n > cmdlen)
197                 return 1;
198         return memcmp(str, cmd, n);
199 }
200
201 static int run_command(const char *line)
202 {
203         const char *cmd = line + strspn(line, " \t");
204         const char *arg = cmd  + strcspn(cmd, " \t");
205
206         if (cmd[0] == '\0')
207                 return 1;
208
209         for (size_t i = 0; i < ncommands; i++) {
210                 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
211                         continue;
212
213                 return commands[i].func(cmd, arg);
214         }
215
216         fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
217         return -1;
218 }
219
220 static int repl(void)
221 {
222         char *line;
223
224         for (; (line = readline("> ")); free(line)) {
225                 if (!run_command(line))
226                         break;
227         }
228
229         free(line);
230         return 0;
231 }
232
233 static int repl_cmdline(int argc, char **argv)
234 {
235         int opt, rc, ret = 0;
236
237         optind = 1;
238         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
239                 if (opt != 'e')
240                         continue;
241
242                 rc = run_command(optarg);
243                 if (rc < 0)
244                         ret = -1;
245                 else if (rc == 0)
246                         break;
247         }
248
249         return ret;
250 }
251
252 static int repl_noninteractive(void)
253 {
254         int rc, ret = 0, saved_errno;
255         char *line = NULL;
256         size_t n;
257
258         while (getline(&line, &n, stdin) >= 0) {
259                 char *c = strchr(line, '\n');
260                 if (c)
261                         *c = '\0';
262
263                 rc = run_command(line);
264                 if (rc < 0)
265                         ret = -1;
266                 else if (rc == 0)
267                         break;
268         }
269
270         saved_errno = errno;
271         free(line);
272
273         if (ferror(stdin)) {
274                 errno = saved_errno;
275                 perror("read error");
276                 return -1;
277         }
278
279         return ret;
280 }
281
282 int main(int argc, char **argv)
283 {
284         bool show_intro = true, interactive = true, execute = false;
285         const char *filename = NULL;
286         int opt, rc;
287
288         if (argc > 0)
289                 progname = argv[0];
290
291         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
292                 switch (opt) {
293                 case 'q':
294                         show_intro = false;
295                         break;
296                 case 'b':
297                         interactive = false;
298                         break;
299                 case 'i':
300                         interactive = true;
301                         break;
302                 case 'f':
303                         filename = optarg;
304                         break;
305                 case 'e':
306                         execute = true;
307                         break;
308                 case 'V':
309                         print_version();
310                         return EXIT_SUCCESS;
311                 case 'H':
312                         print_help();
313                         return EXIT_SUCCESS;
314                 default:
315                         print_usage(stderr);
316                         return EXIT_FAILURE;
317                 }
318         }
319
320         /* --filename and --execute imply --batch. */
321         if (filename || execute)
322                 interactive = false;
323
324         /* --batch implies --quiet */
325         if (interactive && show_intro)
326                 print_version();
327
328         /* --execute supersedes --filename */
329         if (filename && !execute) {
330                 if (!freopen(filename, "r", stdin)) {
331                         perror(filename);
332                         return EXIT_FAILURE;
333                 }
334         }
335
336         if (interactive)
337                 rc = repl();
338         else if (execute)
339                 rc = repl_cmdline(argc, argv);
340         else
341                 rc = repl_noninteractive();
342
343         if (rc != 0)
344                 return EXIT_FAILURE;
345         return EXIT_SUCCESS;
346 }