]>
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)
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'; (c
= *inp
) != '\0'; inp
++) {
50 case 0: /* before arg string, waiting for arg start */
54 argv
[nargs
++] = inp
; /* begin of argument string */
59 case 1: /* in arg string, waiting for end of arg string */
64 if (c
== '\"' || c
== '\'') {
75 case 3: /* in quote */
76 if (c
== '\\' && quote
== '\"') {
86 case 2: /* waiting for next char */
93 if (nargs
> CONFIG_SYS_MAXARGS
) {
101 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
105 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
107 for (int i
= 0; i
< nargs
; i
++)
108 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
115 void append_char(uint_fast8_t pass
, char **p
, char c
)
125 char *process_macros(char *input
, char *output
)
127 char c
, prev
, *inp
, *outp
;
128 const char *varname
= NULL
;
130 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
132 uint_fast8_t state
= 0;
133 /* 0 = waiting for '$' */
134 /* 1 = waiting for '{' */
135 /* 2 = waiting for '}' */
136 /* 3 = waiting for ''' */
141 int outputlen
= outp
- output
;
142 outp
= xrealloc(output
, outputlen
);
148 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
151 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
156 case 0: /* Waiting for (unescaped) $ */
157 if ((c
== '\'') && (prev
!= '\\')) {
161 if ((c
== '$') && (prev
!= '\\')) {
166 case 1: /* Waiting for { */
173 append_char(pass
, &outp
, '$');
176 case 2: /* Waiting for } */
178 /* Terminate variable name */
180 const char *envval
= getenv_char(varname
);
182 /* Copy into the line if it exists */
185 append_char(pass
, &outp
, *(envval
++));
186 /* Look for another '$' */
190 case 3: /* Waiting for ' */
195 append_char(pass
, &outp
, c
);
198 append_char(pass
, &outp
, 0);
201 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
202 strlen(output
), output
);
212 * We must create a temporary copy of the command since the command we get
213 * may be the result from getenv_char(), which returns a pointer directly to
214 * the environment data, which may change magicly when the command we run
215 * creates or modifies environment variables (like "bootp" does).
223 static int cli_run_command(const char *cmd
, int flag
)
225 char *cmdbuf
; /* working copy of cmd */
226 char *token
; /* start of token in cmdbuf */
227 char *sep
; /* end of token (separator) in cmdbuf */
228 char *finaltoken
= NULL
; /* token after macro expansion */
230 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
232 uint_fast8_t inquotes
, repeatable
= 1;
235 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
236 cmd
, cmd
? cmd
: "NULL");
238 clear_ctrlc(); /* forget any previous Control C */
241 return -1; /* empty command */
243 cmdbuf
= strdup(cmd
);
245 return -1; /* not enough memory */
249 /* Process separators and check for invalid
250 * repeatable commands
253 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
256 * Find separator, or string end
257 * Allow simple escape of ';' by writing "\;"
259 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
260 if ((*sep
== '\'') &&
261 (sep
!= str
) && /* past string start */
262 (*(sep
- 1) != '\\')) /* and NOT escaped */
263 inquotes
= !inquotes
;
266 (*sep
== ';' || *sep
== '\n' /* separator */
267 || *sep
== '#') && /* or start of comment */
268 ((sep
== str
) || /* string start */
269 (*(sep
- 1) != '\\'))) /* or NOT escaped */
273 /* no more commands after unescaped '#' token */
277 /* Limit the token to data between separators */
280 str
= sep
+ 1; /* start of command for next pass */
283 str
= sep
; /* no more commands for next pass */
285 debug_parser("token: \"%s\"\n", token
);
287 /* find macros in this token and replace them */
288 finaltoken
= process_macros(token
, finaltoken
);
290 /* Extract arguments */
291 argc
= cli_parse_line(finaltoken
, argv
);
293 rc
= -1; /* no command at all */
297 if (cmd_process(flag
, argc
, argv
, &repeatable
) != CMD_RET_SUCCESS
)
300 /* Did the user stop this? */
302 rc
= -1; /* if stopped then not repeatable */
310 return rc
? rc
: repeatable
;
313 static int cli_run_command_list(const char *cmd
)
315 return (cli_run_command(cmd
, 0) < 0);
318 /******************************************************************************/
324 * @param cmd Command to run
325 * @param flag Execution flags (CMD_FLAG_...)
326 * @return 0 on success, or != 0 on error.
328 int run_command(const char *cmd
, int flag
)
331 * cli_run_command can return 0 or 1 for success, so clean up
334 if (cli_run_command(cmd
, flag
) == -1)
341 * Run a command using the selected parser, and check if it is repeatable.
343 * @param cmd Command to run
344 * @param flag Execution flags (CMD_FLAG_...)
345 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
347 static int run_command_repeatable(const char *cmd
, int flag
)
349 return cli_run_command(cmd
, flag
);
352 int run_command_list(const char *cmd
, int len
)
356 return cli_run_command_list(cmd
);
359 /****************************************************************************/
364 char *lastcommand
= NULL
;
370 len
= cli_readline(PSTR(CONFIG_SYS_PROMPT
), 1);
372 flag
= 0; /* assume no special flags for now */
375 lastcommand
= strdup(console_buffer
);
377 flag
|= CMD_FLAG_REPEAT
;
380 my_puts_P(PSTR("<INTERRUPT>\n"));
382 rc
= run_command_repeatable(lastcommand
, flag
);
385 /* invalid command or not repeatable, forget it */