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