driver: add dev_name inline
[barebox-mini2440.git] / common / command.c
blobf484520572117596758c8d07f9df1bcaccfbdc2c
1 /*
2 * (C) Copyright 2000-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
25 * Command Processor Table
28 #include <common.h>
29 #include <command.h>
30 #include <xfuncs.h>
31 #include <malloc.h>
32 #include <environment.h>
33 #include <list.h>
34 #include <init.h>
35 #include <complete.h>
36 #include <linux/utsrelease.h>
37 #include <getopt.h>
39 const char version_string[] =
40 "U-Boot " UTS_RELEASE " (" __DATE__ " - " __TIME__ ")";
42 LIST_HEAD(command_list);
43 EXPORT_SYMBOL(command_list);
45 #ifdef CONFIG_SHELL_HUSH
47 static int do_exit (cmd_tbl_t *cmdtp, int argc, char *argv[])
49 int r;
51 r = 0;
52 if (argc > 1)
53 r = simple_strtoul(argv[1], NULL, 0);
55 return -r - 2;
58 U_BOOT_CMD_START(exit)
59 .cmd = do_exit,
60 .usage = "exit script",
61 U_BOOT_CMD_END
63 #endif
65 void u_boot_cmd_usage(cmd_tbl_t *cmdtp)
67 #ifdef CONFIG_LONGHELP
68 /* found - print (long) help info */
69 if (cmdtp->help) {
70 puts (cmdtp->help);
71 } else {
72 puts (cmdtp->name);
73 putchar (' ');
74 puts ("- No help available.\n");
76 putchar ('\n');
77 #else /* no long help available */
78 if (cmdtp->usage) {
79 puts (cmdtp->usage);
80 puts("\n");
82 #endif /* CONFIG_LONGHELP */
84 EXPORT_SYMBOL(u_boot_cmd_usage);
86 static int compare(struct list_head *a, struct list_head *b)
88 char *na = (char*)list_entry(a, cmd_tbl_t, list)->name;
89 char *nb = (char*)list_entry(b, cmd_tbl_t, list)->name;
91 return strcmp(na, nb);
94 int execute_command(int argc, char **argv)
96 cmd_tbl_t *cmdtp;
97 int ret;
99 getopt_reset();
101 /* Look up command in command table */
102 if ((cmdtp = find_cmd(argv[0]))) {
103 /* OK - call function to do the command */
104 ret = cmdtp->cmd(cmdtp, argc, argv);
105 if (ret == COMMAND_ERROR_USAGE) {
106 u_boot_cmd_usage(cmdtp);
107 return COMMAND_ERROR;
109 return ret;
110 } else {
111 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
112 return -1; /* give up after bad command */
116 int register_command(cmd_tbl_t *cmd)
119 * We do not check if the command already exists.
120 * This allows us to overwrite a builtin command
121 * with a module.
124 debug("register command %s\n", cmd->name);
126 list_add_sort(&cmd->list, &command_list, compare);
128 if (cmd->aliases) {
129 char **aliases = (char**)cmd->aliases;
130 while(*aliases) {
131 char *usage = "alias for ";
132 cmd_tbl_t *c = xzalloc(sizeof(cmd_tbl_t));
134 memcpy(c, cmd, sizeof(cmd_tbl_t));
136 c->name = *aliases;
137 c->usage = xmalloc(strlen(usage) + strlen(cmd->name) + 1);
138 sprintf((char*)c->usage, "%s%s", usage, cmd->name);
140 c->aliases = NULL;
142 register_command(c);
144 aliases++;
148 return 0;
150 EXPORT_SYMBOL(register_command);
153 * find command table entry for a command
155 cmd_tbl_t *find_cmd (const char *cmd)
157 cmd_tbl_t *cmdtp;
158 cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
159 int len;
160 int n_found = 0;
161 len = strlen (cmd);
163 cmdtp = list_entry(&command_list, cmd_tbl_t, list);
165 for_each_command(cmdtp) {
166 if (strncmp (cmd, cmdtp->name, len) == 0) {
167 if (len == strlen (cmdtp->name))
168 return cmdtp; /* full match */
170 cmdtp_temp = cmdtp; /* abbreviated command ? */
171 n_found++;
175 if (n_found == 1) { /* exactly one match */
176 return cmdtp_temp;
179 return NULL; /* not found or ambiguous command */
183 * Put all commands into a linked list. Without module support we could use
184 * the raw command array, but with module support a list is easier to handle.
185 * It does not create significant overhead so do it also without module
186 * support.
188 static int init_command_list(void)
190 cmd_tbl_t *cmdtp;
192 for (cmdtp = &__u_boot_cmd_start;
193 cmdtp != &__u_boot_cmd_end;
194 cmdtp++)
195 register_command(cmdtp);
197 return 0;
200 late_initcall(init_command_list);