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>
17 #include <linux/ctype.h>
19 #define DEBUG_PARSER 0 /* set to 1 to debug */
21 #define debug_parser(fmt, args...) \
22 debug_cond(DEBUG_PARSER, fmt, ##args)
25 int cli_simple_parse_line(char *line, char *argv[])
29 debug_parser("%s: \"%s\"\n", __func__, line);
30 while (nargs < CONFIG_SYS_MAXARGS) {
31 /* skip any white space */
32 while (isblank(*line))
35 if (*line == '\0') { /* end of line, no more args */
37 debug_parser("%s: nargs=%d\n", __func__, nargs);
41 argv[nargs++] = line; /* begin of argument string */
43 /* find end of string */
44 while (*line && !isblank(*line))
47 if (*line == '\0') { /* end of line, no more args */
49 debug_parser("parse_line: nargs=%d\n", nargs);
53 *line++ = '\0'; /* terminate current arg */
56 printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
58 debug_parser("%s: nargs=%d\n", __func__, nargs);
62 void cli_simple_process_macros(const char *input, char *output)
65 const char *varname_start = NULL;
66 int inputcnt = strlen(input);
67 int outputcnt = CONFIG_SYS_CBSIZE;
68 int state = 0; /* 0 = waiting for '$' */
70 /* 1 = waiting for '(' or '{' */
71 /* 2 = waiting for ')' or '}' */
72 /* 3 = waiting for ''' */
73 char __maybe_unused *output_start = output;
75 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
78 prev = '\0'; /* previous character */
80 while (inputcnt && outputcnt) {
85 /* remove one level of escape characters */
86 if ((c == '\\') && (prev != '\\')) {
95 case 0: /* Waiting for (unescaped) $ */
96 if ((c == '\'') && (prev != '\\')) {
100 if ((c == '$') && (prev != '\\')) {
107 case 1: /* Waiting for ( */
108 if (c == '(' || c == '{') {
110 varname_start = input;
122 case 2: /* Waiting for ) */
123 if (c == ')' || c == '}') {
125 char envname[CONFIG_SYS_CBSIZE], *envval;
126 /* Varname # of chars */
127 int envcnt = input - varname_start - 1;
129 /* Get the varname */
130 for (i = 0; i < envcnt; i++)
131 envname[i] = varname_start[i];
135 envval = env_get(envname);
137 /* Copy into the line if it exists */
139 while ((*envval) && outputcnt) {
140 *(output++) = *(envval++);
143 /* Look for another '$' */
147 case 3: /* Waiting for ' */
148 if ((c == '\'') && (prev != '\\')) {
164 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
165 strlen(output_start), output_start);
171 * We must create a temporary copy of the command since the command we get
172 * may be the result from env_get(), which returns a pointer directly to
173 * the environment data, which may change magicly when the command we run
174 * creates or modifies environment variables (like "bootp" does).
176 int cli_simple_run_command(const char *cmd, int flag)
178 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
179 char *token; /* start of token in cmdbuf */
180 char *sep; /* end of token (separator) in cmdbuf */
181 char finaltoken[CONFIG_SYS_CBSIZE];
183 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
188 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
190 /* use puts - string may be loooong */
191 puts(cmd ? cmd : "NULL");
194 clear_ctrlc(); /* forget any previous Control C */
197 return -1; /* empty command */
199 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
200 puts("## Command too long!\n");
206 /* Process separators and check for invalid
207 * repeatable commands
210 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
213 * Find separator, or string end
214 * Allow simple escape of ';' by writing "\;"
216 for (inquotes = 0, sep = str; *sep; sep++) {
217 if ((*sep == '\'') &&
218 (*(sep - 1) != '\\'))
219 inquotes = !inquotes;
222 (*sep == ';') && /* separator */
223 (sep != str) && /* past string start */
224 (*(sep - 1) != '\\')) /* and NOT escaped */
229 * Limit the token to data between separators
233 str = sep + 1; /* start of command for next pass */
236 str = sep; /* no more commands for next pass */
238 debug_parser("token: \"%s\"\n", token);
240 /* find macros in this token and replace them */
241 cli_simple_process_macros(token, finaltoken);
243 /* Extract arguments */
244 argc = cli_simple_parse_line(finaltoken, argv);
246 rc = -1; /* no command at all */
250 if (cmd_process(flag, argc, argv, &repeatable, NULL))
253 /* Did the user stop this? */
255 return -1; /* if stopped then not repeatable */
258 return rc ? rc : repeatable;
261 void cli_simple_loop(void)
263 static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };
271 /* Saw enough of a valid command to
272 * restart the timeout.
274 bootretry_reset_cmd_timeout();
276 len = cli_readline(CONFIG_SYS_PROMPT);
278 flag = 0; /* assume no special flags for now */
280 strlcpy(lastcommand, console_buffer,
281 CONFIG_SYS_CBSIZE + 1);
283 flag |= CMD_FLAG_REPEAT;
284 #ifdef CONFIG_BOOT_RETRY_TIME
285 else if (len == -2) {
286 /* -2 means timed out, retry autoboot
288 puts("\nTimed out waiting for command\n");
289 # ifdef CONFIG_RESET_TO_RETRY
290 /* Reinit board to run initialization code again */
291 do_reset(NULL, 0, 0, NULL);
293 return; /* retry autoboot */
299 puts("<INTERRUPT>\n");
301 rc = run_command_repeatable(lastcommand, flag);
304 /* invalid command or not repeatable, forget it */
310 int cli_simple_run_command_list(char *cmd, int flag)
316 * Break into individual lines, and execute each line; terminate on
324 /* run only non-empty commands */
326 debug("** exec: \"%s\"\n", line);
327 if (cli_simple_run_command(line, 0) < 0) {
336 if (rcode == 0 && *line)
337 rcode = (cli_simple_run_command(line, 0) < 0);