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