readline.c 13.2 KB
Newer Older
1 2
/*
 * QEMU readline utility
3
 *
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
#include "monitor/readline.h"
#include "monitor/monitor.h"
26 27 28 29

#define IS_NORM 0
#define IS_ESC  1
#define IS_CSI  2
30
#define IS_SS3  3
31

32
#undef printf
33 34
#define printf do_not_use_printf

35
void readline_show_prompt(ReadLineState *rs)
36
{
37 38 39 40 41
    monitor_printf(rs->mon, "%s", rs->prompt);
    monitor_flush(rs->mon);
    rs->last_cmd_buf_index = 0;
    rs->last_cmd_buf_size = 0;
    rs->esc_state = IS_NORM;
42 43 44
}

/* update the displayed command line */
45
static void readline_update(ReadLineState *rs)
46 47 48
{
    int i, delta, len;

49 50 51 52
    if (rs->cmd_buf_size != rs->last_cmd_buf_size ||
        memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) {
        for(i = 0; i < rs->last_cmd_buf_index; i++) {
            monitor_printf(rs->mon, "\033[D");
53
        }
54 55 56
        rs->cmd_buf[rs->cmd_buf_size] = '\0';
        if (rs->read_password) {
            len = strlen(rs->cmd_buf);
57
            for(i = 0; i < len; i++)
58
                monitor_printf(rs->mon, "*");
59
        } else {
60
            monitor_printf(rs->mon, "%s", rs->cmd_buf);
61
        }
62 63 64 65
        monitor_printf(rs->mon, "\033[K");
        memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size);
        rs->last_cmd_buf_size = rs->cmd_buf_size;
        rs->last_cmd_buf_index = rs->cmd_buf_size;
66
    }
67 68
    if (rs->cmd_buf_index != rs->last_cmd_buf_index) {
        delta = rs->cmd_buf_index - rs->last_cmd_buf_index;
69 70
        if (delta > 0) {
            for(i = 0;i < delta; i++) {
71
                monitor_printf(rs->mon, "\033[C");
72 73 74 75
            }
        } else {
            delta = -delta;
            for(i = 0;i < delta; i++) {
76
                monitor_printf(rs->mon, "\033[D");
77 78
            }
        }
79
        rs->last_cmd_buf_index = rs->cmd_buf_index;
80
    }
81
    monitor_flush(rs->mon);
82 83
}

84
static void readline_insert_char(ReadLineState *rs, int ch)
85
{
86 87 88 89 90 91 92
    if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
        memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
                rs->cmd_buf + rs->cmd_buf_index,
                rs->cmd_buf_size - rs->cmd_buf_index);
        rs->cmd_buf[rs->cmd_buf_index] = ch;
        rs->cmd_buf_size++;
        rs->cmd_buf_index++;
93 94 95
    }
}

96
static void readline_backward_char(ReadLineState *rs)
97
{
98 99
    if (rs->cmd_buf_index > 0) {
        rs->cmd_buf_index--;
100 101 102
    }
}

103
static void readline_forward_char(ReadLineState *rs)
104
{
105 106
    if (rs->cmd_buf_index < rs->cmd_buf_size) {
        rs->cmd_buf_index++;
107 108 109
    }
}

110
static void readline_delete_char(ReadLineState *rs)
111
{
112 113 114 115 116
    if (rs->cmd_buf_index < rs->cmd_buf_size) {
        memmove(rs->cmd_buf + rs->cmd_buf_index,
                rs->cmd_buf + rs->cmd_buf_index + 1,
                rs->cmd_buf_size - rs->cmd_buf_index - 1);
        rs->cmd_buf_size--;
117 118 119
    }
}

120
static void readline_backspace(ReadLineState *rs)
121
{
122 123 124
    if (rs->cmd_buf_index > 0) {
        readline_backward_char(rs);
        readline_delete_char(rs);
125 126 127
    }
}

128
static void readline_backword(ReadLineState *rs)
129 130 131
{
    int start;

132
    if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
133 134 135
        return;
    }

136
    start = rs->cmd_buf_index - 1;
137 138 139

    /* find first word (backwards) */
    while (start > 0) {
140
        if (!qemu_isspace(rs->cmd_buf[start])) {
141 142 143 144 145 146 147 148
            break;
        }

        --start;
    }

    /* find first space (backwards) */
    while (start > 0) {
149
        if (qemu_isspace(rs->cmd_buf[start])) {
150 151 152 153 154 155 156 157
            ++start;
            break;
        }

        --start;
    }

    /* remove word */
158 159 160 161 162 163
    if (start < rs->cmd_buf_index) {
        memmove(rs->cmd_buf + start,
                rs->cmd_buf + rs->cmd_buf_index,
                rs->cmd_buf_size - rs->cmd_buf_index);
        rs->cmd_buf_size -= rs->cmd_buf_index - start;
        rs->cmd_buf_index = start;
164 165 166
    }
}

167
static void readline_bol(ReadLineState *rs)
168
{
169
    rs->cmd_buf_index = 0;
170 171
}

172
static void readline_eol(ReadLineState *rs)
173
{
174
    rs->cmd_buf_index = rs->cmd_buf_size;
175 176
}

177
static void readline_up_char(ReadLineState *rs)
178 179 180
{
    int idx;

181
    if (rs->hist_entry == 0)
182
	return;
183
    if (rs->hist_entry == -1) {
184
	/* Find latest entry */
185 186
	for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
	    if (rs->history[idx] == NULL)
187 188
		break;
	}
189
	rs->hist_entry = idx;
190
    }
191 192 193 194 195
    rs->hist_entry--;
    if (rs->hist_entry >= 0) {
	pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
                rs->history[rs->hist_entry]);
	rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
196 197 198
    }
}

199
static void readline_down_char(ReadLineState *rs)
200
{
201 202 203 204
    if (rs->hist_entry == -1)
        return;
    if (rs->hist_entry < READLINE_MAX_CMDS - 1 &&
        rs->history[++rs->hist_entry] != NULL) {
205 206
	pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
                rs->history[rs->hist_entry]);
207
    } else {
208
        rs->cmd_buf[0] = 0;
209
	rs->hist_entry = -1;
210
    }
211
    rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
212 213
}

214
static void readline_hist_add(ReadLineState *rs, const char *cmdline)
215 216 217 218 219 220 221
{
    char *hist_entry, *new_entry;
    int idx;

    if (cmdline[0] == '\0')
	return;
    new_entry = NULL;
222
    if (rs->hist_entry != -1) {
223
	/* We were editing an existing history entry: replace it */
224 225
	hist_entry = rs->history[rs->hist_entry];
	idx = rs->hist_entry;
226 227 228 229 230
	if (strcmp(hist_entry, cmdline) == 0) {
	    goto same_entry;
	}
    }
    /* Search cmdline in history buffers */
231 232
    for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
	hist_entry = rs->history[idx];
233 234 235 236 237 238
	if (hist_entry == NULL)
	    break;
	if (strcmp(hist_entry, cmdline) == 0) {
	same_entry:
	    new_entry = hist_entry;
	    /* Put this entry at the end of history */
239
	    memmove(&rs->history[idx], &rs->history[idx + 1],
240
		    (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
241 242 243
	    rs->history[READLINE_MAX_CMDS - 1] = NULL;
	    for (; idx < READLINE_MAX_CMDS; idx++) {
		if (rs->history[idx] == NULL)
244 245 246 247 248
		    break;
	    }
	    break;
	}
    }
249
    if (idx == READLINE_MAX_CMDS) {
250
	/* Need to get one free slot */
251
        g_free(rs->history[0]);
252 253
	memmove(rs->history, &rs->history[1],
	        (READLINE_MAX_CMDS - 1) * sizeof(char *));
254 255
	rs->history[READLINE_MAX_CMDS - 1] = NULL;
	idx = READLINE_MAX_CMDS - 1;
256 257
    }
    if (new_entry == NULL)
258
        new_entry = g_strdup(cmdline);
259 260
    rs->history[idx] = new_entry;
    rs->hist_entry = -1;
261 262 263 264
}

/* completion support */

265
void readline_add_completion(ReadLineState *rs, const char *str)
266
{
267
    if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
268
        rs->completions[rs->nb_completions++] = g_strdup(str);
269 270 271
    }
}

272
void readline_set_completion_index(ReadLineState *rs, int index)
aliguori's avatar
aliguori committed
273
{
274
    rs->completion_index = index;
aliguori's avatar
aliguori committed
275 276
}

277
static void readline_completion(ReadLineState *rs)
278
{
aliguori's avatar
aliguori committed
279
    Monitor *mon = cur_mon;
280
    int len, i, j, max_width, nb_cols, max_prefix;
281 282
    char *cmdline;

283
    rs->nb_completions = 0;
284

285
    cmdline = g_malloc(rs->cmd_buf_index + 1);
286 287 288
    memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index);
    cmdline[rs->cmd_buf_index] = '\0';
    rs->completion_finder(cmdline);
289
    g_free(cmdline);
290 291

    /* no completion found */
292
    if (rs->nb_completions <= 0)
293
        return;
294 295 296 297
    if (rs->nb_completions == 1) {
        len = strlen(rs->completions[0]);
        for(i = rs->completion_index; i < len; i++) {
            readline_insert_char(rs, rs->completions[0][i]);
298 299
        }
        /* extra space for next argument. XXX: make it more generic */
300 301
        if (len > 0 && rs->completions[0][len - 1] != '/')
            readline_insert_char(rs, ' ');
302
    } else {
aliguori's avatar
aliguori committed
303
        monitor_printf(mon, "\n");
304
        max_width = 0;
305
        max_prefix = 0;	
306 307
        for(i = 0; i < rs->nb_completions; i++) {
            len = strlen(rs->completions[i]);
308 309 310 311 312 313
            if (i==0) {
                max_prefix = len;
            } else {
                if (len < max_prefix)
                    max_prefix = len;
                for(j=0; j<max_prefix; j++) {
314
                    if (rs->completions[i][j] != rs->completions[0][j])
315 316 317
                        max_prefix = j;
                }
            }
318 319 320
            if (len > max_width)
                max_width = len;
        }
321
        if (max_prefix > 0) 
322 323
            for(i = rs->completion_index; i < max_prefix; i++) {
                readline_insert_char(rs, rs->completions[0][i]);
324
            }
325 326 327 328 329 330 331
        max_width += 2;
        if (max_width < 10)
            max_width = 10;
        else if (max_width > 80)
            max_width = 80;
        nb_cols = 80 / max_width;
        j = 0;
332 333 334 335
        for(i = 0; i < rs->nb_completions; i++) {
            monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]);
            if (++j == nb_cols || i == (rs->nb_completions - 1)) {
                monitor_printf(rs->mon, "\n");
336 337 338
                j = 0;
            }
        }
339
        readline_show_prompt(rs);
340
    }
341 342 343
    for (i = 0; i < rs->nb_completions; i++) {
        g_free(rs->completions[i]);
    }
344 345 346
}

/* return true if command handled */
347
void readline_handle_byte(ReadLineState *rs, int ch)
348
{
349
    switch(rs->esc_state) {
350 351 352
    case IS_NORM:
        switch(ch) {
        case 1:
353
            readline_bol(rs);
354 355
            break;
        case 4:
356
            readline_delete_char(rs);
357 358
            break;
        case 5:
359
            readline_eol(rs);
360 361
            break;
        case 9:
362
            readline_completion(rs);
363 364 365
            break;
        case 10:
        case 13:
366 367 368 369 370 371 372 373 374
            rs->cmd_buf[rs->cmd_buf_size] = '\0';
            if (!rs->read_password)
                readline_hist_add(rs, rs->cmd_buf);
            monitor_printf(rs->mon, "\n");
            rs->cmd_buf_index = 0;
            rs->cmd_buf_size = 0;
            rs->last_cmd_buf_index = 0;
            rs->last_cmd_buf_size = 0;
            rs->readline_func(rs->mon, rs->cmd_buf, rs->readline_opaque);
375
            break;
376 377
        case 23:
            /* ^W */
378
            readline_backword(rs);
379
            break;
380
        case 27:
381
            rs->esc_state = IS_ESC;
382 383 384
            break;
        case 127:
        case 8:
385
            readline_backspace(rs);
386 387
            break;
	case 155:
388
            rs->esc_state = IS_CSI;
389 390 391
	    break;
        default:
            if (ch >= 32) {
392
                readline_insert_char(rs, ch);
393 394 395 396 397 398
            }
            break;
        }
        break;
    case IS_ESC:
        if (ch == '[') {
399 400
            rs->esc_state = IS_CSI;
            rs->esc_param = 0;
401 402 403
        } else if (ch == 'O') {
            rs->esc_state = IS_SS3;
            rs->esc_param = 0;
404
        } else {
405
            rs->esc_state = IS_NORM;
406 407 408 409 410 411
        }
        break;
    case IS_CSI:
        switch(ch) {
	case 'A':
	case 'F':
412
	    readline_up_char(rs);
413 414 415
	    break;
	case 'B':
	case 'E':
416
	    readline_down_char(rs);
417 418
	    break;
        case 'D':
419
            readline_backward_char(rs);
420 421
            break;
        case 'C':
422
            readline_forward_char(rs);
423 424
            break;
        case '0' ... '9':
425
            rs->esc_param = rs->esc_param * 10 + (ch - '0');
426 427
            goto the_end;
        case '~':
428
            switch(rs->esc_param) {
429
            case 1:
430
                readline_bol(rs);
431 432
                break;
            case 3:
433
                readline_delete_char(rs);
434 435
                break;
            case 4:
436
                readline_eol(rs);
437 438 439 440 441 442
                break;
            }
            break;
        default:
            break;
        }
443
        rs->esc_state = IS_NORM;
444 445
    the_end:
        break;
446 447 448 449 450 451 452 453 454 455 456
    case IS_SS3:
        switch(ch) {
        case 'F':
            readline_eol(rs);
            break;
        case 'H':
            readline_bol(rs);
            break;
        }
        rs->esc_state = IS_NORM;
        break;
457
    }
458
    readline_update(rs);
459 460
}

461
void readline_start(ReadLineState *rs, const char *prompt, int read_password,
462 463
                    ReadLineFunc *readline_func, void *opaque)
{
464 465 466 467
    pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
    rs->readline_func = readline_func;
    rs->readline_opaque = opaque;
    rs->read_password = read_password;
468 469 470 471 472
    readline_restart(rs);
}

void readline_restart(ReadLineState *rs)
{
473 474
    rs->cmd_buf_index = 0;
    rs->cmd_buf_size = 0;
475 476
}

477
const char *readline_get_history(ReadLineState *rs, unsigned int index)
478
{
479
    if (index >= READLINE_MAX_CMDS)
480
        return NULL;
481 482 483 484 485 486
    return rs->history[index];
}

ReadLineState *readline_init(Monitor *mon,
                             ReadLineCompletionFunc *completion_finder)
{
487
    ReadLineState *rs = g_malloc0(sizeof(*rs));
488 489 490 491 492 493

    rs->hist_entry = -1;
    rs->mon = mon;
    rs->completion_finder = completion_finder;

    return rs;
494
}