]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Kill the "horizontal" declarator chain.
[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         static size_t bufsz;
40         static char *buf;
41
42         struct cdecl *decl;
43         size_t rc;
44
45         decl = cdecl_parse_decl(arg);
46         if (!decl)
47                 goto out;
48
49         for (struct cdecl *i = decl; i; i = i->next) {
50 retry:
51                 rc = cdecl_explain(buf, bufsz, i);
52                 if (rc >= bufsz) {
53                         char *tmp;
54
55                         tmp = realloc(buf, rc + 1);
56                         if (!tmp) {
57                                 fprintf(stderr, "failed to allocate memory\n");
58                                 goto out;
59                         }
60
61                         buf = tmp;
62                         bufsz = rc + 1;
63                         goto retry;
64                 }
65
66                 printf("%s\n", buf);
67         }
68 out:
69         cdecl_free(decl);
70         return 1;
71 }
72
73 static int cmd_quit(char *cmd, char *arg)
74 {
75         return 0;
76 }
77
78 static int cmd_help(char *cmd, char *arg);
79
80 static const struct command {
81         char name[16];
82         int (*func)(char *cmd, char *arg);
83         const char *blurb;
84 } commands[] = {
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 }
89 };
90 static const size_t ncommands = sizeof commands / sizeof commands[0];
91
92 static int cmd_help(char *cmd, char *arg)
93 {
94         for (size_t i = 0; i < ncommands; i++) {
95                 if (!commands[i].blurb)
96                         continue;
97
98                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
99         }
100
101         return 1;
102 }
103
104 static int repl(void)
105 {
106         char *line;
107         int ret;
108
109         print_version();
110
111         for (; (line = readline("> ")); free(line)) {
112                 char *cmd = line + strspn(line, " \t");
113                 char *arg = cmd  + strcspn(cmd, " \t");
114
115                 if (cmd[0] == '\0')
116                         continue;
117                 if (arg[0] != '\0')
118                         *arg++ = '\0';
119
120                 for (size_t i = 0; i < ncommands; i++) {
121                         if (strcmp(cmd, commands[i].name) != 0)
122                                 continue;
123
124                         ret = commands[i].func(cmd, arg);
125                         if (ret <= 0)
126                                 goto out;
127                         goto next;
128                 }
129
130                 fprintf(stderr, "Undefined command: %s\n", cmd);
131         next:
132                 ;
133         }
134
135         ret = 0;
136 out:
137         free(line);
138         return ret;
139 }
140
141 int main(int argc, char **argv)
142 {
143         int opt;
144
145         if (argc > 0)
146                 progname = argv[0];
147
148         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
149                 switch (opt) {
150                 case 'V':
151                         print_version();
152                         return EXIT_SUCCESS;
153                 case 'H':
154                         print_help();
155                         return EXIT_SUCCESS;
156                 default:
157                         print_usage(stderr);
158                         return EXIT_FAILURE;
159                 }
160         }
161
162         if (repl() != 0)
163                 return EXIT_FAILURE;
164
165         return EXIT_SUCCESS;
166 }