cli_simple.c 7.4 KB

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