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
25 #include "qemu/osdep.h"
26 #include "qemu/readline.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
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
)
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
, "*");
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
;
71 for (i
= 0; i
< delta
; i
++) {
72 rs
->printf_func(rs
->opaque
, "\033[C");
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
;
97 static void readline_backward_char(ReadLineState
*rs
)
99 if (rs
->cmd_buf_index
> 0) {
104 static void readline_forward_char(ReadLineState
*rs
)
106 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
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);
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
)
133 if (rs
->cmd_buf_index
== 0 || rs
->cmd_buf_index
> rs
->cmd_buf_size
) {
137 start
= rs
->cmd_buf_index
- 1;
139 /* find first word (backwards) */
141 if (!qemu_isspace(rs
->cmd_buf
[start
])) {
148 /* find first space (backwards) */
150 if (qemu_isspace(rs
->cmd_buf
[start
])) {
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
)
182 if (rs
->hist_entry
== 0) {
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
) {
192 rs
->hist_entry
= idx
;
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) {
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
]);
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
;
223 if (cmdline
[0] == '\0') {
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) {
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
) {
241 if (strcmp(hist_entry
, cmdline
) == 0) {
243 if (idx
== READLINE_MAX_CMDS
- 1) {
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
) {
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
;
274 /* completion support */
276 void readline_add_completion(ReadLineState
*rs
, const char *str
)
278 if (rs
->nb_completions
< READLINE_MAX_COMPLETIONS
) {
280 for (i
= 0; i
< rs
->nb_completions
; i
++) {
281 if (!strcmp(rs
->completions
[i
], str
)) {
285 rs
->completions
[rs
->nb_completions
++] = g_strdup(str
);
289 void readline_set_completion_index(ReadLineState
*rs
, int index
)
291 rs
->completion_index
= index
;
294 static int completion_comp(const void *a
, const void *b
)
296 return strcmp(*(const char **) a
, *(const char **) b
);
299 static void readline_completion(ReadLineState
*rs
)
301 int len
, i
, j
, max_width
, nb_cols
, max_prefix
;
304 rs
->nb_completions
= 0;
306 cmdline
= g_strndup(rs
->cmd_buf
, rs
->cmd_buf_index
);
307 rs
->completion_finder(rs
->opaque
, cmdline
);
310 /* no completion found */
311 if (rs
->nb_completions
<= 0) {
314 if (rs
->nb_completions
== 1) {
315 len
= strlen(rs
->completions
[0]);
316 for (i
= rs
->completion_index
; i
< len
; i
++) {
317 readline_insert_char(rs
, rs
->completions
[0][i
]);
319 /* extra space for next argument. XXX: make it more generic */
320 if (len
> 0 && rs
->completions
[0][len
- 1] != '/') {
321 readline_insert_char(rs
, ' ');
324 qsort(rs
->completions
, rs
->nb_completions
, sizeof(char *),
326 rs
->printf_func(rs
->opaque
, "\n");
329 for (i
= 0; i
< rs
->nb_completions
; i
++) {
330 len
= strlen(rs
->completions
[i
]);
334 if (len
< max_prefix
) {
337 for (j
= 0; j
< max_prefix
; j
++) {
338 if (rs
->completions
[i
][j
] != rs
->completions
[0][j
]) {
343 if (len
> max_width
) {
348 for (i
= rs
->completion_index
; i
< max_prefix
; i
++) {
349 readline_insert_char(rs
, rs
->completions
[0][i
]);
352 if (max_width
< 10) {
354 } else if (max_width
> 80) {
357 nb_cols
= 80 / max_width
;
359 for (i
= 0; i
< rs
->nb_completions
; i
++) {
360 rs
->printf_func(rs
->opaque
, "%-*s", max_width
, rs
->completions
[i
]);
361 if (++j
== nb_cols
|| i
== (rs
->nb_completions
- 1)) {
362 rs
->printf_func(rs
->opaque
, "\n");
366 readline_show_prompt(rs
);
368 for (i
= 0; i
< rs
->nb_completions
; i
++) {
369 g_free(rs
->completions
[i
]);
373 static void readline_clear_screen(ReadLineState
*rs
)
375 rs
->printf_func(rs
->opaque
, "\033[2J\033[1;1H");
376 readline_show_prompt(rs
);
379 /* return true if command handled */
380 void readline_handle_byte(ReadLineState
*rs
, int ch
)
382 switch (rs
->esc_state
) {
389 readline_delete_char(rs
);
395 readline_completion(rs
);
398 readline_clear_screen(rs
);
402 rs
->cmd_buf
[rs
->cmd_buf_size
] = '\0';
403 if (!rs
->read_password
) {
404 readline_hist_add(rs
, rs
->cmd_buf
);
406 rs
->printf_func(rs
->opaque
, "\n");
407 rs
->cmd_buf_index
= 0;
408 rs
->cmd_buf_size
= 0;
409 rs
->last_cmd_buf_index
= 0;
410 rs
->last_cmd_buf_size
= 0;
411 rs
->readline_func(rs
->opaque
, rs
->cmd_buf
, rs
->readline_opaque
);
415 readline_backword(rs
);
418 rs
->esc_state
= IS_ESC
;
422 readline_backspace(rs
);
425 rs
->esc_state
= IS_CSI
;
429 readline_insert_char(rs
, ch
);
436 rs
->esc_state
= IS_CSI
;
438 } else if (ch
== 'O') {
439 rs
->esc_state
= IS_SS3
;
442 rs
->esc_state
= IS_NORM
;
449 readline_up_char(rs
);
453 readline_down_char(rs
);
456 readline_backward_char(rs
);
459 readline_forward_char(rs
);
462 rs
->esc_param
= rs
->esc_param
* 10 + (ch
- '0');
465 switch (rs
->esc_param
) {
470 readline_delete_char(rs
);
480 rs
->esc_state
= IS_NORM
;
492 rs
->esc_state
= IS_NORM
;
498 void readline_start(ReadLineState
*rs
, const char *prompt
, int read_password
,
499 ReadLineFunc
*readline_func
, void *opaque
)
501 pstrcpy(rs
->prompt
, sizeof(rs
->prompt
), prompt
);
502 rs
->readline_func
= readline_func
;
503 rs
->readline_opaque
= opaque
;
504 rs
->read_password
= read_password
;
505 readline_restart(rs
);
508 void readline_restart(ReadLineState
*rs
)
510 rs
->cmd_buf_index
= 0;
511 rs
->cmd_buf_size
= 0;
514 const char *readline_get_history(ReadLineState
*rs
, unsigned int index
)
516 if (index
>= READLINE_MAX_CMDS
) {
519 return rs
->history
[index
];
522 void readline_free(ReadLineState
*rs
)
529 for (i
= 0; i
< READLINE_MAX_CMDS
; i
++) {
530 g_free(rs
->history
[i
]);
535 ReadLineState
*readline_init(ReadLinePrintfFunc
*printf_func
,
536 ReadLineFlushFunc
*flush_func
,
538 ReadLineCompletionFunc
*completion_finder
)
540 ReadLineState
*rs
= g_new0(ReadLineState
, 1);
544 rs
->printf_func
= printf_func
;
545 rs
->flush_func
= flush_func
;
546 rs
->completion_finder
= completion_finder
;