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/>.
32 #include <localcharset.h>
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, ...)
68 fprintf(stderr, "%s: ", progname);
69 fprintf(stderr, "%s", _("error: "));
72 vfprintf(stderr, fmt, ap);
75 fprintf(stderr, "\n");
78 static void print_version(void)
80 const char *copysign = copyright_symbol(locale_charset());
83 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
84 puts("License GPLv3+: GNU GPL version 3 or any later version.");
85 puts("This is free software: you are free to change and redistribute it.");
86 puts("There is NO WARRANTY, to the extent permitted by law.");
89 static void print_usage(FILE *f)
91 fprintf(f, _("Usage: %s [options]\n"), progname);
93 fprintf(f, _("Try %s --help for more information.\n"),
97 static void print_help(const struct option *lopts)
99 struct lopt_help help = {0};
100 const struct option *opt;
104 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
105 "complicated C declarations."));
109 for (opt = lopts; opt->name; opt++) {
110 if (!lopt_get_help(opt, &help))
113 help_print_option(opt, help.arg, help.desc, 20);
117 puts(_("For more information, see the cdecl99(1) man page."));
121 * TRANSLATORS: Please add *another line* indicating where users should
122 * report translation bugs.
124 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
127 static int do_getline(char **linebuf, size_t *n)
131 if ((rc = getline(linebuf, n, stdin)) < 0) {
133 print_error("%s", strerror(errno));
137 if (rc-- && (*linebuf)[rc] == '\n')
138 (*linebuf)[rc] = '\0';
142 static int do_readline(char **linebuf, size_t *n, bool batch)
150 return do_getline(linebuf, n);
153 return do_getline(linebuf, n);
156 if (!(*linebuf = readline("> ")))
159 do_add_history(*linebuf);
164 static int repl(bool batch)
170 while (do_readline(&line, &n, batch)) {
171 int rc = run_command(line, batch);
179 return fail ? EXIT_FAILURE : 0;
182 static int repl_cmdline(unsigned count, char **commands)
187 for (i = 0; i < count; i++) {
188 int rc = run_command(commands[i], true);
195 return fail ? EXIT_FAILURE : 0;
198 /* Initialize gettext */
199 static void init_i18n(void)
204 setlocale(LC_ALL, "");
205 bindtextdomain(PACKAGE, LOCALEDIR);
210 INIT_EXIT_SUCCESS = -1,
211 INIT_EXIT_FAILURE = -2
214 static int initialize(int argc, char **argv)
216 int i, opt, quiet = 0, execute = 0;
217 const char *filename = NULL;
219 XTRA_PACKED_LOPTS(lopts);
226 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
241 argv[execute++] = optarg;
245 return INIT_EXIT_SUCCESS;
248 return INIT_EXIT_SUCCESS;
251 return INIT_EXIT_FAILURE;
256 fprintf(stderr, "%s: ", progname);
257 fprintf(stderr, _("excess command-line arguments:"));
258 for (i = optind; i < argc; i++) {
259 fprintf(stderr, " %s", argv[i]);
261 fprintf(stderr, "\n");
263 return INIT_EXIT_FAILURE;
266 /* --filename and --execute imply --batch. */
267 if (filename || execute)
270 /* --batch implies --quiet */
271 if (!batch_mode && !quiet)
274 /* --execute supersedes --filename */
275 if (filename && !execute) {
276 if (!freopen(filename, "r", stdin)) {
277 print_error("failed to open %s: %s", filename,
279 return INIT_EXIT_FAILURE;
286 int main(int argc, char **argv)
290 switch ((execute = initialize(argc, argv))) {
291 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
292 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
296 return repl_cmdline(execute, argv);
298 return repl(batch_mode);