%{ /* * Copyright © 2021, 2023-2024 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include "cdecl99.h" #include "commands.h" #include "help.h" typedef #if STRTAB_MAX_OFFSET < UINT_LEAST8_MAX uint_least8_t #elif STRTAB_MAX_OFFSET < UINT_LEAST16_MAX uint_least16_t #else #error do not know what type to use #endif cmd_index_type; static const struct command *in_word_set(); %} %struct-type %compare-strncmp %readonly-tables %language=ANSI-C %global-table %pic struct command { int_least8_t name; cmd_index_type cmd; }; %% explain, cmd_explain simplify, cmd_simplify declare, cmd_declare type, cmd_type help, cmd_help quit, cmd_quit exit, cmd_quit %% #include "cmdlist.h" static int run_cmd_help(void) { static const unsigned char offsets[] = CMD_SEQ; unsigned i; printf("Commands:\n"); for (i = 0; i < sizeof offsets / sizeof offsets[0]; i++) { const struct command *c = &wordlist[offsets[i]]; int w; w = printf(" %s", stringpool+c->name); if (w <= 0 || w > 13) { putchar('\n'); w = 0; } help_print_desc(NULL, gettext(strtab+c->cmd), 15, w); } return 0; } int run_command(const char *line, int batch) { const char *cmd = line + strspn(line, " \t"); const char *arg = cmd + strcspn(cmd, " \t"); const struct command *c; /* empty command */ if (cmd[0] == '\0') return 0; c = in_word_set(cmd, arg-cmd); if (!c) { print_error(_("unknown command %.*s"), (int)(arg-cmd), cmd); if (!batch) { fprintf(stderr, "%s\n", _("Try \"help\" for a list of possible commands.")); } return -1; } switch (c->cmd) { case cmd_help: return run_cmd_help(); case cmd_declare: case cmd_type: return run_command_cdecl(cmd, INPUT_ENGLISH, OUTPUT_C); case cmd_simplify: return run_command_cdecl(arg, INPUT_C, OUTPUT_C); case cmd_explain: return run_command_cdecl(arg, INPUT_C, OUTPUT_ENGLISH); case cmd_quit: return 1; } assert(0); }