cli_simple.c 7.3 KB

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