]>
Commit | Line | Data |
---|---|---|
35edb766 | 1 | /* |
04b3ea0e | 2 | * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de> |
35edb766 L |
3 | * |
4 | * (C) Copyright 2000 | |
5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
6 | * | |
7 | * Add to readline cmdline-editing by | |
8 | * (C) Copyright 2005 | |
9 | * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> | |
10 | * | |
8ed66016 | 11 | * SPDX-License-Identifier: GPL-2.0 |
35edb766 L |
12 | */ |
13 | ||
8ed66016 | 14 | #include "cli.h" |
2dec14cd | 15 | #include "command.h" |
d684c216 L |
16 | |
17 | #include "config.h" | |
d684c216 L |
18 | #include "debug.h" |
19 | #include "env.h" | |
20 | #include "cli_readline.h" | |
21 | #include "con-utils.h" | |
d684c216 | 22 | |
bbd45c46 L |
23 | |
24 | /* FIXME: Quoting problems */ | |
25 | ||
d684c216 L |
26 | #define DEBUG_PARSER 0 /* set to 1 to debug */ |
27 | ||
28 | #define debug_parser(fmt, args...) \ | |
29 | debug_cond(DEBUG_PARSER, fmt, ##args) | |
30 | ||
13e88ed5 | 31 | |
78035a8d L |
32 | static int_least8_t exec_flags; |
33 | #define OPT_XTRACE 1 | |
34 | #define OPT_VERBOSE 2 | |
35 | ||
13e88ed5 L |
36 | static int_least8_t command_level; |
37 | ||
38 | static void cli_trace_cmd(int_fast8_t level, int argc, char *argv[]) | |
39 | { | |
40 | while (level-- > 0) | |
41 | putchar('+'); | |
42 | for (int_fast8_t i = 0; i < argc; i++) | |
43 | printf_P(PSTR(" %s"), argv[i]); | |
44 | putchar('\n'); | |
45 | } | |
46 | ||
47 | ||
48 | ||
49 | ||
d684c216 L |
50 | static int cli_parse_line(char *line, char *argv[]) |
51 | { | |
b0e4f7e5 | 52 | uint_fast8_t state = 0; |
d684c216 | 53 | uint_fast8_t nargs = 0; |
b0e4f7e5 L |
54 | char *inp, *outp; |
55 | char c, quote; | |
56 | ||
57 | debug_parser("%s: \"%s\"\n", __func__, line); | |
58 | ||
aca998c3 | 59 | for (outp = inp = line, quote = '\0'; (c = *inp) != '\0'; inp++) { |
b0e4f7e5 L |
60 | |
61 | switch (state) { | |
bbd45c46 | 62 | case 0: /* before arg string, waiting for arg start */ |
b0e4f7e5 L |
63 | if (isblank(c)) |
64 | continue; | |
65 | ||
66 | argv[nargs++] = inp; /* begin of argument string */ | |
67 | outp = inp; | |
68 | state = 1; | |
69 | /* fall thru */ | |
70 | ||
bbd45c46 | 71 | case 1: /* in arg string, waiting for end of arg string */ |
b0e4f7e5 L |
72 | if (c == '\\') { |
73 | ++state; | |
74 | continue; | |
75 | } | |
76 | if (c == '\"' || c == '\'') { | |
77 | quote = c; | |
78 | state = 3; | |
79 | continue; | |
80 | } | |
81 | if (isblank(c)) { | |
82 | c = '\0'; | |
83 | state = 0; | |
84 | } | |
85 | break; | |
86 | ||
bbd45c46 | 87 | case 3: /* in quote */ |
b0e4f7e5 L |
88 | if (c == '\\' && quote == '\"') { |
89 | ++state; | |
90 | continue; | |
91 | } | |
92 | if (c == quote) { | |
93 | state = 1; | |
94 | continue; | |
95 | } | |
96 | break; | |
d684c216 | 97 | |
bbd45c46 | 98 | case 2: /* waiting for next char */ |
b0e4f7e5 L |
99 | case 4: |
100 | --state; | |
101 | break; | |
d684c216 | 102 | |
b0e4f7e5 | 103 | } |
aca998c3 L |
104 | |
105 | if (nargs > CONFIG_SYS_MAXARGS) { | |
106 | --nargs; | |
107 | break; | |
108 | } | |
b0e4f7e5 | 109 | *outp++ = c; |
d684c216 L |
110 | } |
111 | ||
b0e4f7e5 | 112 | if (*inp != '\0') |
d684c216 L |
113 | printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS); |
114 | ||
b0e4f7e5 | 115 | *outp = '\0'; |
d684c216 | 116 | argv[nargs] = NULL; |
b0e4f7e5 L |
117 | debug_parser("%s: nargs=%d\n", __func__, nargs); |
118 | #if 0 | |
119 | for (int i = 0; i < nargs; i++) | |
120 | debug_parser("%s: arg %d: >%s<\n", __func__, i, argv[i]); | |
121 | #endif | |
d684c216 | 122 | return nargs; |
b0e4f7e5 | 123 | |
d684c216 L |
124 | } |
125 | ||
507d25e2 L |
126 | static |
127 | void append_char(uint_fast8_t pass, char **p, char c) | |
d684c216 | 128 | { |
507d25e2 L |
129 | |
130 | if (pass) { | |
131 | **p = c; | |
132 | } | |
133 | ++(*p); | |
134 | } | |
135 | ||
136 | static | |
78035a8d | 137 | char *process_macros(char *input) |
507d25e2 | 138 | { |
78035a8d L |
139 | char c, prev, *inp; |
140 | char *output = NULL; | |
141 | char *outp = NULL; | |
507d25e2 L |
142 | const char *varname = NULL; |
143 | ||
78035a8d L |
144 | debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", |
145 | strlen(input), input); | |
146 | ||
507d25e2 L |
147 | for(uint_fast8_t pass = 0; pass < 2; pass++) |
148 | { | |
b0e4f7e5 L |
149 | uint_fast8_t state = 0; |
150 | /* 0 = waiting for '$' */ | |
507d25e2 L |
151 | /* 1 = waiting for '{' */ |
152 | /* 2 = waiting for '}' */ | |
153 | /* 3 = waiting for ''' */ | |
154 | ||
78035a8d L |
155 | if (pass > 0) { |
156 | size_t outputlen = outp - (char *) NULL; | |
157 | output = (char *) malloc(outputlen); | |
158 | if (output == NULL) { | |
159 | printf_P(PSTR("** Can't process command: Out of memory! **\n")); | |
160 | return NULL; | |
161 | } | |
507d25e2 | 162 | outp = output; |
d684c216 L |
163 | } |
164 | ||
507d25e2 | 165 | inp = input; |
507d25e2 | 166 | |
b0e4f7e5 | 167 | for (prev = '\0'; (c = *inp++) != '\0'; prev = c) { |
507d25e2 | 168 | |
507d25e2 | 169 | |
507d25e2 L |
170 | |
171 | switch (state) { | |
172 | case 0: /* Waiting for (unescaped) $ */ | |
173 | if ((c == '\'') && (prev != '\\')) { | |
174 | state = 3; | |
175 | break; | |
176 | } | |
b0e4f7e5 | 177 | if ((c == '$') && (prev != '\\')) { |
507d25e2 | 178 | state++; |
b0e4f7e5 L |
179 | continue; |
180 | } | |
507d25e2 L |
181 | break; |
182 | case 1: /* Waiting for { */ | |
183 | if (c == '{') { | |
184 | state++; | |
185 | varname = inp; | |
b0e4f7e5 | 186 | continue; |
507d25e2 L |
187 | } else { |
188 | state = 0; | |
189 | append_char(pass, &outp, '$'); | |
507d25e2 L |
190 | } |
191 | break; | |
192 | case 2: /* Waiting for } */ | |
193 | if (c == '}') { | |
194 | /* Terminate variable name */ | |
195 | *(inp-1) = '\0'; | |
bddc7e77 | 196 | const char *envval = getenv_str(varname); |
507d25e2 L |
197 | *(inp-1) = '}'; |
198 | /* Copy into the line if it exists */ | |
199 | if (envval != NULL) | |
200 | while (*envval) | |
201 | append_char(pass, &outp, *(envval++)); | |
202 | /* Look for another '$' */ | |
203 | state = 0; | |
204 | } | |
b0e4f7e5 | 205 | continue; |
507d25e2 | 206 | case 3: /* Waiting for ' */ |
b0e4f7e5 | 207 | if (c == '\'') |
507d25e2 | 208 | state = 0; |
507d25e2 | 209 | break; |
d684c216 | 210 | } |
b0e4f7e5 | 211 | append_char(pass, &outp, c); |
d684c216 | 212 | } |
d684c216 | 213 | |
507d25e2 L |
214 | append_char(pass, &outp, 0); |
215 | } | |
d684c216 L |
216 | |
217 | debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", | |
507d25e2 L |
218 | strlen(output), output); |
219 | ||
220 | return output; | |
d684c216 L |
221 | } |
222 | ||
f76ca346 L |
223 | /** |
224 | * | |
225 | * | |
d684c216 L |
226 | * WARNING: |
227 | * | |
228 | * We must create a temporary copy of the command since the command we get | |
bddc7e77 | 229 | * may be the result from getenv_str(), which returns a pointer directly to |
d684c216 L |
230 | * the environment data, which may change magicly when the command we run |
231 | * creates or modifies environment variables (like "bootp" does). | |
f76ca346 L |
232 | * |
233 | * | |
234 | * @param cmd | |
235 | * @param flag | |
236 | * @returns | |
237 | * | |
d684c216 | 238 | */ |
fcf1d5b3 | 239 | static int cli_run_command(const char *cmd, uint_fast8_t flag) |
d684c216 | 240 | { |
78035a8d | 241 | char cmdbuf[strlen(cmd) + 1]; /* working copy of cmd */ |
d684c216 L |
242 | char *token; /* start of token in cmdbuf */ |
243 | char *sep; /* end of token (separator) in cmdbuf */ | |
507d25e2 | 244 | char *finaltoken = NULL; /* token after macro expansion */ |
d684c216 L |
245 | char *str; |
246 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ | |
247 | int argc; | |
248 | uint_fast8_t inquotes, repeatable = 1; | |
249 | int rc = 0; | |
250 | ||
78035a8d | 251 | exec_flags = 0; |
13e88ed5 L |
252 | char *optenv = getenv_str(PSTR("cli")); |
253 | if (optenv) { | |
78035a8d L |
254 | if (strstr_P(optenv, PSTR("verbose")) != NULL) |
255 | exec_flags |= OPT_VERBOSE; | |
256 | if (strstr_P(optenv, PSTR("xtrace")) != NULL) | |
257 | exec_flags |= OPT_XTRACE; | |
13e88ed5 L |
258 | } |
259 | ||
f76ca346 | 260 | debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n", |
d684c216 L |
261 | cmd, cmd ? cmd : "NULL"); |
262 | ||
78035a8d | 263 | if (exec_flags & OPT_VERBOSE) |
13e88ed5 L |
264 | printf_P(PSTR("%s\n"), cmd, cmd ? cmd : ""); |
265 | ||
d684c216 L |
266 | if (!cmd || !*cmd) |
267 | return -1; /* empty command */ | |
268 | ||
78035a8d | 269 | clear_ctrlc(); /* forget any previous Control C */ |
d684c216 | 270 | |
78035a8d L |
271 | str = strcpy(cmdbuf, cmd); |
272 | //str = cmdbuf; | |
13e88ed5 | 273 | ++command_level; |
d684c216 L |
274 | |
275 | /* Process separators and check for invalid | |
276 | * repeatable commands | |
277 | */ | |
278 | ||
78035a8d | 279 | debug_parser("[PROCESS_SEPARATORS] \"%s\"\n", cmd); |
d684c216 L |
280 | while (*str) { |
281 | /* | |
282 | * Find separator, or string end | |
283 | * Allow simple escape of ';' by writing "\;" | |
284 | */ | |
285 | for (inquotes = 0, sep = str; *sep; sep++) { | |
286 | if ((*sep == '\'') && | |
01c1907d L |
287 | (sep != str) && /* past string start */ |
288 | (*(sep - 1) != '\\')) /* and NOT escaped */ | |
d684c216 L |
289 | inquotes = !inquotes; |
290 | ||
291 | if (!inquotes && | |
01c1907d L |
292 | (*sep == ';' || *sep == '\n' /* separator */ |
293 | || *sep == '#') && /* or start of comment */ | |
294 | ((sep == str) || /* string start */ | |
295 | (*(sep - 1) != '\\'))) /* or NOT escaped */ | |
d684c216 L |
296 | break; |
297 | } | |
298 | ||
01c1907d L |
299 | /* no more commands after unescaped '#' token */ |
300 | if (*sep == '#') | |
301 | *sep = '\0'; | |
302 | ||
303 | /* Limit the token to data between separators */ | |
d684c216 L |
304 | token = str; |
305 | if (*sep) { | |
01c1907d | 306 | str = sep + 1; /* start of command for next pass */ |
d684c216 L |
307 | *sep = '\0'; |
308 | } else { | |
01c1907d | 309 | str = sep; /* no more commands for next pass */ |
d684c216 | 310 | } |
7a66f43c L |
311 | while (isblank(*token)) |
312 | ++token; | |
313 | if (*token == '\0') /* empty (comment only) */ | |
314 | continue; | |
d684c216 L |
315 | debug_parser("token: \"%s\"\n", token); |
316 | ||
317 | /* find macros in this token and replace them */ | |
78035a8d L |
318 | finaltoken = process_macros(token); |
319 | if (finaltoken == NULL) { | |
320 | rc = -1; /* no command at all */ | |
321 | continue; | |
322 | } | |
d684c216 L |
323 | |
324 | /* Extract arguments */ | |
325 | argc = cli_parse_line(finaltoken, argv); | |
326 | if (argc == 0) { | |
78035a8d L |
327 | rc = -1; /* no command at all */ |
328 | free(finaltoken); | |
329 | continue; | |
d684c216 L |
330 | } |
331 | ||
78035a8d | 332 | if (exec_flags & OPT_XTRACE) |
13e88ed5 L |
333 | cli_trace_cmd(command_level, argc, argv); |
334 | ||
335 | rc = cmd_process(flag, argc, argv, &repeatable); | |
78035a8d | 336 | free(finaltoken); |
13e88ed5 | 337 | if (rc != CMD_RET_SUCCESS) { |
78035a8d | 338 | if (exec_flags & OPT_VERBOSE) |
13e88ed5 | 339 | printf_P(PSTR("Command failed, result=%d\n"), rc); |
d684c216 | 340 | rc = -1; |
13e88ed5 | 341 | } |
d684c216 L |
342 | |
343 | /* Did the user stop this? */ | |
344 | if (had_ctrlc()) { | |
345 | rc = -1; /* if stopped then not repeatable */ | |
346 | break; | |
347 | } | |
348 | } | |
349 | ||
13e88ed5 | 350 | --command_level; |
d684c216 L |
351 | |
352 | return rc ? rc : repeatable; | |
353 | } | |
354 | ||
355 | static int cli_run_command_list(const char *cmd) | |
356 | { | |
357 | return (cli_run_command(cmd, 0) < 0); | |
358 | } | |
359 | ||
360 | /******************************************************************************/ | |
361 | ||
362 | ||
363 | /* | |
b08e079d | 364 | * Run a command. |
d684c216 L |
365 | * |
366 | * @param cmd Command to run | |
367 | * @param flag Execution flags (CMD_FLAG_...) | |
368 | * @return 0 on success, or != 0 on error. | |
369 | */ | |
fcf1d5b3 | 370 | int run_command(const char *cmd, uint_fast8_t flag) |
d684c216 L |
371 | { |
372 | /* | |
373 | * cli_run_command can return 0 or 1 for success, so clean up | |
374 | * its result. | |
375 | */ | |
376 | if (cli_run_command(cmd, flag) == -1) | |
377 | return 1; | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | /* | |
383 | * Run a command using the selected parser, and check if it is repeatable. | |
384 | * | |
385 | * @param cmd Command to run | |
386 | * @param flag Execution flags (CMD_FLAG_...) | |
387 | * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error. | |
388 | */ | |
fcf1d5b3 | 389 | static int run_command_repeatable(const char *cmd, uint_fast8_t flag) |
d684c216 L |
390 | { |
391 | return cli_run_command(cmd, flag); | |
392 | } | |
393 | ||
394 | int run_command_list(const char *cmd, int len) | |
395 | { | |
396 | (void) len; | |
f76ca346 | 397 | |
d684c216 L |
398 | return cli_run_command_list(cmd); |
399 | } | |
400 | ||
401 | /****************************************************************************/ | |
402 | ||
d684c216 L |
403 | |
404 | void cli_loop(void) | |
405 | { | |
d684c216 | 406 | char *lastcommand = NULL; |
d684c216 | 407 | int len; |
fcf1d5b3 | 408 | uint_fast8_t flag; |
d684c216 L |
409 | int rc = 1; |
410 | ||
411 | for (;;) { | |
66e23a9d | 412 | len = cli_readline(lastcommand ? PSTR(CONFIG_SYS_PROMPT_REPEAT) : PSTR(CONFIG_SYS_PROMPT), 1); |
d684c216 L |
413 | |
414 | flag = 0; /* assume no special flags for now */ | |
415 | if (len > 0) { | |
69988dc1 L |
416 | free (lastcommand); |
417 | lastcommand = strdup(console_buffer); | |
d684c216 L |
418 | } else if (len == 0) |
419 | flag |= CMD_FLAG_REPEAT; | |
420 | ||
13e88ed5 | 421 | if (len == -1) { |
78035a8d | 422 | if (exec_flags & OPT_VERBOSE) |
13e88ed5 L |
423 | my_puts_P(PSTR("<INTERRUPT>\n")); |
424 | } else | |
d684c216 L |
425 | rc = run_command_repeatable(lastcommand, flag); |
426 | ||
427 | if (rc <= 0) { | |
428 | /* invalid command or not repeatable, forget it */ | |
d684c216 L |
429 | free(lastcommand); |
430 | lastcommand = NULL; | |
d684c216 L |
431 | } |
432 | } | |
433 | } |