2 * Main command implementation routines for cdecl99.
3 * Copyright © 2011-2012, 2020-2021, 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/>.
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 print_error("%s", _("failed to allocate memory"));
60 int run_command_explain(const char *arg)
66 decl = cdecl_parse_decl(arg);
68 print_error("%s", cdecl_get_error()->str);
72 for (struct cdecl *i = decl; i; i = i->next) {
73 str = do_format(cdecl_explain, i);
86 int run_command_simplify(const char *arg)
92 decl = cdecl_parse_decl(arg);
94 print_error("%s", cdecl_get_error()->str);
98 for (struct cdecl *i = decl; i; i = i->next) {
99 struct cdecl_declspec *s = i->specifiers;
102 i->specifiers = NULL;
106 str = do_format(cdecl_declare, i);
123 int run_command_declare(const char *cmd)
129 /* The name of the command is significant here. */
130 decl = cdecl_parse_english(cmd);
132 print_error("%s", cdecl_get_error()->str);
137 * English parses have at most one full declarator, so no loop is
140 str = do_format(cdecl_declare, decl);