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/>.
35 #include <localcharset.h>
40 static const char *progname = "cdecl99";
43 static const char sopts[] = SOPT_STRING;
44 static const struct option lopts[] = {
49 static void print_version(void)
51 const char *copysign = "(C)";
52 void *convsign = NULL;
55 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
61 printf("Copyright %s 2021 Nick Bowler.\n", copysign);
62 puts("License GPLv3+: GNU GPL version 3 or any later version.");
63 puts("This is free software: you are free to change and redistribute it.");
64 puts("There is NO WARRANTY, to the extent permitted by law.");
69 static void print_usage(FILE *f)
71 fprintf(f, _("Usage: %s [options]\n"), progname);
73 fprintf(f, _("Try %s --help for more information.\n"),
78 print_optstring(const struct option *opt, const struct lopt_help *help)
87 w = snprintf(optstring, sizeof optstring,
88 _(" -%c, --%s=%s"), opt->val, opt->name,
89 pgettext_expr(opt->name, help->arg));
91 w = snprintf(optstring, sizeof optstring,
92 _(" -%c, --%s"), opt->val, opt->name);
98 w = mbsnwidth(optstring, w, 0);
99 printf("%s", optstring);
104 w = printf(" -%c, --%s=%s", opt->val, opt->name, help->arg);
106 w = printf(" -%c, --%s", opt->val, opt->name);
109 if (w < 0 || w > 18) {
118 * Print a string, with each line indented by i spaces. The first line
119 * will be indented by w fewer spaces (to account for the cursor being in
120 * some other column).
122 void print_block(const char *s, int i, int w)
125 const char *nl = strchr(s, '\n');
126 int n = (nl ? nl-s : -1);
128 printf("%*s%.*s\n", i-w, "", n, s);
136 static void print_help(void)
138 const struct option *opt;
142 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
143 "complicated C declarations."));
147 for (opt = lopts; opt->name; opt++) {
148 struct lopt_help help;
152 if (!lopt_get_help(opt, &help))
155 w = print_optstring(opt, &help);
158 help.desc = pgettext_expr(opt->name, help.desc);
160 print_block(help.desc, 20, w);
164 puts(_("For more information, see the cdecl99(1) man page."));
168 * TRANSLATORS: Please add *another line* indicating where users should
169 * report translation bugs.
171 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
174 static bool is_blank_line(const char *line)
176 for (size_t i = 0; line[i]; i++) {
177 if (!isblank((unsigned char)line[i]))
184 static int repl(void)
188 for (; (line = readline("> ")); free(line)) {
189 if (!is_blank_line(line))
190 cdecl_add_history(line);
192 if (run_command(line, true) > 0)
200 static int repl_cmdline(int argc, char **argv)
202 int opt, rc, ret = 0;
205 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
209 rc = run_command(optarg, false);
219 static int repl_noninteractive(void)
221 int rc, ret = 0, saved_errno;
225 while (getline(&line, &n, stdin) >= 0) {
226 char *c = strchr(line, '\n');
230 rc = run_command(line, false);
242 perror("read error");
249 /* Initialize gettext */
250 static void init_i18n(void)
255 setlocale(LC_ALL, "");
256 bindtextdomain(PACKAGE, LOCALEDIR);
260 int main(int argc, char **argv)
262 bool show_intro = true, interactive = true, execute = false;
263 const char *filename = NULL;
271 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
301 fprintf(stderr, _("%s: excess command-line arguments:"),
303 for (i = optind; i < argc; i++) {
304 fprintf(stderr, " %s", argv[i]);
306 fprintf(stderr, "\n");
311 /* --filename and --execute imply --batch. */
312 if (filename || execute)
315 /* --batch implies --quiet */
316 if (interactive && show_intro)
319 /* --execute supersedes --filename */
320 if (filename && !execute) {
321 if (!freopen(filename, "r", stdin)) {
330 rc = repl_cmdline(argc, argv);
332 rc = repl_noninteractive();