]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Fix build with NLS disabled.
[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         struct cdecl *decl;
101         const char *str;
102         int ret = -1;
103
104         decl = cdecl_parse_decl(arg);
105         if (!decl)
106                 goto out;
107
108         for (struct cdecl *i = decl; i; i = i->next) {
109                 str = do_format(cdecl_explain, i);
110                 if (!str)
111                         goto out;
112
113                 printf("%s\n", str);
114         }
115
116         ret = 1;
117 out:
118         cdecl_free(decl);
119         return ret;
120 }
121
122 static int cmd_simplify(const char *cmd, const char *arg)
123 {
124         struct cdecl *decl;
125         const char *str;
126         int ret = -1;
127
128         decl = cdecl_parse_decl(arg);
129         if (!decl)
130                 goto out;
131
132         for (struct cdecl *i = decl; i; i = i->next) {
133                 struct cdecl_declspec *s = i->specifiers;
134
135                 if (i != decl) {
136                         i->specifiers = NULL;
137                         printf(", ");
138                 }
139
140                 str = do_format(cdecl_declare, i);
141                 i->specifiers = s;
142
143                 if (!str)
144                         goto out;
145
146                 printf("%s", str);
147         }
148
149         putchar('\n');
150
151         ret = 1;
152 out:
153         cdecl_free(decl);
154         return ret;
155 }
156
157 static int cmd_declare(const char *cmd, const char *arg)
158 {
159         struct cdecl *decl;
160         const char *str;
161         int ret = -1;
162
163         /* The name of the command is significant here. */
164         decl = cdecl_parse_english(cmd);
165         if (!decl)
166                 goto out;
167
168         /*
169          * English parses have at most one full declarator, so no loop is
170          * needed here.
171          */
172         str = do_format(cdecl_declare, decl);
173         if (!str)
174                 goto out;
175
176         printf("%s\n", str);
177         ret = 1;
178 out:
179         cdecl_free(decl);
180         return ret;
181 }
182
183 static int cmd_quit(const char *cmd, const char *arg)
184 {
185         return 0;
186 }
187
188 static int cmd_help(const char *cmd, const char *arg);
189
190 static const struct command {
191         char name[16];
192         int (*func)(const char *cmd, const char *arg);
193         const char *blurb;
194 } commands[] = {
195         { "explain",  cmd_explain,  "Explain a C declaration." },
196         { "simplify", cmd_simplify, "Simplify a C declaration." },
197         { "declare",  cmd_declare,  "Construct a C declaration." },
198         { "type",     cmd_declare,  "Construct a C type name." },
199         { "help",     cmd_help,     "Print this list of commands." },
200         { "quit",     cmd_quit,     "Quit the program." },
201         { "exit",     cmd_quit,     NULL }
202 };
203 static const size_t ncommands = sizeof commands / sizeof commands[0];
204
205 static int cmd_help(const char *cmd, const char *arg)
206 {
207         for (size_t i = 0; i < ncommands; i++) {
208                 if (!commands[i].blurb)
209                         continue;
210
211                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
212         }
213
214         return 1;
215 }
216
217 /*
218  * Ensure that the first n characters of str equal the entire (null-terminated)
219  * string cmd.
220  */
221 static int cmd_cmp(const char *str, const char *cmd, size_t n)
222 {
223         size_t cmdlen = strlen(cmd);
224
225         if (n < cmdlen)
226                 return -1;
227         if (n > cmdlen)
228                 return 1;
229         return memcmp(str, cmd, n);
230 }
231
232 static int run_command(const char *line)
233 {
234         const char *cmd = line + strspn(line, " \t");
235         const char *arg = cmd  + strcspn(cmd, " \t");
236
237         if (cmd[0] == '\0')
238                 return 1;
239
240         for (size_t i = 0; i < ncommands; i++) {
241                 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
242                         continue;
243
244                 return commands[i].func(cmd, arg);
245         }
246
247         fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
248         return -1;
249 }
250
251 static int repl(void)
252 {
253         char *line;
254
255         for (; (line = readline("> ")); free(line)) {
256                 if (!run_command(line))
257                         break;
258         }
259
260         free(line);
261         return 0;
262 }
263
264 static int repl_cmdline(int argc, char **argv)
265 {
266         int opt, rc, ret = 0;
267
268         optind = 1;
269         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
270                 if (opt != 'e')
271                         continue;
272
273                 rc = run_command(optarg);
274                 if (rc < 0)
275                         ret = -1;
276                 else if (rc == 0)
277                         break;
278         }
279
280         return ret;
281 }
282
283 static int repl_noninteractive(void)
284 {
285         int rc, ret = 0, saved_errno;
286         char *line = NULL;
287         size_t n;
288
289         while (getline(&line, &n, stdin) >= 0) {
290                 char *c = strchr(line, '\n');
291                 if (c)
292                         *c = '\0';
293
294                 rc = run_command(line);
295                 if (rc < 0)
296                         ret = -1;
297                 else if (rc == 0)
298                         break;
299         }
300
301         saved_errno = errno;
302         free(line);
303
304         if (ferror(stdin)) {
305                 errno = saved_errno;
306                 perror("read error");
307                 return -1;
308         }
309
310         return ret;
311 }
312
313 int main(int argc, char **argv)
314 {
315         bool show_intro = true, interactive = true, execute = false;
316         const char *filename = NULL;
317         int opt, rc;
318
319         if (argc > 0)
320                 progname = argv[0];
321
322         /* Initialize gettext. */
323         setlocale(LC_ALL, "");
324         bindtextdomain(PACKAGE, LOCALEDIR);
325         textdomain(PACKAGE);
326
327         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
328                 switch (opt) {
329                 case 'q':
330                         show_intro = false;
331                         break;
332                 case 'b':
333                         interactive = false;
334                         break;
335                 case 'i':
336                         interactive = true;
337                         break;
338                 case 'f':
339                         filename = optarg;
340                         break;
341                 case 'e':
342                         execute = true;
343                         break;
344                 case 'V':
345                         print_version();
346                         return EXIT_SUCCESS;
347                 case 'H':
348                         print_help();
349                         return EXIT_SUCCESS;
350                 default:
351                         print_usage(stderr);
352                         return EXIT_FAILURE;
353                 }
354         }
355
356         /* --filename and --execute imply --batch. */
357         if (filename || execute)
358                 interactive = false;
359
360         /* --batch implies --quiet */
361         if (interactive && show_intro)
362                 print_version();
363
364         /* --execute supersedes --filename */
365         if (filename && !execute) {
366                 if (!freopen(filename, "r", stdin)) {
367                         perror(filename);
368                         return EXIT_FAILURE;
369                 }
370         }
371
372         if (interactive)
373                 rc = repl();
374         else if (execute)
375                 rc = repl_cmdline(argc, argv);
376         else
377                 rc = repl_noninteractive();
378
379         if (rc != 0)
380                 return EXIT_FAILURE;
381         return EXIT_SUCCESS;
382 }