2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011-2012 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 <http://www.gnu.org/licenses/>.
33 #define _(x) gettext(x)
37 static const char *progname = "cdecl99";
38 static const char sopts[] = "qbif:e:VH";
40 { PN_("longopt", "quiet"), 0, NULL, 'q' }, \
41 { PN_("longopt", "batch"), 0, NULL, 'b' }, \
42 { PN_("longopt", "interactive"), 0, NULL, 'i' }, \
43 { PN_("longopt", "file"), 1, NULL, 'f' }, \
44 { PN_("longopt", "execute"), 1, NULL, 'e' }, \
45 { PN_("longopt", "version"), 0, NULL, 'V' }, \
46 { PN_("longopt", "help"), 0, NULL, 'H' }
49 * With NLS, we need a buffer big enough to store the translated options.
50 * The translations will be filled in at program startup.
52 enum { NOPTS = sizeof (struct option[]){RAW_LOPTS} / sizeof (struct option) };
53 static struct option lopts[NOPTS + !!ENABLE_NLS * NOPTS + 1] = { RAW_LOPTS };
55 static struct helptext {
61 * TRANSLATORS: Help messages are indented 20 spaces and thus should
62 * not have lines longer than 60 columns.
64 { 'q', N_("Suppress the welcome message.\n") },
65 { 'b', N_("Execute commands as normal, but do not print any prompts.\n") },
66 { 'i', N_("Run in interactive mode. This is the default.\n") },
67 { 'f', N_("Read commands from FILE instead of standard input.\n"),
68 PN_("longopt|file", "FILE") },
69 { 'e', N_("Execute COMMAND as if it were entered at the prompt.\n"
70 "This can be specified multiple times.\n"),
71 PN_("longopt|execute", "COMMAND") },
72 { 'V', N_("Print a version message and then exit.\n") },
73 { 'H', N_("Print this message.\n") },
75 /* TRANSLATORS: ARG is only used for options without help text. */
76 { 0, NULL, PN_("longopt", "ARG") }
79 static void print_version(void)
82 /* TRANSLATORS: (C) must *always* be translated as ©. */
83 printf("Copyright %s 2011 Nick Bowler.\n", gettext("(C)"));
84 puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
85 puts("This is free software: you are free to change and redistribute it.");
86 puts("There is NO WARRANTY, to the extent permitted by law.");
89 static void print_usage(FILE *f)
91 fprintf(f, "Usage: %s [options]\n", progname);
95 * Print the help description of a particular command-line option, identified
96 * by its short option name.
98 static void print_option(int val)
100 const struct option *tmp[2];
101 const struct helptext *help;
107 /* Find the options and help text corresponding to val. */
108 for (const struct option *o = lopts; o->val; o++) {
110 assert(n < sizeof tmp / sizeof tmp[0]);
115 for (help = helptext; help->val; help++) {
116 if (help->val == val)
120 /* Prepare translations. */
122 argname = help->argname;
124 rc = snprintf(context, sizeof context, "longopt%c%s",
125 help->text ? '|' : '\0', tmp[0]->name);
126 assert(rc < sizeof context);
129 argname = pgettext_expr(context, help->argname);
134 w = printf(_(" -%c"), val);
137 w = printf(tmp[0]->has_arg ? _(" -%c, --%s=%s")
139 val, tmp[0]->name, argname);
142 if (tmp[0]->has_arg) {
143 w = printf(_(" -%c, --%s=%s, --%s=%s"), val,
144 tmp[0]->name, argname,
145 tmp[1]->name, argname);
147 w = printf(_(" -%c, --%s, --%s"), val,
148 tmp[0]->name, tmp[1]->name);
165 for (const char *line = gettext(help->text); *line;) {
166 const char *nl = strchr(line, '\n');
169 printf("%*s%s\n", 20-w, "", line);
173 printf("%*s%.*s\n", 20-w, "", (int)(nl-line), line);
178 static void print_help(void)
183 "This is \"cdecl99\": a command-line tool for parsing and constructing\n"
184 "complicated C declarations.\n"));
187 for (const char *c = sopts; *c; c++) {
188 if (*c == ':' || *c == '+' || *c == '-')
195 puts(_("For more information, see the cdecl99(1) man page.\n"));
198 * TRANSLATORS: Please add *another line* indicating where users should
199 * report translation bugs.
201 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
205 * Format a declaration according to the given function and return a pointer
206 * to the formatted string. The returned pointer remains valid until the
207 * next call, after which it must not be re-used.
209 * Returns NULL on failure.
212 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
220 rc = func(buf, bufsz, decl);
224 tmp = realloc(buf, rc + 1);
226 fprintf(stderr, "failed to allocate memory\n");
238 static int cmd_explain(const char *cmd, const char *arg)
240 const struct cdecl_error *err;
245 decl = cdecl_parse_decl(arg);
247 err = cdecl_get_error();
248 fprintf(stderr, "%s\n", err->str);
252 for (struct cdecl *i = decl; i; i = i->next) {
253 str = do_format(cdecl_explain, i);
266 static int cmd_simplify(const char *cmd, const char *arg)
268 const struct cdecl_error *err;
273 decl = cdecl_parse_decl(arg);
275 err = cdecl_get_error();
276 fprintf(stderr, "%s\n", err->str);
280 for (struct cdecl *i = decl; i; i = i->next) {
281 struct cdecl_declspec *s = i->specifiers;
284 i->specifiers = NULL;
288 str = do_format(cdecl_declare, i);
305 static int cmd_declare(const char *cmd, const char *arg)
307 const struct cdecl_error *err;
312 /* The name of the command is significant here. */
313 decl = cdecl_parse_english(cmd);
315 err = cdecl_get_error();
316 fprintf(stderr, "%s\n", err->str);
321 * English parses have at most one full declarator, so no loop is
324 str = do_format(cdecl_declare, decl);
335 static int cmd_quit(const char *cmd, const char *arg)
340 static int cmd_help(const char *cmd, const char *arg);
342 static const struct command {
344 int (*func)(const char *cmd, const char *arg);
347 { "explain", cmd_explain, "Explain a C declaration." },
348 { "simplify", cmd_simplify, "Simplify a C declaration." },
349 { "declare", cmd_declare, "Construct a C declaration." },
350 { "type", cmd_declare, "Construct a C type name." },
351 { "help", cmd_help, "Print this list of commands." },
352 { "quit", cmd_quit, "Quit the program." },
353 { "exit", cmd_quit, NULL }
355 static const size_t ncommands = sizeof commands / sizeof commands[0];
357 static int cmd_help(const char *cmd, const char *arg)
359 for (size_t i = 0; i < ncommands; i++) {
360 if (!commands[i].blurb)
363 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
370 * Ensure that the first n characters of str equal the entire (null-terminated)
373 static int cmd_cmp(const char *str, const char *cmd, size_t n)
375 size_t cmdlen = strlen(cmd);
381 return memcmp(str, cmd, n);
384 static int run_command(const char *line)
386 const char *cmd = line + strspn(line, " \t");
387 const char *arg = cmd + strcspn(cmd, " \t");
392 for (size_t i = 0; i < ncommands; i++) {
393 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
396 return commands[i].func(cmd, arg);
399 fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
403 static bool is_blank_line(const char *line)
405 for (size_t i = 0; line[i]; i++) {
406 if (!isblank((unsigned char)line[i]))
413 static int repl(void)
417 for (; (line = readline("> ")); free(line)) {
418 if (!is_blank_line(line))
419 cdecl_add_history(line);
421 if (!run_command(line))
429 static int repl_cmdline(int argc, char **argv)
431 int opt, rc, ret = 0;
434 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
438 rc = run_command(optarg);
448 static int repl_noninteractive(void)
450 int rc, ret = 0, saved_errno;
454 while (getline(&line, &n, stdin) >= 0) {
455 char *c = strchr(line, '\n');
459 rc = run_command(line);
471 perror("read error");
478 /* Initialize gettext and setup translated long options. */
479 static void init_i18n(void)
481 setlocale(LC_ALL, "");
482 bindtextdomain(PACKAGE, LOCALEDIR);
488 for (int i = 0, j = NOPTS; i < NOPTS; i++) {
489 const char *tname = pgettext_expr("longopt", lopts[i].name);
491 if (strcmp(tname, lopts[i].name) != 0) {
493 lopts[j].name = tname;
499 int main(int argc, char **argv)
501 bool show_intro = true, interactive = true, execute = false;
502 const char *filename = NULL;
510 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
539 /* --filename and --execute imply --batch. */
540 if (filename || execute)
543 /* --batch implies --quiet */
544 if (interactive && show_intro)
547 /* --execute supersedes --filename */
548 if (filename && !execute) {
549 if (!freopen(filename, "r", stdin)) {
558 rc = repl_cmdline(argc, argv);
560 rc = repl_noninteractive();