command.c 14 KB

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