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