1 // SPDX-License-Identifier: GPL-2.0+
6 * Add to readline cmdline-editing by
11 #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)
24 int cli_simple_process_macros(const char *input, char *output, int max_size)
27 const char *varname_start = NULL;
28 int inputcnt = strlen(input);
29 int outputcnt = max_size;
30 int state = 0; /* 0 = waiting for '$' */
33 /* 1 = waiting for '(' or '{' */
34 /* 2 = waiting for ')' or '}' */
35 /* 3 = waiting for ''' */
36 char __maybe_unused *output_start = output;
38 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
41 prev = '\0'; /* previous character */
43 while (inputcnt && outputcnt) {
48 /* remove one level of escape characters */
49 if ((c == '\\') && (prev != '\\')) {
58 case 0: /* Waiting for (unescaped) $ */
59 if ((c == '\'') && (prev != '\\')) {
63 if ((c == '$') && (prev != '\\')) {
70 case 1: /* Waiting for ( */
71 if (c == '(' || c == '{') {
73 varname_start = input;
85 case 2: /* Waiting for ) */
86 if (c == ')' || c == '}') {
88 char envname[CONFIG_SYS_CBSIZE], *envval;
89 /* Varname # of chars */
90 int envcnt = input - varname_start - 1;
93 for (i = 0; i < envcnt; i++)
94 envname[i] = varname_start[i];
98 envval = env_get(envname);
100 /* Copy into the line if it exists */
102 while ((*envval) && outputcnt) {
103 *(output++) = *(envval++);
106 /* Look for another '$' */
110 case 3: /* Waiting for ' */
111 if ((c == '\'') && (prev != '\\')) {
122 ret = inputcnt ? -ENOSPC : 0;
130 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
131 strlen(output_start), output_start);
136 #ifdef CONFIG_CMDLINE
137 int cli_simple_parse_line(char *line, char *argv[])
141 debug_parser("%s: \"%s\"\n", __func__, line);
142 while (nargs < CONFIG_SYS_MAXARGS) {
143 /* skip any white space */
144 while (isblank(*line))
147 if (*line == '\0') { /* end of line, no more args */
149 debug_parser("%s: nargs=%d\n", __func__, nargs);
153 argv[nargs++] = line; /* begin of argument string */
155 /* find end of string */
156 while (*line && !isblank(*line))
159 if (*line == '\0') { /* end of line, no more args */
161 debug_parser("parse_line: nargs=%d\n", nargs);
165 *line++ = '\0'; /* terminate current arg */
168 printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
170 debug_parser("%s: nargs=%d\n", __func__, nargs);
177 * We must create a temporary copy of the command since the command we get
178 * may be the result from env_get(), which returns a pointer directly to
179 * the environment data, which may change magicly when the command we run
180 * creates or modifies environment variables (like "bootp" does).
182 int cli_simple_run_command(const char *cmd, int flag)
184 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
185 char *token; /* start of token in cmdbuf */
186 char *sep; /* end of token (separator) in cmdbuf */
187 char finaltoken[CONFIG_SYS_CBSIZE];
189 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
194 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
196 /* use puts - string may be loooong */
197 puts(cmd ? cmd : "NULL");
200 clear_ctrlc(); /* forget any previous Control C */
203 return -1; /* empty command */
205 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
206 puts("## Command too long!\n");
212 /* Process separators and check for invalid
213 * repeatable commands
216 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
219 * Find separator, or string end
220 * Allow simple escape of ';' by writing "\;"
222 for (inquotes = 0, sep = str; *sep; sep++) {
223 if ((*sep == '\'') &&
224 (*(sep - 1) != '\\'))
225 inquotes = !inquotes;
228 (*sep == ';') && /* separator */
229 (sep != str) && /* past string start */
230 (*(sep - 1) != '\\')) /* and NOT escaped */
235 * Limit the token to data between separators
239 str = sep + 1; /* start of command for next pass */
242 str = sep; /* no more commands for next pass */
244 debug_parser("token: \"%s\"\n", token);
246 /* find macros in this token and replace them */
247 cli_simple_process_macros(token, finaltoken,
250 /* Extract arguments */
251 argc = cli_simple_parse_line(finaltoken, argv);
253 rc = -1; /* no command at all */
257 if (cmd_process(flag, argc, argv, &repeatable, NULL))
260 /* Did the user stop this? */
262 return -1; /* if stopped then not repeatable */
265 return rc ? rc : repeatable;
268 void cli_simple_loop(void)
270 static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };
278 /* Saw enough of a valid command to
279 * restart the timeout.
281 bootretry_reset_cmd_timeout();
283 len = cli_readline(CONFIG_SYS_PROMPT);
285 flag = 0; /* assume no special flags for now */
287 strlcpy(lastcommand, console_buffer,
288 CONFIG_SYS_CBSIZE + 1);
290 flag |= CMD_FLAG_REPEAT;
291 #ifdef CONFIG_BOOT_RETRY_TIME
292 else if (len == -2) {
293 /* -2 means timed out, retry autoboot
295 puts("\nTimed out waiting for command\n");
296 # ifdef CONFIG_RESET_TO_RETRY
297 /* Reinit board to run initialization code again */
298 do_reset(NULL, 0, 0, NULL);
300 return; /* retry autoboot */
306 puts("<INTERRUPT>\n");
308 rc = run_command_repeatable(lastcommand, flag);
311 /* invalid command or not repeatable, forget it */
317 int cli_simple_run_command_list(char *cmd, int flag)
323 * Break into individual lines, and execute each line; terminate on
331 /* run only non-empty commands */
333 debug("** exec: \"%s\"\n", line);
334 if (cli_simple_run_command(line, 0) < 0) {
343 if (rcode == 0 && *line)
344 rcode = (cli_simple_run_command(line, 0) < 0);