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>
43 static const char *progname = "cdecl99";
44 static bool interactive = true;
46 void print_error(const char *fmt, ...)
51 fprintf(stderr, "%s: ", progname);
52 fprintf(stderr, "%s", _("error: "));
55 vfprintf(stderr, fmt, ap);
58 fprintf(stderr, "\n");
61 static void print_version(void)
63 const char *copysign = copyright_symbol(locale_charset());
66 printf("Copyright %s 2023 Nick Bowler.\n", copysign);
67 puts("License GPLv3+: GNU GPL version 3 or any later version.");
68 puts("This is free software: you are free to change and redistribute it.");
69 puts("There is NO WARRANTY, to the extent permitted by law.");
72 static void print_usage(FILE *f)
74 fprintf(f, _("Usage: %s [options]\n"), progname);
76 fprintf(f, _("Try %s --help for more information.\n"),
80 static void print_help(const struct option *lopts)
82 struct lopt_help help = {0};
83 const struct option *opt;
87 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
88 "complicated C declarations."));
92 for (opt = lopts; opt->name; opt++) {
93 if (!lopt_get_help(opt, &help))
96 help_print_option(opt, help.arg, help.desc, 20);
100 puts(_("For more information, see the cdecl99(1) man page."));
104 * TRANSLATORS: Please add *another line* indicating where users should
105 * report translation bugs.
107 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
110 static int is_blank_line(const char *line)
112 return !line[strspn(line, " \t")];
115 static int repl(void)
119 for (; (line = readline("> ")); free(line)) {
120 if (!is_blank_line(line))
123 if (run_command(line, true) > 0)
131 static int repl_cmdline(unsigned count, char **commands)
136 for (i = 0; i < count; i++) {
137 int rc = run_command(commands[i], false);
147 static int repl_noninteractive(void)
149 int rc, ret = 0, saved_errno;
153 while (getline(&line, &n, stdin) >= 0) {
154 char *c = strchr(line, '\n');
158 rc = run_command(line, false);
169 print_error("%s", strerror(saved_errno));
176 /* Initialize gettext */
177 static void init_i18n(void)
182 setlocale(LC_ALL, "");
183 bindtextdomain(PACKAGE, LOCALEDIR);
188 INIT_EXIT_SUCCESS = -1,
189 INIT_EXIT_FAILURE = -2
192 static int initialize(int argc, char **argv)
194 int i, opt, quiet = 0, execute = 0;
195 const char *filename = NULL;
197 XTRA_PACKED_LOPTS(lopts);
204 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
219 argv[execute++] = optarg;
223 return INIT_EXIT_SUCCESS;
226 return INIT_EXIT_SUCCESS;
229 return INIT_EXIT_FAILURE;
234 fprintf(stderr, "%s: ", progname);
235 fprintf(stderr, _("excess command-line arguments:"));
236 for (i = optind; i < argc; i++) {
237 fprintf(stderr, " %s", argv[i]);
239 fprintf(stderr, "\n");
241 return INIT_EXIT_FAILURE;
244 /* --filename and --execute imply --batch. */
245 if (filename || execute)
248 /* --batch implies --quiet */
249 if (interactive && !quiet)
252 /* --execute supersedes --filename */
253 if (filename && !execute) {
254 if (!freopen(filename, "r", stdin)) {
255 print_error("failed to open %s: %s", filename,
257 return INIT_EXIT_FAILURE;
264 int main(int argc, char **argv)
268 switch ((execute = initialize(argc, argv))) {
269 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
270 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
276 rc = repl_cmdline(execute, argv);
278 rc = repl_noninteractive();