2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011-2012, 2020 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>
37 #define _(x) gettext(x)
41 static const char *progname = "cdecl99";
42 static const char sopts[] = "qbif:e:VH";
44 { PN_("longopt", "quiet"), 0, NULL, 'q' }, \
45 { PN_("longopt", "batch"), 0, NULL, 'b' }, \
46 { PN_("longopt", "interactive"), 0, NULL, 'i' }, \
47 { PN_("longopt", "file"), 1, NULL, 'f' }, \
48 { PN_("longopt", "execute"), 1, NULL, 'e' }, \
49 { PN_("longopt", "version"), 0, NULL, 'V' }, \
50 { PN_("longopt", "help"), 0, NULL, 'H' }
53 * With NLS, we need a buffer big enough to store the translated options.
54 * The translations will be filled in at program startup.
56 enum { NOPTS = sizeof (struct option[]){RAW_LOPTS} / sizeof (struct option) };
57 static struct option lopts[NOPTS + !!ENABLE_NLS * NOPTS + 1] = { RAW_LOPTS };
59 static struct helptext {
65 * TRANSLATORS: Help messages are indented 20 spaces and thus should
66 * not have lines longer than 60 columns.
68 { 'q', N_("Suppress the welcome message.\n") },
69 { 'b', N_("Execute commands as normal, but do not print any prompts.\n") },
70 { 'i', N_("Run in interactive mode. This is the default.\n") },
71 { 'f', N_("Read commands from FILE instead of standard input.\n"),
72 PN_("longopt|file", "FILE") },
73 { 'e', N_("Execute COMMAND as if it were entered at the prompt.\n"
74 "This can be specified multiple times.\n"),
75 PN_("longopt|execute", "COMMAND") },
76 { 'V', N_("Print a version message and then exit.\n") },
77 { 'H', N_("Print this message.\n") },
79 /* TRANSLATORS: ARG is only used for options without help text. */
80 { 0, NULL, PN_("longopt", "ARG") }
83 static void print_version(void)
85 const char *copysign = "(C)";
86 void *convsign = NULL;
89 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
95 printf("Copyright %s 2020 Nick Bowler.\n", copysign);
96 puts("License GPLv3+: GNU GPL version 3 or any later version.");
97 puts("This is free software: you are free to change and redistribute it.");
98 puts("There is NO WARRANTY, to the extent permitted by law.");
103 static void print_usage(FILE *f)
105 fprintf(f, "Usage: %s [options]\n", progname);
109 * Print the help description of a particular command-line option, identified
110 * by its short option name.
112 static void print_option(int val)
114 const struct option *tmp[2];
115 const struct helptext *help;
121 /* Find the options and help text corresponding to val. */
122 for (const struct option *o = lopts; o->val; o++) {
124 assert(n < sizeof tmp / sizeof tmp[0]);
129 for (help = helptext; help->val; help++) {
130 if (help->val == val)
134 /* Prepare translations. */
136 argname = help->argname;
138 rc = snprintf(context, sizeof context, "longopt%c%s",
139 help->text ? '|' : '\0', tmp[0]->name);
140 assert(rc < sizeof context);
143 argname = pgettext_expr(context, help->argname);
148 w = printf(_(" -%c"), val);
151 w = printf(tmp[0]->has_arg ? _(" -%c, --%s=%s")
153 val, tmp[0]->name, argname);
156 if (tmp[0]->has_arg) {
157 w = printf(_(" -%c, --%s=%s, --%s=%s"), val,
158 tmp[0]->name, argname,
159 tmp[1]->name, argname);
161 w = printf(_(" -%c, --%s, --%s"), val,
162 tmp[0]->name, tmp[1]->name);
179 for (const char *line = gettext(help->text); *line;) {
180 const char *nl = strchr(line, '\n');
183 printf("%*s%s\n", 20-w, "", line);
187 printf("%*s%.*s\n", 20-w, "", (int)(nl-line), line);
192 static void print_help(void)
197 "This is \"cdecl99\": a command-line tool for parsing and constructing\n"
198 "complicated C declarations.\n"));
201 for (const char *c = sopts; *c; c++) {
202 if (*c == ':' || *c == '+' || *c == '-')
209 puts(_("For more information, see the cdecl99(1) man page.\n"));
212 * TRANSLATORS: Please add *another line* indicating where users should
213 * report translation bugs.
215 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
219 * Format a declaration according to the given function and return a pointer
220 * to the formatted string. The returned pointer remains valid until the
221 * next call, after which it must not be re-used.
223 * Returns NULL on failure.
226 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
234 rc = func(buf, bufsz, decl);
238 tmp = realloc(buf, rc + 1);
240 fprintf(stderr, "failed to allocate memory\n");
252 static int cmd_explain(const char *cmd, const char *arg)
254 const struct cdecl_error *err;
259 decl = cdecl_parse_decl(arg);
261 err = cdecl_get_error();
262 fprintf(stderr, "%s\n", err->str);
266 for (struct cdecl *i = decl; i; i = i->next) {
267 str = do_format(cdecl_explain, i);
280 static int cmd_simplify(const char *cmd, const char *arg)
282 const struct cdecl_error *err;
287 decl = cdecl_parse_decl(arg);
289 err = cdecl_get_error();
290 fprintf(stderr, "%s\n", err->str);
294 for (struct cdecl *i = decl; i; i = i->next) {
295 struct cdecl_declspec *s = i->specifiers;
298 i->specifiers = NULL;
302 str = do_format(cdecl_declare, i);
319 static int cmd_declare(const char *cmd, const char *arg)
321 const struct cdecl_error *err;
326 /* The name of the command is significant here. */
327 decl = cdecl_parse_english(cmd);
329 err = cdecl_get_error();
330 fprintf(stderr, "%s\n", err->str);
335 * English parses have at most one full declarator, so no loop is
338 str = do_format(cdecl_declare, decl);
349 static int cmd_quit(const char *cmd, const char *arg)
354 static int cmd_help(const char *cmd, const char *arg);
356 static const struct command {
358 int (*func)(const char *cmd, const char *arg);
361 { "explain", cmd_explain, "Explain a C declaration." },
362 { "simplify", cmd_simplify, "Simplify a C declaration." },
363 { "declare", cmd_declare, "Construct a C declaration." },
364 { "type", cmd_declare, "Construct a C type name." },
365 { "help", cmd_help, "Print this list of commands." },
366 { "quit", cmd_quit, "Quit the program." },
367 { "exit", cmd_quit, NULL }
369 static const size_t ncommands = sizeof commands / sizeof commands[0];
371 static int cmd_help(const char *cmd, const char *arg)
373 for (size_t i = 0; i < ncommands; i++) {
374 if (!commands[i].blurb)
377 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
384 * Ensure that the first n characters of str equal the entire (null-terminated)
387 static int cmd_cmp(const char *str, const char *cmd, size_t n)
389 size_t cmdlen = strlen(cmd);
395 return memcmp(str, cmd, n);
398 static int run_command(const char *line)
400 const char *cmd = line + strspn(line, " \t");
401 const char *arg = cmd + strcspn(cmd, " \t");
406 for (size_t i = 0; i < ncommands; i++) {
407 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
410 return commands[i].func(cmd, arg);
413 fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
417 static bool is_blank_line(const char *line)
419 for (size_t i = 0; line[i]; i++) {
420 if (!isblank((unsigned char)line[i]))
427 static int repl(void)
431 for (; (line = readline("> ")); free(line)) {
432 if (!is_blank_line(line))
433 cdecl_add_history(line);
435 if (!run_command(line))
443 static int repl_cmdline(int argc, char **argv)
445 int opt, rc, ret = 0;
448 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
452 rc = run_command(optarg);
462 static int repl_noninteractive(void)
464 int rc, ret = 0, saved_errno;
468 while (getline(&line, &n, stdin) >= 0) {
469 char *c = strchr(line, '\n');
473 rc = run_command(line);
485 perror("read error");
492 /* Initialize gettext and setup translated long options. */
493 static void init_i18n(void)
498 setlocale(LC_ALL, "");
499 bindtextdomain(PACKAGE, LOCALEDIR);
502 for (int i = 0, j = NOPTS; i < NOPTS; i++) {
503 const char *tname = pgettext_expr("longopt", lopts[i].name);
505 if (strcmp(tname, lopts[i].name) != 0) {
507 lopts[j].name = tname;
513 int main(int argc, char **argv)
515 bool show_intro = true, interactive = true, execute = false;
516 const char *filename = NULL;
524 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
553 /* --filename and --execute imply --batch. */
554 if (filename || execute)
557 /* --batch implies --quiet */
558 if (interactive && show_intro)
561 /* --execute supersedes --filename */
562 if (filename && !execute) {
563 if (!freopen(filename, "r", stdin)) {
572 rc = repl_cmdline(argc, argv);
574 rc = repl_noninteractive();