1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Add to readline cmdline-editing by
8 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
12 #include <bootretry.h>
18 #include <linux/ctype.h>
20 #define DEBUG_PARSER 0 /* set to 1 to debug */
22 #define debug_parser(fmt, args...) \
23 debug_cond(DEBUG_PARSER, fmt, ##args)
26 int cli_simple_parse_line(char *line, char *argv[])
30 debug_parser("%s: \"%s\"\n", __func__, line);
31 while (nargs < CONFIG_SYS_MAXARGS) {
32 /* skip any white space */
33 while (isblank(*line))
36 if (*line == '\0') { /* end of line, no more args */
38 debug_parser("%s: nargs=%d\n", __func__, nargs);
42 argv[nargs++] = line; /* begin of argument string */
44 /* find end of string */
45 while (*line && !isblank(*line))
48 if (*line == '\0') { /* end of line, no more args */
50 debug_parser("parse_line: nargs=%d\n", nargs);
54 *line++ = '\0'; /* terminate current arg */
57 printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
59 debug_parser("%s: nargs=%d\n", __func__, nargs);
63 void cli_simple_process_macros(const char *input, char *output)
66 const char *varname_start = NULL;
67 int inputcnt = strlen(input);
68 int outputcnt = CONFIG_SYS_CBSIZE;
69 int state = 0; /* 0 = waiting for '$' */
71 /* 1 = waiting for '(' or '{' */
72 /* 2 = waiting for ')' or '}' */
73 /* 3 = waiting for ''' */
74 char __maybe_unused *output_start = output;
76 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
79 prev = '\0'; /* previous character */
81 while (inputcnt && outputcnt) {
86 /* remove one level of escape characters */
87 if ((c == '\\') && (prev != '\\')) {
96 case 0: /* Waiting for (unescaped) $ */
97 if ((c == '\'') && (prev != '\\')) {
101 if ((c == '$') && (prev != '\\')) {
108 case 1: /* Waiting for ( */
109 if (c == '(' || c == '{') {
111 varname_start = input;
123 case 2: /* Waiting for ) */
124 if (c == ')' || c == '}') {
126 char envname[CONFIG_SYS_CBSIZE], *envval;
127 /* Varname # of chars */
128 int envcnt = input - varname_start - 1;
130 /* Get the varname */
131 for (i = 0; i < envcnt; i++)
132 envname[i] = varname_start[i];
136 envval = env_get(envname);
138 /* Copy into the line if it exists */
140 while ((*envval) && outputcnt) {
141 *(output++) = *(envval++);
144 /* Look for another '$' */
148 case 3: /* Waiting for ' */
149 if ((c == '\'') && (prev != '\\')) {
165 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
166 strlen(output_start), output_start);
172 * We must create a temporary copy of the command since the command we get
173 * may be the result from env_get(), which returns a pointer directly to
174 * the environment data, which may change magicly when the command we run
175 * creates or modifies environment variables (like "bootp" does).
177 int cli_simple_run_command(const char *cmd, int flag)
179 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
180 char *token; /* start of token in cmdbuf */
181 char *sep; /* end of token (separator) in cmdbuf */
182 char finaltoken[CONFIG_SYS_CBSIZE];
184 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
189 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
191 /* use puts - string may be loooong */
192 puts(cmd ? cmd : "NULL");
195 clear_ctrlc(); /* forget any previous Control C */
198 return -1; /* empty command */
200 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
201 puts("## Command too long!\n");
207 /* Process separators and check for invalid
208 * repeatable commands
211 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
214 * Find separator, or string end
215 * Allow simple escape of ';' by writing "\;"
217 for (inquotes = 0, sep = str; *sep; sep++) {
218 if ((*sep == '\'') &&
219 (*(sep - 1) != '\\'))
220 inquotes = !inquotes;
223 (*sep == ';') && /* separator */
224 (sep != str) && /* past string start */
225 (*(sep - 1) != '\\')) /* and NOT escaped */
230 * Limit the token to data between separators
234 str = sep + 1; /* start of command for next pass */
237 str = sep; /* no more commands for next pass */
239 debug_parser("token: \"%s\"\n", token);
241 /* find macros in this token and replace them */
242 cli_simple_process_macros(token, finaltoken);
244 /* Extract arguments */
245 argc = cli_simple_parse_line(finaltoken, argv);
247 rc = -1; /* no command at all */
251 if (cmd_process(flag, argc, argv, &repeatable, NULL))
254 /* Did the user stop this? */
256 return -1; /* if stopped then not repeatable */
259 return rc ? rc : repeatable;
262 void cli_simple_loop(void)
264 static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };
272 /* Saw enough of a valid command to
273 * restart the timeout.
275 bootretry_reset_cmd_timeout();
277 len = cli_readline(CONFIG_SYS_PROMPT);
279 flag = 0; /* assume no special flags for now */
281 strlcpy(lastcommand, console_buffer,
282 CONFIG_SYS_CBSIZE + 1);
284 flag |= CMD_FLAG_REPEAT;
285 #ifdef CONFIG_BOOT_RETRY_TIME
286 else if (len == -2) {
287 /* -2 means timed out, retry autoboot
289 puts("\nTimed out waiting for command\n");
290 # ifdef CONFIG_RESET_TO_RETRY
291 /* Reinit board to run initialization code again */
292 do_reset(NULL, 0, 0, NULL);
294 return; /* retry autoboot */
300 puts("<INTERRUPT>\n");
302 rc = run_command_repeatable(lastcommand, flag);
305 /* invalid command or not repeatable, forget it */
311 int cli_simple_run_command_list(char *cmd, int flag)
317 * Break into individual lines, and execute each line; terminate on
325 /* run only non-empty commands */
327 debug("** exec: \"%s\"\n", line);
328 if (cli_simple_run_command(line, 0) < 0) {
337 if (rcode == 0 && *line)
338 rcode = (cli_simple_run_command(line, 0) < 0);