2002-10-19 19:42:10 +00:00
|
|
|
/*
|
2003-06-27 21:31:46 +00:00
|
|
|
* (C) Copyright 2000-2003
|
2002-10-19 19:42:10 +00:00
|
|
|
* 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>
|
2007-07-05 16:01:48 +00:00
|
|
|
#include <xfuncs.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <environment.h>
|
2009-11-02 13:31:18 +00:00
|
|
|
#include <linux/list.h>
|
2007-10-11 19:58:51 +00:00
|
|
|
#include <init.h>
|
2008-03-01 20:08:14 +00:00
|
|
|
#include <complete.h>
|
2008-08-01 09:47:10 +00:00
|
|
|
#include <linux/utsrelease.h>
|
2009-10-17 10:49:47 +00:00
|
|
|
#include <getopt.h>
|
2008-08-01 09:47:10 +00:00
|
|
|
|
|
|
|
const char version_string[] =
|
2009-12-15 08:11:09 +00:00
|
|
|
"barebox " UTS_RELEASE " (" __DATE__ " - " __TIME__ ")";
|
2007-10-11 19:58:51 +00:00
|
|
|
|
|
|
|
LIST_HEAD(command_list);
|
|
|
|
EXPORT_SYMBOL(command_list);
|
2002-10-19 19:42:10 +00:00
|
|
|
|
2007-09-23 23:40:06 +00:00
|
|
|
#ifdef CONFIG_SHELL_HUSH
|
2007-07-05 16:01:48 +00:00
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
static int do_exit(struct command *cmdtp, int argc, char *argv[])
|
2004-04-18 10:13:26 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
if (argc > 1)
|
2007-07-05 16:01:55 +00:00
|
|
|
r = simple_strtoul(argv[1], NULL, 0);
|
2004-04-18 10:13:26 +00:00
|
|
|
|
|
|
|
return -r - 2;
|
|
|
|
}
|
|
|
|
|
2009-12-15 08:11:09 +00:00
|
|
|
BAREBOX_CMD_START(exit)
|
2007-07-05 16:01:52 +00:00
|
|
|
.cmd = do_exit,
|
2007-07-05 16:01:55 +00:00
|
|
|
.usage = "exit script",
|
2009-12-15 08:11:09 +00:00
|
|
|
BAREBOX_CMD_END
|
2004-04-18 10:13:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
void barebox_cmd_usage(struct command *cmdtp)
|
2007-07-05 16:01:55 +00:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_LONGHELP
|
|
|
|
/* found - print (long) help info */
|
|
|
|
if (cmdtp->help) {
|
|
|
|
puts (cmdtp->help);
|
|
|
|
} else {
|
|
|
|
puts (cmdtp->name);
|
2007-09-21 07:09:06 +00:00
|
|
|
putchar (' ');
|
2007-07-05 16:01:55 +00:00
|
|
|
puts ("- No help available.\n");
|
|
|
|
}
|
2007-09-21 07:09:06 +00:00
|
|
|
putchar ('\n');
|
2007-07-05 16:01:55 +00:00
|
|
|
#else /* no long help available */
|
2007-07-05 16:02:05 +00:00
|
|
|
if (cmdtp->usage) {
|
2007-07-05 16:01:55 +00:00
|
|
|
puts (cmdtp->usage);
|
2007-07-05 16:02:05 +00:00
|
|
|
puts("\n");
|
|
|
|
}
|
2007-07-05 16:01:55 +00:00
|
|
|
#endif /* CONFIG_LONGHELP */
|
|
|
|
}
|
2009-12-15 08:11:09 +00:00
|
|
|
EXPORT_SYMBOL(barebox_cmd_usage);
|
2007-07-05 16:01:55 +00:00
|
|
|
|
2007-10-19 11:06:45 +00:00
|
|
|
static int compare(struct list_head *a, struct list_head *b)
|
|
|
|
{
|
2010-01-04 09:21:11 +00:00
|
|
|
char *na = (char*)list_entry(a, struct command, list)->name;
|
|
|
|
char *nb = (char*)list_entry(b, struct command, list)->name;
|
2007-10-19 11:06:45 +00:00
|
|
|
|
|
|
|
return strcmp(na, nb);
|
|
|
|
}
|
2007-10-01 08:15:38 +00:00
|
|
|
|
2009-10-17 10:01:14 +00:00
|
|
|
int execute_command(int argc, char **argv)
|
|
|
|
{
|
2010-01-04 09:21:11 +00:00
|
|
|
struct command *cmdtp;
|
2009-10-17 10:23:03 +00:00
|
|
|
int ret;
|
2009-10-17 10:01:14 +00:00
|
|
|
|
2009-10-17 10:49:47 +00:00
|
|
|
getopt_reset();
|
|
|
|
|
2009-10-17 10:01:14 +00:00
|
|
|
/* Look up command in command table */
|
|
|
|
if ((cmdtp = find_cmd(argv[0]))) {
|
|
|
|
/* OK - call function to do the command */
|
2009-10-17 10:23:03 +00:00
|
|
|
ret = cmdtp->cmd(cmdtp, argc, argv);
|
|
|
|
if (ret == COMMAND_ERROR_USAGE) {
|
2009-12-15 08:11:09 +00:00
|
|
|
barebox_cmd_usage(cmdtp);
|
2009-10-17 10:23:03 +00:00
|
|
|
return COMMAND_ERROR;
|
|
|
|
}
|
|
|
|
return ret;
|
2009-10-17 10:01:14 +00:00
|
|
|
} else {
|
2009-11-18 14:58:58 +00:00
|
|
|
#ifdef CONFIG_CMD_HELP
|
2009-10-17 10:01:14 +00:00
|
|
|
printf ("Unknown command '%s' - try 'help'\n", argv[0]);
|
2009-11-18 14:58:58 +00:00
|
|
|
#else
|
|
|
|
printf ("Unknown command '%s'\n", argv[0]);
|
|
|
|
#endif
|
2009-10-17 10:01:14 +00:00
|
|
|
return -1; /* give up after bad command */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
int register_command(struct command *cmd)
|
2007-10-01 08:15:38 +00:00
|
|
|
{
|
2007-10-11 19:58:51 +00:00
|
|
|
/*
|
|
|
|
* We do not check if the command already exists.
|
|
|
|
* This allows us to overwrite a builtin command
|
|
|
|
* with a module.
|
|
|
|
*/
|
2007-10-01 08:15:38 +00:00
|
|
|
|
2007-10-19 11:06:45 +00:00
|
|
|
debug("register command %s\n", cmd->name);
|
2007-10-01 08:15:38 +00:00
|
|
|
|
2007-10-19 11:06:45 +00:00
|
|
|
list_add_sort(&cmd->list, &command_list, compare);
|
|
|
|
|
|
|
|
if (cmd->aliases) {
|
2008-04-04 09:46:55 +00:00
|
|
|
char **aliases = (char**)cmd->aliases;
|
2007-10-19 11:06:45 +00:00
|
|
|
while(*aliases) {
|
|
|
|
char *usage = "alias for ";
|
2010-01-04 09:21:11 +00:00
|
|
|
struct command *c = xzalloc(sizeof(struct command));
|
2007-10-19 11:06:45 +00:00
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
memcpy(c, cmd, sizeof(struct command));
|
2007-10-19 11:06:45 +00:00
|
|
|
|
|
|
|
c->name = *aliases;
|
|
|
|
c->usage = xmalloc(strlen(usage) + strlen(cmd->name) + 1);
|
2008-04-04 09:46:55 +00:00
|
|
|
sprintf((char*)c->usage, "%s%s", usage, cmd->name);
|
2007-10-19 11:06:45 +00:00
|
|
|
|
|
|
|
c->aliases = NULL;
|
|
|
|
|
|
|
|
register_command(c);
|
|
|
|
|
|
|
|
aliases++;
|
|
|
|
}
|
|
|
|
}
|
2007-10-01 08:15:38 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-19 11:06:45 +00:00
|
|
|
EXPORT_SYMBOL(register_command);
|
2007-07-05 16:01:43 +00:00
|
|
|
|
2007-10-11 19:58:51 +00:00
|
|
|
/*
|
2002-10-19 19:42:10 +00:00
|
|
|
* find command table entry for a command
|
|
|
|
*/
|
2010-01-04 09:21:11 +00:00
|
|
|
struct command *find_cmd (const char *cmd)
|
2002-10-19 19:42:10 +00:00
|
|
|
{
|
2010-01-04 09:21:11 +00:00
|
|
|
struct command *cmdtp;
|
|
|
|
struct command *cmdtp_temp = &__barebox_cmd_start; /*Init value */
|
2003-06-28 23:11:04 +00:00
|
|
|
int len;
|
|
|
|
int n_found = 0;
|
2007-07-05 16:01:52 +00:00
|
|
|
len = strlen (cmd);
|
2003-06-28 23:11:04 +00:00
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
cmdtp = list_entry(&command_list, struct command, list);
|
2007-10-01 08:15:38 +00:00
|
|
|
|
2007-10-11 19:58:51 +00:00
|
|
|
for_each_command(cmdtp) {
|
2003-06-28 23:11:04 +00:00
|
|
|
if (strncmp (cmd, cmdtp->name, len) == 0) {
|
|
|
|
if (len == strlen (cmdtp->name))
|
2007-10-11 19:58:51 +00:00
|
|
|
return cmdtp; /* full match */
|
2003-06-28 23:11:04 +00:00
|
|
|
|
2007-10-11 19:58:51 +00:00
|
|
|
cmdtp_temp = cmdtp; /* abbreviated command ? */
|
2003-06-28 23:11:04 +00:00
|
|
|
n_found++;
|
|
|
|
}
|
2002-10-19 19:42:10 +00:00
|
|
|
}
|
2007-10-11 19:58:51 +00:00
|
|
|
|
2003-06-28 23:11:04 +00:00
|
|
|
if (n_found == 1) { /* exactly one match */
|
2003-06-27 21:31:46 +00:00
|
|
|
return cmdtp_temp;
|
2003-06-28 23:11:04 +00:00
|
|
|
}
|
2002-10-19 19:42:10 +00:00
|
|
|
|
2003-06-28 23:11:04 +00:00
|
|
|
return NULL; /* not found or ambiguous command */
|
2003-06-27 21:31:46 +00:00
|
|
|
}
|
2004-04-15 18:22:41 +00:00
|
|
|
|
2007-10-11 19:58:51 +00:00
|
|
|
/*
|
|
|
|
* Put all commands into a linked list. Without module support we could use
|
|
|
|
* the raw command array, but with module support a list is easier to handle.
|
|
|
|
* It does not create significant overhead so do it also without module
|
|
|
|
* support.
|
|
|
|
*/
|
|
|
|
static int init_command_list(void)
|
|
|
|
{
|
2010-01-04 09:21:11 +00:00
|
|
|
struct command *cmdtp;
|
2007-10-11 19:58:51 +00:00
|
|
|
|
2009-12-15 08:11:09 +00:00
|
|
|
for (cmdtp = &__barebox_cmd_start;
|
|
|
|
cmdtp != &__barebox_cmd_end;
|
2007-10-11 19:58:51 +00:00
|
|
|
cmdtp++)
|
2007-10-19 11:06:45 +00:00
|
|
|
register_command(cmdtp);
|
2007-10-11 19:58:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
late_initcall(init_command_list);
|
|
|
|
|