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>
46 static const char *progname = "cdecl99";
47 static bool interactive = true;
49 void print_error(const char *fmt, ...)
54 fprintf(stderr, "%s: ", progname);
55 fprintf(stderr, "%s", _("error: "));
58 vfprintf(stderr, fmt, ap);
61 fprintf(stderr, "\n");
64 static void print_version(void)
66 const char *copysign = copyright_symbol(locale_charset());
69 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
70 puts("License GPLv3+: GNU GPL version 3 or any later version.");
71 puts("This is free software: you are free to change and redistribute it.");
72 puts("There is NO WARRANTY, to the extent permitted by law.");
75 static void print_usage(FILE *f)
77 fprintf(f, _("Usage: %s [options]\n"), progname);
79 fprintf(f, _("Try %s --help for more information.\n"),
83 static void print_help(const struct option *lopts)
85 struct lopt_help help = {0};
86 const struct option *opt;
90 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
91 "complicated C declarations."));
95 for (opt = lopts; opt->name; opt++) {
96 if (!lopt_get_help(opt, &help))
99 help_print_option(opt, help.arg, help.desc, 20);
103 puts(_("For more information, see the cdecl99(1) man page."));
107 * TRANSLATORS: Please add *another line* indicating where users should
108 * report translation bugs.
110 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
113 static int is_blank_line(const char *line)
115 return !line[strspn(line, " \t")];
118 static int repl_cmdline(unsigned count, char **commands)
123 for (i = 0; i < count; i++) {
124 int rc = run_command(commands[i], false);
134 static int do_getline(char **linebuf, size_t *n)
138 if ((rc = getline(linebuf, n, stdin)) < 0) {
140 print_error("%s", strerror(errno));
144 if (rc-- && (*linebuf)[rc] == '\n')
145 (*linebuf)[rc] = '\0';
149 static int do_readline(char **linebuf, size_t *n, int interactive)
157 return do_getline(linebuf, n);
160 return do_getline(linebuf, n);
163 if (!(*linebuf = readline("> ")))
166 if (!is_blank_line(*linebuf))
167 add_history(*linebuf);
172 static int repl(int interactive)
178 while (do_readline(&line, &n, interactive)) {
179 int rc = run_command(line, interactive);
190 /* Initialize gettext */
191 static void init_i18n(void)
196 setlocale(LC_ALL, "");
197 bindtextdomain(PACKAGE, LOCALEDIR);
202 INIT_EXIT_SUCCESS = -1,
203 INIT_EXIT_FAILURE = -2
206 static int initialize(int argc, char **argv)
208 int i, opt, quiet = 0, execute = 0;
209 const char *filename = NULL;
211 XTRA_PACKED_LOPTS(lopts);
218 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
233 argv[execute++] = optarg;
237 return INIT_EXIT_SUCCESS;
240 return INIT_EXIT_SUCCESS;
243 return INIT_EXIT_FAILURE;
248 fprintf(stderr, "%s: ", progname);
249 fprintf(stderr, _("excess command-line arguments:"));
250 for (i = optind; i < argc; i++) {
251 fprintf(stderr, " %s", argv[i]);
253 fprintf(stderr, "\n");
255 return INIT_EXIT_FAILURE;
258 /* --filename and --execute imply --batch. */
259 if (filename || execute)
262 /* --batch implies --quiet */
263 if (interactive && !quiet)
266 /* --execute supersedes --filename */
267 if (filename && !execute) {
268 if (!freopen(filename, "r", stdin)) {
269 print_error("failed to open %s: %s", filename,
271 return INIT_EXIT_FAILURE;
278 int main(int argc, char **argv)
282 switch ((execute = initialize(argc, argv))) {
283 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
284 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
288 rc = repl_cmdline(execute, argv);
290 rc = repl(interactive);