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);
50 rc = cdecl_explain(buf, bufsz, decl);
54 tmp = realloc(buf, rc + 1);
56 fprintf(stderr, "failed to allocate memory\n");
71 static int cmd_quit(char *cmd, char *arg)
76 static int cmd_help(char *cmd, char *arg);
78 static const struct command {
80 int (*func)(char *cmd, char *arg);
83 { "explain", cmd_explain, "Explain a C declaration." },
84 { "help", cmd_help, "Print this list of commands." },
85 { "quit", cmd_quit, "Quit the program." },
86 { "exit", cmd_quit, NULL }
88 static const size_t ncommands = sizeof commands / sizeof commands[0];
90 static int cmd_help(char *cmd, char *arg)
92 for (size_t i = 0; i < ncommands; i++) {
93 if (!commands[i].blurb)
96 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
102 static int repl(void)
109 for (; (line = readline("> ")); free(line)) {
110 char *cmd = line + strspn(line, " \t");
111 char *arg = cmd + strcspn(cmd, " \t");
118 for (size_t i = 0; i < ncommands; i++) {
119 if (strcmp(cmd, commands[i].name) != 0)
122 ret = commands[i].func(cmd, arg);
128 fprintf(stderr, "Undefined command: %s\n", cmd);
139 int main(int argc, char **argv)
146 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {