]> icculus.org git repositories - btb/d2x.git/blob - main/cmd.c
miscellaneous fixes
[btb/d2x.git] / main / cmd.c
1 #ifdef HAVE_CONFIG_H
2 #include <conf.h>
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9
10 #include "error.h"
11 #include "u_mem.h"
12 #include "strutil.h"
13 #include "inferno.h"
14
15
16 typedef struct cmd_s
17 {
18         char          *name;
19         cmd_handler_t function;
20         char          *help_text;
21         struct cmd_s  *next;
22 } cmd_t;
23
24 /* The list of cmds */
25 static cmd_t *cmd_list;
26
27
28 #define ALIAS_NAME_MAX 32
29 typedef struct cmd_alias_s
30 {
31         char           name[ALIAS_NAME_MAX];
32         char           *value;
33         struct cmd_alias_s *next;
34 } cmd_alias_t;
35
36 /* The list of aliases */
37 static cmd_alias_t *cmd_alias_list;
38
39
40 /* add a new console command */
41 void cmd_addcommand(char *cmd_name, cmd_handler_t cmd_func, char *cmd_help_text)
42 {
43         cmd_t *cmd;
44
45         Assert(cmd_name != NULL);
46
47         for (cmd = cmd_list; cmd; cmd = cmd->next) {
48                 if (!stricmp(cmd_name, cmd->name))
49                 {
50                         Int3();
51                         con_printf(CON_NORMAL, "command %s already exists, not adding\n", cmd_name);
52                         return;
53                 }
54         }
55
56         /* create command, insert at front of list */
57         MALLOC(cmd, cmd_t, 1);
58         cmd->name = cmd_name;
59         cmd->function = cmd_func;
60         cmd->help_text = cmd_help_text;
61         cmd->next = cmd_list;
62         con_printf(CON_DEBUG, "cmd_addcommand: added %s\n", cmd->name);
63         cmd_list = cmd;
64 }
65
66
67 typedef struct cmd_queue_s
68 {
69         char *command_line;
70         struct cmd_queue_s *next;
71 } cmd_queue_t;
72
73 /* The list of commands to be executed */
74 static cmd_queue_t *cmd_queue_head;
75 static cmd_queue_t *cmd_queue_tail;
76
77
78 void cvar_cmd_set(int argc, char **argv);
79
80
81 /* execute a parsed command */
82 void cmd_execute(int argc, char **argv)
83 {
84         cmd_t *cmd;
85         cmd_alias_t *alias;
86
87         for (cmd = cmd_list; cmd; cmd = cmd->next) {
88                 if (!stricmp(argv[0], cmd->name)) {
89                         con_printf(CON_DEBUG, "cmd_execute: executing %s\n", argv[0]);
90                         cmd->function(argc, argv);
91                         return;
92                 }
93         }
94
95         for (alias = cmd_alias_list; alias; alias = alias->next) {
96                 if (!stricmp(argv[0], alias->name)) {
97                         con_printf(CON_DEBUG, "cmd_execute: pushing alias \"%s\": %s\n", alias->name, alias->value);
98                         cmd_insert(alias->value);
99                         return;
100                 }
101         }
102
103         /* Otherwise */
104         {  // set value of cvar
105                 char *new_argv[argc+1];
106                 int i;
107
108                 new_argv[0] = "set";
109                 for (i = 0; i < argc; i++)
110                         new_argv[i+1] = argv[i];
111                 cvar_cmd_set(argc + 1, new_argv);
112         }
113 }
114
115
116 /* Parse an input string */
117 void cmd_parse(char *input)
118 {
119         char buffer[CMD_MAX_LENGTH];
120         char *tokens[CMD_MAX_TOKENS];
121         int num_tokens;
122         int i, l;
123
124         Assert(input != NULL);
125         
126         /* Strip leading spaces */
127         while( isspace(*input) ) { ++input; }
128         strncpy( buffer, input, CMD_MAX_LENGTH );
129
130         //printf("lead strip \"%s\"\n",buffer);
131         l = (int)strlen(buffer);
132         /* If command is empty, give up */
133         if (l==0) return;
134
135         /* Strip trailing spaces */
136         for (i=l-1; i>0 && isspace(buffer[i]); i--) ;
137         buffer[i+1] = 0;
138         //printf("trail strip \"%s\"\n",buffer);
139
140         /* Split into tokens */
141         l = (int)strlen(buffer);
142         num_tokens = 1;
143
144         tokens[0] = buffer;
145         for (i=1; i<l; i++) {
146                 if (buffer[i] == '"') {
147                         tokens[num_tokens - 1] = &buffer[++i];
148                         while (i < l && buffer[i] != '"')
149                                 i++;
150                         buffer[i] = 0;
151                         continue;
152                 }
153                 if (isspace(buffer[i]) || buffer[i] == '=') {
154                         buffer[i] = 0;
155                         while (isspace(buffer[i+1]) && (i+1 < l)) i++;
156                         tokens[num_tokens++] = &buffer[i+1];
157                 }
158         }
159
160         /* Check for matching commands */
161         cmd_execute(num_tokens, tokens);
162 }
163
164
165 int cmd_queue_wait = 0;
166
167 int cmd_queue_process(void)
168 {
169         cmd_queue_t *cmd;
170
171         while (!cmd_queue_wait && cmd_queue_head) {
172                 cmd = cmd_queue_head;
173                 cmd_queue_head = cmd_queue_head->next;
174                 if (!cmd_queue_head)
175                         cmd_queue_tail = NULL;
176
177                 con_printf(CON_DEBUG, "cmd_queue_process: processing %s\n", cmd->command_line);
178                 cmd_parse(cmd->command_line);  // Note, this may change the queue
179
180                 d_free(cmd->command_line);
181                 d_free(cmd);
182         }
183
184         if (cmd_queue_wait > 0) {
185                 cmd_queue_wait--;
186                 if (Function_mode == FMODE_GAME) {
187                         con_printf(CON_DEBUG, "cmd_queue_process: waiting\n");
188                         return 1;
189                 }
190         }
191
192         return 0;
193 }
194
195
196 /* execute until there are no commands left */
197 void cmd_queue_flush(void)
198 {
199         while (cmd_queue_process()) {
200         }
201 }
202
203
204 /* Add some commands to the queue to be executed */
205 void cmd_enqueue(int insert, char *input)
206 {
207         cmd_queue_t *new, *head, *tail;
208         char output[CMD_MAX_LENGTH];
209         char *optr;
210
211         Assert(input != NULL);
212         head = tail = NULL;
213
214         while (*input) {
215                 optr = output;
216                 int quoted = 0;
217
218                 /* Strip leading spaces */
219                 while(isspace(*input) || *input == ';')
220                         input++;
221
222                 /* If command is empty, give up */
223                 if (! *input)
224                         continue;
225
226                 /* Find the end of this line (\n, ;, or nul) */
227                 do {
228                         if (!*input)
229                                 break;
230                         if (*input == '"') {
231                                 quoted = 1 - quoted;
232                                 continue;
233                         } else if ( *input == '\n' || (!quoted && *input == ';') ) {
234                                 input++;
235                                 break;
236                         }
237                 } while ((*optr++ = *input++));
238                 *optr = 0;
239
240                 /* make a new queue item, add it to list */
241                 MALLOC(new, cmd_queue_t, 1);
242                 new->command_line = d_strdup(output);
243                 new->next = NULL;
244
245                 if (!head)
246                         head = new;
247                 if (tail)
248                         tail->next = new;
249                 tail = new;
250
251                 con_printf(CON_DEBUG, "cmd_enqueue: adding %s\n", output);
252         }
253
254         if (insert) {
255                  /* add our list to the head of the main list */
256                 if (cmd_queue_head)
257                         tail->next = cmd_queue_head;
258                 if (!cmd_queue_tail)
259                         cmd_queue_tail = tail;
260                 
261                 cmd_queue_head = head;
262                 con_printf(CON_DEBUG, "cmd_enqueue: added to front of list\n");
263         } else {
264                 /* add our list to the tail of the main list */
265                 if (!cmd_queue_head)
266                         cmd_queue_head = head;
267                 if (cmd_queue_tail)
268                         cmd_queue_tail->next = head;
269                 
270                 cmd_queue_tail = tail;
271                 con_printf(CON_DEBUG, "cmd_enqueue: added to back of list\n");
272         }
273 }
274
275 void cmd_enqueuef(int insert, const char *fmt, ...)
276 {
277         va_list arglist;
278         char buf[CMD_MAX_LENGTH];
279         
280         va_start (arglist, fmt);
281         vsnprintf (buf, CMD_MAX_LENGTH, fmt, arglist);
282         va_end (arglist);
283         
284         cmd_enqueue(insert, buf);
285 }
286
287
288 /* Attempt to autocomplete an input string */
289 const char *cmd_complete(char *input)
290 {
291         cmd_t *ptr;
292         cmd_alias_t *aptr;
293
294         int len = (int)strlen(input);
295
296         if (!len)
297                 return NULL;
298
299         for (ptr = cmd_list; ptr != NULL; ptr = ptr->next)
300                 if (!strnicmp(input, ptr->name, len))
301                         return ptr->name;
302
303         for (aptr = cmd_alias_list; aptr != NULL; aptr = aptr->next)
304                 if (!strnicmp(input, aptr->name, len))
305                         return aptr->name;
306
307         return cvar_complete(input);
308 }
309
310
311 /* alias */
312 void cmd_alias(int argc, char **argv)
313 {
314         cmd_alias_t *alias;
315         char buf[CMD_MAX_LENGTH] = "";
316         int i;
317
318         if (argc < 2) {
319                 con_printf(CON_NORMAL, "aliases:\n");
320                 for (alias = cmd_alias_list; alias; alias = alias->next)
321                         con_printf(CON_NORMAL, "%s: %s\n", alias->name, alias->value);
322                 return;
323         }
324
325         if (argc == 2) {
326                 for (alias = cmd_alias_list; alias; alias = alias->next)
327                         if (!stricmp(argv[1], alias->name)) {
328                                 con_printf(CON_NORMAL, "%s: %s\n", alias->name, alias->value);
329                                 return;
330                         }
331
332                 con_printf(CON_NORMAL, "alias: %s not found\n", argv[1]);
333                 return;
334         }
335
336         for (i = 2; i < argc; i++) {
337                 if (i > 2)
338                         strncat(buf, " ", CMD_MAX_LENGTH);
339                 strncat(buf, argv[i], CMD_MAX_LENGTH);
340         }
341
342         for (alias = cmd_alias_list; alias; alias = alias->next) {
343                 if (!stricmp(argv[1], alias->name)) {
344                         d_free(alias->value);
345                         alias->value = d_strdup(buf);
346                         return;
347                 }
348         }
349
350         MALLOC(alias, cmd_alias_t, 1);
351         strncpy(alias->name, argv[1], ALIAS_NAME_MAX);
352         alias->value = d_strdup(buf);
353         alias->next = cmd_alias_list;
354         cmd_alias_list = alias;
355 }
356
357
358 /* unalias */
359 void cmd_unalias(int argc, char **argv)
360 {
361         cmd_alias_t *alias, *prev_alias = NULL;
362
363         if (argc < 2 || argc > 2) {
364                 cmd_insertf("help %s", argv[0]);
365                 return;
366         }
367         
368         for (alias = cmd_alias_list; alias ; alias = alias->next) {
369                 if (!stricmp(argv[1], alias->name))
370                         break;
371                 prev_alias = alias;
372         }
373
374         if (!alias) {
375                 con_printf(CON_NORMAL, "unalias: %s not found\n", argv[1]);
376                 return;
377         }
378
379         if (prev_alias)
380                 prev_alias->next = alias->next;
381         else
382                 cmd_alias_list = alias->next;
383
384         d_free(alias->value);
385         d_free(alias);
386 }
387
388
389 /* echo to console */
390 void cmd_echo(int argc, char **argv)
391 {
392         char buf[CMD_MAX_LENGTH] = "";
393         int i;
394
395         for (i = 1; i < argc; i++) {
396                 if (i > 1)
397                         strncat(buf, " ", CMD_MAX_LENGTH);
398                 strncat(buf, argv[i], CMD_MAX_LENGTH);
399         }
400         con_printf(CON_NORMAL, "%s\n", buf);
401 }
402
403 /* execute script */
404 void cmd_exec(int argc, char **argv) {
405         cmd_queue_t *new, *head, *tail;
406         PHYSFS_File *f;
407         char line[CMD_MAX_LENGTH] = "";
408
409         if (argc < 2 || argc > 2) {
410                 cmd_insertf("help %s", argv[0]);
411                 return;
412         }
413         
414         head = tail = NULL;
415
416         f = PHYSFSX_openReadBuffered(argv[1]);
417         if (!f) {
418                 con_printf(CON_CRITICAL, "exec: %s not found\n", argv[1]);
419                 return;
420         }
421         while (PHYSFSX_gets(f, line)) {
422                 /* make a new queue item, add it to list */
423                 MALLOC(new, cmd_queue_t, 1);
424                 new->command_line = d_strdup(line);
425                 new->next = NULL;
426
427                 if (!head)
428                         head = new;
429                 if (tail)
430                         tail->next = new;
431                 tail = new;
432
433                 con_printf(CON_DEBUG, "cmd_exec: adding %s\n", line);
434         }
435         PHYSFS_close(f);
436
437         /* add our list to the head of the main list */
438         if (cmd_queue_head)
439                 tail->next = cmd_queue_head;
440         if (!cmd_queue_tail)
441                 cmd_queue_tail = tail;
442
443         cmd_queue_head = head;
444         con_printf(CON_DEBUG, "cmd_exec: added to front of list\n");
445 }
446
447
448 /* get help */
449 void cmd_help(int argc, char **argv)
450 {
451         cmd_t *cmd;
452
453         if (argc > 2) {
454                 cmd_insertf("help %s", argv[0]);
455                 return;
456         }
457
458         if (argc < 2) {
459                 con_printf(CON_NORMAL, "Available commands:\n");
460                 for (cmd = cmd_list; cmd; cmd = cmd->next) {
461                         con_printf(CON_NORMAL, "    %s\n", cmd->name);
462                 }
463
464                 return;
465         }
466
467         for (cmd = cmd_list; cmd != NULL; cmd = cmd->next)
468                 if (!stricmp(argv[1], cmd->name))
469                         break;
470
471         if (!cmd) {
472                 con_printf(CON_URGENT, "Command %s not found\n", argv[1]);
473                 return;
474         }
475
476         if (!cmd->help_text) {
477                 con_printf(CON_NORMAL, "%s: no help found\n", argv[1]);
478                 return;
479         }
480
481         con_printf(CON_NORMAL, cmd->help_text);
482 }
483
484
485 /* execute script */
486 void cmd_wait(int argc, char **argv)
487 {
488         if (argc > 2) {
489                 cmd_insertf("help %s", argv[0]);
490                 return;
491         }
492
493         if (argc < 2)
494                 cmd_queue_wait = 1;
495         else
496                 cmd_queue_wait = atoi(argv[1]);
497 }
498
499
500 void cmd_free(void)
501 {
502         void *p, *temp;
503
504         p = cmd_list;
505         while (p) {
506                 temp = p;
507                 p = ((cmd_t *)p)->next;
508                 d_free(temp);
509         }
510
511         p = cmd_alias_list;
512         while (p) {
513                 d_free(((cmd_alias_t *)p)->value);
514                 temp = p;
515                 p = ((cmd_alias_t *)p)->next;
516                 d_free(temp);
517         }
518 }
519
520
521 void cmd_init(void)
522 {
523         cmd_addcommand("alias",     cmd_alias,      "alias <name> <commands>\n" "    define <name> as an alias for <commands>\n"
524                                                     "alias <name>\n"            "    show the current definition of <name>\n"
525                                                     "alias\n"                   "    show all defined aliases\n");
526         cmd_addcommand("unalias",   cmd_unalias,    "unalias <name>\n"          "    undefine the alias <name>\n");
527         cmd_addcommand("echo",      cmd_echo,       "echo [text]\n"             "    write <text> to the console\n");
528         cmd_addcommand("exec",      cmd_exec,       "exec <file>\n"             "    execute <file>\n");
529         cmd_addcommand("help",      cmd_help,       "help [command]\n"          "    get help for <command>, or list all commands if not specified.\n");
530         cmd_addcommand("wait",      cmd_wait,       "usage: wait [n]\n"         "    stop processing commands, resume in <n> cycles (default 1)\n");
531
532         atexit(cmd_free);
533 }