-
Notifications
You must be signed in to change notification settings - Fork 1
/
console_command_exec.template.c
61 lines (55 loc) · 1.91 KB
/
console_command_exec.template.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
#include "utils.h"
#include "console_internal.h"
#include "console_command.h"
typedef struct console_command_scheme {
const char *name;
const char *alias;
int min_args;
int max_args;
console_command_type_t type;
} console_command_scheme_t;
#define CONSOLE_COMMAND_NUM (<%= coms.select{ |com| !com.internal }.size %>)
static console_command_scheme_t console_command_schemes[CONSOLE_COMMAND_NUM] = {
<% coms.select{ |com| !com.internal }.each do |com| -%>
{
.name = "<%= com.name %>",
<% if com.alias -%>
.alias = "<%= com.alias %>",
<% end -%>
.min_args = <%= com.args.select{ |a| a.default.nil? }.size %>,
.max_args = <%= com.args.size %>,
.type = <%= com.enum %>,
},
<% end -%>
};
void console_exec_command(console_ref self, const char *command, int argc, char **argv)
{
if (command && strnlen(command, 256) > 0) {
for (int i = 0; i < CONSOLE_COMMAND_NUM; ++i) {
console_command_scheme_t *c = &console_command_schemes[i];
if ((c->alias && (strncmp(command, c->alias, 256) == 0)) ||
strncmp(command, c->name, 256) == 0) {
if (argc < c->min_args || argc > c->max_args) {
snprintf(self->display, 256,
"wrong arguments for '%s': expecting min %d, max "
"%d arguments",
c->name, c->min_args, c->max_args);
return;
}
switch (c->type) {
<% coms.select{ |com| !com.internal }.each do |com| -%>
case <%= com.enum %>: {
<% com.argparses.each do |argparse| -%>
<%= indent(argparse.join("\n"), 5) %>
<% end -%>
<%= com.impl_call %>
break;
}
<% end -%>
}
return;
}
}
snprintf(self->display, 256, "unknown command: %s", command);
}
}