File: textconfig.cc

package info (click to toggle)
bochs 2.3-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,116 kB
  • ctags: 16,927
  • sloc: cpp: 130,524; ansic: 18,822; sh: 7,922; makefile: 3,836; yacc: 1,056; asm: 463; perl: 381; lex: 280; csh: 3
file content (1080 lines) | stat: -rw-r--r-- 33,254 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/////////////////////////////////////////////////////////////////////////
// $Id: textconfig.cc,v 1.61 2006/07/29 09:58:24 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
// This is code for a text-mode configuration interface.  Note that this file
// does NOT include bochs.h.  Instead, it does all of its contact with
// the simulator through an object called SIM, defined in siminterface.cc
// and siminterface.h.  This separation adds an extra layer of method
// calls before any work can be done, but the benefit is that the compiler
// enforces the rules.  I can guarantee that textconfig.cc doesn't call any
// I/O device objects directly, for example, because the bx_devices symbol
// isn't even defined in this context.
//

#include "config.h"

#if BX_USE_TEXTCONFIG

#ifndef __QNXNTO__
extern "C" {
#endif

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

#ifndef __QNXNTO__
}
#endif

#include "osdep.h"
#include "textconfig.h"
#include "siminterface.h"
#include "extplugin.h"
#ifdef WIN32
#include "win32dialog.h"
#endif

#define CI_PATH_LENGTH 512

#define BX_INSERTED 11

/* functions for changing particular options */
void bx_config_interface_init();
int bx_read_rc(char *rc);
int bx_write_rc(char *rc);
void bx_log_options(int individual);

/******************************************************************/
/* lots of code stolen from bximage.c */
/* remove leading spaces, newline junk at end.  returns pointer to 
 cleaned string, which is between s0 and the null */
char *
clean_string(char *s0)
{
  char *s = s0;
  char *ptr;
  /* find first nonblank */
  while (isspace(*s))
    s++;
  /* truncate string at first non-alphanumeric */
  ptr = s;
  while (isprint(*ptr))
    ptr++;
  *ptr = 0;
  return s;
}

/* returns 0 on success, -1 on failure.  The value goes into out. */
int 
ask_uint(const char *prompt, const char *help, Bit32u min, Bit32u max, Bit32u the_default, Bit32u *out, int base)
{
  Bit32u n = max + 1;
  char buffer[1024];
  char *clean;
  int illegal;
  assert(base==10 || base==16);
  while (1) {
    printf(prompt, the_default);
    if (!fgets(buffer, sizeof(buffer), stdin))
      return -1;
    clean = clean_string(buffer);
    if (strlen(clean) < 1) {
      // empty line, use the default
      *out = the_default;
      return 0;
    }
    if ((clean[0] == '?') && (strlen(help) > 0)) {
      printf("\n%s\n", help);
      if (base == 10) {
        printf("Your choice must be an integer between %u and %u.\n\n", min, max);
      } else {
        printf("Your choice must be an integer between 0x%x and 0x%x.\n\n", min, max);
      }
      continue;
    }
    const char *format = (base==10) ? "%d" : "%x";
    illegal = (1 != sscanf(buffer, format, &n));
    if (illegal || n<min || n>max) {
      if (base == 10) {
        printf("Your choice (%s) was not an integer between %u and %u.\n\n",
               clean, min, max);
      } else {
        printf("Your choice (%s) was not an integer between 0x%x and 0x%x.\n\n",
               clean, min, max);
      }
    } else {
      // choice is okay
      *out = n;
      return 0;
    }
  }
}

// identical to ask_uint, but uses signed comparisons
int 
ask_int(const char *prompt, const char *help, Bit32s min, Bit32s max, Bit32s the_default, Bit32s *out)
{
  int n = max + 1;
  char buffer[1024];
  char *clean;
  int illegal;
  while (1) {
    printf(prompt, the_default);
    if (!fgets(buffer, sizeof(buffer), stdin))
      return -1;
    clean = clean_string(buffer);
    if (strlen(clean) < 1) {
      // empty line, use the default
      *out = the_default;
      return 0;
    }
    if ((clean[0] == '?') && (strlen(help) > 0)) {
      printf("\n%s\n", help);
      printf("Your choice must be an integer between %u and %u.\n\n", min, max);
      continue;
    }
    illegal = (1 != sscanf(buffer, "%d", &n));
    if (illegal || n<min || n>max) {
      printf("Your choice (%s) was not an integer between %d and %d.\n\n",
	  clean, min, max);
    } else {
      // choice is okay
      *out = n;
      return 0;
    }
  }
}

int 
ask_menu(const char *prompt, const char *help, int n_choices, char *choice[], int the_default, int *out)
{
  char buffer[1024];
  char *clean;
  int i;
  *out = -1;
  while (1) {
    printf(prompt, choice[the_default]);
    if (!fgets(buffer, sizeof(buffer), stdin))
      return -1;
    clean = clean_string(buffer);
    if (strlen(clean) < 1) {
      // empty line, use the default
      *out = the_default;
      return 0;
    }
    for (i=0; i<n_choices; i++) {
      if (!strcmp(choice[i], clean)) {
	// matched, return the choice number
	*out = i;
	return 0;
      }
    }
    if (clean[0] != '?') {
      printf("Your choice (%s) did not match any of the choices:\n", clean);
    } else if (strlen(help) > 0) {
      printf("\n%s\nValid values are: ", help);
    }
    for (i=0; i<n_choices; i++) {
      if (i>0) printf(", ");
      printf("%s", choice[i]);
    }
    printf("\n");
  }
}

int 
ask_yn(const char *prompt, const char *help, Bit32u the_default, Bit32u *out)
{
  char buffer[16];
  char *clean;
  *out = 1<<31;
  while (1) {
    // if there's a %s field, substitute in the default yes/no.
    printf(prompt, the_default ? "yes" : "no");
    if (!fgets(buffer, sizeof(buffer), stdin))
      return -1;
    clean = clean_string(buffer);
    if (strlen(clean) < 1) {
      // empty line, use the default
      *out = the_default;
      return 0;
    }
    switch (tolower(clean[0])) {
      case 'y': *out=1; return 0;
      case 'n': *out=0; return 0;
      case '?':
        if (strlen(help) > 0) {
          printf("\n%s\n", help);
        }
        break;
    }
    printf("Please type either yes or no.\n");
  }
}

// returns -1 on error (stream closed or  something)
// returns 0 if default was taken
// returns 1 if value changed
// returns -2 if help requested
int ask_string(const char *prompt, const char *the_default, char *out)
{
  char buffer[1024];
  char *clean;
  assert(the_default != out);
  out[0] = 0;
  printf(prompt, the_default);
  if (fgets(buffer, sizeof(buffer), stdin) == NULL)
    return -1;
  clean = clean_string(buffer);
  if (clean[0] == '?')
    return -2;
  if (strlen(clean) < 1) {
    // empty line, use the default
    strcpy(out, the_default);
    return 0;
  }
  strcpy(out, clean);
  return 1;
}

/******************************************************************/

static char *startup_menu_prompt =
"------------------------------\n"
"Bochs Configuration: Main Menu\n"
"------------------------------\n"
"\n"
"This is the Bochs Configuration Interface, where you can describe the\n"
"machine that you want to simulate.  Bochs has already searched for a\n"
"configuration file (typically called bochsrc.txt) and loaded it if it\n"
"could be found.  When you are satisfied with the configuration, go\n"
"ahead and start the simulation.\n"
"\n"
"You can also start bochs with the -q option to skip these menus.\n"
"\n"
"1. Restore factory default configuration\n"
"2. Read options from...\n"
"3. Edit options\n"
"4. Save options to...\n"
#if BX_SUPPORT_SAVE_RESTORE
"5. Restore the Bochs state from...\n"
"6. Begin simulation\n"
"7. Quit now\n"
#else
"5. Begin simulation\n"
"6. Quit now\n"
#endif
"\n"
"Please choose one: [%d] ";

static char *startup_options_prompt =
"------------------\n"
"Bochs Options Menu\n"
"------------------\n"
"0. Return to previous menu\n"
"1. Logfile options\n"
"2. Log options for all devices\n"
"3. Log options for individual devices\n"
"4. CPU options\n"
"5. Memory options\n"
"6. Clock & CMOS options\n"
"7. PCI options\n"
"8. Bochs Display & Interface options\n"
"9. Keyboard & Mouse options\n"
"10. Disk options\n"
"11. Serial & Parallel port options\n"
"12. Network card options\n"
"13. Sound Blaster 16 options\n"
"14. Other options\n"
"\n"
"Please choose one: [0] ";

#ifndef WIN32
static char *runtime_menu_prompt =
"---------------------\n"
"Bochs Runtime Options\n"
"---------------------\n"
"1. Floppy disk 0: %s\n"
"2. Floppy disk 1: %s\n"
"3. 1st CDROM: %s\n"
"4. 2nd CDROM: %s\n"
"5. 3rd CDROM: %s\n"
"6. 4th CDROM: %s\n"
"7. (not implemented)\n"
"8. Log options for all devices\n"
"9. Log options for individual devices\n"
"10. Instruction tracing: off (doesn't exist yet)\n"
"11. Misc runtime options\n"
"12. Continue simulation\n"
"13. Quit now\n"
"\n"
"Please choose one:  [12] ";

#if BX_SUPPORT_SAVE_RESTORE
static char *save_state_prompt =
"----------------\n"
"Save Bochs State\n"
"----------------\n\n"
"What is the path to save the Bochs state to?\n"
"To cancel, type 'none'. [%s] ";
#endif
#endif

#define NOT_IMPLEMENTED(choice) \
  fprintf(stderr, "ERROR: choice %d not implemented\n", choice);

#define BAD_OPTION(menu,choice) \
  do {fprintf(stderr, "ERROR: menu %d has no choice %d\n", menu, choice); \
      assert(0); } while (0)

#ifndef WIN32
void build_runtime_options_prompt(char *format, char *buf, int size)
{
  bx_list_c *floppyop;
  bx_list_c *cdromop = NULL;
  char pname[80];
  char buffer[6][128];

  for (int i=0; i<2; i++) {
    sprintf(pname, "floppy.%d", i);
    floppyop = (bx_list_c*) SIM->get_param(pname);
    if (SIM->get_param_enum("devtype", floppyop)->get() == BX_FLOPPY_NONE)
      strcpy(buffer[i], "(not present)");
    else {
      sprintf(buffer[i], "%s, size=%s, %s", SIM->get_param_string("path", floppyop)->getptr(),
        SIM->get_param_enum("type", floppyop)->get_selected(),
        SIM->get_param_enum("status", floppyop)->get_selected());
      if (!SIM->get_param_string("path", floppyop)->getptr()[0]) strcpy(buffer[i], "none");
    }
  }

  // 4 cdroms supported at run time
  int device;
  for (Bit8u cdrom=0; cdrom<4; cdrom++) {
    if (!SIM->get_cdrom_options(cdrom, &cdromop, &device) || !SIM->get_param_bool("present", cdromop)->get())
      sprintf(buffer[2+cdrom], "(not present)");
    else
      sprintf(buffer[2+cdrom], "(%s on ata%d) %s, %s",
        device&1?"slave":"master", device/2, SIM->get_param_string("path", cdromop)->getptr(),
        (SIM->get_param_enum("status", cdromop)->get() == BX_INSERTED)? "inserted" : "ejected");
    }

  snprintf(buf, size, format, buffer[0], buffer[1], buffer[2],
           buffer[3], buffer[4], buffer[5]);
}
#endif

int do_menu(const char *pname)
{
  bx_list_c *menu = (bx_list_c *)SIM->get_param(pname, NULL);
  while (1) {
    menu->get_choice()->set(0);
    int status = menu->text_ask(stdin, stderr);
    if (status < 0) return status;
    bx_param_num_c *choice = menu->get_choice();
    if (choice->get() < 1)
      return choice->get();
    else {
      int index = choice->get() - 1;  // choosing 1 means list[0]
      bx_param_c *chosen = menu->get(index);
      assert(chosen != NULL);
      if (chosen->get_enabled()) {
        if (chosen->get_type() == BXT_LIST) {
          char chosen_pname[80];
          chosen->get_param_path(chosen_pname, 80);
          do_menu(chosen_pname);
        } else {
          chosen->text_ask(stdin, stderr);
        }
      }
    }
  }
}

void askparam(char *pname)
{
  bx_param_c *param = SIM->get_param(pname);
  param->text_ask(stdin, stderr);
}

int bx_config_interface(int menu)
{
  Bit32u choice;
#if BX_SUPPORT_SAVE_RESTORE
  char sr_path[CI_PATH_LENGTH];
#endif
  while (1) {
    switch (menu) {
      case BX_CI_INIT:
        bx_config_interface_init();
        return 0;
      case BX_CI_START_SIMULATION:
        SIM->begin_simulation(bx_startup_flags.argc, bx_startup_flags.argv);
        // we don't expect it to return, but if it does, quit
        SIM->quit_sim(1);
        break;
      case BX_CI_START_MENU:
        {
          Bit32u default_choice;
          switch (SIM->get_param_enum(BXPN_BOCHS_START)->get()) {
            case BX_LOAD_START: 
              default_choice = 2; break;
            case BX_EDIT_START: 
              default_choice = 3; break;
            default: 
#if BX_SUPPORT_SAVE_RESTORE
              default_choice = 6; break;
#else
              default_choice = 5; break;
#endif
          }
#if BX_SUPPORT_SAVE_RESTORE
          if (ask_uint(startup_menu_prompt, "", 1, 7, default_choice, &choice, 10) < 0) return -1;
#else
          if (ask_uint(startup_menu_prompt, "", 1, 6, default_choice, &choice, 10) < 0) return -1;
#endif
          switch (choice) {
            case 1:
              fprintf(stderr, "I reset all options back to their factory defaults.\n\n");
              SIM->reset_all_param();
              SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_EDIT_START);
              break;
            case 2: 
              // Before reading a new configuration, reset every option to its
              // original state.
              SIM->reset_all_param();
              if (bx_read_rc(NULL) >= 0)
                SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_RUN_START);
              break;
            case 3: 
              bx_config_interface(BX_CI_START_OPTS); 
              SIM->get_param_enum(BXPN_BOCHS_START)->set(BX_RUN_START);
              break;
            case 4: bx_write_rc(NULL); break;
#if BX_SUPPORT_SAVE_RESTORE
            case 5:
              if (ask_string("\nWhat is the path to restore the Bochs state from?\nTo cancel, type 'none'. [%s] ", "none", sr_path) >= 0) {
                if (strcmp(sr_path, "none")) {
                  SIM->get_param_bool(BXPN_RESTORE_FLAG)->set(1);
                  SIM->get_param_string(BXPN_RESTORE_PATH)->set(sr_path);
                  bx_config_interface(BX_CI_START_SIMULATION);
                }
              }
              break;
            case 6: bx_config_interface(BX_CI_START_SIMULATION); break;
            case 7: SIM->quit_sim(1); return -1;
#else
            case 5: bx_config_interface(BX_CI_START_SIMULATION); break;
            case 6: SIM->quit_sim(1); return -1;
#endif
            default: BAD_OPTION(menu, choice);
          }
        }
        break;
      case BX_CI_START_OPTS:
        if (ask_uint(startup_options_prompt, "", 0, 14, 0, &choice, 10) < 0) return -1;
        switch (choice) {
          case 0: return 0;
          case 1: do_menu("log"); break;
          case 2: bx_log_options(0); break;
          case 3: bx_log_options(1); break;
          case 4: do_menu("cpu"); break;
          case 5: do_menu(BXPN_MENU_MEMORY); break;
          case 6: do_menu("clock_cmos"); break;
          case 7: do_menu("pci"); break;
          case 8: do_menu("display"); break;
          case 9: do_menu("keyboard_mouse"); break;
          case 10: do_menu(BXPN_MENU_DISK); break;
          case 11: do_menu("ports"); break;
          case 12: do_menu("network"); break;
          case 13: do_menu(BXPN_SB16); break;
          case 14: do_menu("misc"); break;
          default: BAD_OPTION(menu, choice);
        }
        break;
      case BX_CI_RUNTIME:
        {
          bx_list_c *cdromop = NULL;
          char pname[80];
#ifdef WIN32
          choice = RuntimeOptionsDialog();
#else
          char prompt[1024];
          build_runtime_options_prompt(runtime_menu_prompt, prompt, 1024);
          if (ask_uint(prompt, "", 1, BX_CI_RT_QUIT, BX_CI_RT_CONT, &choice, 10) < 0) return -1;
#endif
          switch (choice) {
            case BX_CI_RT_FLOPPYA: 
              if (SIM->get_param_enum(BXPN_FLOPPYA_DEVTYPE)->get() != BX_FLOPPY_NONE) do_menu(BXPN_FLOPPYA);
              break;
            case BX_CI_RT_FLOPPYB:
              if (SIM->get_param_enum(BXPN_FLOPPYB_DEVTYPE)->get() != BX_FLOPPY_NONE) do_menu(BXPN_FLOPPYB);
              break;
            case BX_CI_RT_CDROM1:
            case BX_CI_RT_CDROM2:
            case BX_CI_RT_CDROM3:
            case BX_CI_RT_CDROM4:
              int device;
              if (SIM->get_cdrom_options(choice - BX_CI_RT_CDROM1, &cdromop, &device) && SIM->get_param_bool("present", cdromop)->get()) {
                // disable type selection
                SIM->get_param("type", cdromop)->set_enabled(0);
                SIM->get_param("model", cdromop)->set_enabled(0);
                SIM->get_param("biosdetect", cdromop)->set_enabled(0);
                cdromop->get_param_path(pname, 80);
                do_menu(pname);
              }
              break;
            case BX_CI_RT_IPS:
              // not implemented yet because I would have to mess with
              // resetting timers and pits and everything on the fly.
              // askparam(BXPN_IPS);
              break;
            case BX_CI_RT_LOGOPTS1: bx_log_options(0); break;
            case BX_CI_RT_LOGOPTS2: bx_log_options(1); break;
            case BX_CI_RT_INST_TR: NOT_IMPLEMENTED(choice); break;
            case BX_CI_RT_MISC: do_menu(BXPN_MENU_RUNTIME); break;
            case BX_CI_RT_CONT: fprintf(stderr, "Continuing simulation\n"); return 0;
            case BX_CI_RT_QUIT:
              fprintf(stderr, "You chose quit on the configuration interface.\n");
              bx_user_quit = 1;
              SIM->quit_sim(1);
              return -1;
            default: fprintf(stderr, "Menu choice %d not implemented.\n", choice);
          }
        }
        break;
#if BX_SUPPORT_SAVE_RESTORE
      case BX_CI_SAVE_RESTORE:
        {
          Bit32u cont = 1;
#ifdef WIN32
          cont = win32SaveState();
#else
          if (ask_string(save_state_prompt, "none", sr_path) >= 0) {
            if (strcmp(sr_path, "none")) {
              if (SIM->save_state(sr_path)) {
                cont = 0;
                ask_yn("\nThe save function currently doesn't handle the state of hard drive images,\n"
                       "so we don't recommend to continue, unless you are running a read-only\n"
                       "guest system (e.g. Live-CD).\n\n"
                       "Do you want to continue? [no]", "", 0, &cont);
              }
            }
          }
#endif
          if (!cont) {
            bx_user_quit = 1;
            SIM->quit_sim(1);
            return -1;
          } else {
            return 0;
          }
        }
#endif
      default:
        fprintf(stderr, "Unknown config interface menu type.\n");
        assert(menu >=0 && menu < BX_CI_N_MENUS);
    }
  }
}

static void bx_print_log_action_table()
{
  // just try to print all the prefixes first.
  fprintf(stderr, "Current log settings:\n");
  fprintf(stderr, "                 Debug      Info       Error       Panic       Pass\n");
  fprintf(stderr, "ID    Device     Action     Action     Action      Action      Action\n");
  fprintf(stderr, "----  ---------  ---------  ---------  ----------  ----------  ----------\n");
  int i, j, imax=SIM->get_n_log_modules();
  for (i=0; i<imax; i++) {
    if (strcmp(SIM->get_prefix(i), "[     ]")) {
      fprintf(stderr, "%3d.  %s ", i, SIM->get_prefix(i));
      for (j=0; j<SIM->get_max_log_level(); j++) {
        fprintf(stderr, "%10s ", SIM->get_action_name(SIM->get_log_action(i, j)));
      }
      fprintf(stderr, "\n");
    }
  }
}

static char *log_options_prompt1 = "Enter the ID of the device to edit, or -1 to return: [-1] ";
static char *log_level_choices[] = { "ignore", "report", "ask", "fatal", "no change" };
static int log_level_n_choices_normal = 4;

void bx_log_options(int individual)
{
  if (individual) {
    int done = 0;
    while (!done) {
      bx_print_log_action_table();
      Bit32s id, level, action;
      Bit32s maxid = SIM->get_n_log_modules();
      if (ask_int(log_options_prompt1, "", -1, maxid-1, -1, &id) < 0)
	return;
      if (id < 0) return;
      fprintf(stderr, "Editing log options for the device %s\n", SIM->get_prefix(id));
      for (level=0; level<SIM->get_max_log_level(); level++) {
	char prompt[1024];
	int default_action = SIM->get_log_action(id, level);
	sprintf(prompt, "Enter action for %s event: [%s] ", SIM->get_log_level_name(level), SIM->get_action_name(default_action));
	// don't show the no change choice (choices=3)
	if (ask_menu(prompt, "", log_level_n_choices_normal, log_level_choices, default_action, &action)<0)
	  return;
	SIM->set_log_action(id, level, action);
      }
    }
  } else {
    // provide an easy way to set log options for all devices at once
    bx_print_log_action_table();
    for (int level=0; level<SIM->get_max_log_level(); level++) {
      char prompt[1024];
      int action, default_action = 3;  // default to no change
      sprintf(prompt, "Enter action for %s event on all devices: [no change] ", SIM->get_log_level_name(level));
	// do show the no change choice (choices=4)
      if (ask_menu(prompt, "", log_level_n_choices_normal+1, log_level_choices, default_action, &action)<0)
	return;
      if (action < 3) {
	SIM->set_default_log_action(level, action);
	SIM->set_log_action(-1, level, action);
      }
    }
  }
}

int bx_read_rc(char *rc)
{
  if (rc && SIM->read_rc(rc) >= 0) return 0;
  char oldrc[CI_PATH_LENGTH];
  if (SIM->get_default_rc(oldrc, CI_PATH_LENGTH) < 0)
    strcpy(oldrc, "none");
  char newrc[CI_PATH_LENGTH];
  while (1) {
    if (ask_string("\nWhat is the configuration file name?\nTo cancel, type 'none'. [%s] ", oldrc, newrc) < 0) return -1;
    if (!strcmp(newrc, "none")) return -1;
    if (SIM->read_rc(newrc) >= 0) return 0;
    fprintf(stderr, "The file '%s' could not be found.\n", newrc);
  }
}

int bx_write_rc(char *rc)
{
  char oldrc[CI_PATH_LENGTH], newrc[CI_PATH_LENGTH];
  if (rc == NULL) {
    if (SIM->get_default_rc(oldrc, CI_PATH_LENGTH) < 0)
      strcpy(oldrc, "none");
  } else {
    strncpy(oldrc, rc, CI_PATH_LENGTH);
  }
  while (1) {
    if (ask_string("Save configuration to what file?  To cancel, type 'none'.\n[%s] ", oldrc, newrc) < 0) return -1;
    if (!strcmp(newrc, "none")) return 0;
    // try with overwrite off first
    int status = SIM->write_rc(newrc, 0);
    if (status >= 0) {
      fprintf(stderr, "Wrote configuration to '%s'.\n", newrc);
      return 0;
    } else if (status == -2) {
      // return code -2 indicates the file already exists, and overwrite
      // confirmation is required.
      Bit32u overwrite = 0;
      char prompt[256];
      sprintf(prompt, "Configuration file '%s' already exists.  Overwrite it? [no] ", newrc);
      if (ask_yn(prompt, "", 0, &overwrite) < 0) return -1;
      if (!overwrite) continue;  // if "no", start loop over, asking for a different file
      // they confirmed, so try again with overwrite bit set
      if (SIM->write_rc(newrc, 1) >= 0) {
	fprintf(stderr, "Overwriting existing configuration '%s'.\n", newrc);
	return 0;
      } else {
	fprintf(stderr, "Write failed to '%s'.\n", newrc);
      }
    }
  }
}

char *log_action_ask_choices[] = { "cont", "alwayscont", "die", "abort", "debug" };
int log_action_n_choices = 4 + (BX_DEBUGGER||BX_GDBSTUB?1:0);

BxEvent *
config_interface_notify_callback(void *unused, BxEvent *event)
{
  event->retcode = -1;
  switch (event->type)
  {
    case BX_SYNC_EVT_TICK:
      event->retcode = 0;
      return event;
    case BX_SYNC_EVT_ASK_PARAM:
      event->retcode = event->u.param.param->text_ask(stdin, stderr);
      return event;
    case BX_SYNC_EVT_LOG_ASK:
    {
      int level = event->u.logmsg.level;
      fprintf(stderr, "========================================================================\n");
      fprintf(stderr, "Event type: %s\n", SIM->get_log_level_name (level));
      fprintf(stderr, "Device: %s\n", event->u.logmsg.prefix);
      fprintf(stderr, "Message: %s\n\n", event->u.logmsg.msg);
      fprintf(stderr, "A %s has occurred.  Do you want to:\n", SIM->get_log_level_name (level));
      fprintf(stderr, "  cont       - continue execution\n");
      fprintf(stderr, "  alwayscont - continue execution, and don't ask again.\n");
      fprintf(stderr, "               This affects only %s events from device %s\n", SIM->get_log_level_name (level), event->u.logmsg.prefix);
      fprintf(stderr, "  die        - stop execution now\n");
      fprintf(stderr, "  abort      - dump core %s\n", 
	  BX_HAVE_ABORT ? "" : "(Disabled)");
#if BX_DEBUGGER
      fprintf(stderr, "  debug      - continue and return to bochs debugger\n");
#endif
#if BX_GDBSTUB
      fprintf(stderr, "  debug      - hand control to gdb\n");
#endif

      int choice;
ask:
      if (ask_menu("Choose one of the actions above: [%s] ", "",
                   log_action_n_choices, log_action_ask_choices, 2, &choice) < 0) 
	event->retcode = -1;
      // return 0 for continue, 1 for alwayscontinue, 2 for die, 3 for debug.
      if (!BX_HAVE_ABORT && choice==BX_LOG_ASK_CHOICE_DUMP_CORE) goto ask;
      fflush(stdout);
      fflush(stderr);
      event->retcode = choice;
    }
    return event;
  case BX_ASYNC_EVT_REFRESH:
  case BX_ASYNC_EVT_DBG_MSG:
    // The text mode interface does not use these events, so just ignore
    // them.
    return event;
  default:
    fprintf(stderr, "Control panel: notify callback called with event type %04x\n", event->type);
    return event;
  }
  assert(0); // switch statement should return
}

void bx_config_interface_init() {
  SIM->set_notify_callback(config_interface_notify_callback, NULL);
}

/////////////////////////////////////////////////////////////////////
// implement the text_* methods for bx_param types.

void
bx_param_num_c::text_print(FILE *fp)
{
  if (get_long_format()) {
    fprintf(fp, get_long_format(), get());
  } else {
    char *format = "%s: %d"; 
    assert(base==10 || base==16);
    if (base==16) format = "%s: 0x%x";
    if (get_label()) {
      fprintf(fp, format, get_label(), get());
    } else {
      fprintf(fp, format, get_name(), get());
    }
  }
}

void
bx_param_bool_c::text_print(FILE *fp)
{
  if (get_format()) {
    fprintf(fp, get_format(), get() ? "yes" : "no");
  } else {
    char *format = "%s: %s"; 
    if (get_label()) {
      fprintf(fp, format, get_label(), get() ? "yes" : "no");
    } else {
      fprintf(fp, format, get_name(), get() ? "yes" : "no");
    }
  }
}

void
bx_param_enum_c::text_print(FILE *fp)
{
  int n = get();
  assert(n >= min && n <= max);
  char *choice = choices[n - min];
  if (get_format()) {
    fprintf(fp, get_format(), choice);
  } else {
    char *format = "%s: %s"; 
    if (get_label()) {
      fprintf(fp, format, get_label(), choice);
    } else {
      fprintf(fp, format, get_name(), choice);
    }
  }
}

void
bx_param_string_c::text_print(FILE *fp)
{
  char *value = getptr();
  int opts = options->get();
  if (opts & RAW_BYTES) {
    char buffer[1024];
    buffer[0] = 0;
    char sep_string[2];
    sep_string[0] = separator;
    sep_string[1] = 0;
    for (int i=0; i<maxsize; i++) {
      char eachbyte[16];
      sprintf(eachbyte, "%s%02x", (i>0)?sep_string : "", (unsigned int)0xff&val[i]);
      strncat(buffer, eachbyte, sizeof(buffer));
    }
    if (strlen(buffer) > sizeof(buffer)-4) {
      assert(0); // raw byte print buffer is probably overflowing. increase the max or make it dynamic
    }
    value = buffer;
  }
  if (get_format()) {
    fprintf(fp, get_format(), value);
  } else {
    if (get_label()) {
      fprintf(fp, "%s: %s", get_label(), value);
    } else {
      fprintf(fp, "%s: %s", get_name(), value);
    }
  }
}

void
bx_list_c::text_print(FILE *fp)
{
  fprintf(fp, "%s: ", get_name());
  for (int i=0; i<size; i++) {
    assert(list[i] != NULL);
    if (list[i]->get_enabled()) {
      if ((i>0) && (options->get() & SERIES_ASK))
        fprintf(fp, ", ");
      list[i]->text_print(fp);
      if (!(options->get() & SERIES_ASK))
        fprintf(fp, "\n");
    }
  }
}

int 
bx_param_num_c::text_ask(FILE *fpin, FILE *fpout)
{
  fprintf(fpout, "\n");
  int status;
  const char *prompt = get_ask_format();
  const char *help = get_description();
  if (prompt == NULL) {
    // default prompt, if they didn't set an ask format string
    text_print(fpout);
    fprintf(fpout, "\n");
    prompt = "Enter new value or '?' for help: [%d] ";
    if (base==16)
      prompt = "Enter new value in hex or '?' for help: [%x] ";
  }
  Bit32u n = get();
  status = ask_uint(prompt, help, (Bit32u)min, (Bit32u)max, n, &n, base);
  if (status < 0) return status;
  set(n);
  return 0;
}

int 
bx_param_bool_c::text_ask(FILE *fpin, FILE *fpout)
{
  fprintf(fpout, "\n");
  int status;
  const char *prompt = get_ask_format();
  const char *help = get_description();
  char buffer[512];
  if (prompt == NULL) {
    if (get_label() != NULL) {
      sprintf(buffer, "%s? [%%s] ", get_label());
      prompt = buffer;
    } else {
      // default prompt, if they didn't set an ask format or label string
      sprintf(buffer, "%s? [%%s] ", get_name());
      prompt = buffer;
    }
  }
  Bit32u n = get();
  status = ask_yn(prompt, help, n, &n);
  if (status < 0) return status;
  set(n);
  return 0;
}

int 
bx_param_enum_c::text_ask(FILE *fpin, FILE *fpout)
{
  fprintf(fpout, "\n");
  const char *prompt = get_ask_format();
  const char *help = get_description();
  if (prompt == NULL) {
    // default prompt, if they didn't set an ask format string
    fprintf(fpout, "%s = ", get_name());
    text_print(fpout);
    fprintf(fpout, "\n");
    prompt = "Enter new value or '?' for help: [%s] ";
  }
  Bit32s n = (Bit32s)(get() - min);
  int status = ask_menu(prompt, help, (Bit32u)(max-min+1), choices, n, &n);
  if (status < 0) return status;
  n += (Bit32s)min;
  set(n);
  return 0;
}

int parse_raw_bytes(char *dest, char *src, int destsize, char separator)
{
  int i;
  unsigned int n;
  for (i=0; i<destsize; i++) 
    dest[i] = 0;
  for (i=0; i<destsize; i++) {
    while (*src == separator)
      src++;
    if (*src == 0) break;
    // try to read a byte of hex
    if (sscanf(src, "%02x", &n) == 1) {
      dest[i] = n;
      src+=2;
    } else {
      return -1;
    }
  }
  return 0;
}

int 
bx_param_string_c::text_ask(FILE *fpin, FILE *fpout)
{
  fprintf(fpout, "\n");
  int status;
  const char *prompt = get_ask_format();
  if (prompt == NULL) {
    // default prompt, if they didn't set an ask format string
    text_print(fpout);
    fprintf(fpout, "\n");
    prompt = "Enter a new value, '?' for help, or press return for no change.\n";
  }
  while (1) {
    char buffer[1024];
    status = ask_string(prompt, getptr(), buffer);
    if (status == -2) {
      fprintf(fpout, "\n%s\n", get_description());
      continue;
    }
    if (status < 0) return status;
    int opts = options->get();
    char buffer2[1024];
    strcpy(buffer2, buffer);
    if (status == 1 && opts & RAW_BYTES) {
      // copy raw hex into buffer
      status = parse_raw_bytes(buffer, buffer2, maxsize, separator);
      if (status < 0) {
	fprintf(fpout, "Illegal raw byte format.  I expected something like 3A%c03%c12%c...\n", separator, separator, separator);
	continue;
      }
    }
    if (!equals(buffer)) 
      set(buffer);
    return 0;
  }
}

int bx_list_c::text_ask(FILE *fpin, FILE *fpout)
{
  bx_list_c *child;

  char *my_title = title->getptr();
  fprintf(fpout, "\n");
  int i, imax = strlen(my_title);
  for (i=0; i<imax; i++) fprintf(fpout, "-");
  fprintf(fpout, "\n%s\n", my_title);
  for (i=0; i<imax; i++) fprintf(fpout, "-");
  fprintf(fpout, "\n");
  if (options->get() & SERIES_ASK) {
    for (int i=0; i<size; i++) {
      if (list[i]->get_enabled()) {
        if (!SIM->get_init_done() || list[i]->get_runtime_param()) {
          list[i]->text_ask(fpin, fpout);
        }
      }
    }
  } else {
    if (options->get() & SHOW_PARENT)
      fprintf(fpout, "0. Return to previous menu\n");
    for (int i=0; i<size; i++) {
      assert(list[i] != NULL);
      fprintf(fpout, "%d. ", i+1);
      if (list[i]->get_enabled()) {
        if (list[i]->get_type() == BXT_LIST) {
          child = (bx_list_c*)list[i];
          fprintf(fpout, "%s\n", child->get_title()->getptr());
        } else {
          if ((options->get() & SHOW_GROUP_NAME) && (list[i]->get_group() != NULL))
            fprintf(fpout, "%s ", list[i]->get_group());
          list[i]->text_print(fpout);
          fprintf(fpout, "\n");
        }
      } else {
        if (list[i]->get_type() == BXT_LIST) {
          child = (bx_list_c*)list[i];
          fprintf(fpout, "%s (disabled)\n", child->get_title()->getptr());
        } else {
          fprintf(fpout, "(disabled)\n");
        }
      }
    }
    fprintf(fpout, "\n");
    Bit32u n = choice->get();
    int min = (options->get() & SHOW_PARENT) ? 0 : 1;
    int max = size;
    int status = ask_uint("Please choose one: [%d] ", "", min, max, n, &n, 10);
    if (status < 0) return status;
    choice->set(n);
  }
  return 0;
}

static int ci_callback(void *userdata, ci_command_t command)
{
  switch (command)
  {
    case CI_START:
      bx_config_interface_init();
      if (SIM->get_param_enum(BXPN_BOCHS_START)->get() == BX_QUICK_START)
	bx_config_interface(BX_CI_START_SIMULATION);
      else {
        if (!SIM->test_for_text_console())
	  return CI_ERR_NO_TEXT_CONSOLE;
        bx_config_interface(BX_CI_START_MENU);
      }
      break;
    case CI_RUNTIME_CONFIG:
      bx_config_interface(BX_CI_RUNTIME);
      break;
    case CI_SAVE_RESTORE:
#if BX_SUPPORT_SAVE_RESTORE
      bx_config_interface(BX_CI_SAVE_RESTORE);
#endif
      break;
    case CI_SHUTDOWN:
      break;
  }
  return 0;
}

// if I can make things compile without this module linked in, then
// this file can become a plugin too.
int init_text_config_interface()
{
  SIM->register_configuration_interface("textconfig", ci_callback, NULL);
  return 0;  // success
}

#endif