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 | |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 11 | #include <bootretry.h> |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 12 | #include <cli.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | #include <command.h> |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 14 | #include <hang.h> |
| 15 | #include <malloc.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 16 | #include <time.h> |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 17 | #include <watchdog.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 18 | #include <linux/errno.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | static const char erase_seq[] = "\b \b"; /* erase sequence */ |
| 24 | static const char tab_seq[] = " "; /* used to expand TABs */ |
| 25 | |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 26 | char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ |
| 27 | |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 28 | static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) |
| 29 | { |
| 30 | char *s; |
| 31 | |
| 32 | if (*np == 0) |
| 33 | return p; |
| 34 | |
| 35 | if (*(--p) == '\t') { /* will retype the whole line */ |
| 36 | while (*colp > plen) { |
| 37 | puts(erase_seq); |
| 38 | (*colp)--; |
| 39 | } |
| 40 | for (s = buffer; s < p; ++s) { |
| 41 | if (*s == '\t') { |
| 42 | puts(tab_seq + ((*colp) & 07)); |
| 43 | *colp += 8 - ((*colp) & 07); |
| 44 | } else { |
| 45 | ++(*colp); |
| 46 | putc(*s); |
| 47 | } |
| 48 | } |
| 49 | } else { |
| 50 | puts(erase_seq); |
| 51 | (*colp)--; |
| 52 | } |
| 53 | (*np)--; |
| 54 | |
| 55 | return p; |
| 56 | } |
| 57 | |
| 58 | #ifdef CONFIG_CMDLINE_EDITING |
| 59 | |
| 60 | /* |
| 61 | * cmdline-editing related codes from vivi. |
| 62 | * Author: Janghoon Lyu <nandy@mizi.com> |
| 63 | */ |
| 64 | |
| 65 | #define putnstr(str, n) printf("%.*s", (int)n, str) |
| 66 | |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 67 | #define CTL_BACKSPACE ('\b') |
| 68 | #define DEL ((char)255) |
| 69 | #define DEL7 ((char)127) |
| 70 | #define CREAD_HIST_CHAR ('!') |
| 71 | |
| 72 | #define getcmd_putch(ch) putc(ch) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 73 | #define getcmd_getch() getchar() |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 74 | #define getcmd_cbeep() getcmd_putch('\a') |
| 75 | |
Sean Anderson | 5d5efec | 2022-08-30 16:40:37 -0400 | [diff] [blame] | 76 | #ifdef CONFIG_SPL_BUILD |
| 77 | #define HIST_MAX 3 |
| 78 | #define HIST_SIZE 32 |
| 79 | #else |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 80 | #define HIST_MAX 20 |
| 81 | #define HIST_SIZE CONFIG_SYS_CBSIZE |
Sean Anderson | 5d5efec | 2022-08-30 16:40:37 -0400 | [diff] [blame] | 82 | #endif |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 83 | |
| 84 | static int hist_max; |
| 85 | static int hist_add_idx; |
| 86 | static int hist_cur = -1; |
| 87 | static unsigned hist_num; |
| 88 | |
Hanyuan Zhao | ae31efc | 2024-03-05 15:37:35 +0800 | [diff] [blame] | 89 | #ifndef CONFIG_CMD_HISTORY_USE_CALLOC |
| 90 | static char hist_data[HIST_MAX][HIST_SIZE + 1]; |
| 91 | #endif |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 92 | static char *hist_list[HIST_MAX]; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 93 | |
| 94 | #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) |
| 95 | |
Simon Glass | 415f32d | 2023-10-01 19:13:09 -0600 | [diff] [blame] | 96 | static void getcmd_putchars(int count, int ch) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < count; i++) |
| 101 | getcmd_putch(ch); |
| 102 | } |
| 103 | |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 104 | static int hist_init(void) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | |
Hanyuan Zhao | ae31efc | 2024-03-05 15:37:35 +0800 | [diff] [blame] | 108 | #ifndef CONFIG_CMD_HISTORY_USE_CALLOC |
| 109 | for (i = 0; i < HIST_MAX; i++) { |
| 110 | hist_list[i] = hist_data[i]; |
| 111 | hist_list[i][0] = '\0'; |
| 112 | } |
| 113 | #else |
| 114 | unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1); |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 115 | if (!hist) |
Hanyuan Zhao | 13c54e7 | 2024-03-05 15:37:33 +0800 | [diff] [blame] | 116 | panic("%s: calloc: out of memory!\n", __func__); |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 117 | |
| 118 | for (i = 0; i < HIST_MAX; i++) |
| 119 | hist_list[i] = hist + (i * (HIST_SIZE + 1)); |
Hanyuan Zhao | ae31efc | 2024-03-05 15:37:35 +0800 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | hist_max = 0; |
| 123 | hist_add_idx = 0; |
| 124 | hist_cur = -1; |
| 125 | hist_num = 0; |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 126 | |
| 127 | return 0; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void cread_add_to_hist(char *line) |
| 131 | { |
| 132 | strcpy(hist_list[hist_add_idx], line); |
| 133 | |
| 134 | if (++hist_add_idx >= HIST_MAX) |
| 135 | hist_add_idx = 0; |
| 136 | |
| 137 | if (hist_add_idx > hist_max) |
| 138 | hist_max = hist_add_idx; |
| 139 | |
| 140 | hist_num++; |
| 141 | } |
| 142 | |
| 143 | static char *hist_prev(void) |
| 144 | { |
| 145 | char *ret; |
| 146 | int old_cur; |
| 147 | |
| 148 | if (hist_cur < 0) |
| 149 | return NULL; |
| 150 | |
| 151 | old_cur = hist_cur; |
| 152 | if (--hist_cur < 0) |
| 153 | hist_cur = hist_max; |
| 154 | |
| 155 | if (hist_cur == hist_add_idx) { |
| 156 | hist_cur = old_cur; |
| 157 | ret = NULL; |
| 158 | } else { |
| 159 | ret = hist_list[hist_cur]; |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static char *hist_next(void) |
| 166 | { |
| 167 | char *ret; |
| 168 | |
| 169 | if (hist_cur < 0) |
| 170 | return NULL; |
| 171 | |
| 172 | if (hist_cur == hist_add_idx) |
| 173 | return NULL; |
| 174 | |
| 175 | if (++hist_cur > hist_max) |
| 176 | hist_cur = 0; |
| 177 | |
| 178 | if (hist_cur == hist_add_idx) |
| 179 | ret = ""; |
| 180 | else |
| 181 | ret = hist_list[hist_cur]; |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
Simon Glass | 4c0bf97 | 2023-10-01 19:13:06 -0600 | [diff] [blame] | 186 | void cread_print_hist_list(void) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 187 | { |
| 188 | int i; |
Simon Glass | 9e6201e | 2023-10-01 19:13:10 -0600 | [diff] [blame] | 189 | uint n; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 190 | |
| 191 | n = hist_num - hist_max; |
| 192 | |
| 193 | i = hist_add_idx + 1; |
| 194 | while (1) { |
| 195 | if (i > hist_max) |
| 196 | i = 0; |
| 197 | if (i == hist_add_idx) |
| 198 | break; |
| 199 | printf("%s\n", hist_list[i]); |
| 200 | n++; |
| 201 | i++; |
| 202 | } |
| 203 | } |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 204 | |
| 205 | #define BEGINNING_OF_LINE() { \ |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 206 | while (cls->num) { \ |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 207 | getcmd_putch(CTL_BACKSPACE); \ |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 208 | cls->num--; \ |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 209 | } \ |
| 210 | } |
| 211 | |
| 212 | #define ERASE_TO_EOL() { \ |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 213 | if (cls->num < cls->eol_num) { \ |
| 214 | printf("%*s", (int)(cls->eol_num - cls->num), ""); \ |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 215 | do { \ |
| 216 | getcmd_putch(CTL_BACKSPACE); \ |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 217 | } while (--cls->eol_num > cls->num); \ |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 218 | } \ |
| 219 | } |
| 220 | |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 221 | #define REFRESH_TO_EOL() { \ |
| 222 | if (cls->num < cls->eol_num) { \ |
| 223 | uint wlen = cls->eol_num - cls->num; \ |
| 224 | putnstr(buf + cls->num, wlen); \ |
| 225 | cls->num = cls->eol_num; \ |
| 226 | } \ |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Simon Glass | 9e6201e | 2023-10-01 19:13:10 -0600 | [diff] [blame] | 229 | static void cread_add_char(char ichar, int insert, uint *num, |
| 230 | uint *eol_num, char *buf, uint len) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 231 | { |
Simon Glass | 9e6201e | 2023-10-01 19:13:10 -0600 | [diff] [blame] | 232 | uint wlen; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 233 | |
| 234 | /* room ??? */ |
| 235 | if (insert || *num == *eol_num) { |
| 236 | if (*eol_num > len - 1) { |
| 237 | getcmd_cbeep(); |
| 238 | return; |
| 239 | } |
| 240 | (*eol_num)++; |
| 241 | } |
| 242 | |
| 243 | if (insert) { |
| 244 | wlen = *eol_num - *num; |
| 245 | if (wlen > 1) |
| 246 | memmove(&buf[*num+1], &buf[*num], wlen-1); |
| 247 | |
| 248 | buf[*num] = ichar; |
| 249 | putnstr(buf + *num, wlen); |
| 250 | (*num)++; |
| 251 | while (--wlen) |
| 252 | getcmd_putch(CTL_BACKSPACE); |
| 253 | } else { |
| 254 | /* echo the character */ |
| 255 | wlen = 1; |
| 256 | buf[*num] = ichar; |
| 257 | putnstr(buf + *num, wlen); |
| 258 | (*num)++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void cread_add_str(char *str, int strsize, int insert, |
Simon Glass | 9e6201e | 2023-10-01 19:13:10 -0600 | [diff] [blame] | 263 | uint *num, uint *eol_num, char *buf, uint len) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 264 | { |
| 265 | while (strsize--) { |
| 266 | cread_add_char(*str, insert, num, eol_num, buf, len); |
| 267 | str++; |
| 268 | } |
| 269 | } |
| 270 | |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 271 | int cread_line_process_ch(struct cli_line_state *cls, char ichar) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 272 | { |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 273 | char *buf = cls->buf; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 274 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 275 | /* ichar=0x0 when error occurs in U-Boot getc */ |
| 276 | if (!ichar) |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 277 | return -EAGAIN; |
Patrick Delaunay | f0017d2 | 2018-08-03 13:38:45 +0200 | [diff] [blame] | 278 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 279 | if (ichar == '\n') { |
| 280 | putc('\n'); |
Simon Glass | 9f07109 | 2023-10-01 19:13:14 -0600 | [diff] [blame] | 281 | buf[cls->eol_num] = '\0'; /* terminate the string */ |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 282 | return 0; |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 283 | } |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 284 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 285 | switch (ichar) { |
| 286 | case CTL_CH('a'): |
| 287 | BEGINNING_OF_LINE(); |
| 288 | break; |
| 289 | case CTL_CH('c'): /* ^C - break */ |
| 290 | *buf = '\0'; /* discard input */ |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 291 | return -EINTR; |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 292 | case CTL_CH('f'): |
| 293 | if (cls->num < cls->eol_num) { |
| 294 | getcmd_putch(buf[cls->num]); |
| 295 | cls->num++; |
| 296 | } |
| 297 | break; |
| 298 | case CTL_CH('b'): |
| 299 | if (cls->num) { |
| 300 | getcmd_putch(CTL_BACKSPACE); |
| 301 | cls->num--; |
| 302 | } |
| 303 | break; |
| 304 | case CTL_CH('d'): |
| 305 | if (cls->num < cls->eol_num) { |
| 306 | uint wlen; |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 307 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 308 | wlen = cls->eol_num - cls->num - 1; |
| 309 | if (wlen) { |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 310 | memmove(&buf[cls->num], &buf[cls->num + 1], |
| 311 | wlen); |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 312 | putnstr(buf + cls->num, wlen); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 313 | } |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 314 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 315 | getcmd_putch(' '); |
| 316 | do { |
| 317 | getcmd_putch(CTL_BACKSPACE); |
| 318 | } while (wlen--); |
| 319 | cls->eol_num--; |
| 320 | } |
| 321 | break; |
| 322 | case CTL_CH('k'): |
| 323 | ERASE_TO_EOL(); |
| 324 | break; |
| 325 | case CTL_CH('e'): |
| 326 | REFRESH_TO_EOL(); |
| 327 | break; |
| 328 | case CTL_CH('o'): |
| 329 | cls->insert = !cls->insert; |
| 330 | break; |
| 331 | case CTL_CH('w'): |
| 332 | if (cls->num) { |
| 333 | uint base, wlen; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 334 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 335 | for (base = cls->num - 1; |
| 336 | base >= 0 && buf[base] == ' ';) |
| 337 | base--; |
| 338 | for (; base > 0 && buf[base - 1] != ' ';) |
| 339 | base--; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 340 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 341 | /* now delete chars from base to cls->num */ |
| 342 | wlen = cls->num - base; |
| 343 | cls->eol_num -= wlen; |
| 344 | memmove(&buf[base], &buf[cls->num], |
| 345 | cls->eol_num - base + 1); |
| 346 | cls->num = base; |
| 347 | getcmd_putchars(wlen, CTL_BACKSPACE); |
| 348 | puts(buf + base); |
| 349 | getcmd_putchars(wlen, ' '); |
| 350 | getcmd_putchars(wlen + cls->eol_num - cls->num, |
| 351 | CTL_BACKSPACE); |
| 352 | } |
| 353 | break; |
| 354 | case CTL_CH('x'): |
| 355 | case CTL_CH('u'): |
| 356 | BEGINNING_OF_LINE(); |
| 357 | ERASE_TO_EOL(); |
| 358 | break; |
| 359 | case DEL: |
| 360 | case DEL7: |
| 361 | case 8: |
| 362 | if (cls->num) { |
| 363 | uint wlen; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 364 | |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 365 | wlen = cls->eol_num - cls->num; |
| 366 | cls->num--; |
| 367 | memmove(&buf[cls->num], &buf[cls->num + 1], wlen); |
| 368 | getcmd_putch(CTL_BACKSPACE); |
| 369 | putnstr(buf + cls->num, wlen); |
| 370 | getcmd_putch(' '); |
| 371 | do { |
| 372 | getcmd_putch(CTL_BACKSPACE); |
| 373 | } while (wlen--); |
| 374 | cls->eol_num--; |
| 375 | } |
| 376 | break; |
| 377 | case CTL_CH('p'): |
| 378 | case CTL_CH('n'): |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 379 | if (cls->history) { |
| 380 | char *hline; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 381 | |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 382 | if (ichar == CTL_CH('p')) |
| 383 | hline = hist_prev(); |
| 384 | else |
| 385 | hline = hist_next(); |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 386 | |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 387 | if (!hline) { |
| 388 | getcmd_cbeep(); |
| 389 | break; |
| 390 | } |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 391 | |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 392 | /* nuke the current line */ |
| 393 | /* first, go home */ |
| 394 | BEGINNING_OF_LINE(); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 395 | |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 396 | /* erase to end of line */ |
| 397 | ERASE_TO_EOL(); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 398 | |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 399 | /* copy new line into place and display */ |
| 400 | strcpy(buf, hline); |
| 401 | cls->eol_num = strlen(buf); |
| 402 | REFRESH_TO_EOL(); |
| 403 | break; |
| 404 | } |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 405 | break; |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 406 | case '\t': |
Simon Glass | 3e9bc77 | 2023-10-01 19:13:16 -0600 | [diff] [blame] | 407 | if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) { |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 408 | int num2, col; |
| 409 | |
| 410 | /* do not autocomplete when in the middle */ |
| 411 | if (cls->num < cls->eol_num) { |
| 412 | getcmd_cbeep(); |
Simon Glass | c0734c3 | 2023-10-01 19:13:08 -0600 | [diff] [blame] | 413 | break; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 414 | } |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 415 | |
| 416 | buf[cls->num] = '\0'; |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 417 | col = strlen(cls->prompt) + cls->eol_num; |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 418 | num2 = cls->num; |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 419 | if (cmd_auto_complete(cls->prompt, buf, &num2, &col)) { |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 420 | col = num2 - cls->num; |
| 421 | cls->num += col; |
| 422 | cls->eol_num += col; |
| 423 | } |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 424 | break; |
| 425 | } |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 426 | fallthrough; |
| 427 | default: |
| 428 | cread_add_char(ichar, cls->insert, &cls->num, &cls->eol_num, |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 429 | buf, cls->len); |
Simon Glass | c8d7ac7 | 2023-10-01 19:13:12 -0600 | [diff] [blame] | 430 | break; |
| 431 | } |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 432 | |
Simon Glass | 9f07109 | 2023-10-01 19:13:14 -0600 | [diff] [blame] | 433 | /* |
| 434 | * keep the string terminated...if we added a char at the end then we |
| 435 | * want a \0 after it |
| 436 | */ |
| 437 | buf[cls->eol_num] = '\0'; |
| 438 | |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 439 | return -EAGAIN; |
| 440 | } |
| 441 | |
Simon Glass | b949131 | 2023-10-01 19:13:17 -0600 | [diff] [blame] | 442 | void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size) |
| 443 | { |
| 444 | int init_len = strlen(buf); |
| 445 | |
| 446 | memset(cls, '\0', sizeof(struct cli_line_state)); |
| 447 | cls->insert = true; |
| 448 | cls->buf = buf; |
| 449 | cls->len = buf_size; |
| 450 | |
| 451 | if (init_len) |
| 452 | cread_add_str(buf, init_len, 0, &cls->num, &cls->eol_num, buf, |
| 453 | buf_size); |
| 454 | } |
| 455 | |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 456 | static int cread_line(const char *const prompt, char *buf, unsigned int *len, |
| 457 | int timeout) |
| 458 | { |
| 459 | struct cli_ch_state s_cch, *cch = &s_cch; |
| 460 | struct cli_line_state s_cls, *cls = &s_cls; |
| 461 | char ichar; |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 462 | int first = 1; |
| 463 | |
| 464 | cli_ch_init(cch); |
Simon Glass | b949131 | 2023-10-01 19:13:17 -0600 | [diff] [blame] | 465 | cli_cread_init(cls, buf, *len); |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 466 | cls->prompt = prompt; |
Simon Glass | 1c67ae4 | 2023-10-01 19:13:15 -0600 | [diff] [blame] | 467 | cls->history = true; |
Simon Glass | 3e9bc77 | 2023-10-01 19:13:16 -0600 | [diff] [blame] | 468 | cls->cmd_complete = true; |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 469 | |
Simon Glass | 2f13ae5 | 2023-10-01 19:13:13 -0600 | [diff] [blame] | 470 | while (1) { |
| 471 | int ret; |
| 472 | |
| 473 | /* Check for saved characters */ |
| 474 | ichar = cli_ch_process(cch, 0); |
| 475 | |
| 476 | if (!ichar) { |
| 477 | if (bootretry_tstc_timeout()) |
| 478 | return -2; /* timed out */ |
| 479 | if (first && timeout) { |
| 480 | u64 etime = endtick(timeout); |
| 481 | |
| 482 | while (!tstc()) { /* while no incoming data */ |
| 483 | if (get_ticks() >= etime) |
| 484 | return -2; /* timed out */ |
| 485 | schedule(); |
| 486 | } |
| 487 | first = 0; |
| 488 | } |
| 489 | |
| 490 | ichar = getcmd_getch(); |
| 491 | ichar = cli_ch_process(cch, ichar); |
| 492 | } |
| 493 | |
| 494 | ret = cread_line_process_ch(cls, ichar); |
| 495 | if (ret == -EINTR) |
| 496 | return -1; |
| 497 | else if (!ret) |
| 498 | break; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 499 | } |
Simon Glass | 07627c5 | 2023-10-01 19:13:11 -0600 | [diff] [blame] | 500 | *len = cls->eol_num; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 501 | |
| 502 | if (buf[0] && buf[0] != CREAD_HIST_CHAR) |
| 503 | cread_add_to_hist(buf); |
| 504 | hist_cur = hist_add_idx; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
Simon Glass | 4dc73c4 | 2023-10-01 19:13:07 -0600 | [diff] [blame] | 509 | #else /* !CONFIG_CMDLINE_EDITING */ |
| 510 | |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 511 | static inline int hist_init(void) |
Simon Glass | 4dc73c4 | 2023-10-01 19:13:07 -0600 | [diff] [blame] | 512 | { |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 513 | return 0; |
Simon Glass | 4dc73c4 | 2023-10-01 19:13:07 -0600 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static int cread_line(const char *const prompt, char *buf, unsigned int *len, |
| 517 | int timeout) |
| 518 | { |
| 519 | return 0; |
| 520 | } |
| 521 | |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 522 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 523 | |
| 524 | /****************************************************************************/ |
| 525 | |
Simon Glass | be6aafc | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 526 | int cli_readline(const char *const prompt) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 527 | { |
| 528 | /* |
| 529 | * If console_buffer isn't 0-length the user will be prompted to modify |
| 530 | * it instead of entering it from scratch as desired. |
| 531 | */ |
| 532 | console_buffer[0] = '\0'; |
| 533 | |
Simon Glass | be6aafc | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 534 | return cli_readline_into_buffer(prompt, console_buffer, 0); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 535 | } |
| 536 | |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 537 | /** |
| 538 | * cread_line_simple() - Simple (small) command-line reader |
| 539 | * |
| 540 | * This supports only basic editing, with no cursor movement |
| 541 | * |
| 542 | * @prompt: Prompt to display |
| 543 | * @p: Text buffer to edit |
| 544 | * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C) |
| 545 | */ |
| 546 | static int cread_line_simple(const char *const prompt, char *p) |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 547 | { |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 548 | char *p_buf = p; |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 549 | int n = 0; /* buffer index */ |
| 550 | int plen = 0; /* prompt length */ |
| 551 | int col; /* output column cnt */ |
Tom Rini | 479f54a | 2024-01-09 17:57:16 -0500 | [diff] [blame] | 552 | int c; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 553 | |
| 554 | /* print prompt */ |
| 555 | if (prompt) { |
| 556 | plen = strlen(prompt); |
| 557 | puts(prompt); |
| 558 | } |
| 559 | col = plen; |
| 560 | |
| 561 | for (;;) { |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 562 | if (bootretry_tstc_timeout()) |
| 563 | return -2; /* timed out */ |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 564 | schedule(); /* Trigger watchdog, if needed */ |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 565 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 566 | c = getchar(); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Special character handling |
| 570 | */ |
| 571 | switch (c) { |
| 572 | case '\r': /* Enter */ |
| 573 | case '\n': |
| 574 | *p = '\0'; |
| 575 | puts("\r\n"); |
| 576 | return p - p_buf; |
| 577 | |
| 578 | case '\0': /* nul */ |
| 579 | continue; |
| 580 | |
| 581 | case 0x03: /* ^C - break */ |
| 582 | p_buf[0] = '\0'; /* discard input */ |
| 583 | return -1; |
| 584 | |
| 585 | case 0x15: /* ^U - erase line */ |
| 586 | while (col > plen) { |
| 587 | puts(erase_seq); |
| 588 | --col; |
| 589 | } |
| 590 | p = p_buf; |
| 591 | n = 0; |
| 592 | continue; |
| 593 | |
| 594 | case 0x17: /* ^W - erase word */ |
| 595 | p = delete_char(p_buf, p, &col, &n, plen); |
| 596 | while ((n > 0) && (*p != ' ')) |
| 597 | p = delete_char(p_buf, p, &col, &n, plen); |
| 598 | continue; |
| 599 | |
| 600 | case 0x08: /* ^H - backspace */ |
| 601 | case 0x7F: /* DEL - backspace */ |
| 602 | p = delete_char(p_buf, p, &col, &n, plen); |
| 603 | continue; |
| 604 | |
| 605 | default: |
Simon Glass | c0734c3 | 2023-10-01 19:13:08 -0600 | [diff] [blame] | 606 | /* Must be a normal character then */ |
| 607 | if (n >= CONFIG_SYS_CBSIZE - 2) { /* Buffer full */ |
| 608 | putc('\a'); |
| 609 | break; |
| 610 | } |
| 611 | if (c == '\t') { /* expand TABs */ |
| 612 | if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) { |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 613 | /* |
Simon Glass | c0734c3 | 2023-10-01 19:13:08 -0600 | [diff] [blame] | 614 | * if auto-completion triggered just |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 615 | * continue |
| 616 | */ |
| 617 | *p = '\0'; |
| 618 | if (cmd_auto_complete(prompt, |
| 619 | console_buffer, |
| 620 | &n, &col)) { |
| 621 | p = p_buf + n; /* reset */ |
| 622 | continue; |
| 623 | } |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 624 | } |
Simon Glass | c0734c3 | 2023-10-01 19:13:08 -0600 | [diff] [blame] | 625 | puts(tab_seq + (col & 07)); |
| 626 | col += 8 - (col & 07); |
| 627 | } else { |
| 628 | char __maybe_unused buf[2]; |
| 629 | |
| 630 | /* |
| 631 | * Echo input using puts() to force an LCD |
| 632 | * flush if we are using an LCD |
| 633 | */ |
| 634 | ++col; |
| 635 | buf[0] = c; |
| 636 | buf[1] = '\0'; |
| 637 | puts(buf); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 638 | } |
Simon Glass | c0734c3 | 2023-10-01 19:13:08 -0600 | [diff] [blame] | 639 | *p++ = c; |
| 640 | ++n; |
| 641 | break; |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 642 | } |
| 643 | } |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | int cli_readline_into_buffer(const char *const prompt, char *buffer, |
| 647 | int timeout) |
| 648 | { |
| 649 | char *p = buffer; |
Simon Glass | 4dc73c4 | 2023-10-01 19:13:07 -0600 | [diff] [blame] | 650 | uint len = CONFIG_SYS_CBSIZE; |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 651 | int rc; |
| 652 | static int initted; |
| 653 | |
| 654 | /* |
Hanyuan Zhao | ae31efc | 2024-03-05 15:37:35 +0800 | [diff] [blame] | 655 | * Say N to CMD_HISTORY_USE_CALLOC will skip runtime |
| 656 | * allocation for the history buffer and directly |
| 657 | * use an uninitialized static array as the buffer. |
| 658 | * Doing this might have better performance and not |
| 659 | * increase the binary file's size, as it only marks |
| 660 | * the size. However, the array is only writable after |
| 661 | * relocation to RAM. If u-boot is running from ROM |
| 662 | * all the time, consider say Y to CMD_HISTORY_USE_CALLOC |
| 663 | * or disable CMD_HISTORY. |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 664 | */ |
Simon Glass | 4dc73c4 | 2023-10-01 19:13:07 -0600 | [diff] [blame] | 665 | if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) { |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 666 | if (!initted) { |
Marek Vasut | 6210f6d | 2023-12-02 21:52:30 +0100 | [diff] [blame] | 667 | rc = hist_init(); |
| 668 | if (rc == 0) |
| 669 | initted = 1; |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if (prompt) |
| 673 | puts(prompt); |
| 674 | |
| 675 | rc = cread_line(prompt, p, &len, timeout); |
| 676 | return rc < 0 ? rc : len; |
| 677 | |
| 678 | } else { |
Simon Glass | 043d0fd | 2023-10-01 19:13:05 -0600 | [diff] [blame] | 679 | return cread_line_simple(prompt, p); |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 680 | } |
Simon Glass | 66b8c8b | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 681 | } |