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>
40 static const char *progname = "cdecl99";
41 static bool interactive = true;
44 static const char sopts[] = SOPT_STRING;
45 static const struct option lopts[] = {
50 void print_error(const char *fmt, ...)
55 fprintf(stderr, "%s: ", progname);
56 fprintf(stderr, "%s", _("error: "));
59 vfprintf(stderr, fmt, ap);
62 fprintf(stderr, "\n");
65 static void print_version(void)
67 const char *copysign = "(C)";
68 void *convsign = NULL;
71 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
77 printf("Copyright %s 2021 Nick Bowler.\n", copysign);
78 puts("License GPLv3+: GNU GPL version 3 or any later version.");
79 puts("This is free software: you are free to change and redistribute it.");
80 puts("There is NO WARRANTY, to the extent permitted by law.");
85 static void print_usage(FILE *f)
87 fprintf(f, _("Usage: %s [options]\n"), progname);
89 fprintf(f, _("Try %s --help for more information.\n"),
94 print_optstring(const struct option *opt, const struct lopt_help *help)
103 w = snprintf(optstring, sizeof optstring,
104 _(" -%c, --%s=%s"), opt->val, opt->name,
105 pgettext_expr(opt->name, help->arg));
107 w = snprintf(optstring, sizeof optstring,
108 _(" -%c, --%s"), opt->val, opt->name);
114 w = mbsnwidth(optstring, w, 0);
115 printf("%s", optstring);
120 w = printf(" -%c, --%s=%s", opt->val, opt->name, help->arg);
122 w = printf(" -%c, --%s", opt->val, opt->name);
125 if (w < 0 || w > 18) {
134 * Print a string, with each line indented by i spaces. The first line
135 * will be indented by w fewer spaces (to account for the cursor being in
136 * some other column).
138 void print_block(const char *s, int i, int w)
141 const char *nl = strchr(s, '\n');
142 int n = (nl ? nl-s : -1);
144 printf("%*s%.*s\n", i-w, "", n, s);
152 static void print_help(void)
154 const struct option *opt;
158 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
159 "complicated C declarations."));
163 for (opt = lopts; opt->name; opt++) {
164 struct lopt_help help;
167 if (!lopt_get_help(opt, &help))
170 w = print_optstring(opt, &help);
173 help.desc = pgettext_expr(opt->name, help.desc);
175 print_block(help.desc, 20, w);
179 puts(_("For more information, see the cdecl99(1) man page."));
183 * TRANSLATORS: Please add *another line* indicating where users should
184 * report translation bugs.
186 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
189 static bool is_blank_line(const char *line)
191 for (size_t i = 0; line[i]; i++) {
192 if (!isblank((unsigned char)line[i]))
199 static int repl(void)
203 for (; (line = readline("> ")); free(line)) {
204 if (!is_blank_line(line))
207 if (run_command(line, true) > 0)
215 static int repl_cmdline(int argc, char **argv)
217 int opt, rc, ret = 0;
220 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
224 rc = run_command(optarg, false);
234 static int repl_noninteractive(void)
236 int rc, ret = 0, saved_errno;
240 while (getline(&line, &n, stdin) >= 0) {
241 char *c = strchr(line, '\n');
245 rc = run_command(line, false);
256 print_error("%s", strerror(saved_errno));
263 /* Initialize gettext */
264 static void init_i18n(void)
269 setlocale(LC_ALL, "");
270 bindtextdomain(PACKAGE, LOCALEDIR);
274 int main(int argc, char **argv)
276 bool show_intro = true, execute = false;
277 const char *filename = NULL;
285 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
315 fprintf(stderr, "%s: ", progname);
317 fprintf(stderr, _("excess command-line arguments:"),
319 for (i = optind; i < argc; i++) {
320 fprintf(stderr, " %s", argv[i]);
322 fprintf(stderr, "\n");
327 /* --filename and --execute imply --batch. */
328 if (filename || execute)
331 /* --batch implies --quiet */
332 if (interactive && show_intro)
335 /* --execute supersedes --filename */
336 if (filename && !execute) {
337 if (!freopen(filename, "r", stdin)) {
338 print_error("failed to open %s: %s\n", filename,
347 rc = repl_cmdline(argc, argv);
349 rc = repl_noninteractive();