]>
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" |
ade5670d | 15 | #include "command.h" |
d684c216 | 16 | #include <ctype.h> |
d684c216 L |
17 | |
18 | #include "config.h" | |
d684c216 L |
19 | #include "debug.h" |
20 | #include "env.h" | |
21 | #include "cli_readline.h" | |
22 | #include "con-utils.h" | |
d684c216 | 23 | |
bbd45c46 L |
24 | |
25 | /* FIXME: Quoting problems */ | |
26 | ||
d684c216 L |
27 | #define DEBUG_PARSER 0 /* set to 1 to debug */ |
28 | ||
29 | #define debug_parser(fmt, args...) \ | |
30 | debug_cond(DEBUG_PARSER, fmt, ##args) | |
31 | ||
13e88ed5 L |
32 | |
33 | static bool opt_xtrace; | |
34 | static bool opt_verbose; | |
35 | static int_least8_t command_level; | |
36 | ||
37 | static void cli_trace_cmd(int_fast8_t level, int argc, char *argv[]) | |
38 | { | |
39 | while (level-- > 0) | |
40 | putchar('+'); | |
41 | for (int_fast8_t i = 0; i < argc; i++) | |
42 | printf_P(PSTR(" %s"), argv[i]); | |
43 | putchar('\n'); | |
44 | } | |
45 | ||
46 | ||
47 | ||
48 | ||
d684c216 L |
49 | static int cli_parse_line(char *line, char *argv[]) |
50 | { | |
b0e4f7e5 | 51 | uint_fast8_t state = 0; |
d684c216 | 52 | uint_fast8_t nargs = 0; |
b0e4f7e5 L |
53 | char *inp, *outp; |
54 | char c, quote; | |
55 | ||
56 | debug_parser("%s: \"%s\"\n", __func__, line); | |
57 | ||
aca998c3 | 58 | for (outp = inp = line, quote = '\0'; (c = *inp) != '\0'; inp++) { |
b0e4f7e5 L |
59 | |
60 | switch (state) { | |
bbd45c46 | 61 | case 0: /* before arg string, waiting for arg start */ |
b0e4f7e5 L |
62 | if (isblank(c)) |
63 | continue; | |
64 | ||
65 | argv[nargs++] = inp; /* begin of argument string */ | |
66 | outp = inp; | |
67 | state = 1; | |
68 | /* fall thru */ | |
69 | ||
bbd45c46 | 70 | case 1: /* in arg string, waiting for end of arg string */ |
b0e4f7e5 L |
71 | if (c == '\\') { |
72 | ++state; | |
73 | continue; | |
74 | } | |
75 | if (c == '\"' || c == '\'') { | |
76 | quote = c; | |
77 | state = 3; | |
78 | continue; | |
79 | } | |
80 | if (isblank(c)) { | |
81 | c = '\0'; | |
82 | state = 0; | |
83 | } | |
84 | break; | |
85 | ||
bbd45c46 | 86 | case 3: /* in quote */ |
b0e4f7e5 L |
87 | if (c == '\\' && quote == '\"') { |
88 | ++state; | |
89 | continue; | |
90 | } | |
91 | if (c == quote) { | |
92 | state = 1; | |
93 | continue; | |
94 | } | |
95 | break; | |
d684c216 | 96 | |
bbd45c46 | 97 | case 2: /* waiting for next char */ |
b0e4f7e5 L |
98 | case 4: |
99 | --state; | |
100 | break; | |
d684c216 | 101 | |
b0e4f7e5 | 102 | } |
aca998c3 L |
103 | |
104 | if (nargs > CONFIG_SYS_MAXARGS) { | |
105 | --nargs; | |
106 | break; | |
107 | } | |
b0e4f7e5 | 108 | *outp++ = c; |
d684c216 L |
109 | } |
110 | ||
b0e4f7e5 | 111 | if (*inp != '\0') |
d684c216 L |
112 | printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS); |
113 | ||
b0e4f7e5 | 114 | *outp = '\0'; |
d684c216 | 115 | argv[nargs] = NULL; |
b0e4f7e5 L |
116 | debug_parser("%s: nargs=%d\n", __func__, nargs); |
117 | #if 0 | |
118 | for (int i = 0; i < nargs; i++) | |
119 | debug_parser("%s: arg %d: >%s<\n", __func__, i, argv[i]); | |
120 | #endif | |
d684c216 | 121 | return nargs; |
b0e4f7e5 | 122 | |
d684c216 L |
123 | } |
124 | ||
507d25e2 L |
125 | static |
126 | void append_char(uint_fast8_t pass, char **p, char c) | |
d684c216 | 127 | { |
507d25e2 L |
128 | |
129 | if (pass) { | |
130 | **p = c; | |
131 | } | |
132 | ++(*p); | |
133 | } | |
134 | ||
135 | static | |
136 | char *process_macros(char *input, char *output) | |
137 | { | |
138 | char c, prev, *inp, *outp; | |
139 | const char *varname = NULL; | |
140 | ||
141 | for(uint_fast8_t pass = 0; pass < 2; pass++) | |
142 | { | |
b0e4f7e5 L |
143 | uint_fast8_t state = 0; |
144 | /* 0 = waiting for '$' */ | |
507d25e2 L |
145 | /* 1 = waiting for '{' */ |
146 | /* 2 = waiting for '}' */ | |
147 | /* 3 = waiting for ''' */ | |
148 | ||
149 | if (pass == 0) { | |
150 | outp = output; | |
151 | } else { | |
152 | int outputlen = outp - output; | |
ade5670d L |
153 | outp = realloc(output, outputlen); |
154 | if (outp == NULL) { | |
155 | free(output); | |
156 | output = outp; | |
157 | break; | |
158 | } | |
507d25e2 | 159 | output = outp; |
d684c216 L |
160 | } |
161 | ||
507d25e2 | 162 | inp = input; |
507d25e2 | 163 | |
b0e4f7e5 L |
164 | debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", |
165 | strlen(inp), inp); | |
166 | ||
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 | */ |
93ea25f2 | 239 | static int cli_run_command(const char *cmd, uint_fast8_t flag) |
d684c216 L |
240 | { |
241 | char *cmdbuf; /* working copy of cmd */ | |
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 | ||
13e88ed5 L |
251 | opt_verbose = 0; |
252 | opt_xtrace = 0; | |
253 | char *optenv = getenv_str(PSTR("cli")); | |
254 | if (optenv) { | |
255 | opt_verbose = (strstr_P(optenv, PSTR("verbose")) != NULL); | |
256 | opt_xtrace = (strstr_P(optenv, PSTR("xtrace")) != NULL); | |
257 | } | |
258 | ||
f76ca346 | 259 | debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n", |
d684c216 L |
260 | cmd, cmd ? cmd : "NULL"); |
261 | ||
13e88ed5 L |
262 | if (opt_verbose) |
263 | printf_P(PSTR("%s\n"), cmd, cmd ? cmd : ""); | |
264 | ||
d684c216 L |
265 | clear_ctrlc(); /* forget any previous Control C */ |
266 | ||
267 | if (!cmd || !*cmd) | |
268 | return -1; /* empty command */ | |
269 | ||
69988dc1 | 270 | cmdbuf = strdup(cmd); |
507d25e2 | 271 | if (!cmdbuf) |
69988dc1 | 272 | return -1; /* not enough memory */ |
d684c216 | 273 | |
13e88ed5 | 274 | ++command_level; |
d684c216 L |
275 | str = cmdbuf; |
276 | ||
277 | /* Process separators and check for invalid | |
278 | * repeatable commands | |
279 | */ | |
280 | ||
281 | debug_parser("[PROCESS_SEPARATORS] %s\n", cmd); | |
282 | while (*str) { | |
283 | /* | |
284 | * Find separator, or string end | |
285 | * Allow simple escape of ';' by writing "\;" | |
286 | */ | |
287 | for (inquotes = 0, sep = str; *sep; sep++) { | |
288 | if ((*sep == '\'') && | |
01c1907d L |
289 | (sep != str) && /* past string start */ |
290 | (*(sep - 1) != '\\')) /* and NOT escaped */ | |
d684c216 L |
291 | inquotes = !inquotes; |
292 | ||
293 | if (!inquotes && | |
01c1907d L |
294 | (*sep == ';' || *sep == '\n' /* separator */ |
295 | || *sep == '#') && /* or start of comment */ | |
296 | ((sep == str) || /* string start */ | |
297 | (*(sep - 1) != '\\'))) /* or NOT escaped */ | |
d684c216 L |
298 | break; |
299 | } | |
300 | ||
01c1907d L |
301 | /* no more commands after unescaped '#' token */ |
302 | if (*sep == '#') | |
303 | *sep = '\0'; | |
304 | ||
305 | /* Limit the token to data between separators */ | |
d684c216 L |
306 | token = str; |
307 | if (*sep) { | |
01c1907d | 308 | str = sep + 1; /* start of command for next pass */ |
d684c216 L |
309 | *sep = '\0'; |
310 | } else { | |
01c1907d | 311 | str = sep; /* no more commands for next pass */ |
d684c216 L |
312 | } |
313 | debug_parser("token: \"%s\"\n", token); | |
314 | ||
315 | /* find macros in this token and replace them */ | |
507d25e2 | 316 | finaltoken = process_macros(token, finaltoken); |
ade5670d L |
317 | if (finaltoken == NULL) { |
318 | rc = -1; /* no command at all */ | |
319 | break; | |
320 | } | |
d684c216 L |
321 | |
322 | /* Extract arguments */ | |
323 | argc = cli_parse_line(finaltoken, argv); | |
324 | if (argc == 0) { | |
325 | rc = -1; /* no command at all */ | |
326 | continue; | |
327 | } | |
328 | ||
13e88ed5 L |
329 | if (opt_xtrace) |
330 | cli_trace_cmd(command_level, argc, argv); | |
331 | ||
332 | rc = cmd_process(flag, argc, argv, &repeatable); | |
333 | if (rc != CMD_RET_SUCCESS) { | |
334 | if (opt_verbose) | |
335 | printf_P(PSTR("Command failed, result=%d\n"), rc); | |
d684c216 | 336 | rc = -1; |
13e88ed5 | 337 | } |
d684c216 L |
338 | |
339 | /* Did the user stop this? */ | |
340 | if (had_ctrlc()) { | |
341 | rc = -1; /* if stopped then not repeatable */ | |
342 | break; | |
343 | } | |
344 | } | |
345 | ||
346 | free(cmdbuf); | |
347 | free(finaltoken); | |
13e88ed5 | 348 | --command_level; |
d684c216 L |
349 | |
350 | return rc ? rc : repeatable; | |
351 | } | |
352 | ||
353 | static int cli_run_command_list(const char *cmd) | |
354 | { | |
355 | return (cli_run_command(cmd, 0) < 0); | |
356 | } | |
357 | ||
358 | /******************************************************************************/ | |
359 | ||
360 | ||
361 | /* | |
b08e079d | 362 | * Run a command. |
d684c216 L |
363 | * |
364 | * @param cmd Command to run | |
365 | * @param flag Execution flags (CMD_FLAG_...) | |
366 | * @return 0 on success, or != 0 on error. | |
367 | */ | |
93ea25f2 | 368 | int run_command(const char *cmd, uint_fast8_t flag) |
d684c216 L |
369 | { |
370 | /* | |
371 | * cli_run_command can return 0 or 1 for success, so clean up | |
372 | * its result. | |
373 | */ | |
374 | if (cli_run_command(cmd, flag) == -1) | |
375 | return 1; | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | /* | |
381 | * Run a command using the selected parser, and check if it is repeatable. | |
382 | * | |
383 | * @param cmd Command to run | |
384 | * @param flag Execution flags (CMD_FLAG_...) | |
385 | * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error. | |
386 | */ | |
93ea25f2 | 387 | static int run_command_repeatable(const char *cmd, uint_fast8_t flag) |
d684c216 L |
388 | { |
389 | return cli_run_command(cmd, flag); | |
390 | } | |
391 | ||
392 | int run_command_list(const char *cmd, int len) | |
393 | { | |
394 | (void) len; | |
f76ca346 | 395 | |
d684c216 L |
396 | return cli_run_command_list(cmd); |
397 | } | |
398 | ||
399 | /****************************************************************************/ | |
400 | ||
d684c216 L |
401 | |
402 | void cli_loop(void) | |
403 | { | |
d684c216 | 404 | char *lastcommand = NULL; |
d684c216 | 405 | int len; |
93ea25f2 | 406 | uint_fast8_t flag; |
d684c216 L |
407 | int rc = 1; |
408 | ||
409 | for (;;) { | |
66e23a9d | 410 | len = cli_readline(lastcommand ? PSTR(CONFIG_SYS_PROMPT_REPEAT) : PSTR(CONFIG_SYS_PROMPT), 1); |
d684c216 L |
411 | |
412 | flag = 0; /* assume no special flags for now */ | |
413 | if (len > 0) { | |
69988dc1 L |
414 | free (lastcommand); |
415 | lastcommand = strdup(console_buffer); | |
d684c216 L |
416 | } else if (len == 0) |
417 | flag |= CMD_FLAG_REPEAT; | |
418 | ||
13e88ed5 L |
419 | if (len == -1) { |
420 | if (opt_verbose) | |
421 | my_puts_P(PSTR("<INTERRUPT>\n")); | |
422 | } else | |
d684c216 L |
423 | rc = run_command_repeatable(lastcommand, flag); |
424 | ||
425 | if (rc <= 0) { | |
426 | /* invalid command or not repeatable, forget it */ | |
d684c216 L |
427 | free(lastcommand); |
428 | lastcommand = NULL; | |
d684c216 L |
429 | } |
430 | } | |
431 | } |