forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
167 lines (133 loc) · 2.84 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
157
158
159
160
161
162
163
164
165
166
167
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "stringtree.h"
struct cli_data {
int (*func)(void*, char*);
char *help;
void *data;
};
void cli_tree_destroy(struct list_head *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 list_head *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;
}
/*
* cli_rm_cmd removes a command from the command tree. It does not, however,
* remove any tree nodes: this is a feature to keep the number of malloc()/free()s
* to a sane level, as the cli trees for logged in users are constantly in a
* state of flux and the nodes will probably be reused very soon.
*/
int cli_rm_cmd(struct list_head *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 list_head * 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;
}
static void __cli_print_help(FILE *f, struct list_head *root, char *buf, size_t idx, const size_t len)
{
struct st_node *st;
struct cli_data *cli;
if (len - idx < 2)
return;
buf[idx + 1] = '\0';
list_for_each_entry(st, root, list) {
buf[idx] = st->c;
if (st->data) {
cli = st->data;
if (cli->func) {
if (cli->help)
fprintf(f, "%-10s %s\n", buf, cli->help);
else
fprintf(f, "%-10s No help text available\n", buf);
}
}
}
list_for_each_entry(st, root, list) {
buf[idx] = st->c;
__cli_print_help(f, &st->children, buf, idx + 1, len);
}
}
#define MAX_CMD_LEN 64
void cli_print_help(FILE *f, struct list_head *root)
{
char buf[MAX_CMD_LEN];
memset(buf, 0, sizeof(buf));
__cli_print_help(f, root, buf, 0, sizeof(buf));
}