9
0
Fork 0
barebox/common/command.c

161 lines
3.5 KiB
C
Raw Normal View History

2002-10-19 19:42:10 +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>
#include <xfuncs.h>
#include <malloc.h>
#include <environment.h>
#include <linux/list.h>
#include <init.h>
2008-03-01 20:08:14 +00:00
#include <complete.h>
#include <getopt.h>
LIST_HEAD(command_list);
EXPORT_SYMBOL(command_list);
2002-10-19 19:42:10 +00:00
void barebox_cmd_usage(struct command *cmdtp)
{
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
}
EXPORT_SYMBOL(barebox_cmd_usage);
static int compare(struct list_head *a, struct list_head *b)
{
char *na = (char*)list_entry(a, struct command, list)->name;
char *nb = (char*)list_entry(b, struct command, list)->name;
return strcmp(na, nb);
}
int execute_command(int argc, char **argv)
{
struct command *cmdtp;
int ret;
struct getopt_context gc;
getopt_context_store(&gc);
/* Look up command in command table */
if ((cmdtp = find_cmd(argv[0]))) {
/* OK - call function to do the command */
ret = cmdtp->cmd(argc, argv);
if (ret == COMMAND_ERROR_USAGE) {
barebox_cmd_usage(cmdtp);
ret = COMMAND_ERROR;
}
} else {
#ifdef CONFIG_CMD_HELP
printf ("Unknown command '%s' - try 'help'\n", argv[0]);
#else
printf ("Unknown command '%s'\n", argv[0]);
#endif
ret = 1; /* give up after bad command */
}
getopt_context_restore(&gc);
return ret;
}
int register_command(struct command *cmd)
{
/*
* We do not check if the command already exists.
* This allows us to overwrite a builtin command
* with a module.
*/
debug("register command %s\n", cmd->name);
list_add_sort(&cmd->list, &command_list, compare);
if (cmd->aliases) {
char **aliases = (char**)cmd->aliases;
while(*aliases) {
struct command *c = xzalloc(sizeof(struct command));
memcpy(c, cmd, sizeof(struct command));
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;
c->aliases = NULL;
register_command(c);
aliases++;
}
}
return 0;
}
EXPORT_SYMBOL(register_command);
/*
2002-10-19 19:42:10 +00:00
* find command table entry for a command
*/
struct command *find_cmd (const char *cmd)
2002-10-19 19:42:10 +00:00
{
struct command *cmdtp;
for_each_command(cmdtp)
if (!strcmp(cmd, cmdtp->name))
return cmdtp;
2002-10-19 19:42:10 +00:00
return NULL; /* not found or ambiguous command */
}
EXPORT_SYMBOL(find_cmd);
/*
* 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)
{
struct command *cmdtp;
for (cmdtp = &__barebox_cmd_start;
cmdtp != &__barebox_cmd_end;
cmdtp++)
register_command(cmdtp);
return 0;
}
late_initcall(init_command_list);