nand_base: We have to ignore the -EUCLEAN error
[barebox-mini2440.git] / common / command.c
blobaccaffa4be867fa76e8557028b738763fefa41c0
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>
38 const char version_string[] =
39 "U-Boot " UTS_RELEASE " (" __DATE__ " - " __TIME__ ")";
41 LIST_HEAD(command_list);
42 EXPORT_SYMBOL(command_list);
44 #ifdef CONFIG_SHELL_HUSH
46 static int do_exit (cmd_tbl_t *cmdtp, int argc, char *argv[])
48 int r;
50 r = 0;
51 if (argc > 1)
52 r = simple_strtoul(argv[1], NULL, 0);
54 return -r - 2;
57 U_BOOT_CMD_START(exit)
58 .maxargs = 2,
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 register_command(cmd_tbl_t *cmd)
97 * We do not check if the command already exists.
98 * This allows us to overwrite a builtin command
99 * with a module.
102 debug("register command %s\n", cmd->name);
104 list_add_sort(&cmd->list, &command_list, compare);
106 if (cmd->aliases) {
107 char **aliases = (char**)cmd->aliases;
108 while(*aliases) {
109 char *usage = "alias for ";
110 cmd_tbl_t *c = xzalloc(sizeof(cmd_tbl_t));
112 memcpy(c, cmd, sizeof(cmd_tbl_t));
114 c->name = *aliases;
115 c->usage = xmalloc(strlen(usage) + strlen(cmd->name) + 1);
116 sprintf((char*)c->usage, "%s%s", usage, cmd->name);
118 c->aliases = NULL;
120 register_command(c);
122 aliases++;
126 return 0;
128 EXPORT_SYMBOL(register_command);
131 * find command table entry for a command
133 cmd_tbl_t *find_cmd (const char *cmd)
135 cmd_tbl_t *cmdtp;
136 cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
137 int len;
138 int n_found = 0;
139 len = strlen (cmd);
141 cmdtp = list_entry(&command_list, cmd_tbl_t, list);
143 for_each_command(cmdtp) {
144 if (strncmp (cmd, cmdtp->name, len) == 0) {
145 if (len == strlen (cmdtp->name))
146 return cmdtp; /* full match */
148 cmdtp_temp = cmdtp; /* abbreviated command ? */
149 n_found++;
153 if (n_found == 1) { /* exactly one match */
154 return cmdtp_temp;
157 return NULL; /* not found or ambiguous command */
161 * Put all commands into a linked list. Without module support we could use
162 * the raw command array, but with module support a list is easier to handle.
163 * It does not create significant overhead so do it also without module
164 * support.
166 static int init_command_list(void)
168 cmd_tbl_t *cmdtp;
170 for (cmdtp = &__u_boot_cmd_start;
171 cmdtp != &__u_boot_cmd_end;
172 cmdtp++)
173 register_command(cmdtp);
175 return 0;
178 late_initcall(init_command_list);