wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 1 | /* |
Detlev Zundel | 2dce551 | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 2 | * (C) Copyright 2000-2009 |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Command Processor Table |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 30 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Use puts() instead of printf() to avoid printf buffer overflow |
| 33 | * for long help messages |
| 34 | */ |
Detlev Zundel | 2dce551 | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 35 | |
| 36 | int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 37 | flag, int argc, char * const argv[]) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | int rcode = 0; |
| 41 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 42 | if (argc == 1) { /*show list of commands */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 43 | cmd_tbl_t *cmd_array[cmd_items]; |
| 44 | int i, j, swaps; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 45 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 46 | /* Make array of commands from .uboot_cmd section */ |
Detlev Zundel | 2dce551 | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 47 | cmdtp = cmd_start; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 48 | for (i = 0; i < cmd_items; i++) { |
| 49 | cmd_array[i] = cmdtp++; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 50 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 51 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 52 | /* Sort command list (trivial bubble sort) */ |
| 53 | for (i = cmd_items - 1; i > 0; --i) { |
| 54 | swaps = 0; |
| 55 | for (j = 0; j < i; ++j) { |
| 56 | if (strcmp (cmd_array[j]->name, |
| 57 | cmd_array[j + 1]->name) > 0) { |
| 58 | cmd_tbl_t *tmp; |
| 59 | tmp = cmd_array[j]; |
| 60 | cmd_array[j] = cmd_array[j + 1]; |
| 61 | cmd_array[j + 1] = tmp; |
| 62 | ++swaps; |
| 63 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 64 | } |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 65 | if (!swaps) |
| 66 | break; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* print short help (usage) */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 70 | for (i = 0; i < cmd_items; i++) { |
| 71 | const char *usage = cmd_array[i]->usage; |
| 72 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 73 | /* allow user abort */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 74 | if (ctrlc ()) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 75 | return 1; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 76 | if (usage == NULL) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 77 | continue; |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 78 | printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH, |
| 79 | cmd_array[i]->name, usage); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 80 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 81 | return 0; |
| 82 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 83 | /* |
| 84 | * command help (long version) |
| 85 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 86 | for (i = 1; i < argc; ++i) { |
Detlev Zundel | 2dce551 | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 87 | if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) { |
Wolfgang Denk | 94796d8 | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 88 | rcode |= cmd_usage(cmdtp); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 89 | } else { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 90 | printf ("Unknown command '%s' - try 'help'" |
| 91 | " without arguments for list of all" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 92 | " known commands\n\n", argv[i] |
| 93 | ); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 94 | rcode = 1; |
| 95 | } |
| 96 | } |
| 97 | return rcode; |
| 98 | } |
| 99 | |
| 100 | /*************************************************************************** |
| 101 | * find command table entry for a command |
| 102 | */ |
Kumar Gala | b799cb4 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 103 | cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 104 | { |
| 105 | cmd_tbl_t *cmdtp; |
Kumar Gala | b799cb4 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 106 | cmd_tbl_t *cmdtp_temp = table; /*Init value */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 107 | const char *p; |
| 108 | int len; |
| 109 | int n_found = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 110 | |
Thomas Weber | 7013c06 | 2010-11-24 13:07:51 +0100 | [diff] [blame^] | 111 | if (!cmd) |
| 112 | return NULL; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Some commands allow length modifiers (like "cp.b"); |
| 115 | * compare command name only until first dot. |
| 116 | */ |
| 117 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); |
| 118 | |
Kumar Gala | b799cb4 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 119 | for (cmdtp = table; |
| 120 | cmdtp != table + table_len; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 121 | cmdtp++) { |
| 122 | if (strncmp (cmd, cmdtp->name, len) == 0) { |
| 123 | if (len == strlen (cmdtp->name)) |
| 124 | return cmdtp; /* full match */ |
| 125 | |
| 126 | cmdtp_temp = cmdtp; /* abbreviated command ? */ |
| 127 | n_found++; |
| 128 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 129 | } |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 130 | if (n_found == 1) { /* exactly one match */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 131 | return cmdtp_temp; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 132 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 133 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 134 | return NULL; /* not found or ambiguous command */ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 135 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 136 | |
Kumar Gala | b799cb4 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 137 | cmd_tbl_t *find_cmd (const char *cmd) |
| 138 | { |
| 139 | int len = &__u_boot_cmd_end - &__u_boot_cmd_start; |
| 140 | return find_cmd_tbl(cmd, &__u_boot_cmd_start, len); |
| 141 | } |
| 142 | |
Wolfgang Denk | 94796d8 | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 143 | int cmd_usage(cmd_tbl_t *cmdtp) |
Peter Tyser | 62c3ae7 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 144 | { |
Wolfgang Denk | 94796d8 | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 145 | printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); |
| 146 | |
| 147 | #ifdef CONFIG_SYS_LONGHELP |
| 148 | printf("Usage:\n%s ", cmdtp->name); |
| 149 | |
| 150 | if (!cmdtp->help) { |
| 151 | puts ("- No additional help available.\n"); |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | puts (cmdtp->help); |
| 156 | putc ('\n'); |
| 157 | #endif /* CONFIG_SYS_LONGHELP */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 158 | return 1; |
Peter Tyser | 62c3ae7 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 159 | } |
| 160 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 161 | #ifdef CONFIG_AUTO_COMPLETE |
| 162 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 163 | int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 164 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 165 | #if 0 /* need to reimplement */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 166 | static char tmp_buf[512]; |
| 167 | int space; |
| 168 | |
| 169 | space = last_char == '\0' || last_char == ' ' || last_char == '\t'; |
| 170 | |
| 171 | if (space && argc == 1) |
| 172 | return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf); |
| 173 | |
| 174 | if (!space && argc == 2) |
| 175 | return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 176 | #endif |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void install_auto_complete_handler(const char *cmd, |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 181 | int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 182 | { |
| 183 | cmd_tbl_t *cmdtp; |
| 184 | |
| 185 | cmdtp = find_cmd(cmd); |
| 186 | if (cmdtp == NULL) |
| 187 | return; |
| 188 | |
| 189 | cmdtp->complete = complete; |
| 190 | } |
| 191 | |
| 192 | void install_auto_complete(void) |
| 193 | { |
Trübenbach, Ralf | 39f7aac | 2010-04-14 11:15:16 +0200 | [diff] [blame] | 194 | #if defined(CONFIG_CMD_EDITENV) |
| 195 | install_auto_complete_handler("editenv", var_complete); |
| 196 | #endif |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 197 | install_auto_complete_handler("printenv", var_complete); |
| 198 | install_auto_complete_handler("setenv", var_complete); |
Jon Loeliger | c3517f9 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 199 | #if defined(CONFIG_CMD_RUN) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 200 | install_auto_complete_handler("run", var_complete); |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | /*************************************************************************************/ |
| 205 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 206 | static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 207 | { |
| 208 | cmd_tbl_t *cmdtp; |
| 209 | const char *p; |
| 210 | int len, clen; |
| 211 | int n_found = 0; |
| 212 | const char *cmd; |
| 213 | |
| 214 | /* sanity? */ |
| 215 | if (maxv < 2) |
| 216 | return -2; |
| 217 | |
| 218 | cmdv[0] = NULL; |
| 219 | |
| 220 | if (argc == 0) { |
| 221 | /* output full list of commands */ |
| 222 | for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { |
| 223 | if (n_found >= maxv - 2) { |
| 224 | cmdv[n_found++] = "..."; |
| 225 | break; |
| 226 | } |
| 227 | cmdv[n_found++] = cmdtp->name; |
| 228 | } |
| 229 | cmdv[n_found] = NULL; |
| 230 | return n_found; |
| 231 | } |
| 232 | |
| 233 | /* more than one arg or one but the start of the next */ |
| 234 | if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) { |
| 235 | cmdtp = find_cmd(argv[0]); |
| 236 | if (cmdtp == NULL || cmdtp->complete == NULL) { |
| 237 | cmdv[0] = NULL; |
| 238 | return 0; |
| 239 | } |
| 240 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); |
| 241 | } |
| 242 | |
| 243 | cmd = argv[0]; |
| 244 | /* |
| 245 | * Some commands allow length modifiers (like "cp.b"); |
| 246 | * compare command name only until first dot. |
| 247 | */ |
| 248 | p = strchr(cmd, '.'); |
| 249 | if (p == NULL) |
| 250 | len = strlen(cmd); |
| 251 | else |
| 252 | len = p - cmd; |
| 253 | |
| 254 | /* return the partial matches */ |
| 255 | for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { |
| 256 | |
| 257 | clen = strlen(cmdtp->name); |
| 258 | if (clen < len) |
| 259 | continue; |
| 260 | |
| 261 | if (memcmp(cmd, cmdtp->name, len) != 0) |
| 262 | continue; |
| 263 | |
| 264 | /* too many! */ |
| 265 | if (n_found >= maxv - 2) { |
| 266 | cmdv[n_found++] = "..."; |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | cmdv[n_found++] = cmdtp->name; |
| 271 | } |
| 272 | |
| 273 | cmdv[n_found] = NULL; |
| 274 | return n_found; |
| 275 | } |
| 276 | |
| 277 | static int make_argv(char *s, int argvsz, char *argv[]) |
| 278 | { |
| 279 | int argc = 0; |
| 280 | |
| 281 | /* split into argv */ |
| 282 | while (argc < argvsz - 1) { |
| 283 | |
| 284 | /* skip any white space */ |
| 285 | while ((*s == ' ') || (*s == '\t')) |
| 286 | ++s; |
| 287 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 288 | if (*s == '\0') /* end of s, no more args */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 289 | break; |
| 290 | |
| 291 | argv[argc++] = s; /* begin of argument string */ |
| 292 | |
| 293 | /* find end of string */ |
| 294 | while (*s && (*s != ' ') && (*s != '\t')) |
| 295 | ++s; |
| 296 | |
| 297 | if (*s == '\0') /* end of s, no more args */ |
| 298 | break; |
| 299 | |
| 300 | *s++ = '\0'; /* terminate current arg */ |
| 301 | } |
| 302 | argv[argc] = NULL; |
| 303 | |
| 304 | return argc; |
| 305 | } |
| 306 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 307 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 308 | { |
| 309 | int ll = leader != NULL ? strlen(leader) : 0; |
| 310 | int sl = sep != NULL ? strlen(sep) : 0; |
| 311 | int len, i; |
| 312 | |
| 313 | if (banner) { |
| 314 | puts("\n"); |
| 315 | puts(banner); |
| 316 | } |
| 317 | |
| 318 | i = linemax; /* force leader and newline */ |
| 319 | while (*argv != NULL) { |
| 320 | len = strlen(*argv) + sl; |
| 321 | if (i + len >= linemax) { |
| 322 | puts("\n"); |
| 323 | if (leader) |
| 324 | puts(leader); |
| 325 | i = ll - sl; |
| 326 | } else if (sep) |
| 327 | puts(sep); |
| 328 | puts(*argv++); |
| 329 | i += len; |
| 330 | } |
| 331 | printf("\n"); |
| 332 | } |
| 333 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 334 | static int find_common_prefix(char * const argv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 335 | { |
| 336 | int i, len; |
| 337 | char *anchor, *s, *t; |
| 338 | |
| 339 | if (*argv == NULL) |
| 340 | return 0; |
| 341 | |
| 342 | /* begin with max */ |
| 343 | anchor = *argv++; |
| 344 | len = strlen(anchor); |
| 345 | while ((t = *argv++) != NULL) { |
| 346 | s = anchor; |
| 347 | for (i = 0; i < len; i++, t++, s++) { |
| 348 | if (*t != *s) |
| 349 | break; |
| 350 | } |
| 351 | len = s - anchor; |
| 352 | } |
| 353 | return len; |
| 354 | } |
| 355 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 356 | static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 357 | |
| 358 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) |
| 359 | { |
| 360 | int n = *np, col = *colp; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 361 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 362 | char *cmdv[20]; |
| 363 | char *s, *t; |
| 364 | const char *sep; |
| 365 | int i, j, k, len, seplen, argc; |
| 366 | int cnt; |
| 367 | char last_char; |
| 368 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 369 | if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 370 | return 0; /* not in normal console */ |
| 371 | |
| 372 | cnt = strlen(buf); |
| 373 | if (cnt >= 1) |
| 374 | last_char = buf[cnt - 1]; |
| 375 | else |
| 376 | last_char = '\0'; |
| 377 | |
| 378 | /* copy to secondary buffer which will be affected */ |
| 379 | strcpy(tmp_buf, buf); |
| 380 | |
| 381 | /* separate into argv */ |
| 382 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); |
| 383 | |
| 384 | /* do the completion and return the possible completions */ |
| 385 | i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv); |
| 386 | |
| 387 | /* no match; bell and out */ |
| 388 | if (i == 0) { |
| 389 | if (argc > 1) /* allow tab for non command */ |
| 390 | return 0; |
| 391 | putc('\a'); |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | s = NULL; |
| 396 | len = 0; |
| 397 | sep = NULL; |
| 398 | seplen = 0; |
| 399 | if (i == 1) { /* one match; perfect */ |
| 400 | k = strlen(argv[argc - 1]); |
| 401 | s = cmdv[0] + k; |
| 402 | len = strlen(s); |
| 403 | sep = " "; |
| 404 | seplen = 1; |
| 405 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ |
| 406 | k = strlen(argv[argc - 1]); |
| 407 | j -= k; |
| 408 | if (j > 0) { |
| 409 | s = cmdv[0] + k; |
| 410 | len = j; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (s != NULL) { |
| 415 | k = len + seplen; |
| 416 | /* make sure it fits */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 417 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 418 | putc('\a'); |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | t = buf + cnt; |
| 423 | for (i = 0; i < len; i++) |
| 424 | *t++ = *s++; |
| 425 | if (sep != NULL) |
| 426 | for (i = 0; i < seplen; i++) |
| 427 | *t++ = sep[i]; |
| 428 | *t = '\0'; |
| 429 | n += k; |
| 430 | col += k; |
| 431 | puts(t - k); |
| 432 | if (sep == NULL) |
| 433 | putc('\a'); |
| 434 | *np = n; |
| 435 | *colp = col; |
| 436 | } else { |
| 437 | print_argv(NULL, " ", " ", 78, cmdv); |
| 438 | |
| 439 | puts(prompt); |
| 440 | puts(buf); |
| 441 | } |
| 442 | return 1; |
| 443 | } |
| 444 | |
| 445 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 8a40fb1 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 446 | |
| 447 | #ifdef CMD_DATA_SIZE |
| 448 | int cmd_get_data_size(char* arg, int default_size) |
| 449 | { |
| 450 | /* Check for a size specification .b, .w or .l. |
| 451 | */ |
| 452 | int len = strlen(arg); |
| 453 | if (len > 2 && arg[len-2] == '.') { |
| 454 | switch(arg[len-1]) { |
| 455 | case 'b': |
| 456 | return 1; |
| 457 | case 'w': |
| 458 | return 2; |
| 459 | case 'l': |
| 460 | return 4; |
| 461 | case 's': |
| 462 | return -2; |
| 463 | default: |
| 464 | return -1; |
| 465 | } |
| 466 | } |
| 467 | return default_size; |
| 468 | } |
| 469 | #endif |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 470 | |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 471 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 472 | DECLARE_GLOBAL_DATA_PTR; |
| 473 | |
| 474 | void fixup_cmdtable(cmd_tbl_t *cmdtp, int size) |
| 475 | { |
| 476 | int i; |
| 477 | |
| 478 | if (gd->reloc_off == 0) |
| 479 | return; |
| 480 | |
| 481 | for (i = 0; i < size; i++) { |
| 482 | ulong addr; |
| 483 | |
| 484 | addr = (ulong) (cmdtp->cmd) + gd->reloc_off; |
| 485 | #if DEBUG_COMMANDS |
| 486 | printf("Command \"%s\": 0x%08lx => 0x%08lx\n", |
| 487 | cmdtp->name, (ulong) (cmdtp->cmd), addr); |
| 488 | #endif |
| 489 | cmdtp->cmd = |
| 490 | (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr; |
| 491 | addr = (ulong)(cmdtp->name) + gd->reloc_off; |
| 492 | cmdtp->name = (char *)addr; |
| 493 | if (cmdtp->usage) { |
| 494 | addr = (ulong)(cmdtp->usage) + gd->reloc_off; |
| 495 | cmdtp->usage = (char *)addr; |
| 496 | } |
| 497 | #ifdef CONFIG_SYS_LONGHELP |
| 498 | if (cmdtp->help) { |
| 499 | addr = (ulong)(cmdtp->help) + gd->reloc_off; |
| 500 | cmdtp->help = (char *)addr; |
| 501 | } |
| 502 | #endif |
| 503 | cmdtp++; |
| 504 | } |
| 505 | } |
| 506 | #endif |