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