-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcat36.c
177 lines (148 loc) · 4.35 KB
/
cat36.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
/* Copyright (C) 2013, 2021 Lars Brinkhoff <lars@nocrew.org>
Copyright (C) 2020 Adam Sampson <ats@offog.org>
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/>. */
/* The basic operation of this program is to read input in one word
format, specified with -W, and write output in another word format,
specified with -X. Since the word formats include support for tape
image files, there are also a number of applications for manipulating
tapes. If more than one input file is specified and the output is a
tape, they will be separated in the output by tape marks. If the
inputs are tapes, the physical end of tape will be converted to plain
tape mark.
Here are some interesting use cases:
1. Convert a file from one word format to another.
cat36 -Wformat1 -Xformat2 input > output
2. Convert between tape image formats.
cat36 -Wtape7 -Xtape tape > output
3. Copy several input files to a tape.
cat36 -Wformat1 -Xtape -Bblocksize input1 input2 input3 > output
4. Concatenate several tapes into one.
cat36 -Wtape -Xtape tape1 tape2 tape3 > output */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dis.h"
static size_t block = 0;
static word_t mask = WORDMASK;
static void
usage (char **argv)
{
fprintf (stderr, "Usage: %s [-b] [-t] [-W<input word format>] [-X<output word format>] [<input files...>]\n\n", argv[0]);
usage_word_format ();
exit (1);
}
/* If invoked with a name like "its2bin", default to those formats. */
static void
default_formats (const char *argv0)
{
int bad = 0;
const char *basename;
const char *two;
char *first;
basename = strrchr (argv0, '/');
if (basename == NULL)
basename = argv0;
else
basename++;
two = strchr (basename, '2');
if (two == NULL)
return;
first = strndup (basename, two - basename);
if (first == NULL)
{
fprintf (stderr, "out of memory\n");
exit (1);
}
if (parse_input_word_format (first))
bad = 1;
if (parse_output_word_format (two + 1))
bad = 1;
free (first);
if (bad)
{
fprintf (stderr, "%s: unrecognised word format in executable name\n", argv0);
exit (1);
}
}
static void
convert (char *argv0, char *file)
{
word_t word, tape;
size_t count;
FILE *f;
if (file == NULL)
f = stdin;
else
{
f = fopen (file, "rb");
if (f == NULL)
{
fprintf (stderr, "%s: Error opening %s: %s\n",
argv0, file, strerror (errno));
exit (1);
}
}
/* Put tape marks between input files. */
tape = START_FILE;
count = 0;
while ((word = get_word (f)) != -1)
{
if (block == 0) /* If -B was supplied, ignore input tape structure. */
tape |= word & (START_FILE | START_RECORD | START_TAPE);
write_word (stdout, (word & mask) | tape);
count++;
tape = !block || (count % block) ? 0 : START_RECORD;
}
if (f != stdin)
fclose (f);
}
int
main (int argc, char **argv)
{
int opt;
default_formats (argv[0]);
while ((opt = getopt (argc, argv, "btW:X:B:")) != -1)
{
switch (opt)
{
case 'b':
/* Strip bottom bit */
mask &= ~1LL;
break;
case 't':
/* Strip top bit */
mask &= ~(1LL << 35);
break;
case 'W':
if (parse_input_word_format (optarg))
usage (argv);
break;
case 'X':
if (parse_output_word_format (optarg))
usage (argv);
break;
case 'B':
block = atoi (optarg);
break;
default:
usage (argv);
}
}
if (optind == argc)
convert (argv[0], NULL);
for (; optind < argc; optind++)
convert (argv[0], argv[optind]);
flush_word (stdout);
return 0;
}