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