]> git.proxmox.com Git - qemu.git/blame - readline.c
monitor: Decouple terminals (Jan Kiszka)
[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 */
376253ec
AL
24#include "readline.h"
25#include "monitor.h"
7e2515e8 26
7e2515e8
FB
27#define IS_NORM 0
28#define IS_ESC 1
29#define IS_CSI 2
30
31#define printf do_not_use_printf
32
4c36ba32 33void readline_show_prompt(ReadLineState *rs)
7e2515e8 34{
4c36ba32
AL
35 monitor_printf(rs->mon, "%s", rs->prompt);
36 monitor_flush(rs->mon);
37 rs->last_cmd_buf_index = 0;
38 rs->last_cmd_buf_size = 0;
39 rs->esc_state = IS_NORM;
7e2515e8
FB
40}
41
7e2515e8 42/* update the displayed command line */
4c36ba32 43static void readline_update(ReadLineState *rs)
7e2515e8
FB
44{
45 int i, delta, len;
46
4c36ba32
AL
47 if (rs->cmd_buf_size != rs->last_cmd_buf_size ||
48 memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) {
49 for(i = 0; i < rs->last_cmd_buf_index; i++) {
50 monitor_printf(rs->mon, "\033[D");
7e2515e8 51 }
4c36ba32
AL
52 rs->cmd_buf[rs->cmd_buf_size] = '\0';
53 if (rs->read_password) {
54 len = strlen(rs->cmd_buf);
7e2515e8 55 for(i = 0; i < len; i++)
4c36ba32 56 monitor_printf(rs->mon, "*");
7e2515e8 57 } else {
4c36ba32 58 monitor_printf(rs->mon, "%s", rs->cmd_buf);
7e2515e8 59 }
4c36ba32
AL
60 monitor_printf(rs->mon, "\033[K");
61 memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size);
62 rs->last_cmd_buf_size = rs->cmd_buf_size;
63 rs->last_cmd_buf_index = rs->cmd_buf_size;
7e2515e8 64 }
4c36ba32
AL
65 if (rs->cmd_buf_index != rs->last_cmd_buf_index) {
66 delta = rs->cmd_buf_index - rs->last_cmd_buf_index;
7e2515e8
FB
67 if (delta > 0) {
68 for(i = 0;i < delta; i++) {
4c36ba32 69 monitor_printf(rs->mon, "\033[C");
7e2515e8
FB
70 }
71 } else {
72 delta = -delta;
73 for(i = 0;i < delta; i++) {
4c36ba32 74 monitor_printf(rs->mon, "\033[D");
7e2515e8
FB
75 }
76 }
4c36ba32 77 rs->last_cmd_buf_index = rs->cmd_buf_index;
7e2515e8 78 }
4c36ba32 79 monitor_flush(rs->mon);
7e2515e8
FB
80}
81
4c36ba32 82static void readline_insert_char(ReadLineState *rs, int ch)
7e2515e8 83{
4c36ba32
AL
84 if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
85 memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
86 rs->cmd_buf + rs->cmd_buf_index,
87 rs->cmd_buf_size - rs->cmd_buf_index);
88 rs->cmd_buf[rs->cmd_buf_index] = ch;
89 rs->cmd_buf_size++;
90 rs->cmd_buf_index++;
7e2515e8
FB
91 }
92}
93
4c36ba32 94static void readline_backward_char(ReadLineState *rs)
7e2515e8 95{
4c36ba32
AL
96 if (rs->cmd_buf_index > 0) {
97 rs->cmd_buf_index--;
7e2515e8
FB
98 }
99}
100
4c36ba32 101static void readline_forward_char(ReadLineState *rs)
7e2515e8 102{
4c36ba32
AL
103 if (rs->cmd_buf_index < rs->cmd_buf_size) {
104 rs->cmd_buf_index++;
7e2515e8
FB
105 }
106}
107
4c36ba32 108static void readline_delete_char(ReadLineState *rs)
7e2515e8 109{
4c36ba32
AL
110 if (rs->cmd_buf_index < rs->cmd_buf_size) {
111 memmove(rs->cmd_buf + rs->cmd_buf_index,
112 rs->cmd_buf + rs->cmd_buf_index + 1,
113 rs->cmd_buf_size - rs->cmd_buf_index - 1);
114 rs->cmd_buf_size--;
7e2515e8
FB
115 }
116}
117
4c36ba32 118static void readline_backspace(ReadLineState *rs)
7e2515e8 119{
4c36ba32
AL
120 if (rs->cmd_buf_index > 0) {
121 readline_backward_char(rs);
122 readline_delete_char(rs);
7e2515e8
FB
123 }
124}
125
4c36ba32 126static void readline_backword(ReadLineState *rs)
33fa11d4
TS
127{
128 int start;
129
4c36ba32 130 if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
33fa11d4
TS
131 return;
132 }
133
4c36ba32 134 start = rs->cmd_buf_index - 1;
33fa11d4
TS
135
136 /* find first word (backwards) */
137 while (start > 0) {
4c36ba32 138 if (!qemu_isspace(rs->cmd_buf[start])) {
33fa11d4
TS
139 break;
140 }
141
142 --start;
143 }
144
145 /* find first space (backwards) */
146 while (start > 0) {
4c36ba32 147 if (qemu_isspace(rs->cmd_buf[start])) {
33fa11d4
TS
148 ++start;
149 break;
150 }
151
152 --start;
153 }
154
155 /* remove word */
4c36ba32
AL
156 if (start < rs->cmd_buf_index) {
157 memmove(rs->cmd_buf + start,
158 rs->cmd_buf + rs->cmd_buf_index,
159 rs->cmd_buf_size - rs->cmd_buf_index);
160 rs->cmd_buf_size -= rs->cmd_buf_index - start;
161 rs->cmd_buf_index = start;
33fa11d4
TS
162 }
163}
164
4c36ba32 165static void readline_bol(ReadLineState *rs)
7e2515e8 166{
4c36ba32 167 rs->cmd_buf_index = 0;
7e2515e8
FB
168}
169
4c36ba32 170static void readline_eol(ReadLineState *rs)
7e2515e8 171{
4c36ba32 172 rs->cmd_buf_index = rs->cmd_buf_size;
7e2515e8
FB
173}
174
4c36ba32 175static void readline_up_char(ReadLineState *rs)
7e2515e8
FB
176{
177 int idx;
178
4c36ba32 179 if (rs->hist_entry == 0)
7e2515e8 180 return;
4c36ba32 181 if (rs->hist_entry == -1) {
7e2515e8 182 /* Find latest entry */
4c36ba32
AL
183 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
184 if (rs->history[idx] == NULL)
7e2515e8
FB
185 break;
186 }
4c36ba32 187 rs->hist_entry = idx;
7e2515e8 188 }
4c36ba32
AL
189 rs->hist_entry--;
190 if (rs->hist_entry >= 0) {
191 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
192 rs->history[rs->hist_entry]);
193 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
7e2515e8
FB
194 }
195}
196
4c36ba32 197static void readline_down_char(ReadLineState *rs)
7e2515e8 198{
4c36ba32 199 if (rs->hist_entry == READLINE_MAX_CMDS - 1 || rs->hist_entry == -1)
7e2515e8 200 return;
4c36ba32
AL
201 if (rs->history[++rs->hist_entry] != NULL) {
202 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
203 rs->history[rs->hist_entry]);
7e2515e8 204 } else {
4c36ba32 205 rs->hist_entry = -1;
7e2515e8 206 }
4c36ba32 207 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
7e2515e8
FB
208}
209
4c36ba32 210static void readline_hist_add(ReadLineState *rs, const char *cmdline)
7e2515e8
FB
211{
212 char *hist_entry, *new_entry;
213 int idx;
214
215 if (cmdline[0] == '\0')
216 return;
217 new_entry = NULL;
4c36ba32 218 if (rs->hist_entry != -1) {
7e2515e8 219 /* We were editing an existing history entry: replace it */
4c36ba32
AL
220 hist_entry = rs->history[rs->hist_entry];
221 idx = rs->hist_entry;
7e2515e8
FB
222 if (strcmp(hist_entry, cmdline) == 0) {
223 goto same_entry;
224 }
225 }
226 /* Search cmdline in history buffers */
4c36ba32
AL
227 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
228 hist_entry = rs->history[idx];
7e2515e8
FB
229 if (hist_entry == NULL)
230 break;
231 if (strcmp(hist_entry, cmdline) == 0) {
232 same_entry:
233 new_entry = hist_entry;
234 /* Put this entry at the end of history */
4c36ba32
AL
235 memmove(&rs->history[idx], &rs->history[idx + 1],
236 (READLINE_MAX_CMDS - idx + 1) * sizeof(char *));
237 rs->history[READLINE_MAX_CMDS - 1] = NULL;
238 for (; idx < READLINE_MAX_CMDS; idx++) {
239 if (rs->history[idx] == NULL)
7e2515e8
FB
240 break;
241 }
242 break;
243 }
244 }
4c36ba32 245 if (idx == READLINE_MAX_CMDS) {
7e2515e8 246 /* Need to get one free slot */
4c36ba32
AL
247 free(rs->history[0]);
248 memcpy(rs->history, &rs->history[1],
249 (READLINE_MAX_CMDS - 1) * sizeof(char *));
250 rs->history[READLINE_MAX_CMDS - 1] = NULL;
251 idx = READLINE_MAX_CMDS - 1;
7e2515e8
FB
252 }
253 if (new_entry == NULL)
254 new_entry = strdup(cmdline);
4c36ba32
AL
255 rs->history[idx] = new_entry;
256 rs->hist_entry = -1;
7e2515e8
FB
257}
258
259/* completion support */
260
4c36ba32 261void readline_add_completion(ReadLineState *rs, const char *str)
7e2515e8 262{
4c36ba32
AL
263 if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
264 rs->completions[rs->nb_completions++] = qemu_strdup(str);
7e2515e8
FB
265 }
266}
267
4c36ba32 268void readline_set_completion_index(ReadLineState *rs, int index)
376253ec 269{
4c36ba32 270 rs->completion_index = index;
376253ec
AL
271}
272
4c36ba32 273static void readline_completion(ReadLineState *rs)
7e2515e8 274{
376253ec 275 Monitor *mon = cur_mon;
b427c726 276 int len, i, j, max_width, nb_cols, max_prefix;
7e2515e8
FB
277 char *cmdline;
278
4c36ba32 279 rs->nb_completions = 0;
3b46e624 280
4c36ba32
AL
281 cmdline = qemu_malloc(rs->cmd_buf_index + 1);
282 memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index);
283 cmdline[rs->cmd_buf_index] = '\0';
284 rs->completion_finder(cmdline);
7e2515e8
FB
285 qemu_free(cmdline);
286
287 /* no completion found */
4c36ba32 288 if (rs->nb_completions <= 0)
7e2515e8 289 return;
4c36ba32
AL
290 if (rs->nb_completions == 1) {
291 len = strlen(rs->completions[0]);
292 for(i = rs->completion_index; i < len; i++) {
293 readline_insert_char(rs, rs->completions[0][i]);
7e2515e8
FB
294 }
295 /* extra space for next argument. XXX: make it more generic */
4c36ba32
AL
296 if (len > 0 && rs->completions[0][len - 1] != '/')
297 readline_insert_char(rs, ' ');
7e2515e8 298 } else {
376253ec 299 monitor_printf(mon, "\n");
7e2515e8 300 max_width = 0;
b427c726 301 max_prefix = 0;
4c36ba32
AL
302 for(i = 0; i < rs->nb_completions; i++) {
303 len = strlen(rs->completions[i]);
b427c726
TS
304 if (i==0) {
305 max_prefix = len;
306 } else {
307 if (len < max_prefix)
308 max_prefix = len;
309 for(j=0; j<max_prefix; j++) {
4c36ba32 310 if (rs->completions[i][j] != rs->completions[0][j])
b427c726
TS
311 max_prefix = j;
312 }
313 }
7e2515e8
FB
314 if (len > max_width)
315 max_width = len;
316 }
b427c726 317 if (max_prefix > 0)
4c36ba32
AL
318 for(i = rs->completion_index; i < max_prefix; i++) {
319 readline_insert_char(rs, rs->completions[0][i]);
b427c726 320 }
7e2515e8
FB
321 max_width += 2;
322 if (max_width < 10)
323 max_width = 10;
324 else if (max_width > 80)
325 max_width = 80;
326 nb_cols = 80 / max_width;
327 j = 0;
4c36ba32
AL
328 for(i = 0; i < rs->nb_completions; i++) {
329 monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]);
330 if (++j == nb_cols || i == (rs->nb_completions - 1)) {
331 monitor_printf(rs->mon, "\n");
7e2515e8
FB
332 j = 0;
333 }
334 }
4c36ba32 335 readline_show_prompt(rs);
7e2515e8
FB
336 }
337}
338
339/* return true if command handled */
4c36ba32 340void readline_handle_byte(ReadLineState *rs, int ch)
7e2515e8 341{
4c36ba32 342 switch(rs->esc_state) {
7e2515e8
FB
343 case IS_NORM:
344 switch(ch) {
345 case 1:
4c36ba32 346 readline_bol(rs);
7e2515e8
FB
347 break;
348 case 4:
4c36ba32 349 readline_delete_char(rs);
7e2515e8
FB
350 break;
351 case 5:
4c36ba32 352 readline_eol(rs);
7e2515e8
FB
353 break;
354 case 9:
4c36ba32 355 readline_completion(rs);
7e2515e8
FB
356 break;
357 case 10:
358 case 13:
4c36ba32
AL
359 rs->cmd_buf[rs->cmd_buf_size] = '\0';
360 if (!rs->read_password)
361 readline_hist_add(rs, rs->cmd_buf);
362 monitor_printf(rs->mon, "\n");
363 rs->cmd_buf_index = 0;
364 rs->cmd_buf_size = 0;
365 rs->last_cmd_buf_index = 0;
366 rs->last_cmd_buf_size = 0;
367 rs->readline_func(rs->mon, rs->cmd_buf, rs->readline_opaque);
7e2515e8 368 break;
33fa11d4
TS
369 case 23:
370 /* ^W */
4c36ba32 371 readline_backword(rs);
33fa11d4 372 break;
7e2515e8 373 case 27:
4c36ba32 374 rs->esc_state = IS_ESC;
7e2515e8
FB
375 break;
376 case 127:
377 case 8:
4c36ba32 378 readline_backspace(rs);
7e2515e8
FB
379 break;
380 case 155:
4c36ba32 381 rs->esc_state = IS_CSI;
7e2515e8
FB
382 break;
383 default:
384 if (ch >= 32) {
4c36ba32 385 readline_insert_char(rs, ch);
7e2515e8
FB
386 }
387 break;
388 }
389 break;
390 case IS_ESC:
391 if (ch == '[') {
4c36ba32
AL
392 rs->esc_state = IS_CSI;
393 rs->esc_param = 0;
7e2515e8 394 } else {
4c36ba32 395 rs->esc_state = IS_NORM;
7e2515e8
FB
396 }
397 break;
398 case IS_CSI:
399 switch(ch) {
400 case 'A':
401 case 'F':
4c36ba32 402 readline_up_char(rs);
7e2515e8
FB
403 break;
404 case 'B':
405 case 'E':
4c36ba32 406 readline_down_char(rs);
7e2515e8
FB
407 break;
408 case 'D':
4c36ba32 409 readline_backward_char(rs);
7e2515e8
FB
410 break;
411 case 'C':
4c36ba32 412 readline_forward_char(rs);
7e2515e8
FB
413 break;
414 case '0' ... '9':
4c36ba32 415 rs->esc_param = rs->esc_param * 10 + (ch - '0');
7e2515e8
FB
416 goto the_end;
417 case '~':
4c36ba32 418 switch(rs->esc_param) {
7e2515e8 419 case 1:
4c36ba32 420 readline_bol(rs);
7e2515e8
FB
421 break;
422 case 3:
4c36ba32 423 readline_delete_char(rs);
7e2515e8
FB
424 break;
425 case 4:
4c36ba32 426 readline_eol(rs);
7e2515e8
FB
427 break;
428 }
429 break;
430 default:
431 break;
432 }
4c36ba32 433 rs->esc_state = IS_NORM;
7e2515e8
FB
434 the_end:
435 break;
436 }
4c36ba32 437 readline_update(rs);
7e2515e8
FB
438}
439
4c36ba32 440void readline_start(ReadLineState *rs, const char *prompt, int read_password,
7e2515e8
FB
441 ReadLineFunc *readline_func, void *opaque)
442{
4c36ba32
AL
443 pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
444 rs->readline_func = readline_func;
445 rs->readline_opaque = opaque;
446 rs->read_password = read_password;
447 rs->cmd_buf_index = 0;
448 rs->cmd_buf_size = 0;
7e2515e8
FB
449}
450
4c36ba32 451const char *readline_get_history(ReadLineState *rs, unsigned int index)
7e2515e8 452{
4c36ba32 453 if (index >= READLINE_MAX_CMDS)
7e2515e8 454 return NULL;
4c36ba32
AL
455 return rs->history[index];
456}
457
458ReadLineState *readline_init(Monitor *mon,
459 ReadLineCompletionFunc *completion_finder)
460{
461 ReadLineState *rs = qemu_mallocz(sizeof(*rs));
462
463 if (!rs)
464 return NULL;
465
466 rs->hist_entry = -1;
467 rs->mon = mon;
468 rs->completion_finder = completion_finder;
469
470 return rs;
7e2515e8 471}