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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2009-10-17 10:49:47 +00:00
|
|
|
#include <getopt.h>
|
2008-08-01 09:47:10 +00:00
|
|
|
|
2007-10-11 19:58:51 +00:00
|
|
|
LIST_HEAD(command_list);
|
|
|
|
EXPORT_SYMBOL(command_list);
|
2002-10-19 19:42:10 +00:00
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
void barebox_cmd_usage(struct command *cmdtp)
|
2007-07-05 16:01:55 +00:00
|
|
|
{
|
commands: harmonize in-barebox documentation
This patch does probably too much, but it's hard (and very
cumbersome/time consuming) to break it out. What is does is this:
* each command has one short description, e.g. "list MUX configuration"
* made sure the short descriptions start lowercase
* each command has one usage. That string contains just the
options, e.g. "[-npn]". It's not part of the long help text.
* that is, it doesn't say "[OPTIONS]" anymore, every usable option
is listed by character in this (short) option string (the long
description is in the long help text, as before)
* help texts have been reworked, to make them
- sometimes smaller
- sometimes describe the options better
- more often present themselves in a nicer format
* all long help texts are now created with BUSYBOX_CMD_HELP_
macros, no more 'static const __maybe_unused char cmd_foobar_help[]'
* made sure the long help texts starts uppercase
* because cmdtp->name and cmdtp->opts together provide the new usage,
all "Usage: foobar" texts have been removed from the long help texts
* BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this
is nicer in the source code
* BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself
* made sure no line gets longer than 77 characters
* delibertely renamed cmdtp->usage, so that we can get compile-time
errors (e.g. in out-of-tree modules that use register_command()
* the 'help' command can now always emit the usage, even without
compiled long help texts
* 'help -v' gives a list of commands with their short description, this
is similar like the old "help" command before my patchset
* 'help -a' gives out help of all commands
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
|
|
|
putchar('\n');
|
|
|
|
if (cmdtp->desc)
|
|
|
|
printf("%s - %s\n\n", cmdtp->name, cmdtp->desc);
|
|
|
|
if (cmdtp->opts)
|
|
|
|
printf("Usage: %s %s\n\n", cmdtp->name, cmdtp->opts);
|
|
|
|
#ifdef CONFIG_LONGHELP
|
|
|
|
/* found - print (long) help info */
|
|
|
|
if (cmdtp->help) {
|
|
|
|
puts(cmdtp->help);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#endif
|
2007-07-05 16:01:55 +00:00
|
|
|
}
|
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;
|
2012-02-25 16:10:57 +00:00
|
|
|
struct getopt_context gc;
|
2009-10-17 10:01:14 +00:00
|
|
|
|
2012-02-25 16:10:57 +00:00
|
|
|
getopt_context_store(&gc);
|
2009-10-17 10:49:47 +00:00
|
|
|
|
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 */
|
2011-12-21 13:18:57 +00:00
|
|
|
ret = cmdtp->cmd(argc, argv);
|
2009-10-17 10:23:03 +00:00
|
|
|
if (ret == COMMAND_ERROR_USAGE) {
|
2009-12-15 08:11:09 +00:00
|
|
|
barebox_cmd_usage(cmdtp);
|
2012-02-25 16:10:57 +00:00
|
|
|
ret = COMMAND_ERROR;
|
2009-10-17 10:23:03 +00:00
|
|
|
}
|
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
|
2012-11-01 14:13:05 +00:00
|
|
|
ret = 1; /* give up after bad command */
|
2009-10-17 10:01:14 +00:00
|
|
|
}
|
2012-02-25 16:10:57 +00:00
|
|
|
|
|
|
|
getopt_context_restore(&gc);
|
|
|
|
|
|
|
|
return ret;
|
2009-10-17 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
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;
|
commands: harmonize in-barebox documentation
This patch does probably too much, but it's hard (and very
cumbersome/time consuming) to break it out. What is does is this:
* each command has one short description, e.g. "list MUX configuration"
* made sure the short descriptions start lowercase
* each command has one usage. That string contains just the
options, e.g. "[-npn]". It's not part of the long help text.
* that is, it doesn't say "[OPTIONS]" anymore, every usable option
is listed by character in this (short) option string (the long
description is in the long help text, as before)
* help texts have been reworked, to make them
- sometimes smaller
- sometimes describe the options better
- more often present themselves in a nicer format
* all long help texts are now created with BUSYBOX_CMD_HELP_
macros, no more 'static const __maybe_unused char cmd_foobar_help[]'
* made sure the long help texts starts uppercase
* because cmdtp->name and cmdtp->opts together provide the new usage,
all "Usage: foobar" texts have been removed from the long help texts
* BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this
is nicer in the source code
* BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself
* made sure no line gets longer than 77 characters
* delibertely renamed cmdtp->usage, so that we can get compile-time
errors (e.g. in out-of-tree modules that use register_command()
* the 'help' command can now always emit the usage, even without
compiled long help texts
* 'help -v' gives a list of commands with their short description, this
is similar like the old "help" command before my patchset
* 'help -a' gives out help of all commands
Signed-off-by: Holger Schurig <holgerschurig@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
|
|
|
c->desc = cmd->desc;
|
|
|
|
c->opts = cmd->opts;
|
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;
|
2003-06-28 23:11:04 +00:00
|
|
|
|
2011-12-17 21:30:24 +00:00
|
|
|
for_each_command(cmdtp)
|
|
|
|
if (!strcmp(cmd, cmdtp->name))
|
|
|
|
return cmdtp;
|
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
|
|
|
}
|
2012-07-25 07:47:33 +00:00
|
|
|
EXPORT_SYMBOL(find_cmd);
|
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);
|