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 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 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 | |
Sean Anderson | 069f0d7 | 2022-08-30 16:40:37 -0400 | [diff] [blame] | 74 | #ifdef CONFIG_SPL_BUILD |
| 75 | #define HIST_MAX 3 |
| 76 | #define HIST_SIZE 32 |
| 77 | #else |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 78 | #define HIST_MAX 20 |
| 79 | #define HIST_SIZE CONFIG_SYS_CBSIZE |
Sean Anderson | 069f0d7 | 2022-08-30 16:40:37 -0400 | [diff] [blame] | 80 | #endif |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 81 | |
| 82 | static int hist_max; |
| 83 | static int hist_add_idx; |
| 84 | static int hist_cur = -1; |
| 85 | static unsigned hist_num; |
| 86 | |
| 87 | static char *hist_list[HIST_MAX]; |
| 88 | static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ |
| 89 | |
| 90 | #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) |
| 91 | |
| 92 | static void hist_init(void) |
| 93 | { |
| 94 | int i; |
| 95 | |
| 96 | hist_max = 0; |
| 97 | hist_add_idx = 0; |
| 98 | hist_cur = -1; |
| 99 | hist_num = 0; |
| 100 | |
| 101 | for (i = 0; i < HIST_MAX; i++) { |
| 102 | hist_list[i] = hist_lines[i]; |
| 103 | hist_list[i][0] = '\0'; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void cread_add_to_hist(char *line) |
| 108 | { |
| 109 | strcpy(hist_list[hist_add_idx], line); |
| 110 | |
| 111 | if (++hist_add_idx >= HIST_MAX) |
| 112 | hist_add_idx = 0; |
| 113 | |
| 114 | if (hist_add_idx > hist_max) |
| 115 | hist_max = hist_add_idx; |
| 116 | |
| 117 | hist_num++; |
| 118 | } |
| 119 | |
| 120 | static char *hist_prev(void) |
| 121 | { |
| 122 | char *ret; |
| 123 | int old_cur; |
| 124 | |
| 125 | if (hist_cur < 0) |
| 126 | return NULL; |
| 127 | |
| 128 | old_cur = hist_cur; |
| 129 | if (--hist_cur < 0) |
| 130 | hist_cur = hist_max; |
| 131 | |
| 132 | if (hist_cur == hist_add_idx) { |
| 133 | hist_cur = old_cur; |
| 134 | ret = NULL; |
| 135 | } else { |
| 136 | ret = hist_list[hist_cur]; |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static char *hist_next(void) |
| 143 | { |
| 144 | char *ret; |
| 145 | |
| 146 | if (hist_cur < 0) |
| 147 | return NULL; |
| 148 | |
| 149 | if (hist_cur == hist_add_idx) |
| 150 | return NULL; |
| 151 | |
| 152 | if (++hist_cur > hist_max) |
| 153 | hist_cur = 0; |
| 154 | |
| 155 | if (hist_cur == hist_add_idx) |
| 156 | ret = ""; |
| 157 | else |
| 158 | ret = hist_list[hist_cur]; |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
Simon Glass | 33eb0b9 | 2023-10-01 19:13:06 -0600 | [diff] [blame] | 163 | void cread_print_hist_list(void) |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 164 | { |
| 165 | int i; |
| 166 | unsigned long n; |
| 167 | |
| 168 | n = hist_num - hist_max; |
| 169 | |
| 170 | i = hist_add_idx + 1; |
| 171 | while (1) { |
| 172 | if (i > hist_max) |
| 173 | i = 0; |
| 174 | if (i == hist_add_idx) |
| 175 | break; |
| 176 | printf("%s\n", hist_list[i]); |
| 177 | n++; |
| 178 | i++; |
| 179 | } |
| 180 | } |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 181 | |
| 182 | #define BEGINNING_OF_LINE() { \ |
| 183 | while (num) { \ |
| 184 | getcmd_putch(CTL_BACKSPACE); \ |
| 185 | num--; \ |
| 186 | } \ |
| 187 | } |
| 188 | |
| 189 | #define ERASE_TO_EOL() { \ |
| 190 | if (num < eol_num) { \ |
| 191 | printf("%*s", (int)(eol_num - num), ""); \ |
| 192 | do { \ |
| 193 | getcmd_putch(CTL_BACKSPACE); \ |
| 194 | } while (--eol_num > num); \ |
| 195 | } \ |
| 196 | } |
| 197 | |
| 198 | #define REFRESH_TO_EOL() { \ |
| 199 | if (num < eol_num) { \ |
| 200 | wlen = eol_num - num; \ |
| 201 | putnstr(buf + num, wlen); \ |
| 202 | num = eol_num; \ |
| 203 | } \ |
| 204 | } |
| 205 | |
| 206 | static void cread_add_char(char ichar, int insert, unsigned long *num, |
| 207 | unsigned long *eol_num, char *buf, unsigned long len) |
| 208 | { |
| 209 | unsigned long wlen; |
| 210 | |
| 211 | /* room ??? */ |
| 212 | if (insert || *num == *eol_num) { |
| 213 | if (*eol_num > len - 1) { |
| 214 | getcmd_cbeep(); |
| 215 | return; |
| 216 | } |
| 217 | (*eol_num)++; |
| 218 | } |
| 219 | |
| 220 | if (insert) { |
| 221 | wlen = *eol_num - *num; |
| 222 | if (wlen > 1) |
| 223 | memmove(&buf[*num+1], &buf[*num], wlen-1); |
| 224 | |
| 225 | buf[*num] = ichar; |
| 226 | putnstr(buf + *num, wlen); |
| 227 | (*num)++; |
| 228 | while (--wlen) |
| 229 | getcmd_putch(CTL_BACKSPACE); |
| 230 | } else { |
| 231 | /* echo the character */ |
| 232 | wlen = 1; |
| 233 | buf[*num] = ichar; |
| 234 | putnstr(buf + *num, wlen); |
| 235 | (*num)++; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static void cread_add_str(char *str, int strsize, int insert, |
| 240 | unsigned long *num, unsigned long *eol_num, |
| 241 | char *buf, unsigned long len) |
| 242 | { |
| 243 | while (strsize--) { |
| 244 | cread_add_char(*str, insert, num, eol_num, buf, len); |
| 245 | str++; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static int cread_line(const char *const prompt, char *buf, unsigned int *len, |
| 250 | int timeout) |
| 251 | { |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 252 | struct cli_ch_state s_cch, *cch = &s_cch; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 253 | unsigned long num = 0; |
| 254 | unsigned long eol_num = 0; |
| 255 | unsigned long wlen; |
| 256 | char ichar; |
| 257 | int insert = 1; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 258 | int init_len = strlen(buf); |
| 259 | int first = 1; |
| 260 | |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 261 | cli_ch_init(cch); |
| 262 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 263 | if (init_len) |
| 264 | cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); |
| 265 | |
| 266 | while (1) { |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 267 | /* Check for saved characters */ |
| 268 | ichar = cli_ch_process(cch, 0); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 269 | |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 270 | if (!ichar) { |
| 271 | if (bootretry_tstc_timeout()) |
| 272 | return -2; /* timed out */ |
| 273 | if (first && timeout) { |
| 274 | u64 etime = endtick(timeout); |
| 275 | |
| 276 | while (!tstc()) { /* while no incoming data */ |
| 277 | if (get_ticks() >= etime) |
| 278 | return -2; /* timed out */ |
| 279 | schedule(); |
| 280 | } |
| 281 | first = 0; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 282 | } |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 283 | |
| 284 | ichar = getcmd_getch(); |
Simon Glass | be0169f | 2023-03-28 08:34:14 +1300 | [diff] [blame] | 285 | ichar = cli_ch_process(cch, ichar); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 286 | } |
| 287 | |
Patrick Delaunay | 555e378 | 2018-08-03 13:38:45 +0200 | [diff] [blame] | 288 | /* ichar=0x0 when error occurs in U-Boot getc */ |
| 289 | if (!ichar) |
| 290 | continue; |
| 291 | |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 292 | if (ichar == '\n') { |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 293 | putc('\n'); |
| 294 | break; |
| 295 | } |
| 296 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 297 | switch (ichar) { |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 298 | case CTL_CH('a'): |
| 299 | BEGINNING_OF_LINE(); |
| 300 | break; |
| 301 | case CTL_CH('c'): /* ^C - break */ |
| 302 | *buf = '\0'; /* discard input */ |
| 303 | return -1; |
| 304 | case CTL_CH('f'): |
| 305 | if (num < eol_num) { |
| 306 | getcmd_putch(buf[num]); |
| 307 | num++; |
| 308 | } |
| 309 | break; |
| 310 | case CTL_CH('b'): |
| 311 | if (num) { |
| 312 | getcmd_putch(CTL_BACKSPACE); |
| 313 | num--; |
| 314 | } |
| 315 | break; |
| 316 | case CTL_CH('d'): |
| 317 | if (num < eol_num) { |
| 318 | wlen = eol_num - num - 1; |
| 319 | if (wlen) { |
| 320 | memmove(&buf[num], &buf[num+1], wlen); |
| 321 | putnstr(buf + num, wlen); |
| 322 | } |
| 323 | |
| 324 | getcmd_putch(' '); |
| 325 | do { |
| 326 | getcmd_putch(CTL_BACKSPACE); |
| 327 | } while (wlen--); |
| 328 | eol_num--; |
| 329 | } |
| 330 | break; |
| 331 | case CTL_CH('k'): |
| 332 | ERASE_TO_EOL(); |
| 333 | break; |
| 334 | case CTL_CH('e'): |
| 335 | REFRESH_TO_EOL(); |
| 336 | break; |
| 337 | case CTL_CH('o'): |
| 338 | insert = !insert; |
| 339 | break; |
| 340 | case CTL_CH('x'): |
| 341 | case CTL_CH('u'): |
| 342 | BEGINNING_OF_LINE(); |
| 343 | ERASE_TO_EOL(); |
| 344 | break; |
| 345 | case DEL: |
| 346 | case DEL7: |
| 347 | case 8: |
| 348 | if (num) { |
| 349 | wlen = eol_num - num; |
| 350 | num--; |
| 351 | memmove(&buf[num], &buf[num+1], wlen); |
| 352 | getcmd_putch(CTL_BACKSPACE); |
| 353 | putnstr(buf + num, wlen); |
| 354 | getcmd_putch(' '); |
| 355 | do { |
| 356 | getcmd_putch(CTL_BACKSPACE); |
| 357 | } while (wlen--); |
| 358 | eol_num--; |
| 359 | } |
| 360 | break; |
| 361 | case CTL_CH('p'): |
| 362 | case CTL_CH('n'): |
| 363 | { |
| 364 | char *hline; |
| 365 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 366 | if (ichar == CTL_CH('p')) |
| 367 | hline = hist_prev(); |
| 368 | else |
| 369 | hline = hist_next(); |
| 370 | |
| 371 | if (!hline) { |
| 372 | getcmd_cbeep(); |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | /* nuke the current line */ |
| 377 | /* first, go home */ |
| 378 | BEGINNING_OF_LINE(); |
| 379 | |
| 380 | /* erase to end of line */ |
| 381 | ERASE_TO_EOL(); |
| 382 | |
| 383 | /* copy new line into place and display */ |
| 384 | strcpy(buf, hline); |
| 385 | eol_num = strlen(buf); |
| 386 | REFRESH_TO_EOL(); |
| 387 | continue; |
| 388 | } |
| 389 | #ifdef CONFIG_AUTO_COMPLETE |
| 390 | case '\t': { |
| 391 | int num2, col; |
| 392 | |
| 393 | /* do not autocomplete when in the middle */ |
| 394 | if (num < eol_num) { |
| 395 | getcmd_cbeep(); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | buf[num] = '\0'; |
| 400 | col = strlen(prompt) + eol_num; |
| 401 | num2 = num; |
| 402 | if (cmd_auto_complete(prompt, buf, &num2, &col)) { |
| 403 | col = num2 - num; |
| 404 | num += col; |
| 405 | eol_num += col; |
| 406 | } |
| 407 | break; |
| 408 | } |
| 409 | #endif |
| 410 | default: |
Pali Rohár | 60d7820 | 2022-11-02 00:40:46 +0100 | [diff] [blame] | 411 | cread_add_char(ichar, insert, &num, &eol_num, buf, |
| 412 | *len); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | } |
| 416 | *len = eol_num; |
| 417 | buf[eol_num] = '\0'; /* lose the newline */ |
| 418 | |
| 419 | if (buf[0] && buf[0] != CREAD_HIST_CHAR) |
| 420 | cread_add_to_hist(buf); |
| 421 | hist_cur = hist_add_idx; |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Simon Glass | 039f8cc | 2023-10-01 19:13:07 -0600 | [diff] [blame^] | 426 | #else /* !CONFIG_CMDLINE_EDITING */ |
| 427 | |
| 428 | static inline void hist_init(void) |
| 429 | { |
| 430 | } |
| 431 | |
| 432 | static int cread_line(const char *const prompt, char *buf, unsigned int *len, |
| 433 | int timeout) |
| 434 | { |
| 435 | return 0; |
| 436 | } |
| 437 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 438 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 439 | |
| 440 | /****************************************************************************/ |
| 441 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 442 | int cli_readline(const char *const prompt) |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 443 | { |
| 444 | /* |
| 445 | * If console_buffer isn't 0-length the user will be prompted to modify |
| 446 | * it instead of entering it from scratch as desired. |
| 447 | */ |
| 448 | console_buffer[0] = '\0'; |
| 449 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 450 | return cli_readline_into_buffer(prompt, console_buffer, 0); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 451 | } |
| 452 | |
Simon Glass | 0f97e94 | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 453 | /** |
| 454 | * cread_line_simple() - Simple (small) command-line reader |
| 455 | * |
| 456 | * This supports only basic editing, with no cursor movement |
| 457 | * |
| 458 | * @prompt: Prompt to display |
| 459 | * @p: Text buffer to edit |
| 460 | * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C) |
| 461 | */ |
| 462 | static int cread_line_simple(const char *const prompt, char *p) |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 463 | { |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 464 | char *p_buf = p; |
Simon Glass | 0f97e94 | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 465 | int n = 0; /* buffer index */ |
| 466 | int plen = 0; /* prompt length */ |
| 467 | int col; /* output column cnt */ |
| 468 | char c; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 469 | |
| 470 | /* print prompt */ |
| 471 | if (prompt) { |
| 472 | plen = strlen(prompt); |
| 473 | puts(prompt); |
| 474 | } |
| 475 | col = plen; |
| 476 | |
| 477 | for (;;) { |
Simon Glass | 0098e17 | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 478 | if (bootretry_tstc_timeout()) |
| 479 | return -2; /* timed out */ |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 480 | schedule(); /* Trigger watchdog, if needed */ |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 481 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 482 | c = getchar(); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * Special character handling |
| 486 | */ |
| 487 | switch (c) { |
| 488 | case '\r': /* Enter */ |
| 489 | case '\n': |
| 490 | *p = '\0'; |
| 491 | puts("\r\n"); |
| 492 | return p - p_buf; |
| 493 | |
| 494 | case '\0': /* nul */ |
| 495 | continue; |
| 496 | |
| 497 | case 0x03: /* ^C - break */ |
| 498 | p_buf[0] = '\0'; /* discard input */ |
| 499 | return -1; |
| 500 | |
| 501 | case 0x15: /* ^U - erase line */ |
| 502 | while (col > plen) { |
| 503 | puts(erase_seq); |
| 504 | --col; |
| 505 | } |
| 506 | p = p_buf; |
| 507 | n = 0; |
| 508 | continue; |
| 509 | |
| 510 | case 0x17: /* ^W - erase word */ |
| 511 | p = delete_char(p_buf, p, &col, &n, plen); |
| 512 | while ((n > 0) && (*p != ' ')) |
| 513 | p = delete_char(p_buf, p, &col, &n, plen); |
| 514 | continue; |
| 515 | |
| 516 | case 0x08: /* ^H - backspace */ |
| 517 | case 0x7F: /* DEL - backspace */ |
| 518 | p = delete_char(p_buf, p, &col, &n, plen); |
| 519 | continue; |
| 520 | |
| 521 | default: |
| 522 | /* |
| 523 | * Must be a normal character then |
| 524 | */ |
| 525 | if (n < CONFIG_SYS_CBSIZE-2) { |
| 526 | if (c == '\t') { /* expand TABs */ |
| 527 | #ifdef CONFIG_AUTO_COMPLETE |
| 528 | /* |
| 529 | * if auto completion triggered just |
| 530 | * continue |
| 531 | */ |
| 532 | *p = '\0'; |
| 533 | if (cmd_auto_complete(prompt, |
| 534 | console_buffer, |
| 535 | &n, &col)) { |
| 536 | p = p_buf + n; /* reset */ |
| 537 | continue; |
| 538 | } |
| 539 | #endif |
| 540 | puts(tab_seq + (col & 07)); |
| 541 | col += 8 - (col & 07); |
| 542 | } else { |
Heiko Schocher | 80402f3 | 2015-06-29 09:10:46 +0200 | [diff] [blame] | 543 | char __maybe_unused buf[2]; |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * Echo input using puts() to force an |
| 547 | * LCD flush if we are using an LCD |
| 548 | */ |
| 549 | ++col; |
| 550 | buf[0] = c; |
| 551 | buf[1] = '\0'; |
| 552 | puts(buf); |
| 553 | } |
| 554 | *p++ = c; |
| 555 | ++n; |
| 556 | } else { /* Buffer full */ |
| 557 | putc('\a'); |
| 558 | } |
| 559 | } |
| 560 | } |
Simon Glass | 0f97e94 | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | int cli_readline_into_buffer(const char *const prompt, char *buffer, |
| 564 | int timeout) |
| 565 | { |
| 566 | char *p = buffer; |
Simon Glass | 039f8cc | 2023-10-01 19:13:07 -0600 | [diff] [blame^] | 567 | uint len = CONFIG_SYS_CBSIZE; |
Simon Glass | 0f97e94 | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 568 | int rc; |
| 569 | static int initted; |
| 570 | |
| 571 | /* |
| 572 | * History uses a global array which is not |
| 573 | * writable until after relocation to RAM. |
| 574 | * Revert to non-history version if still |
| 575 | * running from flash. |
| 576 | */ |
Simon Glass | 039f8cc | 2023-10-01 19:13:07 -0600 | [diff] [blame^] | 577 | if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) { |
Simon Glass | 0f97e94 | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 578 | if (!initted) { |
| 579 | hist_init(); |
| 580 | initted = 1; |
| 581 | } |
| 582 | |
| 583 | if (prompt) |
| 584 | puts(prompt); |
| 585 | |
| 586 | rc = cread_line(prompt, p, &len, timeout); |
| 587 | return rc < 0 ? rc : len; |
| 588 | |
| 589 | } else { |
Simon Glass | 0f97e94 | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 590 | return cread_line_simple(prompt, p); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 591 | } |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 592 | } |