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