]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Add support for --execute and --file options.
[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(char *cmd, 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(char *cmd, 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(char *cmd, char *arg)
155 {
156         return 0;
157 }
158
159 static int cmd_help(char *cmd, char *arg);
160
161 static const struct command {
162         char name[16];
163         int (*func)(char *cmd, 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(char *cmd, 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 static int run_command(char *line)
187 {
188         char *cmd = line + strspn(line, " \t");
189         char *arg = cmd  + strcspn(cmd, " \t");
190
191         if (cmd[0] == '\0')
192                 return 1;
193         if (arg[0] != '\0')
194                 *arg++ = '\0';
195
196         for (size_t i = 0; i < ncommands; i++) {
197                 if (strcmp(cmd, commands[i].name) != 0)
198                         continue;
199
200                 return commands[i].func(cmd, arg);
201         }
202
203         fprintf(stderr, "Undefined command: %s\n", cmd);
204         return -1;
205 }
206
207 static int repl(void)
208 {
209         char *line;
210
211         for (; (line = readline("> ")); free(line)) {
212                 if (!run_command(line))
213                         break;
214         }
215
216         free(line);
217         return 0;
218 }
219
220 static int repl_cmdline(int argc, char **argv)
221 {
222         int opt, rc, ret = 0;
223
224         optind = 1;
225         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
226                 if (opt != 'e')
227                         continue;
228
229                 rc = run_command(optarg);
230                 if (rc < 0)
231                         ret = -1;
232                 else if (rc == 0)
233                         break;
234         }
235
236         return ret;
237 }
238
239 static int repl_noninteractive(void)
240 {
241         int rc, ret = 0, saved_errno;
242         char *line = NULL;
243         size_t n;
244
245         while (getline(&line, &n, stdin) >= 0) {
246                 char *c = strchr(line, '\n');
247                 if (c)
248                         *c = '\0';
249
250                 rc = run_command(line);
251                 if (rc < 0)
252                         ret = -1;
253                 else if (rc == 0)
254                         break;
255         }
256
257         saved_errno = errno;
258         free(line);
259
260         if (ferror(stdin)) {
261                 errno = saved_errno;
262                 perror("read error");
263                 return -1;
264         }
265
266         return ret;
267 }
268
269 int main(int argc, char **argv)
270 {
271         bool show_intro = true, interactive = true, execute = false;
272         const char *filename = NULL;
273         int opt, rc;
274
275         if (argc > 0)
276                 progname = argv[0];
277
278         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
279                 switch (opt) {
280                 case 'q':
281                         show_intro = false;
282                         break;
283                 case 'b':
284                         interactive = false;
285                         break;
286                 case 'i':
287                         interactive = true;
288                         break;
289                 case 'f':
290                         filename = optarg;
291                         break;
292                 case 'e':
293                         execute = true;
294                         break;
295                 case 'V':
296                         print_version();
297                         return EXIT_SUCCESS;
298                 case 'H':
299                         print_help();
300                         return EXIT_SUCCESS;
301                 default:
302                         print_usage(stderr);
303                         return EXIT_FAILURE;
304                 }
305         }
306
307         /* --filename and --execute imply --batch. */
308         if (filename || execute)
309                 interactive = false;
310
311         /* --batch implies --quiet */
312         if (interactive && show_intro)
313                 print_version();
314
315         /* --execute supersedes --filename */
316         if (filename && !execute) {
317                 if (!freopen(filename, "r", stdin)) {
318                         perror(filename);
319                         return EXIT_FAILURE;
320                 }
321         }
322
323         if (interactive)
324                 rc = repl();
325         else if (execute)
326                 rc = repl_cmdline(argc, argv);
327         else
328                 rc = repl_noninteractive();
329
330         if (rc != 0)
331                 return EXIT_FAILURE;
332         return EXIT_SUCCESS;
333 }