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