]> icculus.org git repositories - btb/d2x.git/blob - main/cmd.c
portabilization
[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 "pstypes.h"
11 #include "cmd.h"
12 #include "console.h"
13 #include "error.h"
14
15 /* ======
16  * cmd_parse - Parse an input string
17  * ======
18  */
19 void cmd_parse(char *input)
20 {
21         char buffer[CMD_MAX_LENGTH];
22         char *tokens[CMD_MAX_TOKENS];
23         int num_tokens;
24         int i, l;
25
26         Assert(input != NULL);
27         
28         /* Strip leading spaces */
29         for (i=0; isspace(input[i]); i++) ;
30         strncpy( buffer, &input[i], CMD_MAX_LENGTH );
31
32         printf("lead strip \"%s\"\n",buffer);
33         l = strlen(buffer);
34         /* If command is empty, give up */
35         if (l==0) return;
36
37         /* Strip trailing spaces */
38         for (i=l-1; i>0 && isspace(buffer[i]); i--) ;
39         buffer[i+1] = 0;
40         printf("trail strip \"%s\"\n",buffer);
41
42         /* Split into tokens */
43         l = strlen(buffer);
44         num_tokens = 1;
45
46         tokens[0] = buffer;
47         for (i=1; i<l; i++) {
48                 if (isspace(buffer[i])) {
49                         buffer[i] = 0;
50                         while (isspace(buffer[i+1]) && (i+1 < l)) i++;
51                         tokens[num_tokens++] = &buffer[i+1];
52                 }
53         }
54
55         /* Check for matching commands */
56
57         /* Otherwise */
58         printf("n_tokens = %d\n", num_tokens);
59         if (num_tokens>1) {
60                 printf("setting %s %s\n",tokens[0], tokens[1]);
61           cvar_set(tokens[0], tokens[1]);
62         } else {
63           con_printf(CON_NORMAL, "%s: %f\n", tokens[0], cvar(tokens[0]));
64         }
65 }