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