-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.c
156 lines (123 loc) · 2.42 KB
/
cli.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
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "common.h"
#include "stringtrie.h"
struct cli_data {
int (*func)(void*, char*);
char *help;
void *data;
};
void cli_tree_destroy(struct st_root *root)
{
st_destroy(root, ST_DO_FREE_DATA);
}
static char* trim_and_validate(char *string)
{
size_t last;
if (!string || string[0] == '\0')
return NULL;
while (isspace(*string))
string++;
last = strlen(string);
if (!last)
return NULL;
last--;
while (isspace(string[last])) {
string[last] = '\0';
if (last)
last--;
else
break;
}
if (!*string)
return NULL;
return string;
}
int cli_add_cmd(struct st_root *root, char *cmd,
int (*func)(void*, char*), void *ptr, char *help)
{
struct cli_data *node;
cmd = trim_and_validate(cmd);
if (!cmd)
return -1;
node = malloc(sizeof(*node));
if (!node)
return -1;
node->data = ptr;
node->help = help;
node->func = func;
if (st_add_string(root, cmd, node)) {
free(node);
return -1;
}
return 0;
}
int cli_rm_cmd(struct st_root *root, char *cmd)
{
struct st_node *node;
node = st_rm_string(root, cmd);
if (!node)
return -1;
free(node);
return 0;
}
int cli_run_cmd(struct st_root * const root, const char * const string)
{
int r;
unsigned int i, len;
char *cmd, *param;
char *line = NULL;
struct cli_data *node;
if (!string)
return -1;
line = strdup(string);
if (!line)
return -1;
cmd = trim_and_validate(line);
if (!cmd) {
free(line);
return -1;
}
len = strlen(cmd);
for (i = 0; i < len && !isspace(line[i]); i++);
line[i] = '\0';
if (i < len - 1)
param = trim_and_validate(cmd + i + 1);
else
param = NULL;
node = st_lookup_string(root, cmd);
if (node && node->func)
r = node->func(node->data, param);
else
r = -1;
free(line);
return r;
}
struct cli_print {
void (*print)(void*, const char*, ...);
void *hints;
};
void __cli_print_help(void *st_data, const char *cmd_name, void *hints)
{
struct cli_print *data = hints;
struct cli_data *cli;
cli = st_data;
if (!cli->func)
return;
if (cli->help)
data->print(data->hints, "%-10s %s\n", cmd_name, cli->help);
else
data->print(data->hints, "%-10s No help text available\n",
cmd_name);
}
void cli_print_help(struct st_root *root, void (*print)(void*, const char*, ...),
void *hints)
{
struct cli_print data;
data.print = print;
data.hints = hints;
st_foreach_data(root, __cli_print_help, &data);
}