command.c 11 KB

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