command.c 9.7 KB

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