xref: /openbmc/u-boot/common/command.c (revision 8bde7f77)
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 /*
25  *  Command Processor Table
26  */
27 
28 #include <common.h>
29 #include <command.h>
30 
31 int
32 do_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 
39 int
40 do_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(' ');
49 		while ((c = *p++) != '\0') {
50 			if (c == '\\' && *p == 'c') {
51 				putnl = 0;
52 				p++;
53 			} else {
54 				putc(c);
55 			}
56 		}
57 	}
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  */
68 int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
69 {
70 	int i;
71 	int rcode = 0;
72 
73 	if (argc == 1) {	/*show list of commands */
74 
75 		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++) {
107 			/* allow user abort */
108 			if (ctrlc ())
109 				return 1;
110 			if (cmdtp->usage == NULL)
111 				continue;
112 			puts (cmdtp->usage);
113 		}
114 		return 0;
115 	}
116 	/*
117 	 * command help (long version)
118 	 */
119 	for (i = 1; i < argc; ++i) {
120 		if ((cmdtp = find_cmd (argv[i])) != NULL) {
121 #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 */
136 		} else {
137 			printf ("Unknown command '%s' - try 'help'"
138 				" without arguments for list of all"
139 				" known commands\n\n", argv[i]
140 					);
141 			rcode = 1;
142 		}
143 	}
144 	return rcode;
145 }
146 
147 
148 cmd_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 
159 cmd_tbl_t U_BOOT_CMD(QUES) = MK_CMD_ENTRY(
160 	"?",	CFG_MAXARGS,	1,	do_help,
161  	"?       - alias for 'help'\n",
162 	NULL
163 );
164 
165 cmd_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 
171 cmd_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 
178 /***************************************************************************
179  * find command table entry for a command
180  */
181 cmd_tbl_t *find_cmd (const char *cmd)
182 {
183 	cmd_tbl_t *cmdtp;
184 
185 	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)))
191 			return cmdtp;
192 		else if (strncmp (cmd, cmdtp->name, strlen (cmd)) == 0) {
193 			cmdtp_temp = cmdtp;
194 			one_cmd_name++;
195 		} else;
196 	}
197 	if (one_cmd_name == 1)
198 		return cmdtp_temp;
199 
200 	return NULL;	/* not found || one_cmd_name >2 */
201 }
202