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