cli_simple.c 7.3 KB

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