2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011 Nick Bowler
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.
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.
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/>.
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' },
43 static void print_version(void)
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.");
53 static void print_usage(FILE *f)
55 fprintf(f, "Usage: %s [options]\n", progname);
58 static void print_help(void)
61 puts("Detailed help coming soon.");
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.
69 * Returns NULL on failure.
72 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
80 rc = func(buf, bufsz, decl);
84 tmp = realloc(buf, rc + 1);
86 fprintf(stderr, "failed to allocate memory\n");
98 static int cmd_explain(const char *cmd, const char *arg)
100 const struct cdecl_error *err;
105 decl = cdecl_parse_decl(arg);
107 err = cdecl_get_error();
108 fprintf(stderr, "%s\n", err->str);
112 for (struct cdecl *i = decl; i; i = i->next) {
113 str = do_format(cdecl_explain, i);
126 static int cmd_simplify(const char *cmd, const char *arg)
128 const struct cdecl_error *err;
133 decl = cdecl_parse_decl(arg);
135 err = cdecl_get_error();
136 fprintf(stderr, "%s\n", err->str);
140 for (struct cdecl *i = decl; i; i = i->next) {
141 struct cdecl_declspec *s = i->specifiers;
144 i->specifiers = NULL;
148 str = do_format(cdecl_declare, i);
165 static int cmd_declare(const char *cmd, const char *arg)
167 const struct cdecl_error *err;
172 /* The name of the command is significant here. */
173 decl = cdecl_parse_english(cmd);
175 err = cdecl_get_error();
176 fprintf(stderr, "%s\n", err->str);
181 * English parses have at most one full declarator, so no loop is
184 str = do_format(cdecl_declare, decl);
195 static int cmd_quit(const char *cmd, const char *arg)
200 static int cmd_help(const char *cmd, const char *arg);
202 static const struct command {
204 int (*func)(const char *cmd, const char *arg);
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 }
215 static const size_t ncommands = sizeof commands / sizeof commands[0];
217 static int cmd_help(const char *cmd, const char *arg)
219 for (size_t i = 0; i < ncommands; i++) {
220 if (!commands[i].blurb)
223 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
230 * Ensure that the first n characters of str equal the entire (null-terminated)
233 static int cmd_cmp(const char *str, const char *cmd, size_t n)
235 size_t cmdlen = strlen(cmd);
241 return memcmp(str, cmd, n);
244 static int run_command(const char *line)
246 const char *cmd = line + strspn(line, " \t");
247 const char *arg = cmd + strcspn(cmd, " \t");
252 for (size_t i = 0; i < ncommands; i++) {
253 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
256 return commands[i].func(cmd, arg);
259 fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
263 static int repl(void)
267 for (; (line = readline("> ")); free(line)) {
268 if (!run_command(line))
276 static int repl_cmdline(int argc, char **argv)
278 int opt, rc, ret = 0;
281 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
285 rc = run_command(optarg);
295 static int repl_noninteractive(void)
297 int rc, ret = 0, saved_errno;
301 while (getline(&line, &n, stdin) >= 0) {
302 char *c = strchr(line, '\n');
306 rc = run_command(line);
318 perror("read error");
325 int main(int argc, char **argv)
327 bool show_intro = true, interactive = true, execute = false;
328 const char *filename = NULL;
334 /* Initialize gettext. */
335 setlocale(LC_ALL, "");
336 bindtextdomain(PACKAGE, LOCALEDIR);
339 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
368 /* --filename and --execute imply --batch. */
369 if (filename || execute)
372 /* --batch implies --quiet */
373 if (interactive && show_intro)
376 /* --execute supersedes --filename */
377 if (filename && !execute) {
378 if (!freopen(filename, "r", stdin)) {
387 rc = repl_cmdline(argc, argv);
389 rc = repl_noninteractive();