Debian Codegraph @

common/main/cmd.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 *
7 */
8 /*
9 *
10 * Command parsing and processing
11 *
12 */
13
14 #include <cstdarg>
15 #include <map>
16 #include <forward_list>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21
22 #include "dxxerror.h"
23 #include "u_mem.h"
24 #include "strutil.h"
25 #include "inferno.h"
26 #include "cmd.h"
27 #include "console.h"
28 #include "cvar.h"
29 #include "physfsx.h"
30
31 #include "compiler-range_for.h"
32 #include <memory>
33
34 namespace {
35
36 struct cmd_t
37 {
38 const char *name;
39 cmd_handler_t function;
40 const char *help_text;
41 };
42
43 /* The list of cmds */
44 static std::map<const char *, std::unique_ptr<cmd_t>> cmd_list;
45
46 #define ALIAS_NAME_MAX 32
47 struct cmd_alias_t
48 {
49 std::unique_ptr<char[]> value;
50 char name[ALIAS_NAME_MAX];
51 };
52
53 #define CMD_MAX_ALIASES 1024
54
55 /* The list of aliases */
56 static std::map<std::string, std::unique_ptr<cmd_alias_t>> cmd_alias_list;
57
58 static cmd_t *cmd_findcommand(const char *cmd_name)
59 {
60 const auto i = cmd_list.find(cmd_name);
61 return i == cmd_list.end() ? nullptr : i->second.get();
62 }
63
64
65 static cmd_alias_t *cmd_findalias(const char *alias_name )
66 {
67 const auto i = cmd_alias_list.find(alias_name);
68 return i == cmd_alias_list.end() ? nullptr : i->second.get();
69 }
70
71 }
72
73 /* add a new console command */
74 void cmd_addcommand(const char *cmd_name, cmd_handler_t cmd_func, const char *cmd_help_text)
75 {
76 const auto i = cmd_list.insert(std::make_pair(cmd_name, std::unique_ptr<cmd_t>{}));
77 if (!i.second)
78 {
79 Int3();
80 con_printf(CON_NORMAL, "command %s already exists, not adding", cmd_name);
81 return;
82 }
83 auto cmd = (i.first->second = std::make_unique<cmd_t>()).get();
84 /* create command, insert to hashtable */
85 cmd->name = cmd_name;
86 cmd->function = cmd_func;
87 cmd->help_text = cmd_help_text;
88
89 con_printf(CON_DEBUG, "cmd_addcommand: added %s", cmd->name);
90 }
91
92 namespace {
93
94 struct cmd_queue_t
95 {
96 std::unique_ptr<char[]> command_line;
97 explicit cmd_queue_t(std::unique_ptr<char[]> p) :
98 command_line(std::move(p))
99 {
100 }
101 };
102
103 /* The list of commands to be executed */
104 static std::forward_list<cmd_queue_t> cmd_queue;
105
106 /* execute a parsed command */
107 static void cmd_execute(unsigned long argc, const char *const *const argv)
108 {
109 cmd_t *cmd;
110 cmd_alias_t *alias;
111
112 if ( (cmd = cmd_findcommand(argv[0])) )
113 {
114 con_printf(CON_DEBUG, "cmd_execute: executing %s", argv[0]);
115 cmd->function(argc, argv);
116 return;
117 }
118
119 if ( (alias = cmd_findalias(argv[0])) && alias->value )
120 {
121 con_printf(CON_DEBUG, "cmd_execute: pushing alias \"%s\": %s", alias->name, alias->value.get());
122 cmd_insert(alias->value.get());
123 return;
124 }
125
126 /* Otherwise */
127 if (argc < 31)
128 { // set value of cvar
129 const char *new_argv[32];
130 int i;
131
132 new_argv[0] = "set";
133 for (i = 0; i < argc; i++)
134 new_argv[i+1] = argv[i];
135 cvar_cmd_set(argc + 1, new_argv);
136 }
137 }
138
139 /* Parse an input string */
140 static void cmd_parse(char *input)
141 {
142 char buffer[CMD_MAX_LENGTH];
143 char *tokens[CMD_MAX_TOKENS];
144 int num_tokens;
145 uint_fast32_t i, l;
146
147 Assert(input != NULL);
148
149 /* Strip leading spaces */
150 while( isspace(*input) ) { ++input; }
151 buffer[sizeof(buffer) - 1] = 0;
152 strncpy(buffer, input, sizeof(buffer) - 1);
153
154 //printf("lead strip \"%s\"\n",buffer);
155 l = strlen(buffer);
156 /* If command is empty, give up */
157 if (l==0) return;
158
159 /* Strip trailing spaces */
160 for (i=l-1; i>0 && isspace(buffer[i]); i--) ;
161 buffer[i+1] = 0;
162 //printf("trail strip \"%s\"\n",buffer);
163
164 /* Split into tokens */
165 l = strlen(buffer);
166 num_tokens = 1;
167
168 tokens[0] = buffer;
169 for (i=1; i<l; i++) {
170 if (buffer[i] == '"') {
171 tokens[num_tokens - 1] = &buffer[++i];
172 while (i < l && buffer[i] != '"')
173 i++;
174 buffer[i] = 0;
175 continue;
176 }
177 if (isspace(buffer[i]) || buffer[i] == '=') {
178 buffer[i] = 0;
179 while (isspace(buffer[i+1]) && (i+1 < l)) i++;
180 tokens[num_tokens++] = &buffer[i+1];
181 }
182 }
183
184 /* Check for matching commands */
185 cmd_execute(num_tokens, tokens);
186 }
187
188 static int cmd_queue_wait;
189
190 }
191
192 int cmd_queue_process(void)
193 {
194 for (;;)
195 {
196 if (cmd_queue_wait)
197 break;
198 auto cmd = cmd_queue.begin();
199 if (cmd == cmd_queue.end())
200 break;
201 auto command_line = std::move(cmd->command_line);
202 cmd_queue.pop_front();
203 con_printf(CON_DEBUG, "cmd_queue_process: processing %s", command_line.get());
204 cmd_parse(command_line.get()); // Note, this may change the queue
205 }
206
207 if (cmd_queue_wait > 0) {
208 cmd_queue_wait--;
209 con_puts(CON_DEBUG, "cmd_queue_process: waiting");
210 return 1;
211 }
212
213 return 0;
214 }
215
216
217 /* execute until there are no commands left */
218 void cmd_queue_flush(void)
219 {
220 while (cmd_queue_process()) {
221 }
222 }
223
224
225 /* Add some commands to the queue to be executed */
226 void cmd_enqueue(int insert, const char *input)
227 {
228 std::forward_list<cmd_queue_t> l;
229 char output[CMD_MAX_LENGTH];
230 char *optr;
231 auto before_end = [](std::forward_list<cmd_queue_t> &f) {
232 for (auto i = f.before_begin();;)
233 {
234 auto j = i;
235 if (++ i == f.end())
236 return j;
237 }
238 };
239 auto iter = before_end(l);
240 while (*input) {
241 optr = output;
242 int quoted{0};
243
244 /* Strip leading spaces */
245 while(isspace(*input) || *input == ';')
246 input++;
247
248 /* If command is empty, give up */
249 if (! *input)
250 continue;
251
252 /* Find the end of this line (\n, ;, or nul) */
253 do {
254 if (!*input)
255 break;
256 if (*input == '"') {
257 quoted = 1 - quoted;
258 continue;
259 } else if ( *input == '\n' || (!quoted && *input == ';') ) {
260 input++;
261 break;
262 }
263 } while ((*optr++ = *input++));
264 *optr = 0;
265
266 /* make a new queue item, add it to list */
267 iter = l.emplace_after(iter, cmd_queue_t{d_strdup(output)});
268 con_printf(CON_DEBUG, "cmd_enqueue: adding %s", output);
269 }
270 auto after = insert
271 /* add our list to the head of the main list */
272 ? (con_puts(CON_DEBUG, "cmd_enqueue: added to front of list"), cmd_queue.before_begin())
273 /* add our list to the tail of the main list */
274 : (con_puts(CON_DEBUG, "cmd_enqueue: added to back of list"), before_end(cmd_queue))
275 ;
276 cmd_queue.splice_after(after, std::move(l));
277 }
278
279 void cmd_enqueuef(int insert, const char *fmt, ...)
280 {
281 va_list arglist;
282 char buf[CMD_MAX_LENGTH];
283
284 va_start (arglist, fmt);
285 vsnprintf(buf, sizeof(buf), fmt, arglist);
286 va_end (arglist);
287
288 cmd_enqueue(insert, buf);
289 }
290
291
292 /* Attempt to autocomplete an input string */
293 const char *cmd_complete(const char *input)
294 {
295 uint_fast32_t len = strlen(input);
296
297 if (!len)
298 return NULL;
299
300 range_for (const auto &i, cmd_list)
301 if (!d_strnicmp(input, i.first, len))
302 return i.first;
303
304 range_for (const auto &i, cmd_alias_list)
305 if (!d_strnicmp(input, i.first.c_str(), len))
306 return i.first.c_str();
307
308 return cvar_complete(input);
309 }
310
311 namespace {
312
313 /* alias */
314 static void cmd_alias(unsigned long argc, const char *const *const argv)
315 {
316 char buf[CMD_MAX_LENGTH] = "";
317 if (argc < 2) {
318 con_puts(CON_NORMAL, "aliases:");
319 range_for (const auto &i, cmd_alias_list)
320 con_printf(CON_NORMAL, "%s: %s", i.first.c_str(), i.second->value.get());
321 return;
322 }
323
324 if (argc == 2) {
325 cmd_alias_t *alias;
326 if ( (alias = cmd_findalias(argv[1])) && alias->value )
327 {
328 con_printf(CON_NORMAL, "%s: %s", alias->name, alias->value.get());
329 return;
330 }
331
332 con_printf(CON_NORMAL, "alias: %s not found", argv[1]);
333 return;
334 }
335
336 for (int i = 2; i < argc; i++) {
337 if (i > 2)
338 strncat(buf, " ", sizeof(buf) - strlen(buf) - 1);
339 strncat(buf, argv[i], sizeof(buf) - strlen(buf) - 1);
340 }
341 const auto i = cmd_alias_list.insert(std::make_pair(argv[1], std::unique_ptr<cmd_alias_t>{}));
342 auto alias = i.first->second.get();
343 if (i.second)
344 {
345 alias = (i.first->second = std::make_unique<cmd_alias_t>()).get();
346 alias->name[sizeof(alias->name) - 1] = 0;
347 strncpy(alias->name, argv[1], sizeof(alias->name) - 1);
348 }
349 alias->value = d_strdup(buf);
350 }
351
352 /* unalias */
353 static void cmd_unalias(unsigned long argc, const char *const *const argv)
354 {
355 if (argc < 2 || argc > 2) {
356 cmd_insertf("help %s", argv[0]);
357 return;
358 }
359
360 const char *alias_name = argv[1];
361 const auto alias = cmd_alias_list.find(alias_name);
362 if (alias == cmd_alias_list.end())
363 {
364 con_printf(CON_NORMAL, "unalias: %s not found", alias_name);
365 return;
366 }
367 cmd_alias_list.erase(alias);
368 }
369
370 /* echo to console */
371 static void cmd_echo(unsigned long argc, const char *const *const argv)
372 {
373 char buf[CMD_MAX_LENGTH] = "";
374 int i;
375
376 for (i = 1; i < argc; i++) {
377 if (i > 1)
378 strncat(buf, " ", sizeof(buf) - strlen(buf) - 1);
379 strncat(buf, argv[i], sizeof(buf) - strlen(buf) - 1);
380 }
381 con_puts(CON_NORMAL, std::span<char>(buf, strlen(buf)));
382 }
383
384 /* execute script */
385 static void cmd_exec(unsigned long argc, const char *const *const argv)
386 {
387 PHYSFSX_gets_line_t<CMD_MAX_LENGTH> line;
388
389 if (argc < 2 || argc > 2) {
390 cmd_insertf("help %s", argv[0]);
391 return;
392 }
393 auto &&[f, physfserr] = PHYSFSX_openReadBuffered(argv[1]);
394 if (!f) {
395 con_printf(CON_CRITICAL, "exec: failed to open \"%s\": %s", argv[1], PHYSFS_getErrorByCode(physfserr));
396 return;
397 }
398 std::forward_list<cmd_queue_t> l;
399 auto i = l.before_begin();
400 while (PHYSFSX_fgets(line, f)) {
401 /* make a new queue item, add it to list */
402 i = l.emplace_after(i, cmd_queue_t{d_strdup(line)});
403 con_printf(CON_DEBUG, "cmd_exec: adding %s", static_cast<const char *>(line));
404 }
405
406 /* add our list to the head of the main list */
407 con_puts(CON_DEBUG, "cmd_exec: added to front of list");
408 cmd_queue.splice_after(cmd_queue.before_begin(), std::move(l));
409 }
410
411 /* get help */
412 static void cmd_help(unsigned long argc, const char *const *const argv)
413 {
414 cmd_t *cmd;
415
416 if (argc > 2) {
417 cmd_insertf("help %s", argv[0]);
418 return;
419 }
420
421 if (argc < 2) {
422 con_puts(CON_NORMAL, "Available commands:");
423 range_for (const auto &i, cmd_list)
424 con_printf(CON_NORMAL, " %s", i.first);
425
426 return;
427 }
428
429 cmd = cmd_findcommand(argv[1]);
430
431 if (!cmd) {
432 con_printf(CON_URGENT, "Command %s not found", argv[1]);
433 return;
434 }
435
436 if (!cmd->help_text) {
437 con_printf(CON_NORMAL, "%s: no help found", argv[1]);
438 return;
439 }
440
441 con_puts(CON_NORMAL, std::span<const char>(cmd->help_text, strlen(cmd->help_text)));
442 }
443
444 /* execute script */
445 static void cmd_wait(unsigned long argc, const char *const *const argv)
446 {
447 if (argc > 2) {
448 cmd_insertf("help %s", argv[0]);
449 return;
450 }
451
452 if (argc < 2)
453 cmd_queue_wait = 1;
454 else
455 cmd_queue_wait = atoi(argv[1]);
456 }
457
458 }
459
460 void cmd_init(void)
461 {
462 cmd_addcommand("alias", cmd_alias, "alias <name> <commands>\n" " define <name> as an alias for <commands>\n"
463 "alias <name>\n" " show the current definition of <name>\n"
464 "alias\n" " show all defined aliases");
465 cmd_addcommand("unalias", cmd_unalias, "unalias <name>\n" " undefine the alias <name>");
466 cmd_addcommand("echo", cmd_echo, "echo [text]\n" " write <text> to the console");
467 cmd_addcommand("exec", cmd_exec, "exec <file>\n" " execute <file>");
468 cmd_addcommand("help", cmd_help, "help [command]\n" " get help for <command>, or list all commands if not specified.");
469 cmd_addcommand("wait", cmd_wait, "usage: wait [n]\n" " stop processing commands, resume in <n> cycles (default 1)");
470 }