forked from larsbrinkhoff/pdp10-its-disassembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdart.c
950 lines (840 loc) · 21.5 KB
/
dart.c
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
/* Copyright (C) 2021 Lars Brinkhoff <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <time.h>
#include <utime.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include "dis.h"
#define DART 0444162640000LL /* DART */
#define HEAD 0125045414412LL /* *HEAD* */
#define TAIL 0126441515412LL /* *TAIL* */
#define PRMEND 0046045564404LL /* $PEND$ */
#define LMEDER 030 /* Length of media error data. */
#define MAX800 1280 /* Max record size for 800 BPI. */
#define MAX6250 10240 /* Max record size for 6520 BPI. */
#define MAX MAX6250
static word_t block[MAX];
static int extract = 0;
static FILE *list;
static FILE *info;
static FILE *debug;
static FILE *output;
static word_t checksum;
static char file_path[100];
static struct timeval timestamp[2];
static int dart;
static int iover = 2;
static char *trailer = " (NO TRAILER)";
static int file_argc;
static char** file_argv;
static int tape_frames;
static int tape_bpw; /* Tape frames per word. */
static int tape_bpi;
/* Convert a WAITS date to a struct timeval. */
static void
unix_time (struct timeval *tv, word_t date, word_t minutes)
{
struct tm tm;
timestamp_from_dec (&tm, date);
tm.tm_sec = 0;
tm.tm_min = minutes % 60;
tm.tm_hour = minutes / 60;
tm.tm_mday++;
tm.tm_isdst = 0;
tv->tv_sec = mktime (&tm);
tv->tv_usec = 0;
}
/* Print WAITS date and time. */
static void
print_timestamp (FILE *f, word_t date, word_t minutes)
{
print_dec_timestamp (f, date);
fprintf (f, " %02lld:%02lld", minutes / 60, minutes % 60);
}
/* Convert a Unix time_t to WAITS date and time. */
static word_t
waits_timestamp (time_t t)
{
struct tm *tm = localtime (&t);
int minutes = tm->tm_min + 60 * tm->tm_hour;
int days = 31 * (12 * (tm->tm_year - 64) + tm->tm_mon) + tm->tm_mday - 1;
return (days & 07777LL) | minutes << 12 | ((word_t)days & 070000) << 21;
}
static int
right (word_t word)
{
return word & 0777777LL;
}
static int
left (word_t word)
{
return right (word >> 18);
}
/* Print the retrieval information block. */
static void
print_rib (FILE *f, word_t *rib)
{
word_t date, minutes;
fprintf (f, "\nDDLOC: %012llo", rib[004]); //location of first block
if (rib[006] != 0)
{
fprintf (f, "\nReferenced: ");
date = rib[006] & 07777;
minutes = rib[006] >> 12;
minutes &= 03777;
print_timestamp (f, date, minutes);
}
fprintf (f, "\nDDMPTM: %012llo", rib[007]); //dump status
fprintf (f, "\nDGRP1R: %012llo", rib[010]); //group 1
fprintf (f, "\nDNXTGP: %012llo", rib[011]); //next group
fprintf (f, "\nDSATID: %012llo", rib[012]); //SAT ID
fprintf (f, "\nDQINFO: %012llo", rib[013]); //login
if (iover >= 2)
fprintf (f, "\nRecord offset: %012llo", rib[017]);
}
/* Print extended information. */
static void
print_ext (FILE *f, word_t *data, word_t type)
{
word_t date, minutes;
char string[7], prj[4], prg[4];
if (data[000] != DART)
fprintf (stderr, "\nEXPECTED DART");
if (data[001] != type)
fprintf (stderr, "\nEXPECTED *FILE* or CON,,IOVER");
fprintf (f, "\n Dumped: ");
date = data[2] & 07777;
if (dart >= 5)
date |= (data[2] >> 21) & 070000;
minutes = (data[2] >> 12) & 03777;
print_timestamp (f, date, minutes);
sixbit_to_ascii (data[3], string);
strncpy (prj, string, 3);
prj[3] = 0;
strncpy (prg, string + 3, 3);
prg[3] = 0;
fprintf (f, " [%s,%s]", prj, prg);
fprintf (f, " %d,,%d", left (data[4]), right (data[4]));
fprintf (f, " %d/%d", left (data[5]), right (data[5]));
fprintf (f, " %lld feet", data[6]);
fprintf (f, " %lld words left", data[012]);
}
/* Print media error information. */
static void
print_mederr (FILE *f, word_t *mederr)
{
int i;
if (mederr[LMEDER - 1] != PRMEND)
fprintf (stderr, "\nEXPECTED $PEND$");
for (i = 0; i < LMEDER - 1; i++)
{
if (mederr[i] != 0)
goto print;
}
return;
print:
fprintf (list, " MEDERR");
for (i = 0; i < LMEDER - 1; i++)
{
if ((i % 6) == 0)
fputc ('\n', f);
fprintf (f, " %012llo", mederr[i]);
}
}
static void
get_block (FILE *f, word_t *buffer, int words)
{
int i;
if (words > MAX)
{
fprintf (stderr, "\nEXPECTED SMALLER RECORD LENGTH %d", words);
exit (1);
}
checksum = 0;
for (i = 0; i < words; i++)
{
buffer[i] = get_word (f);
if (buffer[i] == -1)
{
fprintf (stderr, "\nUNEXPECTED END OF TAPE");
exit (1);
}
if (buffer[i] & (START_RECORD|START_FILE|START_TAPE))
{
fprintf (stderr, "\nRecord too short.");
exit (1);
}
checksum ^= buffer[i];
}
}
static void
close_file (void)
{
fprintf (debug, "\nCLOSE %s", file_path);
flush_word (output);
fclose (output);
output = NULL;
utimes (file_path, timestamp);
}
/* Convert WAITS file name to an acceptable Unix name. */
static char *
mangle (char *string)
{
char *p;
while (*string == ' ')
string++;
p = string;
for (p = string; *p != 0; p++)
{
if (*p == ' ')
*p = 0;
*p = tolower (*p);
}
return string;
}
static int
check_name (char *string, size_t m)
{
return strlen (string) <= m;
}
/* Ensure PRJ and PRG names have the right size and padding to the left. */
static int
fix_name (char *string)
{
int n = strlen (string);
if (n > 3)
return 0;
memmove (string - n, string, n);
*string = 0;
return 1;
}
/* Check that a file name from the command line has the right syntax. */
static int
names_ok (int n, char *prj, char *prg, char *nam, char *ext)
{
if (n == 3)
return check_name (nam, 6) &&
fix_name (prg) &&
fix_name (prj);
if (n == 4)
return check_name (nam, 6) &&
check_name (ext, 3) &&
fix_name (prg) &&
fix_name (prj);
return 0;
}
/* Convert a file name from the command line to a WAITS file name. */
static void
unmangle (char *name, char *nam, char *ext, char *prj, char *prg)
{
int n;
if (strlen (name) > 18)
{
fprintf (stderr, "\nInput file name too long: %s", name);
exit (1);
}
strcpy (prj, " ");
strcpy (prg, " ");
prj += 3;
prg += 3;
*ext = 0;
n = sscanf (name, "%[^/]/%[^/]/%[^.].%s", prg, prj, nam, ext);
if (n > 2 && names_ok (n, prg, prj, nam, ext))
return;
n = sscanf (name, "%[^.].%[^/]/%[^.].%s", prg, prj, nam, ext);
if (names_ok (n, prg, prj, nam, ext))
return;
fprintf (stderr, "\nBad input file name: %s", name);
exit (1);
}
static void
open_file (char *name, char *ext, char *prj, char *prg)
{
name = mangle (name);
ext = mangle (ext);
prj = mangle (prj);
prg = mangle (prg);
fprintf (debug, "\nFile name: \"%s\"", name);
fprintf (debug, "\nFile ext: \"%s\"", ext);
fprintf (debug, "\nFile project: \"%s\"", prj);
fprintf (debug, "\nFile programmer: \"%s\"", prg);
if (mkdir (prg, 0777) == -1 && errno != EEXIST)
fprintf (stderr, "\nError creating output directory %s: %s",
prg, strerror (errno));
snprintf (file_path, sizeof file_path, "%s/%s", prg, prj);
if (mkdir (file_path, 0777) == -1 && errno != EEXIST)
fprintf (stderr, "\nError creating output directory %s: %s",
file_path, strerror (errno));
strcat (file_path, "/");
strcat (file_path, name);
if (*ext != 0)
{
strcat (file_path, ".");
strcat (file_path, ext);
}
fprintf (debug, "\nFILE: %s", file_path);
output = fopen (file_path, "wb");
if (output == NULL)
fprintf (stderr, "\nError opening output file %s: %s",
file_path, strerror (errno));
}
static word_t
file_size (char *name)
{
word_t size = 0;
FILE *f = fopen (name, "rb");
if (f == NULL)
{
fprintf (stderr, "\nError opening input file %s: %s",
name, strerror (errno));
exit (1);
}
while (get_word (f) != -1)
size++;
fclose (f);
fprintf (debug, "\nFile %s size: %lld", name, size);
return size;
}
/* Compute the rotated checksum for IOVER3 header/trailer. */
static word_t
rotchk (word_t x)
{
checksum = (checksum << 1) | (checksum >> 35);
checksum += x;
checksum &= 0777777777777LL;
return x;
}
static void
write_block (word_t *data, int size)
{
int i;
for (i = 0; i < size; i++)
write_word (output, *data++);
}
static word_t
read_start (FILE *f, int length)
{
word_t word, date, minutes;
char string[9];
char prj[4], prg[4];
char name[7], ext[7];
int offset = 022;
get_block (f, block + 1, length);
sixbit_to_ascii (block[1], string);
string[6] = ' ';
string[7] = ' ';
string[8] = 0;
*strchr (string, ' ') = ':';
fprintf (list, "\n %s", string);
*strchr (string, ':') = ' ';
sixbit_to_ascii (block[2], name);
fprintf (list, "%s", name);
sixbit_to_ascii (block[3], ext);
ext[3] = 0;
fprintf (list, ".%s", ext);
sixbit_to_ascii (block[5], string);
strncpy (prj, string, 3);
prj[3] = 0;
strncpy (prg, string + 3, 3);
prg[3] = 0;
fprintf (list, "[%s,%s] ", prj, prg);
fprintf (list, "%8lld ", block[007]);
date = block[4] & 07777;
if (dart >= 5)
date |= block[3] & 070000;
minutes = (block[4] >> 12) & 03777;
unix_time (×tamp[0], date, minutes);
unix_time (×tamp[1], date, minutes);
if (date != 0)
print_timestamp (list, date, minutes);
//fprintf (list, " [%ld]", ftell (f));
print_rib (debug, &block[2]);
if (iover >= 3)
{
print_ext (info, &block[022], ascii_to_sixbit ("*FILE*"));
print_mederr (info, &block[length - LMEDER + 1]);
offset = 044;
length -= LMEDER;
}
if (extract)
{
open_file (name, ext, prj, prg);
write_block (block + offset, length - offset + 1);
}
word = get_word (f);
if (checksum != word)
fprintf (stderr, "\nBad checksum: %012llo != %012llo", word, checksum);
else
fprintf (debug, "\nGood checksum: %012llo", checksum);
word = get_word (f);
if (left (word) != 0 && extract)
close_file ();
return word;
}
static word_t
read_data (FILE *f, int length)
{
word_t word;
int offset = 0;
get_block (f, block, length);
if (iover >= 3)
{
print_rib (debug, &block[1]);
print_ext (debug, &block[021], ascii_to_sixbit ("CON ") | iover);
print_mederr (info, &block[length - LMEDER]);
length -= LMEDER;
offset = 043;
}
if (extract)
write_block (block + offset, length - offset);
word = get_word (f);
if (checksum != word)
fprintf (stderr, "\nBad checksum: %012llo != %012llo", word, checksum);
else
fprintf (debug, "\nGood checksum: %012llo", checksum);
word = get_word (f);
if (left (word) != 0 && extract)
close_file ();
return word;
}
static void
read_header (FILE *f, word_t word)
{
int length, i;
char string[7];
char prj[4], prg[4];
word_t date, minutes;
length = right (word);
get_block (f, block + 1, length);
if (block[2] != HEAD && block[2] != TAIL)
fprintf (stderr, "\nEXPECTED *HEAD* OR *TAIL*");
if (block[2] == TAIL)
trailer = "";
dart = left (word);
fprintf (list, "\nDART VERSION %-2d TAPE %s",
dart, block[2] == HEAD ? "HEADER" : "TRAILER");
if (block[1] != DART)
fprintf (stderr, "\nEXPECTED DART MAGIC");
sixbit_to_ascii (block[4], string);
strncpy (prj, string, 3);
prj[3] = 0;
strncpy (prg, string + 3, 3);
prg[3] = 0;
fprintf (list, "\nRECORDED ");
date = block[3] & 07777;
if (dart >= 5)
date |= (block[3] >> 21) & 070000;
minutes = (block[3] >> 12) & 03777;
print_timestamp (list, date, minutes);
fprintf (list, ", BY [%s,%s] %s CLASS",
prj, prg, left (block[5]) ? "SYSTEM" : "USER");
if (left (block[5]))
fprintf (list, " TAPE #%d", right (block[5]));
if (block[2] == TAIL)
{
fputc ('\n', list);
return;
}
if (length <= 5)
return;
if (length != 013)
fprintf (stderr, "\nEXPECTED 13 WORD HEADER");
fprintf (info, "\n Tape %d/%d, position %lld",
left (block[6]), right (block[6]), block[7]);
checksum = 0;
for (i = 1; i < length; i++)
rotchk (block[i]);
if (block[013] != checksum)
fprintf (stderr, "\nBad checksum: %012llo != %012llo",
block[013], checksum);
else
fprintf (debug, "\nGood checksum: %012llo", checksum);
}
/* A -9 record type is a gap to be skipped over. */
static word_t
read_gap (FILE *f)
{
word_t word = get_word (f);
int i;
for (i = 0; i < word; i++)
{
if (get_word (f) == -1)
{
fprintf (stderr, "\nUNEXPECTED END OF TAPE");
exit (1);
}
}
fprintf (list, "\n GAP");
fprintf (info, " %lld", word);
return get_word (f);
}
static word_t
read_record (FILE *f, word_t word)
{
int length = right (word);
if (word == -1)
{
fprintf (list, "\nEND OF TAPE%s", trailer);
exit (0);
}
else if (word & START_TAPE)
{
fprintf (list, "\nLOGICAL END OF TAPE%s\n", trailer);
trailer = " (NO TRAILER)";
}
switch (left (word))
{
case 0:
fprintf (debug, "\nRECORD: DATA");
return read_data (f, length);
case 0777775:
case 0777776:
case 0777777:
iover = 01000000 - left (word);
fprintf (debug, "\nRECORD: FILE (IOVER%d)", iover);
return read_start (f, length);
case 0777767:
fprintf (debug, "\nRECORD: GAP");
return read_gap (f);
default:
fprintf (debug, "\nRECORD: HEAD");
if (left (word) < 10)
{
read_header (f, word);
return get_word (f);
}
else
{
fprintf (stderr, "\nEXPECTED RECORD TYPE got %o", left (word));
exit (1);
}
}
}
static void
read_tape (FILE *f)
{
if (f == NULL)
f = stdin;
word_t word = get_word (f);
if (word == -1)
{
fprintf (stderr, "\nEMPTY TAPE");
exit (1);
}
for (;;)
word = read_record (f, word);
}
static word_t
tape_feet (void)
{
return tape_frames / tape_bpi / 12;
}
static void
write_header (FILE *f, word_t type)
{
int length = iover < 3 ? 5 : 013;
time_t now = waits_timestamp (time (NULL));
if (dart < 5)
now &= 037777777LL;
checksum = 0;
write_word (f, dart << 18 | length | START_FILE);
write_word (f, rotchk (DART));
write_word (f, rotchk (type));
write_word (f, rotchk (now));
write_word (f, rotchk (ascii_to_sixbit ("DMPSYS")));
write_word (f, rotchk (0 | 0));
tape_frames += tape_bpw * (length + 1);
if (iover < 3)
return;
write_word (f, rotchk (1 << 18 | 1));
write_word (f, rotchk (tape_feet ()));
write_word (f, rotchk (0));
write_word (f, rotchk (0777777777777LL));
write_word (f, rotchk (0));
write_word (f, checksum);
}
static int
read_block (FILE *f, word_t *data, int offset)
{
int i, max, size = 0;
word_t word;
max = iover < 3 ? MAX800 : MAX6250 - LMEDER;
max -= 2; /* Leave room for length and checksum words. */
for (i = offset; i < max; i++)
{
word = get_word (f);
if (word == -1)
return size;
data[i] = word;
size++;
}
return size;
}
static void
write_data (FILE *f, FILE *input, int offset, word_t start)
{
int i, length;
length = read_block (input, block, offset);
length += offset;
if (iover >= 3)
{
block[027] = tape_feet ();
memset (block + length, 0, LMEDER * sizeof (word_t));
length += LMEDER;
block[length - 1] = PRMEND;
}
write_word (f, length | start);
checksum = 0;
for (i = 0; i < length; i++)
{
write_word (f, block[i]);
checksum ^= block[i];
}
write_word (f, checksum);
if (iover >= 3)
block[033] -= length - offset; /* Update file words left. */
tape_frames += tape_bpw * (length + 2);
tape_frames += 3 * tape_bpi / 5;
}
static void
write_file (FILE *f, char *name)
{
char nam[18], ext[18], prj[18], prg[18];
word_t timestamp;
struct stat st;
int offset = 021;
FILE *input = fopen (name, "rb");
if (input == NULL)
{
fprintf (stderr, "\nError opening input file %s: %s",
name, strerror (errno));
return;
}
if (fstat (fileno (input), &st) == -1)
fprintf (stderr, "\nError calling stat on file %s: %s",
name, strerror (errno));
timestamp = waits_timestamp (st.st_mtime);
unmangle (name, nam, ext, prj, prg);
block[0] = ascii_to_sixbit ("DSK ");
block[1] = ascii_to_sixbit (nam);
block[2] = ascii_to_sixbit (ext);
block[3] = timestamp & 037777777LL;
if (dart >= 5)
block[2] |= (timestamp >> 21) & 070000LL;
block[4] = ascii_to_sixbit (prj) | ascii_to_sixbit (prg) >> 18;
fprintf (debug, "\nFILE: %s.%s[%s,%s]", nam, ext, prj, prg);
block[005] = 0; //ddloc location of first block
block[006] = file_size (name);
block[007] = 0; //dreftm, referenced
block[010] = 0; //ddmptm, dump time
block[011] = 1; //dgrp1r, group 1
block[012] = 0; //dnxtgp, next group
block[013] = 0; //dsatid, SAT ID
block[014] = 0; //dqinfo, login
block[015] = block[016] = block[017] = 0;
if (iover == 1)
block[020] = 0;
else
block[020] = 1;
if (iover >= 3)
{
block[021] = DART;
block[022] = ascii_to_sixbit ("*FILE*");
block[023] = waits_timestamp (time (NULL));
block[024] = ascii_to_sixbit ("DMPSYS");
block[025] = 0 | 0;
block[026] = 1 << 18 | 1;
block[027] = 0;
block[030] = 0;
block[031] = 0777777777777LL;
block[032] = 0;
block[033] = block[006];
memset (block + 034, 0, 7 * sizeof (word_t));
offset = 043;
}
tape_frames += 3 * tape_bpi; /* Tape mark. */
write_data (f, input, offset,
(((01000000 - iover) << 18) & 0777777000000LL) | START_FILE);
if (iover >= 3)
block[022] = ascii_to_sixbit ("CON ") | iover;
else
offset = 0;
while (!feof (input))
write_data (f, input, offset, START_RECORD);
fclose (input);
}
static void
write_tape (FILE *f)
{
int i;
struct word_format *tmp = input_word_format;
input_word_format = output_word_format;
output_word_format = tmp;
if (f == NULL)
f = stdout;
tape_frames = 0;
write_header (f, HEAD);
for (i = 0; i < file_argc; i++)
write_file (f, file_argv[i]);
tape_frames += 3 * tape_bpi; /* Tape mark. */
write_header (f, TAIL);
flush_word (f);
}
static void
usage (const char *x)
{
fprintf (stderr,
"Usage: %s -c|-t|-x [-v789] [-Wformat] [-Cdir] [-f file]\n", x);
usage_word_format ();
exit (1);
}
static void
newline (void)
{
if (list)
fputc ('\n', list);
}
int
main (int argc, char **argv)
{
void (*process_tape) (FILE *) = NULL;
char *tape_name = NULL, *mode;
char *directory = NULL;
int verbose = 0;
FILE *f = NULL;
int opt;
tape_bpi = 6250;
input_word_format = &tape_word_format;
output_word_format = &aa_word_format;
/* If you ask for a file listing with -t or -xv, it's considered the
output data and written to stdout. Overriden by -c, see below. */
list = stdout;
info = debug = stderr;
while ((opt = getopt (argc, argv, "ctvx123789f:W:C:")) != -1)
{
switch (opt)
{
case 'f':
if (tape_name != NULL)
{
fprintf (stderr, "Just one -f allowed.\n");
exit (1);
}
tape_name = optarg;
break;
case 't':
if (process_tape != NULL)
{
fprintf (stderr, "Just one of -c, -t, or -x allowed.\n");
exit (1);
}
process_tape = read_tape;
mode = "rb";
verbose++;
break;
case 'v':
verbose++;
break;
case 'x':
if (process_tape != NULL)
{
fprintf (stderr, "Just one of -c, -t, or -x allowed.\n");
exit (1);
}
process_tape = read_tape;
mode = "rb";
extract = 1;
break;
case 'c':
if (process_tape != NULL)
{
fprintf (stderr, "Just one of -c, -t, or -x allowed.\n");
exit (1);
}
process_tape = write_tape;
/* Do not write file listing to stdout since it may mix with
tape data written to stdout. */
list = stderr;
mode = "wb";
break;
case '1':
case '2':
case '3':
iover = opt - '0';
break;
case '7':
input_word_format = &tape7_word_format;
tape_bpw = 6;
tape_bpi = 800;
break;
case '8':
input_word_format = &data8_word_format;
tape_bpw = 5;
tape_bpi = 6250;
break;
case '9':
input_word_format = &tape_word_format;
tape_bpw = 5;
tape_bpi = 6250;
break;
case 'W':
if (parse_output_word_format (optarg))
usage (argv[0]);
break;
case 'C':
if (mkdir (optarg, 0777) == -1 && errno != EEXIST)
{
fprintf (stderr, "\nError creating directory %s: %s",
optarg, strerror (errno));
exit (1);
}
directory = optarg;
break;
default:
usage (argv[0]);
}
}
if (process_tape == NULL)
usage (argv[0]);
if (verbose < 1)
list = fopen ("/dev/null", "w");
if (verbose < 2)
info = fopen ("/dev/null", "w");
if (verbose < 3)
debug = fopen ("/dev/null", "w");
if (tape_name && (f = fopen (tape_name, mode)) == NULL)
{
fprintf (stderr, "Error opening input %s: %s\n",
optarg, strerror (errno));
exit (1);
}
if (directory && chdir (directory) == -1)
{
fprintf (stderr, "\nError entering directory %s: %s",
directory, strerror (errno));
exit (1);
}
/* DART version is roughly three higher than IOVER. This is a
reasonable approximation. */
dart = iover + 3;
file_argc = argc - optind;
file_argv = argv + optind;
atexit (newline);
process_tape (f);
return 0;
}