]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
libcdecl: Rework cdecl_declare output logic.
[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         struct lopt_help help = {0};
83         const struct option *opt;
84
85         print_usage(stdout);
86
87         puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
88                "complicated C declarations."));
89         putchar('\n');
90
91         puts(_("Options:"));
92         for (opt = lopts; opt->name; opt++) {
93                 if (!lopt_get_help(opt, &help))
94                         continue;
95
96                 help_print_option(opt, help.arg, help.desc, 20);
97         }
98         putchar('\n');
99
100         puts(_("For more information, see the cdecl99(1) man page."));
101         putchar('\n');
102
103         /*
104          * TRANSLATORS: Please add *another line* indicating where users should
105          * report translation bugs.
106          */
107         printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
108 }
109
110 static int is_blank_line(const char *line)
111 {
112         return !line[strspn(line, " \t")];
113 }
114
115 static int repl(void)
116 {
117         char *line;
118
119         for (; (line = readline("> ")); free(line)) {
120                 if (!is_blank_line(line))
121                         add_history(line);
122
123                 if (run_command(line, true) > 0)
124                         break;
125         }
126
127         free(line);
128         return 0;
129 }
130
131 static int repl_cmdline(unsigned count, char **commands)
132 {
133         int ret = 0;
134         unsigned i;
135
136         for (i = 0; i < count; i++) {
137                 int rc = run_command(commands[i], false);
138                 if (rc < 0)
139                         ret = -1;
140                 else if (rc > 0)
141                         break;
142         }
143
144         return ret;
145 }
146
147 static int repl_noninteractive(void)
148 {
149         int rc, ret = 0, saved_errno;
150         char *line = NULL;
151         size_t n;
152
153         while (getline(&line, &n, stdin) >= 0) {
154                 char *c = strchr(line, '\n');
155                 if (c)
156                         *c = '\0';
157
158                 rc = run_command(line, false);
159                 if (rc < 0)
160                         ret = -1;
161                 else if (rc > 0)
162                         break;
163         }
164
165         saved_errno = errno;
166         free(line);
167
168         if (ferror(stdin)) {
169                 print_error("%s", strerror(saved_errno));
170                 return -1;
171         }
172
173         return ret;
174 }
175
176 /* Initialize gettext */
177 static void init_i18n(void)
178 {
179         if (!ENABLE_NLS)
180                 return;
181
182         setlocale(LC_ALL, "");
183         bindtextdomain(PACKAGE, LOCALEDIR);
184         textdomain(PACKAGE);
185 }
186
187 enum {
188         INIT_EXIT_SUCCESS = -1,
189         INIT_EXIT_FAILURE = -2
190 };
191
192 static int initialize(int argc, char **argv)
193 {
194         int i, opt, quiet = 0, execute = 0;
195         const char *filename = NULL;
196
197         XTRA_PACKED_LOPTS(lopts);
198
199         if (argc > 0)
200                 progname = argv[0];
201
202         init_i18n();
203
204         while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
205                 switch (opt) {
206                 case 'q':
207                         quiet = 1;
208                         break;
209                 case 'b':
210                         interactive = false;
211                         break;
212                 case 'i':
213                         interactive = true;
214                         break;
215                 case 'f':
216                         filename = optarg;
217                         break;
218                 case 'e':
219                         argv[execute++] = optarg;
220                         break;
221                 case 'V':
222                         print_version();
223                         return INIT_EXIT_SUCCESS;
224                 case 'H':
225                         print_help(lopts);
226                         return INIT_EXIT_SUCCESS;
227                 default:
228                         print_usage(stderr);
229                         return INIT_EXIT_FAILURE;
230                 }
231         }
232
233         if (optind < argc) {
234                 fprintf(stderr, "%s: ", progname);
235                 fprintf(stderr, _("excess command-line arguments:"));
236                 for (i = optind; i < argc; i++) {
237                         fprintf(stderr, " %s", argv[i]);
238                 }
239                 fprintf(stderr, "\n");
240                 print_usage(stderr);
241                 return INIT_EXIT_FAILURE;
242         }
243
244         /* --filename and --execute imply --batch. */
245         if (filename || execute)
246                 interactive = false;
247
248         /* --batch implies --quiet */
249         if (interactive && !quiet)
250                 print_version();
251
252         /* --execute supersedes --filename */
253         if (filename && !execute) {
254                 if (!freopen(filename, "r", stdin)) {
255                         print_error("failed to open %s: %s", filename,
256                                                         strerror(errno));
257                         return INIT_EXIT_FAILURE;
258                 }
259         }
260
261         return execute;
262 }
263
264 int main(int argc, char **argv)
265 {
266         int rc, execute;
267
268         switch ((execute = initialize(argc, argv))) {
269         case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
270         case INIT_EXIT_FAILURE: return EXIT_FAILURE;
271         }
272
273         if (interactive)
274                 rc = repl();
275         else if (execute)
276                 rc = repl_cmdline(execute, argv);
277         else
278                 rc = repl_noninteractive();
279
280         if (rc != 0)
281                 return EXIT_FAILURE;
282         return EXIT_SUCCESS;
283 }