]>
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
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)
39 static bool opt_xtrace
;
40 static bool opt_verbose
;
41 static int_least8_t command_level
;
43 static void cli_trace_cmd(int_fast8_t level
, int argc
, char *argv
[])
47 for (int_fast8_t i
= 0; i
< argc
; i
++)
48 printf_P(PSTR(" %s"), argv
[i
]);
55 static int cli_parse_line(char *line
, char *argv
[])
57 uint_fast8_t state
= 0;
58 uint_fast8_t nargs
= 0;
62 debug_parser("%s: \"%s\"\n", __func__
, line
);
64 for (outp
= inp
= line
, quote
= '\0'; (c
= *inp
) != '\0'; inp
++) {
67 case 0: /* before arg string, waiting for arg start */
71 argv
[nargs
++] = inp
; /* begin of argument string */
76 case 1: /* in arg string, waiting for end of arg string */
81 if (c
== '\"' || c
== '\'') {
92 case 3: /* in quote */
93 if (c
== '\\' && quote
== '\"') {
103 case 2: /* waiting for next char */
110 if (nargs
> CONFIG_SYS_MAXARGS
) {
118 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
122 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
124 for (int i
= 0; i
< nargs
; i
++)
125 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
132 void append_char(uint_fast8_t pass
, char **p
, char c
)
142 char *process_macros(char *input
, char *output
)
144 char c
, prev
, *inp
, *outp
;
145 const char *varname
= NULL
;
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 ''' */
158 int outputlen
= outp
- output
;
159 outp
= xrealloc(output
, outputlen
);
165 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
168 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
173 case 0: /* Waiting for (unescaped) $ */
174 if ((c
== '\'') && (prev
!= '\\')) {
178 if ((c
== '$') && (prev
!= '\\')) {
183 case 1: /* Waiting for { */
190 append_char(pass
, &outp
, '$');
193 case 2: /* Waiting for } */
195 /* Terminate variable name */
197 const char *envval
= getenv_str(varname
);
199 /* Copy into the line if it exists */
202 append_char(pass
, &outp
, *(envval
++));
203 /* Look for another '$' */
207 case 3: /* Waiting for ' */
212 append_char(pass
, &outp
, c
);
215 append_char(pass
, &outp
, 0);
218 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
219 strlen(output
), output
);
229 * We must create a temporary copy of the command since the command we get
230 * may be the result from getenv_str(), which returns a pointer directly to
231 * the environment data, which may change magicly when the command we run
232 * creates or modifies environment variables (like "bootp" does).
240 static int cli_run_command(const char *cmd
, int flag
)
242 char *cmdbuf
; /* working copy of cmd */
243 char *token
; /* start of token in cmdbuf */
244 char *sep
; /* end of token (separator) in cmdbuf */
245 char *finaltoken
= NULL
; /* token after macro expansion */
247 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
249 uint_fast8_t inquotes
, repeatable
= 1;
254 char *optenv
= getenv_str(PSTR("cli"));
256 opt_verbose
= (strstr_P(optenv
, PSTR("verbose")) != NULL
);
257 opt_xtrace
= (strstr_P(optenv
, PSTR("xtrace")) != NULL
);
260 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
261 cmd
, cmd
? cmd
: "NULL");
264 printf_P(PSTR("%s\n"), cmd
, cmd
? cmd
: "");
266 clear_ctrlc(); /* forget any previous Control C */
269 return -1; /* empty command */
271 cmdbuf
= strdup(cmd
);
273 return -1; /* not enough memory */
278 /* Process separators and check for invalid
279 * repeatable commands
282 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
285 * Find separator, or string end
286 * Allow simple escape of ';' by writing "\;"
288 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
289 if ((*sep
== '\'') &&
290 (sep
!= str
) && /* past string start */
291 (*(sep
- 1) != '\\')) /* and NOT escaped */
292 inquotes
= !inquotes
;
295 (*sep
== ';' || *sep
== '\n' /* separator */
296 || *sep
== '#') && /* or start of comment */
297 ((sep
== str
) || /* string start */
298 (*(sep
- 1) != '\\'))) /* or NOT escaped */
302 /* no more commands after unescaped '#' token */
306 /* Limit the token to data between separators */
309 str
= sep
+ 1; /* start of command for next pass */
312 str
= sep
; /* no more commands for next pass */
314 debug_parser("token: \"%s\"\n", token
);
316 /* find macros in this token and replace them */
317 finaltoken
= process_macros(token
, finaltoken
);
319 /* Extract arguments */
320 argc
= cli_parse_line(finaltoken
, argv
);
323 continue; /* no command at all */
327 cli_trace_cmd(command_level
, argc
, argv
);
329 rc
= cmd_process(flag
, argc
, argv
, &repeatable
);
330 if (rc
!= CMD_RET_SUCCESS
) {
332 printf_P(PSTR("Command failed, result=%d\n"), rc
);
336 /* Did the user stop this? */
338 rc
= -1; /* if stopped then not repeatable */
347 return rc
? rc
: repeatable
;
350 static int cli_run_command_list(const char *cmd
)
352 return (cli_run_command(cmd
, 0) < 0);
355 /******************************************************************************/
361 * @param cmd Command to run
362 * @param flag Execution flags (CMD_FLAG_...)
363 * @return 0 on success, or != 0 on error.
365 int run_command(const char *cmd
, int flag
)
368 * cli_run_command can return 0 or 1 for success, so clean up
371 if (cli_run_command(cmd
, flag
) == -1)
378 * Run a command using the selected parser, and check if it is repeatable.
380 * @param cmd Command to run
381 * @param flag Execution flags (CMD_FLAG_...)
382 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
384 static int run_command_repeatable(const char *cmd
, int flag
)
386 return cli_run_command(cmd
, flag
);
389 int run_command_list(const char *cmd
, int len
)
393 return cli_run_command_list(cmd
);
396 /****************************************************************************/
401 char *lastcommand
= NULL
;
407 len
= cli_readline(lastcommand
? PSTR(CONFIG_SYS_PROMPT_REPEAT
) : PSTR(CONFIG_SYS_PROMPT
), 1);
409 flag
= 0; /* assume no special flags for now */
412 lastcommand
= strdup(console_buffer
);
414 flag
|= CMD_FLAG_REPEAT
;
418 my_puts_P(PSTR("<INTERRUPT>\n"));
420 rc
= run_command_repeatable(lastcommand
, flag
);
423 /* invalid command or not repeatable, forget it */