wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 1 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2003 |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Command Processor Table |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 30 | |
| 31 | int |
| 32 | do_version (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 33 | { |
| 34 | extern char version_string[]; |
| 35 | printf ("\n%s\n", version_string); |
| 36 | return 0; |
| 37 | } |
| 38 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 39 | U_BOOT_CMD( |
| 40 | version, 1, 1, do_version, |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 41 | "version - print monitor version\n", |
| 42 | NULL |
| 43 | ); |
| 44 | |
Jon Loeliger | 052fc84 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 45 | #if defined(CONFIG_CMD_ECHO) |
Wolfgang Denk | 582f3dd | 2006-03-12 16:51:59 +0100 | [diff] [blame] | 46 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 47 | int |
| 48 | do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 49 | { |
| 50 | int i, putnl = 1; |
| 51 | |
| 52 | for (i = 1; i < argc; i++) { |
| 53 | char *p = argv[i], c; |
| 54 | |
| 55 | if (i > 1) |
| 56 | putc(' '); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 57 | while ((c = *p++) != '\0') { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 58 | if (c == '\\' && *p == 'c') { |
| 59 | putnl = 0; |
| 60 | p++; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 61 | } else { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 62 | putc(c); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (putnl) |
| 68 | putc('\n'); |
| 69 | return 0; |
| 70 | } |
| 71 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 72 | U_BOOT_CMD( |
| 73 | echo, CFG_MAXARGS, 1, do_echo, |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 74 | "echo - echo args to console\n", |
| 75 | "[args..]\n" |
| 76 | " - echo args to console; \\c suppresses newline\n" |
| 77 | ); |
| 78 | |
Wolfgang Denk | 582f3dd | 2006-03-12 16:51:59 +0100 | [diff] [blame] | 79 | #endif /* CFG_CMD_ECHO */ |
| 80 | |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 81 | #ifdef CFG_HUSH_PARSER |
| 82 | |
| 83 | int |
| 84 | do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 85 | { |
| 86 | char **ap; |
| 87 | int left, adv, expr, last_expr, neg, last_cmp; |
| 88 | |
| 89 | /* args? */ |
| 90 | if (argc < 3) |
| 91 | return 1; |
| 92 | |
| 93 | #if 0 |
| 94 | { |
| 95 | printf("test:"); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 96 | left = 1; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 97 | while (argv[left]) |
| 98 | printf(" %s", argv[left++]); |
| 99 | } |
| 100 | #endif |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 101 | |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 102 | last_expr = 0; |
| 103 | left = argc - 1; ap = argv + 1; |
| 104 | if (left > 0 && strcmp(ap[0], "!") == 0) { |
| 105 | neg = 1; |
| 106 | ap++; |
| 107 | left--; |
| 108 | } else |
| 109 | neg = 0; |
| 110 | |
| 111 | expr = -1; |
| 112 | last_cmp = -1; |
| 113 | last_expr = -1; |
| 114 | while (left > 0) { |
| 115 | |
| 116 | if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0) |
| 117 | adv = 1; |
| 118 | else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0) |
| 119 | adv = 2; |
| 120 | else |
| 121 | adv = 3; |
| 122 | |
| 123 | if (left < adv) { |
| 124 | expr = 1; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | if (adv == 1) { |
| 129 | if (strcmp(ap[0], "-o") == 0) { |
| 130 | last_expr = expr; |
| 131 | last_cmp = 0; |
| 132 | } else if (strcmp(ap[0], "-a") == 0) { |
| 133 | last_expr = expr; |
| 134 | last_cmp = 1; |
| 135 | } else { |
| 136 | expr = 1; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (adv == 2) { |
| 142 | if (strcmp(ap[0], "-z") == 0) |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 143 | expr = strlen(ap[1]) == 0 ? 1 : 0; |
wdenk | 2a83161 | 2005-04-06 00:04:16 +0000 | [diff] [blame] | 144 | else if (strcmp(ap[0], "-n") == 0) |
| 145 | expr = strlen(ap[1]) == 0 ? 0 : 1; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 146 | else { |
| 147 | expr = 1; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (last_cmp == 0) |
| 152 | expr = last_expr || expr; |
| 153 | else if (last_cmp == 1) |
| 154 | expr = last_expr && expr; |
| 155 | last_cmp = -1; |
| 156 | } |
| 157 | |
| 158 | if (adv == 3) { |
| 159 | if (strcmp(ap[1], "=") == 0) |
| 160 | expr = strcmp(ap[0], ap[2]) == 0; |
| 161 | else if (strcmp(ap[1], "!=") == 0) |
| 162 | expr = strcmp(ap[0], ap[2]) != 0; |
| 163 | else if (strcmp(ap[1], ">") == 0) |
| 164 | expr = strcmp(ap[0], ap[2]) > 0; |
| 165 | else if (strcmp(ap[1], "<") == 0) |
| 166 | expr = strcmp(ap[0], ap[2]) < 0; |
| 167 | else if (strcmp(ap[1], "-eq") == 0) |
| 168 | expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10); |
| 169 | else if (strcmp(ap[1], "-ne") == 0) |
| 170 | expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10); |
| 171 | else if (strcmp(ap[1], "-lt") == 0) |
| 172 | expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10); |
| 173 | else if (strcmp(ap[1], "-le") == 0) |
| 174 | expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10); |
| 175 | else if (strcmp(ap[1], "-gt") == 0) |
| 176 | expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10); |
| 177 | else if (strcmp(ap[1], "-ge") == 0) |
| 178 | expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10); |
| 179 | else { |
| 180 | expr = 1; |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | if (last_cmp == 0) |
| 185 | expr = last_expr || expr; |
| 186 | else if (last_cmp == 1) |
| 187 | expr = last_expr && expr; |
| 188 | last_cmp = -1; |
| 189 | } |
| 190 | |
| 191 | ap += adv; left -= adv; |
| 192 | } |
| 193 | |
| 194 | if (neg) |
| 195 | expr = !expr; |
| 196 | |
| 197 | expr = !expr; |
| 198 | |
| 199 | #if 0 |
| 200 | printf(": returns %d\n", expr); |
| 201 | #endif |
| 202 | |
| 203 | return expr; |
| 204 | } |
| 205 | |
| 206 | U_BOOT_CMD( |
| 207 | test, CFG_MAXARGS, 1, do_test, |
| 208 | "test - minimal test like /bin/sh\n", |
| 209 | "[args..]\n" |
| 210 | " - test functionality\n" |
| 211 | ); |
| 212 | |
| 213 | int |
| 214 | do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 215 | { |
| 216 | int r; |
| 217 | |
| 218 | r = 0; |
| 219 | if (argc > 1) |
| 220 | r = simple_strtoul(argv[1], NULL, 10); |
| 221 | |
| 222 | return -r - 2; |
| 223 | } |
| 224 | |
| 225 | U_BOOT_CMD( |
| 226 | exit, 2, 1, do_exit, |
| 227 | "exit - exit script\n", |
| 228 | " - exit functionality\n" |
| 229 | ); |
| 230 | |
| 231 | |
| 232 | #endif |
| 233 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 234 | /* |
| 235 | * Use puts() instead of printf() to avoid printf buffer overflow |
| 236 | * for long help messages |
| 237 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 238 | int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 239 | { |
| 240 | int i; |
| 241 | int rcode = 0; |
| 242 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 243 | if (argc == 1) { /*show list of commands */ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 244 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 245 | int cmd_items = &__u_boot_cmd_end - |
| 246 | &__u_boot_cmd_start; /* pointer arith! */ |
| 247 | cmd_tbl_t *cmd_array[cmd_items]; |
| 248 | int i, j, swaps; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 249 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 250 | /* Make array of commands from .uboot_cmd section */ |
| 251 | cmdtp = &__u_boot_cmd_start; |
| 252 | for (i = 0; i < cmd_items; i++) { |
| 253 | cmd_array[i] = cmdtp++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 254 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 255 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 256 | /* Sort command list (trivial bubble sort) */ |
| 257 | for (i = cmd_items - 1; i > 0; --i) { |
| 258 | swaps = 0; |
| 259 | for (j = 0; j < i; ++j) { |
| 260 | if (strcmp (cmd_array[j]->name, |
| 261 | cmd_array[j + 1]->name) > 0) { |
| 262 | cmd_tbl_t *tmp; |
| 263 | tmp = cmd_array[j]; |
| 264 | cmd_array[j] = cmd_array[j + 1]; |
| 265 | cmd_array[j + 1] = tmp; |
| 266 | ++swaps; |
| 267 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 268 | } |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 269 | if (!swaps) |
| 270 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* print short help (usage) */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 274 | for (i = 0; i < cmd_items; i++) { |
| 275 | const char *usage = cmd_array[i]->usage; |
| 276 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 277 | /* allow user abort */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 278 | if (ctrlc ()) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 279 | return 1; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 280 | if (usage == NULL) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 281 | continue; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 282 | puts (usage); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 283 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 286 | /* |
| 287 | * command help (long version) |
| 288 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 289 | for (i = 1; i < argc; ++i) { |
| 290 | if ((cmdtp = find_cmd (argv[i])) != NULL) { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 291 | #ifdef CFG_LONGHELP |
| 292 | /* found - print (long) help info */ |
| 293 | puts (cmdtp->name); |
| 294 | putc (' '); |
| 295 | if (cmdtp->help) { |
| 296 | puts (cmdtp->help); |
| 297 | } else { |
| 298 | puts ("- No help available.\n"); |
| 299 | rcode = 1; |
| 300 | } |
| 301 | putc ('\n'); |
| 302 | #else /* no long help available */ |
| 303 | if (cmdtp->usage) |
| 304 | puts (cmdtp->usage); |
| 305 | #endif /* CFG_LONGHELP */ |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 306 | } else { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 307 | printf ("Unknown command '%s' - try 'help'" |
| 308 | " without arguments for list of all" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 309 | " known commands\n\n", argv[i] |
| 310 | ); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 311 | rcode = 1; |
| 312 | } |
| 313 | } |
| 314 | return rcode; |
| 315 | } |
| 316 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 317 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 318 | U_BOOT_CMD( |
| 319 | help, CFG_MAXARGS, 1, do_help, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 320 | "help - print online help\n", |
| 321 | "[command ...]\n" |
| 322 | " - show help information (for 'command')\n" |
| 323 | "'help' prints online help for the monitor commands.\n\n" |
| 324 | "Without arguments, it prints a short usage message for all commands.\n\n" |
| 325 | "To get detailed help information for specific commands you can type\n" |
| 326 | "'help' with one or more command names as arguments.\n" |
| 327 | ); |
| 328 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 329 | /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */ |
| 330 | #ifdef CFG_LONGHELP |
| 331 | cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 332 | "?", CFG_MAXARGS, 1, do_help, |
| 333 | "? - alias for 'help'\n", |
| 334 | NULL |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 335 | }; |
| 336 | #else |
| 337 | cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = { |
| 338 | "?", CFG_MAXARGS, 1, do_help, |
| 339 | "? - alias for 'help'\n" |
| 340 | }; |
| 341 | #endif /* CFG_LONGHELP */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 342 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 343 | /*************************************************************************** |
| 344 | * find command table entry for a command |
| 345 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 346 | cmd_tbl_t *find_cmd (const char *cmd) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 347 | { |
| 348 | cmd_tbl_t *cmdtp; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 349 | cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 350 | const char *p; |
| 351 | int len; |
| 352 | int n_found = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 353 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 354 | /* |
| 355 | * Some commands allow length modifiers (like "cp.b"); |
| 356 | * compare command name only until first dot. |
| 357 | */ |
| 358 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); |
| 359 | |
| 360 | for (cmdtp = &__u_boot_cmd_start; |
| 361 | cmdtp != &__u_boot_cmd_end; |
| 362 | cmdtp++) { |
| 363 | if (strncmp (cmd, cmdtp->name, len) == 0) { |
| 364 | if (len == strlen (cmdtp->name)) |
| 365 | return cmdtp; /* full match */ |
| 366 | |
| 367 | cmdtp_temp = cmdtp; /* abbreviated command ? */ |
| 368 | n_found++; |
| 369 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 370 | } |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 371 | if (n_found == 1) { /* exactly one match */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 372 | return cmdtp_temp; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 373 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 374 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 375 | return NULL; /* not found or ambiguous command */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 376 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 377 | |
| 378 | #ifdef CONFIG_AUTO_COMPLETE |
| 379 | |
| 380 | int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[]) |
| 381 | { |
| 382 | static char tmp_buf[512]; |
| 383 | int space; |
| 384 | |
| 385 | space = last_char == '\0' || last_char == ' ' || last_char == '\t'; |
| 386 | |
| 387 | if (space && argc == 1) |
| 388 | return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf); |
| 389 | |
| 390 | if (!space && argc == 2) |
| 391 | return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf); |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static void install_auto_complete_handler(const char *cmd, |
| 397 | int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[])) |
| 398 | { |
| 399 | cmd_tbl_t *cmdtp; |
| 400 | |
| 401 | cmdtp = find_cmd(cmd); |
| 402 | if (cmdtp == NULL) |
| 403 | return; |
| 404 | |
| 405 | cmdtp->complete = complete; |
| 406 | } |
| 407 | |
| 408 | void install_auto_complete(void) |
| 409 | { |
| 410 | install_auto_complete_handler("printenv", var_complete); |
| 411 | install_auto_complete_handler("setenv", var_complete); |
Jon Loeliger | 052fc84 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 412 | #if defined(CONFIG_CMD_RUN) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 413 | install_auto_complete_handler("run", var_complete); |
| 414 | #endif |
| 415 | } |
| 416 | |
| 417 | /*************************************************************************************/ |
| 418 | |
| 419 | static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[]) |
| 420 | { |
| 421 | cmd_tbl_t *cmdtp; |
| 422 | const char *p; |
| 423 | int len, clen; |
| 424 | int n_found = 0; |
| 425 | const char *cmd; |
| 426 | |
| 427 | /* sanity? */ |
| 428 | if (maxv < 2) |
| 429 | return -2; |
| 430 | |
| 431 | cmdv[0] = NULL; |
| 432 | |
| 433 | if (argc == 0) { |
| 434 | /* output full list of commands */ |
| 435 | for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { |
| 436 | if (n_found >= maxv - 2) { |
| 437 | cmdv[n_found++] = "..."; |
| 438 | break; |
| 439 | } |
| 440 | cmdv[n_found++] = cmdtp->name; |
| 441 | } |
| 442 | cmdv[n_found] = NULL; |
| 443 | return n_found; |
| 444 | } |
| 445 | |
| 446 | /* more than one arg or one but the start of the next */ |
| 447 | if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) { |
| 448 | cmdtp = find_cmd(argv[0]); |
| 449 | if (cmdtp == NULL || cmdtp->complete == NULL) { |
| 450 | cmdv[0] = NULL; |
| 451 | return 0; |
| 452 | } |
| 453 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); |
| 454 | } |
| 455 | |
| 456 | cmd = argv[0]; |
| 457 | /* |
| 458 | * Some commands allow length modifiers (like "cp.b"); |
| 459 | * compare command name only until first dot. |
| 460 | */ |
| 461 | p = strchr(cmd, '.'); |
| 462 | if (p == NULL) |
| 463 | len = strlen(cmd); |
| 464 | else |
| 465 | len = p - cmd; |
| 466 | |
| 467 | /* return the partial matches */ |
| 468 | for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { |
| 469 | |
| 470 | clen = strlen(cmdtp->name); |
| 471 | if (clen < len) |
| 472 | continue; |
| 473 | |
| 474 | if (memcmp(cmd, cmdtp->name, len) != 0) |
| 475 | continue; |
| 476 | |
| 477 | /* too many! */ |
| 478 | if (n_found >= maxv - 2) { |
| 479 | cmdv[n_found++] = "..."; |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | cmdv[n_found++] = cmdtp->name; |
| 484 | } |
| 485 | |
| 486 | cmdv[n_found] = NULL; |
| 487 | return n_found; |
| 488 | } |
| 489 | |
| 490 | static int make_argv(char *s, int argvsz, char *argv[]) |
| 491 | { |
| 492 | int argc = 0; |
| 493 | |
| 494 | /* split into argv */ |
| 495 | while (argc < argvsz - 1) { |
| 496 | |
| 497 | /* skip any white space */ |
| 498 | while ((*s == ' ') || (*s == '\t')) |
| 499 | ++s; |
| 500 | |
| 501 | if (*s == '\0') /* end of s, no more args */ |
| 502 | break; |
| 503 | |
| 504 | argv[argc++] = s; /* begin of argument string */ |
| 505 | |
| 506 | /* find end of string */ |
| 507 | while (*s && (*s != ' ') && (*s != '\t')) |
| 508 | ++s; |
| 509 | |
| 510 | if (*s == '\0') /* end of s, no more args */ |
| 511 | break; |
| 512 | |
| 513 | *s++ = '\0'; /* terminate current arg */ |
| 514 | } |
| 515 | argv[argc] = NULL; |
| 516 | |
| 517 | return argc; |
| 518 | } |
| 519 | |
| 520 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[]) |
| 521 | { |
| 522 | int ll = leader != NULL ? strlen(leader) : 0; |
| 523 | int sl = sep != NULL ? strlen(sep) : 0; |
| 524 | int len, i; |
| 525 | |
| 526 | if (banner) { |
| 527 | puts("\n"); |
| 528 | puts(banner); |
| 529 | } |
| 530 | |
| 531 | i = linemax; /* force leader and newline */ |
| 532 | while (*argv != NULL) { |
| 533 | len = strlen(*argv) + sl; |
| 534 | if (i + len >= linemax) { |
| 535 | puts("\n"); |
| 536 | if (leader) |
| 537 | puts(leader); |
| 538 | i = ll - sl; |
| 539 | } else if (sep) |
| 540 | puts(sep); |
| 541 | puts(*argv++); |
| 542 | i += len; |
| 543 | } |
| 544 | printf("\n"); |
| 545 | } |
| 546 | |
| 547 | static int find_common_prefix(char *argv[]) |
| 548 | { |
| 549 | int i, len; |
| 550 | char *anchor, *s, *t; |
| 551 | |
| 552 | if (*argv == NULL) |
| 553 | return 0; |
| 554 | |
| 555 | /* begin with max */ |
| 556 | anchor = *argv++; |
| 557 | len = strlen(anchor); |
| 558 | while ((t = *argv++) != NULL) { |
| 559 | s = anchor; |
| 560 | for (i = 0; i < len; i++, t++, s++) { |
| 561 | if (*t != *s) |
| 562 | break; |
| 563 | } |
| 564 | len = s - anchor; |
| 565 | } |
| 566 | return len; |
| 567 | } |
| 568 | |
| 569 | static char tmp_buf[CFG_CBSIZE]; /* copy of console I/O buffer */ |
| 570 | |
| 571 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) |
| 572 | { |
| 573 | int n = *np, col = *colp; |
| 574 | char *argv[CFG_MAXARGS + 1]; /* NULL terminated */ |
| 575 | char *cmdv[20]; |
| 576 | char *s, *t; |
| 577 | const char *sep; |
| 578 | int i, j, k, len, seplen, argc; |
| 579 | int cnt; |
| 580 | char last_char; |
| 581 | |
| 582 | if (strcmp(prompt, CFG_PROMPT) != 0) |
| 583 | return 0; /* not in normal console */ |
| 584 | |
| 585 | cnt = strlen(buf); |
| 586 | if (cnt >= 1) |
| 587 | last_char = buf[cnt - 1]; |
| 588 | else |
| 589 | last_char = '\0'; |
| 590 | |
| 591 | /* copy to secondary buffer which will be affected */ |
| 592 | strcpy(tmp_buf, buf); |
| 593 | |
| 594 | /* separate into argv */ |
| 595 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); |
| 596 | |
| 597 | /* do the completion and return the possible completions */ |
| 598 | i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv); |
| 599 | |
| 600 | /* no match; bell and out */ |
| 601 | if (i == 0) { |
| 602 | if (argc > 1) /* allow tab for non command */ |
| 603 | return 0; |
| 604 | putc('\a'); |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | s = NULL; |
| 609 | len = 0; |
| 610 | sep = NULL; |
| 611 | seplen = 0; |
| 612 | if (i == 1) { /* one match; perfect */ |
| 613 | k = strlen(argv[argc - 1]); |
| 614 | s = cmdv[0] + k; |
| 615 | len = strlen(s); |
| 616 | sep = " "; |
| 617 | seplen = 1; |
| 618 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ |
| 619 | k = strlen(argv[argc - 1]); |
| 620 | j -= k; |
| 621 | if (j > 0) { |
| 622 | s = cmdv[0] + k; |
| 623 | len = j; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | if (s != NULL) { |
| 628 | k = len + seplen; |
| 629 | /* make sure it fits */ |
| 630 | if (n + k >= CFG_CBSIZE - 2) { |
| 631 | putc('\a'); |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | t = buf + cnt; |
| 636 | for (i = 0; i < len; i++) |
| 637 | *t++ = *s++; |
| 638 | if (sep != NULL) |
| 639 | for (i = 0; i < seplen; i++) |
| 640 | *t++ = sep[i]; |
| 641 | *t = '\0'; |
| 642 | n += k; |
| 643 | col += k; |
| 644 | puts(t - k); |
| 645 | if (sep == NULL) |
| 646 | putc('\a'); |
| 647 | *np = n; |
| 648 | *colp = col; |
| 649 | } else { |
| 650 | print_argv(NULL, " ", " ", 78, cmdv); |
| 651 | |
| 652 | puts(prompt); |
| 653 | puts(buf); |
| 654 | } |
| 655 | return 1; |
| 656 | } |
| 657 | |
| 658 | #endif |