]>
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
21 #include "cli_readline.h"
22 #include "con-utils.h"
25 /* FIXME: Quoting problems */
27 #define DEBUG_PARSER 0 /* set to 1 to debug */
29 #define debug_parser(fmt, args...) \
30 debug_cond(DEBUG_PARSER, fmt, ##args)
33 static bool opt_xtrace
;
34 static bool opt_verbose
;
35 static int_least8_t command_level
;
37 static void cli_trace_cmd(int_fast8_t level
, int argc
, char *argv
[])
41 for (int_fast8_t i
= 0; i
< argc
; i
++)
42 printf_P(PSTR(" %s"), argv
[i
]);
49 static int cli_parse_line(char *line
, char *argv
[])
51 uint_fast8_t state
= 0;
52 uint_fast8_t nargs
= 0;
56 debug_parser("%s: \"%s\"\n", __func__
, line
);
58 for (outp
= inp
= line
, quote
= '\0'; (c
= *inp
) != '\0'; inp
++) {
61 case 0: /* before arg string, waiting for arg start */
65 argv
[nargs
++] = inp
; /* begin of argument string */
70 case 1: /* in arg string, waiting for end of arg string */
75 if (c
== '\"' || c
== '\'') {
86 case 3: /* in quote */
87 if (c
== '\\' && quote
== '\"') {
97 case 2: /* waiting for next char */
104 if (nargs
> CONFIG_SYS_MAXARGS
) {
112 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
116 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
118 for (int i
= 0; i
< nargs
; i
++)
119 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
126 void append_char(uint_fast8_t pass
, char **p
, char c
)
136 char *process_macros(char *input
, char *output
)
138 char c
, prev
, *inp
, *outp
;
139 const char *varname
= NULL
;
141 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
143 uint_fast8_t state
= 0;
144 /* 0 = waiting for '$' */
145 /* 1 = waiting for '{' */
146 /* 2 = waiting for '}' */
147 /* 3 = waiting for ''' */
152 int outputlen
= outp
- output
;
153 outp
= realloc(output
, outputlen
);
164 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\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
; /* 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;
253 char *optenv
= getenv_str(PSTR("cli"));
255 opt_verbose
= (strstr_P(optenv
, PSTR("verbose")) != NULL
);
256 opt_xtrace
= (strstr_P(optenv
, PSTR("xtrace")) != NULL
);
259 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
260 cmd
, cmd
? cmd
: "NULL");
263 printf_P(PSTR("%s\n"), cmd
, cmd
? cmd
: "");
265 clear_ctrlc(); /* forget any previous Control C */
268 return -1; /* empty command */
270 cmdbuf
= strdup(cmd
);
272 return -1; /* not enough memory */
277 /* Process separators and check for invalid
278 * repeatable commands
281 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
284 * Find separator, or string end
285 * Allow simple escape of ';' by writing "\;"
287 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
288 if ((*sep
== '\'') &&
289 (sep
!= str
) && /* past string start */
290 (*(sep
- 1) != '\\')) /* and NOT escaped */
291 inquotes
= !inquotes
;
294 (*sep
== ';' || *sep
== '\n' /* separator */
295 || *sep
== '#') && /* or start of comment */
296 ((sep
== str
) || /* string start */
297 (*(sep
- 1) != '\\'))) /* or NOT escaped */
301 /* no more commands after unescaped '#' token */
305 /* Limit the token to data between separators */
308 str
= sep
+ 1; /* start of command for next pass */
311 str
= sep
; /* no more commands for next pass */
313 debug_parser("token: \"%s\"\n", token
);
315 /* find macros in this token and replace them */
316 finaltoken
= process_macros(token
, finaltoken
);
317 if (finaltoken
== NULL
) {
318 rc
= -1; /* no command at all */
322 /* Extract arguments */
323 argc
= cli_parse_line(finaltoken
, argv
);
325 rc
= -1; /* no command at all */
330 cli_trace_cmd(command_level
, argc
, argv
);
332 rc
= cmd_process(flag
, argc
, argv
, &repeatable
);
333 if (rc
!= CMD_RET_SUCCESS
) {
335 printf_P(PSTR("Command failed, result=%d\n"), rc
);
339 /* Did the user stop this? */
341 rc
= -1; /* if stopped then not repeatable */
350 return rc
? rc
: repeatable
;
353 static int cli_run_command_list(const char *cmd
)
355 return (cli_run_command(cmd
, 0) < 0);
358 /******************************************************************************/
364 * @param cmd Command to run
365 * @param flag Execution flags (CMD_FLAG_...)
366 * @return 0 on success, or != 0 on error.
368 int run_command(const char *cmd
, uint_fast8_t flag
)
371 * cli_run_command can return 0 or 1 for success, so clean up
374 if (cli_run_command(cmd
, flag
) == -1)
381 * Run a command using the selected parser, and check if it is repeatable.
383 * @param cmd Command to run
384 * @param flag Execution flags (CMD_FLAG_...)
385 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
387 static int run_command_repeatable(const char *cmd
, uint_fast8_t flag
)
389 return cli_run_command(cmd
, flag
);
392 int run_command_list(const char *cmd
, int len
)
396 return cli_run_command_list(cmd
);
399 /****************************************************************************/
404 char *lastcommand
= NULL
;
410 len
= cli_readline(lastcommand
? PSTR(CONFIG_SYS_PROMPT_REPEAT
) : PSTR(CONFIG_SYS_PROMPT
), 1);
412 flag
= 0; /* assume no special flags for now */
415 lastcommand
= strdup(console_buffer
);
417 flag
|= CMD_FLAG_REPEAT
;
421 my_puts_P(PSTR("<INTERRUPT>\n"));
423 rc
= run_command_repeatable(lastcommand
, flag
);
426 /* invalid command or not repeatable, forget it */