]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Split up interactive/noninteractive loops.
[cdecl99.git] / src / cdecl99.c
1 /*
2  *  Command line utility for making sense of C declarations.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <config.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include "readline.h"
26 #include "cdecl.h"
27
28 static const char *progname = "cdecl99";
29 static const char sopts[] = "qbiVH";
30 static const struct option lopts[] = {
31         { "quiet",       0, NULL, 'q' },
32         { "batch",       0, NULL, 'b' },
33         { "interactive", 0, NULL, 'i' },
34         { "version",     0, NULL, 'V' },
35         { "help",        0, NULL, 'H' },
36         { 0 }
37 };
38
39 static void print_version(void)
40 {
41         puts(PACKAGE_STRING);
42         puts("Copyright (C) 2011 Nick Bowler.");
43         puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
44         puts("This is free software: you are free to change and redistribute it.");
45         puts("There is NO WARRANTY, to the extent permitted by law.");
46 }
47
48 static void print_usage(FILE *f)
49 {
50         fprintf(f, "Usage: %s [options]\n", progname);
51 }
52
53 static void print_help(void)
54 {
55         print_usage(stdout);
56         puts("Detailed help coming soon.");
57 }
58
59 /*
60  * Format a declaration according to the given function and return a pointer
61  * to the formatted string.  The returned pointer remains valid until the
62  * next call, after which it must not be re-used.
63  *
64  * Returns NULL on failure.
65  */
66 static const char *
67 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
68 {
69         static size_t bufsz;
70         static char *buf;
71
72         size_t rc;
73
74 retry:
75         rc = func(buf, bufsz, decl);
76         if (rc >= bufsz) {
77                 char *tmp;
78
79                 tmp = realloc(buf, rc + 1);
80                 if (!tmp) {
81                         fprintf(stderr, "failed to allocate memory\n");
82                         return NULL;
83                 }
84
85                 buf = tmp;
86                 bufsz = rc + 1;
87                 goto retry;
88         }
89
90         return buf;
91 }
92
93 static int cmd_explain(char *cmd, char *arg)
94 {
95         struct cdecl *decl;
96         const char *str;
97         int ret = -1;
98
99         decl = cdecl_parse_decl(arg);
100         if (!decl)
101                 goto out;
102
103         for (struct cdecl *i = decl; i; i = i->next) {
104                 str = do_format(cdecl_explain, i);
105                 if (!str)
106                         goto out;
107
108                 printf("%s\n", str);
109         }
110
111         ret = 1;
112 out:
113         cdecl_free(decl);
114         return ret;
115 }
116
117 static int cmd_simplify(char *cmd, char *arg)
118 {
119         struct cdecl *decl;
120         const char *str;
121         int ret = -1;
122
123         decl = cdecl_parse_decl(arg);
124         if (!decl)
125                 goto out;
126
127         for (struct cdecl *i = decl; i; i = i->next) {
128                 struct cdecl_declspec *s = i->specifiers;
129
130                 if (i != decl) {
131                         i->specifiers = NULL;
132                         printf(", ");
133                 }
134
135                 str = do_format(cdecl_declare, i);
136                 i->specifiers = s;
137
138                 if (!str)
139                         goto out;
140
141                 printf("%s", str);
142         }
143
144         putchar('\n');
145
146         ret = 1;
147 out:
148         cdecl_free(decl);
149         return ret;
150 }
151
152 static int cmd_quit(char *cmd, char *arg)
153 {
154         return 0;
155 }
156
157 static int cmd_help(char *cmd, char *arg);
158
159 static const struct command {
160         char name[16];
161         int (*func)(char *cmd, char *arg);
162         const char *blurb;
163 } commands[] = {
164         { "explain",  cmd_explain,  "Explain a C declaration." },
165         { "simplify", cmd_simplify, "Simplify a C declaration." },
166         { "help",     cmd_help,     "Print this list of commands." },
167         { "quit",     cmd_quit,     "Quit the program." },
168         { "exit",     cmd_quit,     NULL }
169 };
170 static const size_t ncommands = sizeof commands / sizeof commands[0];
171
172 static int cmd_help(char *cmd, char *arg)
173 {
174         for (size_t i = 0; i < ncommands; i++) {
175                 if (!commands[i].blurb)
176                         continue;
177
178                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
179         }
180
181         return 1;
182 }
183
184 static int run_command(char *line)
185 {
186         char *cmd = line + strspn(line, " \t");
187         char *arg = cmd  + strcspn(cmd, " \t");
188
189         if (cmd[0] == '\0')
190                 return 1;
191         if (arg[0] != '\0')
192                 *arg++ = '\0';
193
194         for (size_t i = 0; i < ncommands; i++) {
195                 if (strcmp(cmd, commands[i].name) != 0)
196                         continue;
197
198                 return commands[i].func(cmd, arg);
199         }
200
201         fprintf(stderr, "Undefined command: %s\n", cmd);
202         return -1;
203 }
204
205 static int repl(void)
206 {
207         char *line;
208
209         for (; (line = readline("> ")); free(line)) {
210                 if (!run_command(line))
211                         break;
212         }
213
214         free(line);
215         return 0;
216 }
217
218 static int repl_noninteractive(void)
219 {
220         int rc, ret = 0, saved_errno;
221         char *line = NULL;
222         size_t n;
223
224         while (getline(&line, &n, stdin) >= 0) {
225                 char *c = strchr(line, '\n');
226                 if (c)
227                         *c = '\0';
228
229                 rc = run_command(line);
230                 if (rc < 0)
231                         ret = -1;
232                 else if (rc == 0)
233                         break;
234         }
235
236         saved_errno = errno;
237         free(line);
238
239         if (ferror(stdin)) {
240                 errno = saved_errno;
241                 perror("read error");
242                 return -1;
243         }
244
245         return ret;
246 }
247
248 int main(int argc, char **argv)
249 {
250         bool show_intro = true, interactive = true;
251         int opt, rc;
252
253         if (argc > 0)
254                 progname = argv[0];
255
256         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
257                 switch (opt) {
258                 case 'q':
259                         show_intro = false;
260                         break;
261                 case 'b':
262                         interactive = false;
263                         break;
264                 case 'i':
265                         interactive = true;
266                         break;
267                 case 'V':
268                         print_version();
269                         return EXIT_SUCCESS;
270                 case 'H':
271                         print_help();
272                         return EXIT_SUCCESS;
273                 default:
274                         print_usage(stderr);
275                         return EXIT_FAILURE;
276                 }
277         }
278
279         if (interactive && show_intro)
280                 print_version();
281
282         if (interactive)
283                 rc = repl();
284         else
285                 rc = repl_noninteractive();
286
287         if (rc != 0)
288                 return EXIT_FAILURE;
289         return EXIT_SUCCESS;
290 }