]>
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
20 #include "cli_readline.h"
21 #include "con-utils.h"
24 /* FIXME: Quoting problems */
26 #define DEBUG_PARSER 0 /* set to 1 to debug */
28 #define debug_parser(fmt, args...) \
29 debug_cond(DEBUG_PARSER, fmt, ##args)
32 static int_least8_t exec_flags
;
36 static int_least8_t command_level
;
38 static void cli_trace_cmd(int_fast8_t level
, int argc
, char *argv
[])
42 for (int_fast8_t i
= 0; i
< argc
; i
++)
43 printf_P(PSTR(" %s"), argv
[i
]);
50 static int cli_parse_line(char *line
, char *argv
[])
52 uint_fast8_t state
= 0;
53 uint_fast8_t nargs
= 0;
57 debug_parser("%s: \"%s\"\n", __func__
, line
);
59 for (outp
= inp
= line
, quote
= '\0'; (c
= *inp
) != '\0'; inp
++) {
62 case 0: /* before arg string, waiting for arg start */
66 argv
[nargs
++] = inp
; /* begin of argument string */
71 case 1: /* in arg string, waiting for end of arg string */
76 if (c
== '\"' || c
== '\'') {
87 case 3: /* in quote */
88 if (c
== '\\' && quote
== '\"') {
98 case 2: /* waiting for next char */
105 if (nargs
> CONFIG_SYS_MAXARGS
) {
113 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
117 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
119 for (int i
= 0; i
< nargs
; i
++)
120 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
127 void append_char(uint_fast8_t pass
, char **p
, char c
)
137 char *process_macros(char *input
)
142 const char *varname
= NULL
;
144 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
145 strlen(input
), input
);
147 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
149 uint_fast8_t state
= 0;
150 /* 0 = waiting for '$' */
151 /* 1 = waiting for '{' */
152 /* 2 = waiting for '}' */
153 /* 3 = waiting for ''' */
156 size_t outputlen
= outp
- (char *) NULL
;
157 output
= (char *) malloc(outputlen
);
158 if (output
== NULL
) {
159 printf_P(PSTR("** Can't process command: Out of memory! **\n"));
167 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
172 case 0: /* Waiting for (unescaped) $ */
173 if ((c
== '\'') && (prev
!= '\\')) {
177 if ((c
== '$') && (prev
!= '\\')) {
182 case 1: /* Waiting for { */
189 append_char(pass
, &outp
, '$');
192 case 2: /* Waiting for } */
194 /* Terminate variable name */
196 const char *envval
= getenv_str(varname
);
198 /* Copy into the line if it exists */
201 append_char(pass
, &outp
, *(envval
++));
202 /* Look for another '$' */
206 case 3: /* Waiting for ' */
211 append_char(pass
, &outp
, c
);
214 append_char(pass
, &outp
, 0);
217 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
218 strlen(output
), output
);
228 * We must create a temporary copy of the command since the command we get
229 * may be the result from getenv_str(), which returns a pointer directly to
230 * the environment data, which may change magicly when the command we run
231 * creates or modifies environment variables (like "bootp" does).
239 static int cli_run_command(const char *cmd
, uint_fast8_t flag
)
241 char cmdbuf
[strlen(cmd
) + 1]; /* working copy of cmd */
242 char *token
; /* start of token in cmdbuf */
243 char *sep
; /* end of token (separator) in cmdbuf */
244 char *finaltoken
= NULL
; /* token after macro expansion */
246 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
248 uint_fast8_t inquotes
, repeatable
= 1;
252 char *optenv
= getenv_str(PSTR("cli"));
254 if (strstr_P(optenv
, PSTR("verbose")) != NULL
)
255 exec_flags
|= OPT_VERBOSE
;
256 if (strstr_P(optenv
, PSTR("xtrace")) != NULL
)
257 exec_flags
|= OPT_XTRACE
;
260 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
261 cmd
, cmd
? cmd
: "NULL");
263 if (exec_flags
& OPT_VERBOSE
)
264 printf_P(PSTR("%s\n"), cmd
, cmd
? cmd
: "");
267 return -1; /* empty command */
269 clear_ctrlc(); /* forget any previous Control C */
271 str
= strcpy(cmdbuf
, cmd
);
275 /* Process separators and check for invalid
276 * repeatable commands
279 debug_parser("[PROCESS_SEPARATORS] \"%s\"\n", cmd
);
282 * Find separator, or string end
283 * Allow simple escape of ';' by writing "\;"
285 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
286 if ((*sep
== '\'') &&
287 (sep
!= str
) && /* past string start */
288 (*(sep
- 1) != '\\')) /* and NOT escaped */
289 inquotes
= !inquotes
;
292 (*sep
== ';' || *sep
== '\n' /* separator */
293 || *sep
== '#') && /* or start of comment */
294 ((sep
== str
) || /* string start */
295 (*(sep
- 1) != '\\'))) /* or NOT escaped */
299 /* no more commands after unescaped '#' token */
303 /* Limit the token to data between separators */
306 str
= sep
+ 1; /* start of command for next pass */
309 str
= sep
; /* no more commands for next pass */
311 while (isblank(*token
))
313 if (*token
== '\0') /* empty (comment only) */
315 debug_parser("token: \"%s\"\n", token
);
317 /* find macros in this token and replace them */
318 finaltoken
= process_macros(token
);
319 if (finaltoken
== NULL
) {
320 rc
= -1; /* no command at all */
324 /* Extract arguments */
325 argc
= cli_parse_line(finaltoken
, argv
);
327 rc
= -1; /* no command at all */
332 if (exec_flags
& OPT_XTRACE
)
333 cli_trace_cmd(command_level
, argc
, argv
);
335 rc
= cmd_process(flag
, argc
, argv
, &repeatable
);
337 if (rc
!= CMD_RET_SUCCESS
) {
338 if (exec_flags
& OPT_VERBOSE
)
339 printf_P(PSTR("Command failed, result=%d\n"), rc
);
343 /* Did the user stop this? */
345 rc
= -1; /* if stopped then not repeatable */
352 return rc
? rc
: repeatable
;
355 static int cli_run_command_list(const char *cmd
)
357 return (cli_run_command(cmd
, 0) < 0);
360 /******************************************************************************/
366 * @param cmd Command to run
367 * @param flag Execution flags (CMD_FLAG_...)
368 * @return 0 on success, or != 0 on error.
370 int run_command(const char *cmd
, uint_fast8_t flag
)
373 * cli_run_command can return 0 or 1 for success, so clean up
376 if (cli_run_command(cmd
, flag
) == -1)
383 * Run a command using the selected parser, and check if it is repeatable.
385 * @param cmd Command to run
386 * @param flag Execution flags (CMD_FLAG_...)
387 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
389 static int run_command_repeatable(const char *cmd
, uint_fast8_t flag
)
391 return cli_run_command(cmd
, flag
);
394 int run_command_list(const char *cmd
, int len
)
398 return cli_run_command_list(cmd
);
401 /****************************************************************************/
406 char *lastcommand
= NULL
;
412 len
= cli_readline(lastcommand
? PSTR(CONFIG_SYS_PROMPT_REPEAT
) : PSTR(CONFIG_SYS_PROMPT
), 1);
414 flag
= 0; /* assume no special flags for now */
417 lastcommand
= strdup(console_buffer
);
419 flag
|= CMD_FLAG_REPEAT
;
422 if (exec_flags
& OPT_VERBOSE
)
423 my_puts_P(PSTR("<INTERRUPT>\n"));
425 rc
= run_command_repeatable(lastcommand
, flag
);
428 /* invalid command or not repeatable, forget it */