monitor: Break out readline_show_prompt (Jan Kiszka)
[qemu/mini2440/sniper_sniper_test.git] / readline.c
blob5a089be898efbbb74348d0b44edb0569d6a8d5ff
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.
24 #include "qemu-common.h"
25 #include "console.h"
27 #define TERM_CMD_BUF_SIZE 4095
28 #define TERM_MAX_CMDS 64
29 #define NB_COMPLETIONS_MAX 256
31 #define IS_NORM 0
32 #define IS_ESC 1
33 #define IS_CSI 2
35 #define printf do_not_use_printf
37 static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1];
38 static int term_cmd_buf_index;
39 static int term_cmd_buf_size;
41 static char term_last_cmd_buf[TERM_CMD_BUF_SIZE + 1];
42 static int term_last_cmd_buf_index;
43 static int term_last_cmd_buf_size;
45 static int term_esc_state;
46 static int term_esc_param;
48 static char *term_history[TERM_MAX_CMDS];
49 static int term_hist_entry = -1;
51 static int nb_completions;
52 int completion_index;
53 static char *completions[NB_COMPLETIONS_MAX];
55 static ReadLineFunc *term_readline_func;
56 static int term_is_password;
57 static char term_prompt[256];
58 static void *term_readline_opaque;
60 void readline_show_prompt(void)
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;
69 /* update the displayed command line */
70 static void term_update(void)
72 int i, delta, len;
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");
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);
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;
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");
98 } else {
99 delta = -delta;
100 for(i = 0;i < delta; i++) {
101 term_printf("\033[D");
104 term_last_cmd_buf_index = term_cmd_buf_index;
106 term_flush();
109 static void term_insert_char(int ch)
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++;
121 static void term_backward_char(void)
123 if (term_cmd_buf_index > 0) {
124 term_cmd_buf_index--;
128 static void term_forward_char(void)
130 if (term_cmd_buf_index < term_cmd_buf_size) {
131 term_cmd_buf_index++;
135 static void term_delete_char(void)
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--;
145 static void term_backspace(void)
147 if (term_cmd_buf_index > 0) {
148 term_backward_char();
149 term_delete_char();
153 static void term_backword(void)
155 int start;
157 if (term_cmd_buf_index == 0 || term_cmd_buf_index > term_cmd_buf_size) {
158 return;
161 start = term_cmd_buf_index - 1;
163 /* find first word (backwards) */
164 while (start > 0) {
165 if (!qemu_isspace(term_cmd_buf[start])) {
166 break;
169 --start;
172 /* find first space (backwards) */
173 while (start > 0) {
174 if (qemu_isspace(term_cmd_buf[start])) {
175 ++start;
176 break;
179 --start;
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;
192 static void term_bol(void)
194 term_cmd_buf_index = 0;
197 static void term_eol(void)
199 term_cmd_buf_index = term_cmd_buf_size;
202 static void term_up_char(void)
204 int idx;
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;
214 term_hist_entry = idx;
216 term_hist_entry--;
217 if (term_hist_entry >= 0) {
218 pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
219 term_history[term_hist_entry]);
220 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
224 static void term_down_char(void)
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;
234 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
237 static void term_hist_add(const char *cmdline)
239 char *hist_entry, *new_entry;
240 int idx;
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;
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],
263 (TERM_MAX_CMDS - idx + 1) * sizeof(char *));
264 term_history[TERM_MAX_CMDS - 1] = NULL;
265 for (; idx < TERM_MAX_CMDS; idx++) {
266 if (term_history[idx] == NULL)
267 break;
269 break;
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],
276 (TERM_MAX_CMDS - 1) * sizeof(char *));
277 term_history[TERM_MAX_CMDS - 1] = NULL;
278 idx = TERM_MAX_CMDS - 1;
280 if (new_entry == NULL)
281 new_entry = strdup(cmdline);
282 term_history[idx] = new_entry;
283 term_hist_entry = -1;
286 /* completion support */
288 void add_completion(const char *str)
290 if (nb_completions < NB_COMPLETIONS_MAX) {
291 completions[nb_completions++] = qemu_strdup(str);
295 static void term_completion(void)
297 int len, i, j, max_width, nb_cols, max_prefix;
298 char *cmdline;
300 nb_completions = 0;
302 cmdline = qemu_malloc(term_cmd_buf_index + 1);
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);
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]);
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;
322 max_prefix = 0;
323 for(i = 0; i < nb_completions; i++) {
324 len = strlen(completions[i]);
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;
335 if (len > max_width)
336 max_width = len;
338 if (max_prefix > 0)
339 for(i = completion_index; i < max_prefix; i++) {
340 term_insert_char(completions[0][i]);
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;
356 readline_show_prompt();
360 /* return true if command handled */
361 void readline_handle_byte(int ch)
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");
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;
388 /* NOTE: readline_start can be called here */
389 term_readline_func(term_readline_opaque, term_cmd_buf);
390 break;
391 case 23:
392 /* ^W */
393 term_backword();
394 break;
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);
409 break;
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;
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;
451 break;
452 default:
453 break;
455 term_esc_state = IS_NORM;
456 the_end:
457 break;
459 term_update();
462 void readline_start(const char *prompt, int is_password,
463 ReadLineFunc *readline_func, void *opaque)
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;
469 term_cmd_buf_index = 0;
470 term_cmd_buf_size = 0;
473 const char *readline_get_history(unsigned int index)
475 if (index >= TERM_MAX_CMDS)
476 return NULL;
477 return term_history[index];