command.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Command Processor Table
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. int
  29. do_version (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  30. {
  31. extern char version_string[];
  32. printf ("\n%s\n", version_string);
  33. return 0;
  34. }
  35. U_BOOT_CMD(
  36. version, 1, 1, do_version,
  37. "version - print monitor version\n",
  38. NULL
  39. );
  40. #if defined(CONFIG_CMD_ECHO)
  41. int
  42. do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  43. {
  44. int i, putnl = 1;
  45. for (i = 1; i < argc; i++) {
  46. char *p = argv[i], c;
  47. if (i > 1)
  48. putc(' ');
  49. while ((c = *p++) != '\0') {
  50. if (c == '\\' && *p == 'c') {
  51. putnl = 0;
  52. p++;
  53. } else {
  54. putc(c);
  55. }
  56. }
  57. }
  58. if (putnl)
  59. putc('\n');
  60. return 0;
  61. }
  62. U_BOOT_CMD(
  63. echo, CFG_MAXARGS, 1, do_echo,
  64. "echo - echo args to console\n",
  65. "[args..]\n"
  66. " - echo args to console; \\c suppresses newline\n"
  67. );
  68. #endif
  69. #ifdef CFG_HUSH_PARSER
  70. int
  71. do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  72. {
  73. char **ap;
  74. int left, adv, expr, last_expr, neg, last_cmp;
  75. /* args? */
  76. if (argc < 3)
  77. return 1;
  78. #if 0
  79. {
  80. printf("test:");
  81. left = 1;
  82. while (argv[left])
  83. printf(" %s", argv[left++]);
  84. }
  85. #endif
  86. last_expr = 0;
  87. left = argc - 1; ap = argv + 1;
  88. if (left > 0 && strcmp(ap[0], "!") == 0) {
  89. neg = 1;
  90. ap++;
  91. left--;
  92. } else
  93. neg = 0;
  94. expr = -1;
  95. last_cmp = -1;
  96. last_expr = -1;
  97. while (left > 0) {
  98. if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
  99. adv = 1;
  100. else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
  101. adv = 2;
  102. else
  103. adv = 3;
  104. if (left < adv) {
  105. expr = 1;
  106. break;
  107. }
  108. if (adv == 1) {
  109. if (strcmp(ap[0], "-o") == 0) {
  110. last_expr = expr;
  111. last_cmp = 0;
  112. } else if (strcmp(ap[0], "-a") == 0) {
  113. last_expr = expr;
  114. last_cmp = 1;
  115. } else {
  116. expr = 1;
  117. break;
  118. }
  119. }
  120. if (adv == 2) {
  121. if (strcmp(ap[0], "-z") == 0)
  122. expr = strlen(ap[1]) == 0 ? 1 : 0;
  123. else if (strcmp(ap[0], "-n") == 0)
  124. expr = strlen(ap[1]) == 0 ? 0 : 1;
  125. else {
  126. expr = 1;
  127. break;
  128. }
  129. if (last_cmp == 0)
  130. expr = last_expr || expr;
  131. else if (last_cmp == 1)
  132. expr = last_expr && expr;
  133. last_cmp = -1;
  134. }
  135. if (adv == 3) {
  136. if (strcmp(ap[1], "=") == 0)
  137. expr = strcmp(ap[0], ap[2]) == 0;
  138. else if (strcmp(ap[1], "!=") == 0)
  139. expr = strcmp(ap[0], ap[2]) != 0;
  140. else if (strcmp(ap[1], ">") == 0)
  141. expr = strcmp(ap[0], ap[2]) > 0;
  142. else if (strcmp(ap[1], "<") == 0)
  143. expr = strcmp(ap[0], ap[2]) < 0;
  144. else if (strcmp(ap[1], "-eq") == 0)
  145. expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
  146. else if (strcmp(ap[1], "-ne") == 0)
  147. expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
  148. else if (strcmp(ap[1], "-lt") == 0)
  149. expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
  150. else if (strcmp(ap[1], "-le") == 0)
  151. expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
  152. else if (strcmp(ap[1], "-gt") == 0)
  153. expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
  154. else if (strcmp(ap[1], "-ge") == 0)
  155. expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
  156. else {
  157. expr = 1;
  158. break;
  159. }
  160. if (last_cmp == 0)
  161. expr = last_expr || expr;
  162. else if (last_cmp == 1)
  163. expr = last_expr && expr;
  164. last_cmp = -1;
  165. }
  166. ap += adv; left -= adv;
  167. }
  168. if (neg)
  169. expr = !expr;
  170. expr = !expr;
  171. #if 0
  172. printf(": returns %d\n", expr);
  173. #endif
  174. return expr;
  175. }
  176. U_BOOT_CMD(
  177. test, CFG_MAXARGS, 1, do_test,
  178. "test - minimal test like /bin/sh\n",
  179. "[args..]\n"
  180. " - test functionality\n"
  181. );
  182. int
  183. do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  184. {
  185. int r;
  186. r = 0;
  187. if (argc > 1)
  188. r = simple_strtoul(argv[1], NULL, 10);
  189. return -r - 2;
  190. }
  191. U_BOOT_CMD(
  192. exit, 2, 1, do_exit,
  193. "exit - exit script\n",
  194. " - exit functionality\n"
  195. );
  196. #endif
  197. /*
  198. * Use puts() instead of printf() to avoid printf buffer overflow
  199. * for long help messages
  200. */
  201. int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  202. {
  203. int i;
  204. int rcode = 0;
  205. if (argc == 1) { /*show list of commands */
  206. int cmd_items = &__u_boot_cmd_end -
  207. &__u_boot_cmd_start; /* pointer arith! */
  208. cmd_tbl_t *cmd_array[cmd_items];
  209. int i, j, swaps;
  210. /* Make array of commands from .uboot_cmd section */
  211. cmdtp = &__u_boot_cmd_start;
  212. for (i = 0; i < cmd_items; i++) {
  213. cmd_array[i] = cmdtp++;
  214. }
  215. /* Sort command list (trivial bubble sort) */
  216. for (i = cmd_items - 1; i > 0; --i) {
  217. swaps = 0;
  218. for (j = 0; j < i; ++j) {
  219. if (strcmp (cmd_array[j]->name,
  220. cmd_array[j + 1]->name) > 0) {
  221. cmd_tbl_t *tmp;
  222. tmp = cmd_array[j];
  223. cmd_array[j] = cmd_array[j + 1];
  224. cmd_array[j + 1] = tmp;
  225. ++swaps;
  226. }
  227. }
  228. if (!swaps)
  229. break;
  230. }
  231. /* print short help (usage) */
  232. for (i = 0; i < cmd_items; i++) {
  233. const char *usage = cmd_array[i]->usage;
  234. /* allow user abort */
  235. if (ctrlc ())
  236. return 1;
  237. if (usage == NULL)
  238. continue;
  239. puts (usage);
  240. }
  241. return 0;
  242. }
  243. /*
  244. * command help (long version)
  245. */
  246. for (i = 1; i < argc; ++i) {
  247. if ((cmdtp = find_cmd (argv[i])) != NULL) {
  248. #ifdef CFG_LONGHELP
  249. /* found - print (long) help info */
  250. puts (cmdtp->name);
  251. putc (' ');
  252. if (cmdtp->help) {
  253. puts (cmdtp->help);
  254. } else {
  255. puts ("- No help available.\n");
  256. rcode = 1;
  257. }
  258. putc ('\n');
  259. #else /* no long help available */
  260. if (cmdtp->usage)
  261. puts (cmdtp->usage);
  262. #endif /* CFG_LONGHELP */
  263. } else {
  264. printf ("Unknown command '%s' - try 'help'"
  265. " without arguments for list of all"
  266. " known commands\n\n", argv[i]
  267. );
  268. rcode = 1;
  269. }
  270. }
  271. return rcode;
  272. }
  273. U_BOOT_CMD(
  274. help, CFG_MAXARGS, 1, do_help,
  275. "help - print online help\n",
  276. "[command ...]\n"
  277. " - show help information (for 'command')\n"
  278. "'help' prints online help for the monitor commands.\n\n"
  279. "Without arguments, it prints a short usage message for all commands.\n\n"
  280. "To get detailed help information for specific commands you can type\n"
  281. "'help' with one or more command names as arguments.\n"
  282. );
  283. /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
  284. #ifdef CFG_LONGHELP
  285. cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
  286. "?", CFG_MAXARGS, 1, do_help,
  287. "? - alias for 'help'\n",
  288. NULL
  289. };
  290. #else
  291. cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
  292. "?", CFG_MAXARGS, 1, do_help,
  293. "? - alias for 'help'\n"
  294. };
  295. #endif /* CFG_LONGHELP */
  296. /***************************************************************************
  297. * find command table entry for a command
  298. */
  299. cmd_tbl_t *find_cmd (const char *cmd)
  300. {
  301. cmd_tbl_t *cmdtp;
  302. cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
  303. const char *p;
  304. int len;
  305. int n_found = 0;
  306. /*
  307. * Some commands allow length modifiers (like "cp.b");
  308. * compare command name only until first dot.
  309. */
  310. len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
  311. for (cmdtp = &__u_boot_cmd_start;
  312. cmdtp != &__u_boot_cmd_end;
  313. cmdtp++) {
  314. if (strncmp (cmd, cmdtp->name, len) == 0) {
  315. if (len == strlen (cmdtp->name))
  316. return cmdtp; /* full match */
  317. cmdtp_temp = cmdtp; /* abbreviated command ? */
  318. n_found++;
  319. }
  320. }
  321. if (n_found == 1) { /* exactly one match */
  322. return cmdtp_temp;
  323. }
  324. return NULL; /* not found or ambiguous command */
  325. }
  326. #ifdef CONFIG_AUTO_COMPLETE
  327. int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
  328. {
  329. static char tmp_buf[512];
  330. int space;
  331. space = last_char == '\0' || last_char == ' ' || last_char == '\t';
  332. if (space && argc == 1)
  333. return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
  334. if (!space && argc == 2)
  335. return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
  336. return 0;
  337. }
  338. static void install_auto_complete_handler(const char *cmd,
  339. int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
  340. {
  341. cmd_tbl_t *cmdtp;
  342. cmdtp = find_cmd(cmd);
  343. if (cmdtp == NULL)
  344. return;
  345. cmdtp->complete = complete;
  346. }
  347. void install_auto_complete(void)
  348. {
  349. install_auto_complete_handler("printenv", var_complete);
  350. install_auto_complete_handler("setenv", var_complete);
  351. #if defined(CONFIG_CMD_RUN)
  352. install_auto_complete_handler("run", var_complete);
  353. #endif
  354. }
  355. /*************************************************************************************/
  356. static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
  357. {
  358. cmd_tbl_t *cmdtp;
  359. const char *p;
  360. int len, clen;
  361. int n_found = 0;
  362. const char *cmd;
  363. /* sanity? */
  364. if (maxv < 2)
  365. return -2;
  366. cmdv[0] = NULL;
  367. if (argc == 0) {
  368. /* output full list of commands */
  369. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  370. if (n_found >= maxv - 2) {
  371. cmdv[n_found++] = "...";
  372. break;
  373. }
  374. cmdv[n_found++] = cmdtp->name;
  375. }
  376. cmdv[n_found] = NULL;
  377. return n_found;
  378. }
  379. /* more than one arg or one but the start of the next */
  380. if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
  381. cmdtp = find_cmd(argv[0]);
  382. if (cmdtp == NULL || cmdtp->complete == NULL) {
  383. cmdv[0] = NULL;
  384. return 0;
  385. }
  386. return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
  387. }
  388. cmd = argv[0];
  389. /*
  390. * Some commands allow length modifiers (like "cp.b");
  391. * compare command name only until first dot.
  392. */
  393. p = strchr(cmd, '.');
  394. if (p == NULL)
  395. len = strlen(cmd);
  396. else
  397. len = p - cmd;
  398. /* return the partial matches */
  399. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  400. clen = strlen(cmdtp->name);
  401. if (clen < len)
  402. continue;
  403. if (memcmp(cmd, cmdtp->name, len) != 0)
  404. continue;
  405. /* too many! */
  406. if (n_found >= maxv - 2) {
  407. cmdv[n_found++] = "...";
  408. break;
  409. }
  410. cmdv[n_found++] = cmdtp->name;
  411. }
  412. cmdv[n_found] = NULL;
  413. return n_found;
  414. }
  415. static int make_argv(char *s, int argvsz, char *argv[])
  416. {
  417. int argc = 0;
  418. /* split into argv */
  419. while (argc < argvsz - 1) {
  420. /* skip any white space */
  421. while ((*s == ' ') || (*s == '\t'))
  422. ++s;
  423. if (*s == '\0') /* end of s, no more args */
  424. break;
  425. argv[argc++] = s; /* begin of argument string */
  426. /* find end of string */
  427. while (*s && (*s != ' ') && (*s != '\t'))
  428. ++s;
  429. if (*s == '\0') /* end of s, no more args */
  430. break;
  431. *s++ = '\0'; /* terminate current arg */
  432. }
  433. argv[argc] = NULL;
  434. return argc;
  435. }
  436. static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
  437. {
  438. int ll = leader != NULL ? strlen(leader) : 0;
  439. int sl = sep != NULL ? strlen(sep) : 0;
  440. int len, i;
  441. if (banner) {
  442. puts("\n");
  443. puts(banner);
  444. }
  445. i = linemax; /* force leader and newline */
  446. while (*argv != NULL) {
  447. len = strlen(*argv) + sl;
  448. if (i + len >= linemax) {
  449. puts("\n");
  450. if (leader)
  451. puts(leader);
  452. i = ll - sl;
  453. } else if (sep)
  454. puts(sep);
  455. puts(*argv++);
  456. i += len;
  457. }
  458. printf("\n");
  459. }
  460. static int find_common_prefix(char *argv[])
  461. {
  462. int i, len;
  463. char *anchor, *s, *t;
  464. if (*argv == NULL)
  465. return 0;
  466. /* begin with max */
  467. anchor = *argv++;
  468. len = strlen(anchor);
  469. while ((t = *argv++) != NULL) {
  470. s = anchor;
  471. for (i = 0; i < len; i++, t++, s++) {
  472. if (*t != *s)
  473. break;
  474. }
  475. len = s - anchor;
  476. }
  477. return len;
  478. }
  479. static char tmp_buf[CFG_CBSIZE]; /* copy of console I/O buffer */
  480. int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
  481. {
  482. int n = *np, col = *colp;
  483. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  484. char *cmdv[20];
  485. char *s, *t;
  486. const char *sep;
  487. int i, j, k, len, seplen, argc;
  488. int cnt;
  489. char last_char;
  490. if (strcmp(prompt, CFG_PROMPT) != 0)
  491. return 0; /* not in normal console */
  492. cnt = strlen(buf);
  493. if (cnt >= 1)
  494. last_char = buf[cnt - 1];
  495. else
  496. last_char = '\0';
  497. /* copy to secondary buffer which will be affected */
  498. strcpy(tmp_buf, buf);
  499. /* separate into argv */
  500. argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
  501. /* do the completion and return the possible completions */
  502. i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
  503. /* no match; bell and out */
  504. if (i == 0) {
  505. if (argc > 1) /* allow tab for non command */
  506. return 0;
  507. putc('\a');
  508. return 1;
  509. }
  510. s = NULL;
  511. len = 0;
  512. sep = NULL;
  513. seplen = 0;
  514. if (i == 1) { /* one match; perfect */
  515. k = strlen(argv[argc - 1]);
  516. s = cmdv[0] + k;
  517. len = strlen(s);
  518. sep = " ";
  519. seplen = 1;
  520. } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
  521. k = strlen(argv[argc - 1]);
  522. j -= k;
  523. if (j > 0) {
  524. s = cmdv[0] + k;
  525. len = j;
  526. }
  527. }
  528. if (s != NULL) {
  529. k = len + seplen;
  530. /* make sure it fits */
  531. if (n + k >= CFG_CBSIZE - 2) {
  532. putc('\a');
  533. return 1;
  534. }
  535. t = buf + cnt;
  536. for (i = 0; i < len; i++)
  537. *t++ = *s++;
  538. if (sep != NULL)
  539. for (i = 0; i < seplen; i++)
  540. *t++ = sep[i];
  541. *t = '\0';
  542. n += k;
  543. col += k;
  544. puts(t - k);
  545. if (sep == NULL)
  546. putc('\a');
  547. *np = n;
  548. *colp = col;
  549. } else {
  550. print_argv(NULL, " ", " ", 78, cmdv);
  551. puts(prompt);
  552. puts(buf);
  553. }
  554. return 1;
  555. }
  556. #endif