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