Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * Add to readline cmdline-editing by |
| 7 | * (C) Copyright 2005 |
| 8 | * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 0098e17 | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 12 | #include <bootretry.h> |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 13 | #include <cli.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | #include <command.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 15 | #include <time.h> |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 16 | #include <watchdog.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | static const char erase_seq[] = "\b \b"; /* erase sequence */ |
| 22 | static const char tab_seq[] = " "; /* used to expand TABs */ |
| 23 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 24 | char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ |
| 25 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 26 | static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) |
| 27 | { |
| 28 | char *s; |
| 29 | |
| 30 | if (*np == 0) |
| 31 | return p; |
| 32 | |
| 33 | if (*(--p) == '\t') { /* will retype the whole line */ |
| 34 | while (*colp > plen) { |
| 35 | puts(erase_seq); |
| 36 | (*colp)--; |
| 37 | } |
| 38 | for (s = buffer; s < p; ++s) { |
| 39 | if (*s == '\t') { |
| 40 | puts(tab_seq + ((*colp) & 07)); |
| 41 | *colp += 8 - ((*colp) & 07); |
| 42 | } else { |
| 43 | ++(*colp); |
| 44 | putc(*s); |
| 45 | } |
| 46 | } |
| 47 | } else { |
| 48 | puts(erase_seq); |
| 49 | (*colp)--; |
| 50 | } |
| 51 | (*np)--; |
| 52 | |
| 53 | return p; |
| 54 | } |
| 55 | |
| 56 | #ifdef CONFIG_CMDLINE_EDITING |
| 57 | |
| 58 | /* |
| 59 | * cmdline-editing related codes from vivi. |
| 60 | * Author: Janghoon Lyu <nandy@mizi.com> |
| 61 | */ |
| 62 | |
| 63 | #define putnstr(str, n) printf("%.*s", (int)n, str) |
| 64 | |
| 65 | #define CTL_CH(c) ((c) - 'a' + 1) |
| 66 | #define CTL_BACKSPACE ('\b') |
| 67 | #define DEL ((char)255) |
| 68 | #define DEL7 ((char)127) |
| 69 | #define CREAD_HIST_CHAR ('!') |
| 70 | |
| 71 | #define getcmd_putch(ch) putc(ch) |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 72 | #define getcmd_getch() getchar() |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 73 | #define getcmd_cbeep() getcmd_putch('\a') |
| 74 | |
Sean Anderson | 069f0d7 | 2022-08-30 16:40:37 -0400 | [diff] [blame] | 75 | #ifdef CONFIG_SPL_BUILD |
| 76 | #define HIST_MAX 3 |
| 77 | #define HIST_SIZE 32 |
| 78 | #else |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 79 | #define HIST_MAX 20 |
| 80 | #define HIST_SIZE CONFIG_SYS_CBSIZE |
Sean Anderson | 069f0d7 | 2022-08-30 16:40:37 -0400 | [diff] [blame] | 81 | #endif |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 82 | |
| 83 | static int hist_max; |
| 84 | static int hist_add_idx; |
| 85 | static int hist_cur = -1; |
| 86 | static unsigned hist_num; |
| 87 | |
| 88 | static char *hist_list[HIST_MAX]; |
| 89 | static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ |
| 90 | |
| 91 | #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) |
| 92 | |
| 93 | static void hist_init(void) |
| 94 | { |
| 95 | int i; |
| 96 | |
| 97 | hist_max = 0; |
| 98 | hist_add_idx = 0; |
| 99 | hist_cur = -1; |
| 100 | hist_num = 0; |
| 101 | |
| 102 | for (i = 0; i < HIST_MAX; i++) { |
| 103 | hist_list[i] = hist_lines[i]; |
| 104 | hist_list[i][0] = '\0'; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void cread_add_to_hist(char *line) |
| 109 | { |
| 110 | strcpy(hist_list[hist_add_idx], line); |
| 111 | |
| 112 | if (++hist_add_idx >= HIST_MAX) |
| 113 | hist_add_idx = 0; |
| 114 | |
| 115 | if (hist_add_idx > hist_max) |
| 116 | hist_max = hist_add_idx; |
| 117 | |
| 118 | hist_num++; |
| 119 | } |
| 120 | |
| 121 | static char *hist_prev(void) |
| 122 | { |
| 123 | char *ret; |
| 124 | int old_cur; |
| 125 | |
| 126 | if (hist_cur < 0) |
| 127 | return NULL; |
| 128 | |
| 129 | old_cur = hist_cur; |
| 130 | if (--hist_cur < 0) |
| 131 | hist_cur = hist_max; |
| 132 | |
| 133 | if (hist_cur == hist_add_idx) { |
| 134 | hist_cur = old_cur; |
| 135 | ret = NULL; |
| 136 | } else { |
| 137 | ret = hist_list[hist_cur]; |
| 138 | } |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static char *hist_next(void) |
| 144 | { |
| 145 | char *ret; |
| 146 | |
| 147 | if (hist_cur < 0) |
| 148 | return NULL; |
| 149 | |
| 150 | if (hist_cur == hist_add_idx) |
| 151 | return NULL; |
| 152 | |
| 153 | if (++hist_cur > hist_max) |
| 154 | hist_cur = 0; |
| 155 | |
| 156 | if (hist_cur == hist_add_idx) |
| 157 | ret = ""; |
| 158 | else |
| 159 | ret = hist_list[hist_cur]; |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | #ifndef CONFIG_CMDLINE_EDITING |
| 165 | static void cread_print_hist_list(void) |
| 166 | { |
| 167 | int i; |
| 168 | unsigned long n; |
| 169 | |
| 170 | n = hist_num - hist_max; |
| 171 | |
| 172 | i = hist_add_idx + 1; |
| 173 | while (1) { |
| 174 | if (i > hist_max) |
| 175 | i = 0; |
| 176 | if (i == hist_add_idx) |
| 177 | break; |
| 178 | printf("%s\n", hist_list[i]); |
| 179 | n++; |
| 180 | i++; |
| 181 | } |
| 182 | } |
| 183 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 184 | |
| 185 | #define BEGINNING_OF_LINE() { \ |
| 186 | while (num) { \ |
| 187 | getcmd_putch(CTL_BACKSPACE); \ |
| 188 | num--; \ |
| 189 | } \ |
| 190 | } |
| 191 | |
| 192 | #define ERASE_TO_EOL() { \ |
| 193 | if (num < eol_num) { \ |
| 194 | printf("%*s", (int)(eol_num - num), ""); \ |
| 195 | do { \ |
| 196 | getcmd_putch(CTL_BACKSPACE); \ |
| 197 | } while (--eol_num > num); \ |
| 198 | } \ |
| 199 | } |
| 200 | |
| 201 | #define REFRESH_TO_EOL() { \ |
| 202 | if (num < eol_num) { \ |
| 203 | wlen = eol_num - num; \ |
| 204 | putnstr(buf + num, wlen); \ |
| 205 | num = eol_num; \ |
| 206 | } \ |
| 207 | } |
| 208 | |
| 209 | static void cread_add_char(char ichar, int insert, unsigned long *num, |
| 210 | unsigned long *eol_num, char *buf, unsigned long len) |
| 211 | { |
| 212 | unsigned long wlen; |
| 213 | |
| 214 | /* room ??? */ |
| 215 | if (insert || *num == *eol_num) { |
| 216 | if (*eol_num > len - 1) { |
| 217 | getcmd_cbeep(); |
| 218 | return; |
| 219 | } |
| 220 | (*eol_num)++; |
| 221 | } |
| 222 | |
| 223 | if (insert) { |
| 224 | wlen = *eol_num - *num; |
| 225 | if (wlen > 1) |
| 226 | memmove(&buf[*num+1], &buf[*num], wlen-1); |
| 227 | |
| 228 | buf[*num] = ichar; |
| 229 | putnstr(buf + *num, wlen); |
| 230 | (*num)++; |
| 231 | while (--wlen) |
| 232 | getcmd_putch(CTL_BACKSPACE); |
| 233 | } else { |
| 234 | /* echo the character */ |
| 235 | wlen = 1; |
| 236 | buf[*num] = ichar; |
| 237 | putnstr(buf + *num, wlen); |
| 238 | (*num)++; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static void cread_add_str(char *str, int strsize, int insert, |
| 243 | unsigned long *num, unsigned long *eol_num, |
| 244 | char *buf, unsigned long len) |
| 245 | { |
| 246 | while (strsize--) { |
| 247 | cread_add_char(*str, insert, num, eol_num, buf, len); |
| 248 | str++; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static int cread_line(const char *const prompt, char *buf, unsigned int *len, |
| 253 | int timeout) |
| 254 | { |
| 255 | unsigned long num = 0; |
| 256 | unsigned long eol_num = 0; |
| 257 | unsigned long wlen; |
| 258 | char ichar; |
| 259 | int insert = 1; |
| 260 | int esc_len = 0; |
| 261 | char esc_save[8]; |
| 262 | int init_len = strlen(buf); |
| 263 | int first = 1; |
| 264 | |
| 265 | if (init_len) |
| 266 | cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); |
| 267 | |
| 268 | while (1) { |
Simon Glass | 0098e17 | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 269 | if (bootretry_tstc_timeout()) |
| 270 | return -2; /* timed out */ |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 271 | if (first && timeout) { |
| 272 | uint64_t etime = endtick(timeout); |
| 273 | |
| 274 | while (!tstc()) { /* while no incoming data */ |
| 275 | if (get_ticks() >= etime) |
| 276 | return -2; /* timed out */ |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 277 | schedule(); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 278 | } |
| 279 | first = 0; |
| 280 | } |
| 281 | |
| 282 | ichar = getcmd_getch(); |
| 283 | |
Patrick Delaunay | 555e378 | 2018-08-03 13:38:45 +0200 | [diff] [blame] | 284 | /* ichar=0x0 when error occurs in U-Boot getc */ |
| 285 | if (!ichar) |
| 286 | continue; |
| 287 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 288 | if ((ichar == '\n') || (ichar == '\r')) { |
| 289 | putc('\n'); |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * handle standard linux xterm esc sequences for arrow key, etc. |
| 295 | */ |
| 296 | if (esc_len != 0) { |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 297 | enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT; |
| 298 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 299 | if (esc_len == 1) { |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 300 | if (ichar == '[' || ichar == 'O') |
| 301 | act = ESC_SAVE; |
| 302 | } else if (esc_len == 2) { |
| 303 | switch (ichar) { |
| 304 | case 'D': /* <- key */ |
| 305 | ichar = CTL_CH('b'); |
| 306 | act = ESC_CONVERTED; |
| 307 | break; /* pass off to ^B handler */ |
| 308 | case 'C': /* -> key */ |
| 309 | ichar = CTL_CH('f'); |
| 310 | act = ESC_CONVERTED; |
| 311 | break; /* pass off to ^F handler */ |
| 312 | case 'H': /* Home key */ |
| 313 | ichar = CTL_CH('a'); |
| 314 | act = ESC_CONVERTED; |
| 315 | break; /* pass off to ^A handler */ |
| 316 | case 'F': /* End key */ |
| 317 | ichar = CTL_CH('e'); |
| 318 | act = ESC_CONVERTED; |
| 319 | break; /* pass off to ^E handler */ |
| 320 | case 'A': /* up arrow */ |
| 321 | ichar = CTL_CH('p'); |
| 322 | act = ESC_CONVERTED; |
| 323 | break; /* pass off to ^P handler */ |
| 324 | case 'B': /* down arrow */ |
| 325 | ichar = CTL_CH('n'); |
| 326 | act = ESC_CONVERTED; |
| 327 | break; /* pass off to ^N handler */ |
| 328 | case '1': |
Heinrich Schuchardt | 00fa825 | 2022-02-07 19:14:02 +0100 | [diff] [blame] | 329 | case '2': |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 330 | case '3': |
| 331 | case '4': |
| 332 | case '7': |
| 333 | case '8': |
| 334 | if (esc_save[1] == '[') { |
| 335 | /* see if next character is ~ */ |
| 336 | act = ESC_SAVE; |
| 337 | } |
| 338 | break; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 339 | } |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 340 | } else if (esc_len == 3) { |
Heinrich Schuchardt | 00fa825 | 2022-02-07 19:14:02 +0100 | [diff] [blame] | 341 | switch (ichar) { |
| 342 | case '~': |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 343 | switch (esc_save[2]) { |
| 344 | case '3': /* Delete key */ |
| 345 | ichar = CTL_CH('d'); |
| 346 | act = ESC_CONVERTED; |
| 347 | break; /* pass to ^D handler */ |
| 348 | case '1': /* Home key */ |
| 349 | case '7': |
| 350 | ichar = CTL_CH('a'); |
| 351 | act = ESC_CONVERTED; |
| 352 | break; /* pass to ^A handler */ |
| 353 | case '4': /* End key */ |
| 354 | case '8': |
| 355 | ichar = CTL_CH('e'); |
| 356 | act = ESC_CONVERTED; |
| 357 | break; /* pass to ^E handler */ |
| 358 | } |
Heinrich Schuchardt | 00fa825 | 2022-02-07 19:14:02 +0100 | [diff] [blame] | 359 | break; |
| 360 | case '0': |
| 361 | if (esc_save[2] == '2') |
| 362 | act = ESC_SAVE; |
| 363 | break; |
| 364 | } |
| 365 | } else if (esc_len == 4) { |
| 366 | switch (ichar) { |
| 367 | case '0': |
| 368 | case '1': |
| 369 | act = ESC_SAVE; |
| 370 | break; /* bracketed paste */ |
| 371 | } |
| 372 | } else if (esc_len == 5) { |
| 373 | if (ichar == '~') { /* bracketed paste */ |
| 374 | ichar = 0; |
| 375 | act = ESC_CONVERTED; |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 376 | } |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 377 | } |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 378 | switch (act) { |
| 379 | case ESC_SAVE: |
| 380 | esc_save[esc_len++] = ichar; |
| 381 | continue; |
| 382 | case ESC_REJECT: |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 383 | esc_save[esc_len++] = ichar; |
| 384 | cread_add_str(esc_save, esc_len, insert, |
| 385 | &num, &eol_num, buf, *len); |
| 386 | esc_len = 0; |
| 387 | continue; |
James Byrne | fc18e9b | 2016-08-16 18:16:28 +0100 | [diff] [blame] | 388 | case ESC_CONVERTED: |
| 389 | esc_len = 0; |
| 390 | break; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | switch (ichar) { |
| 395 | case 0x1b: |
| 396 | if (esc_len == 0) { |
| 397 | esc_save[esc_len] = ichar; |
| 398 | esc_len = 1; |
| 399 | } else { |
| 400 | puts("impossible condition #876\n"); |
| 401 | esc_len = 0; |
| 402 | } |
| 403 | break; |
| 404 | |
| 405 | case CTL_CH('a'): |
| 406 | BEGINNING_OF_LINE(); |
| 407 | break; |
| 408 | case CTL_CH('c'): /* ^C - break */ |
| 409 | *buf = '\0'; /* discard input */ |
| 410 | return -1; |
| 411 | case CTL_CH('f'): |
| 412 | if (num < eol_num) { |
| 413 | getcmd_putch(buf[num]); |
| 414 | num++; |
| 415 | } |
| 416 | break; |
| 417 | case CTL_CH('b'): |
| 418 | if (num) { |
| 419 | getcmd_putch(CTL_BACKSPACE); |
| 420 | num--; |
| 421 | } |
| 422 | break; |
| 423 | case CTL_CH('d'): |
| 424 | if (num < eol_num) { |
| 425 | wlen = eol_num - num - 1; |
| 426 | if (wlen) { |
| 427 | memmove(&buf[num], &buf[num+1], wlen); |
| 428 | putnstr(buf + num, wlen); |
| 429 | } |
| 430 | |
| 431 | getcmd_putch(' '); |
| 432 | do { |
| 433 | getcmd_putch(CTL_BACKSPACE); |
| 434 | } while (wlen--); |
| 435 | eol_num--; |
| 436 | } |
| 437 | break; |
| 438 | case CTL_CH('k'): |
| 439 | ERASE_TO_EOL(); |
| 440 | break; |
| 441 | case CTL_CH('e'): |
| 442 | REFRESH_TO_EOL(); |
| 443 | break; |
| 444 | case CTL_CH('o'): |
| 445 | insert = !insert; |
| 446 | break; |
| 447 | case CTL_CH('x'): |
| 448 | case CTL_CH('u'): |
| 449 | BEGINNING_OF_LINE(); |
| 450 | ERASE_TO_EOL(); |
| 451 | break; |
| 452 | case DEL: |
| 453 | case DEL7: |
| 454 | case 8: |
| 455 | if (num) { |
| 456 | wlen = eol_num - num; |
| 457 | num--; |
| 458 | memmove(&buf[num], &buf[num+1], wlen); |
| 459 | getcmd_putch(CTL_BACKSPACE); |
| 460 | putnstr(buf + num, wlen); |
| 461 | getcmd_putch(' '); |
| 462 | do { |
| 463 | getcmd_putch(CTL_BACKSPACE); |
| 464 | } while (wlen--); |
| 465 | eol_num--; |
| 466 | } |
| 467 | break; |
| 468 | case CTL_CH('p'): |
| 469 | case CTL_CH('n'): |
| 470 | { |
| 471 | char *hline; |
| 472 | |
| 473 | esc_len = 0; |
| 474 | |
| 475 | if (ichar == CTL_CH('p')) |
| 476 | hline = hist_prev(); |
| 477 | else |
| 478 | hline = hist_next(); |
| 479 | |
| 480 | if (!hline) { |
| 481 | getcmd_cbeep(); |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | /* nuke the current line */ |
| 486 | /* first, go home */ |
| 487 | BEGINNING_OF_LINE(); |
| 488 | |
| 489 | /* erase to end of line */ |
| 490 | ERASE_TO_EOL(); |
| 491 | |
| 492 | /* copy new line into place and display */ |
| 493 | strcpy(buf, hline); |
| 494 | eol_num = strlen(buf); |
| 495 | REFRESH_TO_EOL(); |
| 496 | continue; |
| 497 | } |
| 498 | #ifdef CONFIG_AUTO_COMPLETE |
| 499 | case '\t': { |
| 500 | int num2, col; |
| 501 | |
| 502 | /* do not autocomplete when in the middle */ |
| 503 | if (num < eol_num) { |
| 504 | getcmd_cbeep(); |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | buf[num] = '\0'; |
| 509 | col = strlen(prompt) + eol_num; |
| 510 | num2 = num; |
| 511 | if (cmd_auto_complete(prompt, buf, &num2, &col)) { |
| 512 | col = num2 - num; |
| 513 | num += col; |
| 514 | eol_num += col; |
| 515 | } |
| 516 | break; |
| 517 | } |
| 518 | #endif |
| 519 | default: |
Pali Rohár | 60d7820 | 2022-11-02 00:40:46 +0100 | [diff] [blame] | 520 | cread_add_char(ichar, insert, &num, &eol_num, buf, |
| 521 | *len); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | } |
| 525 | *len = eol_num; |
| 526 | buf[eol_num] = '\0'; /* lose the newline */ |
| 527 | |
| 528 | if (buf[0] && buf[0] != CREAD_HIST_CHAR) |
| 529 | cread_add_to_hist(buf); |
| 530 | hist_cur = hist_add_idx; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 536 | |
| 537 | /****************************************************************************/ |
| 538 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 539 | int cli_readline(const char *const prompt) |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 540 | { |
| 541 | /* |
| 542 | * If console_buffer isn't 0-length the user will be prompted to modify |
| 543 | * it instead of entering it from scratch as desired. |
| 544 | */ |
| 545 | console_buffer[0] = '\0'; |
| 546 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 547 | return cli_readline_into_buffer(prompt, console_buffer, 0); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 551 | int cli_readline_into_buffer(const char *const prompt, char *buffer, |
| 552 | int timeout) |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 553 | { |
| 554 | char *p = buffer; |
| 555 | #ifdef CONFIG_CMDLINE_EDITING |
| 556 | unsigned int len = CONFIG_SYS_CBSIZE; |
| 557 | int rc; |
| 558 | static int initted; |
| 559 | |
| 560 | /* |
| 561 | * History uses a global array which is not |
| 562 | * writable until after relocation to RAM. |
| 563 | * Revert to non-history version if still |
| 564 | * running from flash. |
| 565 | */ |
| 566 | if (gd->flags & GD_FLG_RELOC) { |
| 567 | if (!initted) { |
| 568 | hist_init(); |
| 569 | initted = 1; |
| 570 | } |
| 571 | |
| 572 | if (prompt) |
| 573 | puts(prompt); |
| 574 | |
| 575 | rc = cread_line(prompt, p, &len, timeout); |
| 576 | return rc < 0 ? rc : len; |
| 577 | |
| 578 | } else { |
| 579 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 580 | char *p_buf = p; |
| 581 | int n = 0; /* buffer index */ |
| 582 | int plen = 0; /* prompt length */ |
| 583 | int col; /* output column cnt */ |
| 584 | char c; |
| 585 | |
| 586 | /* print prompt */ |
| 587 | if (prompt) { |
| 588 | plen = strlen(prompt); |
| 589 | puts(prompt); |
| 590 | } |
| 591 | col = plen; |
| 592 | |
| 593 | for (;;) { |
Simon Glass | 0098e17 | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 594 | if (bootretry_tstc_timeout()) |
| 595 | return -2; /* timed out */ |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 596 | schedule(); /* Trigger watchdog, if needed */ |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 597 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 598 | c = getchar(); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * Special character handling |
| 602 | */ |
| 603 | switch (c) { |
| 604 | case '\r': /* Enter */ |
| 605 | case '\n': |
| 606 | *p = '\0'; |
| 607 | puts("\r\n"); |
| 608 | return p - p_buf; |
| 609 | |
| 610 | case '\0': /* nul */ |
| 611 | continue; |
| 612 | |
| 613 | case 0x03: /* ^C - break */ |
| 614 | p_buf[0] = '\0'; /* discard input */ |
| 615 | return -1; |
| 616 | |
| 617 | case 0x15: /* ^U - erase line */ |
| 618 | while (col > plen) { |
| 619 | puts(erase_seq); |
| 620 | --col; |
| 621 | } |
| 622 | p = p_buf; |
| 623 | n = 0; |
| 624 | continue; |
| 625 | |
| 626 | case 0x17: /* ^W - erase word */ |
| 627 | p = delete_char(p_buf, p, &col, &n, plen); |
| 628 | while ((n > 0) && (*p != ' ')) |
| 629 | p = delete_char(p_buf, p, &col, &n, plen); |
| 630 | continue; |
| 631 | |
| 632 | case 0x08: /* ^H - backspace */ |
| 633 | case 0x7F: /* DEL - backspace */ |
| 634 | p = delete_char(p_buf, p, &col, &n, plen); |
| 635 | continue; |
| 636 | |
| 637 | default: |
| 638 | /* |
| 639 | * Must be a normal character then |
| 640 | */ |
| 641 | if (n < CONFIG_SYS_CBSIZE-2) { |
| 642 | if (c == '\t') { /* expand TABs */ |
| 643 | #ifdef CONFIG_AUTO_COMPLETE |
| 644 | /* |
| 645 | * if auto completion triggered just |
| 646 | * continue |
| 647 | */ |
| 648 | *p = '\0'; |
| 649 | if (cmd_auto_complete(prompt, |
| 650 | console_buffer, |
| 651 | &n, &col)) { |
| 652 | p = p_buf + n; /* reset */ |
| 653 | continue; |
| 654 | } |
| 655 | #endif |
| 656 | puts(tab_seq + (col & 07)); |
| 657 | col += 8 - (col & 07); |
| 658 | } else { |
Heiko Schocher | 80402f3 | 2015-06-29 09:10:46 +0200 | [diff] [blame] | 659 | char __maybe_unused buf[2]; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 660 | |
| 661 | /* |
| 662 | * Echo input using puts() to force an |
| 663 | * LCD flush if we are using an LCD |
| 664 | */ |
| 665 | ++col; |
| 666 | buf[0] = c; |
| 667 | buf[1] = '\0'; |
| 668 | puts(buf); |
| 669 | } |
| 670 | *p++ = c; |
| 671 | ++n; |
| 672 | } else { /* Buffer full */ |
| 673 | putc('\a'); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | #ifdef CONFIG_CMDLINE_EDITING |
| 678 | } |
| 679 | #endif |
| 680 | } |