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 "qemu-common.h"
27 #define TERM_CMD_BUF_SIZE 4095
28 #define TERM_MAX_CMDS 64
29 #define NB_COMPLETIONS_MAX 256
35 #define printf do_not_use_printf
37 static char term_cmd_buf
[TERM_CMD_BUF_SIZE
+ 1];
38 static int term_cmd_buf_index
;
39 static int term_cmd_buf_size
;
41 static char term_last_cmd_buf
[TERM_CMD_BUF_SIZE
+ 1];
42 static int term_last_cmd_buf_index
;
43 static int term_last_cmd_buf_size
;
45 static int term_esc_state
;
46 static int term_esc_param
;
48 static char *term_history
[TERM_MAX_CMDS
];
49 static int term_hist_entry
= -1;
51 static int nb_completions
;
53 static char *completions
[NB_COMPLETIONS_MAX
];
55 static ReadLineFunc
*term_readline_func
;
56 static int term_is_password
;
57 static char term_prompt
[256];
58 static void *term_readline_opaque
;
60 void readline_show_prompt(void)
62 term_printf("%s", term_prompt
);
64 term_last_cmd_buf_index
= 0;
65 term_last_cmd_buf_size
= 0;
66 term_esc_state
= IS_NORM
;
69 /* update the displayed command line */
70 static void term_update(void)
74 if (term_cmd_buf_size
!= term_last_cmd_buf_size
||
75 memcmp(term_cmd_buf
, term_last_cmd_buf
, term_cmd_buf_size
) != 0) {
76 for(i
= 0; i
< term_last_cmd_buf_index
; i
++) {
77 term_printf("\033[D");
79 term_cmd_buf
[term_cmd_buf_size
] = '\0';
80 if (term_is_password
) {
81 len
= strlen(term_cmd_buf
);
82 for(i
= 0; i
< len
; i
++)
85 term_printf("%s", term_cmd_buf
);
87 term_printf("\033[K");
88 memcpy(term_last_cmd_buf
, term_cmd_buf
, term_cmd_buf_size
);
89 term_last_cmd_buf_size
= term_cmd_buf_size
;
90 term_last_cmd_buf_index
= term_cmd_buf_size
;
92 if (term_cmd_buf_index
!= term_last_cmd_buf_index
) {
93 delta
= term_cmd_buf_index
- term_last_cmd_buf_index
;
95 for(i
= 0;i
< delta
; i
++) {
96 term_printf("\033[C");
100 for(i
= 0;i
< delta
; i
++) {
101 term_printf("\033[D");
104 term_last_cmd_buf_index
= term_cmd_buf_index
;
109 static void term_insert_char(int ch
)
111 if (term_cmd_buf_index
< TERM_CMD_BUF_SIZE
) {
112 memmove(term_cmd_buf
+ term_cmd_buf_index
+ 1,
113 term_cmd_buf
+ term_cmd_buf_index
,
114 term_cmd_buf_size
- term_cmd_buf_index
);
115 term_cmd_buf
[term_cmd_buf_index
] = ch
;
117 term_cmd_buf_index
++;
121 static void term_backward_char(void)
123 if (term_cmd_buf_index
> 0) {
124 term_cmd_buf_index
--;
128 static void term_forward_char(void)
130 if (term_cmd_buf_index
< term_cmd_buf_size
) {
131 term_cmd_buf_index
++;
135 static void term_delete_char(void)
137 if (term_cmd_buf_index
< term_cmd_buf_size
) {
138 memmove(term_cmd_buf
+ term_cmd_buf_index
,
139 term_cmd_buf
+ term_cmd_buf_index
+ 1,
140 term_cmd_buf_size
- term_cmd_buf_index
- 1);
145 static void term_backspace(void)
147 if (term_cmd_buf_index
> 0) {
148 term_backward_char();
153 static void term_backword(void)
157 if (term_cmd_buf_index
== 0 || term_cmd_buf_index
> term_cmd_buf_size
) {
161 start
= term_cmd_buf_index
- 1;
163 /* find first word (backwards) */
165 if (!qemu_isspace(term_cmd_buf
[start
])) {
172 /* find first space (backwards) */
174 if (qemu_isspace(term_cmd_buf
[start
])) {
183 if (start
< term_cmd_buf_index
) {
184 memmove(term_cmd_buf
+ start
,
185 term_cmd_buf
+ term_cmd_buf_index
,
186 term_cmd_buf_size
- term_cmd_buf_index
);
187 term_cmd_buf_size
-= term_cmd_buf_index
- start
;
188 term_cmd_buf_index
= start
;
192 static void term_bol(void)
194 term_cmd_buf_index
= 0;
197 static void term_eol(void)
199 term_cmd_buf_index
= term_cmd_buf_size
;
202 static void term_up_char(void)
206 if (term_hist_entry
== 0)
208 if (term_hist_entry
== -1) {
209 /* Find latest entry */
210 for (idx
= 0; idx
< TERM_MAX_CMDS
; idx
++) {
211 if (term_history
[idx
] == NULL
)
214 term_hist_entry
= idx
;
217 if (term_hist_entry
>= 0) {
218 pstrcpy(term_cmd_buf
, sizeof(term_cmd_buf
),
219 term_history
[term_hist_entry
]);
220 term_cmd_buf_index
= term_cmd_buf_size
= strlen(term_cmd_buf
);
224 static void term_down_char(void)
226 if (term_hist_entry
== TERM_MAX_CMDS
- 1 || term_hist_entry
== -1)
228 if (term_history
[++term_hist_entry
] != NULL
) {
229 pstrcpy(term_cmd_buf
, sizeof(term_cmd_buf
),
230 term_history
[term_hist_entry
]);
232 term_hist_entry
= -1;
234 term_cmd_buf_index
= term_cmd_buf_size
= strlen(term_cmd_buf
);
237 static void term_hist_add(const char *cmdline
)
239 char *hist_entry
, *new_entry
;
242 if (cmdline
[0] == '\0')
245 if (term_hist_entry
!= -1) {
246 /* We were editing an existing history entry: replace it */
247 hist_entry
= term_history
[term_hist_entry
];
248 idx
= term_hist_entry
;
249 if (strcmp(hist_entry
, cmdline
) == 0) {
253 /* Search cmdline in history buffers */
254 for (idx
= 0; idx
< TERM_MAX_CMDS
; idx
++) {
255 hist_entry
= term_history
[idx
];
256 if (hist_entry
== NULL
)
258 if (strcmp(hist_entry
, cmdline
) == 0) {
260 new_entry
= hist_entry
;
261 /* Put this entry at the end of history */
262 memmove(&term_history
[idx
], &term_history
[idx
+ 1],
263 (TERM_MAX_CMDS
- idx
+ 1) * sizeof(char *));
264 term_history
[TERM_MAX_CMDS
- 1] = NULL
;
265 for (; idx
< TERM_MAX_CMDS
; idx
++) {
266 if (term_history
[idx
] == NULL
)
272 if (idx
== TERM_MAX_CMDS
) {
273 /* Need to get one free slot */
274 free(term_history
[0]);
275 memcpy(term_history
, &term_history
[1],
276 (TERM_MAX_CMDS
- 1) * sizeof(char *));
277 term_history
[TERM_MAX_CMDS
- 1] = NULL
;
278 idx
= TERM_MAX_CMDS
- 1;
280 if (new_entry
== NULL
)
281 new_entry
= strdup(cmdline
);
282 term_history
[idx
] = new_entry
;
283 term_hist_entry
= -1;
286 /* completion support */
288 void add_completion(const char *str
)
290 if (nb_completions
< NB_COMPLETIONS_MAX
) {
291 completions
[nb_completions
++] = qemu_strdup(str
);
295 static void term_completion(void)
297 int len
, i
, j
, max_width
, nb_cols
, max_prefix
;
302 cmdline
= qemu_malloc(term_cmd_buf_index
+ 1);
303 memcpy(cmdline
, term_cmd_buf
, term_cmd_buf_index
);
304 cmdline
[term_cmd_buf_index
] = '\0';
305 readline_find_completion(cmdline
);
308 /* no completion found */
309 if (nb_completions
<= 0)
311 if (nb_completions
== 1) {
312 len
= strlen(completions
[0]);
313 for(i
= completion_index
; i
< len
; i
++) {
314 term_insert_char(completions
[0][i
]);
316 /* extra space for next argument. XXX: make it more generic */
317 if (len
> 0 && completions
[0][len
- 1] != '/')
318 term_insert_char(' ');
323 for(i
= 0; i
< nb_completions
; i
++) {
324 len
= strlen(completions
[i
]);
328 if (len
< max_prefix
)
330 for(j
=0; j
<max_prefix
; j
++) {
331 if (completions
[i
][j
] != completions
[0][j
])
339 for(i
= completion_index
; i
< max_prefix
; i
++) {
340 term_insert_char(completions
[0][i
]);
345 else if (max_width
> 80)
347 nb_cols
= 80 / max_width
;
349 for(i
= 0; i
< nb_completions
; i
++) {
350 term_printf("%-*s", max_width
, completions
[i
]);
351 if (++j
== nb_cols
|| i
== (nb_completions
- 1)) {
356 readline_show_prompt();
360 /* return true if command handled */
361 void readline_handle_byte(int ch
)
363 switch(term_esc_state
) {
380 term_cmd_buf
[term_cmd_buf_size
] = '\0';
381 if (!term_is_password
)
382 term_hist_add(term_cmd_buf
);
384 term_cmd_buf_index
= 0;
385 term_cmd_buf_size
= 0;
386 term_last_cmd_buf_index
= 0;
387 term_last_cmd_buf_size
= 0;
388 /* NOTE: readline_start can be called here */
389 term_readline_func(term_readline_opaque
, term_cmd_buf
);
396 term_esc_state
= IS_ESC
;
403 term_esc_state
= IS_CSI
;
407 term_insert_char(ch
);
414 term_esc_state
= IS_CSI
;
417 term_esc_state
= IS_NORM
;
431 term_backward_char();
437 term_esc_param
= term_esc_param
* 10 + (ch
- '0');
440 switch(term_esc_param
) {
455 term_esc_state
= IS_NORM
;
462 void readline_start(const char *prompt
, int is_password
,
463 ReadLineFunc
*readline_func
, void *opaque
)
465 pstrcpy(term_prompt
, sizeof(term_prompt
), prompt
);
466 term_readline_func
= readline_func
;
467 term_readline_opaque
= opaque
;
468 term_is_password
= is_password
;
469 term_cmd_buf_index
= 0;
470 term_cmd_buf_size
= 0;
473 const char *readline_get_history(unsigned int index
)
475 if (index
>= TERM_MAX_CMDS
)
477 return term_history
[index
];