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)
62 const struct cdecl_error *err;
67 decl = cdecl_parse_decl(arg);
69 print_error("%s", cdecl_get_error()->str);
73 for (struct cdecl *i = decl; i; i = i->next) {
74 str = do_format(cdecl_explain, i);
87 int run_command_simplify(const char *arg)
89 const struct cdecl_error *err;
94 decl = cdecl_parse_decl(arg);
96 print_error("%s", cdecl_get_error()->str);
100 for (struct cdecl *i = decl; i; i = i->next) {
101 struct cdecl_declspec *s = i->specifiers;
104 i->specifiers = NULL;
108 str = do_format(cdecl_declare, i);
125 int run_command_declare(const char *cmd)
131 /* The name of the command is significant here. */
132 decl = cdecl_parse_english(cmd);
134 print_error("%s", cdecl_get_error()->str);
139 * English parses have at most one full declarator, so no loop is
142 str = do_format(cdecl_declare, decl);