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