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/>.
27 static const char *progname = "cdecl99";
28 static const char sopts[] = "qbiVH";
29 static const struct option lopts[] = {
30 { "quiet", 0, NULL, 'q' },
31 { "batch", 0, NULL, 'b' },
32 { "interactive", 0, NULL, 'i' },
33 { "version", 0, NULL, 'V' },
34 { "help", 0, NULL, 'H' },
38 static void print_version(void)
41 puts("Copyright (C) 2011 Nick Bowler.");
42 puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
43 puts("This is free software: you are free to change and redistribute it.");
44 puts("There is NO WARRANTY, to the extent permitted by law.");
47 static void print_usage(FILE *f)
49 fprintf(f, "Usage: %s [options]\n", progname);
52 static void print_help(void)
55 puts("Detailed help coming soon.");
59 * Format a declaration according to the given function and return a pointer
60 * to the formatted string. The returned pointer remains valid until the
61 * next call, after which it must not be re-used.
63 * Returns NULL on failure.
66 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
74 rc = func(buf, bufsz, decl);
78 tmp = realloc(buf, rc + 1);
80 fprintf(stderr, "failed to allocate memory\n");
92 static int cmd_explain(char *cmd, char *arg)
98 decl = cdecl_parse_decl(arg);
102 for (struct cdecl *i = decl; i; i = i->next) {
103 str = do_format(cdecl_explain, i);
116 static int cmd_simplify(char *cmd, char *arg)
122 decl = cdecl_parse_decl(arg);
126 for (struct cdecl *i = decl; i; i = i->next) {
127 struct cdecl_declspec *s = i->specifiers;
130 i->specifiers = NULL;
134 str = do_format(cdecl_declare, i);
151 static int cmd_quit(char *cmd, char *arg)
156 static int cmd_help(char *cmd, char *arg);
158 static const struct command {
160 int (*func)(char *cmd, char *arg);
163 { "explain", cmd_explain, "Explain a C declaration." },
164 { "simplify", cmd_simplify, "Simplify a C declaration." },
165 { "help", cmd_help, "Print this list of commands." },
166 { "quit", cmd_quit, "Quit the program." },
167 { "exit", cmd_quit, NULL }
169 static const size_t ncommands = sizeof commands / sizeof commands[0];
171 static int cmd_help(char *cmd, char *arg)
173 for (size_t i = 0; i < ncommands; i++) {
174 if (!commands[i].blurb)
177 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
183 static int repl(bool interactive)
185 char *prompt = interactive ? "> " : NULL;
189 for (; (line = readline(prompt)); free(line)) {
190 char *cmd = line + strspn(line, " \t");
191 char *arg = cmd + strcspn(cmd, " \t");
198 for (size_t i = 0; i < ncommands; i++) {
199 if (strcmp(cmd, commands[i].name) != 0)
202 rc = commands[i].func(cmd, arg);
203 if (!interactive && rc < 0)
210 fprintf(stderr, "Undefined command: %s\n", cmd);
220 int main(int argc, char **argv)
222 bool show_intro = true, interactive = true;
228 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
251 if (interactive && show_intro)
254 if (repl(interactive) != 0)