command.c 5.04 KB
Newer Older
wdenk's avatar
wdenk committed
1
/*
wdenk's avatar
wdenk committed
2
 * (C) Copyright 2000-2003
wdenk's avatar
wdenk committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 *  Command Processor Table
 */

#include <common.h>
#include <command.h>

int
do_version (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	extern char version_string[];
	printf ("\n%s\n", version_string);
	return 0;
}

39 40
U_BOOT_CMD(
	version,	1,		1,	do_version,
41 42 43 44
 	"version - print monitor version\n",
	NULL
);

wdenk's avatar
wdenk committed
45 46 47 48 49 50 51 52 53 54
int
do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int i, putnl = 1;

	for (i = 1; i < argc; i++) {
		char *p = argv[i], c;

		if (i > 1)
			putc(' ');
55
		while ((c = *p++) != '\0') {
wdenk's avatar
wdenk committed
56 57 58
			if (c == '\\' && *p == 'c') {
				putnl = 0;
				p++;
59
			} else {
wdenk's avatar
wdenk committed
60
				putc(c);
61 62
			}
		}
wdenk's avatar
wdenk committed
63 64 65 66 67 68 69
	}

	if (putnl)
		putc('\n');
	return 0;
}

70 71
U_BOOT_CMD(
	echo,	CFG_MAXARGS,	1,	do_echo,
72 73 74 75 76
 	"echo    - echo args to console\n",
 	"[args..]\n"
	"    - echo args to console; \\c suppresses newline\n"
);

wdenk's avatar
wdenk committed
77 78 79 80
/*
 * Use puts() instead of printf() to avoid printf buffer overflow
 * for long help messages
 */
wdenk's avatar
wdenk committed
81
int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenk's avatar
wdenk committed
82 83 84 85
{
	int i;
	int rcode = 0;

wdenk's avatar
wdenk committed
86 87
	if (argc == 1) {	/*show list of commands */

88 89 90 91 92 93 94 95 96
		int cmd_items = &__u_boot_cmd_end -
				&__u_boot_cmd_start;	/* pointer arith! */
		cmd_tbl_t *cmd_array[cmd_items];
		int i, j, swaps;

		/* Make array of commands from .uboot_cmd section */
		cmdtp = &__u_boot_cmd_start;
		for (i = 0; i < cmd_items; i++) {
			cmd_array[i] = cmdtp++;
wdenk's avatar
wdenk committed
97 98
		}

99 100 101 102 103 104 105 106 107 108 109 110
		/* Sort command list (trivial bubble sort) */
		for (i = cmd_items - 1; i > 0; --i) {
			swaps = 0;
			for (j = 0; j < i; ++j) {
				if (strcmp (cmd_array[j]->name,
					    cmd_array[j + 1]->name) > 0) {
					cmd_tbl_t *tmp;
					tmp = cmd_array[j];
					cmd_array[j] = cmd_array[j + 1];
					cmd_array[j + 1] = tmp;
					++swaps;
				}
wdenk's avatar
wdenk committed
111
			}
112 113
			if (!swaps)
				break;
wdenk's avatar
wdenk committed
114
		}
wdenk's avatar
wdenk committed
115

wdenk's avatar
wdenk committed
116
		/* print short help (usage) */
117 118 119
		for (i = 0; i < cmd_items; i++) {
			const char *usage = cmd_array[i]->usage;

wdenk's avatar
wdenk committed
120
			/* allow user abort */
wdenk's avatar
wdenk committed
121
			if (ctrlc ())
wdenk's avatar
wdenk committed
122
				return 1;
123
			if (usage == NULL)
wdenk's avatar
wdenk committed
124
				continue;
125
			puts (usage);
wdenk's avatar
wdenk committed
126 127 128 129 130 131
		}
		return 0;
	}
	/*
	 * command help (long version)
	 */
wdenk's avatar
wdenk committed
132 133
	for (i = 1; i < argc; ++i) {
		if ((cmdtp = find_cmd (argv[i])) != NULL) {
wdenk's avatar
wdenk committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#ifdef	CFG_LONGHELP
			/* found - print (long) help info */
			puts (cmdtp->name);
			putc (' ');
			if (cmdtp->help) {
				puts (cmdtp->help);
			} else {
				puts ("- No help available.\n");
				rcode = 1;
			}
			putc ('\n');
#else	/* no long help available */
			if (cmdtp->usage)
				puts (cmdtp->usage);
#endif	/* CFG_LONGHELP */
149
		} else {
wdenk's avatar
wdenk committed
150 151
			printf ("Unknown command '%s' - try 'help'"
				" without arguments for list of all"
wdenk's avatar
wdenk committed
152 153
				" known commands\n\n", argv[i]
					);
wdenk's avatar
wdenk committed
154 155 156 157 158 159
			rcode = 1;
		}
	}
	return rcode;
}

wdenk's avatar
wdenk committed
160

161 162
U_BOOT_CMD(
	help,	CFG_MAXARGS,	1,	do_help,
wdenk's avatar
wdenk committed
163 164 165 166 167 168 169 170 171
 	"help    - print online help\n",
 	"[command ...]\n"
 	"    - show help information (for 'command')\n"
 	"'help' prints online help for the monitor commands.\n\n"
 	"Without arguments, it prints a short usage message for all commands.\n\n"
 	"To get detailed help information for specific commands you can type\n"
  "'help' with one or more command names as arguments.\n"
);

172 173 174
/* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
#ifdef  CFG_LONGHELP
cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
wdenk's avatar
wdenk committed
175 176 177
	"?",	CFG_MAXARGS,	1,	do_help,
 	"?       - alias for 'help'\n",
	NULL
178 179 180 181 182 183 184
};
#else
cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
	"?",	CFG_MAXARGS,	1,	do_help,
 	"?       - alias for 'help'\n"
};
#endif /* CFG_LONGHELP */
wdenk's avatar
wdenk committed
185

wdenk's avatar
wdenk committed
186 187 188
/***************************************************************************
 * find command table entry for a command
 */
wdenk's avatar
wdenk committed
189
cmd_tbl_t *find_cmd (const char *cmd)
wdenk's avatar
wdenk committed
190 191
{
	cmd_tbl_t *cmdtp;
wdenk's avatar
wdenk committed
192
	cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start;	/*Init value */
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	const char *p;
	int len;
	int n_found = 0;

	/*
	 * Some commands allow length modifiers (like "cp.b");
	 * compare command name only until first dot.
	 */
	len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);

	for (cmdtp = &__u_boot_cmd_start;
	     cmdtp != &__u_boot_cmd_end;
	     cmdtp++) {
		if (strncmp (cmd, cmdtp->name, len) == 0) {
			if (len == strlen (cmdtp->name))
				return cmdtp;	/* full match */

			cmdtp_temp = cmdtp;	/* abbreviated command ? */
			n_found++;
		}
wdenk's avatar
wdenk committed
213
	}
214
	if (n_found == 1) {			/* exactly one match */
wdenk's avatar
wdenk committed
215
		return cmdtp_temp;
216
	}
wdenk's avatar
wdenk committed
217

218
	return NULL;	/* not found or ambiguous command */
wdenk's avatar
wdenk committed
219
}