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)
43 struct cdecl_declarator *d;
46 decl = cdecl_parse_decl(arg);
50 for (d = decl->declarators; d; d = d->next) {
51 struct cdecl decl_tmp = {
52 .specifiers = decl->specifiers,
57 rc = cdecl_explain(buf, bufsz, &decl_tmp);
61 tmp = realloc(buf, rc + 1);
63 fprintf(stderr, "failed to allocate memory\n");
79 static int cmd_quit(char *cmd, char *arg)
84 static int cmd_help(char *cmd, char *arg);
86 static const struct command {
88 int (*func)(char *cmd, char *arg);
91 { "explain", cmd_explain, "Explain a C declaration." },
92 { "help", cmd_help, "Print this list of commands." },
93 { "quit", cmd_quit, "Quit the program." },
94 { "exit", cmd_quit, NULL }
96 static const size_t ncommands = sizeof commands / sizeof commands[0];
98 static int cmd_help(char *cmd, char *arg)
100 for (size_t i = 0; i < ncommands; i++) {
101 if (!commands[i].blurb)
104 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
110 static int repl(void)
117 for (; (line = readline("> ")); free(line)) {
118 char *cmd = line + strspn(line, " \t");
119 char *arg = cmd + strcspn(cmd, " \t");
126 for (size_t i = 0; i < ncommands; i++) {
127 if (strcmp(cmd, commands[i].name) != 0)
130 ret = commands[i].func(cmd, arg);
136 fprintf(stderr, "Undefined command: %s\n", cmd);
147 int main(int argc, char **argv)
154 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {