blob: 607c74616aa457db639814982113bcea60111d02 [file] [log] [blame]
wdenk5dfa25f2002-10-19 19:42:10 +00001/*
wdenk8bde7f72003-06-27 21:31:46 +00002 * (C) Copyright 2000-2003
wdenk5dfa25f2002-10-19 19:42:10 +00003 * 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/*
25 * Command Processor Table
26 */
27
28#include <common.h>
29#include <command.h>
wdenk5dfa25f2002-10-19 19:42:10 +000030
31int
32do_version (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
33{
34 extern char version_string[];
35 printf ("\n%s\n", version_string);
36 return 0;
37}
38
39int
40do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
41{
42 int i, putnl = 1;
43
44 for (i = 1; i < argc; i++) {
45 char *p = argv[i], c;
46
47 if (i > 1)
48 putc(' ');
wdenk71f95112003-06-15 22:40:42 +000049 while ((c = *p++) != '\0') {
wdenk5dfa25f2002-10-19 19:42:10 +000050 if (c == '\\' && *p == 'c') {
51 putnl = 0;
52 p++;
wdenk71f95112003-06-15 22:40:42 +000053 } else {
wdenk5dfa25f2002-10-19 19:42:10 +000054 putc(c);
wdenk71f95112003-06-15 22:40:42 +000055 }
56 }
wdenk5dfa25f2002-10-19 19:42:10 +000057 }
58
59 if (putnl)
60 putc('\n');
61 return 0;
62}
63
64/*
65 * Use puts() instead of printf() to avoid printf buffer overflow
66 * for long help messages
67 */
wdenk8bde7f72003-06-27 21:31:46 +000068int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000069{
70 int i;
71 int rcode = 0;
72
wdenk8bde7f72003-06-27 21:31:46 +000073 if (argc == 1) { /*show list of commands */
wdenk5dfa25f2002-10-19 19:42:10 +000074
wdenk8bde7f72003-06-27 21:31:46 +000075 int cmd_items = (((int) &__u_boot_cmd_end) -
76 ((int) &__u_boot_cmd_start)) /
77 sizeof (*cmdtp);
78 int end_sort;
79 cmd_tbl_t *cmd_array[(cmd_items + 1)];
80 int i;
81
82 /* Make list of commands from .uboot_cmd section */
83 cmdtp = (cmd_tbl_t *) & __u_boot_cmd_start;
84 for (i = 1; i <= cmd_items; i++) {
85 cmd_array[i] = cmdtp;
86 cmdtp++;
87 }
88 /* Sort command list */
89 end_sort = 0;
90 for (i = 1; end_sort != 1 || i <= cmd_items - 1; i++) {
91 if (i == cmd_items) { /* Last command */
92 end_sort = 1;
93 i = 1;
94 }
95
96 if (strcmp (cmd_array[i]->name, cmd_array[i + 1]->name) > 0) {
97 end_sort = 0;
98 *cmd_array[0] = *cmd_array[i];
99 *cmd_array[i] = *cmd_array[i + 1];
100 *cmd_array[i + 1] = *cmd_array[0];
101 }
102 }
103
104 /* print short help (usage) */
105 for (cmdtp = (cmd_tbl_t *) & __u_boot_cmd_start;
106 cmdtp != (cmd_tbl_t *) & __u_boot_cmd_end; cmdtp++) {
wdenk5dfa25f2002-10-19 19:42:10 +0000107 /* allow user abort */
wdenk8bde7f72003-06-27 21:31:46 +0000108 if (ctrlc ())
wdenk5dfa25f2002-10-19 19:42:10 +0000109 return 1;
wdenk5dfa25f2002-10-19 19:42:10 +0000110 if (cmdtp->usage == NULL)
111 continue;
112 puts (cmdtp->usage);
113 }
wdenk5dfa25f2002-10-19 19:42:10 +0000114 return 0;
115 }
wdenk5dfa25f2002-10-19 19:42:10 +0000116 /*
117 * command help (long version)
118 */
wdenk8bde7f72003-06-27 21:31:46 +0000119 for (i = 1; i < argc; ++i) {
120 if ((cmdtp = find_cmd (argv[i])) != NULL) {
wdenk5dfa25f2002-10-19 19:42:10 +0000121#ifdef CFG_LONGHELP
122 /* found - print (long) help info */
123 puts (cmdtp->name);
124 putc (' ');
125 if (cmdtp->help) {
126 puts (cmdtp->help);
127 } else {
128 puts ("- No help available.\n");
129 rcode = 1;
130 }
131 putc ('\n');
132#else /* no long help available */
133 if (cmdtp->usage)
134 puts (cmdtp->usage);
135#endif /* CFG_LONGHELP */
wdenk71f95112003-06-15 22:40:42 +0000136 } else {
wdenk5dfa25f2002-10-19 19:42:10 +0000137 printf ("Unknown command '%s' - try 'help'"
138 " without arguments for list of all"
wdenk8bde7f72003-06-27 21:31:46 +0000139 " known commands\n\n", argv[i]
140 );
wdenk5dfa25f2002-10-19 19:42:10 +0000141 rcode = 1;
142 }
143 }
144 return rcode;
145}
146
wdenk8bde7f72003-06-27 21:31:46 +0000147
148cmd_tbl_t U_BOOT_CMD(HELP) = MK_CMD_ENTRY(
149 "help", CFG_MAXARGS, 1, do_help,
150 "help - print online help\n",
151 "[command ...]\n"
152 " - show help information (for 'command')\n"
153 "'help' prints online help for the monitor commands.\n\n"
154 "Without arguments, it prints a short usage message for all commands.\n\n"
155 "To get detailed help information for specific commands you can type\n"
156 "'help' with one or more command names as arguments.\n"
157);
158
159cmd_tbl_t U_BOOT_CMD(QUES) = MK_CMD_ENTRY(
160 "?", CFG_MAXARGS, 1, do_help,
161 "? - alias for 'help'\n",
162 NULL
163);
164
165cmd_tbl_t U_BOOT_CMD(VERS) = MK_CMD_ENTRY(
166 "version", 1, 1, do_version,
167 "version - print monitor version\n",
168 NULL
169);
170
171cmd_tbl_t U_BOOT_CMD(ECHO) = MK_CMD_ENTRY(
172 "echo", CFG_MAXARGS, 1, do_echo,
173 "echo - echo args to console\n",
174 "[args..]\n"
175 " - echo args to console; \\c suppresses newline\n"
176);
177
wdenk5dfa25f2002-10-19 19:42:10 +0000178/***************************************************************************
179 * find command table entry for a command
180 */
wdenk8bde7f72003-06-27 21:31:46 +0000181cmd_tbl_t *find_cmd (const char *cmd)
wdenk5dfa25f2002-10-19 19:42:10 +0000182{
183 cmd_tbl_t *cmdtp;
184
wdenk8bde7f72003-06-27 21:31:46 +0000185 cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
186 int one_cmd_name = 0;
187
188 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
189 if ((strncmp (cmd, cmdtp->name, strlen (cmd)) == 0) &&
190 (strlen (cmd) == strlen (cmdtp->name)))
wdenk5dfa25f2002-10-19 19:42:10 +0000191 return cmdtp;
wdenk8bde7f72003-06-27 21:31:46 +0000192 else if (strncmp (cmd, cmdtp->name, strlen (cmd)) == 0) {
193 cmdtp_temp = cmdtp;
194 one_cmd_name++;
195 } else;
wdenk5dfa25f2002-10-19 19:42:10 +0000196 }
wdenk8bde7f72003-06-27 21:31:46 +0000197 if (one_cmd_name == 1)
198 return cmdtp_temp;
199
200 return NULL; /* not found || one_cmd_name >2 */
wdenk5dfa25f2002-10-19 19:42:10 +0000201}