summaryrefslogtreecommitdiffstats
path: root/private/sdktools/damage/cmd.c
blob: 459afb4ec8a94351654aa63f236b841f21cc7594 (plain) (blame)
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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
/*****************************************************************/ 
/**		     Microsoft LAN Manager			**/ 
/**	       Copyright(c) Microsoft Corp., 1988-1990		**/ 
/*****************************************************************/ 
/***	CMD.C - Routines to get and process commands.
 *
 *	DAMAGE
 *	Gregory A. Jones
 *
 *	Modification history:
 *	G.A. Jones	09/07/88	Original for Pinball testing.
 *	G.A. Jones	09/08/88	Coded initial commands.
 *	G.A. Jones	09/09/88	Coded D, N, P, B cmds.
 *	G.A. Jones	09/09/88	Moved from DISPLAY.C.
 *	G.A. Jones	09/12/88	Added support for DIRBLKs.
 *	G.A. Jones	09/13/88	Added bitmap displays.
 *	G.A. Jones	09/13/88	Added data and pathname support.
 *	G.A. Jones	09/14/88	Added Help command.
 *	G.A. Jones	09/19/88	Removed DIR_UID, DIR_UPRM, etc.
 *	G.A. Jones	09/19/88	Added Change, Revert commands.
 *	G.A. Jones	09/20/88	Can change dates as ASCII, not time_t.
 *	G.A. Jones	09/21/88	Added hotfix list displays.
 *	G.A. Jones	10/12/88	Moved ATIM, CTIM from FNODE to DIRENT.
 *	G.A. Jones	10/19/88	Added dirblk banding support.
 *	G.A. Jones	10/21/88	Added ALSEC support.
 *	G.A. Jones	10/27/88	Added Copy command.
 *      S. Hern         02/06/89        Added ability to change the contents
 *                                      of an EA block and data block
 *      S. Hern         03/28/89        Added ability to change other that first
 *                                      character of DIR_NAMA for DIRENT
 *	S. Hern 	04/20/89	Allow either redirection for input
 *      davidbro        04/20/89        changed /r redirection behavior
 *      S. Hern         04/25/89        Changed 13 to 12 to support FNODE
 *                                      field changes in functions "displayable"
 *					and "new_currobj"
 *	davidbro	05/21/89	added "mark_as_bad" and the M)ark
 *					command.
 *	davidbro	05/22/89	added L)og command
 *	davidbro	05/23/89	added U)nmark command
 *      P.A. Williams   05/31/89        Made sure didn't display off end of
 *                                      spare block for bad SPB_SDBMAX field
 *      S.A.Hern        06/22/89        Essentially more of what is listed above
 *                                      under the 4/25/89 change
 *      S.A.Hern        06/28/89        To new_currobj made assignment to
 *                                      filesize to account for EA data runs
 *                                      (used by next_field and previous_field)
 *                                      Fixed item + offset so that allocation
 *                                      sector data runs can be accessed.
 *      S.A.Hern        08/22/89        Fixed allocation sector data display
 *                                      (new_currobj "sec" and "len" fields if
 *                                      TYPE_ALSEC). Set "filesize" using
 *                                      FN_VLEN in new_currobj if TYPE_FNODE).
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "defs.h"
#include "types.h"
#include "globals.h"
//#include <lw.h>



/***	getcmdch - get a character and display corresponding command
 *
 *	This function waits for a keystroke from the user.  If the
 *	keystroke is a valid command, the corresponding command word
 *	(e.g. "Display") is printed and the character is returned.
 *	Otherwise zero is returned.
 *
 *	getcmdch ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		Returns command character or zero if invalid
 *
 *	CALLS		log_getch
 *			printf
 *
 *	WARNINGS	None
 *
 *	EFFECTS         Waits for a character
 */
UCHAR getcmdch ()
{
  UCHAR ch;

  ch = log_getch ();
  switch (ch) {
    case 'D': case 'd': printf ("Display\n"); return (ch);
    case 'C': case 'c':
      if (change) {
	printf ("Change\n"); return (ch);
      }
      else return (0);
    case 'R': case 'r':
      if (change) {
	printf ("Revert\n"); return (ch);
      }
      else return (0);
    case 'O': case 'o':
      if (change) {
	printf ("Copy\n"); return (ch);
      }
      else return (0);
    case 'F': case 'f':
      if (change) {
	printf ("Fence\n"); return (ch);
      }
      else return (0);
    case 'M': case 'm':
      if (change) {
	printf("Mark as bad\n"); return (ch);
      }
      else return (0);
    case 'U': case 'u':
      if (change) {
	printf("Unmark sector\n"); return (ch);
      }
      else return (0);
    case 'L': case 'l':
	if (szLogFile != NULL) {
	    printf ("Log\n"); return (ch);
	}
	else return (0);
    case 'B': case 'b': printf ("Backout\n"); return (ch);
    case 'N': case 'n': printf ("Next\n"); return (ch);
    case 'P': case 'p': printf ("Previous\n"); return (ch);
    case 'Q': case 'q': printf ("Quit\n"); return (ch);
    case 'H': case 'h': case '?': printf ("Help\n"); return (ch);
    default: return (0);
  }
}

/***	new_currobj - build currobj from an item
 *
 *	This function is called to "push into" an item in an object.
 *	It is passed the item number within the current object.  That
 *	item is guaranteed to be "pushable";  for example, this function
 *	will never be asked to build a new currobj for the signature
 *	on an FNODE.
 *
 *	The fields in an object are basically numbered from the top,
 *	starting with 1.  So, for example, item 1 in the super block
 *	is SB_SIG1, etc.
 *
 *	This function sets the "sec" and "len" fields in currobj to
 *	reflect the address and size of the desired object.  Get_object
 *	is then called to read that object.
 *
 *	The caller should push the old object onto the stack before
 *	calling new_currobj, since the contents of currobj are changed.
 *
 *	new_currobj (item)
 *
 *	ENTRY		item - field number to push into
 *
 *	EXIT		No return value
 *			currobj filled in with new item
 *
 *	CALLS		get_object
 *
 *	WARNINGS	Do not call with a non-pushable item like a sig.
 *
 *	EFFECTS         Allocates memory
 *			Reads disk
 */
void new_currobj( USHORT item )
{
#ifdef TRACE
  fprintf (stderr, "new_currobj (%d)\n", item);
  fflush (stderr);
#endif

  switch (currobj.type) {
    case TYPE_SUPERB:
    {
      struct SuperSpare *s = currobj.mem;
      if (currobj.offset == FIELDOFFSET (struct SuperSpare, spb)) {
	if (item == iSPB_HFSEC) {
	  currobj.type = TYPE_HFSEC;
	  currobj.sec = s->spb.SPB_HFSEC;
	  currobj.len = SECTORS_PER_BLOCK;
	  currobj.mem = NULL;
	  get_object ();
	}
#ifdef CODEPAGE
	else if (item == iSPB_CPSEC) {
	  currobj.type = TYPE_CPSEC;
	  currobj.sec = s->spb.SPB_CPSEC;
	  currobj.len = SECTORS_PER_CODEPAGE;
	  currobj.mem = NULL;
	  get_object ();
	}
#endif
      }
      else {
	if (item == iSB_ROOT) {
	  currobj.type = TYPE_FNODE;
	  currobj.sec = s->sb.SB_ROOT;
	  currobj.len = SECTORS_PER_FNODE;
	  currobj.mem = NULL;
	  get_object ();
	}
	else if (item == iSB_BII_P) {
	  currobj.type = TYPE_BII;
	  currobj.sec = s->sb.SB_BII.P;
	  currobj.len = SECTORS_PER_BLOCK;
	  currobj.mem = NULL;
	  get_object ();
	}
	else if (item == iSB_BBL_P) {
	  currobj.type = TYPE_BBL;
	  currobj.sec = s->sb.SB_BBL.P;
	  currobj.len = SECTORS_PER_BLOCK;
	  currobj.mem = NULL;
	  get_object ();
	  currobj.offset = 4;
	}
	else if (item == iSB_DBMAP) {
	  currobj.type = TYPE_DBBIT;
	  currobj.sec = s->sb.SB_DBMAP;
	  currobj.len = 2L;
	  currobj.mem = NULL;
	  get_object ();
	}
      }
    }
    break;
    case TYPE_FNODE:
    {
      struct FNODE *f = currobj.mem;
      ULONG *l = (ULONG *)f->FN_ALREC;

#ifndef CODEPAGE
      if (!currobj.offset && (item == 12) && f->FN_EaDiskLength) {
#else
      if (!currobj.offset && (item == iFN_EA_AI_SEC) && f->FN_EaDiskLength) {
#endif
    currobj.sec = f->FN_EaSector;
        if (f->FN_EaDataFlag) {
	  currobj.type = TYPE_ALSEC;
	  currobj.len = SECTORS_PER_AB;
          filesize = f->FN_EaDiskLength;
        }
	else {
	  currobj.type = TYPE_DATA;
          currobj.len = (f->FN_EaDiskLength - 1) / 512L + 1;
          filesize = f->FN_VLEN;
        }
	currobj.mem = NULL;
	currobj.scratch = 0;
	get_object ();
      }
      else if ((item == 3) && f->FN_ALREC [0].AL_POF && !f->FN_ALREC [0].AL_LEN) {
	currobj.type = TYPE_DIRBLK;
	currobj.sec = f->FN_ALREC [0].AL_POF;
	currobj.len = SECTORS_PER_DIRBLK;
	currobj.mem = NULL;
	get_object ();
	currobj.offset = FIELDOFFSET (struct DIRBLK, DB_START);
      }
      else if (f->FN_AB.AB_FLAG & ABF_NODE) {
	currobj.type = TYPE_ALSEC;
	currobj.sec = *(l+item-1);
	currobj.len = SECTORS_PER_AB;
	currobj.mem = NULL;
	get_object ();
      }
      else {
	currobj.type = TYPE_DATA;
	currobj.sec = f->FN_ALREC [item / 3 - 1].AL_POF;
	currobj.len = f->FN_ALREC [item / 3 - 1].AL_LEN;
	currobj.mem = NULL;
	currobj.scratch = 0;
	get_object ();
      }
    }
    break;
    case TYPE_ALSEC:
    {
      struct ALSEC *a = currobj.mem;
      ULONG *l = (ULONG *)((UCHAR *)currobj.mem + currobj.offset);

      if (a->AS_ALBLK.AB_FLAG & ABF_NODE) {
	currobj.type = TYPE_ALSEC;
	currobj.sec = *(l+item-1);
	currobj.len = SECTORS_PER_AB;
	currobj.mem = NULL;
	get_object ();
      }
      else {
        currobj.type = TYPE_DATA;
        currobj.sec = *(l + (item / 3) - 1 + 2);
        currobj.len = *(l + (item / 3) - 1 + 3);
        currobj.mem = NULL;
	currobj.scratch = 0;
	get_object ();
      }
    }
    break;
    case TYPE_DIRBLK:
    {
      struct DIRBLK *d = currobj.mem;
      union dp dp;

      dp.p = (UCHAR *)currobj.mem + currobj.offset;
      if (item == 8) {
	filesize = dp.d->DIR_SIZE;
	strcat (curpath, "\\");
	strncat (curpath, &dp.d->DIR_NAMA, dp.d->DIR_NAML);
	currobj.type = TYPE_FNODE;
	currobj.sec = dp.d->DIR_FN;
	currobj.len = SECTORS_PER_FNODE;
	currobj.mem = NULL;
	get_object ();
      }
#ifndef CODEPAGE
      else if (item == 16) {
#else
      else if (item == iDIR_BTP) {
#endif
	currobj.type = TYPE_DIRBLK;
	currobj.sec = DOWN_PTR (dp);
	currobj.len = SECTORS_PER_DIRBLK;
	currobj.mem = NULL;
	get_object ();
	currobj.offset = FIELDOFFSET (struct DIRBLK, DB_START);
      }
    }
    break;
    case TYPE_BII:
    {
      ULONG *l;

      l = (ULONG *)((UCHAR *)currobj.mem + currobj.offset);
      currobj.type = TYPE_BITMAP;
      currobj.sec = *(l + (item - 1));		/* items 1-100-->offsets 0-99 */
      currobj.len = SECTORS_PER_BLOCK;
      currobj.mem = NULL;
      currobj.scratch = currobj.offset / 4 + item - 1;
      get_object ();
    }
    break;
    case TYPE_BBL:
      currobj.sec = *(ULONG *)currobj.mem;
      currobj.mem = NULL;
      get_object ();
      currobj.offset = 4;
      break;
    case TYPE_HFSEC:
    {
      ULONG *l;

      l = ((ULONG *)currobj.mem) + currobj.offset + (item-1)/3;
      if (!(item % 3)) {			/* containing FNODE */
	currobj.type = TYPE_FNODE;
	currobj.sec = *(l + 2*hfmax);		/* fetch from third array */
	currobj.len = SECTORS_PER_FNODE;
	currobj.mem = NULL;
	get_object ();
	break;
      }
      else if ((item % 3) == 1)
	currobj.sec = *(l + hfmax);
      else
	currobj.sec = *l;
      currobj.type = TYPE_DATA;
      currobj.len = SECTORS_PER_BLOCK;
      currobj.mem = NULL;
      currobj.scratch = 0;
      get_object ();
      filesize = SECTORS_PER_BLOCK * BYTES_PER_SECTOR;
    }
    break;
#ifdef CODEPAGE
    case TYPE_CPSEC:
      if (currobj.offset == FIELDOFFSET (CPINFOSEC, CP_INFO[0])) {

      if (((item-iCPI_DATASEC) % iCPI_RNGECNT) == 0) {
	 currobj.type = TYPE_CPDATA;
	 currobj.scratch = (item-iCPI_DATASEC)/iCPI_RNGECNT;
	 currobj.sec  = ((PCPINFOSEC)currobj.mem)->CP_INFO[currobj.scratch].CPI_DATASEC;
	 currobj.len  = SECTORS_PER_CODEPAGE;
	 currobj.mem  = NULL;
	 get_object ();
      }
	}
	else {
		currobj.type = TYPE_CPSEC;
	  	currobj.sec = ((PCPINFOSEC)currobj.mem)->CP_NEXTSEC;
	  	currobj.len = SECTORS_PER_CODEPAGE;
	  	currobj.mem = NULL;
	  	get_object ();
	}
    break;
#endif
    default: break;
  }
}

/***	fence_bits - fence bits in a bit map
 *
 *	This function is called to "fence" the bits in a bit map.  This
 *	is done by ANDing every byte in the bitmap with 10101010b,
 *	effectively allocating every other sector.  This tends to cause
 *	the file system to create allocation sector trees quickly, since
 *	it cannot allocate a run longer than one sector.
 *
 *	This function only works if the current item is a bitmap.  The
 *	bitmap is updated on disk immediately.
 *
 *	fence_bits ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		No return value
 *			Current bitmap block fenced
 *
 *	CALLS		None
 *
 *	WARNINGS	None
 *
 *	EFFECTS         Changes global bitmap on disk
 */
void fence_bits ()
{
  register USHORT i;
  UCHAR c;

#ifdef TRACE
  fprintf (stderr, "fence_bits ()\n");
  fflush (stderr);
#endif

  if (currobj.type != TYPE_BITMAP) {
    printf ("Cannot fence this object.\n");
    return;
  }

  printf ("Really fence this bitmap (Y/N) [N]: ");
  do {
    c = log_getch ();
    c = toupper (c);
  } while ((c != 'N') && (c != 'Y'));
  printf ("%c\n", c);
  if (c == 'N')
    printf ("Bitmap not changed.\n");
  else {
    for (i=0; i<SPB * SECSIZE; i++)
      ((UCHAR *)currobj.mem) [i] &= 0xaa;		/* mask bits */
    write_scratch (currobj.sec, currobj.mem, SPB);	/* save bitmap */
  }
  printf ("Bitmap fenced.\n");
}

/***	mark_as_bad - queue the item in the list of bad sectors.
 *
 *	Adds the requested field to the list of sectors to be marked as bad.
 *	If the addition fills the list, set fBadListFull.
 *
 *	ENTRY		item - field number to add to bad sector list.
 *
 *	EXIT		item added, fBadListFull set if list is now full.
 *
 *	CALLS
 *
 *	WARNINGS	item must be the item number of a field that contains
 *			a LSN.
 *
 *	EFFECTS 	sectors are marked bad after DAMAGE has closed the
 *			disk.
 */

#define RM_PATH 2			/*  Route via pathname		      */

void
mark_as_bad (USHORT item, ULONG ulMask)
{
    printf( "ERROR: mark_as_bad not supported.\n" );
}

/***	displayable - determine if an object is "pushable"
 *
 *	This function is called to determine if a field in an object can
 *	be "pushed into".  This includes data runs as well as other
 *	control structures on the disk, but excludes things like signatures.
 *	This function should be called before calling new_currobj().
 *
 *	BUGBUG - is this sucker the same as allocatable()?
 *
 *	displayable (item)
 *
 *	ENTRY		item - field number to analyze
 *
 *	EXIT		Returns TRUE if item displayable, FALSE otherwise
 *
 *	CALLS		None
 *
 *	WARNINGS	None
 *
 *	EFFECTS         None
 */
USHORT displayable (USHORT item)
{
  union dp dp;

#ifdef TRACE
  fprintf (stderr, "displayable (%d)\n", item);
  fflush (stderr);
#endif

  switch (currobj.type) {
    case TYPE_SUPERB:
      if (currobj.offset)
#ifndef CODEPAGE
        return (item == 4);
#else
	return ((item == iSPB_HFSEC) || (item == iSPB_CPSEC));
#endif
      else
	return ((item == iSB_ROOT) || (item == iSB_BII_P) 
	     || (item == iSB_BBL_P) || (item == iSB_DBMAP));
      break;
    case TYPE_FNODE:
      if (currobj.offset == FIELDOFFSET (struct FNODE, FN_ALREC [0]))
	if (((struct FNODE *)currobj.mem)->FN_AB.AB_FLAG & ABF_NODE)
	  return ((item % 2) == 0);
	else
	  return ((item % 3) == 0);
      else if (!currobj.offset)
#ifndef CODEPAGE
        return (item == 12);
#else
        return (item == iFN_EA_AI_SEC);
#endif
      else
	return (FALSE);
      break;
    case TYPE_ALSEC:
      if (currobj.offset)
	if (((struct ALSEC *)currobj.mem)->AS_ALBLK.AB_FLAG & ABF_NODE)
	  return ((item % 2) == 0);
	else
	  return ((item % 3) == 0);
      else
	return (FALSE);
    case TYPE_DIRBLK:
      dp.p = (UCHAR *)currobj.mem + currobj.offset;
      if ((item == 8) && !(dp.d->DIR_FLAG & DF_END))
	return (TRUE);
#ifndef CODEPAGE
      else if ((item == 16) && (dp.d->DIR_FLAG & DF_BTP))
#else
      else if ((item == iDIR_BTP) && (dp.d->DIR_FLAG & DF_BTP))
#endif
	return (TRUE);
      else
	return (FALSE);
      break;
    case TYPE_BII:
      if ((2048 - currobj.offset) < 400)
	return ((item > 0) && (item <= ((2048 - currobj.offset) / 4)));
      else
	return ((item > 0) && (item <= 100));
      break;
    case TYPE_BBL:
      return (item == 1);
      break;
    case TYPE_HFSEC:
      return ((item > 0) && (item <= 60));
      break;
#ifdef CODEPAGE
    case TYPE_CPSEC:
      if (currobj.offset == FIELDOFFSET (CPINFOSEC, CP_INFO[0]))
	 return (((item-iCPI_DATASEC) % iCPI_RNGECNT) == 0);
      else
	 return (item == iCP_NEXTSEC);
      break;
    case TYPE_CPDATA:
      if (currobj.offset == ((PCPDATASEC)currobj.mem)->CPS_OFFSET[0])
	return (0);
      else
	return (0);
      break;
#endif
    default: return (FALSE);
  }
}

/***	change_item - allow the user to change an item in an object
 *
 *	This function is called when the user asks to change an item
 *	in an object.  It asks the user to enter a new value for the
 *	item, then stores that value in the object.  This can range
 *	from simply entering a sector number to changing data in a
 *	file.
 *
 *	It is the caller's responsibility to save the object to disk.
 *
 *	If the user did not type the /D (Damage) switch on the command
 *	line, this function returns immediately.
 *
 *	BUGBUG -- caller should probably remove the user's ability to
 *		  select the "change" option in the first place.
 *
 *	change_item (item)
 *
 *	ENTRY		item - field number to change
 *
 *	EXIT		No return value
 *
 *	CALLS		None
 *
 *	WARNINGS	None
 *
 *	EFFECTS         Changes the contents of a structure
 */
void change_item (USHORT item)
{
  UCHAR c, c2, *pc;
  USHORT u, u2;
  ULONG l, l2;
  USHORT offset, size;
  union dp dp;
  UCHAR buf [10];
  struct FNODE *f;
  USHORT nameChange, charLimit, charNo;

    nameChange = FALSE;


#ifdef TRACE
  fprintf (stderr, "change_item (%d)\n", item);
  fflush (stderr);
#endif

#ifndef CODEPAGE
  if ((currobj.type == TYPE_FNODE && !currobj.offset &&
	(item == 15 || item == 16 || item == 17)) ||
      (currobj.type == TYPE_SUPERB && !currobj.offset && item == 16)) {
    printf ("Use (N)ext or (P)revious to get at that field.\n");
    return;
  }
#else
  if ((currobj.type == TYPE_FNODE && !currobj.offset &&
	(item == 16 || item == 17 || item == 20)) ||
	/* BUGBUG item 16 is not a change option */
      (currobj.type == TYPE_SUPERB && !currobj.offset && item == 16)) {
    printf ("Use (N)ext or (P)revious to get at that field.\n");
    return;
  }
#endif

  switch (currobj.type) {
    case TYPE_SUPERB:
      if (!currobj.offset) {
	offset = superb_off [item-1];
	size = superb_siz [item-1];
      }
      else {
#ifndef CODEPAGE
#ifdef CHECKSUMS
	if (item < 11)
	  offset = (item - 1) * 4 + currobj.offset;
	else
	  offset = FIELDOFFSET (struct SuperSpare, spb.SPB_SPARDB [item - 11]);
	size = (item == 4) ? 1 : 4;
#else
	if (item < 9)
	  offset = (item - 1) * 4 + currobj.offset;
	else
	  offset = FIELDOFFSET (struct SuperSpare, spb.SPB_SPARDB [item - 9]);
	size = (item == 4) ? 1 : 4;
#endif
      }
#else
	if (item < 11)
	  offset = (item - 1) * 4 + currobj.offset;
	else
	  offset = FIELDOFFSET (struct SuperSpare, spb.SPB_SPARDB [item - 11]);
	size = (item == 4) ? 1 : 4;
      }
#endif
      break;
    case TYPE_BII:
      offset = (item-1) * 4 + currobj.offset;
      size = 4;
      break;
    case TYPE_BBL:
      offset = (item==1) ? 0 : (item-2) * 4 + currobj.offset;
      size = 4;
      break;
    case TYPE_FNODE:
      f = (struct FNODE *)currobj.mem;
      if (!currobj.offset) {
	offset = fnode_off [item-1];
	size = fnode_siz [item-1];
      }
      else if (currobj.offset == FIELDOFFSET (struct FNODE, FN_AB)) {
	offset = fnab_off [item-1] + FIELDOFFSET (struct FNODE, FN_AB);
	size = fnab_siz [item-1];
      }
      else if (currobj.offset == FIELDOFFSET (struct FNODE,
          FN_FREE[0]) + f->FN_AclFnodeLength)
        {
        offset = (item-1) + currobj.offset;
        size = sizeof (UCHAR);
        }
      else {
	offset = (item-1) * 4 + currobj.offset;
	size = 4;
      }
      break;
    case TYPE_ALSEC:
      if (!currobj.offset) {
	offset = ab_off [item-1];
	size = ab_siz [item-1];
      }
      else {
	offset = (item-1) * 4 + currobj.offset;
	size = 4;
      }
      break;
    case TYPE_DIRBLK:
      if (item <= iDB_SEC) {
	offset = (item-1) * 4;
	size = 4;
      }
#ifndef CODEPAGE
      else if (item == 16) {
	dp.p = (UCHAR *)currobj.mem + currobj.offset;
	offset = ((UCHAR *)(&(DOWN_PTR (dp))) - dp.p) + currobj.offset;
	size = 4;
      }
      else {
           if (item == 15)
               {
               charLimit = (USHORT )*(UCHAR *)((UCHAR *)currobj.mem +
                   dirent_off [14-6] + currobj.offset); /* DIR_NAML */
               nameChange = TRUE;
               }
	offset = dirent_off [item-6] + currobj.offset;
	size = dirent_siz [item-6];
      }
#else
      else if (item == iDIR_BTP) {
	dp.p = (UCHAR *)currobj.mem + currobj.offset;

	if (dp.d->DIR_ELEN > SECTORS_PER_DIRBLK*512)	// DIR_ELEN must be good
							// for DOWN_PTR()
	   pc = dp.p + ((sizeof(struct DIRENT)+dp.d->DIR_NAML+3U) & ~3U);
	else
	   pc = (UCHAR *)&(DOWN_PTR (dp));

	offset = (pc - dp.p) + currobj.offset;
	size = 4;
      }
      else {
           if (item == iDIR_NAMA)
               {
               charLimit = (USHORT )*(UCHAR *)((UCHAR *)currobj.mem +
                   dirent_off [iDIR_NAMA-iDIR_start] + currobj.offset); /* DIR_NAML */
               nameChange = TRUE;
               }
	offset = dirent_off [item-iDIR_start] + currobj.offset;
	size = dirent_siz [item-iDIR_start];
      }
#endif
      break;
    case TYPE_HFSEC:
      offset = currobj.offset + (item-1) / 3;
      if (!(item % 3))
	offset += 2*hfmax;
      else if ((item % 3) == 1)
	offset += hfmax;
      offset *= 4;
      size = 4;
      break;

#ifdef CODEPAGE
    case TYPE_CPSEC:
	if (currobj.offset == FIELDOFFSET (struct CPINFOSEC, CP_INFO[0])) {
	   offset = FIELDOFFSET (struct CPINFOSEC, CP_INFO[0]) +
	   		((item - 1) / iCPI_RNGECNT) * sizeof (struct CPINFOENT)
		    + cpinfoent_off [(item-1) % iCPI_RNGECNT];
	   size   =  cpinfoent_siz [(item-1) % iCPI_RNGECNT];
	   printf ("offset = %u, size = %u\n", offset, size);
	}
	else {
	   offset = (item-1) * sizeof (ULONG);
	   size   = sizeof (ULONG);
	}
        break;

      case TYPE_CPDATA:
		if (currobj.offset == 0) {
			offset = cpdatasec_off[item-1];
			size   = cpdatasec_siz[item-1];
		}
		else {
			offset = ((PCPDATASEC)currobj.mem)->CPS_OFFSET[currobj.scratch]
					+ cpdataent_off[item-1];
			size   = cpdataent_siz[item-1];
		}
        break; 
#endif

    case TYPE_DATA:
      offset = (item-1) + currobj.offset;
      size = sizeof (UCHAR);
      break;
    case TYPE_BITMAP:
      offset = item-1 + currobj.offset;
      size = sizeof (UCHAR);
      break;
    default:
      printf ("This kind of structure can't be changed yet.\n");
      return;
  }

  if (!size) {			/* this field is an ASCII date */
    l = *(ULONG *)((UCHAR *)currobj.mem + offset);
    strcpy (scratch, get_time (l));
    if (strchr (scratch, '\n'))
      *strchr (scratch, '\n') = '\0';
    printf ("%d) %s: ", item, scratch);
    log_gets (scratch);
    if (!*scratch) {
      printf ("Item not changed.\n");
      return;
    }
    if (sscanf (scratch, "%3s %2d %2d:%2d:%2d %d", buf, &tm.tm_mday,
		&tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year) != 6) {
      printf ("Invalid date format, item not changed.\n");
      return;
    }
    if (tm.tm_year > 1900)
      tm.tm_year -= 1900;
    for (u=0; u<12; u++)
      if (!_stricmp (buf, months [u]))
	break;
    if (u == 12) {
      printf ("Invalid date format, item not changed.\n");
      return;
    }
    tm.tm_mon = u;
    l2 = mktime (&tm);
    l2 -= timezone;
    if (tm.tm_isdst)
      l2 += 60L * 60L;
    if (l == l2) {
      printf ("Item not changed.\n");
      return;
    }
    *(ULONG *)((UCHAR *)currobj.mem + offset) = l2;
  }
  else if (size == sizeof (UCHAR)) {
    if (nameChange && (item == iDIR_NAMA))
        {
        printf ("Enter number (1-%d) of character to change or 0 to abort: ",
            charLimit);
	log_gets (scratch);
        charNo = atoi (scratch);
        if (charNo > 0 && charNo <= charLimit)
            {
            c = *((UCHAR *)currobj.mem + offset + charNo - 1);
            printf ("%d + %d) %02x: ", item, charNo, c);
	    log_gets (scratch);
            if (!*scratch) {
                printf ("Item not changed.\n");
                return;
                }
            sscanf (scratch, "%2x", &c2);
            if (c == c2) {
                printf ("Item not changed.\n");
                return;
                }
            *((UCHAR *)currobj.mem + offset + charNo - 1) = c2;
            }
        else
            {
            printf ("Item not changed.\n");
            return;
            }
        }
    else
    {
    c = *((UCHAR *)currobj.mem + offset);
    printf ("%d) %02x: ", item, c);
    log_gets (scratch);
    if (!*scratch) {
      printf ("Item not changed.\n");
      return;
    }
    sscanf (scratch, "%2x", &c2);
    if (c == c2) {
      printf ("Item not changed.\n");
      return;
    }
    *((UCHAR *)currobj.mem + offset) = c2;
    }
  }
  else if (size == sizeof (USHORT)) {
    u = *(USHORT *)((UCHAR *)currobj.mem + offset);
    printf ("%d) %04x: ", item, u);
    log_gets (scratch);
    if (!*scratch) {
      printf ("Item not changed.\n");
      return;
    }
    sscanf (scratch, "%4x", &u2);
    if (u == u2) {
      printf ("Item not changed.\n");
      return;
    }
    *(USHORT *)((UCHAR *)currobj.mem + offset) = u2;
  }
  else if (size == sizeof (ULONG)) {
    l = *(ULONG *)((UCHAR *)currobj.mem + offset);
    printf ("%d) %08lx: ", item, l);
    log_gets (scratch);
    if (!*scratch) {
      printf ("Item not changed.\n");
      return;
    }
    sscanf (scratch, "%8lx", &l2);
    if (l == l2) {
      printf ("Item not changed.\n");
      return;
    }
    *(ULONG *)((UCHAR *)currobj.mem + offset) = l2;
  }
  else {
    printf ("I can't deal with an object %d bytes long.\n", size);
    return;
  }
  printf ("Item %d) changed.\n", item);
  currobj.dirty = TRUE;
}

/***	cant_backout - display error message about stack underflow
 *
 *	This function is called when the user selects the "backout"
 *	function with no previous object to display.  An error message
 *	is displayed and the current object is unchanged.
 *
 *	BUGBUG - should this guy terminate the program, ala incr. search ESC?
 *
 *	cant_backout ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		No return value
 *
 *	CALLS		printf
 *
 *	WARNINGS	None
 *
 *	EFFECTS         None
 */
void cant_backout ()
{
#ifdef TRACE
  fprintf (stderr, "cant_backout ()\n");
  fflush (stderr);
#endif
  printf ("You are at the top level and cannot back out further.\n");
}

/***	get_item - get an item number from the user
 *
 *	This function is called to ask the user to select an item
 *	to act on.  It examines the current object to see whether
 *	the item number given is in range, but does not check whether
 *	the requested item can be used with a given command.
 *
 *	BUGBUG -- later, this guy can use arrows, tab, mouse, etc. to select
 *
 *	get_item ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		Returns selected item number, zero if aborted
 *
 *	CALLS		printf
 *			log_gets
 *
 *	WARNINGS	None
 *
 *	EFFECTS         None
 */
USHORT get_item ()
{
  int i, j;
  USHORT threshold;
  ULONG bigoffset;
  struct FNODE *f;

#ifdef TRACE
  fprintf (stderr, "get_item ()\n");
  fflush (stderr);
#endif

  while (TRUE) {
    switch (currobj.type) {
      case TYPE_SUPERB:
#ifndef CODEPAGE
	if (currobj.offset) {
	  /* don't display off end of spare block for bad SPB_SDBMAX field */
	  j = ((struct SuperSpare *)currobj.mem)->spb.SPB_SDBMAX;
	  threshold = 8 + (j > 55 ? 55 : j);
	}
	else
	  threshold = 15;
#else
	if (currobj.offset) {
	  /* don't display off end of spare block for bad SPB_SDBMAX field */
	  j = ((struct SuperSpare *)currobj.mem)->spb.SPB_SDBMAX;
	  threshold = iSPB_CPCNT + (j > 55 ? 55 : j);
	}
	else
	  threshold = 15;
#endif
	break;
      case TYPE_BII: threshold = (currobj.offset > (2048 - 400)) ? 12 : 100; break;
      case TYPE_BBL: threshold = (currobj.offset > (2048 - 396)) ? 12 : 101; break;
      case TYPE_FNODE:
        f = (struct FNODE *)currobj.mem;
	if (!currobj.offset)
#ifndef CODEPAGE
	  threshold = 15;
#else
	  threshold = iFN_NEACNT;
#endif
	else if (currobj.offset == FIELDOFFSET (struct FNODE, FN_AB))
	  threshold = 4;
	else if (currobj.offset == FIELDOFFSET (struct FNODE, FN_ALREC [0]))
	  threshold = 24;
        else if (currobj.offset == FIELDOFFSET (struct FNODE,
                FN_FREE[0]) + f->FN_AclFnodeLength)
          threshold = f->FN_EaFnodeLength;
	else
	  threshold = 0;
	break;
      case TYPE_ALSEC:
	if (!currobj.offset)
	  threshold = 7;
	else if (((struct ALSEC *)currobj.mem)->AS_ALBLK.AB_FLAG & ABF_NODE)
	  threshold = 40;
	else
	  threshold = 60;
	break;
      case TYPE_DIRBLK:
	if (((struct DIRENT *)((UCHAR *)currobj.mem + currobj.offset))->DIR_FLAG
	     & DF_BTP)
#ifndef CODEPAGE
	  threshold = 16;
	else
	  threshold = 15;
#else
	  threshold = iDIR_BTP;
	else
	  threshold = iDIR_NAMA;
#endif
	break;
      case TYPE_HFSEC:
	if (hfmax - currobj.offset >= 20)
	  threshold = 60;
	else
	  threshold = (hfmax - currobj.offset) * 3;
	break;
#ifdef CODEPAGE
      case TYPE_CPSEC:
	if (currobj.offset == FIELDOFFSET (CPINFOSEC, CP_INFO[0]))
	   threshold = iCPI_RNGECNT * ((PCPINFOSEC)currobj.mem)->CP_INFOCNT;
	else
	   threshold = iCP_NEXTSEC;
        break;

      case TYPE_CPDATA:
	if (currobj.offset == ((PCPDATASEC)currobj.mem)->CPS_OFFSET[0])
	   threshold = iCPD_TABLE 
                     + 2*(((PCPDATAENT)((UCHAR *)currobj.mem+((PCPDATASEC)currobj.mem)->CPS_OFFSET[currobj.scratch]))->CPD_RNGECNT);
	else
	   threshold = iCPS_INDEX + 2*((PCPDATASEC)currobj.mem)->CPS_DATACNT;
        break; 

#endif
      case TYPE_DATA:
        threshold = 256; /* allow access to first 16 bytes */
	break;
      case TYPE_BITMAP:
	printf ("Enter sector offset of byte (%lx - %lx), 'x' to abort: ",
		currobj.offset * 8L, currobj.offset * 8L + 0x400);
	log_gets (scratch);
	if (scratch [0] == 'x')
	  return (0);
	else {
	  sscanf (scratch, "%lx", &bigoffset);
	  if (bigoffset / 8 < currobj.offset ||
	      bigoffset / 8 > currobj.offset + 0x400) {
	    printf ("Byte offset out of range.\n");
	    return (0);
	  }
	  return (bigoffset / 8L - currobj.offset + 1);
	}

      default: threshold = 0; break;
    }

    printf ("Enter item (1-%d) or 0 to abort: ", threshold);
    log_gets (scratch);
    i = atoi (scratch);
    if ((i <= threshold) && (i >= 0))
      return (i);
    else
      printf ("Item number %d is out of range.\n", i);
  }
}

/***	cant_display - display error message about unpushable item
 *
 *	This function is called when the user selects the "display"
 *	function on an item which cannot be pushed into.  For example,
 *	it doesn't make sense to "go into" the signature on an FNODE.
 *	An error message is displayed and the current object is not changed.
 *
 *	cant_display (item)
 *
 *	ENTRY		item - undisplayable item number
 *
 *	EXIT		No return value
 *
 *	CALLS		printf
 *
 *	WARNINGS	None
 *
 *	EFFECTS         None
 */
void cant_display (USHORT item)
{
#ifdef TRACE
  fprintf (stderr, "cant_display (%d)\n", item);
  fflush (stderr);
#endif

  printf ("Item %d of this object cannot be displayed.\n", item);
}

/***	cant_mark - display error message about unmarkable item
 *
 *	This function is called when the "Mark" function is called on an
 *	item that would make no sense to mark as bad.  So far, anything that
 *	cannot be displayed, cannot be marked.
 *
 *	cant_mark (item)
 *
 *	ENTRY		item - unmarkable item number
 *
 *	EXIT		No return value
 *
 *	CALLS		printf
 *
 *	WARNINGS	None
 *
 *	EFFECTS         None
 */
void cant_mark(USHORT item)
{
#ifdef TRACE
  fprintf (stderr, "cant_mark (%d)\n", item);
  fflush (stderr);
#endif

  printf ("Item %d of this object cannot be marked or unmarked.\n", item);
}

/***	get_command - get a command from the user
 *
 *	This function is called to prompt the user for a command to
 *	perform.  It displays a list of available command letters
 *	and accepts a keystroke from the user.	If the keystroke is
 *	not a valid command, the user is reprompted.  If the keystroke
 *	is valid, it is translated into a CMD_xxxx code and returned
 *	to the caller.
 *
 *	BUGBUG - allow help here with special return code to redisplay
 *
 *	get_command ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		Returns command code (CMD_xxx, in DEFS.H)
 *
 *	CALLS		printf
 *			log_getch
 *
 *	WARNINGS	None
 *
 *	EFFECTS         None
 */
USHORT get_command ()
{
  UCHAR ch;

#ifdef TRACE
  fprintf (stderr, "get_command ()\n");
  fflush (stderr);
#endif
  if (change)
    printf ("\nHelp/Display/%scOpy/Change/Fence/Revert\nMark/Unmark/Backout/Next/Previous/Quit: ",
	    szLogFile == NULL ? "" : "Log/");
  else
    printf ("\nHelp/Display/%sBackout/Next/Previous/Quit: ",
	    szLogFile == NULL ? "" : "Log/");
  while (!(ch=getcmdch ()))
    ;

  switch (ch) {
    case 'D': case 'd': return (CMD_DISPLAY);
    case 'O': case 'o': return (CMD_COPY);
    case 'C': case 'c': return (CMD_CHANGE);
    case 'R': case 'r': return (CMD_REVERT);
    case 'F': case 'f': return (CMD_FENCE);
    case 'B': case 'b': return (CMD_BACKOUT);
    case 'N': case 'n': return (CMD_NEXT);
    case 'P': case 'p': return (CMD_PREVIOUS);
    case 'M': case 'm': return (CMD_MARKBAD);
    case 'U': case 'u': return (CMD_UNMARKBAD);
    case 'L': case 'l': return (CMD_LOG);
    case 'Q': case 'q': return (CMD_QUIT);
    case 'H': case 'h': case '?': return (CMD_HELP);
  }
}

/***	next_field - move to the next major field in this object
 *
 *	This function is called for objects too large to be displayed
 *	completely on the screen.  It sets currobj.offset to the offset
 *	of the next major field that can be displayed.	This might be
 *	the next page of data for a data run, bitmap, or bitmap indirect
 *	block;	the next directory entry in a DIRBLK;  the FN_AB or FN_ALREC
 *	field of an FNODE;  and so on.	The previous position can be
 *	regained with the "Previous" command.
 *
 *	If the offset is already at the end of the structure, it wraps
 *	around to the beginning.
 *
 *	next_field ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		No return value
 *			currobj.offset updated
 *
 *	CALLS		printf
 *
 *	WARNINGS	None
 *
 *	EFFECTS         currobj.offset updated
 */
void next_field ()
{
  union dp dp;
  USHORT size;
  struct FNODE *f;

  switch (currobj.type) {
    case TYPE_SUPERB:
      if (!currobj.offset)
	currobj.offset = FIELDOFFSET (struct SuperSpare, spb);
      else
	currobj.offset = 0;
      break;
    case TYPE_BII:
      currobj.offset += 400;
      if (currobj.offset > 2048)
	currobj.offset = 0;
      break;
    case TYPE_BBL:
      currobj.offset += 400;
      if (currobj.offset > 2048)
	currobj.offset = 4;
      break;
    case TYPE_BITMAP:
    case TYPE_DBBIT:
      currobj.offset += 128;
      if (currobj.offset >= (currobj.type == TYPE_DBBIT ? 1024 : 2048))
	currobj.offset = 0;
      break;
    case TYPE_FNODE:
      f = (struct FNODE *)currobj.mem;
      if (!currobj.offset)
	currobj.offset = FIELDOFFSET (struct FNODE, FN_AB);
      else if (currobj.offset == FIELDOFFSET (struct FNODE, FN_AB))
	currobj.offset = FIELDOFFSET (struct FNODE, FN_ALREC [0]);
      else if (currobj.offset == FIELDOFFSET (struct FNODE, FN_ALREC [0])) {
        if (f->FN_EaFnodeLength)
	  currobj.offset = FIELDOFFSET (struct FNODE, FN_FREE [0]) +
               f->FN_AclFnodeLength;
	else
	  currobj.offset = 0;
      }
      else if (currobj.offset != FIELDOFFSET (struct FNODE, FN_FREE [0]) +
                 f->FN_AclFnodeLength) {
	printf ("Illegal FNODE offset %d, returning to start.\n", currobj.offset);
	currobj.offset = 0;
      }
      else
	currobj.offset = 0;
      break;
    case TYPE_ALSEC:
      if (!currobj.offset)
	currobj.offset = FIELDOFFSET (struct ALSEC, AS_ALBLK) + sizeof (struct ALBLK);
      else {
	size = (((struct ALSEC *)currobj.mem)->AS_ALBLK.AB_FLAG & ABF_NODE) ?
	       sizeof (struct ALNODE) : sizeof (struct ALLEAF);
	currobj.offset += size * 20;
	if (currobj.offset + size*20 > SECSIZE * SECTORS_PER_AB)
	  currobj.offset = 0;
      }
      break;
    case TYPE_DIRBLK:
      dp.p = (UCHAR *)currobj.mem + currobj.offset;
      if (dp.d->DIR_FLAG & DF_END)
	currobj.offset = FIELDOFFSET (struct DIRBLK, DB_START);
      else
	currobj.offset += dp.d->DIR_ELEN;
      break;
    case TYPE_DATA:
      currobj.scratch += 256;
      if (currobj.scratch + (currobj.offset * BYTES_PER_SECTOR) > filesize) {
	currobj.scratch = 0;
	if (currobj.offset) {
	  currobj.offset = 0L;
	  get_object ();
	}
      }
      else if (currobj.scratch == 2048) {	/* need more data */
	currobj.scratch = 0;
	currobj.offset += SECTORS_PER_BLOCK;
	get_object ();				/* read more data */
      }
      break;
    case TYPE_HFSEC:
      if ((currobj.offset + 20) >= hfmax)
	currobj.offset = 0;
      else
	currobj.offset += 20;
      break;
#ifdef CODEPAGE
    case TYPE_CPSEC:
      if (currobj.offset)
	 currobj.offset = 0;
      else
	 currobj.offset = FIELDOFFSET (CPINFOSEC, CP_INFO[0]);
      break;
    case TYPE_CPDATA:
      if (currobj.offset == ((PCPDATASEC)currobj.mem)->CPS_OFFSET[0]) {
	 if (++currobj.scratch == ((PCPDATASEC)currobj.mem)->CPS_DATACNT) 
	    currobj.offset = 0;
      } else {
	 currobj.offset = ((PCPDATASEC)currobj.mem)->CPS_OFFSET[0];
	 if (currobj.scratch == ((PCPDATASEC)currobj.mem)->CPS_DATACNT) 
	    currobj.scratch = 0;
      }
      break;
#endif
    default:
      printf (nonext_str);
  }
}

/***	prev_field - move to the previous major field in this object
 *
 *	This function is called for objects too large to be displayed
 *	completely on the screen.  It sets currobj.offset to the offset
 *	of the previous major field that can be displayed.  This might be
 *	the previous page of data for a data run, bitmap, or bitmap indirect
 *	block;	the next directory entry in a DIRBLK;  the beginning or
 *	FN_AB field of an FNODE;  and so on.  The previous position (the
 *	next major field) can be regained with the "Next" command.
 *
 *	If the offset is already at the beginning of the structure, it
 *	wraps around to the last major field.
 *
 *	prev_field ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		No return value
 *			currobj.offset updated
 *
 *	CALLS		printf
 *
 *	WARNINGS	None
 *
 *	EFFECTS         currobj.offset updated
 */
void prev_field ()
{
  union dp dp;
  UCHAR *target;
  USHORT size, ofs;
  struct FNODE *f;

  switch (currobj.type) {
    case TYPE_SUPERB:
      if (!currobj.offset)
	currobj.offset = FIELDOFFSET (struct SuperSpare, spb);
      else
	currobj.offset = 0;
      break;
    case TYPE_BII:
      if (currobj.offset < 400)
	currobj.offset = 2048 - (2048 % 400);
      else
	currobj.offset -= 400;
      break;
    case TYPE_BBL:
      if (currobj.offset < 404)
	currobj.offset = 2048 - (2048 % 400) + 4;
      else
	currobj.offset -= 400;
      break;
    case TYPE_BITMAP:
      if (!currobj.offset)
	currobj.offset = 2048 - 128;
      else
	currobj.offset -= 128;
      break;
    case TYPE_DBBIT:
      if (!currobj.offset)
	currobj.offset = 1024 - 128;
      else
	currobj.offset -= 128;
      break;
    case TYPE_FNODE:
      f = (struct FNODE *)currobj.mem;
      if (currobj.offset == FIELDOFFSET (struct FNODE, FN_AB))
	currobj.offset = 0;
      else if (currobj.offset == FIELDOFFSET (struct FNODE, FN_ALREC [0]))
	currobj.offset = FIELDOFFSET (struct FNODE, FN_AB);
      else if ((currobj.offset == FIELDOFFSET (struct FNODE, FN_FREE [0]) +
                                  f->FN_AclFnodeLength) || !f->FN_EaFnodeLength)
	currobj.offset = FIELDOFFSET (struct FNODE, FN_ALREC [0]);
      else if (currobj.offset) {
	printf ("Illegal FNODE offset %d, returning to start.\n", currobj.offset);
	currobj.offset = 0;
      }
      else
	currobj.offset = FIELDOFFSET (struct FNODE, FN_FREE [0]) +
             ((struct FNODE *)currobj.mem)->FN_AclFnodeLength;
      break;
    case TYPE_ALSEC:
      size = (((struct ALSEC *)currobj.mem)->AS_ALBLK.AB_FLAG & ABF_NODE) ?
	     sizeof (struct ALNODE) : sizeof (struct ALLEAF);
      ofs = FIELDOFFSET (struct ALSEC, AS_ALBLK) + sizeof (struct ALBLK);
      if (currobj.offset == ofs)
	currobj.offset = 0;
      else if (!currobj.offset)
	currobj.offset = ofs + ((size == sizeof (struct ALNODE)) ? size * 40
				: size * 20);
      else
	currobj.offset -= size * 20;
      break;
    case TYPE_DIRBLK:
      if (currobj.offset == FIELDOFFSET (struct DIRBLK, DB_START)) {
	dp.p = (UCHAR *)currobj.mem + currobj.offset;
	while (!(dp.d->DIR_FLAG & DF_END))
	  NEXT_ENTRY (dp);
      }
      else {
	target = (UCHAR *)currobj.mem + currobj.offset;
	dp.p = (UCHAR *)currobj.mem + FIELDOFFSET (struct DIRBLK, DB_START);
	while ((dp.p + dp.d->DIR_ELEN) != target)
	  NEXT_ENTRY (dp);
      }
      currobj.offset = dp.p - (UCHAR *)currobj.mem;
      break;
    case TYPE_DATA:
      if (!currobj.scratch) {			/* beginning of block */
	if (!currobj.offset) {			/* and beginning of file! */
	  currobj.offset = (filesize / 2048L) * 4L;
	  currobj.scratch = (filesize / 256) * 256; /* "seek" to end */
	  currobj.scratch %= 2048;		/* offset within a block */
	  get_object ();			/* get tail of file */
	}
	else {					/* not beginning of file */
	  currobj.scratch = 2048 - 256;         /* last 256 bytes of block */
	  currobj.offset -= 4L;                 /* back up one block */
	  get_object ();			/* read previous block */
	}
      }
      else					/* in middle of block */
	currobj.scratch -= 256;
      break;
    case TYPE_HFSEC:
      if (!currobj.offset) {
	currobj.offset = hfmax - (hfmax % 20);
	if (currobj.offset == hfmax)
	  currobj.offset -= 20;
      }
      else
	currobj.offset -= 20;
      break;
    default:
      printf (noprev_str);
  }
}

/***	revert - undo changes made to an object
 *
 *	This function is called when the user wants to undo changes he
 *	has made to an object.	It rereads the current structure off the
 *	disk into currobj.  Note that if the user has already saved his
 *	changes (using Backout), Revert is not possible.  An informational
 *	message is displayed if the current object has not been changed.
 *
 *	revert ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		No return value
 *
 *	CALLS		read_scratch
 *
 *	WARNINGS	Changes cannot be reverted once saved with Backout
 *
 *	EFFECTS         Rereads current structure into memory
 */
void revert ()
{
  if (!currobj.dirty) {
    printf ("This structure has not been changed, or has already been saved.\n");
    return;
  }

  if (currobj.type == TYPE_DATA)
    get_object ();			/* just reread current portion of data */
  else
    read_scratch (currobj.sec, currobj.mem, currobj.len);
  printf ("Structure reverted.\n");
  currobj.dirty = FALSE;
}

/***	copy_sectors - process the Copy command
 *
 *	This function is called when the user requests the Copy command.
 *	It asks for source and destination sectors, and the number of
 *	sectors to copy.  It then copies that many sectors from the source
 *	to the destination.  The ranges should not overlap, as this
 *	function doesn't worry about rippling in the right direction.
 *
 *	copy_sectors ()
 *
 *	ENTRY		No parameters
 *
 *	EXIT		No return value
 *
 *	CALLS		read_scratch
 *			write_scratch
 *
 *	WARNINGS	Sector ranges that the user gives should not overlap
 *
 *	EFFECTS         Reads and writes to disk
 */
void copy_sectors ()
{
  ULONG src, dest, len, sect;
  UCHAR *p;

  printf ("Source sector (0 to abort): ");
  log_gets (scratch);
  if (!(src = strtol (scratch, &p, 16)))
    return;
  printf ("Dest sector (0 to abort): ");
  log_gets (scratch);
  if (!(dest = strtol (scratch, &p, 16)))
    return;
  printf ("Number of sectors (0 to abort): ");
  log_gets (scratch);
  if (!(len = strtol (scratch, &p, 16)))
    return;

  if (src+len > dest && dest+len > src) {
    printf ("Sector ranges overlap.\n");
    return;
  }

  printf ("\n");
  for (sect=0L; sect<len; sect++) {
    read_scratch (src+sect, scratch, 1);
    write_scratch (dest+sect, scratch, 1);
    printf (".");
  }
  printf ("\nSectors copied.\n");
  return;
}

/***	display - main object-displaying loop
 *
 *	This function displays an object and asks the user to do something
 *	with it.  Commands include displaying the contents of a field in
 *	an object, changing a field, allocating or freeing sectors in the
 *	bit map, moving to the next logical unit in an object (the next
 *	page of data in a data dump, or the next entry in a DIRBLK), and
 *	returning to the previous location (backing out one level in the
 *	hierarchy).  Some of these commands are not valid for some fields
 *	in some structures;  for example, it doesn't make much sense to
 *	display or allocate the signature of an FNODE.	Such commands are
 *	gently denied.
 *
 *	display ()
 *
 *	ENTRY		No parameters
 *			currobj filled in with superblock description
 *
 *	EXIT		No return value
 *
 *  CALLS   display_object
 *			get_command
 *			get_item
 *			displayable
 *			new_currobj
 *			cant_display
 *			change_item
 *			fence_bits
 *			cant_backout
 *			next_field
 *			prev_field
 *
 *	WARNINGS	None
 */
void
display ()
{
  USHORT command, item;
  UCHAR *p;

  while (TRUE) {
    display_object ();
    command = get_command ();
    switch (command) {
      case CMD_DISPLAY:
	while (TRUE) {
	  if (!(item = get_item ()))
	    break;
	  if (displayable (item)) {
	    objstack [stackptr++] = currobj;
	    new_currobj (item);
	    break;
	  }
	  else
	    cant_display (item);
	}
	break;
      case CMD_UNMARKBAD:
	while (TRUE) {
	    if (!(item = get_item()))
		break;
	    if (displayable(item)) {
		mark_as_bad(item, REMOVE);
		break;
	    }
	    else
		cant_mark(item);
	}
	break;
      case CMD_MARKBAD:
	while (TRUE) {
	    if (!(item = get_item()))
		break;
	    if (displayable(item)) {
		mark_as_bad(item, ADD);
		break;
	    }
	    else
		cant_mark(item);
	}
	break;
      case CMD_LOG:
	if (!(item = get_item()))
	    break;
	write_log(item);
	break;
      case CMD_COPY:
	copy_sectors ();
	break;
      case CMD_CHANGE:
	if (!(item = get_item ()))
	  break;
	change_item (item);
	break;
      case CMD_REVERT:
	revert ();
	break;
      case CMD_FENCE:
	fence_bits ();
	break;
      case CMD_BACKOUT:
	if (currobj.dirty) {
	  currobj.dirty = FALSE;
	  printf ("Saving changes...\n");
	  write_scratch (currobj.sec, currobj.mem, currobj.len);
	  if (!stackptr)
	    break;
	}
	if (!stackptr)
	  cant_backout ();
	else {
	  if (currobj.type == TYPE_FNODE) {
	    p = strrchr (curpath, '\\');
	    if (p)
	      memset (p, '\0', 1023 - (p - curpath));
	  }
	  free_block (currobj.mem);
	  currobj = objstack [--stackptr];
	}
	break;
      case CMD_NEXT:
	next_field ();
	break;
      case CMD_PREVIOUS:
	prev_field ();
	break;
      case CMD_HELP:
	printf ("\nType the first letter of the command you want.\n");
	printf ("Valid commands are:\n");
	printf ("  D - Display  -- dereference a field in an object.\n");
	printf ("  L - Log      -- write a field to a file.\n");
	if (change) {
	  printf ("  C - Change   -- change the contents of a field.\n");
	  printf ("  R - Revert   -- undo changes to a structure.\n");
	  printf ("  O - Copy     -- copy a range of sectors.\n");
	  printf ("  F - Fence    -- fence bits in a bitmap.\n");
	  printf ("  M - Mark     -- mark a sector as bad.\n");
	}
	printf ("  B - Backout  -- return to the previous level.  Undoes 'D'.\n");
	printf ("  N - Next     -- display next field/page.  Different from Display.\n");
	printf ("  P - Previous -- display previous field/page.  Undoes 'N'.\n");
	printf ("  Q - Quit     -- terminate the program.\n");
	printf ("  H - Help     -- print this help message.\n\n");
	printf ("Most commands will ask you for an item number to act upon.\n");
	printf ("Each field's item number is printed before the field, with\n");
	printf ("a parenthesis after it, like:  3) XX_ITEM3: 42.  Type an item\n");
	printf ("number that appears in the display.  Some items are not valid\n");
	printf ("for some commands;  for example, you can't dereference the signature\n");
	printf ("on a structure.  Item 0 will cancel the command.\n\n");
	printf ("Press any key to continue: ");
	log_getch ();
	printf ("\n\n");
	break;
      case CMD_QUIT:
	return;
    }
  }
}