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>
15 #include <linux/ctype.h>
17 #define DEBUG_PARSER 0 /* set to 1 to debug */
19 #define debug_parser(fmt, args...) \
20 debug_cond(DEBUG_PARSER, fmt, ##args)
23 int cli_simple_parse_line(char *line, char *argv[])
27 debug_parser("%s: \"%s\"\n", __func__, line);
28 while (nargs < CONFIG_SYS_MAXARGS) {
29 /* skip any white space */
30 while (isblank(*line))
33 if (*line == '\0') { /* end of line, no more args */
35 debug_parser("%s: nargs=%d\n", __func__, nargs);
39 argv[nargs++] = line; /* begin of argument string */
41 /* find end of string */
42 while (*line && !isblank(*line))
45 if (*line == '\0') { /* end of line, no more args */
47 debug_parser("parse_line: nargs=%d\n", nargs);
51 *line++ = '\0'; /* terminate current arg */
54 printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
56 debug_parser("%s: nargs=%d\n", __func__, nargs);
60 void cli_simple_process_macros(const char *input, char *output)
63 const char *varname_start = NULL;
64 int inputcnt = strlen(input);
65 int outputcnt = CONFIG_SYS_CBSIZE;
66 int state = 0; /* 0 = waiting for '$' */
68 /* 1 = waiting for '(' or '{' */
69 /* 2 = waiting for ')' or '}' */
70 /* 3 = waiting for ''' */
71 char __maybe_unused *output_start = output;
73 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
76 prev = '\0'; /* previous character */
78 while (inputcnt && outputcnt) {
83 /* remove one level of escape characters */
84 if ((c == '\\') && (prev != '\\')) {
93 case 0: /* Waiting for (unescaped) $ */
94 if ((c == '\'') && (prev != '\\')) {
98 if ((c == '$') && (prev != '\\')) {
105 case 1: /* Waiting for ( */
106 if (c == '(' || c == '{') {
108 varname_start = input;
120 case 2: /* Waiting for ) */
121 if (c == ')' || c == '}') {
123 char envname[CONFIG_SYS_CBSIZE], *envval;
124 /* Varname # of chars */
125 int envcnt = input - varname_start - 1;
127 /* Get the varname */
128 for (i = 0; i < envcnt; i++)
129 envname[i] = varname_start[i];
133 envval = env_get(envname);
135 /* Copy into the line if it exists */
137 while ((*envval) && outputcnt) {
138 *(output++) = *(envval++);
141 /* Look for another '$' */
145 case 3: /* Waiting for ' */
146 if ((c == '\'') && (prev != '\\')) {
162 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
163 strlen(output_start), output_start);
169 * We must create a temporary copy of the command since the command we get
170 * may be the result from env_get(), which returns a pointer directly to
171 * the environment data, which may change magicly when the command we run
172 * creates or modifies environment variables (like "bootp" does).
174 int cli_simple_run_command(const char *cmd, int flag)
176 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
177 char *token; /* start of token in cmdbuf */
178 char *sep; /* end of token (separator) in cmdbuf */
179 char finaltoken[CONFIG_SYS_CBSIZE];
181 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
186 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
188 /* use puts - string may be loooong */
189 puts(cmd ? cmd : "NULL");
192 clear_ctrlc(); /* forget any previous Control C */
195 return -1; /* empty command */
197 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
198 puts("## Command too long!\n");
204 /* Process separators and check for invalid
205 * repeatable commands
208 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
211 * Find separator, or string end
212 * Allow simple escape of ';' by writing "\;"
214 for (inquotes = 0, sep = str; *sep; sep++) {
215 if ((*sep == '\'') &&
216 (*(sep - 1) != '\\'))
217 inquotes = !inquotes;
220 (*sep == ';') && /* separator */
221 (sep != str) && /* past string start */
222 (*(sep - 1) != '\\')) /* and NOT escaped */
227 * Limit the token to data between separators
231 str = sep + 1; /* start of command for next pass */
234 str = sep; /* no more commands for next pass */
236 debug_parser("token: \"%s\"\n", token);
238 /* find macros in this token and replace them */
239 cli_simple_process_macros(token, finaltoken);
241 /* Extract arguments */
242 argc = cli_simple_parse_line(finaltoken, argv);
244 rc = -1; /* no command at all */
248 if (cmd_process(flag, argc, argv, &repeatable, NULL))
251 /* Did the user stop this? */
253 return -1; /* if stopped then not repeatable */
256 return rc ? rc : repeatable;
259 void cli_simple_loop(void)
261 static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };
269 /* Saw enough of a valid command to
270 * restart the timeout.
272 bootretry_reset_cmd_timeout();
274 len = cli_readline(CONFIG_SYS_PROMPT);
276 flag = 0; /* assume no special flags for now */
278 strlcpy(lastcommand, console_buffer,
279 CONFIG_SYS_CBSIZE + 1);
281 flag |= CMD_FLAG_REPEAT;
282 #ifdef CONFIG_BOOT_RETRY_TIME
283 else if (len == -2) {
284 /* -2 means timed out, retry autoboot
286 puts("\nTimed out waiting for command\n");
287 # ifdef CONFIG_RESET_TO_RETRY
288 /* Reinit board to run initialization code again */
289 do_reset(NULL, 0, 0, NULL);
291 return; /* retry autoboot */
297 puts("<INTERRUPT>\n");
299 rc = run_command_repeatable(lastcommand, flag);
302 /* invalid command or not repeatable, forget it */
308 int cli_simple_run_command_list(char *cmd, int flag)
314 * Break into individual lines, and execute each line; terminate on
322 /* run only non-empty commands */
324 debug("** exec: \"%s\"\n", line);
325 if (cli_simple_run_command(line, 0) < 0) {
334 if (rcode == 0 && *line)
335 rcode = (cli_simple_run_command(line, 0) < 0);