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