forked from bertrik/LoraWanPmSensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdproc.cpp
47 lines (41 loc) · 1015 Bytes
/
cmdproc.cpp
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
#include <string.h>
#include "cmdproc.h"
static const cmd_t *find_cmd(const cmd_t * commands, const char *name)
{
const cmd_t *cmd;
for (cmd = commands; cmd->cmd != NULL; cmd++) {
if (strcmp(name, cmd->name) == 0) {
return cmd;
}
}
return NULL;
}
static int split(char *input, char *args[], int maxargs)
{
int argc = 0;
char *next = strtok(input, " ");
while ((next != NULL) && (argc < maxargs)) {
args[argc++] = next;
next = strtok(NULL, " ");
}
return argc;
}
int cmd_process(const cmd_t * commands, char *line)
{
char *argv[CMD_MAX_ARGS];
// parse line
int argc = split(line, argv, CMD_MAX_ARGS);
if (argc == 0) {
// no command present
return CMD_NO_CMD;
}
// find matching entry
const cmd_t *cmd = find_cmd(commands, argv[0]);
if (cmd == NULL) {
// no command found
return CMD_UNKNOWN;
}
// execute
int res = cmd->cmd(argc, argv);
return res;
}