]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli.c
2 * (C) Copyright 2014-2016 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
26 #include "cli_readline.h"
27 #include "con-utils.h"
30 /* FIXME: Quoting problems */
32 #define DEBUG_PARSER 0 /* set to 1 to debug */
34 #define debug_parser(fmt, args...) \
35 debug_cond(DEBUG_PARSER, fmt, ##args)
38 static int_least8_t exec_flags
;
42 static int_least8_t command_level
;
44 static void cli_trace_cmd(int_fast8_t level
, int argc
, char *argv
[])
48 for (int_fast8_t i
= 0; i
< argc
; i
++)
49 printf_P(PSTR(" %s"), argv
[i
]);
56 static int cli_parse_line(char *line
, char *argv
[])
58 uint_fast8_t state
= 0;
59 uint_fast8_t nargs
= 0;
63 debug_parser("%s: \"%s\"\n", __func__
, line
);
65 for (outp
= inp
= line
, quote
= '\0'; (c
= *inp
) != '\0'; inp
++) {
68 case 0: /* before arg string, waiting for arg start */
72 argv
[nargs
++] = inp
; /* begin of argument string */
77 case 1: /* in arg string, waiting for end of arg string */
82 if (c
== '\"' || c
== '\'') {
93 case 3: /* in quote */
94 if (c
== '\\' && quote
== '\"') {
104 case 2: /* waiting for next char */
111 if (nargs
> CONFIG_SYS_MAXARGS
) {
119 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
123 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
125 for (int i
= 0; i
< nargs
; i
++)
126 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
133 void append_char(uint_fast8_t pass
, char **p
, char c
)
143 char *process_macros(char *input
)
148 const char *varname
= NULL
;
150 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
151 strlen(input
), input
);
153 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
155 uint_fast8_t state
= 0;
156 /* 0 = waiting for '$' */
157 /* 1 = waiting for '{' */
158 /* 2 = waiting for '}' */
159 /* 3 = waiting for ''' */
162 size_t outputlen
= outp
- (char *) NULL
;
163 output
= (char *) malloc(outputlen
);
164 if (output
== NULL
) {
165 printf_P(PSTR("** Can't process command: Out of memory! **\n"));
173 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
178 case 0: /* Waiting for (unescaped) $ */
179 if ((c
== '\'') && (prev
!= '\\')) {
183 if ((c
== '$') && (prev
!= '\\')) {
188 case 1: /* Waiting for { */
195 append_char(pass
, &outp
, '$');
198 case 2: /* Waiting for } */
200 /* Terminate variable name */
202 const char *envval
= getenv_str(varname
);
204 /* Copy into the line if it exists */
207 append_char(pass
, &outp
, *(envval
++));
208 /* Look for another '$' */
212 case 3: /* Waiting for ' */
217 append_char(pass
, &outp
, c
);
220 append_char(pass
, &outp
, 0);
223 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
224 strlen(output
), output
);
234 * We must create a temporary copy of the command since the command we get
235 * may be the result from getenv_str(), which returns a pointer directly to
236 * the environment data, which may change magicly when the command we run
237 * creates or modifies environment variables (like "bootp" does).
245 static int cli_run_command(const char *cmd
, int flag
)
247 char cmdbuf
[strlen(cmd
) + 1]; /* working copy of cmd */
248 char *token
; /* start of token in cmdbuf */
249 char *sep
; /* end of token (separator) in cmdbuf */
250 char *finaltoken
= NULL
; /* token after macro expansion */
252 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
254 uint_fast8_t inquotes
, repeatable
= 1;
258 char *optenv
= getenv_str(PSTR("cli"));
260 if (strstr_P(optenv
, PSTR("verbose")) != NULL
)
261 exec_flags
|= OPT_VERBOSE
;
262 if (strstr_P(optenv
, PSTR("xtrace")) != NULL
)
263 exec_flags
|= OPT_XTRACE
;
266 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
267 cmd
, cmd
? cmd
: "NULL");
269 if (exec_flags
& OPT_VERBOSE
)
270 printf_P(PSTR("%s\n"), cmd
, cmd
? cmd
: "");
273 return -1; /* empty command */
275 clear_ctrlc(); /* forget any previous Control C */
277 str
= strcpy(cmdbuf
, cmd
);
281 /* Process separators and check for invalid
282 * repeatable commands
285 debug_parser("[PROCESS_SEPARATORS] \"%s\"\n", cmd
);
288 * Find separator, or string end
289 * Allow simple escape of ';' by writing "\;"
291 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
292 if ((*sep
== '\'') &&
293 (sep
!= str
) && /* past string start */
294 (*(sep
- 1) != '\\')) /* and NOT escaped */
295 inquotes
= !inquotes
;
298 (*sep
== ';' || *sep
== '\n' /* separator */
299 || *sep
== '#') && /* or start of comment */
300 ((sep
== str
) || /* string start */
301 (*(sep
- 1) != '\\'))) /* or NOT escaped */
305 /* no more commands after unescaped '#' token */
309 /* Limit the token to data between separators */
312 str
= sep
+ 1; /* start of command for next pass */
315 str
= sep
; /* no more commands for next pass */
317 debug_parser("token: \"%s\"\n", token
);
319 /* find macros in this token and replace them */
320 finaltoken
= process_macros(token
);
321 if (finaltoken
== NULL
) {
322 rc
= -1; /* no command at all */
326 /* Extract arguments */
327 argc
= cli_parse_line(finaltoken
, argv
);
329 rc
= -1; /* no command at all */
334 if (exec_flags
& OPT_XTRACE
)
335 cli_trace_cmd(command_level
, argc
, argv
);
337 rc
= cmd_process(flag
, argc
, argv
, &repeatable
);
339 if (rc
!= CMD_RET_SUCCESS
) {
340 if (exec_flags
& OPT_VERBOSE
)
341 printf_P(PSTR("Command failed, result=%d\n"), rc
);
345 /* Did the user stop this? */
347 rc
= -1; /* if stopped then not repeatable */
354 return rc
? rc
: repeatable
;
357 static int cli_run_command_list(const char *cmd
)
359 return (cli_run_command(cmd
, 0) < 0);
362 /******************************************************************************/
368 * @param cmd Command to run
369 * @param flag Execution flags (CMD_FLAG_...)
370 * @return 0 on success, or != 0 on error.
372 int run_command(const char *cmd
, int flag
)
375 * cli_run_command can return 0 or 1 for success, so clean up
378 if (cli_run_command(cmd
, flag
) == -1)
385 * Run a command using the selected parser, and check if it is repeatable.
387 * @param cmd Command to run
388 * @param flag Execution flags (CMD_FLAG_...)
389 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
391 static int run_command_repeatable(const char *cmd
, int flag
)
393 return cli_run_command(cmd
, flag
);
396 int run_command_list(const char *cmd
, int len
)
400 return cli_run_command_list(cmd
);
403 /****************************************************************************/
408 char *lastcommand
= NULL
;
414 len
= cli_readline(lastcommand
? PSTR(CONFIG_SYS_PROMPT_REPEAT
) : PSTR(CONFIG_SYS_PROMPT
), 1);
416 flag
= 0; /* assume no special flags for now */
419 lastcommand
= strdup(console_buffer
);
421 flag
|= CMD_FLAG_REPEAT
;
424 if (exec_flags
& OPT_VERBOSE
)
425 my_puts_P(PSTR("<INTERRUPT>\n"));
427 rc
= run_command_repeatable(lastcommand
, flag
);
430 /* invalid command or not repeatable, forget it */