]>
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 int_least8_t exec_flags
;
37 static int_least8_t command_level
;
39 static void cli_trace_cmd(int_fast8_t level
, int argc
, char *argv
[])
43 for (int_fast8_t i
= 0; i
< argc
; i
++)
44 printf_P(PSTR(" %s"), argv
[i
]);
51 static int cli_parse_line(char *line
, char *argv
[])
53 uint_fast8_t state
= 0;
54 uint_fast8_t nargs
= 0;
58 debug_parser("%s: \"%s\"\n", __func__
, line
);
60 for (outp
= inp
= line
, quote
= '\0'; (c
= *inp
) != '\0'; inp
++) {
63 case 0: /* before arg string, waiting for arg start */
67 argv
[nargs
++] = inp
; /* begin of argument string */
72 case 1: /* in arg string, waiting for end of arg string */
77 if (c
== '\"' || c
== '\'') {
88 case 3: /* in quote */
89 if (c
== '\\' && quote
== '\"') {
99 case 2: /* waiting for next char */
106 if (nargs
> CONFIG_SYS_MAXARGS
) {
114 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
118 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
120 for (int i
= 0; i
< nargs
; i
++)
121 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
128 void append_char(uint_fast8_t pass
, char **p
, char c
)
138 char *process_macros(char *input
)
143 const char *varname
= NULL
;
145 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
146 strlen(input
), input
);
148 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
150 uint_fast8_t state
= 0;
151 /* 0 = waiting for '$' */
152 /* 1 = waiting for '{' */
153 /* 2 = waiting for '}' */
154 /* 3 = waiting for ''' */
157 size_t outputlen
= outp
- (char *) NULL
;
158 output
= (char *) malloc(outputlen
);
159 if (output
== NULL
) {
160 printf_P(PSTR("** Can't process command: Out of memory! **\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
, uint_fast8_t flag
)
242 char cmdbuf
[strlen(cmd
) + 1]; /* 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;
253 char *optenv
= getenv_str(PSTR("cli"));
255 if (strstr_P(optenv
, PSTR("verbose")) != NULL
)
256 exec_flags
|= OPT_VERBOSE
;
257 if (strstr_P(optenv
, PSTR("xtrace")) != NULL
)
258 exec_flags
|= OPT_XTRACE
;
261 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
262 cmd
, cmd
? cmd
: "NULL");
264 if (exec_flags
& OPT_VERBOSE
)
265 printf_P(PSTR("%s\n"), cmd
, cmd
? cmd
: "");
268 return -1; /* empty command */
270 clear_ctrlc(); /* forget any previous Control C */
272 str
= strcpy(cmdbuf
, cmd
);
276 /* Process separators and check for invalid
277 * repeatable commands
280 debug_parser("[PROCESS_SEPARATORS] \"%s\"\n", cmd
);
283 * Find separator, or string end
284 * Allow simple escape of ';' by writing "\;"
286 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
287 if ((*sep
== '\'') &&
288 (sep
!= str
) && /* past string start */
289 (*(sep
- 1) != '\\')) /* and NOT escaped */
290 inquotes
= !inquotes
;
293 (*sep
== ';' || *sep
== '\n' /* separator */
294 || *sep
== '#') && /* or start of comment */
295 ((sep
== str
) || /* string start */
296 (*(sep
- 1) != '\\'))) /* or NOT escaped */
300 /* no more commands after unescaped '#' token */
304 /* Limit the token to data between separators */
307 str
= sep
+ 1; /* start of command for next pass */
310 str
= sep
; /* no more commands for next pass */
312 while (isblank(*token
))
314 if (*token
== '\0') /* empty (comment only) */
316 debug_parser("token: \"%s\"\n", token
);
318 /* find macros in this token and replace them */
319 finaltoken
= process_macros(token
);
320 if (finaltoken
== NULL
) {
321 rc
= -1; /* no command at all */
325 /* Extract arguments */
326 argc
= cli_parse_line(finaltoken
, argv
);
328 rc
= -1; /* no command at all */
333 if (exec_flags
& OPT_XTRACE
)
334 cli_trace_cmd(command_level
, argc
, argv
);
336 rc
= cmd_process(flag
, argc
, argv
, &repeatable
);
338 if (rc
!= CMD_RET_SUCCESS
) {
339 if (exec_flags
& OPT_VERBOSE
)
340 printf_P(PSTR("Command failed, result=%d\n"), rc
);
344 /* Did the user stop this? */
346 rc
= -1; /* if stopped then not repeatable */
353 return rc
? rc
: repeatable
;
356 static int cli_run_command_list(const char *cmd
)
358 return (cli_run_command(cmd
, 0) < 0);
361 /******************************************************************************/
367 * @param cmd Command to run
368 * @param flag Execution flags (CMD_FLAG_...)
369 * @return 0 on success, or != 0 on error.
371 int run_command(const char *cmd
, uint_fast8_t flag
)
374 * cli_run_command can return 0 or 1 for success, so clean up
377 if (cli_run_command(cmd
, flag
) == -1)
384 * Run a command using the selected parser, and check if it is repeatable.
386 * @param cmd Command to run
387 * @param flag Execution flags (CMD_FLAG_...)
388 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
390 static int run_command_repeatable(const char *cmd
, uint_fast8_t flag
)
392 return cli_run_command(cmd
, flag
);
395 int run_command_list(const char *cmd
, int len
)
399 return cli_run_command_list(cmd
);
402 /****************************************************************************/
407 char *lastcommand
= NULL
;
413 len
= cli_readline(lastcommand
? PSTR(CONFIG_SYS_PROMPT_REPEAT
) : PSTR(CONFIG_SYS_PROMPT
), 1);
415 flag
= 0; /* assume no special flags for now */
418 lastcommand
= strdup(console_buffer
);
420 flag
|= CMD_FLAG_REPEAT
;
423 if (exec_flags
& OPT_VERBOSE
)
424 my_puts_P(PSTR("<INTERRUPT>\n"));
426 rc
= run_command_repeatable(lastcommand
, flag
);
429 /* invalid command or not repeatable, forget it */