%{ /* * Copyright © 2021 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" 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'); 0; } print_block(gettext(strtab+c->cmd), 15, w); } return 1; } int run_command(const char *line, int interactive) { 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 1; c = in_word_set(cmd, arg-cmd); if (!c) { fprintf(stderr, _("unknown command %.*s\n"), (int)(arg-cmd), cmd); if (interactive) { 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_declare(cmd); case cmd_simplify: return run_command_simplify(arg); case cmd_explain: return run_command_explain(arg); case cmd_quit: return 1; } assert(0); }