2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011-2012, 2020-2023 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/>.
41 #if HAVE_READLINE_READLINE_H
42 # include <readline/readline.h>
44 #if HAVE_RL_ADD_HISTORY && HAVE_READLINE_HISTORY_H
45 # include <readline/history.h>
47 /* call add_history only if the line is non-blank */
48 static void do_add_history(const char *line)
50 if (line[strspn(line, " \t")])
54 static void do_add_history(const char *line)
59 static const char *progname = "cdecl99";
60 static bool batch_mode;
62 void print_error(const char *fmt, ...)
66 fprintf(stderr, batch_mode ? "%s: %s" : "%.0s%s",
67 progname, _("error: "));
70 vfprintf(stderr, fmt, ap);
73 fprintf(stderr, "\n");
76 static void print_version(void)
78 do_print_version(PACKAGE_STRING);
81 static void print_usage(FILE *f)
83 fprintf(f, _("Usage: %s [options]\n"), progname);
85 fprintf(f, _("Try %s --help for more information.\n"),
89 static void print_help(const struct option *lopts)
91 struct lopt_help help = {0};
92 const struct option *opt;
96 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
97 "complicated C declarations."));
101 for (opt = lopts; opt->name; opt++) {
102 if (!lopt_get_help(opt, &help))
105 help_print_option(opt, help.arg, help.desc, 20);
109 _("For more information, see the cdecl99(1) man page."));
112 * TRANSLATORS: Please add *another line* indicating where users should
113 * report translation bugs.
115 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
118 static int do_getline(char **linebuf, size_t *n)
122 if ((rc = getline(linebuf, n, stdin)) < 0) {
124 print_error("%s", strerror(errno));
128 if (rc-- && (*linebuf)[rc] == '\n')
129 (*linebuf)[rc] = '\0';
133 static int do_readline(char **linebuf, size_t *n, bool batch)
141 return do_getline(linebuf, n);
144 return do_getline(linebuf, n);
147 if (!(*linebuf = readline("> ")))
150 do_add_history(*linebuf);
155 static int repl(bool batch)
161 while (do_readline(&line, &n, batch)) {
162 int rc = run_command(line, batch);
170 return fail ? EXIT_FAILURE : 0;
173 static int repl_cmdline(unsigned count, char **commands)
178 for (i = 0; i < count; i++) {
179 int rc = run_command(commands[i], true);
186 return fail ? EXIT_FAILURE : 0;
189 /* Initialize gettext */
190 static void init_i18n(void)
195 setlocale(LC_ALL, "");
196 bindtextdomain(PACKAGE, LOCALEDIR);
201 INIT_EXIT_SUCCESS = -1,
202 INIT_EXIT_FAILURE = -2
205 static int initialize(int argc, char **argv)
207 int i, opt, quiet = 0, execute = 0;
208 const char *filename = NULL;
210 XTRA_PACKED_LOPTS(lopts);
217 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
232 argv[execute++] = optarg;
236 return INIT_EXIT_SUCCESS;
239 return INIT_EXIT_SUCCESS;
242 return INIT_EXIT_FAILURE;
247 fprintf(stderr, "%s: %s", progname,
248 _("excess command-line arguments:"));
249 for (i = optind; i < argc; i++) {
250 fprintf(stderr, " %s", argv[i]);
252 fprintf(stderr, "\n");
254 return INIT_EXIT_FAILURE;
257 /* --filename and --execute imply --batch. */
258 if (filename || execute)
261 /* --batch implies --quiet */
262 if (!batch_mode && !quiet)
265 /* --execute supersedes --filename */
266 if (filename && !execute) {
267 if (!freopen(filename, "r", stdin)) {
268 print_error("failed to open %s: %s", filename,
270 return INIT_EXIT_FAILURE;
277 int main(int argc, char **argv)
281 switch ((execute = initialize(argc, argv))) {
282 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
283 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
287 return repl_cmdline(execute, argv);
289 return repl(batch_mode);