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-common.h"
27 #include "qemu/readline.h"
34 void readline_show_prompt(ReadLineState
*rs
)
36 rs
->printf_func(rs
->opaque
, "%s", rs
->prompt
);
37 rs
->flush_func(rs
->opaque
);
38 rs
->last_cmd_buf_index
= 0;
39 rs
->last_cmd_buf_size
= 0;
40 rs
->esc_state
= IS_NORM
;
43 /* update the displayed command line */
44 static void readline_update(ReadLineState
*rs
)
48 if (rs
->cmd_buf_size
!= rs
->last_cmd_buf_size
||
49 memcmp(rs
->cmd_buf
, rs
->last_cmd_buf
, rs
->cmd_buf_size
) != 0) {
50 for(i
= 0; i
< rs
->last_cmd_buf_index
; i
++) {
51 rs
->printf_func(rs
->opaque
, "\033[D");
53 rs
->cmd_buf
[rs
->cmd_buf_size
] = '\0';
54 if (rs
->read_password
) {
55 len
= strlen(rs
->cmd_buf
);
56 for(i
= 0; i
< len
; i
++)
57 rs
->printf_func(rs
->opaque
, "*");
59 rs
->printf_func(rs
->opaque
, "%s", rs
->cmd_buf
);
61 rs
->printf_func(rs
->opaque
, "\033[K");
62 memcpy(rs
->last_cmd_buf
, rs
->cmd_buf
, rs
->cmd_buf_size
);
63 rs
->last_cmd_buf_size
= rs
->cmd_buf_size
;
64 rs
->last_cmd_buf_index
= rs
->cmd_buf_size
;
66 if (rs
->cmd_buf_index
!= rs
->last_cmd_buf_index
) {
67 delta
= rs
->cmd_buf_index
- rs
->last_cmd_buf_index
;
69 for(i
= 0;i
< delta
; i
++) {
70 rs
->printf_func(rs
->opaque
, "\033[C");
74 for(i
= 0;i
< delta
; i
++) {
75 rs
->printf_func(rs
->opaque
, "\033[D");
78 rs
->last_cmd_buf_index
= rs
->cmd_buf_index
;
80 rs
->flush_func(rs
->opaque
);
83 static void readline_insert_char(ReadLineState
*rs
, int ch
)
85 if (rs
->cmd_buf_index
< READLINE_CMD_BUF_SIZE
) {
86 memmove(rs
->cmd_buf
+ rs
->cmd_buf_index
+ 1,
87 rs
->cmd_buf
+ rs
->cmd_buf_index
,
88 rs
->cmd_buf_size
- rs
->cmd_buf_index
);
89 rs
->cmd_buf
[rs
->cmd_buf_index
] = ch
;
95 static void readline_backward_char(ReadLineState
*rs
)
97 if (rs
->cmd_buf_index
> 0) {
102 static void readline_forward_char(ReadLineState
*rs
)
104 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
109 static void readline_delete_char(ReadLineState
*rs
)
111 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
112 memmove(rs
->cmd_buf
+ rs
->cmd_buf_index
,
113 rs
->cmd_buf
+ rs
->cmd_buf_index
+ 1,
114 rs
->cmd_buf_size
- rs
->cmd_buf_index
- 1);
119 static void readline_backspace(ReadLineState
*rs
)
121 if (rs
->cmd_buf_index
> 0) {
122 readline_backward_char(rs
);
123 readline_delete_char(rs
);
127 static void readline_backword(ReadLineState
*rs
)
131 if (rs
->cmd_buf_index
== 0 || rs
->cmd_buf_index
> rs
->cmd_buf_size
) {
135 start
= rs
->cmd_buf_index
- 1;
137 /* find first word (backwards) */
139 if (!qemu_isspace(rs
->cmd_buf
[start
])) {
146 /* find first space (backwards) */
148 if (qemu_isspace(rs
->cmd_buf
[start
])) {
157 if (start
< rs
->cmd_buf_index
) {
158 memmove(rs
->cmd_buf
+ start
,
159 rs
->cmd_buf
+ rs
->cmd_buf_index
,
160 rs
->cmd_buf_size
- rs
->cmd_buf_index
);
161 rs
->cmd_buf_size
-= rs
->cmd_buf_index
- start
;
162 rs
->cmd_buf_index
= start
;
166 static void readline_bol(ReadLineState
*rs
)
168 rs
->cmd_buf_index
= 0;
171 static void readline_eol(ReadLineState
*rs
)
173 rs
->cmd_buf_index
= rs
->cmd_buf_size
;
176 static void readline_up_char(ReadLineState
*rs
)
180 if (rs
->hist_entry
== 0)
182 if (rs
->hist_entry
== -1) {
183 /* Find latest entry */
184 for (idx
= 0; idx
< READLINE_MAX_CMDS
; idx
++) {
185 if (rs
->history
[idx
] == NULL
)
188 rs
->hist_entry
= idx
;
191 if (rs
->hist_entry
>= 0) {
192 pstrcpy(rs
->cmd_buf
, sizeof(rs
->cmd_buf
),
193 rs
->history
[rs
->hist_entry
]);
194 rs
->cmd_buf_index
= rs
->cmd_buf_size
= strlen(rs
->cmd_buf
);
198 static void readline_down_char(ReadLineState
*rs
)
200 if (rs
->hist_entry
== -1)
202 if (rs
->hist_entry
< READLINE_MAX_CMDS
- 1 &&
203 rs
->history
[++rs
->hist_entry
] != NULL
) {
204 pstrcpy(rs
->cmd_buf
, sizeof(rs
->cmd_buf
),
205 rs
->history
[rs
->hist_entry
]);
210 rs
->cmd_buf_index
= rs
->cmd_buf_size
= strlen(rs
->cmd_buf
);
213 static void readline_hist_add(ReadLineState
*rs
, const char *cmdline
)
215 char *hist_entry
, *new_entry
;
218 if (cmdline
[0] == '\0')
221 if (rs
->hist_entry
!= -1) {
222 /* We were editing an existing history entry: replace it */
223 hist_entry
= rs
->history
[rs
->hist_entry
];
224 idx
= rs
->hist_entry
;
225 if (strcmp(hist_entry
, cmdline
) == 0) {
229 /* Search cmdline in history buffers */
230 for (idx
= 0; idx
< READLINE_MAX_CMDS
; idx
++) {
231 hist_entry
= rs
->history
[idx
];
232 if (hist_entry
== NULL
)
234 if (strcmp(hist_entry
, cmdline
) == 0) {
236 new_entry
= hist_entry
;
237 /* Put this entry at the end of history */
238 memmove(&rs
->history
[idx
], &rs
->history
[idx
+ 1],
239 (READLINE_MAX_CMDS
- (idx
+ 1)) * sizeof(char *));
240 rs
->history
[READLINE_MAX_CMDS
- 1] = NULL
;
241 for (; idx
< READLINE_MAX_CMDS
; idx
++) {
242 if (rs
->history
[idx
] == NULL
)
248 if (idx
== READLINE_MAX_CMDS
) {
249 /* Need to get one free slot */
250 g_free(rs
->history
[0]);
251 memmove(rs
->history
, &rs
->history
[1],
252 (READLINE_MAX_CMDS
- 1) * sizeof(char *));
253 rs
->history
[READLINE_MAX_CMDS
- 1] = NULL
;
254 idx
= READLINE_MAX_CMDS
- 1;
256 if (new_entry
== NULL
)
257 new_entry
= g_strdup(cmdline
);
258 rs
->history
[idx
] = new_entry
;
262 /* completion support */
264 void readline_add_completion(ReadLineState
*rs
, const char *str
)
266 if (rs
->nb_completions
< READLINE_MAX_COMPLETIONS
) {
268 for (i
= 0; i
< rs
->nb_completions
; i
++) {
269 if (!strcmp(rs
->completions
[i
], str
)) {
273 rs
->completions
[rs
->nb_completions
++] = g_strdup(str
);
277 void readline_set_completion_index(ReadLineState
*rs
, int index
)
279 rs
->completion_index
= index
;
282 static int completion_comp(const void *a
, const void *b
)
284 return strcmp(*(const char **) a
, *(const char **) b
);
287 static void readline_completion(ReadLineState
*rs
)
289 int len
, i
, j
, max_width
, nb_cols
, max_prefix
;
292 rs
->nb_completions
= 0;
294 cmdline
= g_strndup(rs
->cmd_buf
, rs
->cmd_buf_index
);
295 rs
->completion_finder(rs
->opaque
, cmdline
);
298 /* no completion found */
299 if (rs
->nb_completions
<= 0)
301 if (rs
->nb_completions
== 1) {
302 len
= strlen(rs
->completions
[0]);
303 for(i
= rs
->completion_index
; i
< len
; i
++) {
304 readline_insert_char(rs
, rs
->completions
[0][i
]);
306 /* extra space for next argument. XXX: make it more generic */
307 if (len
> 0 && rs
->completions
[0][len
- 1] != '/')
308 readline_insert_char(rs
, ' ');
310 qsort(rs
->completions
, rs
->nb_completions
, sizeof(char *),
312 rs
->printf_func(rs
->opaque
, "\n");
315 for(i
= 0; i
< rs
->nb_completions
; i
++) {
316 len
= strlen(rs
->completions
[i
]);
320 if (len
< max_prefix
)
322 for(j
=0; j
<max_prefix
; j
++) {
323 if (rs
->completions
[i
][j
] != rs
->completions
[0][j
])
331 for(i
= rs
->completion_index
; i
< max_prefix
; i
++) {
332 readline_insert_char(rs
, rs
->completions
[0][i
]);
337 else if (max_width
> 80)
339 nb_cols
= 80 / max_width
;
341 for(i
= 0; i
< rs
->nb_completions
; i
++) {
342 rs
->printf_func(rs
->opaque
, "%-*s", max_width
, rs
->completions
[i
]);
343 if (++j
== nb_cols
|| i
== (rs
->nb_completions
- 1)) {
344 rs
->printf_func(rs
->opaque
, "\n");
348 readline_show_prompt(rs
);
350 for (i
= 0; i
< rs
->nb_completions
; i
++) {
351 g_free(rs
->completions
[i
]);
355 static void readline_clear_screen(ReadLineState
*rs
)
357 rs
->printf_func(rs
->opaque
, "\033[2J\033[1;1H");
358 readline_show_prompt(rs
);
361 /* return true if command handled */
362 void readline_handle_byte(ReadLineState
*rs
, int ch
)
364 switch(rs
->esc_state
) {
371 readline_delete_char(rs
);
377 readline_completion(rs
);
380 readline_clear_screen(rs
);
384 rs
->cmd_buf
[rs
->cmd_buf_size
] = '\0';
385 if (!rs
->read_password
)
386 readline_hist_add(rs
, rs
->cmd_buf
);
387 rs
->printf_func(rs
->opaque
, "\n");
388 rs
->cmd_buf_index
= 0;
389 rs
->cmd_buf_size
= 0;
390 rs
->last_cmd_buf_index
= 0;
391 rs
->last_cmd_buf_size
= 0;
392 rs
->readline_func(rs
->opaque
, rs
->cmd_buf
, rs
->readline_opaque
);
396 readline_backword(rs
);
399 rs
->esc_state
= IS_ESC
;
403 readline_backspace(rs
);
406 rs
->esc_state
= IS_CSI
;
410 readline_insert_char(rs
, ch
);
417 rs
->esc_state
= IS_CSI
;
419 } else if (ch
== 'O') {
420 rs
->esc_state
= IS_SS3
;
423 rs
->esc_state
= IS_NORM
;
430 readline_up_char(rs
);
434 readline_down_char(rs
);
437 readline_backward_char(rs
);
440 readline_forward_char(rs
);
443 rs
->esc_param
= rs
->esc_param
* 10 + (ch
- '0');
446 switch(rs
->esc_param
) {
451 readline_delete_char(rs
);
461 rs
->esc_state
= IS_NORM
;
473 rs
->esc_state
= IS_NORM
;
479 void readline_start(ReadLineState
*rs
, const char *prompt
, int read_password
,
480 ReadLineFunc
*readline_func
, void *opaque
)
482 pstrcpy(rs
->prompt
, sizeof(rs
->prompt
), prompt
);
483 rs
->readline_func
= readline_func
;
484 rs
->readline_opaque
= opaque
;
485 rs
->read_password
= read_password
;
486 readline_restart(rs
);
489 void readline_restart(ReadLineState
*rs
)
491 rs
->cmd_buf_index
= 0;
492 rs
->cmd_buf_size
= 0;
495 const char *readline_get_history(ReadLineState
*rs
, unsigned int index
)
497 if (index
>= READLINE_MAX_CMDS
)
499 return rs
->history
[index
];
502 ReadLineState
*readline_init(ReadLinePrintfFunc
*printf_func
,
503 ReadLineFlushFunc
*flush_func
,
505 ReadLineCompletionFunc
*completion_finder
)
507 ReadLineState
*rs
= g_malloc0(sizeof(*rs
));
511 rs
->printf_func
= printf_func
;
512 rs
->flush_func
= flush_func
;
513 rs
->completion_finder
= completion_finder
;