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/>.
33 #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 = copyright_symbol(locale_charset());
71 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
72 puts("License GPLv3+: GNU GPL version 3 or any later version.");
73 puts("This is free software: you are free to change and redistribute it.");
74 puts("There is NO WARRANTY, to the extent permitted by law.");
77 static void print_usage(FILE *f)
79 fprintf(f, _("Usage: %s [options]\n"), progname);
81 fprintf(f, _("Try %s --help for more information.\n"),
85 static void print_help(void)
87 const struct option *opt;
91 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
92 "complicated C declarations."));
96 for (opt = lopts; opt->name; opt++) {
97 struct lopt_help help;
99 if (!lopt_get_help(opt, &help))
102 help_print_option(opt, help.arg, help.desc, 20);
106 puts(_("For more information, see the cdecl99(1) man page."));
110 * TRANSLATORS: Please add *another line* indicating where users should
111 * report translation bugs.
113 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
116 static int is_blank_line(const char *line)
118 return !line[strspn(line, " \t")];
121 static int repl(void)
125 for (; (line = readline("> ")); free(line)) {
126 if (!is_blank_line(line))
129 if (run_command(line, true) > 0)
137 static int repl_cmdline(unsigned count, char **commands)
142 for (i = 0; i < count; i++) {
143 int rc = run_command(commands[i], false);
153 static int repl_noninteractive(void)
155 int rc, ret = 0, saved_errno;
159 while (getline(&line, &n, stdin) >= 0) {
160 char *c = strchr(line, '\n');
164 rc = run_command(line, false);
175 print_error("%s", strerror(saved_errno));
182 /* Initialize gettext */
183 static void init_i18n(void)
188 setlocale(LC_ALL, "");
189 bindtextdomain(PACKAGE, LOCALEDIR);
193 int main(int argc, char **argv)
195 bool show_intro = true;
196 const char *filename = NULL;
197 unsigned execute = 0;
205 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
220 argv[execute++] = optarg;
235 fprintf(stderr, "%s: ", progname);
236 fprintf(stderr, _("excess command-line arguments:"));
237 for (i = optind; i < argc; i++) {
238 fprintf(stderr, " %s", argv[i]);
240 fprintf(stderr, "\n");
245 /* --filename and --execute imply --batch. */
246 if (filename || execute)
249 /* --batch implies --quiet */
250 if (interactive && show_intro)
253 /* --execute supersedes --filename */
254 if (filename && !execute) {
255 if (!freopen(filename, "r", stdin)) {
256 print_error("failed to open %s: %s", filename,
265 rc = repl_cmdline(execute, argv);
267 rc = repl_noninteractive();