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