-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopl.c
488 lines (408 loc) · 10.9 KB
/
opl.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
/*
* Copyright 2024 Andrew B. Hastings. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* MODIFY and UPDATE PL routines.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
#include <time.h>
#include "cdctap.h"
#include "dcode.h"
#include "ifmt.h"
#include "opl.h"
#include "outfile.h"
#include "simtap.h"
/* set lastmask = 040 for UPDATE PLs */
/* returns -2 if early EOR, -1 if not found, else mod number */
int read_hist(cdc_ctx_t *cd, char *cp, int idx, int lastmask)
{
int rv = -1;
int hist;
/* iterate through modification history "bytes" (18 bits) */
while (1) {
hist = (cp[idx] << 12) | (cp[idx + 1] << 6) | cp[idx + 2];
/* end of history "bytes"? */
if (!hist)
break;
/* bit 16 = activated the line */
if (hist & 0200000)
rv = hist & 0177777;
idx += 3;
if (idx > 9) {
/* last history word? */
if (cp[0] & lastmask)
break;
if (!(cp = cdc_getword(cd)))
return -2;
idx = 1;
}
}
return rv;
}
#define MAXLEN 160 /* max expanded line length */
#define EXPAND_IS_64 1
#define EXPAND_63_IS_COL 2 /* only for MODIFY OPL */
/* return -1=line too long, -2=EOR, else remaining word count */
int expand_text(cdc_ctx_t *cd, int wc, char *obuf, int flags)
{
int state = 0; /* 0=default, 1=00, 2=0077, 3=007700 */
int i;
char c, *cp, *op;
op = obuf;
for (; wc > 0; wc--) {
if (op - obuf > MAXLEN) /* line length exceeded? */
return -1;
if (!(cp = cdc_getword(cd)))
return -2;
dprint(("expand_text: cp=%p wc=%d\n", cp, wc));
for (i = 0; i < 10; i++) {
c = cp[i];
dprint(("expand_text: state=%d c=%d\n", state, c));
if (c == 0) {
/* 0000 = end-of-line */
if (state == 1)
break;
/* 0077 -> 007700 transition? */
if (state == 2) {
state = 3;
continue;
}
/* 00770000 is invalid; treat as 00 */
if (state == 3)
dprint(("expand_text: 00770000\n"));
/* single 00 */
state = 1;
continue;
}
/* 0001 expansion depends on OPL charset */
if (state == 1 && c == 1 && (flags & EXPAND_IS_64)) {
/* 64-character set ':' */
*op++ = dcmap[0];
state = 0;
continue;
}
/* 00xx or 007700xx: expand spaces */
if (state == 1 || state == 3) {
int j;
state = 0;
if (op - obuf + c > MAXLEN)
return -1;
for (j = 0; j < c+1; j++)
*op++ = ' ';
if (c == 077)
state = 2;
continue;
}
/* xx or 0077xx: normal char */
state = 0;
/* 063 is always ':' if OPL charset is 63 */
*op++ = c == 063 && (flags & EXPAND_63_IS_COL)
? ':'
: dcmap[c];
}
/* EOL marker found: mark word as consumed, exit loop */
if (i < 10) {
wc--;
break;
}
}
*op = '\0';
return wc;
}
/* MODIFY OPL/OPLC */
char *extract_opl(cdc_ctx_t *cd, char *name)
{
FILE *of;
struct tm tm;
char fname[16], deck[8];
char *cp, *mods;
int i, len, nmods, nread;
int is_ascii = 0, flags = EXPAND_63_IS_COL;
int width = 72, seqon = 1;
dprint(("extract_opl: %s\n", name));
memset(&tm, 0, sizeof tm);
tm.tm_hour = 12;
/* process 7700 table */
cp = cdc_getword(cd);
if (!cp || cp[0] != 077 || cp[1] != 0)
return "no 7700 table";
len = (cp[2] << 6) | cp[3];
dprint(("extract_opl: 7700 len=%d\n", len));
if (!(cp = cdc_getword(cd)))
return "short 7700 table";
copy_dc(cp, deck, 7, DC_ALNUM); /* deck name */
nread = 1;
if (len >= 3) {
/* extract creation and modification dates */
char mdate[11];
if (!(cp = cdc_getword(cd)))
return "EOR reading cdate from 7700 table";
copy_dc(cp, mdate, 10, DC_NONUL);
if (!(cp = cdc_getword(cd)))
return "EOR reading mdate from 7700 table";
if (cp[0]) /* prefer mdate if present, else use cdate */
copy_dc(cp, mdate, 10, DC_NONUL);
nread = 3;
(void) parse_date(mdate, &tm);
}
if (len >= 14) {
/* extract 63/64 charset flag */
if (!cdc_skipwords(cd, 13 - nread))
return "EOR reading 7700 table";
if (!(cp = cdc_getword(cd)))
return "EOR reading charset from 7700 table";
if (cp[8] <= 1 && cp[9] == 064)
flags = EXPAND_IS_64;
if (cp[8] == 1 && (cp[9] == 0 || cp[9] == 064))
is_ascii = 1;
nread = 14;
}
dprint(("extract_opl: nread %d ascii %d flags %d\n",
nread, is_ascii, flags));
if (!cdc_skipwords(cd, len - nread))
return "EOR skipping over 7700 table";
/* process 7001/7002 table */
cp = cdc_getword(cd);
if (!cp || cp[0] != 070 || cp[1] != 1 && cp[1] != 2)
return "no 700x table";
nmods = ((cp[8] << 6) | cp[9]) + 1;
mods = alloca(nmods * 8);
if (!mods)
return "too many modsets";
memset(mods, 0, nmods * 8);
strcpy(mods, deck);
for (i = 8; i < nmods * 8; i += 8) {
if (!(cp = cdc_getword(cd)))
return "700x table too short";
copy_dc(cp, mods+i, 7, DC_ALNUM);
dprint(("extract_opl: mod %s%c\n", mods+i,
" *"[(cp[7] & 020) >> 4])); /* bit 16 = yanked */
}
of = out_open(name, "txt", fname);
if (!of) {
(void) cdc_skipr(cd);
return "";
}
/* iterate through text lines */
while (cp = cdc_getword(cd)) {
int active, wc, seq, modnum;
char *modname = "unknown";
char obuf[MAXLEN + 12];
active = cp[0] & 040;
wc = cp[0] & 037;
seq = (cp[1] << 12) | (cp[2] << 6) | cp[3];
/* first history "byte" (18 bits) is bits 35-18 */
modnum = read_hist(cd, cp, 4, 0);
if (modnum == -2) {
out_close(of);
return "EOR reading modification history";
}
if (modnum >= 0)
modname = modnum < nmods ? mods + modnum*8 : "invalid";
/* skip over inactive line */
if (!active) {
if (!cdc_skipwords(cd, wc))
break;
continue;
}
dprint(("extract_opl: line %s:%d wc=%d\n", modname, seq, wc));
/* process compressed text */
wc = expand_text(cd, wc, obuf, flags);
if (wc == -2) {
out_close(of);
return "EOR reading compressed text";
}
if (wc == -1) {
out_close(of);
return "line too long in compressed text";
}
if (wc) {
out_close(of);
return "missing EOL in compressed text";
}
/* TBD: *SEQ, *NOSEQ, *WIDTH n */
if (verbose && seqon || verbose > 1)
fprintf(of, "%-*.*s%-7s%6d\n", width, width, obuf,
modname, seq);
else
fprintf(of, "%s\n", obuf);
}
out_close(of);
set_mtime(fname, &tm);
return NULL;
}
/* sequential UPDATE PL */
char *extract_upl(cdc_ctx_t *cd, char *name, struct tm *tm)
{
FILE *of;
char fname[16];
char *cp;
char *ids;
int width = verbose > 1 ? 80 : 72;
int flags = 0;
int i, deckcnt, idcnt;
dprint(("extract_upl: %s\n", name));
/* process sequential OLDPL header: must start with "CHECK" */
cp = cdc_getword(cd);
if (!cp || memcmp(cp, "\003\010\005\003\013", 5) != 0 || (cp[5] & 076))
return "invalid OLDPL header";
if (cp[6] != 036) /* '3' */
flags = EXPAND_IS_64;
if (!(cp = cdc_getword(cd)))
return "short OLDPL header";
idcnt = (cp[4] << 12) | (cp[5] << 6) | cp[6];
deckcnt = (cp[7] << 12) | (cp[8] << 6) | cp[9];
dprint(("extract_upl: ids %d decks %d\n", idcnt, deckcnt));
/* process OLDPL directory */
ids = alloca(idcnt * 10);
if (!ids)
return "too many ids";
memset(ids, 0, idcnt * 10);
for (i = 0; i < idcnt * 10; i += 10) {
if (!(cp = cdc_getword(cd)))
return "OLDPL directory too short";
copy_dc(cp, ids+i, 9, DC_ALNUM);
dprint(("extract_upl: mod %s\n", ids+i));
}
/* skip over OLDPL deck list */
if (!cdc_skipwords(cd, deckcnt))
return "EOR skipping over OLDPL deck list";
of = out_open(name, "txt", fname);
if (!of) {
(void) cdc_skipr(cd);
return "";
}
/* iterate through text lines */
while (cp = cdc_getword(cd)) {
int active, wc, seq, modnum;
char *modname = "unknown";
char obuf[MAXLEN + 12];
/* checksum word? */
if (memcmp(cp, "\000\000\000\000\000", 5) == 0)
break;
active = cp[0] & 020;
wc = (cp[1] << 12) | (cp[2] << 6) | cp[3];
seq = (cp[4] << 12) | (cp[5] << 6) | cp[6];
/* first history "byte" (18 bits) is bits 17-0 */
modnum = read_hist(cd, cp, 7, 040);
if (modnum == -2) {
out_close(of);
return "EOR reading modification history";
}
if (modnum > 0)
modname = modnum <= idcnt ? ids + 10 * (modnum - 1)
: "invalid";
/* skip over inactive line */
if (!active) {
if (!cdc_skipwords(cd, wc))
break;
continue;
}
dprint(("extract_upl: line %s:%d wc=%d\n", modname, seq, wc));
/* process compressed text */
wc = expand_text(cd, wc, obuf, flags);
if (wc == -2) {
out_close(of);
return "EOR reading compressed text";
}
if (wc == -1) {
out_close(of);
return "line too long in compressed text";
}
if (wc) {
out_close(of);
return "missing EOL in compressed text";
}
if (verbose)
fprintf(of, "%-*.*s%s.%d\n", width, width, obuf,
modname, seq);
else
fprintf(of, "%s\n", obuf);
}
out_close(of);
set_mtime(fname, tm);
return NULL;
}
/* random UPDATE PL */
char *extract_uplr(cdc_ctx_t *cd, char *name, struct tm *tm)
{
FILE *of;
char fname[16];
char *cp;
int flags = (dcmap[063] == ':') ? 0 : EXPAND_IS_64;
int width = verbose > 1 ? 80 : 72;
/*
* XXX In an UPDATE random PL, the directory of identifiers is found
* in a separate record following the decks.
*
* Currently, extract_uplr() does not have access to that directory,
* so the identifier field is displayed as a "d" followed by the
* identifier number in octal.
*/
dprint(("extract_uplr: %s\n", name));
of = out_open(name, "txt", fname);
if (!of) {
(void) cdc_skipr(cd);
return "";
}
/* iterate through text lines */
while (cp = cdc_getword(cd)) {
int active, wc, seq, modnum;
char obuf[MAXLEN + 12];
active = cp[0] & 020;
wc = (cp[1] << 12) | (cp[2] << 6) | cp[3];
seq = (cp[4] << 12) | (cp[5] << 6) | cp[6];
/* first history "byte" (18 bits) is bits 17-0 */
modnum = read_hist(cd, cp, 7, 040);
if (modnum == -2) {
out_close(of);
return "EOR reading modification history";
}
/* skip over inactive line */
if (!active) {
if (!cdc_skipwords(cd, wc))
break;
continue;
}
dprint(("extract_uplr: line d%06o:%d wc=%d\n", modnum, seq, wc));
/* process compressed text */
wc = expand_text(cd, wc, obuf, flags);
if (wc == -2) {
out_close(of);
return "EOR reading compressed text";
}
if (wc == -1) {
out_close(of);
return "line too long in compressed text";
}
if (wc) {
out_close(of);
return "missing EOL in compressed text";
}
if (verbose)
fprintf(of, "%-*.*sd%06o.%d\n", width, width, obuf,
modnum, seq);
else
fprintf(of, "%s\n", obuf);
}
out_close(of);
set_mtime(fname, tm);
return NULL;
}