2 * Command line utility for making sense of C declarations.
3 * Copyright © 2011 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/>.
31 #define _(x) gettext(x)
35 static const char *progname = "cdecl99";
36 static const char sopts[] = "qbif:e:VH";
38 { PN_("longopt", "quiet"), 0, NULL, 'q' }, \
39 { PN_("longopt", "batch"), 0, NULL, 'b' }, \
40 { PN_("longopt", "interactive"), 0, NULL, 'i' }, \
41 { PN_("longopt", "file"), 1, NULL, 'f' }, \
42 { PN_("longopt", "execute"), 1, NULL, 'e' }, \
43 { PN_("longopt", "version"), 0, NULL, 'V' }, \
44 { PN_("longopt", "help"), 0, NULL, 'H' }
47 * With NLS, we need a buffer big enough to store the translated options.
48 * The translations will be filled in at program startup.
50 enum { NOPTS = sizeof (struct option[]){RAW_LOPTS} / sizeof (struct option) };
51 static struct option lopts[NOPTS + !!ENABLE_NLS * NOPTS + 1] = { RAW_LOPTS };
53 static struct helptext {
59 * TRANSLATORS: Help messages are indented 20 spaces and thus should
60 * not have lines longer than 60 columns.
62 { 'q', N_("Suppress the welcome message.\n") },
63 { 'b', N_("Execute commands as normal, but do not print any prompts.\n") },
64 { 'i', N_("Run in interactive mode. This is the default.\n") },
65 { 'f', N_("Read commands from FILE instead of standard input.\n"),
66 PN_("longopt|file", "FILE") },
67 { 'e', N_("Execute COMMAND as if it were entered at the prompt.\n"
68 "This can be specified multiple times.\n"),
69 PN_("longopt|execute", "COMMAND") },
70 { 'V', N_("Print a version message and then exit.\n") },
71 { 'H', N_("Print this message.\n") },
73 /* TRANSLATORS: ARG is only used for options without help text. */
74 { 0, NULL, PN_("longopt", "ARG") }
77 static void print_version(void)
80 /* TRANSLATORS: (C) must *always* be translated as ©. */
81 printf("Copyright %s 2011 Nick Bowler.\n", gettext("(C)"));
82 puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
83 puts("This is free software: you are free to change and redistribute it.");
84 puts("There is NO WARRANTY, to the extent permitted by law.");
87 static void print_usage(FILE *f)
89 fprintf(f, "Usage: %s [options]\n", progname);
93 * Print the help description of a particular command-line option, identified
94 * by its short option name.
96 static void print_option(int val)
98 const struct option *tmp[2];
99 const struct helptext *help;
105 /* Find the options and help text corresponding to val. */
106 for (const struct option *o = lopts; o->val; o++) {
108 assert(n < sizeof tmp / sizeof tmp[0]);
113 for (help = helptext; help->val; help++) {
114 if (help->val == val)
118 /* Prepare translations. */
120 argname = help->argname;
122 rc = snprintf(context, sizeof context, "longopt%c%s",
123 help->text ? '|' : '\0', tmp[0]->name);
124 assert(rc < sizeof context);
127 argname = pgettext_expr(context, help->argname);
132 w = printf(_(" -%c"), val);
135 w = printf(tmp[0]->has_arg ? _(" -%c, --%s=%s")
137 val, tmp[0]->name, argname);
140 if (tmp[0]->has_arg) {
141 w = printf(_(" -%c, --%s=%s, --%s=%s"), val,
142 tmp[0]->name, argname,
143 tmp[1]->name, argname);
145 w = printf(_(" -%c, --%s, --%s"), val,
146 tmp[0]->name, tmp[1]->name);
163 for (const char *line = gettext(help->text); *line;) {
164 const char *nl = strchr(line, '\n');
167 printf("%*s%s\n", 20-w, "", line);
171 printf("%*s%.*s\n", 20-w, "", (int)(nl-line), line);
176 static void print_help(void)
181 "This is \"cdecl99\": a command-line tool for parsing and constructing\n"
182 "complicated C declarations.\n"));
185 for (const char *c = sopts; *c; c++) {
186 if (*c == ':' || *c == '+' || *c == '-')
193 puts(_("For more information, see the cdecl99(1) man page.\n"));
196 * TRANSLATORS: Please add *another line* indicating where users should
197 * report translation bugs.
199 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
203 * Format a declaration according to the given function and return a pointer
204 * to the formatted string. The returned pointer remains valid until the
205 * next call, after which it must not be re-used.
207 * Returns NULL on failure.
210 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
218 rc = func(buf, bufsz, decl);
222 tmp = realloc(buf, rc + 1);
224 fprintf(stderr, "failed to allocate memory\n");
236 static int cmd_explain(const char *cmd, const char *arg)
238 const struct cdecl_error *err;
243 decl = cdecl_parse_decl(arg);
245 err = cdecl_get_error();
246 fprintf(stderr, "%s\n", err->str);
250 for (struct cdecl *i = decl; i; i = i->next) {
251 str = do_format(cdecl_explain, i);
264 static int cmd_simplify(const char *cmd, const char *arg)
266 const struct cdecl_error *err;
271 decl = cdecl_parse_decl(arg);
273 err = cdecl_get_error();
274 fprintf(stderr, "%s\n", err->str);
278 for (struct cdecl *i = decl; i; i = i->next) {
279 struct cdecl_declspec *s = i->specifiers;
282 i->specifiers = NULL;
286 str = do_format(cdecl_declare, i);
303 static int cmd_declare(const char *cmd, const char *arg)
305 const struct cdecl_error *err;
310 /* The name of the command is significant here. */
311 decl = cdecl_parse_english(cmd);
313 err = cdecl_get_error();
314 fprintf(stderr, "%s\n", err->str);
319 * English parses have at most one full declarator, so no loop is
322 str = do_format(cdecl_declare, decl);
333 static int cmd_quit(const char *cmd, const char *arg)
338 static int cmd_help(const char *cmd, const char *arg);
340 static const struct command {
342 int (*func)(const char *cmd, const char *arg);
345 { "explain", cmd_explain, "Explain a C declaration." },
346 { "simplify", cmd_simplify, "Simplify a C declaration." },
347 { "declare", cmd_declare, "Construct a C declaration." },
348 { "type", cmd_declare, "Construct a C type name." },
349 { "help", cmd_help, "Print this list of commands." },
350 { "quit", cmd_quit, "Quit the program." },
351 { "exit", cmd_quit, NULL }
353 static const size_t ncommands = sizeof commands / sizeof commands[0];
355 static int cmd_help(const char *cmd, const char *arg)
357 for (size_t i = 0; i < ncommands; i++) {
358 if (!commands[i].blurb)
361 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
368 * Ensure that the first n characters of str equal the entire (null-terminated)
371 static int cmd_cmp(const char *str, const char *cmd, size_t n)
373 size_t cmdlen = strlen(cmd);
379 return memcmp(str, cmd, n);
382 static int run_command(const char *line)
384 const char *cmd = line + strspn(line, " \t");
385 const char *arg = cmd + strcspn(cmd, " \t");
390 for (size_t i = 0; i < ncommands; i++) {
391 if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
394 return commands[i].func(cmd, arg);
397 fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
401 static int repl(void)
405 for (; (line = readline("> ")); free(line)) {
406 if (!run_command(line))
414 static int repl_cmdline(int argc, char **argv)
416 int opt, rc, ret = 0;
419 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
423 rc = run_command(optarg);
433 static int repl_noninteractive(void)
435 int rc, ret = 0, saved_errno;
439 while (getline(&line, &n, stdin) >= 0) {
440 char *c = strchr(line, '\n');
444 rc = run_command(line);
456 perror("read error");
463 /* Initialize gettext and setup translated long options. */
464 static void init_i18n(void)
466 setlocale(LC_ALL, "");
467 bindtextdomain(PACKAGE, LOCALEDIR);
473 for (int i = 0, j = NOPTS; i < NOPTS; i++) {
474 const char *tname = pgettext_expr("longopt", lopts[i].name);
476 if (strcmp(tname, lopts[i].name) != 0) {
478 lopts[j].name = tname;
484 int main(int argc, char **argv)
486 bool show_intro = true, interactive = true, execute = false;
487 const char *filename = NULL;
495 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
524 /* --filename and --execute imply --batch. */
525 if (filename || execute)
528 /* --batch implies --quiet */
529 if (interactive && show_intro)
532 /* --execute supersedes --filename */
533 if (filename && !execute) {
534 if (!freopen(filename, "r", stdin)) {
543 rc = repl_cmdline(argc, argv);
545 rc = repl_noninteractive();