diff options
author | Sascha Hauer <sha@octopus.labnet.pengutronix.de> | 2007-10-11 21:58:51 +0200 |
---|---|---|
committer | Sascha Hauer <sha@octopus.labnet.pengutronix.de> | 2007-10-11 21:58:51 +0200 |
commit | 82e54558b2cdbe2b95a7eb7986059931b4518f3d (patch) | |
tree | e375cae7290d104daaddefd6f487d76d25ecc627 /common | |
parent | afaaf9a5d9f86c7d5910ae4bd801ad22df186de6 (diff) | |
download | barebox-82e54558b2cdbe2b95a7eb7986059931b4518f3d.tar.gz barebox-82e54558b2cdbe2b95a7eb7986059931b4518f3d.tar.xz |
Collect all commands in a linked list. This makes it easier to handle
dynamically registered commands.
Diffstat (limited to 'common')
-rw-r--r-- | common/command.c | 84 |
1 files changed, 43 insertions, 41 deletions
diff --git a/common/command.c b/common/command.c index f3b3d360b3..c78da7e3a5 100644 --- a/common/command.c +++ b/common/command.c @@ -30,6 +30,11 @@ #include <xfuncs.h> #include <malloc.h> #include <environment.h> +#include <list.h> +#include <init.h> + +LIST_HEAD(command_list); +EXPORT_SYMBOL(command_list); static int do_version (cmd_tbl_t *cmdtp, int argc, char *argv[]) { @@ -200,36 +205,28 @@ U_BOOT_CMD_START(help) U_BOOT_CMD_END #ifdef CONFIG_MODULES -struct cmd_list { - cmd_tbl_t *cmd; - struct cmd_list *next; -}; - -static struct cmd_list *cmd_list; int register_command(cmd_tbl_t *cmd) { - struct cmd_list *c = cmd_list; - - debug("register command %s\n", cmd->name); - - if (!c) { - cmd_list = (struct cmd_list *)xzalloc(sizeof(struct cmd_list)); - cmd_list->cmd = cmd; - return 0; - } + /* + * We do not check if the command already exists. + * This allows us to overwrite a builtin command + * with a module. + */ - while (c->next) - c = c->next; + printf("register command %s\n", cmd->name); - c->next = (struct cmd_list *)xzalloc(sizeof(struct cmd_list)); - c->next->cmd = cmd; + /* + * Would be nice to have some kind of list_add_sort + * to keep the command list in order + */ + list_add_tail(&cmd->list, &command_list); return 0; } #endif -/*************************************************************************** +/* * find command table entry for a command */ cmd_tbl_t *find_cmd (const char *cmd) @@ -238,35 +235,19 @@ cmd_tbl_t *find_cmd (const char *cmd) cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */ int len; int n_found = 0; -#ifdef CONFIG_MODULES - struct cmd_list *list = cmd_list; -#endif len = strlen (cmd); -#ifdef CONFIG_MODULES - while(list) { - cmdtp = list->cmd; - if (strncmp (cmd, cmdtp->name, len) == 0) { - if (len == strlen (cmdtp->name)) - return cmdtp; /* full match */ + cmdtp = list_entry(&command_list, cmd_tbl_t, list); - cmdtp_temp = cmdtp; /* abbreviated command ? */ - n_found++; - } - list = list->next; - } -#endif - - for (cmdtp = &__u_boot_cmd_start; - cmdtp != &__u_boot_cmd_end; - cmdtp++) { + for_each_command(cmdtp) { if (strncmp (cmd, cmdtp->name, len) == 0) { if (len == strlen (cmdtp->name)) - return cmdtp; /* full match */ + return cmdtp; /* full match */ - cmdtp_temp = cmdtp; /* abbreviated command ? */ + cmdtp_temp = cmdtp; /* abbreviated command ? */ n_found++; } + if (cmdtp->aliases) { char **aliases = cmdtp->aliases; while(*aliases) { @@ -281,6 +262,7 @@ cmd_tbl_t *find_cmd (const char *cmd) } } } + if (n_found == 1) { /* exactly one match */ return cmdtp_temp; } @@ -288,6 +270,26 @@ cmd_tbl_t *find_cmd (const char *cmd) return NULL; /* not found or ambiguous command */ } +/* + * 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) +{ + cmd_tbl_t *cmdtp; + + for (cmdtp = &__u_boot_cmd_start; + cmdtp != &__u_boot_cmd_end; + cmdtp++) + list_add_tail(&cmdtp->list, &command_list); + + return 0; +} + +late_initcall(init_command_list); + #ifdef CONFIG_AUTO_COMPLETE int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[]) |