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/>.
34 #include <localcharset.h>
41 static const char *progname = "cdecl99";
42 static bool interactive = true;
45 static const char sopts[] = SOPT_STRING;
46 static const struct option lopts[] = {
51 void print_error(const char *fmt, ...)
56 fprintf(stderr, "%s: ", progname);
57 fprintf(stderr, "%s", _("error: "));
60 vfprintf(stderr, fmt, ap);
63 fprintf(stderr, "\n");
66 static void print_version(void)
68 const char *copysign = "(C)";
69 void *convsign = NULL;
72 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
78 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
79 puts("License GPLv3+: GNU GPL version 3 or any later version.");
80 puts("This is free software: you are free to change and redistribute it.");
81 puts("There is NO WARRANTY, to the extent permitted by law.");
86 static void print_usage(FILE *f)
88 fprintf(f, _("Usage: %s [options]\n"), progname);
90 fprintf(f, _("Try %s --help for more information.\n"),
94 static void print_help(void)
96 const struct option *opt;
100 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
101 "complicated C declarations."));
105 for (opt = lopts; opt->name; opt++) {
106 struct lopt_help help;
108 if (!lopt_get_help(opt, &help))
111 help_print_option(opt, help.arg, help.desc, 20);
115 puts(_("For more information, see the cdecl99(1) man page."));
119 * TRANSLATORS: Please add *another line* indicating where users should
120 * report translation bugs.
122 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
125 static int is_blank_line(const char *line)
127 return !line[strspn(line, " \t")];
130 static int repl(void)
134 for (; (line = readline("> ")); free(line)) {
135 if (!is_blank_line(line))
138 if (run_command(line, true) > 0)
146 static int repl_cmdline(unsigned count, char **commands)
151 for (i = 0; i < count; i++) {
152 int rc = run_command(commands[i], false);
162 static int repl_noninteractive(void)
164 int rc, ret = 0, saved_errno;
168 while (getline(&line, &n, stdin) >= 0) {
169 char *c = strchr(line, '\n');
173 rc = run_command(line, false);
184 print_error("%s", strerror(saved_errno));
191 /* Initialize gettext */
192 static void init_i18n(void)
197 setlocale(LC_ALL, "");
198 bindtextdomain(PACKAGE, LOCALEDIR);
202 int main(int argc, char **argv)
204 bool show_intro = true;
205 const char *filename = NULL;
206 unsigned execute = 0;
214 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
229 argv[execute++] = optarg;
244 fprintf(stderr, "%s: ", progname);
245 fprintf(stderr, _("excess command-line arguments:"));
246 for (i = optind; i < argc; i++) {
247 fprintf(stderr, " %s", argv[i]);
249 fprintf(stderr, "\n");
254 /* --filename and --execute imply --batch. */
255 if (filename || execute)
258 /* --batch implies --quiet */
259 if (interactive && show_intro)
262 /* --execute supersedes --filename */
263 if (filename && !execute) {
264 if (!freopen(filename, "r", stdin)) {
265 print_error("failed to open %s: %s", filename,
274 rc = repl_cmdline(execute, argv);
276 rc = repl_noninteractive();