xref: /openbmc/qemu/util/readline.c (revision b2c623a3)
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-common.h"
26 #include "qemu/readline.h"
27 
28 #define IS_NORM 0
29 #define IS_ESC  1
30 #define IS_CSI  2
31 #define IS_SS3  3
32 
33 void readline_show_prompt(ReadLineState *rs)
34 {
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;
40 }
41 
42 /* update the displayed command line */
43 static void readline_update(ReadLineState *rs)
44 {
45     int i, delta, len;
46 
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");
51         }
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);
59         }
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;
64     }
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");
70             }
71         } else {
72             delta = -delta;
73             for(i = 0;i < delta; i++) {
74                 rs->printf_func(rs->opaque, "\033[D");
75             }
76         }
77         rs->last_cmd_buf_index = rs->cmd_buf_index;
78     }
79     rs->flush_func(rs->opaque);
80 }
81 
82 static void readline_insert_char(ReadLineState *rs, int ch)
83 {
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++;
91     }
92 }
93 
94 static void readline_backward_char(ReadLineState *rs)
95 {
96     if (rs->cmd_buf_index > 0) {
97         rs->cmd_buf_index--;
98     }
99 }
100 
101 static void readline_forward_char(ReadLineState *rs)
102 {
103     if (rs->cmd_buf_index < rs->cmd_buf_size) {
104         rs->cmd_buf_index++;
105     }
106 }
107 
108 static void readline_delete_char(ReadLineState *rs)
109 {
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--;
115     }
116 }
117 
118 static void readline_backspace(ReadLineState *rs)
119 {
120     if (rs->cmd_buf_index > 0) {
121         readline_backward_char(rs);
122         readline_delete_char(rs);
123     }
124 }
125 
126 static void readline_backword(ReadLineState *rs)
127 {
128     int start;
129 
130     if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
131         return;
132     }
133 
134     start = rs->cmd_buf_index - 1;
135 
136     /* find first word (backwards) */
137     while (start > 0) {
138         if (!qemu_isspace(rs->cmd_buf[start])) {
139             break;
140         }
141 
142         --start;
143     }
144 
145     /* find first space (backwards) */
146     while (start > 0) {
147         if (qemu_isspace(rs->cmd_buf[start])) {
148             ++start;
149             break;
150         }
151 
152         --start;
153     }
154 
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;
162     }
163 }
164 
165 static void readline_bol(ReadLineState *rs)
166 {
167     rs->cmd_buf_index = 0;
168 }
169 
170 static void readline_eol(ReadLineState *rs)
171 {
172     rs->cmd_buf_index = rs->cmd_buf_size;
173 }
174 
175 static void readline_up_char(ReadLineState *rs)
176 {
177     int idx;
178 
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;
186 	}
187 	rs->hist_entry = idx;
188     }
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);
194     }
195 }
196 
197 static void readline_down_char(ReadLineState *rs)
198 {
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;
208     }
209     rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
210 }
211 
212 static void readline_hist_add(ReadLineState *rs, const char *cmdline)
213 {
214     char *hist_entry, *new_entry;
215     int idx;
216 
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;
226 	}
227     }
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;
243 	    }
244 	    break;
245 	}
246     }
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;
254     }
255     if (new_entry == NULL)
256         new_entry = g_strdup(cmdline);
257     rs->history[idx] = new_entry;
258     rs->hist_entry = -1;
259 }
260 
261 /* completion support */
262 
263 void readline_add_completion(ReadLineState *rs, const char *str)
264 {
265     if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
266         rs->completions[rs->nb_completions++] = g_strdup(str);
267     }
268 }
269 
270 void readline_set_completion_index(ReadLineState *rs, int index)
271 {
272     rs->completion_index = index;
273 }
274 
275 static void readline_completion(ReadLineState *rs)
276 {
277     int len, i, j, max_width, nb_cols, max_prefix;
278     char *cmdline;
279 
280     rs->nb_completions = 0;
281 
282     cmdline = g_malloc(rs->cmd_buf_index + 1);
283     memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index);
284     cmdline[rs->cmd_buf_index] = '\0';
285     rs->completion_finder(rs->opaque, cmdline);
286     g_free(cmdline);
287 
288     /* no completion found */
289     if (rs->nb_completions <= 0)
290         return;
291     if (rs->nb_completions == 1) {
292         len = strlen(rs->completions[0]);
293         for(i = rs->completion_index; i < len; i++) {
294             readline_insert_char(rs, rs->completions[0][i]);
295         }
296         /* extra space for next argument. XXX: make it more generic */
297         if (len > 0 && rs->completions[0][len - 1] != '/')
298             readline_insert_char(rs, ' ');
299     } else {
300         rs->printf_func(rs->opaque, "\n");
301         max_width = 0;
302         max_prefix = 0;
303         for(i = 0; i < rs->nb_completions; i++) {
304             len = strlen(rs->completions[i]);
305             if (i==0) {
306                 max_prefix = len;
307             } else {
308                 if (len < max_prefix)
309                     max_prefix = len;
310                 for(j=0; j<max_prefix; j++) {
311                     if (rs->completions[i][j] != rs->completions[0][j])
312                         max_prefix = j;
313                 }
314             }
315             if (len > max_width)
316                 max_width = len;
317         }
318         if (max_prefix > 0)
319             for(i = rs->completion_index; i < max_prefix; i++) {
320                 readline_insert_char(rs, rs->completions[0][i]);
321             }
322         max_width += 2;
323         if (max_width < 10)
324             max_width = 10;
325         else if (max_width > 80)
326             max_width = 80;
327         nb_cols = 80 / max_width;
328         j = 0;
329         for(i = 0; i < rs->nb_completions; i++) {
330             rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]);
331             if (++j == nb_cols || i == (rs->nb_completions - 1)) {
332                 rs->printf_func(rs->opaque, "\n");
333                 j = 0;
334             }
335         }
336         readline_show_prompt(rs);
337     }
338     for (i = 0; i < rs->nb_completions; i++) {
339         g_free(rs->completions[i]);
340     }
341 }
342 
343 /* return true if command handled */
344 void readline_handle_byte(ReadLineState *rs, int ch)
345 {
346     switch(rs->esc_state) {
347     case IS_NORM:
348         switch(ch) {
349         case 1:
350             readline_bol(rs);
351             break;
352         case 4:
353             readline_delete_char(rs);
354             break;
355         case 5:
356             readline_eol(rs);
357             break;
358         case 9:
359             readline_completion(rs);
360             break;
361         case 10:
362         case 13:
363             rs->cmd_buf[rs->cmd_buf_size] = '\0';
364             if (!rs->read_password)
365                 readline_hist_add(rs, rs->cmd_buf);
366             rs->printf_func(rs->opaque, "\n");
367             rs->cmd_buf_index = 0;
368             rs->cmd_buf_size = 0;
369             rs->last_cmd_buf_index = 0;
370             rs->last_cmd_buf_size = 0;
371             rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque);
372             break;
373         case 23:
374             /* ^W */
375             readline_backword(rs);
376             break;
377         case 27:
378             rs->esc_state = IS_ESC;
379             break;
380         case 127:
381         case 8:
382             readline_backspace(rs);
383             break;
384 	case 155:
385             rs->esc_state = IS_CSI;
386 	    break;
387         default:
388             if (ch >= 32) {
389                 readline_insert_char(rs, ch);
390             }
391             break;
392         }
393         break;
394     case IS_ESC:
395         if (ch == '[') {
396             rs->esc_state = IS_CSI;
397             rs->esc_param = 0;
398         } else if (ch == 'O') {
399             rs->esc_state = IS_SS3;
400             rs->esc_param = 0;
401         } else {
402             rs->esc_state = IS_NORM;
403         }
404         break;
405     case IS_CSI:
406         switch(ch) {
407 	case 'A':
408 	case 'F':
409 	    readline_up_char(rs);
410 	    break;
411 	case 'B':
412 	case 'E':
413 	    readline_down_char(rs);
414 	    break;
415         case 'D':
416             readline_backward_char(rs);
417             break;
418         case 'C':
419             readline_forward_char(rs);
420             break;
421         case '0' ... '9':
422             rs->esc_param = rs->esc_param * 10 + (ch - '0');
423             goto the_end;
424         case '~':
425             switch(rs->esc_param) {
426             case 1:
427                 readline_bol(rs);
428                 break;
429             case 3:
430                 readline_delete_char(rs);
431                 break;
432             case 4:
433                 readline_eol(rs);
434                 break;
435             }
436             break;
437         default:
438             break;
439         }
440         rs->esc_state = IS_NORM;
441     the_end:
442         break;
443     case IS_SS3:
444         switch(ch) {
445         case 'F':
446             readline_eol(rs);
447             break;
448         case 'H':
449             readline_bol(rs);
450             break;
451         }
452         rs->esc_state = IS_NORM;
453         break;
454     }
455     readline_update(rs);
456 }
457 
458 void readline_start(ReadLineState *rs, const char *prompt, int read_password,
459                     ReadLineFunc *readline_func, void *opaque)
460 {
461     pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
462     rs->readline_func = readline_func;
463     rs->readline_opaque = opaque;
464     rs->read_password = read_password;
465     readline_restart(rs);
466 }
467 
468 void readline_restart(ReadLineState *rs)
469 {
470     rs->cmd_buf_index = 0;
471     rs->cmd_buf_size = 0;
472 }
473 
474 const char *readline_get_history(ReadLineState *rs, unsigned int index)
475 {
476     if (index >= READLINE_MAX_CMDS)
477         return NULL;
478     return rs->history[index];
479 }
480 
481 ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
482                              ReadLineFlushFunc *flush_func,
483                              void *opaque,
484                              ReadLineCompletionFunc *completion_finder)
485 {
486     ReadLineState *rs = g_malloc0(sizeof(*rs));
487 
488     rs->hist_entry = -1;
489     rs->opaque = opaque;
490     rs->printf_func = printf_func;
491     rs->flush_func = flush_func;
492     rs->completion_finder = completion_finder;
493 
494     return rs;
495 }
496