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