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