command.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /*
  101. * Some commands allow length modifiers (like "cp.b");
  102. * compare command name only until first dot.
  103. */
  104. len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
  105. for (cmdtp = table;
  106. cmdtp != table + table_len;
  107. cmdtp++) {
  108. if (strncmp (cmd, cmdtp->name, len) == 0) {
  109. if (len == strlen (cmdtp->name))
  110. return cmdtp; /* full match */
  111. cmdtp_temp = cmdtp; /* abbreviated command ? */
  112. n_found++;
  113. }
  114. }
  115. if (n_found == 1) { /* exactly one match */
  116. return cmdtp_temp;
  117. }
  118. return NULL; /* not found or ambiguous command */
  119. }
  120. cmd_tbl_t *find_cmd (const char *cmd)
  121. {
  122. int len = &__u_boot_cmd_end - &__u_boot_cmd_start;
  123. return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);
  124. }
  125. int cmd_usage(cmd_tbl_t *cmdtp)
  126. {
  127. printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
  128. #ifdef CONFIG_SYS_LONGHELP
  129. printf("Usage:\n%s ", cmdtp->name);
  130. if (!cmdtp->help) {
  131. puts ("- No additional help available.\n");
  132. return 1;
  133. }
  134. puts (cmdtp->help);
  135. putc ('\n');
  136. #endif /* CONFIG_SYS_LONGHELP */
  137. return 1;
  138. }
  139. #ifdef CONFIG_AUTO_COMPLETE
  140. int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
  141. {
  142. #if 0 /* need to reimplement */
  143. static char tmp_buf[512];
  144. int space;
  145. space = last_char == '\0' || last_char == ' ' || last_char == '\t';
  146. if (space && argc == 1)
  147. return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
  148. if (!space && argc == 2)
  149. return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
  150. #endif
  151. return 0;
  152. }
  153. static void install_auto_complete_handler(const char *cmd,
  154. int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]))
  155. {
  156. cmd_tbl_t *cmdtp;
  157. cmdtp = find_cmd(cmd);
  158. if (cmdtp == NULL)
  159. return;
  160. cmdtp->complete = complete;
  161. }
  162. void install_auto_complete(void)
  163. {
  164. #if defined(CONFIG_CMD_EDITENV)
  165. install_auto_complete_handler("editenv", var_complete);
  166. #endif
  167. install_auto_complete_handler("printenv", var_complete);
  168. install_auto_complete_handler("setenv", var_complete);
  169. #if defined(CONFIG_CMD_RUN)
  170. install_auto_complete_handler("run", var_complete);
  171. #endif
  172. }
  173. /*************************************************************************************/
  174. static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
  175. {
  176. cmd_tbl_t *cmdtp;
  177. const char *p;
  178. int len, clen;
  179. int n_found = 0;
  180. const char *cmd;
  181. /* sanity? */
  182. if (maxv < 2)
  183. return -2;
  184. cmdv[0] = NULL;
  185. if (argc == 0) {
  186. /* output full list of commands */
  187. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  188. if (n_found >= maxv - 2) {
  189. cmdv[n_found++] = "...";
  190. break;
  191. }
  192. cmdv[n_found++] = cmdtp->name;
  193. }
  194. cmdv[n_found] = NULL;
  195. return n_found;
  196. }
  197. /* more than one arg or one but the start of the next */
  198. if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
  199. cmdtp = find_cmd(argv[0]);
  200. if (cmdtp == NULL || cmdtp->complete == NULL) {
  201. cmdv[0] = NULL;
  202. return 0;
  203. }
  204. return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
  205. }
  206. cmd = argv[0];
  207. /*
  208. * Some commands allow length modifiers (like "cp.b");
  209. * compare command name only until first dot.
  210. */
  211. p = strchr(cmd, '.');
  212. if (p == NULL)
  213. len = strlen(cmd);
  214. else
  215. len = p - cmd;
  216. /* return the partial matches */
  217. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  218. clen = strlen(cmdtp->name);
  219. if (clen < len)
  220. continue;
  221. if (memcmp(cmd, cmdtp->name, len) != 0)
  222. continue;
  223. /* too many! */
  224. if (n_found >= maxv - 2) {
  225. cmdv[n_found++] = "...";
  226. break;
  227. }
  228. cmdv[n_found++] = cmdtp->name;
  229. }
  230. cmdv[n_found] = NULL;
  231. return n_found;
  232. }
  233. static int make_argv(char *s, int argvsz, char *argv[])
  234. {
  235. int argc = 0;
  236. /* split into argv */
  237. while (argc < argvsz - 1) {
  238. /* skip any white space */
  239. while ((*s == ' ') || (*s == '\t'))
  240. ++s;
  241. if (*s == '\0') /* end of s, no more args */
  242. break;
  243. argv[argc++] = s; /* begin of argument string */
  244. /* find end of string */
  245. while (*s && (*s != ' ') && (*s != '\t'))
  246. ++s;
  247. if (*s == '\0') /* end of s, no more args */
  248. break;
  249. *s++ = '\0'; /* terminate current arg */
  250. }
  251. argv[argc] = NULL;
  252. return argc;
  253. }
  254. static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
  255. {
  256. int ll = leader != NULL ? strlen(leader) : 0;
  257. int sl = sep != NULL ? strlen(sep) : 0;
  258. int len, i;
  259. if (banner) {
  260. puts("\n");
  261. puts(banner);
  262. }
  263. i = linemax; /* force leader and newline */
  264. while (*argv != NULL) {
  265. len = strlen(*argv) + sl;
  266. if (i + len >= linemax) {
  267. puts("\n");
  268. if (leader)
  269. puts(leader);
  270. i = ll - sl;
  271. } else if (sep)
  272. puts(sep);
  273. puts(*argv++);
  274. i += len;
  275. }
  276. printf("\n");
  277. }
  278. static int find_common_prefix(char * const argv[])
  279. {
  280. int i, len;
  281. char *anchor, *s, *t;
  282. if (*argv == NULL)
  283. return 0;
  284. /* begin with max */
  285. anchor = *argv++;
  286. len = strlen(anchor);
  287. while ((t = *argv++) != NULL) {
  288. s = anchor;
  289. for (i = 0; i < len; i++, t++, s++) {
  290. if (*t != *s)
  291. break;
  292. }
  293. len = s - anchor;
  294. }
  295. return len;
  296. }
  297. static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
  298. int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
  299. {
  300. int n = *np, col = *colp;
  301. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  302. char *cmdv[20];
  303. char *s, *t;
  304. const char *sep;
  305. int i, j, k, len, seplen, argc;
  306. int cnt;
  307. char last_char;
  308. if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
  309. return 0; /* not in normal console */
  310. cnt = strlen(buf);
  311. if (cnt >= 1)
  312. last_char = buf[cnt - 1];
  313. else
  314. last_char = '\0';
  315. /* copy to secondary buffer which will be affected */
  316. strcpy(tmp_buf, buf);
  317. /* separate into argv */
  318. argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
  319. /* do the completion and return the possible completions */
  320. i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
  321. /* no match; bell and out */
  322. if (i == 0) {
  323. if (argc > 1) /* allow tab for non command */
  324. return 0;
  325. putc('\a');
  326. return 1;
  327. }
  328. s = NULL;
  329. len = 0;
  330. sep = NULL;
  331. seplen = 0;
  332. if (i == 1) { /* one match; perfect */
  333. k = strlen(argv[argc - 1]);
  334. s = cmdv[0] + k;
  335. len = strlen(s);
  336. sep = " ";
  337. seplen = 1;
  338. } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
  339. k = strlen(argv[argc - 1]);
  340. j -= k;
  341. if (j > 0) {
  342. s = cmdv[0] + k;
  343. len = j;
  344. }
  345. }
  346. if (s != NULL) {
  347. k = len + seplen;
  348. /* make sure it fits */
  349. if (n + k >= CONFIG_SYS_CBSIZE - 2) {
  350. putc('\a');
  351. return 1;
  352. }
  353. t = buf + cnt;
  354. for (i = 0; i < len; i++)
  355. *t++ = *s++;
  356. if (sep != NULL)
  357. for (i = 0; i < seplen; i++)
  358. *t++ = sep[i];
  359. *t = '\0';
  360. n += k;
  361. col += k;
  362. puts(t - k);
  363. if (sep == NULL)
  364. putc('\a');
  365. *np = n;
  366. *colp = col;
  367. } else {
  368. print_argv(NULL, " ", " ", 78, cmdv);
  369. puts(prompt);
  370. puts(buf);
  371. }
  372. return 1;
  373. }
  374. #endif
  375. #ifdef CMD_DATA_SIZE
  376. int cmd_get_data_size(char* arg, int default_size)
  377. {
  378. /* Check for a size specification .b, .w or .l.
  379. */
  380. int len = strlen(arg);
  381. if (len > 2 && arg[len-2] == '.') {
  382. switch(arg[len-1]) {
  383. case 'b':
  384. return 1;
  385. case 'w':
  386. return 2;
  387. case 'l':
  388. return 4;
  389. case 's':
  390. return -2;
  391. default:
  392. return -1;
  393. }
  394. }
  395. return default_size;
  396. }
  397. #endif