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/>.
26 static const char *progname = "cdecl99";
27 static const char sopts[] = "VH";
28 static const struct option lopts[] = {
29 { "version", 0, NULL, 'V' },
30 { "help", 0, NULL, 'H' },
34 static void print_version(void)
37 puts("Copyright (C) 2011 Nick Bowler.");
38 puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
39 puts("This is free software: you are free to change and redistribute it.");
40 puts("There is NO WARRANTY, to the extent permitted by law.");
43 static void print_usage(FILE *f)
45 fprintf(f, "Usage: %s [options]\n", progname);
48 static void print_help(void)
51 puts("Detailed help coming soon.");
54 static int cmd_explain(char *cmd, char *arg)
62 decl = cdecl_parse_decl(arg);
66 for (struct cdecl *i = decl; i; i = i->next) {
68 rc = cdecl_explain(buf, bufsz, i);
72 tmp = realloc(buf, rc + 1);
74 fprintf(stderr, "failed to allocate memory\n");
90 static int cmd_quit(char *cmd, char *arg)
95 static int cmd_help(char *cmd, char *arg);
97 static const struct command {
99 int (*func)(char *cmd, char *arg);
102 { "explain", cmd_explain, "Explain a C declaration." },
103 { "help", cmd_help, "Print this list of commands." },
104 { "quit", cmd_quit, "Quit the program." },
105 { "exit", cmd_quit, NULL }
107 static const size_t ncommands = sizeof commands / sizeof commands[0];
109 static int cmd_help(char *cmd, char *arg)
111 for (size_t i = 0; i < ncommands; i++) {
112 if (!commands[i].blurb)
115 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
121 static int repl(void)
128 for (; (line = readline("> ")); free(line)) {
129 char *cmd = line + strspn(line, " \t");
130 char *arg = cmd + strcspn(cmd, " \t");
137 for (size_t i = 0; i < ncommands; i++) {
138 if (strcmp(cmd, commands[i].name) != 0)
141 ret = commands[i].func(cmd, arg);
147 fprintf(stderr, "Undefined command: %s\n", cmd);
158 int main(int argc, char **argv)
165 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {