]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Add initial support for non-interactive modes to cdecl99.
[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 static int cmd_explain(char *cmd, char *arg)
59 {
60         static size_t bufsz;
61         static char *buf;
62         int ret = -1;
63
64         struct cdecl *decl;
65         size_t rc;
66
67         decl = cdecl_parse_decl(arg);
68         if (!decl)
69                 goto out;
70
71         for (struct cdecl *i = decl; i; i = i->next) {
72 retry:
73                 rc = cdecl_explain(buf, bufsz, i);
74                 if (rc >= bufsz) {
75                         char *tmp;
76
77                         tmp = realloc(buf, rc + 1);
78                         if (!tmp) {
79                                 fprintf(stderr, "failed to allocate memory\n");
80                                 goto out;
81                         }
82
83                         buf = tmp;
84                         bufsz = rc + 1;
85                         goto retry;
86                 }
87
88                 printf("%s\n", buf);
89         }
90
91         ret = 1;
92 out:
93         cdecl_free(decl);
94         return ret;
95 }
96
97 static int cmd_quit(char *cmd, char *arg)
98 {
99         return 0;
100 }
101
102 static int cmd_help(char *cmd, char *arg);
103
104 static const struct command {
105         char name[16];
106         int (*func)(char *cmd, char *arg);
107         const char *blurb;
108 } commands[] = {
109         { "explain", cmd_explain, "Explain a C declaration." },
110         { "help",    cmd_help,    "Print this list of commands." },
111         { "quit",    cmd_quit,    "Quit the program." },
112         { "exit",    cmd_quit,    NULL }
113 };
114 static const size_t ncommands = sizeof commands / sizeof commands[0];
115
116 static int cmd_help(char *cmd, char *arg)
117 {
118         for (size_t i = 0; i < ncommands; i++) {
119                 if (!commands[i].blurb)
120                         continue;
121
122                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
123         }
124
125         return 1;
126 }
127
128 static int repl(bool interactive)
129 {
130         char *prompt = interactive ? "> " : NULL;
131         int rc, ret = 0;
132         char *line;
133
134         for (; (line = readline(prompt)); free(line)) {
135                 char *cmd = line + strspn(line, " \t");
136                 char *arg = cmd  + strcspn(cmd, " \t");
137
138                 if (cmd[0] == '\0')
139                         continue;
140                 if (arg[0] != '\0')
141                         *arg++ = '\0';
142
143                 for (size_t i = 0; i < ncommands; i++) {
144                         if (strcmp(cmd, commands[i].name) != 0)
145                                 continue;
146
147                         rc = commands[i].func(cmd, arg);
148                         if (!interactive && rc < 0)
149                                 ret = -1;
150                         if (rc == 0)
151                                 goto out;
152                         goto next;
153                 }
154
155                 fprintf(stderr, "Undefined command: %s\n", cmd);
156                 ret = -1;
157         next:
158                 ;
159         }
160 out:
161         free(line);
162         return ret;
163 }
164
165 int main(int argc, char **argv)
166 {
167         bool show_intro = true, interactive = true;
168         int opt;
169
170         if (argc > 0)
171                 progname = argv[0];
172
173         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
174                 switch (opt) {
175                 case 'q':
176                         show_intro = false;
177                         break;
178                 case 'b':
179                         interactive = false;
180                         break;
181                 case 'i':
182                         interactive = true;
183                         break;
184                 case 'V':
185                         print_version();
186                         return EXIT_SUCCESS;
187                 case 'H':
188                         print_help();
189                         return EXIT_SUCCESS;
190                 default:
191                         print_usage(stderr);
192                         return EXIT_FAILURE;
193                 }
194         }
195
196         if (interactive && show_intro)
197                 print_version();
198
199         if (repl(interactive) != 0)
200                 return EXIT_FAILURE;
201
202         return EXIT_SUCCESS;
203 }