]>
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 "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" | |
29 | ||
30 | ||
31 | /* FIXME: Quoting problems */ | |
32 | ||
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 | ||
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 | ||
55 | static int cli_parse_line(char *line, char *argv[]) | |
56 | { | |
57 | uint_fast8_t state = 0; | |
58 | uint_fast8_t nargs = 0; | |
59 | char *inp, *outp; | |
60 | char c, quote; | |
61 | ||
62 | debug_parser("%s: \"%s\"\n", __func__, line); | |
63 | ||
64 | for (outp = inp = line, quote = '\0'; (c = *inp) != '\0'; inp++) { | |
65 | ||
66 | switch (state) { | |
67 | case 0: /* before arg string, waiting for arg start */ | |
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 | ||
76 | case 1: /* in arg string, waiting for end of arg string */ | |
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 | ||
92 | case 3: /* in quote */ | |
93 | if (c == '\\' && quote == '\"') { | |
94 | ++state; | |
95 | continue; | |
96 | } | |
97 | if (c == quote) { | |
98 | state = 1; | |
99 | continue; | |
100 | } | |
101 | break; | |
102 | ||
103 | case 2: /* waiting for next char */ | |
104 | case 4: | |
105 | --state; | |
106 | break; | |
107 | ||
108 | } | |
109 | ||
110 | if (nargs > CONFIG_SYS_MAXARGS) { | |
111 | --nargs; | |
112 | break; | |
113 | } | |
114 | *outp++ = c; | |
115 | } | |
116 | ||
117 | if (*inp != '\0') | |
118 | printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS); | |
119 | ||
120 | *outp = '\0'; | |
121 | argv[nargs] = NULL; | |
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 | |
127 | return nargs; | |
128 | ||
129 | } | |
130 | ||
131 | static | |
132 | void append_char(uint_fast8_t pass, char **p, char c) | |
133 | { | |
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 | { | |
149 | uint_fast8_t state = 0; | |
150 | /* 0 = waiting for '$' */ | |
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; | |
161 | } | |
162 | ||
163 | inp = input; | |
164 | ||
165 | debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", | |
166 | strlen(inp), inp); | |
167 | ||
168 | for (prev = '\0'; (c = *inp++) != '\0'; prev = c) { | |
169 | ||
170 | ||
171 | ||
172 | switch (state) { | |
173 | case 0: /* Waiting for (unescaped) $ */ | |
174 | if ((c == '\'') && (prev != '\\')) { | |
175 | state = 3; | |
176 | break; | |
177 | } | |
178 | if ((c == '$') && (prev != '\\')) { | |
179 | state++; | |
180 | continue; | |
181 | } | |
182 | break; | |
183 | case 1: /* Waiting for { */ | |
184 | if (c == '{') { | |
185 | state++; | |
186 | varname = inp; | |
187 | continue; | |
188 | } else { | |
189 | state = 0; | |
190 | append_char(pass, &outp, '$'); | |
191 | } | |
192 | break; | |
193 | case 2: /* Waiting for } */ | |
194 | if (c == '}') { | |
195 | /* Terminate variable name */ | |
196 | *(inp-1) = '\0'; | |
197 | const char *envval = getenv_str(varname); | |
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 | } | |
206 | continue; | |
207 | case 3: /* Waiting for ' */ | |
208 | if (c == '\'') | |
209 | state = 0; | |
210 | break; | |
211 | } | |
212 | append_char(pass, &outp, c); | |
213 | } | |
214 | ||
215 | append_char(pass, &outp, 0); | |
216 | } | |
217 | ||
218 | debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", | |
219 | strlen(output), output); | |
220 | ||
221 | return output; | |
222 | } | |
223 | ||
224 | /** | |
225 | * | |
226 | * | |
227 | * WARNING: | |
228 | * | |
229 | * We must create a temporary copy of the command since the command we get | |
230 | * may be the result from getenv_str(), which returns a pointer directly to | |
231 | * the environment data, which may change magicly when the command we run | |
232 | * creates or modifies environment variables (like "bootp" does). | |
233 | * | |
234 | * | |
235 | * @param cmd | |
236 | * @param flag | |
237 | * @returns | |
238 | * | |
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 */ | |
245 | char *finaltoken = NULL; /* token after macro expansion */ | |
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 | ||
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 | ||
260 | debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n", | |
261 | cmd, cmd ? cmd : "NULL"); | |
262 | ||
263 | if (opt_verbose) | |
264 | printf_P(PSTR("%s\n"), cmd, cmd ? cmd : ""); | |
265 | ||
266 | clear_ctrlc(); /* forget any previous Control C */ | |
267 | ||
268 | if (!cmd || !*cmd) | |
269 | return -1; /* empty command */ | |
270 | ||
271 | cmdbuf = strdup(cmd); | |
272 | if (!cmdbuf) | |
273 | return -1; /* not enough memory */ | |
274 | ||
275 | ++command_level; | |
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 == '\'') && | |
290 | (sep != str) && /* past string start */ | |
291 | (*(sep - 1) != '\\')) /* and NOT escaped */ | |
292 | inquotes = !inquotes; | |
293 | ||
294 | if (!inquotes && | |
295 | (*sep == ';' || *sep == '\n' /* separator */ | |
296 | || *sep == '#') && /* or start of comment */ | |
297 | ((sep == str) || /* string start */ | |
298 | (*(sep - 1) != '\\'))) /* or NOT escaped */ | |
299 | break; | |
300 | } | |
301 | ||
302 | /* no more commands after unescaped '#' token */ | |
303 | if (*sep == '#') | |
304 | *sep = '\0'; | |
305 | ||
306 | /* Limit the token to data between separators */ | |
307 | token = str; | |
308 | if (*sep) { | |
309 | str = sep + 1; /* start of command for next pass */ | |
310 | *sep = '\0'; | |
311 | } else { | |
312 | str = sep; /* no more commands for next pass */ | |
313 | } | |
314 | debug_parser("token: \"%s\"\n", token); | |
315 | ||
316 | /* find macros in this token and replace them */ | |
317 | finaltoken = process_macros(token, finaltoken); | |
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 | ||
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); | |
333 | rc = -1; | |
334 | } | |
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); | |
345 | --command_level; | |
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 | /* | |
359 | * Run a command. | |
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; | |
392 | ||
393 | return cli_run_command_list(cmd); | |
394 | } | |
395 | ||
396 | /****************************************************************************/ | |
397 | ||
398 | ||
399 | void cli_loop(void) | |
400 | { | |
401 | char *lastcommand = NULL; | |
402 | int len; | |
403 | int flag; | |
404 | int rc = 1; | |
405 | ||
406 | for (;;) { | |
407 | len = cli_readline(PSTR(CONFIG_SYS_PROMPT), 1); | |
408 | ||
409 | flag = 0; /* assume no special flags for now */ | |
410 | if (len > 0) { | |
411 | free (lastcommand); | |
412 | lastcommand = strdup(console_buffer); | |
413 | } else if (len == 0) | |
414 | flag |= CMD_FLAG_REPEAT; | |
415 | ||
416 | if (len == -1) { | |
417 | if (opt_verbose) | |
418 | my_puts_P(PSTR("<INTERRUPT>\n")); | |
419 | } else | |
420 | rc = run_command_repeatable(lastcommand, flag); | |
421 | ||
422 | if (rc <= 0) { | |
423 | /* invalid command or not repeatable, forget it */ | |
424 | free(lastcommand); | |
425 | lastcommand = NULL; | |
426 | } | |
427 | } | |
428 | } |