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.");
58 static int cmd_explain(char *cmd, char *arg)
67 decl = cdecl_parse_decl(arg);
71 for (struct cdecl *i = decl; i; i = i->next) {
73 rc = cdecl_explain(buf, bufsz, i);
77 tmp = realloc(buf, rc + 1);
79 fprintf(stderr, "failed to allocate memory\n");
97 static int cmd_quit(char *cmd, char *arg)
102 static int cmd_help(char *cmd, char *arg);
104 static const struct command {
106 int (*func)(char *cmd, char *arg);
109 { "explain", cmd_explain, "Explain a C declaration." },
110 { "help", cmd_help, "Print this list of commands." },
111 { "quit", cmd_quit, "Quit the program." },
112 { "exit", cmd_quit, NULL }
114 static const size_t ncommands = sizeof commands / sizeof commands[0];
116 static int cmd_help(char *cmd, char *arg)
118 for (size_t i = 0; i < ncommands; i++) {
119 if (!commands[i].blurb)
122 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
128 static int repl(bool interactive)
130 char *prompt = interactive ? "> " : NULL;
134 for (; (line = readline(prompt)); free(line)) {
135 char *cmd = line + strspn(line, " \t");
136 char *arg = cmd + strcspn(cmd, " \t");
143 for (size_t i = 0; i < ncommands; i++) {
144 if (strcmp(cmd, commands[i].name) != 0)
147 rc = commands[i].func(cmd, arg);
148 if (!interactive && rc < 0)
155 fprintf(stderr, "Undefined command: %s\n", cmd);
165 int main(int argc, char **argv)
167 bool show_intro = true, interactive = true;
173 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
196 if (interactive && show_intro)
199 if (repl(interactive) != 0)