-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
258 lines (224 loc) · 7 KB
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* shell.c -- source for interacting with the engine via a shell
*
* Authored by Karl "p0lyh3dron" Kreuze on September 15, 2022
*
* This file is part of the Chik library, a general purpose
* library for the Chik engine and her games.
*
* This file defines the functions for interacting with the
* engine via a shell.
*/
#include "shell.h"
#include <stdarg.h>
#include <string.h>
#include "log.h"
shell_command_t _coms[LIBCHIK_SHELL_MAX_COMMANDS];
shell_variable_t _vars[LIBCHIK_SHELL_MAX_VARIABLES];
/*
* Displays the help text.
*/
void shell_help() {
log_msg("\n\t- Welcome to Chishi, the shell for the Chik Engine!\n\n");
log_msg("\t- To get started, you can type the name of a variable,\n");
log_msg("\t followed by a space, followed by a value,\n");
log_msg("\t to set the variable to that value. For example,\n");
log_msg("\t 'fov 90' will set 'fov' to 90, if that variable name is "
"registered.\n\n");
log_msg(
"\t- You can also type the name of a command, followed by a space,\n");
log_msg("\t followed by arguments, to run that command.\n");
log_msg(
"\t For example, 'load_map test' will run the 'load_map' command,\n");
log_msg("\t if that command is registered.\n\n");
log_msg("\t- To get a list of all registered commands, type 'commands'.\n");
log_msg(
"\t- To get a list of all registered variables, type 'variables'.\n");
log_msg("\t- To get a list of all registered commands and variables, type "
"'all'.\n");
log_msg("\t- To see this message again, type 'help'.\n");
}
/*
* Displays a list of all registered commands.
*/
void shell_list_commands() {
unsigned long i;
log_msg("\n\t* Registered commands:\n\n");
for (i = 0; i < LIBCHIK_SHELL_MAX_COMMANDS; i++) {
if (_coms[i].name == nullptr) {
break;
}
log_msg("\t\t- %s: '%s'\n", _coms[i].name, _coms[i].desc);
}
}
/*
* Displays a list of all registered variables.
*/
void shell_list_variables() {
unsigned long i;
log_msg("\n\t* Registered variables:\n\n");
for (i = 0; i < LIBCHIK_SHELL_MAX_VARIABLES; i++) {
if (_vars[i].name == nullptr) {
break;
}
log_msg("\t\t- %s: (set to %s) '%s'\n", _vars[i].name, _vars[i].val,
_vars[i].desc);
}
}
/*
* Displays a list of all registered commands and variables.
*/
void shell_list_all() {
shell_list_commands();
shell_list_variables();
}
/*
* Initializes the shell.
*/
void shell_init(void) {
shell_command_t commands[] = {
{"help", "Print the help message for the shell usage.", shell_help},
{"commands", "List all registered commands.", shell_list_commands},
{"variables", "List all registered variables.", shell_list_variables},
{"all", "List all registered commands and variables.", shell_list_all},
{nullptr, nullptr, nullptr}};
memset(_coms, 0, sizeof(shell_command_t) * LIBCHIK_SHELL_MAX_COMMANDS);
memset(_vars, 0, sizeof(shell_variable_t) * LIBCHIK_SHELL_MAX_VARIABLES);
shell_register_commands(commands);
}
/*
* Registers shell commands.
*
* @param shell_command_t *com A shell command.
*/
void shell_register_commands(shell_command_t *com) {
unsigned long i = 0;
unsigned long j = 0;
while (_coms[i].name != nullptr) {
i++;
}
while (com[j].name != nullptr) {
_coms[i++] = com[j];
j++;
}
}
/*
* Registers shell variables.
*
* @param shell_variable_t *var A shell variable.
*/
void shell_register_variables(shell_variable_t *var) {
unsigned long i = 0;
unsigned long j = 0;
while (_vars[i].name != nullptr) {
i++;
}
while (var[j].name != nullptr) {
_vars[i++] = var[j];
j++;
}
}
/*
* Executes a shell command.
*
* @param char *com The command to execute.
*/
void shell_execute(char *com) {
unsigned long i;
char *argv[LIBCHIK_SHELL_ARGV_SIZE];
long argc = 0;
char *arg;
/*
* Parse the command into arguments.
*/
arg = strtok(com, " ");
while (arg != nullptr) {
argv[argc] = (char *)malloc(sizeof(char) * strlen(arg));
if (argv[argc] == nullptr) {
LOGF_ERR("Failed to allocate memory for shell argument.\n");
return;
}
strcpy(argv[argc], arg);
argc++;
arg = strtok(nullptr, " ");
}
argv[argc] = nullptr;
/*
* Execute the command.
*/
for (i = 0; i < LIBCHIK_SHELL_MAX_COMMANDS; i++) {
if (_coms[i].name != nullptr) {
if (strcmp(_coms[i].name, argv[0]) == 0) {
log_msg("> %s\n", com);
_coms[i].fun(argc, (char **)argv);
argc = 0;
while (argv[argc] != nullptr) {
free(argv[argc]);
argc++;
}
return;
}
}
if (_vars[i].name != nullptr) {
if (strcmp(_vars[i].name, argv[0]) == 0) {
log_msg("> %s\n", com);
if (argc > 1) {
strcpy(_vars[i].val, argv[1]);
if (_vars[i].fun != nullptr) {
_vars[i].fun(argv[1]);
}
}
log_msg("\t- %s = %s\n", _vars[i].name, _vars[i].val);
return;
}
}
}
VLOGF_WARN("Unknown command or variable: %s\n", argv[0]);
return;
}
/*
* Gets a shell variable value.
*
* @param char name The name of the variable.
*
* @return shell_val_u The value of the variable.
*/
shell_val_u shell_get_variable(char *name) {
unsigned long i;
for (i = 0; i < LIBCHIK_SHELL_MAX_VARIABLES; i++) {
if (_vars[i].name != nullptr) {
if (strcmp(_vars[i].name, name) == 0) {
switch (_vars[i].type) {
case SHELL_VAR_INT:
return (shell_val_u){.i = atoi(_vars[i].val)};
case SHELL_VAR_FLOAT:
return (shell_val_u){.f = atof(_vars[i].val)};
case SHELL_VAR_STRING:
return (shell_val_u){.s = _vars[i].val};
case SHELL_VAR_BOOL:
return (shell_val_u){
.b = (strcmp(_vars[i].val, "true") == 0)};
}
}
}
}
return (shell_val_u){.i = 0};
}
/*
* Sets a shell variable value.
*
* @param char *name The name of the variable.
* @param char *value The value of the variable.
*/
void shell_set_variable(char *name, char *value) {
unsigned long i;
for (i = 0; i < LIBCHIK_SHELL_MAX_VARIABLES; i++) {
if (_vars[i].name != nullptr) {
if (strcmp(_vars[i].name, name) == 0) {
strcpy(_vars[i].val, value);
return;
}
}
}
VLOGF_WARN("Unknown variable: %s\n", name);
}