9 static const char *progname = "cdecl99";
10 static const char sopts[] = "VH";
11 static const struct option lopts[] = {
12 { "version", 0, NULL, 'V' },
13 { "help", 0, NULL, 'H' },
17 static void print_version(void)
20 puts("Copyright (C) 2011 Nick Bowler.");
21 puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
22 puts("This is free software: you are free to change and redistribute it.");
23 puts("There is NO WARRANTY, to the extent permitted by law.");
26 static void print_usage(FILE *f)
28 fprintf(f, "Usage: %s [options]\n", progname);
31 static void print_help(void)
34 puts("Detailed help coming soon.");
37 static int cmd_explain(char *cmd, char *arg)
45 decl = cdecl_parse_decl(arg);
49 for (struct cdecl *i = decl; i; i = i->next) {
51 rc = cdecl_explain(buf, bufsz, i);
55 tmp = realloc(buf, rc + 1);
57 fprintf(stderr, "failed to allocate memory\n");
73 static int cmd_quit(char *cmd, char *arg)
78 static int cmd_help(char *cmd, char *arg);
80 static const struct command {
82 int (*func)(char *cmd, char *arg);
85 { "explain", cmd_explain, "Explain a C declaration." },
86 { "help", cmd_help, "Print this list of commands." },
87 { "quit", cmd_quit, "Quit the program." },
88 { "exit", cmd_quit, NULL }
90 static const size_t ncommands = sizeof commands / sizeof commands[0];
92 static int cmd_help(char *cmd, char *arg)
94 for (size_t i = 0; i < ncommands; i++) {
95 if (!commands[i].blurb)
98 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
104 static int repl(void)
111 for (; (line = readline("> ")); free(line)) {
112 char *cmd = line + strspn(line, " \t");
113 char *arg = cmd + strcspn(cmd, " \t");
120 for (size_t i = 0; i < ncommands; i++) {
121 if (strcmp(cmd, commands[i].name) != 0)
124 ret = commands[i].func(cmd, arg);
130 fprintf(stderr, "Undefined command: %s\n", cmd);
141 int main(int argc, char **argv)
148 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {