]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014-2016 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 | ||
14 | #include "cli.h" | |
15 | #include "command.h" | |
16 | #include <ctype.h> | |
17 | ||
18 | #include "config.h" | |
19 | #include "debug.h" | |
20 | #include "env.h" | |
21 | #include "cli_readline.h" | |
22 | #include "con-utils.h" | |
23 | ||
24 | ||
25 | /* FIXME: Quoting problems */ | |
26 | ||
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 | ||
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 | ||
49 | static int cli_parse_line(char *line, char *argv[]) | |
50 | { | |
51 | uint_fast8_t state = 0; | |
52 | uint_fast8_t nargs = 0; | |
53 | char *inp, *outp; | |
54 | char c, quote; | |
55 | ||
56 | debug_parser("%s: \"%s\"\n", __func__, line); | |
57 | ||
58 | for (outp = inp = line, quote = '\0'; (c = *inp) != '\0'; inp++) { | |
59 | ||
60 | switch (state) { | |
61 | case 0: /* before arg string, waiting for arg start */ | |
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 | ||
70 | case 1: /* in arg string, waiting for end of arg string */ | |
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 | ||
86 | case 3: /* in quote */ | |
87 | if (c == '\\' && quote == '\"') { | |
88 | ++state; | |
89 | continue; | |
90 | } | |
91 | if (c == quote) { | |
92 | state = 1; | |
93 | continue; | |
94 | } | |
95 | break; | |
96 | ||
97 | case 2: /* waiting for next char */ | |
98 | case 4: | |
99 | --state; | |
100 | break; | |
101 | ||
102 | } | |
103 | ||
104 | if (nargs > CONFIG_SYS_MAXARGS) { | |
105 | --nargs; | |
106 | break; | |
107 | } | |
108 | *outp++ = c; | |
109 | } | |
110 | ||
111 | if (*inp != '\0') | |
112 | printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS); | |
113 | ||
114 | *outp = '\0'; | |
115 | argv[nargs] = NULL; | |
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 | |
121 | return nargs; | |
122 | ||
123 | } | |
124 | ||
125 | static | |
126 | void append_char(uint_fast8_t pass, char **p, char c) | |
127 | { | |
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 | { | |
143 | uint_fast8_t state = 0; | |
144 | /* 0 = waiting for '$' */ | |
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; | |
153 | outp = realloc(output, outputlen); | |
154 | if (outp == NULL) { | |
155 | free(output); | |
156 | output = outp; | |
157 | break; | |
158 | } | |
159 | output = outp; | |
160 | } | |
161 | ||
162 | inp = input; | |
163 | ||
164 | debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", | |
165 | strlen(inp), inp); | |
166 | ||
167 | for (prev = '\0'; (c = *inp++) != '\0'; prev = c) { | |
168 | ||
169 | ||
170 | ||
171 | switch (state) { | |
172 | case 0: /* Waiting for (unescaped) $ */ | |
173 | if ((c == '\'') && (prev != '\\')) { | |
174 | state = 3; | |
175 | break; | |
176 | } | |
177 | if ((c == '$') && (prev != '\\')) { | |
178 | state++; | |
179 | continue; | |
180 | } | |
181 | break; | |
182 | case 1: /* Waiting for { */ | |
183 | if (c == '{') { | |
184 | state++; | |
185 | varname = inp; | |
186 | continue; | |
187 | } else { | |
188 | state = 0; | |
189 | append_char(pass, &outp, '$'); | |
190 | } | |
191 | break; | |
192 | case 2: /* Waiting for } */ | |
193 | if (c == '}') { | |
194 | /* Terminate variable name */ | |
195 | *(inp-1) = '\0'; | |
196 | const char *envval = getenv_str(varname); | |
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 | } | |
205 | continue; | |
206 | case 3: /* Waiting for ' */ | |
207 | if (c == '\'') | |
208 | state = 0; | |
209 | break; | |
210 | } | |
211 | append_char(pass, &outp, c); | |
212 | } | |
213 | ||
214 | append_char(pass, &outp, 0); | |
215 | } | |
216 | ||
217 | debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", | |
218 | strlen(output), output); | |
219 | ||
220 | return output; | |
221 | } | |
222 | ||
223 | /** | |
224 | * | |
225 | * | |
226 | * WARNING: | |
227 | * | |
228 | * We must create a temporary copy of the command since the command we get | |
229 | * may be the result from getenv_str(), which returns a pointer directly to | |
230 | * the environment data, which may change magicly when the command we run | |
231 | * creates or modifies environment variables (like "bootp" does). | |
232 | * | |
233 | * | |
234 | * @param cmd | |
235 | * @param flag | |
236 | * @returns | |
237 | * | |
238 | */ | |
239 | static int cli_run_command(const char *cmd, uint_fast8_t flag) | |
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 */ | |
244 | char *finaltoken = NULL; /* token after macro expansion */ | |
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 | ||
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 | ||
259 | debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n", | |
260 | cmd, cmd ? cmd : "NULL"); | |
261 | ||
262 | if (opt_verbose) | |
263 | printf_P(PSTR("%s\n"), cmd, cmd ? cmd : ""); | |
264 | ||
265 | clear_ctrlc(); /* forget any previous Control C */ | |
266 | ||
267 | if (!cmd || !*cmd) | |
268 | return -1; /* empty command */ | |
269 | ||
270 | cmdbuf = strdup(cmd); | |
271 | if (!cmdbuf) | |
272 | return -1; /* not enough memory */ | |
273 | ||
274 | ++command_level; | |
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 == '\'') && | |
289 | (sep != str) && /* past string start */ | |
290 | (*(sep - 1) != '\\')) /* and NOT escaped */ | |
291 | inquotes = !inquotes; | |
292 | ||
293 | if (!inquotes && | |
294 | (*sep == ';' || *sep == '\n' /* separator */ | |
295 | || *sep == '#') && /* or start of comment */ | |
296 | ((sep == str) || /* string start */ | |
297 | (*(sep - 1) != '\\'))) /* or NOT escaped */ | |
298 | break; | |
299 | } | |
300 | ||
301 | /* no more commands after unescaped '#' token */ | |
302 | if (*sep == '#') | |
303 | *sep = '\0'; | |
304 | ||
305 | /* Limit the token to data between separators */ | |
306 | token = str; | |
307 | if (*sep) { | |
308 | str = sep + 1; /* start of command for next pass */ | |
309 | *sep = '\0'; | |
310 | } else { | |
311 | str = sep; /* no more commands for next pass */ | |
312 | } | |
313 | debug_parser("token: \"%s\"\n", token); | |
314 | ||
315 | /* find macros in this token and replace them */ | |
316 | finaltoken = process_macros(token, finaltoken); | |
317 | if (finaltoken == NULL) { | |
318 | rc = -1; /* no command at all */ | |
319 | break; | |
320 | } | |
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 | ||
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); | |
336 | rc = -1; | |
337 | } | |
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); | |
348 | --command_level; | |
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 | /* | |
362 | * Run a command. | |
363 | * | |
364 | * @param cmd Command to run | |
365 | * @param flag Execution flags (CMD_FLAG_...) | |
366 | * @return 0 on success, or != 0 on error. | |
367 | */ | |
368 | int run_command(const char *cmd, uint_fast8_t flag) | |
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 | */ | |
387 | static int run_command_repeatable(const char *cmd, uint_fast8_t flag) | |
388 | { | |
389 | return cli_run_command(cmd, flag); | |
390 | } | |
391 | ||
392 | int run_command_list(const char *cmd, int len) | |
393 | { | |
394 | (void) len; | |
395 | ||
396 | return cli_run_command_list(cmd); | |
397 | } | |
398 | ||
399 | /****************************************************************************/ | |
400 | ||
401 | ||
402 | void cli_loop(void) | |
403 | { | |
404 | char *lastcommand = NULL; | |
405 | int len; | |
406 | uint_fast8_t flag; | |
407 | int rc = 1; | |
408 | ||
409 | for (;;) { | |
410 | len = cli_readline(lastcommand ? PSTR(CONFIG_SYS_PROMPT_REPEAT) : PSTR(CONFIG_SYS_PROMPT), 1); | |
411 | ||
412 | flag = 0; /* assume no special flags for now */ | |
413 | if (len > 0) { | |
414 | free (lastcommand); | |
415 | lastcommand = strdup(console_buffer); | |
416 | } else if (len == 0) | |
417 | flag |= CMD_FLAG_REPEAT; | |
418 | ||
419 | if (len == -1) { | |
420 | if (opt_verbose) | |
421 | my_puts_P(PSTR("<INTERRUPT>\n")); | |
422 | } else | |
423 | rc = run_command_repeatable(lastcommand, flag); | |
424 | ||
425 | if (rc <= 0) { | |
426 | /* invalid command or not repeatable, forget it */ | |
427 | free(lastcommand); | |
428 | lastcommand = NULL; | |
429 | } | |
430 | } | |
431 | } |