command.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. int
  36. do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  37. {
  38. int i, putnl = 1;
  39. for (i = 1; i < argc; i++) {
  40. char *p = argv[i], c;
  41. if (i > 1)
  42. putc(' ');
  43. while ((c = *p++) != '\0') {
  44. if (c == '\\' && *p == 'c') {
  45. putnl = 0;
  46. p++;
  47. } else {
  48. putc(c);
  49. }
  50. }
  51. }
  52. if (putnl)
  53. putc('\n');
  54. return 0;
  55. }
  56. /*
  57. * Use puts() instead of printf() to avoid printf buffer overflow
  58. * for long help messages
  59. */
  60. int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  61. {
  62. int i;
  63. int rcode = 0;
  64. if (argc == 1) { /*show list of commands */
  65. int cmd_items = &__u_boot_cmd_end -
  66. &__u_boot_cmd_start; /* pointer arith! */
  67. cmd_tbl_t *cmd_array[cmd_items];
  68. int i, j, swaps;
  69. /* Make array of commands from .uboot_cmd section */
  70. cmdtp = &__u_boot_cmd_start;
  71. for (i = 0; i < cmd_items; i++) {
  72. cmd_array[i] = cmdtp++;
  73. }
  74. /* Sort command list (trivial bubble sort) */
  75. for (i = cmd_items - 1; i > 0; --i) {
  76. swaps = 0;
  77. for (j = 0; j < i; ++j) {
  78. if (strcmp (cmd_array[j]->name,
  79. cmd_array[j + 1]->name) > 0) {
  80. cmd_tbl_t *tmp;
  81. tmp = cmd_array[j];
  82. cmd_array[j] = cmd_array[j + 1];
  83. cmd_array[j + 1] = tmp;
  84. ++swaps;
  85. }
  86. }
  87. if (!swaps)
  88. break;
  89. }
  90. /* print short help (usage) */
  91. for (i = 0; i < cmd_items; i++) {
  92. const char *usage = cmd_array[i]->usage;
  93. /* allow user abort */
  94. if (ctrlc ())
  95. return 1;
  96. if (usage == NULL)
  97. continue;
  98. puts (usage);
  99. }
  100. return 0;
  101. }
  102. /*
  103. * command help (long version)
  104. */
  105. for (i = 1; i < argc; ++i) {
  106. if ((cmdtp = find_cmd (argv[i])) != NULL) {
  107. #ifdef CFG_LONGHELP
  108. /* found - print (long) help info */
  109. puts (cmdtp->name);
  110. putc (' ');
  111. if (cmdtp->help) {
  112. puts (cmdtp->help);
  113. } else {
  114. puts ("- No help available.\n");
  115. rcode = 1;
  116. }
  117. putc ('\n');
  118. #else /* no long help available */
  119. if (cmdtp->usage)
  120. puts (cmdtp->usage);
  121. #endif /* CFG_LONGHELP */
  122. } else {
  123. printf ("Unknown command '%s' - try 'help'"
  124. " without arguments for list of all"
  125. " known commands\n\n", argv[i]
  126. );
  127. rcode = 1;
  128. }
  129. }
  130. return rcode;
  131. }
  132. cmd_tbl_t U_BOOT_CMD(HELP) = MK_CMD_ENTRY(
  133. "help", CFG_MAXARGS, 1, do_help,
  134. "help - print online help\n",
  135. "[command ...]\n"
  136. " - show help information (for 'command')\n"
  137. "'help' prints online help for the monitor commands.\n\n"
  138. "Without arguments, it prints a short usage message for all commands.\n\n"
  139. "To get detailed help information for specific commands you can type\n"
  140. "'help' with one or more command names as arguments.\n"
  141. );
  142. cmd_tbl_t U_BOOT_CMD(QUES) = MK_CMD_ENTRY(
  143. "?", CFG_MAXARGS, 1, do_help,
  144. "? - alias for 'help'\n",
  145. NULL
  146. );
  147. cmd_tbl_t U_BOOT_CMD(VERS) = MK_CMD_ENTRY(
  148. "version", 1, 1, do_version,
  149. "version - print monitor version\n",
  150. NULL
  151. );
  152. cmd_tbl_t U_BOOT_CMD(ECHO) = MK_CMD_ENTRY(
  153. "echo", CFG_MAXARGS, 1, do_echo,
  154. "echo - echo args to console\n",
  155. "[args..]\n"
  156. " - echo args to console; \\c suppresses newline\n"
  157. );
  158. /***************************************************************************
  159. * find command table entry for a command
  160. */
  161. cmd_tbl_t *find_cmd (const char *cmd)
  162. {
  163. cmd_tbl_t *cmdtp;
  164. cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
  165. const char *p;
  166. int len;
  167. int n_found = 0;
  168. /*
  169. * Some commands allow length modifiers (like "cp.b");
  170. * compare command name only until first dot.
  171. */
  172. len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
  173. for (cmdtp = &__u_boot_cmd_start;
  174. cmdtp != &__u_boot_cmd_end;
  175. cmdtp++) {
  176. if (strncmp (cmd, cmdtp->name, len) == 0) {
  177. if (len == strlen (cmdtp->name))
  178. return cmdtp; /* full match */
  179. cmdtp_temp = cmdtp; /* abbreviated command ? */
  180. n_found++;
  181. }
  182. }
  183. if (n_found == 1) { /* exactly one match */
  184. return cmdtp_temp;
  185. }
  186. return NULL; /* not found or ambiguous command */
  187. }