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