]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
eac0023731ce9838342dd286032ad876c36463b4
[cdecl99.git] / src / cdecl99.c
1 /*
2  * Command line utility for making sense of C declarations.
3  * Copyright © 2011-2012, 2020-2023 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 <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <assert.h>
28 #include <stdarg.h>
29
30 #include <getopt.h>
31 #include <gettext.h>
32 #include <readline.h>
33 #include <localcharset.h>
34 #include <mbswidth.h>
35
36 #include "cdecl99.h"
37 #include "cdecl.h"
38 #include "help.h"
39 #include "xtra.h"
40 #include "copysym.h"
41 #include "options.h"
42
43 static const char *progname = "cdecl99";
44 static bool interactive = true;
45
46 void print_error(const char *fmt, ...)
47 {
48         va_list(ap);
49
50         if (!interactive)
51                 fprintf(stderr, "%s: ", progname);
52         fprintf(stderr, "%s", _("error: "));
53
54         va_start(ap, fmt);
55         vfprintf(stderr, fmt, ap);
56         va_end(ap);
57
58         fprintf(stderr, "\n");
59 }
60
61 static void print_version(void)
62 {
63         const char *copysign = copyright_symbol(locale_charset());
64
65         puts(PACKAGE_STRING);
66         printf("Copyright %s 2023 Nick Bowler.\n", copysign);
67         puts("License GPLv3+: GNU GPL version 3 or any later version.");
68         puts("This is free software: you are free to change and redistribute it.");
69         puts("There is NO WARRANTY, to the extent permitted by law.");
70 }
71
72 static void print_usage(FILE *f)
73 {
74         fprintf(f, _("Usage: %s [options]\n"), progname);
75         if (f != stdout)
76                 fprintf(f, _("Try %s --help for more information.\n"),
77                            progname);
78 }
79
80 static void print_help(const struct option *lopts)
81 {
82         const struct option *opt;
83
84         print_usage(stdout);
85
86         puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
87                "complicated C declarations."));
88         putchar('\n');
89
90         puts(_("Options:"));
91         for (opt = lopts; opt->name; opt++) {
92                 struct lopt_help help;
93
94                 if (!lopt_get_help(opt, &help))
95                         continue;
96
97                 help_print_option(opt, help.arg, help.desc, 20);
98         }
99         putchar('\n');
100
101         puts(_("For more information, see the cdecl99(1) man page."));
102         putchar('\n');
103
104         /*
105          * TRANSLATORS: Please add *another line* indicating where users should
106          * report translation bugs.
107          */
108         printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
109 }
110
111 static int is_blank_line(const char *line)
112 {
113         return !line[strspn(line, " \t")];
114 }
115
116 static int repl(void)
117 {
118         char *line;
119
120         for (; (line = readline("> ")); free(line)) {
121                 if (!is_blank_line(line))
122                         add_history(line);
123
124                 if (run_command(line, true) > 0)
125                         break;
126         }
127
128         free(line);
129         return 0;
130 }
131
132 static int repl_cmdline(unsigned count, char **commands)
133 {
134         int ret = 0;
135         unsigned i;
136
137         for (i = 0; i < count; i++) {
138                 int rc = run_command(commands[i], false);
139                 if (rc < 0)
140                         ret = -1;
141                 else if (rc > 0)
142                         break;
143         }
144
145         return ret;
146 }
147
148 static int repl_noninteractive(void)
149 {
150         int rc, ret = 0, saved_errno;
151         char *line = NULL;
152         size_t n;
153
154         while (getline(&line, &n, stdin) >= 0) {
155                 char *c = strchr(line, '\n');
156                 if (c)
157                         *c = '\0';
158
159                 rc = run_command(line, false);
160                 if (rc < 0)
161                         ret = -1;
162                 else if (rc > 0)
163                         break;
164         }
165
166         saved_errno = errno;
167         free(line);
168
169         if (ferror(stdin)) {
170                 print_error("%s", strerror(saved_errno));
171                 return -1;
172         }
173
174         return ret;
175 }
176
177 /* Initialize gettext */
178 static void init_i18n(void)
179 {
180         if (!ENABLE_NLS)
181                 return;
182
183         setlocale(LC_ALL, "");
184         bindtextdomain(PACKAGE, LOCALEDIR);
185         textdomain(PACKAGE);
186 }
187
188 enum {
189         INIT_EXIT_SUCCESS = -1,
190         INIT_EXIT_FAILURE = -2
191 };
192
193 static int initialize(int argc, char **argv)
194 {
195         int i, opt, quiet = 0, execute = 0;
196         const char *filename = NULL;
197
198         XTRA_PACKED_LOPTS(lopts);
199
200         if (argc > 0)
201                 progname = argv[0];
202
203         init_i18n();
204
205         while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
206                 switch (opt) {
207                 case 'q':
208                         quiet = 1;
209                         break;
210                 case 'b':
211                         interactive = false;
212                         break;
213                 case 'i':
214                         interactive = true;
215                         break;
216                 case 'f':
217                         filename = optarg;
218                         break;
219                 case 'e':
220                         argv[execute++] = optarg;
221                         break;
222                 case 'V':
223                         print_version();
224                         return INIT_EXIT_SUCCESS;
225                 case 'H':
226                         print_help(lopts);
227                         return INIT_EXIT_SUCCESS;
228                 default:
229                         print_usage(stderr);
230                         return INIT_EXIT_FAILURE;
231                 }
232         }
233
234         if (optind < argc) {
235                 fprintf(stderr, "%s: ", progname);
236                 fprintf(stderr, _("excess command-line arguments:"));
237                 for (i = optind; i < argc; i++) {
238                         fprintf(stderr, " %s", argv[i]);
239                 }
240                 fprintf(stderr, "\n");
241                 print_usage(stderr);
242                 return INIT_EXIT_FAILURE;
243         }
244
245         /* --filename and --execute imply --batch. */
246         if (filename || execute)
247                 interactive = false;
248
249         /* --batch implies --quiet */
250         if (interactive && !quiet)
251                 print_version();
252
253         /* --execute supersedes --filename */
254         if (filename && !execute) {
255                 if (!freopen(filename, "r", stdin)) {
256                         print_error("failed to open %s: %s", filename,
257                                                         strerror(errno));
258                         return INIT_EXIT_FAILURE;
259                 }
260         }
261
262         return execute;
263 }
264
265 int main(int argc, char **argv)
266 {
267         int rc, execute;
268
269         switch ((execute = initialize(argc, argv))) {
270         case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
271         case INIT_EXIT_FAILURE: return EXIT_FAILURE;
272         }
273
274         if (interactive)
275                 rc = repl();
276         else if (execute)
277                 rc = repl_cmdline(execute, argv);
278         else
279                 rc = repl_noninteractive();
280
281         if (rc != 0)
282                 return EXIT_FAILURE;
283         return EXIT_SUCCESS;
284 }