cli_simple.c 7.3 KB

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