2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011-2012, 2020-2024 Nick Bowler
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.
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.
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/>.
43 #if HAVE_READLINE_READLINE_H
44 # include <readline/readline.h>
46 #if HAVE_RL_ADD_HISTORY && HAVE_READLINE_HISTORY_H
47 # include <readline/history.h>
49 /* call add_history only if the line is non-blank */
50 static void do_add_history(const char *line)
52 if (line[strspn(line, " \t")])
56 static void do_add_history(const char *line)
61 static const char *progname = "cdecl99";
62 static bool batch_mode;
64 void print_error(const char *fmt, ...)
68 fprintf(stderr, batch_mode ? "%s: %s" : "%.0s%s",
69 progname, _("error: "));
72 vfprintf(stderr, fmt, ap);
75 fprintf(stderr, "\n");
78 static void print_version(void)
80 do_print_version(PACKAGE_STRING);
83 static void print_usage(FILE *f)
85 fprintf(f, _("Usage: %s [options]\n"), progname);
87 fprintf(f, _("Try %s --help for more information.\n"),
91 static void print_help(const struct option *lopts)
93 struct lopt_help help = {0};
94 const struct option *opt;
98 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
99 "complicated C declarations."));
103 for (opt = lopts; opt->name; opt++) {
104 if (!lopt_get_help(opt, &help))
107 help_print_option(opt, help.arg, help.desc, 20);
111 _("For more information, see the cdecl99(1) man page."));
114 * TRANSLATORS: Please add *another line* indicating where users should
115 * report translation bugs.
117 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
120 static int do_getline(char **linebuf, size_t *n)
124 if ((rc = dx_getline(linebuf, n, stdin)) < 0) {
125 if (rc == DX_GETLINE_ENOMEM)
126 print_error("%s", _("failed to allocate memory"));
128 print_error("%s", strerror(errno));
135 static int do_readline(char **linebuf, size_t *n, bool batch)
143 return do_getline(linebuf, n);
146 return do_getline(linebuf, n);
149 if (!(*linebuf = readline("> ")))
152 do_add_history(*linebuf);
157 static int repl(bool batch)
163 while (do_readline(&line, &n, batch)) {
164 int rc = run_command(line, batch);
172 return fail ? EXIT_FAILURE : 0;
175 static int repl_cmdline(unsigned count, char **commands)
180 for (i = 0; i < count; i++) {
181 int rc = run_command(commands[i], true);
188 return fail ? EXIT_FAILURE : 0;
191 /* Initialize gettext */
192 static void init_i18n(void)
197 setlocale(LC_ALL, "");
198 bindtextdomain(PACKAGE, LOCALEDIR);
203 INIT_EXIT_SUCCESS = -1,
204 INIT_EXIT_FAILURE = -2
207 static int initialize(int argc, char **argv)
209 int i, opt, quiet = 0, execute = 0;
210 const char *filename = NULL;
212 XTRA_PACKED_LOPTS(lopts);
219 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
234 argv[execute++] = optarg;
238 return INIT_EXIT_SUCCESS;
241 return INIT_EXIT_SUCCESS;
244 return INIT_EXIT_FAILURE;
249 fprintf(stderr, "%s: %s", progname,
250 _("excess command-line arguments:"));
251 for (i = optind; i < argc; i++) {
252 fprintf(stderr, " %s", argv[i]);
254 fprintf(stderr, "\n");
256 return INIT_EXIT_FAILURE;
259 /* --filename and --execute imply --batch. */
260 if (filename || execute)
263 /* --batch implies --quiet */
264 if (!batch_mode && !quiet)
267 /* --execute supersedes --filename */
268 if (filename && !execute) {
269 if (!freopen(filename, "r", stdin)) {
270 print_error("failed to open %s: %s", filename,
272 return INIT_EXIT_FAILURE;
279 int main(int argc, char **argv)
283 switch ((execute = initialize(argc, argv))) {
284 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
285 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
289 return repl_cmdline(execute, argv);
291 return repl(batch_mode);