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
24 #include "monitor/readline.h"
25 #include "monitor/monitor.h"
33 #define printf do_not_use_printf
35 void readline_show_prompt(ReadLineState
*rs
)
37 monitor_printf(rs
->mon
, "%s", rs
->prompt
);
38 monitor_flush(rs
->mon
);
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 monitor_printf(rs
->mon
, "\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 monitor_printf(rs
->mon
, "*");
60 monitor_printf(rs
->mon
, "%s", rs
->cmd_buf
);
62 monitor_printf(rs
->mon
, "\033[K");
63 memcpy(rs
->last_cmd_buf
, rs
->cmd_buf
, rs
->cmd_buf_size
);
64 rs
->last_cmd_buf_size
= rs
->cmd_buf_size
;
65 rs
->last_cmd_buf_index
= rs
->cmd_buf_size
;
67 if (rs
->cmd_buf_index
!= rs
->last_cmd_buf_index
) {
68 delta
= rs
->cmd_buf_index
- rs
->last_cmd_buf_index
;
70 for(i
= 0;i
< delta
; i
++) {
71 monitor_printf(rs
->mon
, "\033[C");
75 for(i
= 0;i
< delta
; i
++) {
76 monitor_printf(rs
->mon
, "\033[D");
79 rs
->last_cmd_buf_index
= rs
->cmd_buf_index
;
81 monitor_flush(rs
->mon
);
84 static void readline_insert_char(ReadLineState
*rs
, int ch
)
86 if (rs
->cmd_buf_index
< READLINE_CMD_BUF_SIZE
) {
87 memmove(rs
->cmd_buf
+ rs
->cmd_buf_index
+ 1,
88 rs
->cmd_buf
+ rs
->cmd_buf_index
,
89 rs
->cmd_buf_size
- rs
->cmd_buf_index
);
90 rs
->cmd_buf
[rs
->cmd_buf_index
] = ch
;
96 static void readline_backward_char(ReadLineState
*rs
)
98 if (rs
->cmd_buf_index
> 0) {
103 static void readline_forward_char(ReadLineState
*rs
)
105 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
110 static void readline_delete_char(ReadLineState
*rs
)
112 if (rs
->cmd_buf_index
< rs
->cmd_buf_size
) {
113 memmove(rs
->cmd_buf
+ rs
->cmd_buf_index
,
114 rs
->cmd_buf
+ rs
->cmd_buf_index
+ 1,
115 rs
->cmd_buf_size
- rs
->cmd_buf_index
- 1);
120 static void readline_backspace(ReadLineState
*rs
)
122 if (rs
->cmd_buf_index
> 0) {
123 readline_backward_char(rs
);
124 readline_delete_char(rs
);
128 static void readline_backword(ReadLineState
*rs
)
132 if (rs
->cmd_buf_index
== 0 || rs
->cmd_buf_index
> rs
->cmd_buf_size
) {
136 start
= rs
->cmd_buf_index
- 1;
138 /* find first word (backwards) */
140 if (!qemu_isspace(rs
->cmd_buf
[start
])) {
147 /* find first space (backwards) */
149 if (qemu_isspace(rs
->cmd_buf
[start
])) {
158 if (start
< rs
->cmd_buf_index
) {
159 memmove(rs
->cmd_buf
+ start
,
160 rs
->cmd_buf
+ rs
->cmd_buf_index
,
161 rs
->cmd_buf_size
- rs
->cmd_buf_index
);
162 rs
->cmd_buf_size
-= rs
->cmd_buf_index
- start
;
163 rs
->cmd_buf_index
= start
;
167 static void readline_bol(ReadLineState
*rs
)
169 rs
->cmd_buf_index
= 0;
172 static void readline_eol(ReadLineState
*rs
)
174 rs
->cmd_buf_index
= rs
->cmd_buf_size
;
177 static void readline_up_char(ReadLineState
*rs
)
181 if (rs
->hist_entry
== 0)
183 if (rs
->hist_entry
== -1) {
184 /* Find latest entry */
185 for (idx
= 0; idx
< READLINE_MAX_CMDS
; idx
++) {
186 if (rs
->history
[idx
] == NULL
)
189 rs
->hist_entry
= idx
;
192 if (rs
->hist_entry
>= 0) {
193 pstrcpy(rs
->cmd_buf
, sizeof(rs
->cmd_buf
),
194 rs
->history
[rs
->hist_entry
]);
195 rs
->cmd_buf_index
= rs
->cmd_buf_size
= strlen(rs
->cmd_buf
);
199 static void readline_down_char(ReadLineState
*rs
)
201 if (rs
->hist_entry
== -1)
203 if (rs
->hist_entry
< READLINE_MAX_CMDS
- 1 &&
204 rs
->history
[++rs
->hist_entry
] != NULL
) {
205 pstrcpy(rs
->cmd_buf
, sizeof(rs
->cmd_buf
),
206 rs
->history
[rs
->hist_entry
]);
211 rs
->cmd_buf_index
= rs
->cmd_buf_size
= strlen(rs
->cmd_buf
);
214 static void readline_hist_add(ReadLineState
*rs
, const char *cmdline
)
216 char *hist_entry
, *new_entry
;
219 if (cmdline
[0] == '\0')
222 if (rs
->hist_entry
!= -1) {
223 /* We were editing an existing history entry: replace it */
224 hist_entry
= rs
->history
[rs
->hist_entry
];
225 idx
= rs
->hist_entry
;
226 if (strcmp(hist_entry
, cmdline
) == 0) {
230 /* Search cmdline in history buffers */
231 for (idx
= 0; idx
< READLINE_MAX_CMDS
; idx
++) {
232 hist_entry
= rs
->history
[idx
];
233 if (hist_entry
== NULL
)
235 if (strcmp(hist_entry
, cmdline
) == 0) {
237 new_entry
= hist_entry
;
238 /* Put this entry at the end of history */
239 memmove(&rs
->history
[idx
], &rs
->history
[idx
+ 1],
240 (READLINE_MAX_CMDS
- (idx
+ 1)) * sizeof(char *));
241 rs
->history
[READLINE_MAX_CMDS
- 1] = NULL
;
242 for (; idx
< READLINE_MAX_CMDS
; idx
++) {
243 if (rs
->history
[idx
] == NULL
)
249 if (idx
== READLINE_MAX_CMDS
) {
250 /* Need to get one free slot */
251 g_free(rs
->history
[0]);
252 memmove(rs
->history
, &rs
->history
[1],
253 (READLINE_MAX_CMDS
- 1) * sizeof(char *));
254 rs
->history
[READLINE_MAX_CMDS
- 1] = NULL
;
255 idx
= READLINE_MAX_CMDS
- 1;
257 if (new_entry
== NULL
)
258 new_entry
= g_strdup(cmdline
);
259 rs
->history
[idx
] = new_entry
;
263 /* completion support */
265 void readline_add_completion(ReadLineState
*rs
, const char *str
)
267 if (rs
->nb_completions
< READLINE_MAX_COMPLETIONS
) {
268 rs
->completions
[rs
->nb_completions
++] = g_strdup(str
);
272 void readline_set_completion_index(ReadLineState
*rs
, int index
)
274 rs
->completion_index
= index
;
277 static void readline_completion(ReadLineState
*rs
)
279 Monitor
*mon
= cur_mon
;
280 int len
, i
, j
, max_width
, nb_cols
, max_prefix
;
283 rs
->nb_completions
= 0;
285 cmdline
= g_malloc(rs
->cmd_buf_index
+ 1);
286 memcpy(cmdline
, rs
->cmd_buf
, rs
->cmd_buf_index
);
287 cmdline
[rs
->cmd_buf_index
] = '\0';
288 rs
->completion_finder(cmdline
);
291 /* no completion found */
292 if (rs
->nb_completions
<= 0)
294 if (rs
->nb_completions
== 1) {
295 len
= strlen(rs
->completions
[0]);
296 for(i
= rs
->completion_index
; i
< len
; i
++) {
297 readline_insert_char(rs
, rs
->completions
[0][i
]);
299 /* extra space for next argument. XXX: make it more generic */
300 if (len
> 0 && rs
->completions
[0][len
- 1] != '/')
301 readline_insert_char(rs
, ' ');
303 monitor_printf(mon
, "\n");
306 for(i
= 0; i
< rs
->nb_completions
; i
++) {
307 len
= strlen(rs
->completions
[i
]);
311 if (len
< max_prefix
)
313 for(j
=0; j
<max_prefix
; j
++) {
314 if (rs
->completions
[i
][j
] != rs
->completions
[0][j
])
322 for(i
= rs
->completion_index
; i
< max_prefix
; i
++) {
323 readline_insert_char(rs
, rs
->completions
[0][i
]);
328 else if (max_width
> 80)
330 nb_cols
= 80 / max_width
;
332 for(i
= 0; i
< rs
->nb_completions
; i
++) {
333 monitor_printf(rs
->mon
, "%-*s", max_width
, rs
->completions
[i
]);
334 if (++j
== nb_cols
|| i
== (rs
->nb_completions
- 1)) {
335 monitor_printf(rs
->mon
, "\n");
339 readline_show_prompt(rs
);
341 for (i
= 0; i
< rs
->nb_completions
; i
++) {
342 g_free(rs
->completions
[i
]);
346 /* return true if command handled */
347 void readline_handle_byte(ReadLineState
*rs
, int ch
)
349 switch(rs
->esc_state
) {
356 readline_delete_char(rs
);
362 readline_completion(rs
);
366 rs
->cmd_buf
[rs
->cmd_buf_size
] = '\0';
367 if (!rs
->read_password
)
368 readline_hist_add(rs
, rs
->cmd_buf
);
369 monitor_printf(rs
->mon
, "\n");
370 rs
->cmd_buf_index
= 0;
371 rs
->cmd_buf_size
= 0;
372 rs
->last_cmd_buf_index
= 0;
373 rs
->last_cmd_buf_size
= 0;
374 rs
->readline_func(rs
->mon
, rs
->cmd_buf
, rs
->readline_opaque
);
378 readline_backword(rs
);
381 rs
->esc_state
= IS_ESC
;
385 readline_backspace(rs
);
388 rs
->esc_state
= IS_CSI
;
392 readline_insert_char(rs
, ch
);
399 rs
->esc_state
= IS_CSI
;
401 } else if (ch
== 'O') {
402 rs
->esc_state
= IS_SS3
;
405 rs
->esc_state
= IS_NORM
;
412 readline_up_char(rs
);
416 readline_down_char(rs
);
419 readline_backward_char(rs
);
422 readline_forward_char(rs
);
425 rs
->esc_param
= rs
->esc_param
* 10 + (ch
- '0');
428 switch(rs
->esc_param
) {
433 readline_delete_char(rs
);
443 rs
->esc_state
= IS_NORM
;
455 rs
->esc_state
= IS_NORM
;
461 void readline_start(ReadLineState
*rs
, const char *prompt
, int read_password
,
462 ReadLineFunc
*readline_func
, void *opaque
)
464 pstrcpy(rs
->prompt
, sizeof(rs
->prompt
), prompt
);
465 rs
->readline_func
= readline_func
;
466 rs
->readline_opaque
= opaque
;
467 rs
->read_password
= read_password
;
468 readline_restart(rs
);
471 void readline_restart(ReadLineState
*rs
)
473 rs
->cmd_buf_index
= 0;
474 rs
->cmd_buf_size
= 0;
477 const char *readline_get_history(ReadLineState
*rs
, unsigned int index
)
479 if (index
>= READLINE_MAX_CMDS
)
481 return rs
->history
[index
];
484 ReadLineState
*readline_init(Monitor
*mon
,
485 ReadLineCompletionFunc
*completion_finder
)
487 ReadLineState
*rs
= g_malloc0(sizeof(*rs
));
491 rs
->completion_finder
= completion_finder
;