command.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }