]> git.draconx.ca Git - cdecl99.git/blob - src/execute.gperf
Use gperf to implement command selection.
[cdecl99.git] / src / execute.gperf
1 %{
2 /*
3  * Copyright © 2021 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <assert.h>
24
25 #include "cdecl99.h"
26 #include "commands.h"
27
28 typedef
29 #if STRTAB_MAX_OFFSET < UINT_LEAST8_MAX
30 uint_least8_t
31 #elif STRTAB_MAX_OFFSET < UINT_LEAST16_MAX
32 uint_least16_t
33 #else
34 #error do not know what type to use
35 #endif
36 cmd_index_type;
37
38 static const struct command *in_word_set();
39 %}
40
41 %struct-type
42 %compare-strncmp
43 %readonly-tables
44 %language=ANSI-C
45 %global-table
46 %pic
47
48 struct command {
49         int_least8_t name;
50         cmd_index_type cmd;
51 };
52 %%
53 explain, cmd_explain
54 simplify, cmd_simplify
55 declare, cmd_declare
56 type, cmd_type
57 help, cmd_help
58 quit, cmd_quit
59 exit, cmd_quit
60 %%
61 #include "cmdlist.h"
62
63 static int run_cmd_help(void)
64 {
65         static const unsigned char offsets[] = CMD_SEQ;
66         unsigned i;
67
68         printf("Commands:\n");
69         for (i = 0; i < sizeof offsets / sizeof offsets[0]; i++) {
70                 const struct command *c = &wordlist[offsets[i]];
71                 int w;
72
73                 w = printf("  %s", stringpool+c->name);
74                 if (w < 0 || w > 13) {
75                         putchar('\n');
76                          0;
77                 }
78
79                 print_block(gettext(strtab+c->cmd), 15, w);
80         }
81
82         return 1;
83 }
84
85 int run_command(const char *line, int interactive)
86 {
87         const char *cmd = line + strspn(line, " \t");
88         const char *arg = cmd + strcspn(cmd, " \t");
89         const struct command *c;
90
91         /* empty command */
92         if (cmd[0] == '\0')
93                 return 1;
94
95         c = in_word_set(cmd, arg-cmd);
96         if (!c) {
97                 fprintf(stderr, _("unknown command %.*s\n"),
98                                 (int)(arg-cmd), cmd);
99                 if (interactive) {
100                         fprintf(stderr, "%s\n",
101                                 _("Try \"help\" for a list of possible commands."));
102                 }
103                 return -1;
104         }
105
106         switch (c->cmd) {
107         case cmd_help: return run_cmd_help();
108         case cmd_declare: case cmd_type: return run_command_declare(cmd);
109         case cmd_simplify: return run_command_simplify(arg);
110         case cmd_explain: return run_command_explain(arg);
111         case cmd_quit: return 1;
112         }
113
114         assert(0);
115 }