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
26 #define TERM_CMD_BUF_SIZE 4095
27 #define TERM_MAX_CMDS 64
28 #define NB_COMPLETIONS_MAX 256
34 #define printf do_not_use_printf
36 static char term_cmd_buf
[TERM_CMD_BUF_SIZE
+ 1];
37 static int term_cmd_buf_index
;
38 static int term_cmd_buf_size
;
40 static char term_last_cmd_buf
[TERM_CMD_BUF_SIZE
+ 1];
41 static int term_last_cmd_buf_index
;
42 static int term_last_cmd_buf_size
;
44 static int term_esc_state
;
45 static int term_esc_param
;
47 static char *term_history
[TERM_MAX_CMDS
];
48 static int term_hist_entry
= -1;
50 static int nb_completions
;
52 static char *completions
[NB_COMPLETIONS_MAX
];
54 static ReadLineFunc
*term_readline_func
;
55 static int term_is_password
;
56 static char term_prompt
[256];
57 static void *term_readline_opaque
;
59 static void term_show_prompt2(void)
61 term_printf("%s", term_prompt
);
63 term_last_cmd_buf_index
= 0;
64 term_last_cmd_buf_size
= 0;
65 term_esc_state
= IS_NORM
;
68 static void term_show_prompt(void)
71 term_cmd_buf_index
= 0;
72 term_cmd_buf_size
= 0;
75 /* update the displayed command line */
76 static void term_update(void)
80 if (term_cmd_buf_size
!= term_last_cmd_buf_size
||
81 memcmp(term_cmd_buf
, term_last_cmd_buf
, term_cmd_buf_size
) != 0) {
82 for(i
= 0; i
< term_last_cmd_buf_index
; i
++) {
83 term_printf("\033[D");
85 term_cmd_buf
[term_cmd_buf_size
] = '\0';
86 if (term_is_password
) {
87 len
= strlen(term_cmd_buf
);
88 for(i
= 0; i
< len
; i
++)
91 term_printf("%s", term_cmd_buf
);
93 term_printf("\033[K");
94 memcpy(term_last_cmd_buf
, term_cmd_buf
, term_cmd_buf_size
);
95 term_last_cmd_buf_size
= term_cmd_buf_size
;
96 term_last_cmd_buf_index
= term_cmd_buf_size
;
98 if (term_cmd_buf_index
!= term_last_cmd_buf_index
) {
99 delta
= term_cmd_buf_index
- term_last_cmd_buf_index
;
101 for(i
= 0;i
< delta
; i
++) {
102 term_printf("\033[C");
106 for(i
= 0;i
< delta
; i
++) {
107 term_printf("\033[D");
110 term_last_cmd_buf_index
= term_cmd_buf_index
;
115 static void term_insert_char(int ch
)
117 if (term_cmd_buf_index
< TERM_CMD_BUF_SIZE
) {
118 memmove(term_cmd_buf
+ term_cmd_buf_index
+ 1,
119 term_cmd_buf
+ term_cmd_buf_index
,
120 term_cmd_buf_size
- term_cmd_buf_index
);
121 term_cmd_buf
[term_cmd_buf_index
] = ch
;
123 term_cmd_buf_index
++;
127 static void term_backward_char(void)
129 if (term_cmd_buf_index
> 0) {
130 term_cmd_buf_index
--;
134 static void term_forward_char(void)
136 if (term_cmd_buf_index
< term_cmd_buf_size
) {
137 term_cmd_buf_index
++;
141 static void term_delete_char(void)
143 if (term_cmd_buf_index
< term_cmd_buf_size
) {
144 memmove(term_cmd_buf
+ term_cmd_buf_index
,
145 term_cmd_buf
+ term_cmd_buf_index
+ 1,
146 term_cmd_buf_size
- term_cmd_buf_index
- 1);
151 static void term_backspace(void)
153 if (term_cmd_buf_index
> 0) {
154 term_backward_char();
159 static void term_bol(void)
161 term_cmd_buf_index
= 0;
164 static void term_eol(void)
166 term_cmd_buf_index
= term_cmd_buf_size
;
169 static void term_up_char(void)
173 if (term_hist_entry
== 0)
175 if (term_hist_entry
== -1) {
176 /* Find latest entry */
177 for (idx
= 0; idx
< TERM_MAX_CMDS
; idx
++) {
178 if (term_history
[idx
] == NULL
)
181 term_hist_entry
= idx
;
184 if (term_hist_entry
>= 0) {
185 pstrcpy(term_cmd_buf
, sizeof(term_cmd_buf
),
186 term_history
[term_hist_entry
]);
187 term_cmd_buf_index
= term_cmd_buf_size
= strlen(term_cmd_buf
);
191 static void term_down_char(void)
193 if (term_hist_entry
== TERM_MAX_CMDS
- 1 || term_hist_entry
== -1)
195 if (term_history
[++term_hist_entry
] != NULL
) {
196 pstrcpy(term_cmd_buf
, sizeof(term_cmd_buf
),
197 term_history
[term_hist_entry
]);
199 term_hist_entry
= -1;
201 term_cmd_buf_index
= term_cmd_buf_size
= strlen(term_cmd_buf
);
204 static void term_hist_add(const char *cmdline
)
206 char *hist_entry
, *new_entry
;
209 if (cmdline
[0] == '\0')
212 if (term_hist_entry
!= -1) {
213 /* We were editing an existing history entry: replace it */
214 hist_entry
= term_history
[term_hist_entry
];
215 idx
= term_hist_entry
;
216 if (strcmp(hist_entry
, cmdline
) == 0) {
220 /* Search cmdline in history buffers */
221 for (idx
= 0; idx
< TERM_MAX_CMDS
; idx
++) {
222 hist_entry
= term_history
[idx
];
223 if (hist_entry
== NULL
)
225 if (strcmp(hist_entry
, cmdline
) == 0) {
227 new_entry
= hist_entry
;
228 /* Put this entry at the end of history */
229 memmove(&term_history
[idx
], &term_history
[idx
+ 1],
230 &term_history
[TERM_MAX_CMDS
] - &term_history
[idx
+ 1]);
231 term_history
[TERM_MAX_CMDS
- 1] = NULL
;
232 for (; idx
< TERM_MAX_CMDS
; idx
++) {
233 if (term_history
[idx
] == NULL
)
239 if (idx
== TERM_MAX_CMDS
) {
240 /* Need to get one free slot */
241 free(term_history
[0]);
242 memcpy(term_history
, &term_history
[1],
243 &term_history
[TERM_MAX_CMDS
] - &term_history
[1]);
244 term_history
[TERM_MAX_CMDS
- 1] = NULL
;
245 idx
= TERM_MAX_CMDS
- 1;
247 if (new_entry
== NULL
)
248 new_entry
= strdup(cmdline
);
249 term_history
[idx
] = new_entry
;
250 term_hist_entry
= -1;
253 /* completion support */
255 void add_completion(const char *str
)
257 if (nb_completions
< NB_COMPLETIONS_MAX
) {
258 completions
[nb_completions
++] = qemu_strdup(str
);
262 static void term_completion(void)
264 int len
, i
, j
, max_width
, nb_cols
;
269 cmdline
= qemu_malloc(term_cmd_buf_index
+ 1);
272 memcpy(cmdline
, term_cmd_buf
, term_cmd_buf_index
);
273 cmdline
[term_cmd_buf_index
] = '\0';
274 readline_find_completion(cmdline
);
277 /* no completion found */
278 if (nb_completions
<= 0)
280 if (nb_completions
== 1) {
281 len
= strlen(completions
[0]);
282 for(i
= completion_index
; i
< len
; i
++) {
283 term_insert_char(completions
[0][i
]);
285 /* extra space for next argument. XXX: make it more generic */
286 if (len
> 0 && completions
[0][len
- 1] != '/')
287 term_insert_char(' ');
291 for(i
= 0; i
< nb_completions
; i
++) {
292 len
= strlen(completions
[i
]);
299 else if (max_width
> 80)
301 nb_cols
= 80 / max_width
;
303 for(i
= 0; i
< nb_completions
; i
++) {
304 term_printf("%-*s", max_width
, completions
[i
]);
305 if (++j
== nb_cols
|| i
== (nb_completions
- 1)) {
314 /* return true if command handled */
315 void readline_handle_byte(int ch
)
317 switch(term_esc_state
) {
334 term_cmd_buf
[term_cmd_buf_size
] = '\0';
335 if (!term_is_password
)
336 term_hist_add(term_cmd_buf
);
338 /* NOTE: readline_start can be called here */
339 term_readline_func(term_readline_opaque
, term_cmd_buf
);
342 term_esc_state
= IS_ESC
;
349 term_esc_state
= IS_CSI
;
353 term_insert_char(ch
);
360 term_esc_state
= IS_CSI
;
363 term_esc_state
= IS_NORM
;
377 term_backward_char();
383 term_esc_param
= term_esc_param
* 10 + (ch
- '0');
386 switch(term_esc_param
) {
401 term_esc_state
= IS_NORM
;
408 void readline_start(const char *prompt
, int is_password
,
409 ReadLineFunc
*readline_func
, void *opaque
)
411 pstrcpy(term_prompt
, sizeof(term_prompt
), prompt
);
412 term_readline_func
= readline_func
;
413 term_readline_opaque
= opaque
;
414 term_is_password
= is_password
;
418 const char *readline_get_history(unsigned int index
)
420 if (index
>= TERM_MAX_CMDS
)
422 return term_history
[index
];