2 * Main command implementation routines for cdecl99.
3 * Copyright © 2011-2012, 2020-2021 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/>.
27 * Format a declaration according to the given function and return a pointer
28 * to the formatted string. The returned pointer remains valid until the
29 * next call, after which it must not be re-used.
31 * Returns NULL on failure.
34 do_format(size_t func(char *, size_t, struct cdecl *), struct cdecl *decl)
42 rc = func(buf, bufsz, decl);
46 tmp = realloc(buf, rc + 1);
48 fprintf(stderr, "%s\n", _("failed to allocate memory"));
60 int run_command_explain(const char *arg)
62 const struct cdecl_error *err;
67 decl = cdecl_parse_decl(arg);
69 err = cdecl_get_error();
70 fprintf(stderr, "%s\n", err->str);
74 for (struct cdecl *i = decl; i; i = i->next) {
75 str = do_format(cdecl_explain, i);
88 int run_command_simplify(const char *arg)
90 const struct cdecl_error *err;
95 decl = cdecl_parse_decl(arg);
97 err = cdecl_get_error();
98 fprintf(stderr, "%s\n", err->str);
102 for (struct cdecl *i = decl; i; i = i->next) {
103 struct cdecl_declspec *s = i->specifiers;
106 i->specifiers = NULL;
110 str = do_format(cdecl_declare, i);
127 int run_command_declare(const char *cmd)
129 const struct cdecl_error *err;
134 /* The name of the command is significant here. */
135 decl = cdecl_parse_english(cmd);
137 err = cdecl_get_error();
138 fprintf(stderr, "%s\n", err->str);
143 * English parses have at most one full declarator, so no loop is
146 str = do_format(cdecl_declare, decl);