]>
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"
30 #define DEBUG_PARSER 0 /* set to 1 to debug */
32 #define debug_parser(fmt, args...) \
33 debug_cond(DEBUG_PARSER, fmt, ##args)
35 static int cli_parse_line(char *line
, char *argv
[])
37 uint_fast8_t state
= 0;
38 uint_fast8_t nargs
= 0;
42 debug_parser("%s: \"%s\"\n", __func__
, line
);
44 for (outp
= inp
= line
, quote
= '\0' ;
45 nargs
< CONFIG_SYS_MAXARGS
&& (c
= *inp
) != '\0';
53 argv
[nargs
++] = inp
; /* begin of argument string */
63 if (c
== '\"' || c
== '\'') {
75 if (c
== '\\' && quote
== '\"') {
95 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
99 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
101 for (int i
= 0; i
< nargs
; i
++)
102 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
109 void append_char(uint_fast8_t pass
, char **p
, char c
)
119 char *process_macros(char *input
, char *output
)
121 char c
, prev
, *inp
, *outp
;
122 const char *varname
= NULL
;
124 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
126 uint_fast8_t state
= 0;
127 /* 0 = waiting for '$' */
128 /* 1 = waiting for '{' */
129 /* 2 = waiting for '}' */
130 /* 3 = waiting for ''' */
135 int outputlen
= outp
- output
;
136 outp
= xrealloc(output
, outputlen
);
142 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
145 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
150 case 0: /* Waiting for (unescaped) $ */
151 if ((c
== '\'') && (prev
!= '\\')) {
155 if ((c
== '$') && (prev
!= '\\')) {
160 case 1: /* Waiting for { */
167 append_char(pass
, &outp
, '$');
170 case 2: /* Waiting for } */
172 /* Terminate variable name */
174 const char *envval
= getenv(varname
);
176 /* Copy into the line if it exists */
179 append_char(pass
, &outp
, *(envval
++));
180 /* Look for another '$' */
184 case 3: /* Waiting for ' */
189 append_char(pass
, &outp
, c
);
192 append_char(pass
, &outp
, 0);
195 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
196 strlen(output
), output
);
206 * We must create a temporary copy of the command since the command we get
207 * may be the result from getenv(), which returns a pointer directly to
208 * the environment data, which may change magicly when the command we run
209 * creates or modifies environment variables (like "bootp" does).
217 static int cli_run_command(const char *cmd
, int flag
)
219 char *cmdbuf
; /* working copy of cmd */
220 char *token
; /* start of token in cmdbuf */
221 char *sep
; /* end of token (separator) in cmdbuf */
222 char *finaltoken
= NULL
; /* token after macro expansion */
224 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
226 uint_fast8_t inquotes
, repeatable
= 1;
229 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
230 cmd
, cmd
? cmd
: "NULL");
232 clear_ctrlc(); /* forget any previous Control C */
235 return -1; /* empty command */
237 cmdbuf
= strdup(cmd
);
239 return -1; /* not enough memory */
243 /* Process separators and check for invalid
244 * repeatable commands
247 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
250 * Find separator, or string end
251 * Allow simple escape of ';' by writing "\;"
253 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
254 if ((*sep
== '\'') &&
255 (*(sep
- 1) != '\\'))
256 inquotes
= !inquotes
;
259 (*sep
== ';' || *sep
== '\n') && /* separator */
260 (sep
!= str
) && /* past string start */
261 (*(sep
- 1) != '\\')) /* and NOT escaped */
266 * Limit the token to data between separators
270 str
= sep
+ 1; /* start of command for next pass */
273 str
= sep
; /* no more commands for next pass */
275 debug_parser("token: \"%s\"\n", token
);
277 /* find macros in this token and replace them */
278 finaltoken
= process_macros(token
, finaltoken
);
280 /* Extract arguments */
281 argc
= cli_parse_line(finaltoken
, argv
);
283 rc
= -1; /* no command at all */
287 if (cmd_process(flag
, argc
, argv
, &repeatable
) != CMD_RET_SUCCESS
)
290 /* Did the user stop this? */
292 rc
= -1; /* if stopped then not repeatable */
300 return rc
? rc
: repeatable
;
303 static int cli_run_command_list(const char *cmd
)
305 return (cli_run_command(cmd
, 0) < 0);
308 /******************************************************************************/
312 * Run a command using the selected parser.
314 * @param cmd Command to run
315 * @param flag Execution flags (CMD_FLAG_...)
316 * @return 0 on success, or != 0 on error.
318 int run_command(const char *cmd
, int flag
)
321 * cli_run_command can return 0 or 1 for success, so clean up
324 if (cli_run_command(cmd
, flag
) == -1)
331 * Run a command using the selected parser, and check if it is repeatable.
333 * @param cmd Command to run
334 * @param flag Execution flags (CMD_FLAG_...)
335 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
337 static int run_command_repeatable(const char *cmd
, int flag
)
339 return cli_run_command(cmd
, flag
);
342 int run_command_list(const char *cmd
, int len
)
346 return cli_run_command_list(cmd
);
349 /****************************************************************************/
354 char *lastcommand
= NULL
;
360 len
= cli_readline(PSTR(CONFIG_SYS_PROMPT
));
362 flag
= 0; /* assume no special flags for now */
365 lastcommand
= strdup(console_buffer
);
367 flag
|= CMD_FLAG_REPEAT
;
370 my_puts_P(PSTR("<INTERRUPT>\n"));
372 rc
= run_command_repeatable(lastcommand
, flag
);
375 /* invalid command or not repeatable, forget it */
383 command_ret_t
do_run(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
389 return CMD_RET_USAGE
;
391 for (i
= 1; i
< argc
; ++i
) {
394 arg
= getenv(argv
[i
]);
396 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv
[i
]);
397 return CMD_RET_FAILURE
;
400 if (run_command(arg
, flag
) != 0)
401 return CMD_RET_FAILURE
;
403 return CMD_RET_SUCCESS
;