command.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. /*
  68. * Use puts() instead of printf() to avoid printf buffer overflow
  69. * for long help messages
  70. */
  71. int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  72. {
  73. int i;
  74. int rcode = 0;
  75. if (argc == 1) { /*show list of commands */
  76. int cmd_items = &__u_boot_cmd_end -
  77. &__u_boot_cmd_start; /* pointer arith! */
  78. cmd_tbl_t *cmd_array[cmd_items];
  79. int i, j, swaps;
  80. /* Make array of commands from .uboot_cmd section */
  81. cmdtp = &__u_boot_cmd_start;
  82. for (i = 0; i < cmd_items; i++) {
  83. cmd_array[i] = cmdtp++;
  84. }
  85. /* Sort command list (trivial bubble sort) */
  86. for (i = cmd_items - 1; i > 0; --i) {
  87. swaps = 0;
  88. for (j = 0; j < i; ++j) {
  89. if (strcmp (cmd_array[j]->name,
  90. cmd_array[j + 1]->name) > 0) {
  91. cmd_tbl_t *tmp;
  92. tmp = cmd_array[j];
  93. cmd_array[j] = cmd_array[j + 1];
  94. cmd_array[j + 1] = tmp;
  95. ++swaps;
  96. }
  97. }
  98. if (!swaps)
  99. break;
  100. }
  101. /* print short help (usage) */
  102. for (i = 0; i < cmd_items; i++) {
  103. const char *usage = cmd_array[i]->usage;
  104. /* allow user abort */
  105. if (ctrlc ())
  106. return 1;
  107. if (usage == NULL)
  108. continue;
  109. puts (usage);
  110. }
  111. return 0;
  112. }
  113. /*
  114. * command help (long version)
  115. */
  116. for (i = 1; i < argc; ++i) {
  117. if ((cmdtp = find_cmd (argv[i])) != NULL) {
  118. #ifdef CFG_LONGHELP
  119. /* found - print (long) help info */
  120. puts (cmdtp->name);
  121. putc (' ');
  122. if (cmdtp->help) {
  123. puts (cmdtp->help);
  124. } else {
  125. puts ("- No help available.\n");
  126. rcode = 1;
  127. }
  128. putc ('\n');
  129. #else /* no long help available */
  130. if (cmdtp->usage)
  131. puts (cmdtp->usage);
  132. #endif /* CFG_LONGHELP */
  133. } else {
  134. printf ("Unknown command '%s' - try 'help'"
  135. " without arguments for list of all"
  136. " known commands\n\n", argv[i]
  137. );
  138. rcode = 1;
  139. }
  140. }
  141. return rcode;
  142. }
  143. U_BOOT_CMD(
  144. help, CFG_MAXARGS, 1, do_help,
  145. "help - print online help\n",
  146. "[command ...]\n"
  147. " - show help information (for 'command')\n"
  148. "'help' prints online help for the monitor commands.\n\n"
  149. "Without arguments, it prints a short usage message for all commands.\n\n"
  150. "To get detailed help information for specific commands you can type\n"
  151. "'help' with one or more command names as arguments.\n"
  152. );
  153. /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
  154. #ifdef CFG_LONGHELP
  155. cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
  156. "?", CFG_MAXARGS, 1, do_help,
  157. "? - alias for 'help'\n",
  158. NULL
  159. };
  160. #else
  161. cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
  162. "?", CFG_MAXARGS, 1, do_help,
  163. "? - alias for 'help'\n"
  164. };
  165. #endif /* CFG_LONGHELP */
  166. /***************************************************************************
  167. * find command table entry for a command
  168. */
  169. cmd_tbl_t *find_cmd (const char *cmd)
  170. {
  171. cmd_tbl_t *cmdtp;
  172. cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
  173. const char *p;
  174. int len;
  175. int n_found = 0;
  176. /*
  177. * Some commands allow length modifiers (like "cp.b");
  178. * compare command name only until first dot.
  179. */
  180. len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
  181. for (cmdtp = &__u_boot_cmd_start;
  182. cmdtp != &__u_boot_cmd_end;
  183. cmdtp++) {
  184. if (strncmp (cmd, cmdtp->name, len) == 0) {
  185. if (len == strlen (cmdtp->name))
  186. return cmdtp; /* full match */
  187. cmdtp_temp = cmdtp; /* abbreviated command ? */
  188. n_found++;
  189. }
  190. }
  191. if (n_found == 1) { /* exactly one match */
  192. return cmdtp_temp;
  193. }
  194. return NULL; /* not found or ambiguous command */
  195. }
  196. #ifdef CONFIG_AUTO_COMPLETE
  197. int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
  198. {
  199. static char tmp_buf[512];
  200. int space;
  201. space = last_char == '\0' || last_char == ' ' || last_char == '\t';
  202. if (space && argc == 1)
  203. return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
  204. if (!space && argc == 2)
  205. return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
  206. return 0;
  207. }
  208. static void install_auto_complete_handler(const char *cmd,
  209. int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
  210. {
  211. cmd_tbl_t *cmdtp;
  212. cmdtp = find_cmd(cmd);
  213. if (cmdtp == NULL)
  214. return;
  215. cmdtp->complete = complete;
  216. }
  217. void install_auto_complete(void)
  218. {
  219. install_auto_complete_handler("printenv", var_complete);
  220. install_auto_complete_handler("setenv", var_complete);
  221. #if (CONFIG_COMMANDS & CFG_CMD_RUN)
  222. install_auto_complete_handler("run", var_complete);
  223. #endif
  224. }
  225. /*************************************************************************************/
  226. static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
  227. {
  228. cmd_tbl_t *cmdtp;
  229. const char *p;
  230. int len, clen;
  231. int n_found = 0;
  232. const char *cmd;
  233. /* sanity? */
  234. if (maxv < 2)
  235. return -2;
  236. cmdv[0] = NULL;
  237. if (argc == 0) {
  238. /* output full list of commands */
  239. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  240. if (n_found >= maxv - 2) {
  241. cmdv[n_found++] = "...";
  242. break;
  243. }
  244. cmdv[n_found++] = cmdtp->name;
  245. }
  246. cmdv[n_found] = NULL;
  247. return n_found;
  248. }
  249. /* more than one arg or one but the start of the next */
  250. if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
  251. cmdtp = find_cmd(argv[0]);
  252. if (cmdtp == NULL || cmdtp->complete == NULL) {
  253. cmdv[0] = NULL;
  254. return 0;
  255. }
  256. return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
  257. }
  258. cmd = argv[0];
  259. /*
  260. * Some commands allow length modifiers (like "cp.b");
  261. * compare command name only until first dot.
  262. */
  263. p = strchr(cmd, '.');
  264. if (p == NULL)
  265. len = strlen(cmd);
  266. else
  267. len = p - cmd;
  268. /* return the partial matches */
  269. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  270. clen = strlen(cmdtp->name);
  271. if (clen < len)
  272. continue;
  273. if (memcmp(cmd, cmdtp->name, len) != 0)
  274. continue;
  275. /* too many! */
  276. if (n_found >= maxv - 2) {
  277. cmdv[n_found++] = "...";
  278. break;
  279. }
  280. cmdv[n_found++] = cmdtp->name;
  281. }
  282. cmdv[n_found] = NULL;
  283. return n_found;
  284. }
  285. static int make_argv(char *s, int argvsz, char *argv[])
  286. {
  287. int argc = 0;
  288. /* split into argv */
  289. while (argc < argvsz - 1) {
  290. /* skip any white space */
  291. while ((*s == ' ') || (*s == '\t'))
  292. ++s;
  293. if (*s == '\0') /* end of s, no more args */
  294. break;
  295. argv[argc++] = s; /* begin of argument string */
  296. /* find end of string */
  297. while (*s && (*s != ' ') && (*s != '\t'))
  298. ++s;
  299. if (*s == '\0') /* end of s, no more args */
  300. break;
  301. *s++ = '\0'; /* terminate current arg */
  302. }
  303. argv[argc] = NULL;
  304. return argc;
  305. }
  306. static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
  307. {
  308. int ll = leader != NULL ? strlen(leader) : 0;
  309. int sl = sep != NULL ? strlen(sep) : 0;
  310. int len, i;
  311. if (banner) {
  312. puts("\n");
  313. puts(banner);
  314. }
  315. i = linemax; /* force leader and newline */
  316. while (*argv != NULL) {
  317. len = strlen(*argv) + sl;
  318. if (i + len >= linemax) {
  319. puts("\n");
  320. if (leader)
  321. puts(leader);
  322. i = ll - sl;
  323. } else if (sep)
  324. puts(sep);
  325. puts(*argv++);
  326. i += len;
  327. }
  328. printf("\n");
  329. }
  330. static int find_common_prefix(char *argv[])
  331. {
  332. int i, len;
  333. char *anchor, *s, *t;
  334. if (*argv == NULL)
  335. return 0;
  336. /* begin with max */
  337. anchor = *argv++;
  338. len = strlen(anchor);
  339. while ((t = *argv++) != NULL) {
  340. s = anchor;
  341. for (i = 0; i < len; i++, t++, s++) {
  342. if (*t != *s)
  343. break;
  344. }
  345. len = s - anchor;
  346. }
  347. return len;
  348. }
  349. static char tmp_buf[CFG_CBSIZE]; /* copy of console I/O buffer */
  350. int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
  351. {
  352. int n = *np, col = *colp;
  353. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  354. char *cmdv[20];
  355. char *s, *t;
  356. const char *sep;
  357. int i, j, k, len, seplen, argc;
  358. int cnt;
  359. char last_char;
  360. if (strcmp(prompt, CFG_PROMPT) != 0)
  361. return 0; /* not in normal console */
  362. cnt = strlen(buf);
  363. if (cnt >= 1)
  364. last_char = buf[cnt - 1];
  365. else
  366. last_char = '\0';
  367. /* copy to secondary buffer which will be affected */
  368. strcpy(tmp_buf, buf);
  369. /* separate into argv */
  370. argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
  371. /* do the completion and return the possible completions */
  372. i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
  373. /* no match; bell and out */
  374. if (i == 0) {
  375. if (argc > 1) /* allow tab for non command */
  376. return 0;
  377. putc('\a');
  378. return 1;
  379. }
  380. s = NULL;
  381. len = 0;
  382. sep = NULL;
  383. seplen = 0;
  384. if (i == 1) { /* one match; perfect */
  385. k = strlen(argv[argc - 1]);
  386. s = cmdv[0] + k;
  387. len = strlen(s);
  388. sep = " ";
  389. seplen = 1;
  390. } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
  391. k = strlen(argv[argc - 1]);
  392. j -= k;
  393. if (j > 0) {
  394. s = cmdv[0] + k;
  395. len = j;
  396. }
  397. }
  398. if (s != NULL) {
  399. k = len + seplen;
  400. /* make sure it fits */
  401. if (n + k >= CFG_CBSIZE - 2) {
  402. putc('\a');
  403. return 1;
  404. }
  405. t = buf + cnt;
  406. for (i = 0; i < len; i++)
  407. *t++ = *s++;
  408. if (sep != NULL)
  409. for (i = 0; i < seplen; i++)
  410. *t++ = sep[i];
  411. *t = '\0';
  412. n += k;
  413. col += k;
  414. puts(t - k);
  415. if (sep == NULL)
  416. putc('\a');
  417. *np = n;
  418. *colp = col;
  419. } else {
  420. print_argv(NULL, " ", " ", 78, cmdv);
  421. puts(prompt);
  422. puts(buf);
  423. }
  424. return 1;
  425. }
  426. #endif