]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli.c
2 * (C) Copyright 2014 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"
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)
38 static int cli_parse_line(char *line
, char *argv
[])
40 uint_fast8_t state
= 0;
41 uint_fast8_t nargs
= 0;
45 debug_parser("%s: \"%s\"\n", __func__
, line
);
47 for (outp
= inp
= line
, quote
= '\0' ;
48 nargs
< CONFIG_SYS_MAXARGS
&& (c
= *inp
) != '\0';
52 case 0: /* before arg string, waiting for arg start */
56 argv
[nargs
++] = inp
; /* begin of argument string */
61 case 1: /* in arg string, waiting for end of arg string */
66 if (c
== '\"' || c
== '\'') {
77 case 3: /* in quote */
78 if (c
== '\\' && quote
== '\"') {
88 case 2: /* waiting for next char */
98 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
102 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
104 for (int i
= 0; i
< nargs
; i
++)
105 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
112 void append_char(uint_fast8_t pass
, char **p
, char c
)
122 char *process_macros(char *input
, char *output
)
124 char c
, prev
, *inp
, *outp
;
125 const char *varname
= NULL
;
127 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
129 uint_fast8_t state
= 0;
130 /* 0 = waiting for '$' */
131 /* 1 = waiting for '{' */
132 /* 2 = waiting for '}' */
133 /* 3 = waiting for ''' */
138 int outputlen
= outp
- output
;
139 outp
= xrealloc(output
, outputlen
);
145 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
148 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
153 case 0: /* Waiting for (unescaped) $ */
154 if ((c
== '\'') && (prev
!= '\\')) {
158 if ((c
== '$') && (prev
!= '\\')) {
163 case 1: /* Waiting for { */
170 append_char(pass
, &outp
, '$');
173 case 2: /* Waiting for } */
175 /* Terminate variable name */
177 const char *envval
= getenv(varname
);
179 /* Copy into the line if it exists */
182 append_char(pass
, &outp
, *(envval
++));
183 /* Look for another '$' */
187 case 3: /* Waiting for ' */
192 append_char(pass
, &outp
, c
);
195 append_char(pass
, &outp
, 0);
198 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
199 strlen(output
), output
);
209 * We must create a temporary copy of the command since the command we get
210 * may be the result from getenv(), which returns a pointer directly to
211 * the environment data, which may change magicly when the command we run
212 * creates or modifies environment variables (like "bootp" does).
220 static int cli_run_command(const char *cmd
, int flag
)
222 char *cmdbuf
; /* working copy of cmd */
223 char *token
; /* start of token in cmdbuf */
224 char *sep
; /* end of token (separator) in cmdbuf */
225 char *finaltoken
= NULL
; /* token after macro expansion */
227 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
229 uint_fast8_t inquotes
, repeatable
= 1;
232 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
233 cmd
, cmd
? cmd
: "NULL");
235 clear_ctrlc(); /* forget any previous Control C */
238 return -1; /* empty command */
240 cmdbuf
= strdup(cmd
);
242 return -1; /* not enough memory */
246 /* Process separators and check for invalid
247 * repeatable commands
250 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
253 * Find separator, or string end
254 * Allow simple escape of ';' by writing "\;"
256 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
257 if ((*sep
== '\'') &&
258 (sep
!= str
) && /* past string start */
259 (*(sep
- 1) != '\\')) /* and NOT escaped */
260 inquotes
= !inquotes
;
263 (*sep
== ';' || *sep
== '\n') && /* separator */
264 (sep
!= str
) && /* past string start */
265 (*(sep
- 1) != '\\')) /* and NOT escaped */
270 * Limit the token to data between separators
274 str
= sep
+ 1; /* start of command for next pass */
277 str
= sep
; /* no more commands for next pass */
279 debug_parser("token: \"%s\"\n", token
);
281 /* find macros in this token and replace them */
282 finaltoken
= process_macros(token
, finaltoken
);
284 /* Extract arguments */
285 argc
= cli_parse_line(finaltoken
, argv
);
287 rc
= -1; /* no command at all */
291 if (cmd_process(flag
, argc
, argv
, &repeatable
) != CMD_RET_SUCCESS
)
294 /* Did the user stop this? */
296 rc
= -1; /* if stopped then not repeatable */
304 return rc
? rc
: repeatable
;
307 static int cli_run_command_list(const char *cmd
)
309 return (cli_run_command(cmd
, 0) < 0);
312 /******************************************************************************/
316 * Run a command using the selected parser.
318 * @param cmd Command to run
319 * @param flag Execution flags (CMD_FLAG_...)
320 * @return 0 on success, or != 0 on error.
322 int run_command(const char *cmd
, int flag
)
325 * cli_run_command can return 0 or 1 for success, so clean up
328 if (cli_run_command(cmd
, flag
) == -1)
335 * Run a command using the selected parser, and check if it is repeatable.
337 * @param cmd Command to run
338 * @param flag Execution flags (CMD_FLAG_...)
339 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
341 static int run_command_repeatable(const char *cmd
, int flag
)
343 return cli_run_command(cmd
, flag
);
346 int run_command_list(const char *cmd
, int len
)
350 return cli_run_command_list(cmd
);
353 /****************************************************************************/
358 char *lastcommand
= NULL
;
364 len
= cli_readline(PSTR(CONFIG_SYS_PROMPT
));
366 flag
= 0; /* assume no special flags for now */
369 lastcommand
= strdup(console_buffer
);
371 flag
|= CMD_FLAG_REPEAT
;
374 my_puts_P(PSTR("<INTERRUPT>\n"));
376 rc
= run_command_repeatable(lastcommand
, flag
);
379 /* invalid command or not repeatable, forget it */
387 command_ret_t
do_run(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
393 return CMD_RET_USAGE
;
395 for (i
= 1; i
< argc
; ++i
) {
398 arg
= getenv(argv
[i
]);
400 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv
[i
]);
401 return CMD_RET_FAILURE
;
404 if (run_command(arg
, flag
) != 0)
405 return CMD_RET_FAILURE
;
407 return CMD_RET_SUCCESS
;