]> git.ipfire.org Git - thirdparty/qemu.git/blame - monitor.c
smb config fix for NT
[thirdparty/qemu.git] / monitor.c
CommitLineData
9dc39cba
FB
1/*
2 * QEMU monitor
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 */
9dc39cba 24#include "vl.h"
9307c4c1 25#include "disas.h"
81d0912d 26#include <dirent.h>
9dc39cba
FB
27
28//#define DEBUG
81d0912d 29//#define DEBUG_COMPLETION
9dc39cba 30
9307c4c1
FB
31#ifndef offsetof
32#define offsetof(type, field) ((size_t) &((type *)0)->field)
33#endif
34
9307c4c1
FB
35/*
36 * Supported types:
37 *
38 * 'F' filename
81d0912d 39 * 'B' block device name
9307c4c1
FB
40 * 's' string (accept optional quote)
41 * 'i' integer
42 * '/' optional gdb-like print format (like "/10x")
43 *
44 * '?' optional type (for 'F', 's' and 'i')
45 *
46 */
47
9dc39cba
FB
48typedef struct term_cmd_t {
49 const char *name;
9307c4c1
FB
50 const char *args_type;
51 void (*handler)();
9dc39cba
FB
52 const char *params;
53 const char *help;
54} term_cmd_t;
55
7e2515e8
FB
56static CharDriverState *monitor_hd;
57
9dc39cba
FB
58static term_cmd_t term_cmds[];
59static term_cmd_t info_cmds[];
60
7e2515e8
FB
61static char term_outbuf[1024];
62static int term_outbuf_index;
81d0912d 63
7e2515e8
FB
64static void monitor_start_input(void);
65
66void term_flush(void)
67{
68 if (term_outbuf_index > 0) {
69 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
70 term_outbuf_index = 0;
71 }
72}
73
74/* flush at every end of line or if the buffer is full */
75void term_puts(const char *str)
76{
77 int c;
78 for(;;) {
79 c = *str++;
80 if (c == '\0')
81 break;
82 term_outbuf[term_outbuf_index++] = c;
83 if (term_outbuf_index >= sizeof(term_outbuf) ||
84 c == '\n')
85 term_flush();
86 }
87}
88
89void term_vprintf(const char *fmt, va_list ap)
9dc39cba 90{
81d0912d 91 char buf[4096];
81d0912d 92 vsnprintf(buf, sizeof(buf), fmt, ap);
7e2515e8 93 term_puts(buf);
9dc39cba
FB
94}
95
7e2515e8 96void term_printf(const char *fmt, ...)
9dc39cba 97{
7e2515e8
FB
98 va_list ap;
99 va_start(ap, fmt);
100 term_vprintf(fmt, ap);
101 va_end(ap);
9dc39cba
FB
102}
103
104static int compare_cmd(const char *name, const char *list)
105{
106 const char *p, *pstart;
107 int len;
108 len = strlen(name);
109 p = list;
110 for(;;) {
111 pstart = p;
112 p = strchr(p, '|');
113 if (!p)
114 p = pstart + strlen(pstart);
115 if ((p - pstart) == len && !memcmp(pstart, name, len))
116 return 1;
117 if (*p == '\0')
118 break;
119 p++;
120 }
121 return 0;
122}
123
124static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
125{
126 term_cmd_t *cmd;
127
128 for(cmd = cmds; cmd->name != NULL; cmd++) {
129 if (!name || !strcmp(name, cmd->name))
130 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
131 }
132}
133
134static void help_cmd(const char *name)
135{
136 if (name && !strcmp(name, "info")) {
137 help_cmd1(info_cmds, "info ", NULL);
138 } else {
139 help_cmd1(term_cmds, "", name);
f193c797
FB
140 if (name && !strcmp(name, "log")) {
141 CPULogItem *item;
142 term_printf("Log items (comma separated):\n");
143 term_printf("%-10s %s\n", "none", "remove all logs");
144 for(item = cpu_log_items; item->mask != 0; item++) {
145 term_printf("%-10s %s\n", item->name, item->help);
146 }
147 }
9dc39cba
FB
148 }
149}
150
9307c4c1 151static void do_help(const char *name)
9dc39cba 152{
9307c4c1 153 help_cmd(name);
9dc39cba
FB
154}
155
9307c4c1 156static void do_commit(void)
9dc39cba
FB
157{
158 int i;
159
160 for (i = 0; i < MAX_DISKS; i++) {
7e2515e8 161 if (bs_table[i]) {
9dc39cba 162 bdrv_commit(bs_table[i]);
7e2515e8 163 }
9dc39cba
FB
164 }
165}
166
9307c4c1 167static void do_info(const char *item)
9dc39cba
FB
168{
169 term_cmd_t *cmd;
9dc39cba 170
9307c4c1 171 if (!item)
9dc39cba 172 goto help;
9dc39cba 173 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
9307c4c1 174 if (compare_cmd(item, cmd->name))
9dc39cba
FB
175 goto found;
176 }
177 help:
9307c4c1 178 help_cmd("info");
9dc39cba
FB
179 return;
180 found:
9307c4c1 181 cmd->handler();
9dc39cba
FB
182}
183
9307c4c1 184static void do_info_network(void)
9dc39cba
FB
185{
186 int i, j;
187 NetDriverState *nd;
188
189 for(i = 0; i < nb_nics; i++) {
190 nd = &nd_table[i];
191 term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
192 for(j = 0; j < 6; j++) {
193 if (j > 0)
194 term_printf(":");
195 term_printf("%02x", nd->macaddr[j]);
196 }
197 term_printf("\n");
198 }
199}
200
9307c4c1 201static void do_info_block(void)
9dc39cba
FB
202{
203 bdrv_info();
204}
205
9307c4c1
FB
206static void do_info_registers(void)
207{
208#ifdef TARGET_I386
209 cpu_dump_state(cpu_single_env, stdout, X86_DUMP_FPU | X86_DUMP_CCOP);
210#else
211 cpu_dump_state(cpu_single_env, stdout, 0);
212#endif
213}
214
aa455485
FB
215static void do_info_history (void)
216{
217 int i;
7e2515e8
FB
218 const char *str;
219
220 i = 0;
221 for(;;) {
222 str = readline_get_history(i);
223 if (!str)
224 break;
225 term_printf("%d: '%s'\n", i, str);
aa455485
FB
226 }
227}
228
9307c4c1 229static void do_quit(void)
9dc39cba
FB
230{
231 exit(0);
232}
233
234static int eject_device(BlockDriverState *bs, int force)
235{
236 if (bdrv_is_inserted(bs)) {
237 if (!force) {
238 if (!bdrv_is_removable(bs)) {
239 term_printf("device is not removable\n");
240 return -1;
241 }
242 if (bdrv_is_locked(bs)) {
243 term_printf("device is locked\n");
244 return -1;
245 }
246 }
247 bdrv_close(bs);
248 }
249 return 0;
250}
251
9307c4c1 252static void do_eject(int force, const char *filename)
9dc39cba
FB
253{
254 BlockDriverState *bs;
9dc39cba 255
9307c4c1 256 bs = bdrv_find(filename);
9dc39cba
FB
257 if (!bs) {
258 term_printf("device not found\n");
259 return;
260 }
261 eject_device(bs, force);
262}
263
9307c4c1 264static void do_change(const char *device, const char *filename)
9dc39cba
FB
265{
266 BlockDriverState *bs;
7e2515e8
FB
267 int i;
268 char password[256];
9dc39cba 269
9307c4c1 270 bs = bdrv_find(device);
9dc39cba
FB
271 if (!bs) {
272 term_printf("device not found\n");
273 return;
274 }
275 if (eject_device(bs, 0) < 0)
276 return;
9307c4c1 277 bdrv_open(bs, filename, 0);
7e2515e8
FB
278 if (bdrv_is_encrypted(bs)) {
279 term_printf("%s is encrypted.\n", device);
280 for(i = 0; i < 3; i++) {
281 monitor_readline("Password: ", 1, password, sizeof(password));
282 if (bdrv_set_key(bs, password) == 0)
283 break;
284 term_printf("invalid password\n");
285 }
286 }
9dc39cba
FB
287}
288
9307c4c1 289static void do_screen_dump(const char *filename)
59a983b9 290{
9307c4c1 291 vga_screen_dump(filename);
59a983b9
FB
292}
293
9307c4c1 294static void do_log(const char *items)
f193c797
FB
295{
296 int mask;
297
9307c4c1 298 if (!strcmp(items, "none")) {
f193c797
FB
299 mask = 0;
300 } else {
9307c4c1 301 mask = cpu_str_to_log_mask(items);
f193c797 302 if (!mask) {
9307c4c1 303 help_cmd("log");
f193c797
FB
304 return;
305 }
306 }
307 cpu_set_log(mask);
308}
309
9307c4c1 310static void do_savevm(const char *filename)
8a7ddc38 311{
9307c4c1
FB
312 if (qemu_savevm(filename) < 0)
313 term_printf("I/O error when saving VM to '%s'\n", filename);
8a7ddc38
FB
314}
315
9307c4c1 316static void do_loadvm(const char *filename)
8a7ddc38 317{
9307c4c1
FB
318 if (qemu_loadvm(filename) < 0)
319 term_printf("I/O error when loading VM from '%s'\n", filename);
8a7ddc38
FB
320}
321
9307c4c1 322static void do_stop(void)
8a7ddc38
FB
323{
324 vm_stop(EXCP_INTERRUPT);
325}
326
9307c4c1 327static void do_cont(void)
8a7ddc38
FB
328{
329 vm_start();
330}
331
67b915a5 332#ifdef CONFIG_GDBSTUB
9307c4c1 333static void do_gdbserver(int has_port, int port)
8a7ddc38 334{
9307c4c1
FB
335 if (!has_port)
336 port = DEFAULT_GDBSTUB_PORT;
8a7ddc38
FB
337 if (gdbserver_start(port) < 0) {
338 qemu_printf("Could not open gdbserver socket on port %d\n", port);
339 } else {
340 qemu_printf("Waiting gdb connection on port %d\n", port);
341 }
342}
67b915a5 343#endif
8a7ddc38 344
9307c4c1
FB
345static void term_printc(int c)
346{
347 term_printf("'");
348 switch(c) {
349 case '\'':
350 term_printf("\\'");
351 break;
352 case '\\':
353 term_printf("\\\\");
354 break;
355 case '\n':
356 term_printf("\\n");
357 break;
358 case '\r':
359 term_printf("\\r");
360 break;
361 default:
362 if (c >= 32 && c <= 126) {
363 term_printf("%c", c);
364 } else {
365 term_printf("\\x%02x", c);
366 }
367 break;
368 }
369 term_printf("'");
370}
371
372static void memory_dump(int count, int format, int wsize,
373 target_ulong addr, int is_physical)
374{
375 int nb_per_line, l, line_size, i, max_digits, len;
376 uint8_t buf[16];
377 uint64_t v;
378
379 if (format == 'i') {
380 int flags;
381 flags = 0;
382#ifdef TARGET_I386
4c27ba27 383 if (wsize == 2) {
9307c4c1 384 flags = 1;
4c27ba27
FB
385 } else if (wsize == 4) {
386 flags = 0;
387 } else {
388 /* as default we use the current CS size */
389 flags = 0;
390 if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
391 flags = 1;
392 }
393#endif
9307c4c1
FB
394 monitor_disas(addr, count, is_physical, flags);
395 return;
396 }
397
398 len = wsize * count;
399 if (wsize == 1)
400 line_size = 8;
401 else
402 line_size = 16;
403 nb_per_line = line_size / wsize;
404 max_digits = 0;
405
406 switch(format) {
407 case 'o':
408 max_digits = (wsize * 8 + 2) / 3;
409 break;
410 default:
411 case 'x':
412 max_digits = (wsize * 8) / 4;
413 break;
414 case 'u':
415 case 'd':
416 max_digits = (wsize * 8 * 10 + 32) / 33;
417 break;
418 case 'c':
419 wsize = 1;
420 break;
421 }
422
423 while (len > 0) {
424 term_printf("0x%08x:", addr);
425 l = len;
426 if (l > line_size)
427 l = line_size;
428 if (is_physical) {
429 cpu_physical_memory_rw(addr, buf, l, 0);
430 } else {
431 cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
432 }
433 i = 0;
434 while (i < l) {
435 switch(wsize) {
436 default:
437 case 1:
438 v = ldub_raw(buf + i);
439 break;
440 case 2:
441 v = lduw_raw(buf + i);
442 break;
443 case 4:
444 v = ldl_raw(buf + i);
445 break;
446 case 8:
447 v = ldq_raw(buf + i);
448 break;
449 }
450 term_printf(" ");
451 switch(format) {
452 case 'o':
453 term_printf("%#*llo", max_digits, v);
454 break;
455 case 'x':
456 term_printf("0x%0*llx", max_digits, v);
457 break;
458 case 'u':
459 term_printf("%*llu", max_digits, v);
460 break;
461 case 'd':
462 term_printf("%*lld", max_digits, v);
463 break;
464 case 'c':
465 term_printc(v);
466 break;
467 }
468 i += wsize;
469 }
470 term_printf("\n");
471 addr += l;
472 len -= l;
473 }
474}
475
476static void do_memory_dump(int count, int format, int size, int addr)
477{
478 memory_dump(count, format, size, addr, 0);
479}
480
481static void do_physical_memory_dump(int count, int format, int size, int addr)
482{
483 memory_dump(count, format, size, addr, 1);
484}
485
486static void do_print(int count, int format, int size, int val)
487{
488 switch(format) {
489 case 'o':
490 term_printf("%#o", val);
491 break;
492 case 'x':
493 term_printf("%#x", val);
494 break;
495 case 'u':
496 term_printf("%u", val);
497 break;
498 default:
499 case 'd':
500 term_printf("%d", val);
501 break;
502 case 'c':
503 term_printc(val);
504 break;
505 }
506 term_printf("\n");
507}
508
a3a91a35
FB
509typedef struct {
510 int keycode;
511 const char *name;
512} KeyDef;
513
514static const KeyDef key_defs[] = {
515 { 0x2a, "shift" },
516 { 0x36, "shift_r" },
517
518 { 0x38, "alt" },
519 { 0xb8, "alt_r" },
520 { 0x1d, "ctrl" },
521 { 0x9d, "ctrl_r" },
522
523 { 0xdd, "menu" },
524
525 { 0x01, "esc" },
526
527 { 0x02, "1" },
528 { 0x03, "2" },
529 { 0x04, "3" },
530 { 0x05, "4" },
531 { 0x06, "5" },
532 { 0x07, "6" },
533 { 0x08, "7" },
534 { 0x09, "8" },
535 { 0x0a, "9" },
536 { 0x0b, "0" },
537 { 0x0e, "backspace" },
538
539 { 0x0f, "tab" },
540 { 0x10, "q" },
541 { 0x11, "w" },
542 { 0x12, "e" },
543 { 0x13, "r" },
544 { 0x14, "t" },
545 { 0x15, "y" },
546 { 0x16, "u" },
547 { 0x17, "i" },
548 { 0x18, "o" },
549 { 0x19, "p" },
550
551 { 0x1c, "ret" },
552
553 { 0x1e, "a" },
554 { 0x1f, "s" },
555 { 0x20, "d" },
556 { 0x21, "f" },
557 { 0x22, "g" },
558 { 0x23, "h" },
559 { 0x24, "j" },
560 { 0x25, "k" },
561 { 0x26, "l" },
562
563 { 0x2c, "z" },
564 { 0x2d, "x" },
565 { 0x2e, "c" },
566 { 0x2f, "v" },
567 { 0x30, "b" },
568 { 0x31, "n" },
569 { 0x32, "m" },
570
571 { 0x39, "spc" },
00ffa62a 572 { 0x3a, "caps_lock" },
a3a91a35
FB
573 { 0x3b, "f1" },
574 { 0x3c, "f2" },
575 { 0x3d, "f3" },
576 { 0x3e, "f4" },
577 { 0x3f, "f5" },
578 { 0x40, "f6" },
579 { 0x41, "f7" },
580 { 0x42, "f8" },
581 { 0x43, "f9" },
582 { 0x44, "f10" },
00ffa62a 583 { 0x45, "num_lock" },
a3a91a35
FB
584 { 0x46, "scroll_lock" },
585
586 { 0x56, "<" },
587
588 { 0x57, "f11" },
589 { 0x58, "f12" },
590
591 { 0xb7, "print" },
592
593 { 0xc7, "home" },
594 { 0xc9, "pgup" },
595 { 0xd1, "pgdn" },
596 { 0xcf, "end" },
597
598 { 0xcb, "left" },
599 { 0xc8, "up" },
600 { 0xd0, "down" },
601 { 0xcd, "right" },
602
603 { 0xd2, "insert" },
604 { 0xd3, "delete" },
605 { 0, NULL },
606};
607
608static int get_keycode(const char *key)
609{
610 const KeyDef *p;
611
612 for(p = key_defs; p->name != NULL; p++) {
613 if (!strcmp(key, p->name))
614 return p->keycode;
615 }
616 return -1;
617}
618
619static void do_send_key(const char *string)
620{
621 char keybuf[16], *q;
622 uint8_t keycodes[16];
623 const char *p;
624 int nb_keycodes, keycode, i;
625
626 nb_keycodes = 0;
627 p = string;
628 while (*p != '\0') {
629 q = keybuf;
630 while (*p != '\0' && *p != '-') {
631 if ((q - keybuf) < sizeof(keybuf) - 1) {
632 *q++ = *p;
633 }
634 p++;
635 }
636 *q = '\0';
637 keycode = get_keycode(keybuf);
638 if (keycode < 0) {
639 term_printf("unknown key: '%s'\n", keybuf);
640 return;
641 }
642 keycodes[nb_keycodes++] = keycode;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 /* key down events */
648 for(i = 0; i < nb_keycodes; i++) {
649 keycode = keycodes[i];
650 if (keycode & 0x80)
651 kbd_put_keycode(0xe0);
652 kbd_put_keycode(keycode & 0x7f);
653 }
654 /* key up events */
655 for(i = nb_keycodes - 1; i >= 0; i--) {
656 keycode = keycodes[i];
657 if (keycode & 0x80)
658 kbd_put_keycode(0xe0);
659 kbd_put_keycode(keycode | 0x80);
660 }
661}
662
3440557b
FB
663static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
664{
665 uint32_t val;
666 int suffix;
667
668 if (has_index) {
669 cpu_outb(NULL, addr & 0xffff, index & 0xff);
670 addr++;
671 }
672 addr &= 0xffff;
673
674 switch(size) {
675 default:
676 case 1:
677 val = cpu_inb(NULL, addr);
678 suffix = 'b';
679 break;
680 case 2:
681 val = cpu_inw(NULL, addr);
682 suffix = 'w';
683 break;
684 case 4:
685 val = cpu_inl(NULL, addr);
686 suffix = 'l';
687 break;
688 }
689 term_printf("port%c[0x%04x] = %#0*x\n",
690 suffix, addr, size * 2, val);
691}
a3a91a35 692
e4f9082b
FB
693static void do_system_reset(void)
694{
695 qemu_system_reset_request();
696}
697
9dc39cba 698static term_cmd_t term_cmds[] = {
9307c4c1 699 { "help|?", "s?", do_help,
9dc39cba 700 "[cmd]", "show the help" },
9307c4c1 701 { "commit", "", do_commit,
9dc39cba 702 "", "commit changes to the disk images (if -snapshot is used)" },
9307c4c1 703 { "info", "s?", do_info,
9dc39cba 704 "subcommand", "show various information about the system state" },
9307c4c1 705 { "q|quit", "", do_quit,
9dc39cba 706 "", "quit the emulator" },
81d0912d 707 { "eject", "-fB", do_eject,
9dc39cba 708 "[-f] device", "eject a removable media (use -f to force it)" },
81d0912d 709 { "change", "BF", do_change,
9dc39cba 710 "device filename", "change a removable media" },
9307c4c1 711 { "screendump", "F", do_screen_dump,
59a983b9 712 "filename", "save screen into PPM image 'filename'" },
9307c4c1 713 { "log", "s", do_log,
f193c797 714 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
9307c4c1 715 { "savevm", "F", do_savevm,
8a7ddc38 716 "filename", "save the whole virtual machine state to 'filename'" },
9307c4c1 717 { "loadvm", "F", do_loadvm,
8a7ddc38 718 "filename", "restore the whole virtual machine state from 'filename'" },
9307c4c1
FB
719 { "stop", "", do_stop,
720 "", "stop emulation", },
721 { "c|cont", "", do_cont,
722 "", "resume emulation", },
67b915a5 723#ifdef CONFIG_GDBSTUB
9307c4c1
FB
724 { "gdbserver", "i?", do_gdbserver,
725 "[port]", "start gdbserver session (default port=1234)", },
67b915a5 726#endif
9307c4c1
FB
727 { "x", "/i", do_memory_dump,
728 "/fmt addr", "virtual memory dump starting at 'addr'", },
729 { "xp", "/i", do_physical_memory_dump,
730 "/fmt addr", "physical memory dump starting at 'addr'", },
731 { "p|print", "/i", do_print,
732 "/fmt expr", "print expression value (use $reg for CPU register access)", },
3440557b
FB
733 { "i", "/ii.", do_ioport_read,
734 "/fmt addr", "I/O port read" },
735
a3a91a35
FB
736 { "sendkey", "s", do_send_key,
737 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
e4f9082b
FB
738 { "system_reset", "", do_system_reset,
739 "", "reset the system" },
f193c797 740 { NULL, NULL, },
9dc39cba
FB
741};
742
743static term_cmd_t info_cmds[] = {
9307c4c1 744 { "network", "", do_info_network,
9dc39cba 745 "", "show the network state" },
9307c4c1 746 { "block", "", do_info_block,
9dc39cba 747 "", "show the block devices" },
9307c4c1
FB
748 { "registers", "", do_info_registers,
749 "", "show the cpu registers" },
aa455485
FB
750 { "history", "", do_info_history,
751 "", "show the command line history", },
4a0fb71e
FB
752 { "irq", "", irq_info,
753 "", "show the interrupts statistics (if available)", },
4c27ba27
FB
754 { "pic", "", pic_info,
755 "", "show i8259 (PIC) state", },
86e0c048
FB
756 { "pci", "", pci_info,
757 "", "show PCI info", },
9dc39cba
FB
758 { NULL, NULL, },
759};
760
9307c4c1
FB
761/*******************************************************************/
762
763static const char *pch;
764static jmp_buf expr_env;
765
766typedef struct MonitorDef {
767 const char *name;
768 int offset;
769 int (*get_value)(struct MonitorDef *md);
770} MonitorDef;
771
57206fd4
FB
772#if defined(TARGET_I386)
773static int monitor_get_pc (struct MonitorDef *md)
774{
775 return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base;
776}
777#endif
778
a541f297
FB
779#if defined(TARGET_PPC)
780static int monitor_get_ccr (struct MonitorDef *md)
781{
782 unsigned int u;
783 int i;
784
785 u = 0;
786 for (i = 0; i < 8; i++)
787 u |= cpu_single_env->crf[i] << (32 - (4 * i));
788
789 return u;
790}
791
792static int monitor_get_msr (struct MonitorDef *md)
793{
794 return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
795 (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
796 (cpu_single_env->msr[MSR_EE] << MSR_EE) |
797 (cpu_single_env->msr[MSR_PR] << MSR_PR) |
798 (cpu_single_env->msr[MSR_FP] << MSR_FP) |
799 (cpu_single_env->msr[MSR_ME] << MSR_ME) |
800 (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
801 (cpu_single_env->msr[MSR_SE] << MSR_SE) |
802 (cpu_single_env->msr[MSR_BE] << MSR_BE) |
803 (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
804 (cpu_single_env->msr[MSR_IP] << MSR_IP) |
805 (cpu_single_env->msr[MSR_IR] << MSR_IR) |
806 (cpu_single_env->msr[MSR_DR] << MSR_DR) |
807 (cpu_single_env->msr[MSR_RI] << MSR_RI) |
808 (cpu_single_env->msr[MSR_LE] << MSR_LE);
809}
810
811static int monitor_get_xer (struct MonitorDef *md)
812{
813 return (cpu_single_env->xer[XER_SO] << XER_SO) |
814 (cpu_single_env->xer[XER_OV] << XER_OV) |
815 (cpu_single_env->xer[XER_CA] << XER_CA) |
816 (cpu_single_env->xer[XER_BC] << XER_BC);
817}
9fddaa0c
FB
818
819uint32_t cpu_ppc_load_decr (CPUState *env);
820static int monitor_get_decr (struct MonitorDef *md)
821{
822 return cpu_ppc_load_decr(cpu_single_env);
823}
824
825uint32_t cpu_ppc_load_tbu (CPUState *env);
826static int monitor_get_tbu (struct MonitorDef *md)
827{
828 return cpu_ppc_load_tbu(cpu_single_env);
829}
830
831uint32_t cpu_ppc_load_tbl (CPUState *env);
832static int monitor_get_tbl (struct MonitorDef *md)
833{
834 return cpu_ppc_load_tbl(cpu_single_env);
835}
a541f297
FB
836#endif
837
9307c4c1
FB
838static MonitorDef monitor_defs[] = {
839#ifdef TARGET_I386
57206fd4
FB
840
841#define SEG(name, seg) \
842 { name, offsetof(CPUState, segs[seg].selector) },\
843 { name ".base", offsetof(CPUState, segs[seg].base) },\
844 { name ".limit", offsetof(CPUState, segs[seg].limit) },
845
9307c4c1
FB
846 { "eax", offsetof(CPUState, regs[0]) },
847 { "ecx", offsetof(CPUState, regs[1]) },
848 { "edx", offsetof(CPUState, regs[2]) },
849 { "ebx", offsetof(CPUState, regs[3]) },
850 { "esp|sp", offsetof(CPUState, regs[4]) },
851 { "ebp|fp", offsetof(CPUState, regs[5]) },
852 { "esi", offsetof(CPUState, regs[6]) },
853 { "esi", offsetof(CPUState, regs[7]) },
854 { "eflags", offsetof(CPUState, eflags) },
57206fd4
FB
855 { "eip", offsetof(CPUState, eip) },
856 SEG("cs", R_CS)
857 SEG("ds", R_DS)
858 SEG("es", R_ES)
859 SEG("fs", R_FS)
860 SEG("gs", R_GS)
861 { "pc", 0, monitor_get_pc, },
a541f297
FB
862#elif defined(TARGET_PPC)
863 { "r0", offsetof(CPUState, gpr[0]) },
864 { "r1", offsetof(CPUState, gpr[1]) },
865 { "r2", offsetof(CPUState, gpr[2]) },
866 { "r3", offsetof(CPUState, gpr[3]) },
867 { "r4", offsetof(CPUState, gpr[4]) },
868 { "r5", offsetof(CPUState, gpr[5]) },
869 { "r6", offsetof(CPUState, gpr[6]) },
870 { "r7", offsetof(CPUState, gpr[7]) },
871 { "r8", offsetof(CPUState, gpr[8]) },
872 { "r9", offsetof(CPUState, gpr[9]) },
873 { "r10", offsetof(CPUState, gpr[10]) },
874 { "r11", offsetof(CPUState, gpr[11]) },
875 { "r12", offsetof(CPUState, gpr[12]) },
876 { "r13", offsetof(CPUState, gpr[13]) },
877 { "r14", offsetof(CPUState, gpr[14]) },
878 { "r15", offsetof(CPUState, gpr[15]) },
879 { "r16", offsetof(CPUState, gpr[16]) },
880 { "r17", offsetof(CPUState, gpr[17]) },
881 { "r18", offsetof(CPUState, gpr[18]) },
882 { "r19", offsetof(CPUState, gpr[19]) },
883 { "r20", offsetof(CPUState, gpr[20]) },
884 { "r21", offsetof(CPUState, gpr[21]) },
885 { "r22", offsetof(CPUState, gpr[22]) },
886 { "r23", offsetof(CPUState, gpr[23]) },
887 { "r24", offsetof(CPUState, gpr[24]) },
888 { "r25", offsetof(CPUState, gpr[25]) },
889 { "r26", offsetof(CPUState, gpr[26]) },
890 { "r27", offsetof(CPUState, gpr[27]) },
891 { "r28", offsetof(CPUState, gpr[28]) },
892 { "r29", offsetof(CPUState, gpr[29]) },
893 { "r30", offsetof(CPUState, gpr[30]) },
894 { "r31", offsetof(CPUState, gpr[31]) },
57206fd4 895 { "nip|pc", offsetof(CPUState, nip) },
a541f297
FB
896 { "lr", offsetof(CPUState, lr) },
897 { "ctr", offsetof(CPUState, ctr) },
9fddaa0c 898 { "decr", 0, &monitor_get_decr, },
a541f297
FB
899 { "ccr", 0, &monitor_get_ccr, },
900 { "msr", 0, &monitor_get_msr, },
901 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
902 { "tbu", 0, &monitor_get_tbu, },
903 { "tbl", 0, &monitor_get_tbl, },
a541f297
FB
904 { "sdr1", offsetof(CPUState, sdr1) },
905 { "sr0", offsetof(CPUState, sr[0]) },
906 { "sr1", offsetof(CPUState, sr[1]) },
907 { "sr2", offsetof(CPUState, sr[2]) },
908 { "sr3", offsetof(CPUState, sr[3]) },
909 { "sr4", offsetof(CPUState, sr[4]) },
910 { "sr5", offsetof(CPUState, sr[5]) },
911 { "sr6", offsetof(CPUState, sr[6]) },
912 { "sr7", offsetof(CPUState, sr[7]) },
913 { "sr8", offsetof(CPUState, sr[8]) },
914 { "sr9", offsetof(CPUState, sr[9]) },
915 { "sr10", offsetof(CPUState, sr[10]) },
916 { "sr11", offsetof(CPUState, sr[11]) },
917 { "sr12", offsetof(CPUState, sr[12]) },
918 { "sr13", offsetof(CPUState, sr[13]) },
919 { "sr14", offsetof(CPUState, sr[14]) },
920 { "sr15", offsetof(CPUState, sr[15]) },
921 /* Too lazy to put BATs and SPRs ... */
9307c4c1
FB
922#endif
923 { NULL },
924};
925
926static void expr_error(const char *fmt)
9dc39cba 927{
9307c4c1
FB
928 term_printf(fmt);
929 term_printf("\n");
930 longjmp(expr_env, 1);
931}
932
933static int get_monitor_def(int *pval, const char *name)
934{
935 MonitorDef *md;
936 for(md = monitor_defs; md->name != NULL; md++) {
937 if (compare_cmd(name, md->name)) {
938 if (md->get_value) {
939 *pval = md->get_value(md);
940 } else {
941 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
942 }
943 return 0;
944 }
945 }
946 return -1;
947}
948
949static void next(void)
950{
951 if (pch != '\0') {
952 pch++;
953 while (isspace(*pch))
954 pch++;
955 }
956}
957
958static int expr_sum(void);
959
960static int expr_unary(void)
961{
962 int n;
963 char *p;
964
965 switch(*pch) {
966 case '+':
967 next();
968 n = expr_unary();
969 break;
970 case '-':
971 next();
972 n = -expr_unary();
973 break;
974 case '~':
975 next();
976 n = ~expr_unary();
977 break;
978 case '(':
979 next();
980 n = expr_sum();
981 if (*pch != ')') {
982 expr_error("')' expected");
983 }
984 next();
985 break;
81d0912d
FB
986 case '\'':
987 pch++;
988 if (*pch == '\0')
989 expr_error("character constant expected");
990 n = *pch;
991 pch++;
992 if (*pch != '\'')
993 expr_error("missing terminating \' character");
994 next();
995 break;
9307c4c1
FB
996 case '$':
997 {
998 char buf[128], *q;
999
1000 pch++;
1001 q = buf;
1002 while ((*pch >= 'a' && *pch <= 'z') ||
1003 (*pch >= 'A' && *pch <= 'Z') ||
1004 (*pch >= '0' && *pch <= '9') ||
57206fd4 1005 *pch == '_' || *pch == '.') {
9307c4c1
FB
1006 if ((q - buf) < sizeof(buf) - 1)
1007 *q++ = *pch;
1008 pch++;
1009 }
1010 while (isspace(*pch))
1011 pch++;
1012 *q = 0;
1013 if (get_monitor_def(&n, buf))
1014 expr_error("unknown register");
1015 }
1016 break;
1017 case '\0':
1018 expr_error("unexpected end of expression");
1019 n = 0;
1020 break;
1021 default:
1022 n = strtoul(pch, &p, 0);
1023 if (pch == p) {
1024 expr_error("invalid char in expression");
1025 }
1026 pch = p;
1027 while (isspace(*pch))
1028 pch++;
1029 break;
1030 }
1031 return n;
1032}
1033
1034
1035static int expr_prod(void)
1036{
1037 int val, val2, op;
1038
1039 val = expr_unary();
1040 for(;;) {
1041 op = *pch;
1042 if (op != '*' && op != '/' && op != '%')
1043 break;
1044 next();
1045 val2 = expr_unary();
1046 switch(op) {
1047 default:
1048 case '*':
1049 val *= val2;
1050 break;
1051 case '/':
1052 case '%':
1053 if (val2 == 0)
5b60212f 1054 expr_error("division by zero");
9307c4c1
FB
1055 if (op == '/')
1056 val /= val2;
1057 else
1058 val %= val2;
1059 break;
1060 }
1061 }
1062 return val;
1063}
1064
1065static int expr_logic(void)
1066{
1067 int val, val2, op;
1068
1069 val = expr_prod();
1070 for(;;) {
1071 op = *pch;
1072 if (op != '&' && op != '|' && op != '^')
1073 break;
1074 next();
1075 val2 = expr_prod();
1076 switch(op) {
1077 default:
1078 case '&':
1079 val &= val2;
1080 break;
1081 case '|':
1082 val |= val2;
1083 break;
1084 case '^':
1085 val ^= val2;
1086 break;
1087 }
1088 }
1089 return val;
1090}
1091
1092static int expr_sum(void)
1093{
1094 int val, val2, op;
1095
1096 val = expr_logic();
1097 for(;;) {
1098 op = *pch;
1099 if (op != '+' && op != '-')
1100 break;
1101 next();
1102 val2 = expr_logic();
1103 if (op == '+')
1104 val += val2;
1105 else
1106 val -= val2;
1107 }
1108 return val;
1109}
1110
1111static int get_expr(int *pval, const char **pp)
1112{
1113 pch = *pp;
1114 if (setjmp(expr_env)) {
1115 *pp = pch;
1116 return -1;
1117 }
1118 while (isspace(*pch))
1119 pch++;
1120 *pval = expr_sum();
1121 *pp = pch;
1122 return 0;
1123}
1124
1125static int get_str(char *buf, int buf_size, const char **pp)
1126{
1127 const char *p;
1128 char *q;
1129 int c;
1130
81d0912d 1131 q = buf;
9307c4c1
FB
1132 p = *pp;
1133 while (isspace(*p))
1134 p++;
1135 if (*p == '\0') {
1136 fail:
81d0912d 1137 *q = '\0';
9307c4c1
FB
1138 *pp = p;
1139 return -1;
1140 }
9307c4c1
FB
1141 if (*p == '\"') {
1142 p++;
1143 while (*p != '\0' && *p != '\"') {
1144 if (*p == '\\') {
1145 p++;
1146 c = *p++;
1147 switch(c) {
1148 case 'n':
1149 c = '\n';
1150 break;
1151 case 'r':
1152 c = '\r';
1153 break;
1154 case '\\':
1155 case '\'':
1156 case '\"':
1157 break;
1158 default:
1159 qemu_printf("unsupported escape code: '\\%c'\n", c);
1160 goto fail;
1161 }
1162 if ((q - buf) < buf_size - 1) {
1163 *q++ = c;
1164 }
1165 } else {
1166 if ((q - buf) < buf_size - 1) {
1167 *q++ = *p;
1168 }
1169 p++;
1170 }
1171 }
1172 if (*p != '\"') {
5b60212f 1173 qemu_printf("unterminated string\n");
9307c4c1
FB
1174 goto fail;
1175 }
1176 p++;
1177 } else {
1178 while (*p != '\0' && !isspace(*p)) {
1179 if ((q - buf) < buf_size - 1) {
1180 *q++ = *p;
1181 }
1182 p++;
1183 }
9307c4c1 1184 }
81d0912d 1185 *q = '\0';
9307c4c1
FB
1186 *pp = p;
1187 return 0;
1188}
1189
1190static int default_fmt_format = 'x';
1191static int default_fmt_size = 4;
1192
1193#define MAX_ARGS 16
1194
7e2515e8 1195static void monitor_handle_command(const char *cmdline)
9307c4c1
FB
1196{
1197 const char *p, *pstart, *typestr;
1198 char *q;
1199 int c, nb_args, len, i, has_arg;
9dc39cba 1200 term_cmd_t *cmd;
9307c4c1
FB
1201 char cmdname[256];
1202 char buf[1024];
1203 void *str_allocated[MAX_ARGS];
1204 void *args[MAX_ARGS];
9dc39cba
FB
1205
1206#ifdef DEBUG
1207 term_printf("command='%s'\n", cmdline);
1208#endif
1209
9307c4c1 1210 /* extract the command name */
9dc39cba 1211 p = cmdline;
9307c4c1
FB
1212 q = cmdname;
1213 while (isspace(*p))
1214 p++;
1215 if (*p == '\0')
1216 return;
1217 pstart = p;
1218 while (*p != '\0' && *p != '/' && !isspace(*p))
1219 p++;
1220 len = p - pstart;
1221 if (len > sizeof(cmdname) - 1)
1222 len = sizeof(cmdname) - 1;
1223 memcpy(cmdname, pstart, len);
1224 cmdname[len] = '\0';
1225
1226 /* find the command */
1227 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1228 if (compare_cmd(cmdname, cmd->name))
1229 goto found;
1230 }
1231 term_printf("unknown command: '%s'\n", cmdname);
1232 return;
1233 found:
1234
1235 for(i = 0; i < MAX_ARGS; i++)
1236 str_allocated[i] = NULL;
1237
1238 /* parse the parameters */
1239 typestr = cmd->args_type;
1240 nb_args = 0;
9dc39cba 1241 for(;;) {
9307c4c1
FB
1242 c = *typestr;
1243 if (c == '\0')
9dc39cba 1244 break;
9307c4c1
FB
1245 typestr++;
1246 switch(c) {
1247 case 'F':
81d0912d 1248 case 'B':
9307c4c1
FB
1249 case 's':
1250 {
1251 int ret;
1252 char *str;
1253
1254 while (isspace(*p))
1255 p++;
1256 if (*typestr == '?') {
1257 typestr++;
1258 if (*p == '\0') {
1259 /* no optional string: NULL argument */
1260 str = NULL;
1261 goto add_str;
1262 }
1263 }
1264 ret = get_str(buf, sizeof(buf), &p);
1265 if (ret < 0) {
81d0912d
FB
1266 switch(c) {
1267 case 'F':
9307c4c1 1268 term_printf("%s: filename expected\n", cmdname);
81d0912d
FB
1269 break;
1270 case 'B':
1271 term_printf("%s: block device name expected\n", cmdname);
1272 break;
1273 default:
9307c4c1 1274 term_printf("%s: string expected\n", cmdname);
81d0912d
FB
1275 break;
1276 }
9307c4c1
FB
1277 goto fail;
1278 }
1279 str = qemu_malloc(strlen(buf) + 1);
1280 strcpy(str, buf);
1281 str_allocated[nb_args] = str;
1282 add_str:
1283 if (nb_args >= MAX_ARGS) {
1284 error_args:
1285 term_printf("%s: too many arguments\n", cmdname);
1286 goto fail;
1287 }
1288 args[nb_args++] = str;
1289 }
9dc39cba 1290 break;
9307c4c1
FB
1291 case '/':
1292 {
1293 int count, format, size;
1294
1295 while (isspace(*p))
1296 p++;
1297 if (*p == '/') {
1298 /* format found */
1299 p++;
1300 count = 1;
1301 if (isdigit(*p)) {
1302 count = 0;
1303 while (isdigit(*p)) {
1304 count = count * 10 + (*p - '0');
1305 p++;
1306 }
1307 }
1308 size = -1;
1309 format = -1;
1310 for(;;) {
1311 switch(*p) {
1312 case 'o':
1313 case 'd':
1314 case 'u':
1315 case 'x':
1316 case 'i':
1317 case 'c':
1318 format = *p++;
1319 break;
1320 case 'b':
1321 size = 1;
1322 p++;
1323 break;
1324 case 'h':
1325 size = 2;
1326 p++;
1327 break;
1328 case 'w':
1329 size = 4;
1330 p++;
1331 break;
1332 case 'g':
1333 case 'L':
1334 size = 8;
1335 p++;
1336 break;
1337 default:
1338 goto next;
1339 }
1340 }
1341 next:
1342 if (*p != '\0' && !isspace(*p)) {
1343 term_printf("invalid char in format: '%c'\n", *p);
1344 goto fail;
1345 }
9307c4c1
FB
1346 if (format < 0)
1347 format = default_fmt_format;
4c27ba27
FB
1348 if (format != 'i') {
1349 /* for 'i', not specifying a size gives -1 as size */
1350 if (size < 0)
1351 size = default_fmt_size;
1352 }
9307c4c1
FB
1353 default_fmt_size = size;
1354 default_fmt_format = format;
1355 } else {
1356 count = 1;
1357 format = default_fmt_format;
4c27ba27
FB
1358 if (format != 'i') {
1359 size = default_fmt_size;
1360 } else {
1361 size = -1;
1362 }
9307c4c1
FB
1363 }
1364 if (nb_args + 3 > MAX_ARGS)
1365 goto error_args;
1366 args[nb_args++] = (void*)count;
1367 args[nb_args++] = (void*)format;
1368 args[nb_args++] = (void*)size;
1369 }
9dc39cba 1370 break;
9307c4c1
FB
1371 case 'i':
1372 {
1373 int val;
1374 while (isspace(*p))
1375 p++;
3440557b 1376 if (*typestr == '?' || *typestr == '.') {
9307c4c1 1377 typestr++;
3440557b
FB
1378 if (*typestr == '?') {
1379 if (*p == '\0')
1380 has_arg = 0;
1381 else
1382 has_arg = 1;
1383 } else {
1384 if (*p == '.') {
1385 p++;
1386 while (isspace(*p))
1387 p++;
1388 has_arg = 1;
1389 } else {
1390 has_arg = 0;
1391 }
1392 }
9307c4c1
FB
1393 if (nb_args >= MAX_ARGS)
1394 goto error_args;
1395 args[nb_args++] = (void *)has_arg;
1396 if (!has_arg) {
1397 if (nb_args >= MAX_ARGS)
1398 goto error_args;
1399 val = -1;
1400 goto add_num;
1401 }
1402 }
1403 if (get_expr(&val, &p))
1404 goto fail;
1405 add_num:
1406 if (nb_args >= MAX_ARGS)
1407 goto error_args;
1408 args[nb_args++] = (void *)val;
1409 }
1410 break;
1411 case '-':
1412 {
1413 int has_option;
1414 /* option */
1415
1416 c = *typestr++;
1417 if (c == '\0')
1418 goto bad_type;
1419 while (isspace(*p))
1420 p++;
1421 has_option = 0;
1422 if (*p == '-') {
1423 p++;
1424 if (*p != c) {
1425 term_printf("%s: unsupported option -%c\n",
1426 cmdname, *p);
1427 goto fail;
1428 }
1429 p++;
1430 has_option = 1;
1431 }
1432 if (nb_args >= MAX_ARGS)
1433 goto error_args;
1434 args[nb_args++] = (void *)has_option;
1435 }
1436 break;
1437 default:
1438 bad_type:
1439 term_printf("%s: unknown type '%c'\n", cmdname, c);
1440 goto fail;
1441 }
9dc39cba 1442 }
9307c4c1
FB
1443 /* check that all arguments were parsed */
1444 while (isspace(*p))
1445 p++;
1446 if (*p != '\0') {
1447 term_printf("%s: extraneous characters at the end of line\n",
1448 cmdname);
1449 goto fail;
9dc39cba 1450 }
9307c4c1
FB
1451
1452 switch(nb_args) {
1453 case 0:
1454 cmd->handler();
1455 break;
1456 case 1:
1457 cmd->handler(args[0]);
1458 break;
1459 case 2:
1460 cmd->handler(args[0], args[1]);
1461 break;
1462 case 3:
1463 cmd->handler(args[0], args[1], args[2]);
1464 break;
1465 case 4:
1466 cmd->handler(args[0], args[1], args[2], args[3]);
1467 break;
1468 case 5:
1469 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1470 break;
3440557b
FB
1471 case 6:
1472 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1473 break;
9307c4c1
FB
1474 default:
1475 term_printf("unsupported number of arguments: %d\n", nb_args);
1476 goto fail;
9dc39cba 1477 }
9307c4c1
FB
1478 fail:
1479 for(i = 0; i < MAX_ARGS; i++)
1480 qemu_free(str_allocated[i]);
9dc39cba 1481 return;
9dc39cba
FB
1482}
1483
81d0912d
FB
1484static void cmd_completion(const char *name, const char *list)
1485{
1486 const char *p, *pstart;
1487 char cmd[128];
1488 int len;
1489
1490 p = list;
1491 for(;;) {
1492 pstart = p;
1493 p = strchr(p, '|');
1494 if (!p)
1495 p = pstart + strlen(pstart);
1496 len = p - pstart;
1497 if (len > sizeof(cmd) - 2)
1498 len = sizeof(cmd) - 2;
1499 memcpy(cmd, pstart, len);
1500 cmd[len] = '\0';
1501 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1502 add_completion(cmd);
1503 }
1504 if (*p == '\0')
1505 break;
1506 p++;
1507 }
1508}
1509
1510static void file_completion(const char *input)
1511{
1512 DIR *ffs;
1513 struct dirent *d;
1514 char path[1024];
1515 char file[1024], file_prefix[1024];
1516 int input_path_len;
1517 const char *p;
1518
1519 p = strrchr(input, '/');
1520 if (!p) {
1521 input_path_len = 0;
1522 pstrcpy(file_prefix, sizeof(file_prefix), input);
1523 strcpy(path, ".");
1524 } else {
1525 input_path_len = p - input + 1;
1526 memcpy(path, input, input_path_len);
1527 if (input_path_len > sizeof(path) - 1)
1528 input_path_len = sizeof(path) - 1;
1529 path[input_path_len] = '\0';
1530 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1531 }
1532#ifdef DEBUG_COMPLETION
1533 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1534#endif
1535 ffs = opendir(path);
1536 if (!ffs)
1537 return;
1538 for(;;) {
1539 struct stat sb;
1540 d = readdir(ffs);
1541 if (!d)
1542 break;
1543 if (strstart(d->d_name, file_prefix, NULL)) {
1544 memcpy(file, input, input_path_len);
1545 strcpy(file + input_path_len, d->d_name);
1546 /* stat the file to find out if it's a directory.
1547 * In that case add a slash to speed up typing long paths
1548 */
1549 stat(file, &sb);
1550 if(S_ISDIR(sb.st_mode))
1551 strcat(file, "/");
1552 add_completion(file);
1553 }
1554 }
1555 closedir(ffs);
1556}
1557
1558static void block_completion_it(void *opaque, const char *name)
1559{
1560 const char *input = opaque;
1561
1562 if (input[0] == '\0' ||
1563 !strncmp(name, (char *)input, strlen(input))) {
1564 add_completion(name);
1565 }
1566}
1567
1568/* NOTE: this parser is an approximate form of the real command parser */
1569static void parse_cmdline(const char *cmdline,
1570 int *pnb_args, char **args)
1571{
1572 const char *p;
1573 int nb_args, ret;
1574 char buf[1024];
1575
1576 p = cmdline;
1577 nb_args = 0;
1578 for(;;) {
1579 while (isspace(*p))
1580 p++;
1581 if (*p == '\0')
1582 break;
1583 if (nb_args >= MAX_ARGS)
1584 break;
1585 ret = get_str(buf, sizeof(buf), &p);
1586 args[nb_args] = qemu_strdup(buf);
1587 nb_args++;
1588 if (ret < 0)
1589 break;
1590 }
1591 *pnb_args = nb_args;
1592}
1593
7e2515e8 1594void readline_find_completion(const char *cmdline)
81d0912d
FB
1595{
1596 const char *cmdname;
1597 char *args[MAX_ARGS];
1598 int nb_args, i, len;
1599 const char *ptype, *str;
1600 term_cmd_t *cmd;
1601
1602 parse_cmdline(cmdline, &nb_args, args);
1603#ifdef DEBUG_COMPLETION
1604 for(i = 0; i < nb_args; i++) {
1605 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1606 }
1607#endif
1608
1609 /* if the line ends with a space, it means we want to complete the
1610 next arg */
1611 len = strlen(cmdline);
1612 if (len > 0 && isspace(cmdline[len - 1])) {
1613 if (nb_args >= MAX_ARGS)
1614 return;
1615 args[nb_args++] = qemu_strdup("");
1616 }
1617 if (nb_args <= 1) {
1618 /* command completion */
1619 if (nb_args == 0)
1620 cmdname = "";
1621 else
1622 cmdname = args[0];
1623 completion_index = strlen(cmdname);
1624 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1625 cmd_completion(cmdname, cmd->name);
1626 }
1627 } else {
1628 /* find the command */
1629 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1630 if (compare_cmd(args[0], cmd->name))
1631 goto found;
1632 }
1633 return;
1634 found:
1635 ptype = cmd->args_type;
1636 for(i = 0; i < nb_args - 2; i++) {
1637 if (*ptype != '\0') {
1638 ptype++;
1639 while (*ptype == '?')
1640 ptype++;
1641 }
1642 }
1643 str = args[nb_args - 1];
1644 switch(*ptype) {
1645 case 'F':
1646 /* file completion */
1647 completion_index = strlen(str);
1648 file_completion(str);
1649 break;
1650 case 'B':
1651 /* block device name completion */
1652 completion_index = strlen(str);
1653 bdrv_iterate(block_completion_it, (void *)str);
1654 break;
1655 default:
1656 break;
1657 }
1658 }
1659 for(i = 0; i < nb_args; i++)
1660 qemu_free(args[i]);
1661}
1662
7e2515e8 1663static int term_can_read(void *opaque)
9dc39cba 1664{
7e2515e8 1665 return 128;
9dc39cba
FB
1666}
1667
7e2515e8 1668static void term_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 1669{
7e2515e8
FB
1670 int i;
1671 for(i = 0; i < size; i++)
1672 readline_handle_byte(buf[i]);
9dc39cba
FB
1673}
1674
7e2515e8 1675static void monitor_start_input(void);
9dc39cba 1676
7e2515e8 1677static void monitor_handle_command1(void *opaque, const char *cmdline)
aa455485 1678{
7e2515e8
FB
1679 monitor_handle_command(cmdline);
1680 monitor_start_input();
aa455485
FB
1681}
1682
7e2515e8 1683static void monitor_start_input(void)
aa455485 1684{
7e2515e8 1685 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
aa455485
FB
1686}
1687
7e2515e8 1688void monitor_init(CharDriverState *hd, int show_banner)
aa455485 1689{
7e2515e8
FB
1690 monitor_hd = hd;
1691 if (show_banner) {
1692 term_printf("QEMU %s monitor - type 'help' for more information\n",
1693 QEMU_VERSION);
aa455485 1694 }
7e2515e8
FB
1695 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
1696 monitor_start_input();
aa455485
FB
1697}
1698
7e2515e8
FB
1699/* XXX: use threads ? */
1700/* modal monitor readline */
1701static int monitor_readline_started;
1702static char *monitor_readline_buf;
1703static int monitor_readline_buf_size;
81d0912d 1704
7e2515e8 1705static void monitor_readline_cb(void *opaque, const char *input)
81d0912d 1706{
7e2515e8
FB
1707 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
1708 monitor_readline_started = 0;
81d0912d
FB
1709}
1710
7e2515e8
FB
1711void monitor_readline(const char *prompt, int is_password,
1712 char *buf, int buf_size)
9dc39cba 1713{
7e2515e8
FB
1714 if (is_password) {
1715 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
9dc39cba 1716 }
7e2515e8
FB
1717 readline_start(prompt, is_password, monitor_readline_cb, NULL);
1718 monitor_readline_buf = buf;
1719 monitor_readline_buf_size = buf_size;
1720 monitor_readline_started = 1;
1721 while (monitor_readline_started) {
1722 main_loop_wait(10);
9dc39cba 1723 }
9dc39cba 1724}