-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXXDP.pm
1976 lines (1554 loc) · 64.8 KB
/
XXDP.pm
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
package XXDP;
require 5.008;
# options
use strict;
# external standard modules
use FindBin;
use FileHandle;
use File::Spec;
use File::Basename;
use File::Path qw( make_path );
use Data::Dumper;
use Time::Local;
use List::Util qw( min max );
# external local modules search path
BEGIN { unshift(@INC, $FindBin::Bin);
unshift(@INC, '.'); }
#----------------------------------------------------------------------------------------------------
#
# P U B L I C M e t h o d s
#
#----------------------------------------------------------------------------------------------------
# table of standard XXDP device filesystem layouts
my %db = ( # *** MFD1/MFD2 type, format 1 ***
#
TU58 => { BOOT => [0], MFD => [1,2], UFD => [3..6], MAP => [7], MON => [8..39], SIZE => 512, DRIVER => 'DD.SYS' },
TU58X => { BOOT => [0], MFD => [1,2], UFD => [3..6], MAP => [7], MON => [8..38], SIZE => 512, DRIVER => 'DD.SYS' }, # legacy XXDPDIR uses MON size 31 vs 32
#
RX01 => { BOOT => [0], MFD => [1,2], UFD => [3..6], MAP => [7], MON => [8..39], SIZE => 494, DRIVER => 'DX.SYS' },
RX02 => { BOOT => [0], MFD => [1,2], UFD => [3..18], MAP => [19..22], MON => [23..54], SIZE => 988, DRIVER => 'DY.SYS' },
#
RP02 => { BOOT => [0], MFD => [1,2], UFD => [3..172], MAP => [173..222], MON => [223..254], SIZE => 48000, DRIVER => 'DP.SYS' },
RP04 => { BOOT => [0], MFD => [1,2], UFD => [3..172], MAP => [173..222], MON => [223..254], SIZE => 48000, DRIVER => 'DB.SYS' },
#
# *** MFD1/MFD2 type, format 2 ***
#
TU56 => { BOOT => [0], MFD => [64,65], UFD => [66..67], MAP => [68], MON => [24..39], INTERLEAVE => 5, SIZE => 576, DRIVER => 'DT.SYS' }, # 578 physical, XXDP only uses 576
#
# *** MFD1/MFD2 type, format 3 ***
#
##RK05 => { BOOT => [0], MFD => [1,4794], UFD => [3..18], MAP => [4795..4799], MON => [30..61], INTERLEAVE => 5, SIZE => 4800, DRIVER => 'DK.SYS' }, # documented block usage disagrees with available images
#
# *** MFD type, format 1 ***
#
RL01 => { BOOT => [0], MFD => [1], MAP => [2..23], UFD => [24..169], MON => [170..201], BAD => [10220..10239], SIZE => 10220, DRIVER => 'DL.SYS' },
RL01X => { BOOT => [0], MFD => [1], MAP => [2..23], UFD => [24..169], MON => [170..199], BAD => [10220..10239], SIZE => 10220, DRIVER => 'DL.SYS' }, # version per doc is BAD ... MON too small, 30 vs 32
RL02 => { BOOT => [0], MFD => [1], MAP => [2..23], UFD => [24..169], MON => [170..201], BAD => [20460..20479], SIZE => 20460, DRIVER => 'DL.SYS' },
#
RK06 => { BOOT => [0], MFD => [1], MAP => [2..30], UFD => [31..126], MON => [127..158], BAD => [27104..27125], SIZE => 27104, DRIVER => 'DM.SYS' },
RK07 => { BOOT => [0], MFD => [1], MAP => [2..30], UFD => [31..126], MON => [127..158], BAD => [53768..53789], SIZE => 53768, DRIVER => 'DM.SYS' },
#
RM03 => { BOOT => [0], MFD => [1], MAP => [2..51], UFD => [52..221], MON => [222..254], SIZE => 48000, DRIVER => 'DR.SYS' },
#
# *** MFD type, format 2 ***
#
MSCP => { BOOT => [0], MFD => [1], MSC => [2], MON => [3..34], UFD => [35..268], MAP => [269..337], SIZE => 65535, DRIVER => 'DU.SYS' },
#
);
my %xl = ( # table of translations
NONE => 'MSCP',
RP05 => 'RP04', RP06 => 'RP04',
RP03 => 'RP02',
RM05 => 'RM03',
RD54 => 'MSCP',
);
#----------------------------------------------------------------------------------------------------
# RX01/RX02 words/sector, bytes/sector, and sectors/track
my %rxdb = ( RX01 => { WPS => 64, BPS => 128, SPT => 26 },
RX02 => { WPS => 128, BPS => 256, SPT => 26 } );
#----------------------------------------------------------------------------------------------------
# create a new base object
#
# returns handle to the oject class
sub new {
my ($class, %arg) = @_;
# class setup
my $self = {};
bless $self, $class;
# initialization
$self->{WARN} = 0; # no warnings enabled
$self->{DEBUG} = 0; # no debug messages
$self->{VERBOSE} = 0; # quiet message mode
$self->{VERSION} = '1.2'; # our code version
# global arguments
$self->{WARN} = $arg{-warn} if exists $arg{-warn};
$self->{DEBUG} = $arg{-debug} if exists $arg{-debug};
$self->{VERBOSE} = $arg{-verbose} if exists $arg{-verbose};
# defaults
$self->{VERBOSE} = 1 if $self->{DEBUG} >= 1; # debug implies verbose messages
# say hello
printf STDERR "%s %s %s (perl %g)\n", $0, $class, $self->{VERSION}, $] if $self->{DEBUG};
# default data
$self->default();
# user supplied arguments
$self->{path} = $arg{-path} if exists $arg{-path};
$self->{image} = $arg{-image} if exists $arg{-image};
$self->{device} = uc($arg{-device}) if exists $arg{-device};
# done
return $self;
}
#----------------------------------------------------------------------------------------------------
# return version number
sub version {
my ($self) = @_;
return $self->{VERSION};
}
#----------------------------------------------------------------------------------------------------
# dump all the values in the data table
#
# return the ascii dump string for printing
sub dump {
my ($self) = @_;
$Data::Dumper::Indent = 2;
$Data::Dumper::Purity = 0;
$Data::Dumper::Sortkeys = sub { [sort {$a =~ m/^\d+$/ && $b =~ m/^\d+$/ ? $a <=> $b : $a cmp $b} keys %{$_[0]}] };
my $result = "\n";
my $s = 0;
my $c = 0;
foreach my $line (split("\n", Dumper($self))) {
$s = 10 if $line =~ m/^\s+'boot'\s+=>/;
$s = 20 if $line =~ m/^\s+'mfd\d'\s+=>/;
$s = 30 if $line =~ m/^\s+'map'\s+=>/;
$s = 40 if $line =~ m/^\s+'ufd'\s+=>/;
$s++ if $line =~ m/\[/;
$s-- if $line =~ m/\]/;
$c = 0 if $s % 10 == 1;
if ($line =~ m/^(\s+)(\d+)(,?)/) {
if ($s % 10 == 2) {
$result .= sprintf("%s", $1) if $c % 8 == 0;
$result .= sprintf("%06o%s", $2,$3);
$result .= sprintf("\n") if $c % 8 == 7;
$c++;
} else {
$result .= sprintf("%s%06o%s\n", $1,$2,$3);
}
} else {
$result .= sprintf("%s\n", $line);
}
}
$result .= "\n";
return $result;
}
#----------------------------------------------------------------------------------------------------
# test print arguments
#
# return the listing as a string for printing
sub test {
my ($self, %arg) = @_;
# arguments
my $pattern = exists $arg{-pattern} ? $arg{-pattern} : ['blank'];
# print arguments
foreach my $entry (@$pattern) {
printf STDERR "test: entry='%s'\n", $entry;
}
printf STDERR "\n";
return '';
}
#----------------------------------------------------------------------------------------------------
# open a disk for access, must exist and be initialized
#
# return 0 on success, else count of errors
sub open {
my ($self) = @_;
# get filehandle, open existing file, return if failure
$self->{disk} = new FileHandle;
return 1 unless sysopen($self->{disk}, $self->{image}, O_RDWR|O_BINARY);
# figure out device layout to use
my $device = exists($xl{$self->{device}}) ? $xl{$self->{device}} : $self->{device};
# read the MFD block(s)
my $mfd1blk = $db{$device}{MFD}->[0]; # block number of MFD1 or MFD1/2 block
$self->{mfd1} = [$mfd1blk,0,[$self->readblk($mfd1blk)]]; # MFD1 or MFD1/2 block
$self->{mfd2} = [$self->mfdnxt,0,[$self->readblk($self->mfdnxt)]] if $self->mfdnxt > 0 && $self->mfdnxt < $db{$device}{SIZE}; # MFD2 block if present
# fake out the MFD blocks on a TU56 image that does not have valid data in the MFD1 block
if ($device eq 'TU56' && ($self->{mfd1}[2]->[0] <= 0 || $self->{mfd1}[2]->[0] >= $db{$device}{SIZE} ||
$self->{mfd1}[2]->[2] <= 0 || $self->{mfd1}[2]->[2] >= $db{$device}{SIZE} ||
$self->{mfd1}[2]->[3] <= 0 || $self->{mfd1}[2]->[3] >= $db{$device}{SIZE} )) {
# fake the MFD block(s)
printf STDERR "Generating virtual MFD1/MFD2 blocks\n" if $self->{DEBUG} >= 2;
# MFD1 block
my @map = @{$db{$device}{MAP}};
$self->{mfd1} = [$db{$device}{MFD}->[0], 0, [$db{$device}{MFD}->[1],
$db{$device}{INTERLEAVE},
$map[0],
@map,
(0) x (253-scalar(@map))]];
# MFD2 block
$self->{mfd2} = [0, 0, [0, 0401, $db{$device}{UFD}->[0], 9, (0) x 252]];
}
# debug print the MFD block(s)
if ($self->{DEBUG} >= 2) {
foreach my $t ('mfd1','mfd2') { # iterate over types
next unless defined $self->{$t}; # check if type exists
my ($blk,$chg,$dat) = @{$self->{$t}}; # retrieve the data
printf STDERR "%s block at %06o:\n", uc($t),$blk;
printf STDERR " (%s,\n", join(',',map {sprintf("%06o",$_)} @$dat[0..15]);
foreach my $i (1..14) { printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$dat[(16*$i+0)..(16*$i+15)]); }
printf STDERR " %s)\n", join(',',map {sprintf("%06o",$_)} @$dat[240..255]);
}
}
# sanity checks
warn sprintf("MFD UFDlen error: %d <> %d", $self->ufdlen, 9) unless $self->ufdlen == 9; # must use 9. word dir entry
# get overall size of the image, in bytes
$self->{bytes} = ($self->{disk}->stat)[7];
# for devices RX01/RX02, fix size by removing track 0 sectors from overall size count
$self->{bytes} -= $rxdb{$device}{SPT}*$rxdb{$device}{BPS} if $device eq 'RX01' || $device eq 'RX02';
# read UFD blocks
if ($self->ufdblk != 0) {
# process UFD blocks
my $ufdptr = $self->ufdblk; # next UFD ptr
$self->ufdnum(0); # init UFD block count
until ($ufdptr == 0) { # done when link goes to zero
my @ufd = $self->readblk($ufdptr); # read next UFD block
$self->ufdnum($self->ufdnum+1); # count UFD blocks
$self->{ufd}{$self->ufdnum} = [$ufdptr,0,[@ufd]]; # store UFD block
$ufdptr = $ufd[0]; # link to next UFD block
}
# debug print all UFDs seen
if ($self->{DEBUG} >= 2) {
foreach my $i (sort({$a<=>$b}keys(%{$self->{ufd}}))) { # iterate over UFD blocks in order
my ($blk,$chg,$ufd) = @{$self->{ufd}{$i}}; # retrieve UFD block
printf STDERR "UFD block %d. at %06o:\n", $i,$blk;
my $j = $self->ufdlen;
for (my $k = 1; $k+$j-1 < $self->{blklen}; $k += $j) {
my @t = @$ufd[$k..($k+$j-1)];
my $d = sprintf(" %s.%s %s", &_rad2asc($t[0],$t[1]), &_rad2asc($t[2]), &_date2asc($t[3]));
$d = '' if $t[0] == 0 && $t[1] == 0;
printf STDERR " %2d:(%s)%s\n", ($k-1)/$j, join(',',map {sprintf("%06o",$_)} @t), $d;
}
}
}
}
# read MAP blocks
if ($self->mapblk != 0) {
# process MAP blocks
my $mapptr = $self->mapblk; # next MAP ptr
$self->mapnum(0); # init MAP block count
until ($mapptr == 0) { # done when link goes to zero
my @map = $self->readblk($mapptr); # read next MAP block from linked
$self->mapnum($self->mapnum+1); # count MAP blocks
# fix MAP blocks on TU56 device image
if ($device eq 'TU56') {
# zap extra unused map words above length value
foreach my $i ($map[2]..251) { $map[$i+4] = 0; }
# set length to standard value
$map[2] = $self->maplen;
}
if ($self->{WARN}) {
warn "MAP ptr error" unless $map[3] == $self->mapblk; # must point to first block
warn "MAP size error" unless $map[2] == $self->maplen; # size must exact
warn "MAP count error" unless $map[1] == $self->mapnum; # must match index
## next unless $map[3] == $self->mapblk && $map[2] == $self->maplen && $map[1] == $self->mapnum;
}
$self->{map}{$self->mapnum} = [$mapptr,0,[@map]]; # store MAP block
$mapptr = $map[0]; # link to next MAP block
}
# scan MAP to find total number of blocks used
$self->usenum(0); # init blocks used count
foreach my $i (keys(%{$self->{map}})) { # iterate over MAP blocks
my ($blk,$chg,$map) = @{$self->{map}{$i}}; # retrieve MAP block
$self->usenum($self->usenum+&_countones(@$map[4..63])); # count all the ones in the MAP
}
if ($self->mfdnxt == 0) {
# MFD1/2 has supported number of blocks, but max still dictated by bitmap
$self->supnum($self->mapnum*16*$self->maplen) if $self->mapnum*16*$self->maplen < $self->supnum;
} else {
# MFD1/MFD2 does not indicate number of blocks, so use the bitmap block count
$self->supnum($self->mapnum*16*$self->maplen);
}
# size of file dictates maximum number of blocks supported
$self->supnum($self->actnum) if $self->actnum < $self->supnum;
# never allow number of supported blocks be larger than the 16b maximum
$self->supnum(65535) if 65535 < $self->supnum;
# debug print all MAPs seen
if ($self->{DEBUG} >= 2) {
foreach my $i (sort({$a<=>$b}keys(%{$self->{map}}))) { # iterate over MAP blocks in order
my ($blk,$chg,$map) = @{$self->{map}{$i}}; # retrieve MAP block
printf STDERR "MAP block %d. at %06o:\n", $i,$blk;
printf STDERR " (%s,\n", join(',',map {sprintf("%06o",$_)} @$map[0..3]);
printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$map[4..18]);
printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$map[19..33]);
printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$map[34..48]);
printf STDERR " %s)\n", join(',',map {sprintf("%06o",$_)} @$map[49..63]);
}
}
}
return 0;
}
#----------------------------------------------------------------------------------------------------
# close an open disk file, but write out any changed overhead blocks first
#
# return 0 on success, else count of errors
sub close {
my ($self) = @_;
# count any errors seen
my $error = 0;
# write the MFD block(s) that have changed (possible, not likely)
foreach my $t ('mfd1','mfd2') {
next unless defined $self->{$t}; # check if block exists
my ($blk,$chg,$dat) = @{$self->{$t}}; # retrieve the data
next unless $chg; # next iteration if unchanged
$self->writeblk($blk,$dat); # write the updated block
$self->{$t}[1] = 0; # no longer changed
if ($self->{DEBUG} >= 2) {
printf STDERR "%s block at %06o was UPDATED\n", uc($t),$blk;
}
}
# write updated UFD blocks
if ($self->ufdblk != 0) {
# process UFD blocks that have changed
foreach my $i (sort({$a<=>$b}keys(%{$self->{ufd}}))) { # iterate over UFD blocks in order
my ($blk,$chg,$ufd) = @{$self->{ufd}{$i}}; # retrieve UFD block
next unless $chg; # next iteration if unchanged
$self->writeblk($blk,$ufd); # write the updated UFD block
$self->{ufd}{$i}[1] = 0; # no longer changed
if ($self->{DEBUG} >= 2) {
printf STDERR "UFD block %d. at %06o was UPDATED:\n", $i,$blk;
my $j = $self->ufdlen;
for (my $k = 1; $k+$j-1 < $self->{blklen}; $k += $j) {
printf STDERR " %2d:(%s)\n", ($k-1)/$j, join(',',map {sprintf("%06o",$_)} @$ufd[$k..($k+$j-1)]);
}
}
}
}
# write updated MAP blocks
if ($self->mapblk != 0) {
# process MAP blocks that have changed
foreach my $i (sort({$a<=>$b}keys(%{$self->{map}}))) { # iterate over MAP blocks in order
my ($blk,$chg,$map) = @{$self->{map}{$i}}; # retrieve MAP block
next unless $chg; # next iteration if unchanged
$self->writeblk($blk,$map); # write the updated MAP block
$self->{map}{$i}[1] = 0; # no longer changed
if ($self->{DEBUG} >= 2) {
printf STDERR "MAP block %d. at %06o was UPDATED:\n", $i,$blk;
printf STDERR " (%s,\n", join(',',map {sprintf("%06o",$_)} @$map[0..3]);
printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$map[4..18]);
printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$map[19..33]);
printf STDERR " %s,\n", join(',',map {sprintf("%06o",$_)} @$map[34..48]);
printf STDERR " %s)\n", join(',',map {sprintf("%06o",$_)} @$map[49..63]);
}
}
}
# all done
CORE::close($self->{disk});
$self->default;
return $error;
}
#----------------------------------------------------------------------------------------------------
# print a directory listing
#
# return the listing as a string for printing
sub directory {
my ($self, %arg) = @_;
# arguments
my $pattern = exists $arg{-pattern} ? $arg{-pattern} : ['*.*'];
my $format = exists $arg{-format} ? lc($arg{-format}) : 'standard'; # or 'extended', 'diagdir', or 'xxdp'
my $result = '';
my $blocks = 0;
my $overhead = 1;
if ($format eq 'diagdir') {
# legacy diagdir/xxdpdir header format
$result .= "\nWill's Works PDP-11 XXDP Image Viewer Version: 1.02\n\n";
if ($self->mfdnxt == 0) {
# MFD1/2 format
$result .= sprintf("image supports %d blocks, %d are pre-allocated\n", $self->supnum, $self->prenum);
$result .= sprintf("XXDP format, monitor starts at block %d\n", $self->monblk);
} else {
# MFD1/MFD2 format
$result .= sprintf("%d directory blocks, ending in block %d\n", $self->ufdnum, $self->ufdblk+$self->ufdnum-1);
$result .= sprintf("Assume DOSBATCH device with %d blocks\n", $self->supnum);
$result .= sprintf("DOSBATCH format, assume monitor starts at block %d\n", $self->mapblk+$self->mapnum);
}
$result .= sprintf("%d blocks in bitmap %d device blocks in use\n", $self->mapnum, $self->usenum);
$result .= sprintf("Attempting dir starting at block %d LIST ALL\n", $self->ufdblk);
} else {
# XXDP / new header format
if ($self->mfdnxt == 0) {
# MFD1/2 format
$result .= sprintf("XXDP format, monitor starts at block %d\n", $self->monblk);
$result .= sprintf("Image supports %d blocks, %d are pre-allocated\n", $self->supnum, $self->prenum);
$overhead += 1;
} else {
# MFD1/MFD2 format
$result .= sprintf("DOS11 format, assume monitor starts at block %d\n", $self->mapblk+$self->mapnum);
$result .= sprintf("Image supports %d blocks\n", $self->supnum);
$overhead += 2;
}
if ($self->ufdnum == 1) {
$result .= sprintf("%d directory block, block %d\n", $self->ufdnum, $self->ufdblk);
} else {
$result .= sprintf("%d directory blocks, block %d thru %d\n", $self->ufdnum, $self->ufdblk, $self->ufdblk+$self->ufdnum-1);
}
$overhead += $self->ufdnum;
if ($self->mapnum == 1) {
$result .= sprintf("%d bitmap block, block %d\n", $self->mapnum, $self->mapblk);
} else {
$result .= sprintf("%d bitmap blocks, block %d thru %d\n", $self->mapnum, $self->mapblk, $self->mapblk+$self->mapnum-1);
}
$overhead += $self->mapnum;
$result .= sprintf("%d device blocks in use according to the bitmap\n", $self->usenum);
$result .= "\nENTRY# FILNAM.EXT DATE LENGTH START VERSION\n\n" unless $format eq 'diagdir';
}
# scan UFD entries in order, print them out
for (my $index = 1; $index <= $self->ufdentrynum; ++$index) {
# get elements of current UFD entry
my ($file1,$file2,$extn,$date,$actend,$start,$length,$last,$act52) = $self->ufdentry($index);
# skip deleted file entries
next if $file1 == 0 && $file2 == 0 && $extn == 0;
# decode filename
my $filename = &_rad2asc($file1,$file2); # decode filename
my $fileextn = &_rad2asc($extn); # decode extension
my $realdate = &_date2asc($date); # decode DEC date
# iterate over all patterns, looking for a match
my $match = 0;
foreach my $entry (@$pattern) {
# count file matches
$match++ if &_file_matches($entry,$filename,$fileextn);
}
# file did not match any pattern
next if $match == 0;
# read first block to get file type/version
my $first = $self->readblk($start);
# system files .SYS have a defined binary version ID
my $sysfile = $fileextn eq 'SYS' && $first->[1]==1;
my $version = $sysfile && $first->[8]!=0 ? chr(($first->[8]>>0)&0xFF).'.'.chr(($first->[8]>>8)&0xFF) : '';
$version = '' unless $version =~ m/^[A-Z][.][0-9]$/; # only valid format allowed
# print selected file(s)
if ($format eq 'diagdir') {
# the way that XXDPDIR.EXE/DIAGDIR.EXE prints it
$result .= sprintf("%3d: %6s.%3s %9s %4d blocks start %4d end %4d", $index,$filename,$fileextn,$realdate,$length,$start,$last);
$result .= sprintf(" %s", $version) if $version ne '';
$result .= " - Contiguous" if $fileextn eq 'SAV';
} elsif ($format eq 'xxdp') {
# the way that XXDPv2 DIR command prints it
$result .= sprintf("%5d %6s.%3s %9s %6d %06o", $index,$filename,$fileextn,$realdate,$length,$start);
$result .= sprintf(" %s", $version) if $version ne '';
$result .= " - Contiguous" if $fileextn eq 'SAV';
} else {
# slight modification of XXDPv2 DIR command with decimal only
$result .= sprintf("%5d %6s.%3s %9s %6d %6d", $index,$filename,$fileextn,$realdate,$length,$start);
$result .= sprintf(" %s", $version) if $version ne '';
$result .= " - Contiguous" if $fileextn eq 'SAV';
}
$result .= "\n";
# count blocks
$blocks += $length;
# check that all blocks in file are mapped as used, compare allocated length vs expected length
if ($self->{WARN}) {
my $count = 0;
if ($fileextn eq 'SAV') {
# contiguous file
for (my $blk = $start; $blk <= $last && $blk < $self->supnum; ++$blk) {
my @data = $self->readblk($blk);
warn sprintf("Warning file %s.%s block %06o is unmapped",
&_strp($filename),&_strp($fileextn),$blk)
unless $self->mapentry($blk) == 1;
$count += 1;
}
} else {
# linked file
for (my $blk = $start; $blk != 0 && $blk < $self->supnum; ) {
my @data = $self->readblk($blk);
warn sprintf("Warning file %s.%s block %06o is unmapped",
&_strp($filename),&_strp($fileextn),$blk)
unless $self->mapentry($blk) == 1;
$count += 1;
$blk = $data[0];
warn sprintf("Warning file %s.%s link pointer %06o is invalid",
&_strp($filename),&_strp($fileextn),$blk)
unless $blk < $self->supnum;
}
}
warn sprintf("Warning file %s.%s expected length %d. <> allocated length %d.",
&_strp($filename),&_strp($fileextn),$length,$count)
unless $length == $count;
}
# print file contents if requested
if ($self->{DEBUG} >= 3) {
# dump file contents if requested
printf STDERR "DUMP %6s.%3s %9s %5d blocks start %5d end %5d\n",
$filename,$fileextn,$realdate,$length,$start,$last;
if ($fileextn eq 'SAV') {
# contiguous file
for (my $blk = $start; $blk <= $last && $blk < $self->supnum; ++$blk) {
my @data = $self->readblk($blk);
my $char;
for (my $i = 0; $i <= $#data; ++$i) {
printf STDERR "%06o:", $blk if $i == 0;
if ($i % 8 == 0 && $i > 0) { printf STDERR " "; $char = ""; }
printf STDERR " %06o", $data[$i];
$char .= &_mapchr($data[$i]) . &_mapchr($data[$i]>>8);
printf STDERR " %s\n", $char if $i % 8 == 7;
}
}
} else {
# linked file
for (my $blk = $start; $blk != 0 && $blk < $self->supnum; ) {
my @data = $self->readblk($blk);
my $char;
for (my $i = 0; $i <= $#data; ++$i) {
printf STDERR "%06o:", $blk if $i == 0;
if ($i % 8 == 0 && $i > 0) { printf STDERR " "; $char = ""; }
printf STDERR " %06o", $data[$i];
$char .= &_mapchr($data[$i]) . &_mapchr($data[$i]>>8);
printf STDERR " %s\n", $char if $i % 8 == 7;
}
$blk = $data[0];
}
}
}
} # for my $index
# old way has no summary
return $result if $format eq 'diagdir';
# xxdp adds free block info
$result .= "\n";
$result .= sprintf("FREE BLOCKS: %5d\n", $self->supnum-$self->usenum);
return $result if $format ne 'extended';
# extended adds lots more info
$result .= "\n";
$result .= sprintf("Bitmap used block count: %6d\n", $self->usenum);
$result .= sprintf("File block count: %6d\n", -$blocks);
$result .= sprintf("Overhead block count: %6d\n", -$overhead);
$result .= " ------\n";
$result .= sprintf("Extra used block count: %6d (monitor area)\n", $self->usenum-$blocks-$overhead);
$result .= "\n";
$result .= sprintf("Device block count: %6d\n", $self->supnum);
$result .= sprintf("Bitmap used block count: %6d\n", -$self->usenum);
$result .= " ------\n";
$result .= sprintf("Free block count: %6d\n", $self->supnum-$self->usenum);
return $result;
}
#----------------------------------------------------------------------------------------------------
# extract files matching pattern
sub extract {
my ($self, %arg) = @_;
# arguments
my $pattern = exists $arg{-pattern} ? $arg{-pattern} : ['*.*'];
my $path = exists $arg{-path} ? $arg{-path} : $self->{path};
my $data = exists $arg{-data} ? $arg{-data} : undef;
my $result = '';
my $count = 0;
# iterate over all UFD entries
for (my $index = 1; $index <= $self->ufdentrynum; ++$index) {
# get elements of currrent UFD entry
my ($file1,$file2,$extn,$date,$actend,$start,$length,$last,$act52) = $self->ufdentry($index);
# skip deleted file entries
next if $file1 == 0 && $file2 == 0 && $extn == 0;
# decode filename
my $filename = &_rad2asc($file1,$file2); # decode filename
my $fileextn = &_rad2asc($extn); # decode extension
my $realdate = &_date2asc($date); # decode DEC date
# iterate over all patterns, looking for a match
my $match = 0;
foreach my $entry (@$pattern) {
# count file matches
$match++ if &_file_matches($entry,$filename,$fileextn);
}
# file did not match any pattern
next if $match == 0;
# print selected file(s)
$result .= sprintf("Extract: %4d: %6s.%3s %9s %5d blocks start %5d end %5d\n",
$index,$filename,$fileextn,$realdate,$length,$start,$last) if $self->{VERBOSE};
# full filename
my $file = &_strp($filename.'.'.$fileextn);
# read all blocks of the file to an array
my @data = ();
if ($fileextn eq 'SAV') {
# core image file, contiguous data image with 256. words of data per block
# loop over all blocks
for (my $blk = $start; $blk <= $last && $blk < $self->supnum; ++$blk) {
# read a block
my @tmp = $self->readblk($blk);
# pack words into buffer and write to file
push(@data, @tmp);
}
} else {
# linked block file, first word is link to next block, followed by 255. words of data
# loop until next block goes to zero
for (my $blk = $start; $blk != 0 && $blk < $self->supnum; ) {
# read a block
my @tmp = $self->readblk($blk);
# remove link to next block
$blk = shift(@tmp);
# pack words into buffer and write to file
push(@data, @tmp);
}
}
if (defined($data)) {
# caller wants data returned
$$data[$count] = [ $index, $file, \@data ];
} else {
# caller wants data extracted to a file
# create output folder if does not exist
make_path($path);
# merged path/file, prune blanks
my $name = File::Spec->catfile($path, $file);
# open a filehandle for writing, binary mode
my $fh = new FileHandle $name, "w";
binmode($fh);
# write all the data
$fh->print(pack("v*",@data));
# we are done, count files
$fh->close;
# set the access and modification times from the source media
utime(&_date2stamp($date), &_date2stamp($date), $name);
}
# count files
$count++;
}
$result .= sprintf("Extracted %d files\n", $count) if $self->{VERBOSE};
return $result;
}
#----------------------------------------------------------------------------------------------------
# insert files matching pattern
sub insert {
my ($self, %arg) = @_;
# arguments
my $pattern = exists $arg{-pattern} ? $arg{-pattern} : ['*.*'];
my $path = exists $arg{-path} ? $arg{-path} : $self->{path};
my $result = '';
my $count = 0;
# get all filenames in the source directory that match pattern
my @files = ();
opendir(DIR, $path);
foreach my $filename (sort(readdir(DIR))) {
my $match = 0;
# iterate over all patterns, looking for a match
foreach my $entry (@$pattern) {
# count file matches
$match++ if &_file_matches($entry,$filename);
}
# save files that match
push(@files, $filename) if $match;
}
closedir(DIR);
# iterate over all file names selected
foreach my $file (@files) {
# merged path/file, prune blanks
my $name = File::Spec->catfile($path, $file);
# skip files that don't exist, or are zero size, or are not plain files
next if ! -e $name || ! -f $name || -z $name;
# open a filehandle for reading, binary mode
my $fh = new FileHandle $name, "r";
binmode($fh);
# data buckets
my $data = undef;
my @data = ();
# read all the data, use optimal host blocksize
while (my $size = $fh->read($data, (stat($fh))[11])) {
push(@data, unpack("v*",$data));
}
# last modified date for the file
my ($dd,$mm,$yy) = (localtime((stat($fh))[9]))[3,4,5];
# we are done
$fh->close;
# split file into name and extension parts
my ($filename, $filepath, $fileextn) = fileparse($name, qr/\.[^.]*$/);
# drop the leading dot from the extension
$fileextn = substr($fileextn,1);
# and build the last modified date
my $realdate = sprintf("%02d-%s-%04d",
$dd,
('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC')[$mm],
$yy+1900);
# encode filename for DOS11
my ($file1,$file2) = &_asc2rad(sprintf("%-6s",$filename)); # encode filename
my $extn = &_asc2rad($fileextn); # encode extension
my $date = &_asc2date($realdate); # encode DEC date
# (re)decode filename
$filename = &_rad2asc($file1,$file2); # decode filename
$fileextn = &_rad2asc($extn); # decode extension
$realdate = &_date2asc($date); # decode DEC date
# delete file if already exists
$result .= $self->delete( -summary => 0, -pattern => [&_strp($filename.'.'.$fileextn)] );
# get next free UFD entry
my $index = $self->ufdfree();
# if no more left ... :-(
return $result.sprintf("ERROR: no more free UFD entries; directory is full!\n") if $index == 0;
# current block used
my $current = 1;
# allocate all blocks needed
my @list = ();
# get location of blocks on disk
my ($length,$start,$last) = (0,0,0);
do { # loop until a solution is found with enough contiguous blocks (if required) or enough blocks
# loop while more data, allocating blocks
@list = ();
my @tmp = @data;
my $ovhd = $fileextn eq 'SAV' ? 0 : 1;
while ($current && @tmp) {
push(@list, $current = $self->mapfree($current+1));
splice(@tmp, 0, $self->{blklen}-$ovhd);
}
# get location of blocks on disk
($length,$start,$last) = (scalar(@list), $list[0], $list[-1]);
# debug print
printf STDERR "name=%s length=%d list=%s\n", $name, $length, join(',',@list) if $self->{DEBUG} >= 2;
# reset for next pass, if required
$current = $start;
# loop for contiguous file request that is not yet satisfied
} while ($fileextn eq 'SAV' && $last != $start+$length-1 && $last != 0);
# for contiguous files, the disk blocks must be contiguous
return $result.sprintf("ERROR: no contiguous free blocks!\n") if $fileextn eq 'SAV' && $last != $start+$length-1;
# check if last block allocated was OK, else we ran out of space
return $result.sprintf("ERROR: no more free blocks!\n") if $last == 0;
# last block link pointer must be null
push(@list, 0);
# ok, now we can finally write the data to disk
while (@data) {
# write the data
if ($fileextn eq 'SAV') {
# core image file, contiguous data image with 256. words of data per block
$self->writeblk($list[0], [splice(@data,0,$self->{blklen})]);
} else {
# linked block file, first word is link to next block, followed by 255. words of data
$self->writeblk($list[0], [$list[1], splice(@data,0,$self->{blklen}-1)]);
}
# allocate the entry, shift block list
$self->mapentry(shift(@list), 1);
}
# print selected file(s)
$result .= sprintf("Insert: %4d: %6s.%3s %9s %5d blocks start %5d end %5d\n",
$index,$filename,$fileextn,$realdate,$length,$start,$last) if $self->{VERBOSE};
# update the directory entry
$self->ufdentry($index,($file1,$file2,$extn,$date,0,$start,$length,$last,0));
# count files
$count++;
}
$result .= sprintf("Inserted %d files\n", $count) if $self->{VERBOSE};
return $result;
}
#----------------------------------------------------------------------------------------------------
# delete files matching pattern
sub delete {
my ($self, %arg) = @_;
# arguments
my $pattern = exists $arg{-pattern} ? $arg{-pattern} : ['*.*'];
my $summary = exists $arg{-summary} ? $arg{-summary} : 1;
my $result = '';
my $count = 0;
# iterate over all UFD entries
for (my $index = 1; $index <= $self->ufdentrynum; ++$index) {
# get elements of current UFD entry
my ($file1,$file2,$extn,$date,$actend,$start,$length,$last,$act52) = $self->ufdentry($index);
# skip deleted file entries
next if $file1 == 0 && $file2 == 0 && $extn == 0;
# decode filename
my $filename = &_rad2asc($file1,$file2); # decode filename
my $fileextn = &_rad2asc($extn); # decode extension
my $realdate = &_date2asc($date); # decode DEC date
# iterate over all patterns, looking for a match
my $match = 0;
foreach my $entry (@$pattern) {
# count file matches
$match++ if &_file_matches($entry,$filename,$fileextn);
}
# file did not match any pattern
next if $match == 0;
# print selected file(s) as being deleted
$result .= sprintf("Deleted: %4d: %6s.%3s %9s %5d blocks start %5d end %5d\n",
$index,$filename,$fileextn,$realdate,$length,$start,$last) if $self->{VERBOSE};
# zero the UFD entry to delete the file
$self->ufdentry($index,(0,0,0,0,0,0,0,0,0));
# zero bitmap entries for the file to deallocate the blocks
for (my $blk = $start; $blk != 0; ) { # loop until next block goes to zero
my @data = $self->readblk($blk); # read a block
$self->mapentry($blk,0); # delete block from bitmap
$blk = $data[0]; # link to next block
}
# count files
$count++;
}
$result .= $summary ? sprintf("Deleted %d files\n", $count) : '' if $self->{VERBOSE};
return $result;
}
#----------------------------------------------------------------------------------------------------
# intitialize a disk file as an empty device
#
# return 0 on success, else count of errors
sub init {
my ($self) = @_;
# count any errors
my $error = 0;
# figure out device layout to use
my $device = exists($xl{$self->{device}}) ? $xl{$self->{device}} : $self->{device};
# exit if device layout is not valid
return -1 unless exists($db{$device});
# delete file if it already exists
unlink $self->{image} if -e $self->{image};
# get filehandle, create new file, return if failure
$self->{disk} = new FileHandle;