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
31 #define printf do_not_use_printf
33 void readline_show_prompt(ReadLineState
*rs
)
35 monitor_printf(rs
->mon
, "%s", rs
->prompt
);
36 monitor_flush(rs
->mon
);
37 rs
->last_cmd_buf_index
= 0;
38 rs
->last_cmd_buf_size
= 0;
39 rs
->esc_state
= IS_NORM
;
42 /* update the displayed command line */
43 static void readline_update(ReadLineState
*rs
)
47 if (rs
->cmd_buf_size
!= rs
->last_cmd_buf_size
||
48 memcmp(rs
->cmd_buf
, rs
->last_cmd_buf
, rs
->cmd_buf_size
) != 0) {
49 for(i
= 0; i
< rs
->last_cmd_buf_index
; i
++) {
50 monitor_printf(rs
->mon
, "\033[D");
52 rs
->cmd_buf
[rs
->cmd_buf_size
] = '\0';
53 if (rs
->read_password
) {
54 len
= strlen(rs
->cmd_buf
);
55 for(i
= 0; i
< len
; i
++)
56 monitor_printf(rs
->mon
, "*");
58 monitor_printf(rs
->mon
, "%s", rs
->cmd_buf
);
60 monitor_printf(rs
->mon
, "\033[K");
61 memcpy(rs
->last_cmd_buf
, rs
->cmd_buf
, rs
->cmd_buf_size
);
62 rs
->last_cmd_buf_size
= rs
->cmd_buf_size
;
63 rs
->last_cmd_buf_index
= rs
->cmd_buf_size
;
65 if (rs
->cmd_buf_index
!= rs
->last_cmd_buf_index
) {
66 delta
= rs
->cmd_buf_index
- rs
->last_cmd_buf_index
;
68 for(i
= 0;i
< delta
; i
++) {
69 monitor_printf(rs
->mon
, "\033[C");
73 for(i
= 0;i
< delta
; i
++) {
74 monitor_printf(rs
->mon
, "\033[D");
77 rs
->last_cmd_buf_index
= rs
->cmd_buf_index
;
79 monitor_flush(rs
->mon
);
82 static void readline_insert_char(ReadLineState
*rs
, int ch
)
84 if (rs
->cmd_buf_index
< READLINE_CMD_BUF_SIZE
) {
85 memmove(rs
->cmd_buf
+ rs
->cmd_buf_index
+ 1,
86 rs
->cmd_buf
+ rs
->cmd_buf_index
,
87 rs
->cmd_buf_size
- rs
->cmd_buf_index
);
88 rs
->cmd_buf
[rs
->cmd_buf_index
] = ch
;
94 static void readline_backward_char(ReadLineState
*rs
)
96 if (rs
->cmd_buf_index
> 0) {
101 static void readline_forward_char(ReadLineState
*rs
)
103 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
108 static void readline_delete_char(ReadLineState
*rs
)
110 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
111 memmove(rs
->cmd_buf
+ rs
->cmd_buf_index
,
112 rs
->cmd_buf
+ rs
->cmd_buf_index
+ 1,
113 rs
->cmd_buf_size
- rs
->cmd_buf_index
- 1);
118 static void readline_backspace(ReadLineState
*rs
)
120 if (rs
->cmd_buf_index
> 0) {
121 readline_backward_char(rs
);
122 readline_delete_char(rs
);
126 static void readline_backword(ReadLineState
*rs
)
130 if (rs
->cmd_buf_index
== 0 || rs
->cmd_buf_index
> rs
->cmd_buf_size
) {
134 start
= rs
->cmd_buf_index
- 1;
136 /* find first word (backwards) */
138 if (!qemu_isspace(rs
->cmd_buf
[start
])) {
145 /* find first space (backwards) */
147 if (qemu_isspace(rs
->cmd_buf
[start
])) {
156 if (start
< rs
->cmd_buf_index
) {
157 memmove(rs
->cmd_buf
+ start
,
158 rs
->cmd_buf
+ rs
->cmd_buf_index
,
159 rs
->cmd_buf_size
- rs
->cmd_buf_index
);
160 rs
->cmd_buf_size
-= rs
->cmd_buf_index
- start
;
161 rs
->cmd_buf_index
= start
;
165 static void readline_bol(ReadLineState
*rs
)
167 rs
->cmd_buf_index
= 0;
170 static void readline_eol(ReadLineState
*rs
)
172 rs
->cmd_buf_index
= rs
->cmd_buf_size
;
175 static void readline_up_char(ReadLineState
*rs
)
179 if (rs
->hist_entry
== 0)
181 if (rs
->hist_entry
== -1) {
182 /* Find latest entry */
183 for (idx
= 0; idx
< READLINE_MAX_CMDS
; idx
++) {
184 if (rs
->history
[idx
] == NULL
)
187 rs
->hist_entry
= idx
;
190 if (rs
->hist_entry
>= 0) {
191 pstrcpy(rs
->cmd_buf
, sizeof(rs
->cmd_buf
),
192 rs
->history
[rs
->hist_entry
]);
193 rs
->cmd_buf_index
= rs
->cmd_buf_size
= strlen(rs
->cmd_buf
);
197 static void readline_down_char(ReadLineState
*rs
)
199 if (rs
->hist_entry
== READLINE_MAX_CMDS
- 1 || rs
->hist_entry
== -1)
201 if (rs
->history
[++rs
->hist_entry
] != NULL
) {
202 pstrcpy(rs
->cmd_buf
, sizeof(rs
->cmd_buf
),
203 rs
->history
[rs
->hist_entry
]);
207 rs
->cmd_buf_index
= rs
->cmd_buf_size
= strlen(rs
->cmd_buf
);
210 static void readline_hist_add(ReadLineState
*rs
, const char *cmdline
)
212 char *hist_entry
, *new_entry
;
215 if (cmdline
[0] == '\0')
218 if (rs
->hist_entry
!= -1) {
219 /* We were editing an existing history entry: replace it */
220 hist_entry
= rs
->history
[rs
->hist_entry
];
221 idx
= rs
->hist_entry
;
222 if (strcmp(hist_entry
, cmdline
) == 0) {
226 /* Search cmdline in history buffers */
227 for (idx
= 0; idx
< READLINE_MAX_CMDS
; idx
++) {
228 hist_entry
= rs
->history
[idx
];
229 if (hist_entry
== NULL
)
231 if (strcmp(hist_entry
, cmdline
) == 0) {
233 new_entry
= hist_entry
;
234 /* Put this entry at the end of history */
235 memmove(&rs
->history
[idx
], &rs
->history
[idx
+ 1],
236 (READLINE_MAX_CMDS
- idx
+ 1) * sizeof(char *));
237 rs
->history
[READLINE_MAX_CMDS
- 1] = NULL
;
238 for (; idx
< READLINE_MAX_CMDS
; idx
++) {
239 if (rs
->history
[idx
] == NULL
)
245 if (idx
== READLINE_MAX_CMDS
) {
246 /* Need to get one free slot */
247 free(rs
->history
[0]);
248 memcpy(rs
->history
, &rs
->history
[1],
249 (READLINE_MAX_CMDS
- 1) * sizeof(char *));
250 rs
->history
[READLINE_MAX_CMDS
- 1] = NULL
;
251 idx
= READLINE_MAX_CMDS
- 1;
253 if (new_entry
== NULL
)
254 new_entry
= strdup(cmdline
);
255 rs
->history
[idx
] = new_entry
;
259 /* completion support */
261 void readline_add_completion(ReadLineState
*rs
, const char *str
)
263 if (rs
->nb_completions
< READLINE_MAX_COMPLETIONS
) {
264 rs
->completions
[rs
->nb_completions
++] = qemu_strdup(str
);
268 void readline_set_completion_index(ReadLineState
*rs
, int index
)
270 rs
->completion_index
= index
;
273 static void readline_completion(ReadLineState
*rs
)
275 Monitor
*mon
= cur_mon
;
276 int len
, i
, j
, max_width
, nb_cols
, max_prefix
;
279 rs
->nb_completions
= 0;
281 cmdline
= qemu_malloc(rs
->cmd_buf_index
+ 1);
282 memcpy(cmdline
, rs
->cmd_buf
, rs
->cmd_buf_index
);
283 cmdline
[rs
->cmd_buf_index
] = '\0';
284 rs
->completion_finder(cmdline
);
287 /* no completion found */
288 if (rs
->nb_completions
<= 0)
290 if (rs
->nb_completions
== 1) {
291 len
= strlen(rs
->completions
[0]);
292 for(i
= rs
->completion_index
; i
< len
; i
++) {
293 readline_insert_char(rs
, rs
->completions
[0][i
]);
295 /* extra space for next argument. XXX: make it more generic */
296 if (len
> 0 && rs
->completions
[0][len
- 1] != '/')
297 readline_insert_char(rs
, ' ');
299 monitor_printf(mon
, "\n");
302 for(i
= 0; i
< rs
->nb_completions
; i
++) {
303 len
= strlen(rs
->completions
[i
]);
307 if (len
< max_prefix
)
309 for(j
=0; j
<max_prefix
; j
++) {
310 if (rs
->completions
[i
][j
] != rs
->completions
[0][j
])
318 for(i
= rs
->completion_index
; i
< max_prefix
; i
++) {
319 readline_insert_char(rs
, rs
->completions
[0][i
]);
324 else if (max_width
> 80)
326 nb_cols
= 80 / max_width
;
328 for(i
= 0; i
< rs
->nb_completions
; i
++) {
329 monitor_printf(rs
->mon
, "%-*s", max_width
, rs
->completions
[i
]);
330 if (++j
== nb_cols
|| i
== (rs
->nb_completions
- 1)) {
331 monitor_printf(rs
->mon
, "\n");
335 readline_show_prompt(rs
);
339 /* return true if command handled */
340 void readline_handle_byte(ReadLineState
*rs
, int ch
)
342 switch(rs
->esc_state
) {
349 readline_delete_char(rs
);
355 readline_completion(rs
);
359 rs
->cmd_buf
[rs
->cmd_buf_size
] = '\0';
360 if (!rs
->read_password
)
361 readline_hist_add(rs
, rs
->cmd_buf
);
362 monitor_printf(rs
->mon
, "\n");
363 rs
->cmd_buf_index
= 0;
364 rs
->cmd_buf_size
= 0;
365 rs
->last_cmd_buf_index
= 0;
366 rs
->last_cmd_buf_size
= 0;
367 rs
->readline_func(rs
->mon
, rs
->cmd_buf
, rs
->readline_opaque
);
371 readline_backword(rs
);
374 rs
->esc_state
= IS_ESC
;
378 readline_backspace(rs
);
381 rs
->esc_state
= IS_CSI
;
385 readline_insert_char(rs
, ch
);
392 rs
->esc_state
= IS_CSI
;
395 rs
->esc_state
= IS_NORM
;
402 readline_up_char(rs
);
406 readline_down_char(rs
);
409 readline_backward_char(rs
);
412 readline_forward_char(rs
);
415 rs
->esc_param
= rs
->esc_param
* 10 + (ch
- '0');
418 switch(rs
->esc_param
) {
423 readline_delete_char(rs
);
433 rs
->esc_state
= IS_NORM
;
440 void readline_start(ReadLineState
*rs
, const char *prompt
, int read_password
,
441 ReadLineFunc
*readline_func
, void *opaque
)
443 pstrcpy(rs
->prompt
, sizeof(rs
->prompt
), prompt
);
444 rs
->readline_func
= readline_func
;
445 rs
->readline_opaque
= opaque
;
446 rs
->read_password
= read_password
;
447 readline_restart(rs
);
450 void readline_restart(ReadLineState
*rs
)
452 rs
->cmd_buf_index
= 0;
453 rs
->cmd_buf_size
= 0;
456 const char *readline_get_history(ReadLineState
*rs
, unsigned int index
)
458 if (index
>= READLINE_MAX_CMDS
)
460 return rs
->history
[index
];
463 ReadLineState
*readline_init(Monitor
*mon
,
464 ReadLineCompletionFunc
*completion_finder
)
466 ReadLineState
*rs
= qemu_mallocz(sizeof(*rs
));
473 rs
->completion_finder
= completion_finder
;