cli_simple.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * Add to readline cmdline-editing by
  7. * (C) Copyright 2005
  8. * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
  9. */
  10. #include <common.h>
  11. #include <bootretry.h>
  12. #include <cli.h>
  13. #include <command.h>
  14. #include <console.h>
  15. #include <env.h>
  16. #include <log.h>
  17. #include <linux/ctype.h>
  18. #define DEBUG_PARSER 0 /* set to 1 to debug */
  19. #define debug_parser(fmt, args...) \
  20. debug_cond(DEBUG_PARSER, fmt, ##args)
  21. int cli_simple_parse_line(char *line, char *argv[])
  22. {
  23. int nargs = 0;
  24. debug_parser("%s: \"%s\"\n", __func__, line);
  25. while (nargs < CONFIG_SYS_MAXARGS) {
  26. /* skip any white space */
  27. while (isblank(*line))
  28. ++line;
  29. if (*line == '\0') { /* end of line, no more args */
  30. argv[nargs] = NULL;
  31. debug_parser("%s: nargs=%d\n", __func__, nargs);
  32. return nargs;
  33. }
  34. argv[nargs++] = line; /* begin of argument string */
  35. /* find end of string */
  36. while (*line && !isblank(*line))
  37. ++line;
  38. if (*line == '\0') { /* end of line, no more args */
  39. argv[nargs] = NULL;
  40. debug_parser("parse_line: nargs=%d\n", nargs);
  41. return nargs;
  42. }
  43. *line++ = '\0'; /* terminate current arg */
  44. }
  45. printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
  46. debug_parser("%s: nargs=%d\n", __func__, nargs);
  47. return nargs;
  48. }
  49. void cli_simple_process_macros(const char *input, char *output)
  50. {
  51. char c, prev;
  52. const char *varname_start = NULL;
  53. int inputcnt = strlen(input);
  54. int outputcnt = CONFIG_SYS_CBSIZE;
  55. int state = 0; /* 0 = waiting for '$' */
  56. /* 1 = waiting for '(' or '{' */
  57. /* 2 = waiting for ')' or '}' */
  58. /* 3 = waiting for ''' */
  59. char __maybe_unused *output_start = output;
  60. debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
  61. input);
  62. prev = '\0'; /* previous character */
  63. while (inputcnt && outputcnt) {
  64. c = *input++;
  65. inputcnt--;
  66. if (state != 3) {
  67. /* remove one level of escape characters */
  68. if ((c == '\\') && (prev != '\\')) {
  69. if (inputcnt-- == 0)
  70. break;
  71. prev = c;
  72. c = *input++;
  73. }
  74. }
  75. switch (state) {
  76. case 0: /* Waiting for (unescaped) $ */
  77. if ((c == '\'') && (prev != '\\')) {
  78. state = 3;
  79. break;
  80. }
  81. if ((c == '$') && (prev != '\\')) {
  82. state++;
  83. } else {
  84. *(output++) = c;
  85. outputcnt--;
  86. }
  87. break;
  88. case 1: /* Waiting for ( */
  89. if (c == '(' || c == '{') {
  90. state++;
  91. varname_start = input;
  92. } else {
  93. state = 0;
  94. *(output++) = '$';
  95. outputcnt--;
  96. if (outputcnt) {
  97. *(output++) = c;
  98. outputcnt--;
  99. }
  100. }
  101. break;
  102. case 2: /* Waiting for ) */
  103. if (c == ')' || c == '}') {
  104. int i;
  105. char envname[CONFIG_SYS_CBSIZE], *envval;
  106. /* Varname # of chars */
  107. int envcnt = input - varname_start - 1;
  108. /* Get the varname */
  109. for (i = 0; i < envcnt; i++)
  110. envname[i] = varname_start[i];
  111. envname[i] = 0;
  112. /* Get its value */
  113. envval = env_get(envname);
  114. /* Copy into the line if it exists */
  115. if (envval != NULL)
  116. while ((*envval) && outputcnt) {
  117. *(output++) = *(envval++);
  118. outputcnt--;
  119. }
  120. /* Look for another '$' */
  121. state = 0;
  122. }
  123. break;
  124. case 3: /* Waiting for ' */
  125. if ((c == '\'') && (prev != '\\')) {
  126. state = 0;
  127. } else {
  128. *(output++) = c;
  129. outputcnt--;
  130. }
  131. break;
  132. }
  133. prev = c;
  134. }
  135. if (outputcnt)
  136. *output = 0;
  137. else
  138. *(output - 1) = 0;
  139. debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
  140. strlen(output_start), output_start);
  141. }
  142. /*
  143. * WARNING:
  144. *
  145. * We must create a temporary copy of the command since the command we get
  146. * may be the result from env_get(), which returns a pointer directly to
  147. * the environment data, which may change magicly when the command we run
  148. * creates or modifies environment variables (like "bootp" does).
  149. */
  150. int cli_simple_run_command(const char *cmd, int flag)
  151. {
  152. char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
  153. char *token; /* start of token in cmdbuf */
  154. char *sep; /* end of token (separator) in cmdbuf */
  155. char finaltoken[CONFIG_SYS_CBSIZE];
  156. char *str = cmdbuf;
  157. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  158. int argc, inquotes;
  159. int repeatable = 1;
  160. int rc = 0;
  161. debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
  162. if (DEBUG_PARSER) {
  163. /* use puts - string may be loooong */
  164. puts(cmd ? cmd : "NULL");
  165. puts("\"\n");
  166. }
  167. clear_ctrlc(); /* forget any previous Control C */
  168. if (!cmd || !*cmd)
  169. return -1; /* empty command */
  170. if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
  171. puts("## Command too long!\n");
  172. return -1;
  173. }
  174. strcpy(cmdbuf, cmd);
  175. /* Process separators and check for invalid
  176. * repeatable commands
  177. */
  178. debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
  179. while (*str) {
  180. /*
  181. * Find separator, or string end
  182. * Allow simple escape of ';' by writing "\;"
  183. */
  184. for (inquotes = 0, sep = str; *sep; sep++) {
  185. if ((*sep == '\'') &&
  186. (*(sep - 1) != '\\'))
  187. inquotes = !inquotes;
  188. if (!inquotes &&
  189. (*sep == ';') && /* separator */
  190. (sep != str) && /* past string start */
  191. (*(sep - 1) != '\\')) /* and NOT escaped */
  192. break;
  193. }
  194. /*
  195. * Limit the token to data between separators
  196. */
  197. token = str;
  198. if (*sep) {
  199. str = sep + 1; /* start of command for next pass */
  200. *sep = '\0';
  201. } else {
  202. str = sep; /* no more commands for next pass */
  203. }
  204. debug_parser("token: \"%s\"\n", token);
  205. /* find macros in this token and replace them */
  206. cli_simple_process_macros(token, finaltoken);
  207. /* Extract arguments */
  208. argc = cli_simple_parse_line(finaltoken, argv);
  209. if (argc == 0) {
  210. rc = -1; /* no command at all */
  211. continue;
  212. }
  213. if (cmd_process(flag, argc, argv, &repeatable, NULL))
  214. rc = -1;
  215. /* Did the user stop this? */
  216. if (had_ctrlc())
  217. return -1; /* if stopped then not repeatable */
  218. }
  219. return rc ? rc : repeatable;
  220. }
  221. void cli_simple_loop(void)
  222. {
  223. static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };
  224. int len;
  225. int flag;
  226. int rc = 1;
  227. for (;;) {
  228. if (rc >= 0) {
  229. /* Saw enough of a valid command to
  230. * restart the timeout.
  231. */
  232. bootretry_reset_cmd_timeout();
  233. }
  234. len = cli_readline(CONFIG_SYS_PROMPT);
  235. flag = 0; /* assume no special flags for now */
  236. if (len > 0)
  237. strlcpy(lastcommand, console_buffer,
  238. CONFIG_SYS_CBSIZE + 1);
  239. else if (len == 0)
  240. flag |= CMD_FLAG_REPEAT;
  241. #ifdef CONFIG_BOOT_RETRY_TIME
  242. else if (len == -2) {
  243. /* -2 means timed out, retry autoboot
  244. */
  245. puts("\nTimed out waiting for command\n");
  246. # ifdef CONFIG_RESET_TO_RETRY
  247. /* Reinit board to run initialization code again */
  248. do_reset(NULL, 0, 0, NULL);
  249. # else
  250. return; /* retry autoboot */
  251. # endif
  252. }
  253. #endif
  254. if (len == -1)
  255. puts("<INTERRUPT>\n");
  256. else
  257. rc = run_command_repeatable(lastcommand, flag);
  258. if (rc <= 0) {
  259. /* invalid command or not repeatable, forget it */
  260. lastcommand[0] = 0;
  261. }
  262. }
  263. }
  264. int cli_simple_run_command_list(char *cmd, int flag)
  265. {
  266. char *line, *next;
  267. int rcode = 0;
  268. /*
  269. * Break into individual lines, and execute each line; terminate on
  270. * error.
  271. */
  272. next = cmd;
  273. line = cmd;
  274. while (*next) {
  275. if (*next == '\n') {
  276. *next = '\0';
  277. /* run only non-empty commands */
  278. if (*line) {
  279. debug("** exec: \"%s\"\n", line);
  280. if (cli_simple_run_command(line, 0) < 0) {
  281. rcode = 1;
  282. break;
  283. }
  284. }
  285. line = next + 1;
  286. }
  287. ++next;
  288. }
  289. if (rcode == 0 && *line)
  290. rcode = (cli_simple_run_command(line, 0) < 0);
  291. return rcode;
  292. }