wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 1 | /* |
Detlev Zundel | 5e6c06b | 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> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 30 | #include <linux/ctype.h> |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 31 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Use puts() instead of printf() to avoid printf buffer overflow |
| 34 | * for long help messages |
| 35 | */ |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 36 | |
| 37 | int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 38 | flag, int argc, char * const argv[]) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 39 | { |
| 40 | int i; |
| 41 | int rcode = 0; |
| 42 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 43 | if (argc == 1) { /*show list of commands */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 44 | cmd_tbl_t *cmd_array[cmd_items]; |
| 45 | int i, j, swaps; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 46 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 47 | /* Make array of commands from .uboot_cmd section */ |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 48 | cmdtp = cmd_start; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 49 | for (i = 0; i < cmd_items; i++) { |
| 50 | cmd_array[i] = cmdtp++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 51 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 52 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 53 | /* Sort command list (trivial bubble sort) */ |
| 54 | for (i = cmd_items - 1; i > 0; --i) { |
| 55 | swaps = 0; |
| 56 | for (j = 0; j < i; ++j) { |
| 57 | if (strcmp (cmd_array[j]->name, |
| 58 | cmd_array[j + 1]->name) > 0) { |
| 59 | cmd_tbl_t *tmp; |
| 60 | tmp = cmd_array[j]; |
| 61 | cmd_array[j] = cmd_array[j + 1]; |
| 62 | cmd_array[j + 1] = tmp; |
| 63 | ++swaps; |
| 64 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 65 | } |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 66 | if (!swaps) |
| 67 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* print short help (usage) */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 71 | for (i = 0; i < cmd_items; i++) { |
| 72 | const char *usage = cmd_array[i]->usage; |
| 73 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 74 | /* allow user abort */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 75 | if (ctrlc ()) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 76 | return 1; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 77 | if (usage == NULL) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 78 | continue; |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 79 | printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH, |
| 80 | cmd_array[i]->name, usage); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 81 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 82 | return 0; |
| 83 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 84 | /* |
| 85 | * command help (long version) |
| 86 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | for (i = 1; i < argc; ++i) { |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 88 | if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) { |
Wolfgang Denk | 9dd775b | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 89 | rcode |= cmd_usage(cmdtp); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 90 | } else { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 91 | printf ("Unknown command '%s' - try 'help'" |
| 92 | " without arguments for list of all" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 93 | " known commands\n\n", argv[i] |
| 94 | ); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 95 | rcode = 1; |
| 96 | } |
| 97 | } |
| 98 | return rcode; |
| 99 | } |
| 100 | |
| 101 | /*************************************************************************** |
| 102 | * find command table entry for a command |
| 103 | */ |
Kumar Gala | ac2dad6 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 104 | 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] | 105 | { |
| 106 | cmd_tbl_t *cmdtp; |
Kumar Gala | ac2dad6 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 107 | cmd_tbl_t *cmdtp_temp = table; /*Init value */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 108 | const char *p; |
| 109 | int len; |
| 110 | int n_found = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 111 | |
Thomas Weber | 97ea46f | 2010-11-24 13:07:51 +0100 | [diff] [blame] | 112 | if (!cmd) |
| 113 | return NULL; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 114 | /* |
| 115 | * Some commands allow length modifiers (like "cp.b"); |
| 116 | * compare command name only until first dot. |
| 117 | */ |
| 118 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); |
| 119 | |
Kumar Gala | ac2dad6 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 120 | for (cmdtp = table; |
| 121 | cmdtp != table + table_len; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 122 | cmdtp++) { |
| 123 | if (strncmp (cmd, cmdtp->name, len) == 0) { |
| 124 | if (len == strlen (cmdtp->name)) |
| 125 | return cmdtp; /* full match */ |
| 126 | |
| 127 | cmdtp_temp = cmdtp; /* abbreviated command ? */ |
| 128 | n_found++; |
| 129 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 130 | } |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 131 | if (n_found == 1) { /* exactly one match */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | return cmdtp_temp; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 133 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 134 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 135 | return NULL; /* not found or ambiguous command */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 136 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 137 | |
Kumar Gala | ac2dad6 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 138 | cmd_tbl_t *find_cmd (const char *cmd) |
| 139 | { |
| 140 | int len = &__u_boot_cmd_end - &__u_boot_cmd_start; |
| 141 | return find_cmd_tbl(cmd, &__u_boot_cmd_start, len); |
| 142 | } |
| 143 | |
Mike Frysinger | 33e84d1 | 2011-04-23 23:43:24 +0000 | [diff] [blame] | 144 | int cmd_usage(const cmd_tbl_t *cmdtp) |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 145 | { |
Wolfgang Denk | 9dd775b | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 146 | printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); |
| 147 | |
| 148 | #ifdef CONFIG_SYS_LONGHELP |
| 149 | printf("Usage:\n%s ", cmdtp->name); |
| 150 | |
| 151 | if (!cmdtp->help) { |
| 152 | puts ("- No additional help available.\n"); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | puts (cmdtp->help); |
| 157 | putc ('\n'); |
| 158 | #endif /* CONFIG_SYS_LONGHELP */ |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 159 | return 1; |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 160 | } |
| 161 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 162 | #ifdef CONFIG_AUTO_COMPLETE |
| 163 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 164 | int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 165 | { |
| 166 | static char tmp_buf[512]; |
| 167 | int space; |
| 168 | |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 169 | space = last_char == '\0' || isblank(last_char); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 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); |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 176 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 180 | /*************************************************************************************/ |
| 181 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 182 | static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 183 | { |
| 184 | cmd_tbl_t *cmdtp; |
| 185 | const char *p; |
| 186 | int len, clen; |
| 187 | int n_found = 0; |
| 188 | const char *cmd; |
| 189 | |
| 190 | /* sanity? */ |
| 191 | if (maxv < 2) |
| 192 | return -2; |
| 193 | |
| 194 | cmdv[0] = NULL; |
| 195 | |
| 196 | if (argc == 0) { |
| 197 | /* output full list of commands */ |
| 198 | for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { |
| 199 | if (n_found >= maxv - 2) { |
| 200 | cmdv[n_found++] = "..."; |
| 201 | break; |
| 202 | } |
| 203 | cmdv[n_found++] = cmdtp->name; |
| 204 | } |
| 205 | cmdv[n_found] = NULL; |
| 206 | return n_found; |
| 207 | } |
| 208 | |
| 209 | /* more than one arg or one but the start of the next */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 210 | if (argc > 1 || (last_char == '\0' || isblank(last_char))) { |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 211 | cmdtp = find_cmd(argv[0]); |
| 212 | if (cmdtp == NULL || cmdtp->complete == NULL) { |
| 213 | cmdv[0] = NULL; |
| 214 | return 0; |
| 215 | } |
| 216 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); |
| 217 | } |
| 218 | |
| 219 | cmd = argv[0]; |
| 220 | /* |
| 221 | * Some commands allow length modifiers (like "cp.b"); |
| 222 | * compare command name only until first dot. |
| 223 | */ |
| 224 | p = strchr(cmd, '.'); |
| 225 | if (p == NULL) |
| 226 | len = strlen(cmd); |
| 227 | else |
| 228 | len = p - cmd; |
| 229 | |
| 230 | /* return the partial matches */ |
| 231 | for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { |
| 232 | |
| 233 | clen = strlen(cmdtp->name); |
| 234 | if (clen < len) |
| 235 | continue; |
| 236 | |
| 237 | if (memcmp(cmd, cmdtp->name, len) != 0) |
| 238 | continue; |
| 239 | |
| 240 | /* too many! */ |
| 241 | if (n_found >= maxv - 2) { |
| 242 | cmdv[n_found++] = "..."; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | cmdv[n_found++] = cmdtp->name; |
| 247 | } |
| 248 | |
| 249 | cmdv[n_found] = NULL; |
| 250 | return n_found; |
| 251 | } |
| 252 | |
| 253 | static int make_argv(char *s, int argvsz, char *argv[]) |
| 254 | { |
| 255 | int argc = 0; |
| 256 | |
| 257 | /* split into argv */ |
| 258 | while (argc < argvsz - 1) { |
| 259 | |
| 260 | /* skip any white space */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 261 | while (isblank(*s)) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 262 | ++s; |
| 263 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 264 | if (*s == '\0') /* end of s, no more args */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 265 | break; |
| 266 | |
| 267 | argv[argc++] = s; /* begin of argument string */ |
| 268 | |
| 269 | /* find end of string */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 270 | while (*s && !isblank(*s)) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 271 | ++s; |
| 272 | |
| 273 | if (*s == '\0') /* end of s, no more args */ |
| 274 | break; |
| 275 | |
| 276 | *s++ = '\0'; /* terminate current arg */ |
| 277 | } |
| 278 | argv[argc] = NULL; |
| 279 | |
| 280 | return argc; |
| 281 | } |
| 282 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 283 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 284 | { |
| 285 | int ll = leader != NULL ? strlen(leader) : 0; |
| 286 | int sl = sep != NULL ? strlen(sep) : 0; |
| 287 | int len, i; |
| 288 | |
| 289 | if (banner) { |
| 290 | puts("\n"); |
| 291 | puts(banner); |
| 292 | } |
| 293 | |
| 294 | i = linemax; /* force leader and newline */ |
| 295 | while (*argv != NULL) { |
| 296 | len = strlen(*argv) + sl; |
| 297 | if (i + len >= linemax) { |
| 298 | puts("\n"); |
| 299 | if (leader) |
| 300 | puts(leader); |
| 301 | i = ll - sl; |
| 302 | } else if (sep) |
| 303 | puts(sep); |
| 304 | puts(*argv++); |
| 305 | i += len; |
| 306 | } |
| 307 | printf("\n"); |
| 308 | } |
| 309 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 310 | static int find_common_prefix(char * const argv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 311 | { |
| 312 | int i, len; |
| 313 | char *anchor, *s, *t; |
| 314 | |
| 315 | if (*argv == NULL) |
| 316 | return 0; |
| 317 | |
| 318 | /* begin with max */ |
| 319 | anchor = *argv++; |
| 320 | len = strlen(anchor); |
| 321 | while ((t = *argv++) != NULL) { |
| 322 | s = anchor; |
| 323 | for (i = 0; i < len; i++, t++, s++) { |
| 324 | if (*t != *s) |
| 325 | break; |
| 326 | } |
| 327 | len = s - anchor; |
| 328 | } |
| 329 | return len; |
| 330 | } |
| 331 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 332 | static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 333 | |
| 334 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) |
| 335 | { |
| 336 | int n = *np, col = *colp; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 337 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 338 | char *cmdv[20]; |
| 339 | char *s, *t; |
| 340 | const char *sep; |
| 341 | int i, j, k, len, seplen, argc; |
| 342 | int cnt; |
| 343 | char last_char; |
| 344 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 345 | if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 346 | return 0; /* not in normal console */ |
| 347 | |
| 348 | cnt = strlen(buf); |
| 349 | if (cnt >= 1) |
| 350 | last_char = buf[cnt - 1]; |
| 351 | else |
| 352 | last_char = '\0'; |
| 353 | |
| 354 | /* copy to secondary buffer which will be affected */ |
| 355 | strcpy(tmp_buf, buf); |
| 356 | |
| 357 | /* separate into argv */ |
| 358 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); |
| 359 | |
| 360 | /* do the completion and return the possible completions */ |
| 361 | i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv); |
| 362 | |
| 363 | /* no match; bell and out */ |
| 364 | if (i == 0) { |
| 365 | if (argc > 1) /* allow tab for non command */ |
| 366 | return 0; |
| 367 | putc('\a'); |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | s = NULL; |
| 372 | len = 0; |
| 373 | sep = NULL; |
| 374 | seplen = 0; |
| 375 | if (i == 1) { /* one match; perfect */ |
| 376 | k = strlen(argv[argc - 1]); |
| 377 | s = cmdv[0] + k; |
| 378 | len = strlen(s); |
| 379 | sep = " "; |
| 380 | seplen = 1; |
| 381 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ |
| 382 | k = strlen(argv[argc - 1]); |
| 383 | j -= k; |
| 384 | if (j > 0) { |
| 385 | s = cmdv[0] + k; |
| 386 | len = j; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (s != NULL) { |
| 391 | k = len + seplen; |
| 392 | /* make sure it fits */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 393 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 394 | putc('\a'); |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | t = buf + cnt; |
| 399 | for (i = 0; i < len; i++) |
| 400 | *t++ = *s++; |
| 401 | if (sep != NULL) |
| 402 | for (i = 0; i < seplen; i++) |
| 403 | *t++ = sep[i]; |
| 404 | *t = '\0'; |
| 405 | n += k; |
| 406 | col += k; |
| 407 | puts(t - k); |
| 408 | if (sep == NULL) |
| 409 | putc('\a'); |
| 410 | *np = n; |
| 411 | *colp = col; |
| 412 | } else { |
| 413 | print_argv(NULL, " ", " ", 78, cmdv); |
| 414 | |
| 415 | puts(prompt); |
| 416 | puts(buf); |
| 417 | } |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 82290f2 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 422 | |
| 423 | #ifdef CMD_DATA_SIZE |
| 424 | int cmd_get_data_size(char* arg, int default_size) |
| 425 | { |
| 426 | /* Check for a size specification .b, .w or .l. |
| 427 | */ |
| 428 | int len = strlen(arg); |
| 429 | if (len > 2 && arg[len-2] == '.') { |
| 430 | switch(arg[len-1]) { |
| 431 | case 'b': |
| 432 | return 1; |
| 433 | case 'w': |
| 434 | return 2; |
| 435 | case 'l': |
| 436 | return 4; |
| 437 | case 's': |
| 438 | return -2; |
| 439 | default: |
| 440 | return -1; |
| 441 | } |
| 442 | } |
| 443 | return default_size; |
| 444 | } |
| 445 | #endif |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 446 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 447 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 448 | DECLARE_GLOBAL_DATA_PTR; |
| 449 | |
| 450 | void fixup_cmdtable(cmd_tbl_t *cmdtp, int size) |
| 451 | { |
| 452 | int i; |
| 453 | |
| 454 | if (gd->reloc_off == 0) |
| 455 | return; |
| 456 | |
| 457 | for (i = 0; i < size; i++) { |
| 458 | ulong addr; |
| 459 | |
| 460 | addr = (ulong) (cmdtp->cmd) + gd->reloc_off; |
| 461 | #if DEBUG_COMMANDS |
| 462 | printf("Command \"%s\": 0x%08lx => 0x%08lx\n", |
| 463 | cmdtp->name, (ulong) (cmdtp->cmd), addr); |
| 464 | #endif |
| 465 | cmdtp->cmd = |
| 466 | (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr; |
| 467 | addr = (ulong)(cmdtp->name) + gd->reloc_off; |
| 468 | cmdtp->name = (char *)addr; |
| 469 | if (cmdtp->usage) { |
| 470 | addr = (ulong)(cmdtp->usage) + gd->reloc_off; |
| 471 | cmdtp->usage = (char *)addr; |
| 472 | } |
| 473 | #ifdef CONFIG_SYS_LONGHELP |
| 474 | if (cmdtp->help) { |
| 475 | addr = (ulong)(cmdtp->help) + gd->reloc_off; |
| 476 | cmdtp->help = (char *)addr; |
| 477 | } |
| 478 | #endif |
Daniel Schwierzeck | e516622 | 2011-10-13 06:07:39 +0000 | [diff] [blame] | 479 | #ifdef CONFIG_AUTO_COMPLETE |
| 480 | if (cmdtp->complete) { |
| 481 | addr = (ulong)(cmdtp->complete) + gd->reloc_off; |
Daniel Schwierzeck | 76deb6c | 2011-10-18 11:12:22 +0000 | [diff] [blame] | 482 | cmdtp->complete = |
| 483 | (int (*)(int, char * const [], char, int, char * []))addr; |
Daniel Schwierzeck | e516622 | 2011-10-13 06:07:39 +0000 | [diff] [blame] | 484 | } |
| 485 | #endif |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 486 | cmdtp++; |
| 487 | } |
| 488 | } |
| 489 | #endif |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 490 | |
| 491 | /** |
| 492 | * Call a command function. This should be the only route in U-Boot to call |
| 493 | * a command, so that we can track whether we are waiting for input or |
| 494 | * executing a command. |
| 495 | * |
| 496 | * @param cmdtp Pointer to the command to execute |
| 497 | * @param flag Some flags normally 0 (see CMD_FLAG_.. above) |
| 498 | * @param argc Number of arguments (arg 0 must be the command text) |
| 499 | * @param argv Arguments |
| 500 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) |
| 501 | */ |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 502 | static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 503 | { |
| 504 | int result; |
| 505 | |
| 506 | result = (cmdtp->cmd)(cmdtp, flag, argc, argv); |
| 507 | if (result) |
| 508 | debug("Command failed, result=%d", result); |
| 509 | return result; |
| 510 | } |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 511 | |
| 512 | enum command_ret_t cmd_process(int flag, int argc, char * const argv[], |
| 513 | int *repeatable) |
| 514 | { |
| 515 | enum command_ret_t rc = CMD_RET_SUCCESS; |
| 516 | cmd_tbl_t *cmdtp; |
| 517 | |
| 518 | /* Look up command in command table */ |
| 519 | cmdtp = find_cmd(argv[0]); |
| 520 | if (cmdtp == NULL) { |
| 521 | printf("Unknown command '%s' - try 'help'\n", argv[0]); |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | /* found - check max args */ |
| 526 | if (argc > cmdtp->maxargs) |
| 527 | rc = CMD_RET_USAGE; |
| 528 | |
| 529 | #if defined(CONFIG_CMD_BOOTD) |
| 530 | /* avoid "bootd" recursion */ |
| 531 | else if (cmdtp->cmd == do_bootd) { |
| 532 | if (flag & CMD_FLAG_BOOTD) { |
| 533 | puts("'bootd' recursion detected\n"); |
| 534 | rc = CMD_RET_FAILURE; |
| 535 | } else { |
| 536 | flag |= CMD_FLAG_BOOTD; |
| 537 | } |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | /* If OK so far, then do the command */ |
| 542 | if (!rc) { |
| 543 | rc = cmd_call(cmdtp, flag, argc, argv); |
| 544 | *repeatable &= cmdtp->repeatable; |
| 545 | } |
| 546 | if (rc == CMD_RET_USAGE) |
| 547 | rc = cmd_usage(cmdtp); |
| 548 | return rc; |
| 549 | } |