1
2
3
4
5
6
7
8
9
10
11#include <common.h>
12#include <bootretry.h>
13#include <cli.h>
14#include <console.h>
15#include <linux/ctype.h>
16
17#define DEBUG_PARSER 0
18
19#define debug_parser(fmt, args...) \
20 debug_cond(DEBUG_PARSER, fmt, ##args)
21
22
23int cli_simple_parse_line(char *line, char *argv[])
24{
25 int nargs = 0;
26
27 debug_parser("%s: \"%s\"\n", __func__, line);
28 while (nargs < CONFIG_SYS_MAXARGS) {
29
30 while (isblank(*line))
31 ++line;
32
33 if (*line == '\0') {
34 argv[nargs] = NULL;
35 debug_parser("%s: nargs=%d\n", __func__, nargs);
36 return nargs;
37 }
38
39 argv[nargs++] = line;
40
41
42 while (*line && !isblank(*line))
43 ++line;
44
45 if (*line == '\0') {
46 argv[nargs] = NULL;
47 debug_parser("parse_line: nargs=%d\n", nargs);
48 return nargs;
49 }
50
51 *line++ = '\0';
52 }
53
54 printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
55
56 debug_parser("%s: nargs=%d\n", __func__, nargs);
57 return nargs;
58}
59
60void cli_simple_process_macros(const char *input, char *output)
61{
62 char c, prev;
63 const char *varname_start = NULL;
64 int inputcnt = strlen(input);
65 int outputcnt = CONFIG_SYS_CBSIZE;
66 int state = 0;
67
68
69
70
71 char __maybe_unused *output_start = output;
72
73 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
74 input);
75
76 prev = '\0';
77
78 while (inputcnt && outputcnt) {
79 c = *input++;
80 inputcnt--;
81
82 if (state != 3) {
83
84 if ((c == '\\') && (prev != '\\')) {
85 if (inputcnt-- == 0)
86 break;
87 prev = c;
88 c = *input++;
89 }
90 }
91
92 switch (state) {
93 case 0:
94 if ((c == '\'') && (prev != '\\')) {
95 state = 3;
96 break;
97 }
98 if ((c == '$') && (prev != '\\')) {
99 state++;
100 } else {
101 *(output++) = c;
102 outputcnt--;
103 }
104 break;
105 case 1:
106 if (c == '(' || c == '{') {
107 state++;
108 varname_start = input;
109 } else {
110 state = 0;
111 *(output++) = '$';
112 outputcnt--;
113
114 if (outputcnt) {
115 *(output++) = c;
116 outputcnt--;
117 }
118 }
119 break;
120 case 2:
121 if (c == ')' || c == '}') {
122 int i;
123 char envname[CONFIG_SYS_CBSIZE], *envval;
124
125 int envcnt = input - varname_start - 1;
126
127
128 for (i = 0; i < envcnt; i++)
129 envname[i] = varname_start[i];
130 envname[i] = 0;
131
132
133 envval = env_get(envname);
134
135
136 if (envval != NULL)
137 while ((*envval) && outputcnt) {
138 *(output++) = *(envval++);
139 outputcnt--;
140 }
141
142 state = 0;
143 }
144 break;
145 case 3:
146 if ((c == '\'') && (prev != '\\')) {
147 state = 0;
148 } else {
149 *(output++) = c;
150 outputcnt--;
151 }
152 break;
153 }
154 prev = c;
155 }
156
157 if (outputcnt)
158 *output = 0;
159 else
160 *(output - 1) = 0;
161
162 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
163 strlen(output_start), output_start);
164}
165
166
167
168
169
170
171
172
173
174int cli_simple_run_command(const char *cmd, int flag)
175{
176 char cmdbuf[CONFIG_SYS_CBSIZE];
177 char *token;
178 char *sep;
179 char finaltoken[CONFIG_SYS_CBSIZE];
180 char *str = cmdbuf;
181 char *argv[CONFIG_SYS_MAXARGS + 1];
182 int argc, inquotes;
183 int repeatable = 1;
184 int rc = 0;
185
186 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
187 if (DEBUG_PARSER) {
188
189 puts(cmd ? cmd : "NULL");
190 puts("\"\n");
191 }
192 clear_ctrlc();
193
194 if (!cmd || !*cmd)
195 return -1;
196
197 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
198 puts("## Command too long!\n");
199 return -1;
200 }
201
202 strcpy(cmdbuf, cmd);
203
204
205
206
207
208 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
209 while (*str) {
210
211
212
213
214 for (inquotes = 0, sep = str; *sep; sep++) {
215 if ((*sep == '\'') &&
216 (*(sep - 1) != '\\'))
217 inquotes = !inquotes;
218
219 if (!inquotes &&
220 (*sep == ';') &&
221 (sep != str) &&
222 (*(sep - 1) != '\\'))
223 break;
224 }
225
226
227
228
229 token = str;
230 if (*sep) {
231 str = sep + 1;
232 *sep = '\0';
233 } else {
234 str = sep;
235 }
236 debug_parser("token: \"%s\"\n", token);
237
238
239 cli_simple_process_macros(token, finaltoken);
240
241
242 argc = cli_simple_parse_line(finaltoken, argv);
243 if (argc == 0) {
244 rc = -1;
245 continue;
246 }
247
248 if (cmd_process(flag, argc, argv, &repeatable, NULL))
249 rc = -1;
250
251
252 if (had_ctrlc())
253 return -1;
254 }
255
256 return rc ? rc : repeatable;
257}
258
259void cli_simple_loop(void)
260{
261 static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };
262
263 int len;
264 int flag;
265 int rc = 1;
266
267 for (;;) {
268 if (rc >= 0) {
269
270
271
272 bootretry_reset_cmd_timeout();
273 }
274 len = cli_readline(CONFIG_SYS_PROMPT);
275
276 flag = 0;
277 if (len > 0)
278 strlcpy(lastcommand, console_buffer,
279 CONFIG_SYS_CBSIZE + 1);
280 else if (len == 0)
281 flag |= CMD_FLAG_REPEAT;
282#ifdef CONFIG_BOOT_RETRY_TIME
283 else if (len == -2) {
284
285
286 puts("\nTimed out waiting for command\n");
287# ifdef CONFIG_RESET_TO_RETRY
288
289 do_reset(NULL, 0, 0, NULL);
290# else
291 return;
292# endif
293 }
294#endif
295
296 if (len == -1)
297 puts("<INTERRUPT>\n");
298 else
299 rc = run_command_repeatable(lastcommand, flag);
300
301 if (rc <= 0) {
302
303 lastcommand[0] = 0;
304 }
305 }
306}
307
308int cli_simple_run_command_list(char *cmd, int flag)
309{
310 char *line, *next;
311 int rcode = 0;
312
313
314
315
316
317 next = cmd;
318 line = cmd;
319 while (*next) {
320 if (*next == '\n') {
321 *next = '\0';
322
323 if (*line) {
324 debug("** exec: \"%s\"\n", line);
325 if (cli_simple_run_command(line, 0) < 0) {
326 rcode = 1;
327 break;
328 }
329 }
330 line = next + 1;
331 }
332 ++next;
333 }
334 if (rcode == 0 && *line)
335 rcode = (cli_simple_run_command(line, 0) < 0);
336
337 return rcode;
338}
339