forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibcmd.c
221 lines (203 loc) · 5.25 KB
/
libcmd.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
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
/******************************************************************************
* Copyright (C) 2014-2015
* file: libcmd.c
* author: gozfree <[email protected]>
* created: 2015-10-31 16:58
* updated: 2015-10-31 16:58
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <libmacro.h>
#include "libcmd.h"
#define CMD_NAME "libcmd_shell"
#define CMD_PROMPT "libcmd_shell > "
#define CMD_MAX_ARGS 16 /* max number of command args */
#define CMD_MAX_NUM 1024
static struct cmd *g_cmd = NULL;
static char *g_line_read = NULL;
static int parse_line(const char *name, char *argv[])
{
int nargs = 0;
char last = 0;
char *line = (char *)name;
while (nargs < CMD_MAX_ARGS) {
/* skip any white space */
while ((*line == ' ') || (*line == '\t') || (*line == '-')) {
last = *line;
++line;
}
if (*line == '\0') { /* end of line, no more args */
argv[nargs] = NULL;
return (nargs);
}
argv[nargs++] = line; /* begin of argument string */
/* find end of string */
while (*line && (*line != ' ') && (*line != '\t')) {
if (*line == '-') {
if (last != ' ') {
last = *line;
++line;
} else {
break;
}
} else {
last = *line;
++line;
}
}
if (*line == '\0') { /* end of line, no more args */
argv[nargs] = NULL;
return (nargs);
}
*line++ = '\0'; /* terminate current arg */
}
printf("** Too many args (max. %d) **\n", CMD_MAX_ARGS);
return (nargs);
}
static char *cmd_gen(const char *text, int state)
{
static int len = 0;
static int rank = 0;//must be static, search dict continuously
char *key, *val;
if (!state) {
rank = 0;
len = strlen(text);
}
while (1) {
rank = dict_enumerate(g_cmd->dict, rank, &key, &val);
if (rank < 0) {
break;
}
if (strncmp(key, text, len) == 0) {
return strdup(key);
}
}
return NULL;
}
static char **cmd_completion(const char *text, int start, int end)
{
char **matches = NULL;
if (start == 0)
matches = rl_completion_matches(text, cmd_gen);
return matches;
}
static char *cmd_get_input(void)
{
if (g_line_read) {
free(g_line_read);
g_line_read = NULL;
}
g_line_read = readline(CMD_PROMPT);//alloc string, need free
if (g_line_read == NULL) {
printf("readline null\n");
return NULL;
}
add_history(g_line_read);
return g_line_read;
}
struct cmd *cmd_init(int cnt)
{
int res = 0;
struct cmd *cmd = NULL;
do {
if (cnt < 0 || cnt > CMD_MAX_NUM) {
printf("cmd num can't more than %d\n", CMD_MAX_NUM);
res = -1;
break;
}
cmd = CALLOC(1, struct cmd);
if (!cmd) {
printf("malloc cmd failed!\n");
res = -1;
break;
}
cmd->cnt = cnt;
cmd->dict = dict_new();
if (!cmd->dict) {
printf("dict_new failed!\n");
res = -1;
break;
}
rl_readline_name = CMD_NAME;
rl_attempted_completion_function = cmd_completion;
} while (0);
if (UNLIKELY(res != 0)) {
if (cmd->dict) dict_free(cmd->dict);
if (cmd) free(cmd);
}
g_cmd = cmd;
return cmd;
}
void cmd_deinit(struct cmd *cmd)
{
if (!cmd) return;
if (cmd->dict) dict_free(cmd->dict);
if (cmd) free(cmd);
}
int cmd_register(struct cmd *cmd, const char *name, cmd_cb func)
{
if (!cmd || !name || !func) {
printf("%s: invalid paraments!", __func__);
return -1;
}
if (dict_add(cmd->dict, (char *)name, (char *)func) < 0) {
printf("dict_add %s failed!\n", name);
return -1;
}
return 0;
}
int cmd_execute(struct cmd *cmd, const char *name)
{
char *argv[CMD_MAX_ARGS + 1];
int argc;
if (!cmd || !name) {
printf("%s: invalid paraments!", __func__);
return -1;
}
if ((argc = parse_line(name, argv)) == 0) {
//printf("parse_line failed!\n");
return -1;
}
cmd_cb func = (cmd_cb)dict_get(cmd->dict, argv[0], NULL);
if (!func) {
printf("dict_get %s failed!\n", argv[0]);
return -1;
}
if ((func)(argc, argv) != 0) {
printf("run do_cmd error\n");
}
return 0;
}
int cmd_get_registered(struct cmd *cmd)
{
if (!cmd) {
printf("%s: invalid paraments!", __func__);
return -1;
}
int i = 0;
int rank = 0;
char *key, *val;
while (1) {
rank = dict_enumerate(cmd->dict, rank, &key, &val);
if (rank < 0) {
break;
}
printf("key = %s\n", key);
++i;
}
return i;
}
void cmd_loop(struct cmd *cmd)
{
char *s;
while (1) {
s = cmd_get_input();
if (!s) {
continue;
}
cmd_execute(cmd, s);
}
}