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
|
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "triton.h"
#include "telnet.h"
#include "cli.h"
#include "log.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 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);
}
void __export cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd)
{
int err;
cmd->re = pcre_compile2(cmd->pattern, cmd->options, &err, NULL, NULL, NULL);
if (!cmd->re) {
log_emerg("cli: failed to compile regexp %s: %i\n", cmd->pattern, err);
_exit(EXIT_FAILURE);
}
list_add_tail(&cmd->entry, &simple_cmd_list);
}
int __export cli_send(void *client, const char *data)
{
struct client_t *cln = (struct client_t *)client;
return telnet_send(cln, data, strlen(data));
}
int __export cli_sendv(void *client, const char *fmt, ...)
{
struct client_t *cln = (struct client_t *)client;
int r;
va_list ap;
va_start(ap, fmt);
r = telnet_sendv(cln, fmt, ap);
va_end(ap);
return r;
}
static char *skip_word(char *ptr)
{
for(; *ptr; ptr++)
if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
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;
ptr[0] = buf;
for (i = 1; i <= MAX_CMD_ITEMS; i++) {
buf = skip_word(buf);
if (!*buf)
return i;
*buf = 0;
buf = skip_space(buf + 1);
if (!*buf)
return i;
ptr[i] = buf;
}
buf = skip_word(buf);
*buf = 0;
return i;
}
int process_cmd(struct client_t *cln)
{
struct cli_simple_cmd_t *cmd1;
struct cli_regexp_cmd_t *cmd2;
char *f[MAX_CMD_ITEMS];
int r, i, n, found = 0;
n = split((char *)cln->cmdline, f);
if (n >= 1 && !strcmp(f[0], "help")) {
list_for_each_entry(cmd1, &simple_cmd_list, entry)
if (cmd1->help)
cmd1->help(f, n, cln);
list_for_each_entry(cmd2, ®exp_cmd_list, entry)
if (cmd2->help)
cmd1->help(f, n, cln);
return 0;
}
list_for_each_entry(cmd1, &simple_cmd_list, entry) {
if (cmd1->hdr_len && n >= cmd1->hdr_len) {
for (i = 0; i < cmd1->hdr_len; i++) {
if (strcmp(cmd1->hdr[i], f[i]))
break;
}
if (i < cmd1->hdr_len)
continue;
r = cmd1->exec((char *)cln->cmdline, f, n, cln);
switch (r) {
case CLI_CMD_EXIT:
telnet_disconnect(cln);
case CLI_CMD_FAILED:
return -1;
case CLI_CMD_SYNTAX:
telnet_send(cln, MSG_SYNTAX_ERROR, sizeof(MSG_SYNTAX_ERROR));
return 0;
case CLI_CMD_INVAL:
telnet_send(cln, MSG_INVAL_ERROR, sizeof(MSG_INVAL_ERROR));
return 0;
case CLI_CMD_OK:
found = 1;
}
}
}
list_for_each_entry(cmd2, ®exp_cmd_list, entry) {
r = cmd2->exec((char *)cln->cmdline, cln);
switch (r) {
case CLI_CMD_EXIT:
telnet_disconnect(cln);
case CLI_CMD_FAILED:
return -1;
case CLI_CMD_SYNTAX:
if (telnet_send(cln, MSG_SYNTAX_ERROR, sizeof(MSG_SYNTAX_ERROR)))
return -1;
return 0;
case CLI_CMD_OK:
found = 1;
}
}
if (!found) {
if (telnet_send(cln, MSG_UNKNOWN_CMD, sizeof(MSG_UNKNOWN_CMD)))
return -1;
}
return 0;
}
|