]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Add to readline cmdline-editing by
9 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
11 * SPDX-License-Identifier: GPL-2.0
27 #include "cli_readline.h"
28 #include "con-utils.h"
31 /* FIXME: Quoting problems */
33 #define DEBUG_PARSER 0 /* set to 1 to debug */
35 #define debug_parser(fmt, args...) \
36 debug_cond(DEBUG_PARSER, fmt, ##args)
38 static int cli_parse_line(char *line
, char *argv
[])
40 uint_fast8_t state
= 0;
41 uint_fast8_t nargs
= 0;
45 debug_parser("%s: \"%s\"\n", __func__
, line
);
47 for (outp
= inp
= line
, quote
= '\0'; (c
= *inp
) != '\0'; inp
++) {
50 case 0: /* before arg string, waiting for arg start */
54 argv
[nargs
++] = inp
; /* begin of argument string */
59 case 1: /* in arg string, waiting for end of arg string */
64 if (c
== '\"' || c
== '\'') {
75 case 3: /* in quote */
76 if (c
== '\\' && quote
== '\"') {
86 case 2: /* waiting for next char */
93 if (nargs
> CONFIG_SYS_MAXARGS
) {
101 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
105 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
107 for (int i
= 0; i
< nargs
; i
++)
108 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
115 void append_char(uint_fast8_t pass
, char **p
, char c
)
125 char *process_macros(char *input
, char *output
)
127 char c
, prev
, *inp
, *outp
;
128 const char *varname
= NULL
;
130 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
132 uint_fast8_t state
= 0;
133 /* 0 = waiting for '$' */
134 /* 1 = waiting for '{' */
135 /* 2 = waiting for '}' */
136 /* 3 = waiting for ''' */
141 int outputlen
= outp
- output
;
142 outp
= xrealloc(output
, outputlen
);
148 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
151 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
156 case 0: /* Waiting for (unescaped) $ */
157 if ((c
== '\'') && (prev
!= '\\')) {
161 if ((c
== '$') && (prev
!= '\\')) {
166 case 1: /* Waiting for { */
173 append_char(pass
, &outp
, '$');
176 case 2: /* Waiting for } */
178 /* Terminate variable name */
180 const char *envval
= getenv_char(varname
);
182 /* Copy into the line if it exists */
185 append_char(pass
, &outp
, *(envval
++));
186 /* Look for another '$' */
190 case 3: /* Waiting for ' */
195 append_char(pass
, &outp
, c
);
198 append_char(pass
, &outp
, 0);
201 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
202 strlen(output
), output
);
212 * We must create a temporary copy of the command since the command we get
213 * may be the result from getenv_char(), which returns a pointer directly to
214 * the environment data, which may change magicly when the command we run
215 * creates or modifies environment variables (like "bootp" does).
223 static int cli_run_command(const char *cmd
, int flag
)
225 char *cmdbuf
; /* working copy of cmd */
226 char *token
; /* start of token in cmdbuf */
227 char *sep
; /* end of token (separator) in cmdbuf */
228 char *finaltoken
= NULL
; /* token after macro expansion */
230 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
232 uint_fast8_t inquotes
, repeatable
= 1;
235 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
236 cmd
, cmd
? cmd
: "NULL");
238 clear_ctrlc(); /* forget any previous Control C */
241 return -1; /* empty command */
243 cmdbuf
= strdup(cmd
);
245 return -1; /* not enough memory */
249 /* Process separators and check for invalid
250 * repeatable commands
253 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
256 * Find separator, or string end
257 * Allow simple escape of ';' by writing "\;"
259 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
260 if ((*sep
== '\'') &&
261 (sep
!= str
) && /* past string start */
262 (*(sep
- 1) != '\\')) /* and NOT escaped */
263 inquotes
= !inquotes
;
266 (*sep
== ';' || *sep
== '\n') && /* separator */
267 (sep
!= str
) && /* past string start */
268 (*(sep
- 1) != '\\')) /* and NOT escaped */
273 * Limit the token to data between separators
277 str
= sep
+ 1; /* start of command for next pass */
280 str
= sep
; /* no more commands for next pass */
282 debug_parser("token: \"%s\"\n", token
);
284 /* find macros in this token and replace them */
285 finaltoken
= process_macros(token
, finaltoken
);
287 /* Extract arguments */
288 argc
= cli_parse_line(finaltoken
, argv
);
290 rc
= -1; /* no command at all */
294 if (cmd_process(flag
, argc
, argv
, &repeatable
) != CMD_RET_SUCCESS
)
297 /* Did the user stop this? */
299 rc
= -1; /* if stopped then not repeatable */
307 return rc
? rc
: repeatable
;
310 static int cli_run_command_list(const char *cmd
)
312 return (cli_run_command(cmd
, 0) < 0);
315 /******************************************************************************/
321 * @param cmd Command to run
322 * @param flag Execution flags (CMD_FLAG_...)
323 * @return 0 on success, or != 0 on error.
325 int run_command(const char *cmd
, int flag
)
328 * cli_run_command can return 0 or 1 for success, so clean up
331 if (cli_run_command(cmd
, flag
) == -1)
338 * Run a command using the selected parser, and check if it is repeatable.
340 * @param cmd Command to run
341 * @param flag Execution flags (CMD_FLAG_...)
342 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
344 static int run_command_repeatable(const char *cmd
, int flag
)
346 return cli_run_command(cmd
, flag
);
349 int run_command_list(const char *cmd
, int len
)
353 return cli_run_command_list(cmd
);
356 /****************************************************************************/
361 char *lastcommand
= NULL
;
367 len
= cli_readline(PSTR(CONFIG_SYS_PROMPT
), 1);
369 flag
= 0; /* assume no special flags for now */
372 lastcommand
= strdup(console_buffer
);
374 flag
|= CMD_FLAG_REPEAT
;
377 my_puts_P(PSTR("<INTERRUPT>\n"));
379 rc
= run_command_repeatable(lastcommand
, flag
);
382 /* invalid command or not repeatable, forget it */
390 command_ret_t
do_run(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
396 return CMD_RET_USAGE
;
398 for (i
= 1; i
< argc
; ++i
) {
401 arg
= getenv_char(argv
[i
]);
403 printf_P(PSTR("## Error: \"%s\" is not set\n"), argv
[i
]);
404 return CMD_RET_FAILURE
;
407 if (run_command(arg
, flag
) != 0)
408 return CMD_RET_FAILURE
;
410 return CMD_RET_SUCCESS
;