Merge remote-tracking branch 'remotes/gonglei/tags/bootdevice-next-20141222' into...
[qemu/qmp-unstable.git] / util / readline.c
blobcc1302ac0adcd195c9ad70f1043d798503dde752
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu-common.h"
26 #include "qemu/readline.h"
28 #define IS_NORM 0
29 #define IS_ESC 1
30 #define IS_CSI 2
31 #define IS_SS3 3
33 void readline_show_prompt(ReadLineState *rs)
35 rs->printf_func(rs->opaque, "%s", rs->prompt);
36 rs->flush_func(rs->opaque);
37 rs->last_cmd_buf_index = 0;
38 rs->last_cmd_buf_size = 0;
39 rs->esc_state = IS_NORM;
42 /* update the displayed command line */
43 static void readline_update(ReadLineState *rs)
45 int i, delta, len;
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 rs->printf_func(rs->opaque, "\033[D");
52 rs->cmd_buf[rs->cmd_buf_size] = '\0';
53 if (rs->read_password) {
54 len = strlen(rs->cmd_buf);
55 for(i = 0; i < len; i++)
56 rs->printf_func(rs->opaque, "*");
57 } else {
58 rs->printf_func(rs->opaque, "%s", rs->cmd_buf);
60 rs->printf_func(rs->opaque, "\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;
65 if (rs->cmd_buf_index != rs->last_cmd_buf_index) {
66 delta = rs->cmd_buf_index - rs->last_cmd_buf_index;
67 if (delta > 0) {
68 for(i = 0;i < delta; i++) {
69 rs->printf_func(rs->opaque, "\033[C");
71 } else {
72 delta = -delta;
73 for(i = 0;i < delta; i++) {
74 rs->printf_func(rs->opaque, "\033[D");
77 rs->last_cmd_buf_index = rs->cmd_buf_index;
79 rs->flush_func(rs->opaque);
82 static void readline_insert_char(ReadLineState *rs, int ch)
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++;
94 static void readline_backward_char(ReadLineState *rs)
96 if (rs->cmd_buf_index > 0) {
97 rs->cmd_buf_index--;
101 static void readline_forward_char(ReadLineState *rs)
103 if (rs->cmd_buf_index < rs->cmd_buf_size) {
104 rs->cmd_buf_index++;
108 static void readline_delete_char(ReadLineState *rs)
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--;
118 static void readline_backspace(ReadLineState *rs)
120 if (rs->cmd_buf_index > 0) {
121 readline_backward_char(rs);
122 readline_delete_char(rs);
126 static void readline_backword(ReadLineState *rs)
128 int start;
130 if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
131 return;
134 start = rs->cmd_buf_index - 1;
136 /* find first word (backwards) */
137 while (start > 0) {
138 if (!qemu_isspace(rs->cmd_buf[start])) {
139 break;
142 --start;
145 /* find first space (backwards) */
146 while (start > 0) {
147 if (qemu_isspace(rs->cmd_buf[start])) {
148 ++start;
149 break;
152 --start;
155 /* remove word */
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;
165 static void readline_bol(ReadLineState *rs)
167 rs->cmd_buf_index = 0;
170 static void readline_eol(ReadLineState *rs)
172 rs->cmd_buf_index = rs->cmd_buf_size;
175 static void readline_up_char(ReadLineState *rs)
177 int idx;
179 if (rs->hist_entry == 0)
180 return;
181 if (rs->hist_entry == -1) {
182 /* Find latest entry */
183 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
184 if (rs->history[idx] == NULL)
185 break;
187 rs->hist_entry = idx;
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);
197 static void readline_down_char(ReadLineState *rs)
199 if (rs->hist_entry == -1)
200 return;
201 if (rs->hist_entry < READLINE_MAX_CMDS - 1 &&
202 rs->history[++rs->hist_entry] != NULL) {
203 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
204 rs->history[rs->hist_entry]);
205 } else {
206 rs->cmd_buf[0] = 0;
207 rs->hist_entry = -1;
209 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
212 static void readline_hist_add(ReadLineState *rs, const char *cmdline)
214 char *hist_entry, *new_entry;
215 int idx;
217 if (cmdline[0] == '\0')
218 return;
219 new_entry = NULL;
220 if (rs->hist_entry != -1) {
221 /* We were editing an existing history entry: replace it */
222 hist_entry = rs->history[rs->hist_entry];
223 idx = rs->hist_entry;
224 if (strcmp(hist_entry, cmdline) == 0) {
225 goto same_entry;
228 /* Search cmdline in history buffers */
229 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
230 hist_entry = rs->history[idx];
231 if (hist_entry == NULL)
232 break;
233 if (strcmp(hist_entry, cmdline) == 0) {
234 same_entry:
235 new_entry = hist_entry;
236 /* Put this entry at the end of history */
237 memmove(&rs->history[idx], &rs->history[idx + 1],
238 (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
239 rs->history[READLINE_MAX_CMDS - 1] = NULL;
240 for (; idx < READLINE_MAX_CMDS; idx++) {
241 if (rs->history[idx] == NULL)
242 break;
244 break;
247 if (idx == READLINE_MAX_CMDS) {
248 /* Need to get one free slot */
249 g_free(rs->history[0]);
250 memmove(rs->history, &rs->history[1],
251 (READLINE_MAX_CMDS - 1) * sizeof(char *));
252 rs->history[READLINE_MAX_CMDS - 1] = NULL;
253 idx = READLINE_MAX_CMDS - 1;
255 if (new_entry == NULL)
256 new_entry = g_strdup(cmdline);
257 rs->history[idx] = new_entry;
258 rs->hist_entry = -1;
261 /* completion support */
263 void readline_add_completion(ReadLineState *rs, const char *str)
265 if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
266 int i;
267 for (i = 0; i < rs->nb_completions; i++) {
268 if (!strcmp(rs->completions[i], str)) {
269 return;
272 rs->completions[rs->nb_completions++] = g_strdup(str);
276 void readline_set_completion_index(ReadLineState *rs, int index)
278 rs->completion_index = index;
281 static int completion_comp(const void *a, const void *b)
283 return strcmp(*(const char **) a, *(const char **) b);
286 static void readline_completion(ReadLineState *rs)
288 int len, i, j, max_width, nb_cols, max_prefix;
289 char *cmdline;
291 rs->nb_completions = 0;
293 cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index);
294 rs->completion_finder(rs->opaque, cmdline);
295 g_free(cmdline);
297 /* no completion found */
298 if (rs->nb_completions <= 0)
299 return;
300 if (rs->nb_completions == 1) {
301 len = strlen(rs->completions[0]);
302 for(i = rs->completion_index; i < len; i++) {
303 readline_insert_char(rs, rs->completions[0][i]);
305 /* extra space for next argument. XXX: make it more generic */
306 if (len > 0 && rs->completions[0][len - 1] != '/')
307 readline_insert_char(rs, ' ');
308 } else {
309 qsort(rs->completions, rs->nb_completions, sizeof(char *),
310 completion_comp);
311 rs->printf_func(rs->opaque, "\n");
312 max_width = 0;
313 max_prefix = 0;
314 for(i = 0; i < rs->nb_completions; i++) {
315 len = strlen(rs->completions[i]);
316 if (i==0) {
317 max_prefix = len;
318 } else {
319 if (len < max_prefix)
320 max_prefix = len;
321 for(j=0; j<max_prefix; j++) {
322 if (rs->completions[i][j] != rs->completions[0][j])
323 max_prefix = j;
326 if (len > max_width)
327 max_width = len;
329 if (max_prefix > 0)
330 for(i = rs->completion_index; i < max_prefix; i++) {
331 readline_insert_char(rs, rs->completions[0][i]);
333 max_width += 2;
334 if (max_width < 10)
335 max_width = 10;
336 else if (max_width > 80)
337 max_width = 80;
338 nb_cols = 80 / max_width;
339 j = 0;
340 for(i = 0; i < rs->nb_completions; i++) {
341 rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]);
342 if (++j == nb_cols || i == (rs->nb_completions - 1)) {
343 rs->printf_func(rs->opaque, "\n");
344 j = 0;
347 readline_show_prompt(rs);
349 for (i = 0; i < rs->nb_completions; i++) {
350 g_free(rs->completions[i]);
354 static void readline_clear_screen(ReadLineState *rs)
356 rs->printf_func(rs->opaque, "\033[2J\033[1;1H");
357 readline_show_prompt(rs);
360 /* return true if command handled */
361 void readline_handle_byte(ReadLineState *rs, int ch)
363 switch(rs->esc_state) {
364 case IS_NORM:
365 switch(ch) {
366 case 1:
367 readline_bol(rs);
368 break;
369 case 4:
370 readline_delete_char(rs);
371 break;
372 case 5:
373 readline_eol(rs);
374 break;
375 case 9:
376 readline_completion(rs);
377 break;
378 case 12:
379 readline_clear_screen(rs);
380 break;
381 case 10:
382 case 13:
383 rs->cmd_buf[rs->cmd_buf_size] = '\0';
384 if (!rs->read_password)
385 readline_hist_add(rs, rs->cmd_buf);
386 rs->printf_func(rs->opaque, "\n");
387 rs->cmd_buf_index = 0;
388 rs->cmd_buf_size = 0;
389 rs->last_cmd_buf_index = 0;
390 rs->last_cmd_buf_size = 0;
391 rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque);
392 break;
393 case 23:
394 /* ^W */
395 readline_backword(rs);
396 break;
397 case 27:
398 rs->esc_state = IS_ESC;
399 break;
400 case 127:
401 case 8:
402 readline_backspace(rs);
403 break;
404 case 155:
405 rs->esc_state = IS_CSI;
406 break;
407 default:
408 if (ch >= 32) {
409 readline_insert_char(rs, ch);
411 break;
413 break;
414 case IS_ESC:
415 if (ch == '[') {
416 rs->esc_state = IS_CSI;
417 rs->esc_param = 0;
418 } else if (ch == 'O') {
419 rs->esc_state = IS_SS3;
420 rs->esc_param = 0;
421 } else {
422 rs->esc_state = IS_NORM;
424 break;
425 case IS_CSI:
426 switch(ch) {
427 case 'A':
428 case 'F':
429 readline_up_char(rs);
430 break;
431 case 'B':
432 case 'E':
433 readline_down_char(rs);
434 break;
435 case 'D':
436 readline_backward_char(rs);
437 break;
438 case 'C':
439 readline_forward_char(rs);
440 break;
441 case '0' ... '9':
442 rs->esc_param = rs->esc_param * 10 + (ch - '0');
443 goto the_end;
444 case '~':
445 switch(rs->esc_param) {
446 case 1:
447 readline_bol(rs);
448 break;
449 case 3:
450 readline_delete_char(rs);
451 break;
452 case 4:
453 readline_eol(rs);
454 break;
456 break;
457 default:
458 break;
460 rs->esc_state = IS_NORM;
461 the_end:
462 break;
463 case IS_SS3:
464 switch(ch) {
465 case 'F':
466 readline_eol(rs);
467 break;
468 case 'H':
469 readline_bol(rs);
470 break;
472 rs->esc_state = IS_NORM;
473 break;
475 readline_update(rs);
478 void readline_start(ReadLineState *rs, const char *prompt, int read_password,
479 ReadLineFunc *readline_func, void *opaque)
481 pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
482 rs->readline_func = readline_func;
483 rs->readline_opaque = opaque;
484 rs->read_password = read_password;
485 readline_restart(rs);
488 void readline_restart(ReadLineState *rs)
490 rs->cmd_buf_index = 0;
491 rs->cmd_buf_size = 0;
494 const char *readline_get_history(ReadLineState *rs, unsigned int index)
496 if (index >= READLINE_MAX_CMDS)
497 return NULL;
498 return rs->history[index];
501 ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
502 ReadLineFlushFunc *flush_func,
503 void *opaque,
504 ReadLineCompletionFunc *completion_finder)
506 ReadLineState *rs = g_malloc0(sizeof(*rs));
508 rs->hist_entry = -1;
509 rs->opaque = opaque;
510 rs->printf_func = printf_func;
511 rs->flush_func = flush_func;
512 rs->completion_finder = completion_finder;
514 return rs;