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 const struct option *opt;
86 puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
87 "complicated C declarations."));
91 for (opt = lopts; opt->name; opt++) {
92 struct lopt_help help;
94 if (!lopt_get_help(opt, &help))
97 help_print_option(opt, help.arg, help.desc, 20);
101 puts(_("For more information, see the cdecl99(1) man page."));
105 * TRANSLATORS: Please add *another line* indicating where users should
106 * report translation bugs.
108 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
111 static int is_blank_line(const char *line)
113 return !line[strspn(line, " \t")];
116 static int repl(void)
120 for (; (line = readline("> ")); free(line)) {
121 if (!is_blank_line(line))
124 if (run_command(line, true) > 0)
132 static int repl_cmdline(unsigned count, char **commands)
137 for (i = 0; i < count; i++) {
138 int rc = run_command(commands[i], false);
148 static int repl_noninteractive(void)
150 int rc, ret = 0, saved_errno;
154 while (getline(&line, &n, stdin) >= 0) {
155 char *c = strchr(line, '\n');
159 rc = run_command(line, false);
170 print_error("%s", strerror(saved_errno));
177 /* Initialize gettext */
178 static void init_i18n(void)
183 setlocale(LC_ALL, "");
184 bindtextdomain(PACKAGE, LOCALEDIR);
189 INIT_EXIT_SUCCESS = -1,
190 INIT_EXIT_FAILURE = -2
193 static int initialize(int argc, char **argv)
195 int i, opt, quiet = 0, execute = 0;
196 const char *filename = NULL;
198 XTRA_PACKED_LOPTS(lopts);
205 while ((opt = getopt_long(argc, argv, SOPT_STRING, lopts, 0)) != -1) {
220 argv[execute++] = optarg;
224 return INIT_EXIT_SUCCESS;
227 return INIT_EXIT_SUCCESS;
230 return INIT_EXIT_FAILURE;
235 fprintf(stderr, "%s: ", progname);
236 fprintf(stderr, _("excess command-line arguments:"));
237 for (i = optind; i < argc; i++) {
238 fprintf(stderr, " %s", argv[i]);
240 fprintf(stderr, "\n");
242 return INIT_EXIT_FAILURE;
245 /* --filename and --execute imply --batch. */
246 if (filename || execute)
249 /* --batch implies --quiet */
250 if (interactive && !quiet)
253 /* --execute supersedes --filename */
254 if (filename && !execute) {
255 if (!freopen(filename, "r", stdin)) {
256 print_error("failed to open %s: %s", filename,
258 return INIT_EXIT_FAILURE;
265 int main(int argc, char **argv)
269 switch ((execute = initialize(argc, argv))) {
270 case INIT_EXIT_SUCCESS: return EXIT_SUCCESS;
271 case INIT_EXIT_FAILURE: return EXIT_FAILURE;
277 rc = repl_cmdline(execute, argv);
279 rc = repl_noninteractive();