Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / util / readline.c
blob0f19674f52631e49f2c2adde38cd5bdaab09eb5d
1 /*
2 * QEMU readline utility
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qemu/readline.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
30 #define IS_NORM 0
31 #define IS_ESC 1
32 #define IS_CSI 2
33 #define IS_SS3 3
35 void readline_show_prompt(ReadLineState *rs)
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;
44 /* update the displayed command line */
45 static void readline_update(ReadLineState *rs)
47 int i, delta, len;
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");
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, "*");
60 } else {
61 rs->printf_func(rs->opaque, "%s", rs->cmd_buf);
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;
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");
74 } else {
75 delta = -delta;
76 for (i = 0; i < delta; i++) {
77 rs->printf_func(rs->opaque, "\033[D");
80 rs->last_cmd_buf_index = rs->cmd_buf_index;
82 rs->flush_func(rs->opaque);
85 static void readline_insert_char(ReadLineState *rs, int ch)
87 if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
88 memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
89 rs->cmd_buf + rs->cmd_buf_index,
90 rs->cmd_buf_size - rs->cmd_buf_index);
91 rs->cmd_buf[rs->cmd_buf_index] = ch;
92 rs->cmd_buf_size++;
93 rs->cmd_buf_index++;
97 static void readline_backward_char(ReadLineState *rs)
99 if (rs->cmd_buf_index > 0) {
100 rs->cmd_buf_index--;
104 static void readline_forward_char(ReadLineState *rs)
106 if (rs->cmd_buf_index < rs->cmd_buf_size) {
107 rs->cmd_buf_index++;
111 static void readline_delete_char(ReadLineState *rs)
113 if (rs->cmd_buf_index < rs->cmd_buf_size) {
114 memmove(rs->cmd_buf + rs->cmd_buf_index,
115 rs->cmd_buf + rs->cmd_buf_index + 1,
116 rs->cmd_buf_size - rs->cmd_buf_index - 1);
117 rs->cmd_buf_size--;
121 static void readline_backspace(ReadLineState *rs)
123 if (rs->cmd_buf_index > 0) {
124 readline_backward_char(rs);
125 readline_delete_char(rs);
129 static void readline_backword(ReadLineState *rs)
131 int start;
133 if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
134 return;
137 start = rs->cmd_buf_index - 1;
139 /* find first word (backwards) */
140 while (start > 0) {
141 if (!qemu_isspace(rs->cmd_buf[start])) {
142 break;
145 --start;
148 /* find first space (backwards) */
149 while (start > 0) {
150 if (qemu_isspace(rs->cmd_buf[start])) {
151 ++start;
152 break;
155 --start;
158 /* remove word */
159 if (start < rs->cmd_buf_index) {
160 memmove(rs->cmd_buf + start,
161 rs->cmd_buf + rs->cmd_buf_index,
162 rs->cmd_buf_size - rs->cmd_buf_index);
163 rs->cmd_buf_size -= rs->cmd_buf_index - start;
164 rs->cmd_buf_index = start;
168 static void readline_bol(ReadLineState *rs)
170 rs->cmd_buf_index = 0;
173 static void readline_eol(ReadLineState *rs)
175 rs->cmd_buf_index = rs->cmd_buf_size;
178 static void readline_up_char(ReadLineState *rs)
180 int idx;
182 if (rs->hist_entry == 0) {
183 return;
185 if (rs->hist_entry == -1) {
186 /* Find latest entry */
187 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
188 if (rs->history[idx] == NULL) {
189 break;
192 rs->hist_entry = idx;
194 rs->hist_entry--;
195 if (rs->hist_entry >= 0) {
196 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
197 rs->history[rs->hist_entry]);
198 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
202 static void readline_down_char(ReadLineState *rs)
204 if (rs->hist_entry == -1) {
205 return;
207 if (rs->hist_entry < READLINE_MAX_CMDS - 1 &&
208 rs->history[++rs->hist_entry] != NULL) {
209 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
210 rs->history[rs->hist_entry]);
211 } else {
212 rs->cmd_buf[0] = 0;
213 rs->hist_entry = -1;
215 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
218 static void readline_hist_add(ReadLineState *rs, const char *cmdline)
220 char *hist_entry, *new_entry;
221 int idx;
223 if (cmdline[0] == '\0') {
224 return;
226 new_entry = NULL;
227 if (rs->hist_entry != -1) {
228 /* We were editing an existing history entry: replace it */
229 hist_entry = rs->history[rs->hist_entry];
230 idx = rs->hist_entry;
231 if (strcmp(hist_entry, cmdline) == 0) {
232 goto same_entry;
235 /* Search cmdline in history buffers */
236 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
237 hist_entry = rs->history[idx];
238 if (hist_entry == NULL) {
239 break;
241 if (strcmp(hist_entry, cmdline) == 0) {
242 same_entry:
243 if (idx == READLINE_MAX_CMDS - 1) {
244 return;
246 new_entry = hist_entry;
247 /* Put this entry at the end of history */
248 memmove(&rs->history[idx], &rs->history[idx + 1],
249 (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
250 rs->history[READLINE_MAX_CMDS - 1] = NULL;
251 for (; idx < READLINE_MAX_CMDS; idx++) {
252 if (rs->history[idx] == NULL) {
253 break;
256 break;
259 if (idx == READLINE_MAX_CMDS) {
260 /* Need to get one free slot */
261 g_free(rs->history[0]);
262 memmove(rs->history, &rs->history[1],
263 (READLINE_MAX_CMDS - 1) * sizeof(char *));
264 rs->history[READLINE_MAX_CMDS - 1] = NULL;
265 idx = READLINE_MAX_CMDS - 1;
267 if (new_entry == NULL) {
268 new_entry = g_strdup(cmdline);
270 rs->history[idx] = new_entry;
271 rs->hist_entry = -1;
274 static void readline_kill_line(ReadLineState *rs)
276 while (rs->cmd_buf_index > 0) {
277 readline_backward_char(rs);
278 readline_delete_char(rs);
282 /* completion support */
284 void readline_add_completion(ReadLineState *rs, const char *str)
286 if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
287 int i;
288 for (i = 0; i < rs->nb_completions; i++) {
289 if (!strcmp(rs->completions[i], str)) {
290 return;
293 rs->completions[rs->nb_completions++] = g_strdup(str);
297 void readline_add_completion_of(ReadLineState *rs,
298 const char *pfx, const char *str)
300 if (!strncmp(str, pfx, strlen(pfx))) {
301 readline_add_completion(rs, str);
305 void readline_set_completion_index(ReadLineState *rs, int index)
307 rs->completion_index = index;
310 static int completion_comp(const void *a, const void *b)
312 return strcmp(*(const char **) a, *(const char **) b);
315 static void readline_completion(ReadLineState *rs)
317 int len, i, j, max_width, nb_cols, max_prefix;
318 char *cmdline;
320 rs->nb_completions = 0;
322 cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index);
323 rs->completion_finder(rs->opaque, cmdline);
324 g_free(cmdline);
326 /* no completion found */
327 if (rs->nb_completions <= 0) {
328 return;
330 if (rs->nb_completions == 1) {
331 len = strlen(rs->completions[0]);
332 for (i = rs->completion_index; i < len; i++) {
333 readline_insert_char(rs, rs->completions[0][i]);
335 /* extra space for next argument. XXX: make it more generic */
336 if (len > 0 && rs->completions[0][len - 1] != '/') {
337 readline_insert_char(rs, ' ');
339 } else {
340 qsort(rs->completions, rs->nb_completions, sizeof(char *),
341 completion_comp);
342 rs->printf_func(rs->opaque, "\n");
343 max_width = 0;
344 max_prefix = 0;
345 for (i = 0; i < rs->nb_completions; i++) {
346 len = strlen(rs->completions[i]);
347 if (i == 0) {
348 max_prefix = len;
349 } else {
350 if (len < max_prefix) {
351 max_prefix = len;
353 for (j = 0; j < max_prefix; j++) {
354 if (rs->completions[i][j] != rs->completions[0][j]) {
355 max_prefix = j;
359 if (len > max_width) {
360 max_width = len;
363 if (max_prefix > 0)
364 for (i = rs->completion_index; i < max_prefix; i++) {
365 readline_insert_char(rs, rs->completions[0][i]);
367 max_width += 2;
368 if (max_width < 10) {
369 max_width = 10;
370 } else if (max_width > 80) {
371 max_width = 80;
373 nb_cols = 80 / max_width;
374 j = 0;
375 for (i = 0; i < rs->nb_completions; i++) {
376 rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]);
377 if (++j == nb_cols || i == (rs->nb_completions - 1)) {
378 rs->printf_func(rs->opaque, "\n");
379 j = 0;
382 readline_show_prompt(rs);
384 for (i = 0; i < rs->nb_completions; i++) {
385 g_free(rs->completions[i]);
389 static void readline_clear_screen(ReadLineState *rs)
391 rs->printf_func(rs->opaque, "\033[2J\033[1;1H");
392 readline_show_prompt(rs);
395 /* return true if command handled */
396 void readline_handle_byte(ReadLineState *rs, int ch)
398 switch (rs->esc_state) {
399 case IS_NORM:
400 switch (ch) {
401 case 1:
402 readline_bol(rs);
403 break;
404 case 4:
405 readline_delete_char(rs);
406 break;
407 case 5:
408 readline_eol(rs);
409 break;
410 case 9:
411 readline_completion(rs);
412 break;
413 case 12:
414 readline_clear_screen(rs);
415 break;
416 case 10: /* fallthrough */
417 case 13:
418 rs->cmd_buf[rs->cmd_buf_size] = '\0';
419 if (!rs->read_password) {
420 readline_hist_add(rs, rs->cmd_buf);
422 rs->printf_func(rs->opaque, "\n");
423 rs->cmd_buf_index = 0;
424 rs->cmd_buf_size = 0;
425 rs->last_cmd_buf_index = 0;
426 rs->last_cmd_buf_size = 0;
427 rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque);
428 break;
429 case 14:
430 /* ^N Next line in history */
431 readline_down_char(rs);
432 break;
433 case 16:
434 /* ^P Prev line in history */
435 readline_up_char(rs);
436 break;
437 case 21:
438 /* ^U Kill backward from point to the beginning of the line. */
439 readline_kill_line(rs);
440 break;
441 case 23:
442 /* ^W */
443 readline_backword(rs);
444 break;
445 case 27:
446 rs->esc_state = IS_ESC;
447 break;
448 case 127: /* fallthrough */
449 case 8:
450 readline_backspace(rs);
451 break;
452 case 155:
453 rs->esc_state = IS_CSI;
454 break;
455 default:
456 if (ch >= 32) {
457 readline_insert_char(rs, ch);
459 break;
461 break;
462 case IS_ESC:
463 if (ch == '[') {
464 rs->esc_state = IS_CSI;
465 rs->esc_param = 0;
466 } else if (ch == 'O') {
467 rs->esc_state = IS_SS3;
468 rs->esc_param = 0;
469 } else {
470 rs->esc_state = IS_NORM;
472 break;
473 case IS_CSI:
474 switch (ch) {
475 case 'A': /* fallthrough */
476 case 'F':
477 readline_up_char(rs);
478 break;
479 case 'B': /* fallthrough */
480 case 'E':
481 readline_down_char(rs);
482 break;
483 case 'D':
484 readline_backward_char(rs);
485 break;
486 case 'C':
487 readline_forward_char(rs);
488 break;
489 case '0' ... '9':
490 rs->esc_param = rs->esc_param * 10 + (ch - '0');
491 goto the_end;
492 case '~':
493 switch (rs->esc_param) {
494 case 1:
495 readline_bol(rs);
496 break;
497 case 3:
498 readline_delete_char(rs);
499 break;
500 case 4:
501 readline_eol(rs);
502 break;
503 default:
504 break;
506 break;
507 default:
508 break;
510 rs->esc_state = IS_NORM;
511 /* fallthrough */
512 the_end:
513 break;
514 case IS_SS3:
515 switch (ch) {
516 case 'F':
517 readline_eol(rs);
518 break;
519 case 'H':
520 readline_bol(rs);
521 break;
522 default:
523 break;
525 rs->esc_state = IS_NORM;
526 break;
527 default:
528 break;
530 readline_update(rs);
533 void readline_start(ReadLineState *rs, const char *prompt, int read_password,
534 ReadLineFunc *readline_func, void *opaque)
536 pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
537 rs->readline_func = readline_func;
538 rs->readline_opaque = opaque;
539 rs->read_password = read_password;
540 readline_restart(rs);
543 void readline_restart(ReadLineState *rs)
545 rs->cmd_buf_index = 0;
546 rs->cmd_buf_size = 0;
549 const char *readline_get_history(ReadLineState *rs, unsigned int index)
551 if (index >= READLINE_MAX_CMDS) {
552 return NULL;
554 return rs->history[index];
557 void readline_free(ReadLineState *rs)
559 int i;
561 if (!rs) {
562 return;
564 for (i = 0; i < READLINE_MAX_CMDS; i++) {
565 g_free(rs->history[i]);
567 g_free(rs);
570 ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
571 ReadLineFlushFunc *flush_func,
572 void *opaque,
573 ReadLineCompletionFunc *completion_finder)
575 ReadLineState *rs = g_new0(ReadLineState, 1);
577 rs->hist_entry = -1;
578 rs->opaque = opaque;
579 rs->printf_func = printf_func;
580 rs->flush_func = flush_func;
581 rs->completion_finder = completion_finder;
583 return rs;