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