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