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/>.
42 #if HAVE_READLINE_READLINE_H
43 # include <readline/readline.h>
45 #if HAVE_RL_ADD_HISTORY && HAVE_READLINE_HISTORY_H
46 # include <readline/history.h>
48 /* call add_history only if the line is non-blank */
49 static void do_add_history(const char *line)
51 if (line[strspn(line, " \t")])
55 static void do_add_history(const char *line)
60 static const char *progname = "cdecl99";
61 static bool batch_mode;
63 void print_error(const char *fmt, ...)
67 fprintf(stderr, batch_mode ? "%s: %s" : "%.0s%s",
68 progname, _("error: "));
71 vfprintf(stderr, fmt, ap);
74 fprintf(stderr, "\n");
77 static void print_version(void)
79 do_print_version(PACKAGE_STRING);
82 static void print_usage(FILE *f)
84 fprintf(f, _("Usage: %s [options]\n"), progname);
86 fprintf(f, _("Try %s --help for more information.\n"),
90 static void print_help(const struct option *lopts)
92 struct lopt_help help = {0};
93 const struct option *opt;
97 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
98 "complicated C declarations."));
102 for (opt = lopts; opt->name; opt++) {
103 if (!lopt_get_help(opt, &help))
106 help_print_option(opt, help.arg, help.desc, 20);
110 _("For more information, see the cdecl99(1) man page."));
113 * TRANSLATORS: Please add *another line* indicating where users should
114 * report translation bugs.
116 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
119 static int do_readline(char **linebuf, size_t *n, bool batch)
127 return do_getline(linebuf, n);
130 return do_getline(linebuf, n);
133 if (!(*linebuf = readline("> ")))
136 do_add_history(*linebuf);
141 static int repl(bool batch)
147 while (do_readline(&line, &n, batch)) {
148 int rc = run_command(line, batch);
156 return fail ? EXIT_FAILURE : 0;
159 static int repl_cmdline(unsigned count, char **commands)
164 for (i = 0; i < count; i++) {
165 int rc = run_command(commands[i], true);
172 return fail ? EXIT_FAILURE : 0;
175 /* Initialize gettext */
176 static void init_i18n(void)
181 setlocale(LC_ALL, "");
182 bindtextdomain(PACKAGE, LOCALEDIR);
187 INIT_EXIT_SUCCESS = -1,
188 INIT_EXIT_FAILURE = -2
191 static int initialize(int argc, char **argv)
193 int i, opt, quiet = 0, execute = 0;
194 const char *filename = NULL;
196 XTRA_PACKED_LOPTS(lopts);
203 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
218 argv[execute++] = optarg;
222 return INIT_EXIT_SUCCESS;
225 return INIT_EXIT_SUCCESS;
228 return INIT_EXIT_FAILURE;
233 fprintf(stderr, "%s: %s", progname,
234 _("excess command-line arguments:"));
235 for (i = optind; i < argc; i++) {
236 fprintf(stderr, " %s", argv[i]);
238 fprintf(stderr, "\n");
240 return INIT_EXIT_FAILURE;
243 /* --filename and --execute imply --batch. */
244 if (filename || execute)
247 /* --batch implies --quiet */
248 if (!batch_mode && !quiet)
251 /* --execute supersedes --filename */
252 if (filename && !execute) {
253 if (!freopen(filename, "r", stdin)) {
254 print_error("failed to open %s: %s", filename,
256 return INIT_EXIT_FAILURE;
263 int main(int argc, char **argv)
267 switch ((execute = initialize(argc, argv))) {
268 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
269 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
273 return repl_cmdline(execute, argv);
275 return repl(batch_mode);