2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011-2012, 2020-2021 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 2021 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 bool is_blank_line(const char *line)
127 for (size_t i = 0; line[i]; i++) {
128 if (!isblank((unsigned char)line[i]))
135 static int repl(void)
139 for (; (line = readline("> ")); free(line)) {
140 if (!is_blank_line(line))
143 if (run_command(line, true) > 0)
151 static int repl_cmdline(int argc, char **argv)
153 int opt, rc, ret = 0;
156 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
160 rc = run_command(optarg, false);
170 static int repl_noninteractive(void)
172 int rc, ret = 0, saved_errno;
176 while (getline(&line, &n, stdin) >= 0) {
177 char *c = strchr(line, '\n');
181 rc = run_command(line, false);
192 print_error("%s", strerror(saved_errno));
199 /* Initialize gettext */
200 static void init_i18n(void)
205 setlocale(LC_ALL, "");
206 bindtextdomain(PACKAGE, LOCALEDIR);
210 int main(int argc, char **argv)
212 bool show_intro = true, execute = false;
213 const char *filename = NULL;
221 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
251 fprintf(stderr, "%s: ", progname);
253 fprintf(stderr, _("excess command-line arguments:"),
255 for (i = optind; i < argc; i++) {
256 fprintf(stderr, " %s", argv[i]);
258 fprintf(stderr, "\n");
263 /* --filename and --execute imply --batch. */
264 if (filename || execute)
267 /* --batch implies --quiet */
268 if (interactive && show_intro)
271 /* --execute supersedes --filename */
272 if (filename && !execute) {
273 if (!freopen(filename, "r", stdin)) {
274 print_error("failed to open %s: %s\n", filename,
283 rc = repl_cmdline(argc, argv);
285 rc = repl_noninteractive();