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