]>
git.proxmox.com Git - qemu.git/blob - readline.c
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
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
;
52 static int completion_index
;
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 Monitor
*mon
= cur_mon
;
64 monitor_printf(mon
, "%s", term_prompt
);
66 term_last_cmd_buf_index
= 0;
67 term_last_cmd_buf_size
= 0;
68 term_esc_state
= IS_NORM
;
71 /* update the displayed command line */
72 static void term_update(void)
74 Monitor
*mon
= cur_mon
;
77 if (term_cmd_buf_size
!= term_last_cmd_buf_size
||
78 memcmp(term_cmd_buf
, term_last_cmd_buf
, term_cmd_buf_size
) != 0) {
79 for(i
= 0; i
< term_last_cmd_buf_index
; i
++) {
80 monitor_printf(mon
, "\033[D");
82 term_cmd_buf
[term_cmd_buf_size
] = '\0';
83 if (term_is_password
) {
84 len
= strlen(term_cmd_buf
);
85 for(i
= 0; i
< len
; i
++)
86 monitor_printf(mon
, "*");
88 monitor_printf(mon
, "%s", term_cmd_buf
);
90 monitor_printf(mon
, "\033[K");
91 memcpy(term_last_cmd_buf
, term_cmd_buf
, term_cmd_buf_size
);
92 term_last_cmd_buf_size
= term_cmd_buf_size
;
93 term_last_cmd_buf_index
= term_cmd_buf_size
;
95 if (term_cmd_buf_index
!= term_last_cmd_buf_index
) {
96 delta
= term_cmd_buf_index
- term_last_cmd_buf_index
;
98 for(i
= 0;i
< delta
; i
++) {
99 monitor_printf(mon
, "\033[C");
103 for(i
= 0;i
< delta
; i
++) {
104 monitor_printf(mon
, "\033[D");
107 term_last_cmd_buf_index
= term_cmd_buf_index
;
112 static void term_insert_char(int ch
)
114 if (term_cmd_buf_index
< TERM_CMD_BUF_SIZE
) {
115 memmove(term_cmd_buf
+ term_cmd_buf_index
+ 1,
116 term_cmd_buf
+ term_cmd_buf_index
,
117 term_cmd_buf_size
- term_cmd_buf_index
);
118 term_cmd_buf
[term_cmd_buf_index
] = ch
;
120 term_cmd_buf_index
++;
124 static void term_backward_char(void)
126 if (term_cmd_buf_index
> 0) {
127 term_cmd_buf_index
--;
131 static void term_forward_char(void)
133 if (term_cmd_buf_index
< term_cmd_buf_size
) {
134 term_cmd_buf_index
++;
138 static void term_delete_char(void)
140 if (term_cmd_buf_index
< term_cmd_buf_size
) {
141 memmove(term_cmd_buf
+ term_cmd_buf_index
,
142 term_cmd_buf
+ term_cmd_buf_index
+ 1,
143 term_cmd_buf_size
- term_cmd_buf_index
- 1);
148 static void term_backspace(void)
150 if (term_cmd_buf_index
> 0) {
151 term_backward_char();
156 static void term_backword(void)
160 if (term_cmd_buf_index
== 0 || term_cmd_buf_index
> term_cmd_buf_size
) {
164 start
= term_cmd_buf_index
- 1;
166 /* find first word (backwards) */
168 if (!qemu_isspace(term_cmd_buf
[start
])) {
175 /* find first space (backwards) */
177 if (qemu_isspace(term_cmd_buf
[start
])) {
186 if (start
< term_cmd_buf_index
) {
187 memmove(term_cmd_buf
+ start
,
188 term_cmd_buf
+ term_cmd_buf_index
,
189 term_cmd_buf_size
- term_cmd_buf_index
);
190 term_cmd_buf_size
-= term_cmd_buf_index
- start
;
191 term_cmd_buf_index
= start
;
195 static void term_bol(void)
197 term_cmd_buf_index
= 0;
200 static void term_eol(void)
202 term_cmd_buf_index
= term_cmd_buf_size
;
205 static void term_up_char(void)
209 if (term_hist_entry
== 0)
211 if (term_hist_entry
== -1) {
212 /* Find latest entry */
213 for (idx
= 0; idx
< TERM_MAX_CMDS
; idx
++) {
214 if (term_history
[idx
] == NULL
)
217 term_hist_entry
= idx
;
220 if (term_hist_entry
>= 0) {
221 pstrcpy(term_cmd_buf
, sizeof(term_cmd_buf
),
222 term_history
[term_hist_entry
]);
223 term_cmd_buf_index
= term_cmd_buf_size
= strlen(term_cmd_buf
);
227 static void term_down_char(void)
229 if (term_hist_entry
== TERM_MAX_CMDS
- 1 || term_hist_entry
== -1)
231 if (term_history
[++term_hist_entry
] != NULL
) {
232 pstrcpy(term_cmd_buf
, sizeof(term_cmd_buf
),
233 term_history
[term_hist_entry
]);
235 term_hist_entry
= -1;
237 term_cmd_buf_index
= term_cmd_buf_size
= strlen(term_cmd_buf
);
240 static void term_hist_add(const char *cmdline
)
242 char *hist_entry
, *new_entry
;
245 if (cmdline
[0] == '\0')
248 if (term_hist_entry
!= -1) {
249 /* We were editing an existing history entry: replace it */
250 hist_entry
= term_history
[term_hist_entry
];
251 idx
= term_hist_entry
;
252 if (strcmp(hist_entry
, cmdline
) == 0) {
256 /* Search cmdline in history buffers */
257 for (idx
= 0; idx
< TERM_MAX_CMDS
; idx
++) {
258 hist_entry
= term_history
[idx
];
259 if (hist_entry
== NULL
)
261 if (strcmp(hist_entry
, cmdline
) == 0) {
263 new_entry
= hist_entry
;
264 /* Put this entry at the end of history */
265 memmove(&term_history
[idx
], &term_history
[idx
+ 1],
266 (TERM_MAX_CMDS
- idx
+ 1) * sizeof(char *));
267 term_history
[TERM_MAX_CMDS
- 1] = NULL
;
268 for (; idx
< TERM_MAX_CMDS
; idx
++) {
269 if (term_history
[idx
] == NULL
)
275 if (idx
== TERM_MAX_CMDS
) {
276 /* Need to get one free slot */
277 free(term_history
[0]);
278 memcpy(term_history
, &term_history
[1],
279 (TERM_MAX_CMDS
- 1) * sizeof(char *));
280 term_history
[TERM_MAX_CMDS
- 1] = NULL
;
281 idx
= TERM_MAX_CMDS
- 1;
283 if (new_entry
== NULL
)
284 new_entry
= strdup(cmdline
);
285 term_history
[idx
] = new_entry
;
286 term_hist_entry
= -1;
289 /* completion support */
291 void readline_add_completion(const char *str
)
293 if (nb_completions
< NB_COMPLETIONS_MAX
) {
294 completions
[nb_completions
++] = qemu_strdup(str
);
298 void readline_set_completion_index(int index
)
300 completion_index
= index
;
303 static void term_completion(void)
305 Monitor
*mon
= cur_mon
;
306 int len
, i
, j
, max_width
, nb_cols
, max_prefix
;
311 cmdline
= qemu_malloc(term_cmd_buf_index
+ 1);
312 memcpy(cmdline
, term_cmd_buf
, term_cmd_buf_index
);
313 cmdline
[term_cmd_buf_index
] = '\0';
314 readline_find_completion(cmdline
);
317 /* no completion found */
318 if (nb_completions
<= 0)
320 if (nb_completions
== 1) {
321 len
= strlen(completions
[0]);
322 for(i
= completion_index
; i
< len
; i
++) {
323 term_insert_char(completions
[0][i
]);
325 /* extra space for next argument. XXX: make it more generic */
326 if (len
> 0 && completions
[0][len
- 1] != '/')
327 term_insert_char(' ');
329 monitor_printf(mon
, "\n");
332 for(i
= 0; i
< nb_completions
; i
++) {
333 len
= strlen(completions
[i
]);
337 if (len
< max_prefix
)
339 for(j
=0; j
<max_prefix
; j
++) {
340 if (completions
[i
][j
] != completions
[0][j
])
348 for(i
= completion_index
; i
< max_prefix
; i
++) {
349 term_insert_char(completions
[0][i
]);
354 else if (max_width
> 80)
356 nb_cols
= 80 / max_width
;
358 for(i
= 0; i
< nb_completions
; i
++) {
359 monitor_printf(mon
, "%-*s", max_width
, completions
[i
]);
360 if (++j
== nb_cols
|| i
== (nb_completions
- 1)) {
361 monitor_printf(mon
, "\n");
365 readline_show_prompt();
369 /* return true if command handled */
370 void readline_handle_byte(int ch
)
372 Monitor
*mon
= cur_mon
;
374 switch(term_esc_state
) {
391 term_cmd_buf
[term_cmd_buf_size
] = '\0';
392 if (!term_is_password
)
393 term_hist_add(term_cmd_buf
);
394 monitor_printf(mon
, "\n");
395 term_cmd_buf_index
= 0;
396 term_cmd_buf_size
= 0;
397 term_last_cmd_buf_index
= 0;
398 term_last_cmd_buf_size
= 0;
399 /* NOTE: readline_start can be called here */
400 term_readline_func(mon
, term_cmd_buf
, term_readline_opaque
);
407 term_esc_state
= IS_ESC
;
414 term_esc_state
= IS_CSI
;
418 term_insert_char(ch
);
425 term_esc_state
= IS_CSI
;
428 term_esc_state
= IS_NORM
;
442 term_backward_char();
448 term_esc_param
= term_esc_param
* 10 + (ch
- '0');
451 switch(term_esc_param
) {
466 term_esc_state
= IS_NORM
;
473 void readline_start(const char *prompt
, int is_password
,
474 ReadLineFunc
*readline_func
, void *opaque
)
476 pstrcpy(term_prompt
, sizeof(term_prompt
), prompt
);
477 term_readline_func
= readline_func
;
478 term_readline_opaque
= opaque
;
479 term_is_password
= is_password
;
480 term_cmd_buf_index
= 0;
481 term_cmd_buf_size
= 0;
484 const char *readline_get_history(unsigned int index
)
486 if (index
>= TERM_MAX_CMDS
)
488 return term_history
[index
];