]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Add initial command parser.
[cdecl99.git] / src / cdecl99.c
1 #include <config.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include "readline.h"
7 #include "cdecl.h"
8
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' },
14         { 0 }
15 };
16
17 static void print_version(void)
18 {
19         puts(PACKAGE_STRING);
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.");
24 }
25
26 static void print_usage(FILE *f)
27 {
28         fprintf(f, "Usage: %s [options]\n", progname);
29 }
30
31 static void print_help(void)
32 {
33         print_usage(stdout);
34         puts("Detailed help coming soon.");
35 }
36
37 static int cmd_explain(char *cmd, char *arg)
38 {
39         struct cdecl *decl;
40
41         decl = cdecl_parse_decl(arg);
42         cdecl_free(decl);
43
44         return 1;
45 }
46
47 static int cmd_quit(char *cmd, char *arg)
48 {
49         return 0;
50 }
51
52 static int cmd_help(char *cmd, char *arg);
53
54 static const struct command {
55         char name[16];
56         int (*func)(char *cmd, char *arg);
57         const char *blurb;
58 } commands[] = {
59         { "explain", cmd_explain, "Explain a C declaration." },
60         { "help",    cmd_help,    "Print this list of commands." },
61         { "quit",    cmd_quit,    "Quit the program." },
62         { "exit",    cmd_quit,    NULL }
63 };
64 static const size_t ncommands = sizeof commands / sizeof commands[0];
65
66 static int cmd_help(char *cmd, char *arg)
67 {
68         for (size_t i = 0; i < ncommands; i++) {
69                 if (!commands[i].blurb)
70                         continue;
71
72                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
73         }
74
75         return 1;
76 }
77
78 static int repl(void)
79 {
80         char *line;
81         int ret;
82
83         print_version();
84
85         for (; (line = readline("> ")); free(line)) {
86                 char *cmd = line + strspn(line, " \t");
87                 char *arg = cmd  + strcspn(cmd, " \t");
88
89                 if (cmd[0] == '\0')
90                         continue;
91                 if (arg[0] != '\0')
92                         *arg++ = '\0';
93
94                 for (size_t i = 0; i < ncommands; i++) {
95                         if (strcmp(cmd, commands[i].name) != 0)
96                                 continue;
97
98                         ret = commands[i].func(cmd, arg);
99                         if (ret <= 0)
100                                 goto out;
101                         goto next;
102                 }
103
104                 fprintf(stderr, "Undefined command: %s\n", cmd);
105         next:
106                 ;
107         }
108
109         ret = 0;
110 out:
111         free(line);
112         return ret;
113 }
114
115 int main(int argc, char **argv)
116 {
117         int opt;
118
119         if (argc > 0)
120                 progname = argv[0];
121
122         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
123                 switch (opt) {
124                 case 'V':
125                         print_version();
126                         return EXIT_SUCCESS;
127                 case 'H':
128                         print_help();
129                         return EXIT_SUCCESS;
130                 default:
131                         print_usage(stderr);
132                         return EXIT_FAILURE;
133                 }
134         }
135
136         if (repl() != 0)
137                 return EXIT_FAILURE;
138
139         return EXIT_SUCCESS;
140 }