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/>.
33 #include <localcharset.h>
39 static const char *progname = "cdecl99";
42 static const char sopts[] = SOPT_STRING;
43 static const struct option lopts[] = {
48 static void print_version(void)
50 const char *copysign = "(C)";
51 void *convsign = NULL;
54 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
60 printf("Copyright %s 2021 Nick Bowler.\n", copysign);
61 puts("License GPLv3+: GNU GPL version 3 or any later version.");
62 puts("This is free software: you are free to change and redistribute it.");
63 puts("There is NO WARRANTY, to the extent permitted by law.");
68 static void print_usage(FILE *f)
70 fprintf(f, _("Usage: %s [options]\n"), progname);
72 fprintf(f, _("Try %s --help for more information.\n"),
77 print_optstring(const struct option *opt, const struct lopt_help *help)
86 w = snprintf(optstring, sizeof optstring,
87 _(" -%c, --%s=%s"), opt->val, opt->name,
88 pgettext_expr(opt->name, help->arg));
90 w = snprintf(optstring, sizeof optstring,
91 _(" -%c, --%s"), opt->val, opt->name);
97 w = mbsnwidth(optstring, w, 0);
98 printf("%s", optstring);
103 w = printf(" -%c, --%s=%s", opt->val, opt->name, help->arg);
105 w = printf(" -%c, --%s", opt->val, opt->name);
108 if (w < 0 || w > 18) {
117 * Print a string, with each line indented by i spaces. The first line
118 * will be indented by w fewer spaces (to account for the cursor being in
119 * some other column).
121 void print_block(const char *s, int i, int w)
124 const char *nl = strchr(s, '\n');
125 int n = (nl ? nl-s : -1);
127 printf("%*s%.*s\n", i-w, "", n, s);
135 static void print_help(void)
137 const struct option *opt;
141 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
142 "complicated C declarations."));
146 for (opt = lopts; opt->name; opt++) {
147 struct lopt_help help;
151 if (!lopt_get_help(opt, &help))
154 w = print_optstring(opt, &help);
157 help.desc = pgettext_expr(opt->name, help.desc);
159 print_block(help.desc, 20, w);
163 puts(_("For more information, see the cdecl99(1) man page."));
167 * TRANSLATORS: Please add *another line* indicating where users should
168 * report translation bugs.
170 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
173 static bool is_blank_line(const char *line)
175 for (size_t i = 0; line[i]; i++) {
176 if (!isblank((unsigned char)line[i]))
183 static int repl(void)
187 for (; (line = readline("> ")); free(line)) {
188 if (!is_blank_line(line))
191 if (run_command(line, true) > 0)
199 static int repl_cmdline(int argc, char **argv)
201 int opt, rc, ret = 0;
204 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
208 rc = run_command(optarg, false);
218 static int repl_noninteractive(void)
220 int rc, ret = 0, saved_errno;
224 while (getline(&line, &n, stdin) >= 0) {
225 char *c = strchr(line, '\n');
229 rc = run_command(line, false);
241 perror("read error");
248 /* Initialize gettext */
249 static void init_i18n(void)
254 setlocale(LC_ALL, "");
255 bindtextdomain(PACKAGE, LOCALEDIR);
259 int main(int argc, char **argv)
261 bool show_intro = true, interactive = true, execute = false;
262 const char *filename = NULL;
270 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
300 fprintf(stderr, _("%s: excess command-line arguments:"),
302 for (i = optind; i < argc; i++) {
303 fprintf(stderr, " %s", argv[i]);
305 fprintf(stderr, "\n");
310 /* --filename and --execute imply --batch. */
311 if (filename || execute)
314 /* --batch implies --quiet */
315 if (interactive && show_intro)
318 /* --execute supersedes --filename */
319 if (filename && !execute) {
320 if (!freopen(filename, "r", stdin)) {
329 rc = repl_cmdline(argc, argv);
331 rc = repl_noninteractive();