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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "triton.h"
#include "cli.h"
#include "cli_p.h"
#include "log.h"
#include "events.h"
#include "memdebug.h"
#define MAX_CMD_ITEMS 100
#define MSG_SYNTAX_ERROR "syntax error\r\n"
#define MSG_INVAL_ERROR "invalid argument\r\n"
#define MSG_UNKNOWN_CMD "command unknown\r\n"
static const char helpcmd[] = "help";
static const size_t helpcmd_len = sizeof(helpcmd) -1;
char *conf_cli_passwd;
static const char *def_cli_prompt = "accel-ppp";
char *conf_cli_prompt;
static LIST_HEAD(simple_cmd_list);
static LIST_HEAD(regexp_cmd_list);
void __export cli_register_simple_cmd(struct cli_simple_cmd_t *cmd)
{
list_add_tail(&cmd->entry, &simple_cmd_list);
}
void __export cli_register_simple_cmd2(
int (*exec)(const char *cmd, char * const *fields, int fields_cnt, void *client),
void (*help)(char * const *fields, int fields_cnt, void *client),
int hdr_len,
...
)
{
struct cli_simple_cmd_t *c;
int i;
va_list ap;
va_start(ap, hdr_len);
c = malloc(sizeof(*c));
memset(c, 0, sizeof(*c));
c->exec = exec;
c->help = help;
c->hdr_len = hdr_len;
c->hdr = malloc(hdr_len * sizeof(void*));
for (i = 0; i < hdr_len; i++)
c->hdr[i] = va_arg(ap, char *);
list_add_tail(&c->entry, &simple_cmd_list);
va_end(ap);
}
void __export cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd)
{
int err;
int erroffset;
const char *errptr;
cmd->re = pcre_compile2(cmd->pattern, cmd->options, &err,
&errptr, &erroffset, NULL);
if (!cmd->re) {
log_emerg("cli: failed to compile regexp \"%s\": %s (error %i)"
" at positon %i (unprocessed characters: \"%s\")\n",
cmd->pattern, errptr, err, erroffset,
cmd->pattern + erroffset);
_exit(EXIT_FAILURE);
}
list_add_tail(&cmd->entry, ®exp_cmd_list);
}
int __export cli_send(void *client, const char *data)
{
struct cli_client_t *cln = (struct cli_client_t *)client;
return cln->send(cln, data, strlen(data));
}
int __export cli_sendv(void *client, const char *fmt, ...)
{
struct cli_client_t *cln = (struct cli_client_t *)client;
int r;
va_list ap;
va_start(ap, fmt);
r = cln->sendv(cln, fmt, ap);
va_end(ap);
return r;
}
static char *skip_word(char *ptr)
{
for(; *ptr; ptr++)
if (!isgraph(*ptr))
break;
return ptr;
}
static char *skip_space(char *ptr)
{
for(; *ptr; ptr++)
if (*ptr != ' ' && *ptr != '\t')
break;
return ptr;
}
static int split(char *buf, char **ptr)
{
int i;
buf = skip_space(buf);
if (!*buf)
return 0;
for (i = 0; i < MAX_CMD_ITEMS; i++) {
ptr[i] = buf;
buf = skip_word(buf);
if (!*buf)
return i + 1;
*buf = 0;
buf = skip_space(buf + 1);
if (!*buf)
return i + 1;
}
return i;
}
static int cli_process_help_cmd(struct cli_client_t *cln)
{
struct cli_regexp_cmd_t *recmd = NULL;
struct cli_simple_cmd_t *sicmd = NULL;
char *cmd = (char *)cln->cmdline;
char *items[MAX_CMD_ITEMS] = { 0 };
int nb_items;
cmd = skip_space(cmd);
if (strncmp(helpcmd, cmd, helpcmd_len) != 0)
return 0;
if (!isblank(cmd[helpcmd_len]) && cmd[helpcmd_len] != '\0')
return 0;
nb_items = split(cmd, items);
list_for_each_entry(recmd, ®exp_cmd_list, entry)
if (recmd->help)
recmd->help(items, nb_items, cln);
list_for_each_entry(sicmd, &simple_cmd_list, entry)
if (sicmd->help)
sicmd->help(items, nb_items, cln);
return 1;
}
static int cli_process_regexp_cmd(struct cli_client_t *cln, int *err)
{
struct cli_regexp_cmd_t *recmd = NULL;
char *cmd = (char *)cln->cmdline;
int found = 0;
int res;
list_for_each_entry(recmd, ®exp_cmd_list, entry)
if (pcre_exec(recmd->re, NULL, cmd, strlen(cmd),
0, 0, NULL, 0) >= 0) {
found = 1;
res = recmd->exec(cmd, cln);
if (res != CLI_CMD_OK)
break;
}
if (found)
*err = res;
return found;
}
static int cli_process_simple_cmd(struct cli_client_t *cln, int *err)
{
struct cli_simple_cmd_t *sicmd = NULL;
char *cmd = (char *)cln->cmdline;
char *items[MAX_CMD_ITEMS] = { 0 };
int found = 0;
int nb_items;
int indx;
int res;
nb_items = split(cmd, items);
list_for_each_entry(sicmd, &simple_cmd_list, entry) {
if (sicmd->hdr_len <= 0 || nb_items < sicmd->hdr_len)
continue;
for (indx = 0; indx < sicmd->hdr_len; ++indx) {
if (strcmp(sicmd->hdr[indx], items[indx]) != 0)
break;
}
if (indx == sicmd->hdr_len) {
found = 1;
res = sicmd->exec(cmd, items, nb_items, cln);
if (res != CLI_CMD_OK)
break;
}
}
if (found)
*err = res;
return found;
}
int __export cli_process_cmd(struct cli_client_t *cln)
{
int found;
int err;
if (cli_process_help_cmd(cln))
return 0;
found = cli_process_regexp_cmd(cln, &err);
if (found && err != CLI_CMD_OK)
goto out_found;
found |= cli_process_simple_cmd(cln, &err);
if (found)
goto out_found;
if (cli_send(cln, MSG_UNKNOWN_CMD))
return -1;
else
return 0;
out_found:
switch (err) {
case CLI_CMD_EXIT:
cln->disconnect(cln);
case CLI_CMD_FAILED:
return -1;
case CLI_CMD_SYNTAX:
cli_send(cln, MSG_SYNTAX_ERROR);
return 0;
case CLI_CMD_INVAL:
cli_send(cln, MSG_INVAL_ERROR);
return 0;
case CLI_CMD_OK:
return 0;
}
return -1;
}
static void load_config(void)
{
const char *opt;
if (conf_cli_passwd)
_free(conf_cli_passwd);
opt = conf_get_opt("cli", "password");
if (opt)
conf_cli_passwd = _strdup(opt);
else
conf_cli_passwd = NULL;
if (conf_cli_prompt && conf_cli_prompt != def_cli_prompt)
_free(conf_cli_prompt);
opt = conf_get_opt("cli", "prompt");
if (opt)
conf_cli_prompt = _strdup(opt);
else
conf_cli_prompt = (char *)def_cli_prompt;
}
static void init(void)
{
load_config();
triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
}
DEFINE_INIT(10, init);
|