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