| 1 | /* |
| 2 | * QEMU readline utility |
| 3 | * |
| 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qemu/readline.h" |
| 27 | #include "qemu/ctype.h" |
| 28 | #include "qemu/cutils.h" |
| 29 | |
| 30 | #define IS_NORM 0 |
| 31 | #define IS_ESC 1 |
| 32 | #define IS_CSI 2 |
| 33 | #define IS_SS3 3 |
| 34 | |
| 35 | void readline_show_prompt(ReadLineState *rs) |
| 36 | { |
| 37 | rs->printf_func(rs->opaque, "%s", rs->prompt); |
| 38 | rs->flush_func(rs->opaque); |
| 39 | rs->last_cmd_buf_index = 0; |
| 40 | rs->last_cmd_buf_size = 0; |
| 41 | rs->esc_state = IS_NORM; |
| 42 | } |
| 43 | |
| 44 | /* update the displayed command line */ |
| 45 | static void readline_update(ReadLineState *rs) |
| 46 | { |
| 47 | int i, delta, len; |
| 48 | |
| 49 | if (rs->cmd_buf_size != rs->last_cmd_buf_size || |
| 50 | memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) { |
| 51 | for (i = 0; i < rs->last_cmd_buf_index; i++) { |
| 52 | rs->printf_func(rs->opaque, "\033[D"); |
| 53 | } |
| 54 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
| 55 | if (rs->read_password) { |
| 56 | len = strlen(rs->cmd_buf); |
| 57 | for (i = 0; i < len; i++) { |
| 58 | rs->printf_func(rs->opaque, "*"); |
| 59 | } |
| 60 | } else { |
| 61 | rs->printf_func(rs->opaque, "%s", rs->cmd_buf); |
| 62 | } |
| 63 | rs->printf_func(rs->opaque, "\033[K"); |
| 64 | memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size); |
| 65 | rs->last_cmd_buf_size = rs->cmd_buf_size; |
| 66 | rs->last_cmd_buf_index = rs->cmd_buf_size; |
| 67 | } |
| 68 | if (rs->cmd_buf_index != rs->last_cmd_buf_index) { |
| 69 | delta = rs->cmd_buf_index - rs->last_cmd_buf_index; |
| 70 | if (delta > 0) { |
| 71 | for (i = 0; i < delta; i++) { |
| 72 | rs->printf_func(rs->opaque, "\033[C"); |
| 73 | } |
| 74 | } else { |
| 75 | delta = -delta; |
| 76 | for (i = 0; i < delta; i++) { |
| 77 | rs->printf_func(rs->opaque, "\033[D"); |
| 78 | } |
| 79 | } |
| 80 | rs->last_cmd_buf_index = rs->cmd_buf_index; |
| 81 | } |
| 82 | rs->flush_func(rs->opaque); |
| 83 | } |
| 84 | |
| 85 | static void readline_insert_char(ReadLineState *rs, int ch) |
| 86 | { |
| 87 | assert(rs->cmd_buf_index <= rs->cmd_buf_size); |
| 88 | |
| 89 | if (rs->cmd_buf_size < READLINE_CMD_BUF_SIZE) { |
| 90 | memmove(rs->cmd_buf + rs->cmd_buf_index + 1, |
| 91 | rs->cmd_buf + rs->cmd_buf_index, |
| 92 | rs->cmd_buf_size - rs->cmd_buf_index); |
| 93 | rs->cmd_buf[rs->cmd_buf_index] = ch; |
| 94 | rs->cmd_buf_size++; |
| 95 | rs->cmd_buf_index++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static void readline_backward_char(ReadLineState *rs) |
| 100 | { |
| 101 | if (rs->cmd_buf_index > 0) { |
| 102 | rs->cmd_buf_index--; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void readline_forward_char(ReadLineState *rs) |
| 107 | { |
| 108 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
| 109 | rs->cmd_buf_index++; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static void readline_delete_char(ReadLineState *rs) |
| 114 | { |
| 115 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
| 116 | memmove(rs->cmd_buf + rs->cmd_buf_index, |
| 117 | rs->cmd_buf + rs->cmd_buf_index + 1, |
| 118 | rs->cmd_buf_size - rs->cmd_buf_index - 1); |
| 119 | rs->cmd_buf_size--; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void readline_backspace(ReadLineState *rs) |
| 124 | { |
| 125 | if (rs->cmd_buf_index > 0) { |
| 126 | readline_backward_char(rs); |
| 127 | readline_delete_char(rs); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void readline_backword(ReadLineState *rs) |
| 132 | { |
| 133 | int start; |
| 134 | |
| 135 | if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | start = rs->cmd_buf_index - 1; |
| 140 | |
| 141 | /* find first word (backwards) */ |
| 142 | while (start > 0) { |
| 143 | if (!qemu_isspace(rs->cmd_buf[start])) { |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | --start; |
| 148 | } |
| 149 | |
| 150 | /* find first space (backwards) */ |
| 151 | while (start > 0) { |
| 152 | if (qemu_isspace(rs->cmd_buf[start])) { |
| 153 | ++start; |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | --start; |
| 158 | } |
| 159 | |
| 160 | /* remove word */ |
| 161 | if (start < rs->cmd_buf_index) { |
| 162 | memmove(rs->cmd_buf + start, |
| 163 | rs->cmd_buf + rs->cmd_buf_index, |
| 164 | rs->cmd_buf_size - rs->cmd_buf_index); |
| 165 | rs->cmd_buf_size -= rs->cmd_buf_index - start; |
| 166 | rs->cmd_buf_index = start; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static void readline_bol(ReadLineState *rs) |
| 171 | { |
| 172 | rs->cmd_buf_index = 0; |
| 173 | } |
| 174 | |
| 175 | static void readline_eol(ReadLineState *rs) |
| 176 | { |
| 177 | rs->cmd_buf_index = rs->cmd_buf_size; |
| 178 | } |
| 179 | |
| 180 | static void readline_up_char(ReadLineState *rs) |
| 181 | { |
| 182 | int idx; |
| 183 | |
| 184 | if (rs->hist_entry == 0) { |
| 185 | return; |
| 186 | } |
| 187 | if (rs->hist_entry == -1) { |
| 188 | /* Find latest entry */ |
| 189 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
| 190 | if (rs->history[idx] == NULL) { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | rs->hist_entry = idx; |
| 195 | } |
| 196 | rs->hist_entry--; |
| 197 | if (rs->hist_entry >= 0) { |
| 198 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
| 199 | rs->history[rs->hist_entry]); |
| 200 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | static void readline_down_char(ReadLineState *rs) |
| 205 | { |
| 206 | if (rs->hist_entry == -1) { |
| 207 | return; |
| 208 | } |
| 209 | if (rs->hist_entry < READLINE_MAX_CMDS - 1 && |
| 210 | rs->history[++rs->hist_entry] != NULL) { |
| 211 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
| 212 | rs->history[rs->hist_entry]); |
| 213 | } else { |
| 214 | rs->cmd_buf[0] = 0; |
| 215 | rs->hist_entry = -1; |
| 216 | } |
| 217 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
| 218 | } |
| 219 | |
| 220 | static void readline_hist_add(ReadLineState *rs, const char *cmdline) |
| 221 | { |
| 222 | char *hist_entry, *new_entry; |
| 223 | int idx; |
| 224 | |
| 225 | if (cmdline[0] == '\0') { |
| 226 | return; |
| 227 | } |
| 228 | new_entry = NULL; |
| 229 | if (rs->hist_entry != -1) { |
| 230 | /* We were editing an existing history entry: replace it */ |
| 231 | hist_entry = rs->history[rs->hist_entry]; |
| 232 | idx = rs->hist_entry; |
| 233 | if (strcmp(hist_entry, cmdline) == 0) { |
| 234 | goto same_entry; |
| 235 | } |
| 236 | } |
| 237 | /* Search cmdline in history buffers */ |
| 238 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
| 239 | hist_entry = rs->history[idx]; |
| 240 | if (hist_entry == NULL) { |
| 241 | break; |
| 242 | } |
| 243 | if (strcmp(hist_entry, cmdline) == 0) { |
| 244 | same_entry: |
| 245 | if (idx == READLINE_MAX_CMDS - 1) { |
| 246 | return; |
| 247 | } |
| 248 | new_entry = hist_entry; |
| 249 | /* Put this entry at the end of history */ |
| 250 | memmove(&rs->history[idx], &rs->history[idx + 1], |
| 251 | (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *)); |
| 252 | rs->history[READLINE_MAX_CMDS - 1] = NULL; |
| 253 | for (; idx < READLINE_MAX_CMDS; idx++) { |
| 254 | if (rs->history[idx] == NULL) { |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | if (idx == READLINE_MAX_CMDS) { |
| 262 | /* Need to get one free slot */ |
| 263 | g_free(rs->history[0]); |
| 264 | memmove(rs->history, &rs->history[1], |
| 265 | (READLINE_MAX_CMDS - 1) * sizeof(char *)); |
| 266 | rs->history[READLINE_MAX_CMDS - 1] = NULL; |
| 267 | idx = READLINE_MAX_CMDS - 1; |
| 268 | } |
| 269 | if (new_entry == NULL) { |
| 270 | new_entry = g_strdup(cmdline); |
| 271 | } |
| 272 | rs->history[idx] = new_entry; |
| 273 | rs->hist_entry = -1; |
| 274 | } |
| 275 | |
| 276 | static void readline_kill_line(ReadLineState *rs) |
| 277 | { |
| 278 | while (rs->cmd_buf_index > 0) { |
| 279 | readline_backward_char(rs); |
| 280 | readline_delete_char(rs); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* completion support */ |
| 285 | |
| 286 | void readline_add_completion(ReadLineState *rs, const char *str) |
| 287 | { |
| 288 | if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { |
| 289 | int i; |
| 290 | for (i = 0; i < rs->nb_completions; i++) { |
| 291 | if (!strcmp(rs->completions[i], str)) { |
| 292 | return; |
| 293 | } |
| 294 | } |
| 295 | rs->completions[rs->nb_completions++] = g_strdup(str); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void readline_add_completion_of(ReadLineState *rs, |
| 300 | const char *pfx, const char *str) |
| 301 | { |
| 302 | if (!strncmp(str, pfx, strlen(pfx))) { |
| 303 | readline_add_completion(rs, str); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void readline_set_completion_index(ReadLineState *rs, int index) |
| 308 | { |
| 309 | rs->completion_index = index; |
| 310 | } |
| 311 | |
| 312 | static int completion_comp(const void *a, const void *b) |
| 313 | { |
| 314 | return strcmp(*(const char **) a, *(const char **) b); |
| 315 | } |
| 316 | |
| 317 | static void readline_completion(ReadLineState *rs) |
| 318 | { |
| 319 | int len, i, j, max_width, nb_cols, max_prefix; |
| 320 | char *cmdline; |
| 321 | |
| 322 | rs->nb_completions = 0; |
| 323 | |
| 324 | cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index); |
| 325 | rs->completion_finder(rs->opaque, cmdline); |
| 326 | g_free(cmdline); |
| 327 | |
| 328 | /* no completion found */ |
| 329 | if (rs->nb_completions <= 0) { |
| 330 | return; |
| 331 | } |
| 332 | if (rs->nb_completions == 1) { |
| 333 | len = strlen(rs->completions[0]); |
| 334 | for (i = rs->completion_index; i < len; i++) { |
| 335 | readline_insert_char(rs, rs->completions[0][i]); |
| 336 | } |
| 337 | /* extra space for next argument. XXX: make it more generic */ |
| 338 | if (len > 0 && rs->completions[0][len - 1] != '/') { |
| 339 | readline_insert_char(rs, ' '); |
| 340 | } |
| 341 | } else { |
| 342 | qsort(rs->completions, rs->nb_completions, sizeof(char *), |
| 343 | completion_comp); |
| 344 | rs->printf_func(rs->opaque, "\n"); |
| 345 | max_width = 0; |
| 346 | max_prefix = 0; |
| 347 | for (i = 0; i < rs->nb_completions; i++) { |
| 348 | len = strlen(rs->completions[i]); |
| 349 | if (i == 0) { |
| 350 | max_prefix = len; |
| 351 | } else { |
| 352 | if (len < max_prefix) { |
| 353 | max_prefix = len; |
| 354 | } |
| 355 | for (j = 0; j < max_prefix; j++) { |
| 356 | if (rs->completions[i][j] != rs->completions[0][j]) { |
| 357 | max_prefix = j; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | if (len > max_width) { |
| 362 | max_width = len; |
| 363 | } |
| 364 | } |
| 365 | if (max_prefix > 0) |
| 366 | for (i = rs->completion_index; i < max_prefix; i++) { |
| 367 | readline_insert_char(rs, rs->completions[0][i]); |
| 368 | } |
| 369 | max_width += 2; |
| 370 | if (max_width < 10) { |
| 371 | max_width = 10; |
| 372 | } else if (max_width > 80) { |
| 373 | max_width = 80; |
| 374 | } |
| 375 | nb_cols = 80 / max_width; |
| 376 | j = 0; |
| 377 | for (i = 0; i < rs->nb_completions; i++) { |
| 378 | rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]); |
| 379 | if (++j == nb_cols || i == (rs->nb_completions - 1)) { |
| 380 | rs->printf_func(rs->opaque, "\n"); |
| 381 | j = 0; |
| 382 | } |
| 383 | } |
| 384 | readline_show_prompt(rs); |
| 385 | } |
| 386 | for (i = 0; i < rs->nb_completions; i++) { |
| 387 | g_free(rs->completions[i]); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | static void readline_clear_screen(ReadLineState *rs) |
| 392 | { |
| 393 | rs->printf_func(rs->opaque, "\033[2J\033[1;1H"); |
| 394 | readline_show_prompt(rs); |
| 395 | } |
| 396 | |
| 397 | /* return true if command handled */ |
| 398 | void readline_handle_byte(ReadLineState *rs, int ch) |
| 399 | { |
| 400 | switch (rs->esc_state) { |
| 401 | case IS_NORM: |
| 402 | switch (ch) { |
| 403 | case 1: |
| 404 | readline_bol(rs); |
| 405 | break; |
| 406 | case 4: |
| 407 | readline_delete_char(rs); |
| 408 | break; |
| 409 | case 5: |
| 410 | readline_eol(rs); |
| 411 | break; |
| 412 | case 9: |
| 413 | readline_completion(rs); |
| 414 | break; |
| 415 | case 12: |
| 416 | readline_clear_screen(rs); |
| 417 | break; |
| 418 | case 10: /* fallthrough */ |
| 419 | case 13: |
| 420 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
| 421 | if (!rs->read_password) { |
| 422 | readline_hist_add(rs, rs->cmd_buf); |
| 423 | } |
| 424 | rs->printf_func(rs->opaque, "\n"); |
| 425 | rs->cmd_buf_index = 0; |
| 426 | rs->cmd_buf_size = 0; |
| 427 | rs->last_cmd_buf_index = 0; |
| 428 | rs->last_cmd_buf_size = 0; |
| 429 | rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque); |
| 430 | break; |
| 431 | case 14: |
| 432 | /* ^N Next line in history */ |
| 433 | readline_down_char(rs); |
| 434 | break; |
| 435 | case 16: |
| 436 | /* ^P Prev line in history */ |
| 437 | readline_up_char(rs); |
| 438 | break; |
| 439 | case 21: |
| 440 | /* ^U Kill backward from point to the beginning of the line. */ |
| 441 | readline_kill_line(rs); |
| 442 | break; |
| 443 | case 23: |
| 444 | /* ^W */ |
| 445 | readline_backword(rs); |
| 446 | break; |
| 447 | case 27: |
| 448 | rs->esc_state = IS_ESC; |
| 449 | break; |
| 450 | case 127: /* fallthrough */ |
| 451 | case 8: |
| 452 | readline_backspace(rs); |
| 453 | break; |
| 454 | case 155: |
| 455 | rs->esc_state = IS_CSI; |
| 456 | break; |
| 457 | default: |
| 458 | if (ch >= 32) { |
| 459 | readline_insert_char(rs, ch); |
| 460 | } |
| 461 | break; |
| 462 | } |
| 463 | break; |
| 464 | case IS_ESC: |
| 465 | if (ch == '[') { |
| 466 | rs->esc_state = IS_CSI; |
| 467 | rs->esc_param = 0; |
| 468 | } else if (ch == 'O') { |
| 469 | rs->esc_state = IS_SS3; |
| 470 | rs->esc_param = 0; |
| 471 | } else { |
| 472 | rs->esc_state = IS_NORM; |
| 473 | } |
| 474 | break; |
| 475 | case IS_CSI: |
| 476 | switch (ch) { |
| 477 | case 'A': /* fallthrough */ |
| 478 | case 'F': |
| 479 | readline_up_char(rs); |
| 480 | break; |
| 481 | case 'B': /* fallthrough */ |
| 482 | case 'E': |
| 483 | readline_down_char(rs); |
| 484 | break; |
| 485 | case 'D': |
| 486 | readline_backward_char(rs); |
| 487 | break; |
| 488 | case 'C': |
| 489 | readline_forward_char(rs); |
| 490 | break; |
| 491 | case '0' ... '9': |
| 492 | rs->esc_param = rs->esc_param * 10 + (ch - '0'); |
| 493 | goto the_end; |
| 494 | case '~': |
| 495 | switch (rs->esc_param) { |
| 496 | case 1: |
| 497 | readline_bol(rs); |
| 498 | break; |
| 499 | case 3: |
| 500 | readline_delete_char(rs); |
| 501 | break; |
| 502 | case 4: |
| 503 | readline_eol(rs); |
| 504 | break; |
| 505 | default: |
| 506 | break; |
| 507 | } |
| 508 | break; |
| 509 | default: |
| 510 | break; |
| 511 | } |
| 512 | rs->esc_state = IS_NORM; |
| 513 | /* fallthrough */ |
| 514 | the_end: |
| 515 | break; |
| 516 | case IS_SS3: |
| 517 | switch (ch) { |
| 518 | case 'F': |
| 519 | readline_eol(rs); |
| 520 | break; |
| 521 | case 'H': |
| 522 | readline_bol(rs); |
| 523 | break; |
| 524 | default: |
| 525 | break; |
| 526 | } |
| 527 | rs->esc_state = IS_NORM; |
| 528 | break; |
| 529 | default: |
| 530 | break; |
| 531 | } |
| 532 | readline_update(rs); |
| 533 | } |
| 534 | |
| 535 | void readline_start(ReadLineState *rs, const char *prompt, int read_password, |
| 536 | ReadLineFunc *readline_func, void *opaque) |
| 537 | { |
| 538 | pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); |
| 539 | rs->readline_func = readline_func; |
| 540 | rs->readline_opaque = opaque; |
| 541 | rs->read_password = read_password; |
| 542 | readline_restart(rs); |
| 543 | } |
| 544 | |
| 545 | void readline_restart(ReadLineState *rs) |
| 546 | { |
| 547 | rs->cmd_buf_index = 0; |
| 548 | rs->cmd_buf_size = 0; |
| 549 | } |
| 550 | |
| 551 | const char *readline_get_history(ReadLineState *rs, unsigned int index) |
| 552 | { |
| 553 | if (index >= READLINE_MAX_CMDS) { |
| 554 | return NULL; |
| 555 | } |
| 556 | return rs->history[index]; |
| 557 | } |
| 558 | |
| 559 | void readline_free(ReadLineState *rs) |
| 560 | { |
| 561 | int i; |
| 562 | |
| 563 | if (!rs) { |
| 564 | return; |
| 565 | } |
| 566 | for (i = 0; i < READLINE_MAX_CMDS; i++) { |
| 567 | g_free(rs->history[i]); |
| 568 | } |
| 569 | g_free(rs); |
| 570 | } |
| 571 | |
| 572 | ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, |
| 573 | ReadLineFlushFunc *flush_func, |
| 574 | void *opaque, |
| 575 | ReadLineCompletionFunc *completion_finder) |
| 576 | { |
| 577 | ReadLineState *rs = g_new0(ReadLineState, 1); |
| 578 | |
| 579 | rs->hist_entry = -1; |
| 580 | rs->opaque = opaque; |
| 581 | rs->printf_func = printf_func; |
| 582 | rs->flush_func = flush_func; |
| 583 | rs->completion_finder = completion_finder; |
| 584 | |
| 585 | return rs; |
| 586 | } |