]> git.proxmox.com Git - mirror_qemu.git/blame - readline.c
monitor: Rework modal password input (Jan Kiszka)
[mirror_qemu.git] / readline.c
CommitLineData
7e2515e8
FB
1/*
2 * QEMU readline utility
5fafdf24 3 *
7e2515e8 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
7e2515e8
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
87ecb68b
PB
24#include "qemu-common.h"
25#include "console.h"
7e2515e8
FB
26
27#define TERM_CMD_BUF_SIZE 4095
28#define TERM_MAX_CMDS 64
29#define NB_COMPLETIONS_MAX 256
30
31#define IS_NORM 0
32#define IS_ESC 1
33#define IS_CSI 2
34
35#define printf do_not_use_printf
36
37static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1];
38static int term_cmd_buf_index;
39static int term_cmd_buf_size;
40
41static char term_last_cmd_buf[TERM_CMD_BUF_SIZE + 1];
42static int term_last_cmd_buf_index;
43static int term_last_cmd_buf_size;
44
45static int term_esc_state;
46static int term_esc_param;
47
48static char *term_history[TERM_MAX_CMDS];
49static int term_hist_entry = -1;
50
51static int nb_completions;
52int completion_index;
53static char *completions[NB_COMPLETIONS_MAX];
54
55static ReadLineFunc *term_readline_func;
56static int term_is_password;
57static char term_prompt[256];
58static void *term_readline_opaque;
59
9dd442b1 60void readline_show_prompt(void)
7e2515e8
FB
61{
62 term_printf("%s", term_prompt);
63 term_flush();
64 term_last_cmd_buf_index = 0;
65 term_last_cmd_buf_size = 0;
66 term_esc_state = IS_NORM;
67}
68
7e2515e8
FB
69/* update the displayed command line */
70static void term_update(void)
71{
72 int i, delta, len;
73
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");
78 }
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++)
83 term_printf("*");
84 } else {
85 term_printf("%s", term_cmd_buf);
86 }
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;
91 }
92 if (term_cmd_buf_index != term_last_cmd_buf_index) {
93 delta = term_cmd_buf_index - term_last_cmd_buf_index;
94 if (delta > 0) {
95 for(i = 0;i < delta; i++) {
96 term_printf("\033[C");
97 }
98 } else {
99 delta = -delta;
100 for(i = 0;i < delta; i++) {
101 term_printf("\033[D");
102 }
103 }
104 term_last_cmd_buf_index = term_cmd_buf_index;
105 }
106 term_flush();
107}
108
109static void term_insert_char(int ch)
110{
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;
116 term_cmd_buf_size++;
117 term_cmd_buf_index++;
118 }
119}
120
121static void term_backward_char(void)
122{
123 if (term_cmd_buf_index > 0) {
124 term_cmd_buf_index--;
125 }
126}
127
128static void term_forward_char(void)
129{
130 if (term_cmd_buf_index < term_cmd_buf_size) {
131 term_cmd_buf_index++;
132 }
133}
134
135static void term_delete_char(void)
136{
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);
141 term_cmd_buf_size--;
142 }
143}
144
145static void term_backspace(void)
146{
147 if (term_cmd_buf_index > 0) {
148 term_backward_char();
149 term_delete_char();
150 }
151}
152
33fa11d4
TS
153static void term_backword(void)
154{
155 int start;
156
157 if (term_cmd_buf_index == 0 || term_cmd_buf_index > term_cmd_buf_size) {
158 return;
159 }
160
161 start = term_cmd_buf_index - 1;
162
163 /* find first word (backwards) */
164 while (start > 0) {
cd390083 165 if (!qemu_isspace(term_cmd_buf[start])) {
33fa11d4
TS
166 break;
167 }
168
169 --start;
170 }
171
172 /* find first space (backwards) */
173 while (start > 0) {
cd390083 174 if (qemu_isspace(term_cmd_buf[start])) {
33fa11d4
TS
175 ++start;
176 break;
177 }
178
179 --start;
180 }
181
182 /* remove word */
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;
189 }
190}
191
7e2515e8
FB
192static void term_bol(void)
193{
194 term_cmd_buf_index = 0;
195}
196
197static void term_eol(void)
198{
199 term_cmd_buf_index = term_cmd_buf_size;
200}
201
202static void term_up_char(void)
203{
204 int idx;
205
206 if (term_hist_entry == 0)
207 return;
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)
212 break;
213 }
214 term_hist_entry = idx;
215 }
216 term_hist_entry--;
217 if (term_hist_entry >= 0) {
5fafdf24 218 pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
7e2515e8
FB
219 term_history[term_hist_entry]);
220 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
221 }
222}
223
224static void term_down_char(void)
225{
226 if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1)
227 return;
228 if (term_history[++term_hist_entry] != NULL) {
229 pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
230 term_history[term_hist_entry]);
231 } else {
232 term_hist_entry = -1;
233 }
234 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
235}
236
237static void term_hist_add(const char *cmdline)
238{
239 char *hist_entry, *new_entry;
240 int idx;
241
242 if (cmdline[0] == '\0')
243 return;
244 new_entry = NULL;
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) {
250 goto same_entry;
251 }
252 }
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)
257 break;
258 if (strcmp(hist_entry, cmdline) == 0) {
259 same_entry:
260 new_entry = hist_entry;
261 /* Put this entry at the end of history */
262 memmove(&term_history[idx], &term_history[idx + 1],
ccf7aa52 263 (TERM_MAX_CMDS - idx + 1) * sizeof(char *));
7e2515e8
FB
264 term_history[TERM_MAX_CMDS - 1] = NULL;
265 for (; idx < TERM_MAX_CMDS; idx++) {
266 if (term_history[idx] == NULL)
267 break;
268 }
269 break;
270 }
271 }
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],
ccf7aa52 276 (TERM_MAX_CMDS - 1) * sizeof(char *));
7e2515e8
FB
277 term_history[TERM_MAX_CMDS - 1] = NULL;
278 idx = TERM_MAX_CMDS - 1;
279 }
280 if (new_entry == NULL)
281 new_entry = strdup(cmdline);
282 term_history[idx] = new_entry;
283 term_hist_entry = -1;
284}
285
286/* completion support */
287
288void add_completion(const char *str)
289{
290 if (nb_completions < NB_COMPLETIONS_MAX) {
291 completions[nb_completions++] = qemu_strdup(str);
292 }
293}
294
295static void term_completion(void)
296{
b427c726 297 int len, i, j, max_width, nb_cols, max_prefix;
7e2515e8
FB
298 char *cmdline;
299
300 nb_completions = 0;
3b46e624 301
7e2515e8 302 cmdline = qemu_malloc(term_cmd_buf_index + 1);
7e2515e8
FB
303 memcpy(cmdline, term_cmd_buf, term_cmd_buf_index);
304 cmdline[term_cmd_buf_index] = '\0';
305 readline_find_completion(cmdline);
306 qemu_free(cmdline);
307
308 /* no completion found */
309 if (nb_completions <= 0)
310 return;
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]);
315 }
316 /* extra space for next argument. XXX: make it more generic */
317 if (len > 0 && completions[0][len - 1] != '/')
318 term_insert_char(' ');
319 } else {
320 term_printf("\n");
321 max_width = 0;
b427c726 322 max_prefix = 0;
7e2515e8
FB
323 for(i = 0; i < nb_completions; i++) {
324 len = strlen(completions[i]);
b427c726
TS
325 if (i==0) {
326 max_prefix = len;
327 } else {
328 if (len < max_prefix)
329 max_prefix = len;
330 for(j=0; j<max_prefix; j++) {
331 if (completions[i][j] != completions[0][j])
332 max_prefix = j;
333 }
334 }
7e2515e8
FB
335 if (len > max_width)
336 max_width = len;
337 }
b427c726
TS
338 if (max_prefix > 0)
339 for(i = completion_index; i < max_prefix; i++) {
340 term_insert_char(completions[0][i]);
341 }
7e2515e8
FB
342 max_width += 2;
343 if (max_width < 10)
344 max_width = 10;
345 else if (max_width > 80)
346 max_width = 80;
347 nb_cols = 80 / max_width;
348 j = 0;
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)) {
352 term_printf("\n");
353 j = 0;
354 }
355 }
9dd442b1 356 readline_show_prompt();
7e2515e8
FB
357 }
358}
359
360/* return true if command handled */
361void readline_handle_byte(int ch)
362{
363 switch(term_esc_state) {
364 case IS_NORM:
365 switch(ch) {
366 case 1:
367 term_bol();
368 break;
369 case 4:
370 term_delete_char();
371 break;
372 case 5:
373 term_eol();
374 break;
375 case 9:
376 term_completion();
377 break;
378 case 10:
379 case 13:
380 term_cmd_buf[term_cmd_buf_size] = '\0';
381 if (!term_is_password)
382 term_hist_add(term_cmd_buf);
383 term_printf("\n");
c92843b5
TS
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;
7e2515e8
FB
388 /* NOTE: readline_start can be called here */
389 term_readline_func(term_readline_opaque, term_cmd_buf);
390 break;
33fa11d4
TS
391 case 23:
392 /* ^W */
393 term_backword();
394 break;
7e2515e8
FB
395 case 27:
396 term_esc_state = IS_ESC;
397 break;
398 case 127:
399 case 8:
400 term_backspace();
401 break;
402 case 155:
403 term_esc_state = IS_CSI;
404 break;
405 default:
406 if (ch >= 32) {
407 term_insert_char(ch);
408 }
409 break;
410 }
411 break;
412 case IS_ESC:
413 if (ch == '[') {
414 term_esc_state = IS_CSI;
415 term_esc_param = 0;
416 } else {
417 term_esc_state = IS_NORM;
418 }
419 break;
420 case IS_CSI:
421 switch(ch) {
422 case 'A':
423 case 'F':
424 term_up_char();
425 break;
426 case 'B':
427 case 'E':
428 term_down_char();
429 break;
430 case 'D':
431 term_backward_char();
432 break;
433 case 'C':
434 term_forward_char();
435 break;
436 case '0' ... '9':
437 term_esc_param = term_esc_param * 10 + (ch - '0');
438 goto the_end;
439 case '~':
440 switch(term_esc_param) {
441 case 1:
442 term_bol();
443 break;
444 case 3:
445 term_delete_char();
446 break;
447 case 4:
448 term_eol();
449 break;
450 }
451 break;
452 default:
453 break;
454 }
455 term_esc_state = IS_NORM;
456 the_end:
457 break;
458 }
459 term_update();
460}
461
462void readline_start(const char *prompt, int is_password,
463 ReadLineFunc *readline_func, void *opaque)
464{
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;
9dd442b1
AL
469 term_cmd_buf_index = 0;
470 term_cmd_buf_size = 0;
7e2515e8
FB
471}
472
473const char *readline_get_history(unsigned int index)
474{
475 if (index >= TERM_MAX_CMDS)
476 return NULL;
477 return term_history[index];
478}