]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Add support for outputting declarations as C code.
[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 <getopt.h>
24 #include "readline.h"
25 #include "cdecl.h"
26
27 static const char *progname = "cdecl99";
28 static const char sopts[] = "qbiVH";
29 static const struct option lopts[] = {
30         { "quiet",       0, NULL, 'q' },
31         { "batch",       0, NULL, 'b' },
32         { "interactive", 0, NULL, 'i' },
33         { "version",     0, NULL, 'V' },
34         { "help",        0, NULL, 'H' },
35         { 0 }
36 };
37
38 static void print_version(void)
39 {
40         puts(PACKAGE_STRING);
41         puts("Copyright (C) 2011 Nick Bowler.");
42         puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
43         puts("This is free software: you are free to change and redistribute it.");
44         puts("There is NO WARRANTY, to the extent permitted by law.");
45 }
46
47 static void print_usage(FILE *f)
48 {
49         fprintf(f, "Usage: %s [options]\n", progname);
50 }
51
52 static void print_help(void)
53 {
54         print_usage(stdout);
55         puts("Detailed help coming soon.");
56 }
57
58 /*
59  * Format a declaration according to the given function and return a pointer
60  * to the formatted string.  The returned pointer remains valid until the
61  * next call, after which it must not be re-used.
62  *
63  * Returns NULL on failure.
64  */
65 static const char *
66 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
67 {
68         static size_t bufsz;
69         static char *buf;
70
71         size_t rc;
72
73 retry:
74         rc = func(buf, bufsz, decl);
75         if (rc >= bufsz) {
76                 char *tmp;
77
78                 tmp = realloc(buf, rc + 1);
79                 if (!tmp) {
80                         fprintf(stderr, "failed to allocate memory\n");
81                         return NULL;
82                 }
83
84                 buf = tmp;
85                 bufsz = rc + 1;
86                 goto retry;
87         }
88
89         return buf;
90 }
91
92 static int cmd_explain(char *cmd, char *arg)
93 {
94         struct cdecl *decl;
95         const char *str;
96         int ret = -1;
97
98         decl = cdecl_parse_decl(arg);
99         if (!decl)
100                 goto out;
101
102         for (struct cdecl *i = decl; i; i = i->next) {
103                 str = do_format(cdecl_explain, i);
104                 if (!str)
105                         goto out;
106
107                 printf("%s\n", str);
108         }
109
110         ret = 1;
111 out:
112         cdecl_free(decl);
113         return ret;
114 }
115
116 static int cmd_simplify(char *cmd, char *arg)
117 {
118         struct cdecl *decl;
119         const char *str;
120         int ret = -1;
121
122         decl = cdecl_parse_decl(arg);
123         if (!decl)
124                 goto out;
125
126         for (struct cdecl *i = decl; i; i = i->next) {
127                 struct cdecl_declspec *s = i->specifiers;
128
129                 if (i != decl) {
130                         i->specifiers = NULL;
131                         printf(", ");
132                 }
133
134                 str = do_format(cdecl_declare, i);
135                 i->specifiers = s;
136
137                 if (!str)
138                         goto out;
139
140                 printf("%s", str);
141         }
142
143         putchar('\n');
144
145         ret = 1;
146 out:
147         cdecl_free(decl);
148         return ret;
149 }
150
151 static int cmd_quit(char *cmd, char *arg)
152 {
153         return 0;
154 }
155
156 static int cmd_help(char *cmd, char *arg);
157
158 static const struct command {
159         char name[16];
160         int (*func)(char *cmd, char *arg);
161         const char *blurb;
162 } commands[] = {
163         { "explain",  cmd_explain,  "Explain a C declaration." },
164         { "simplify", cmd_simplify, "Simplify a C declaration." },
165         { "help",     cmd_help,     "Print this list of commands." },
166         { "quit",     cmd_quit,     "Quit the program." },
167         { "exit",     cmd_quit,     NULL }
168 };
169 static const size_t ncommands = sizeof commands / sizeof commands[0];
170
171 static int cmd_help(char *cmd, char *arg)
172 {
173         for (size_t i = 0; i < ncommands; i++) {
174                 if (!commands[i].blurb)
175                         continue;
176
177                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
178         }
179
180         return 1;
181 }
182
183 static int repl(bool interactive)
184 {
185         char *prompt = interactive ? "> " : NULL;
186         int rc, ret = 0;
187         char *line;
188
189         for (; (line = readline(prompt)); free(line)) {
190                 char *cmd = line + strspn(line, " \t");
191                 char *arg = cmd  + strcspn(cmd, " \t");
192
193                 if (cmd[0] == '\0')
194                         continue;
195                 if (arg[0] != '\0')
196                         *arg++ = '\0';
197
198                 for (size_t i = 0; i < ncommands; i++) {
199                         if (strcmp(cmd, commands[i].name) != 0)
200                                 continue;
201
202                         rc = commands[i].func(cmd, arg);
203                         if (!interactive && rc < 0)
204                                 ret = -1;
205                         if (rc == 0)
206                                 goto out;
207                         goto next;
208                 }
209
210                 fprintf(stderr, "Undefined command: %s\n", cmd);
211                 ret = -1;
212         next:
213                 ;
214         }
215 out:
216         free(line);
217         return ret;
218 }
219
220 int main(int argc, char **argv)
221 {
222         bool show_intro = true, interactive = true;
223         int opt;
224
225         if (argc > 0)
226                 progname = argv[0];
227
228         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
229                 switch (opt) {
230                 case 'q':
231                         show_intro = false;
232                         break;
233                 case 'b':
234                         interactive = false;
235                         break;
236                 case 'i':
237                         interactive = true;
238                         break;
239                 case 'V':
240                         print_version();
241                         return EXIT_SUCCESS;
242                 case 'H':
243                         print_help();
244                         return EXIT_SUCCESS;
245                 default:
246                         print_usage(stderr);
247                         return EXIT_FAILURE;
248                 }
249         }
250
251         if (interactive && show_intro)
252                 print_version();
253
254         if (repl(interactive) != 0)
255                 return EXIT_FAILURE;
256
257         return EXIT_SUCCESS;
258 }