]>
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 | ||
46 | static void process_macros(const char *input, char *output) | |
47 | { | |
48 | char c, prev; | |
49 | const char *varname_start = NULL; | |
50 | #if 0 && (CONFIG_SYS_CBSIZE < __UINT_FAST8_MAX__) | |
51 | uint_fast8_t inputcnt = strlen(input); | |
52 | uint_fast8_t outputcnt = CONFIG_SYS_CBSIZE; | |
53 | #else | |
54 | int inputcnt = strlen(input); | |
55 | int outputcnt = CONFIG_SYS_CBSIZE; | |
56 | #endif | |
57 | uint_fast8_t state = 0; /* 0 = waiting for '$' */ | |
58 | ||
59 | /* 1 = waiting for '(' or '{' */ | |
60 | /* 2 = waiting for ')' or '}' */ | |
61 | /* 3 = waiting for ''' */ | |
62 | char *output_start = output; | |
63 | ||
64 | debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), | |
65 | input); | |
66 | ||
67 | prev = '\0'; /* previous character */ | |
68 | ||
69 | while (inputcnt && outputcnt) { | |
70 | c = *input++; | |
71 | inputcnt--; | |
72 | ||
73 | if (state != 3) { | |
74 | /* remove one level of escape characters */ | |
75 | if ((c == '\\') && (prev != '\\')) { | |
76 | if (inputcnt-- == 0) | |
77 | break; | |
78 | prev = c; | |
79 | c = *input++; | |
80 | } | |
81 | } | |
82 | ||
83 | switch (state) { | |
84 | case 0: /* Waiting for (unescaped) $ */ | |
85 | if ((c == '\'') && (prev != '\\')) { | |
86 | state = 3; | |
87 | break; | |
88 | } | |
89 | if ((c == '$') && (prev != '\\')) { | |
90 | state++; | |
91 | } else { | |
92 | *(output++) = c; | |
93 | outputcnt--; | |
94 | } | |
95 | break; | |
96 | case 1: /* Waiting for ( */ | |
97 | if (c == '(' || c == '{') { | |
98 | state++; | |
99 | varname_start = input; | |
100 | } else { | |
101 | state = 0; | |
102 | *(output++) = '$'; | |
103 | outputcnt--; | |
104 | ||
105 | if (outputcnt) { | |
106 | *(output++) = c; | |
107 | outputcnt--; | |
108 | } | |
109 | } | |
110 | break; | |
111 | case 2: /* Waiting for ) */ | |
112 | if (c == ')' || c == '}') { | |
113 | char envname[CONFIG_SYS_ENV_NAMELEN+1], *envval; | |
114 | /* Varname # of chars */ | |
115 | uint_fast8_t envcnt = input - varname_start - 1; | |
116 | if (envcnt > CONFIG_SYS_ENV_NAMELEN) | |
117 | envcnt = CONFIG_SYS_ENV_NAMELEN; | |
118 | ||
119 | memcpy(envname, varname_start, envcnt); | |
120 | envname[envcnt] = '\0'; | |
121 | ||
122 | /* Get its value */ | |
123 | envval = getenv(envname); | |
124 | ||
125 | /* Copy into the line if it exists */ | |
126 | if (envval != NULL) | |
127 | while ((*envval) && outputcnt) { | |
128 | *(output++) = *(envval++); | |
129 | outputcnt--; | |
130 | } | |
131 | /* Look for another '$' */ | |
132 | state = 0; | |
133 | } | |
134 | break; | |
135 | case 3: /* Waiting for ' */ | |
136 | if ((c == '\'') && (prev != '\\')) { | |
137 | state = 0; | |
138 | } else { | |
139 | *(output++) = c; | |
140 | outputcnt--; | |
141 | } | |
142 | break; | |
143 | } | |
144 | prev = c; | |
145 | } | |
146 | ||
147 | if (outputcnt) | |
148 | *output = 0; | |
149 | else | |
150 | *(output - 1) = 0; | |
151 | ||
152 | debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", | |
153 | strlen(output_start), output_start); | |
154 | } | |
155 | ||
156 | /* | |
157 | * WARNING: | |
158 | * | |
159 | * We must create a temporary copy of the command since the command we get | |
160 | * may be the result from getenv(), which returns a pointer directly to | |
161 | * the environment data, which may change magicly when the command we run | |
162 | * creates or modifies environment variables (like "bootp" does). | |
163 | */ | |
164 | static int cli_run_command(const char *cmd, int flag) | |
165 | { | |
166 | char *cmdbuf; /* working copy of cmd */ | |
167 | char *token; /* start of token in cmdbuf */ | |
168 | char *sep; /* end of token (separator) in cmdbuf */ | |
169 | char *finaltoken; | |
170 | char *str; | |
171 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ | |
172 | int argc; | |
173 | uint_fast8_t inquotes, repeatable = 1; | |
174 | int rc = 0; | |
175 | ||
176 | debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n", | |
177 | cmd, cmd ? cmd : "NULL"); | |
178 | ||
179 | clear_ctrlc(); /* forget any previous Control C */ | |
180 | ||
181 | if (!cmd || !*cmd) | |
182 | return -1; /* empty command */ | |
183 | ||
69988dc1 | 184 | cmdbuf = strdup(cmd); |
55027f3b L |
185 | finaltoken = xmalloc(CONFIG_SYS_CBSIZE); |
186 | if (!finaltoken) | |
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 */ | |
226 | process_macros(token, finaltoken); | |
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; | |
293 | ||
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 |