forked from MikeRalphson/zx81-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2txt.c
411 lines (362 loc) · 13.7 KB
/
p2txt.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
/* p2txt - list the BASIC program in an xtender/Z81 format .p file
* PD by RJM 1993
* cleaned up and ansified 960504
* Forked by ryangray Jan2023
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VERSION "1.0.2"
#define QUOTE_code 11
#define NUM_code 126
#define REM_code 234
unsigned char linebuf[32768]; /* BIG buffer for those lovely m/c REMs */
char *infile;
enum outstyle {OUT_READABLE, OUT_ZMAKEBAS, OUT_ZXTEXT2P};
enum outstyle style = OUT_READABLE;
int inFirstLineREM; /* 1=First line is a REM and we are on the first line */
int onlyFirstLineREM = 0; /* 1=Only preserve codes in a first line REM, 0=Preserve codes everywhere */
/* Character mapping ZX81 character set to ASCII
*
* We have 3 different mappings for optional output styles: readable, Zmakebas,
* and ZXText2P
*/
/* Definitions for a "readable" character mapping in charset_read that uses
Unicode characters when possible (upper ASCII for DOS) and square-bracketed
letters for inverse characters.
*/
#define NAK "#" /* Not A Kharacter */
/* Define character strings for DOS Code Page 437 */
#ifdef __MSDOS__
#define POUND "\x9C"
#define BLK "\xDB"
#define BTM "\xDC"
#define BUL "\\' "
#define BUR "\\ '"
#define TOP "\xDF"
#define BLF "\xDD"
#define BRT "\xDE"
#define BGY "\xB1"
#define BUP "\\.'"
#define BDN "\\'."
#define BLL "\\. "
#define BLR "\\ ."
#define ILR "\\:'"
#define ILL "\\':"
#define IUL "\\.:"
#define IUR "\\:."
#else
#define POUND "£"
#define BLK "█"
#define BTM "▄"
#define BUL "▘"
#define BUR "▝"
#define TOP "▀"
#define BLF "▌"
#define BRT "▐"
#define BGY "▒"
#define BUP "▞"
#define BDN "▚"
#define BLL "▖"
#define BLR "▗"
#define ILR "▛"
#define ILL "▜"
#define IUL "▟"
#define IUR "▙"
#endif
char *charset_read[] =
{
/* spc \' \ ' \'' \. \: \.' \:' \!: \!. */
/* 000-009 */ " ",BUL,BUR,TOP,BLL,BLF,BUP,ILR,BGY,"\\,,",
/* 010-019 */ "\\~~","\"",POUND,"$",":","?","(",")",">","<",
/* 020-029 */ "=","+","-","*","/",";",",",".","0","1",
/* 030-039 */ "2","3","4","5","6","7","8","9","A","B",
/* 040-049 */ "C","D","E","F","G","H","I","J","K","L",
/* 050-059 */ "M","N","O","P","Q","R","S","T","U","V",
/* 060-069 */ "W","X","Y","Z","RND","INKEY$ ","PI",NAK,NAK,NAK,
/* 070-079 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 080-089 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 090-099 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 100-109 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 110-119 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 120-129 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,BLK,IUL, /* \:: \.: */
/* \:. \.. \': \ : \'. \ . \|: */
/* 130-139 */ IUR,BTM,ILL,BRT,BDN, BLR,"[" BGY "]","[,,]","[~~]","[\"]",
/* 140-149 */ "[" POUND "]","[$]","[:]","[?]","[(]","[)]","[>]","[<]","[=]","[+]",
/* 150-159 */ "[-]","[*]","[/]","[;]","[,]","[.]","[0]","[1]","[2]","[3]",
/* 160-169 */ "[4]","[5]","[6]","[7]","[8]","[9]","[A]","[B]","[C]","[D]",
/* 170-179 */ "[E]","[F]","[G]","[H]","[I]","[J]","[K]","[L]","[M]","[N]",
/* 180-189 */ "[O]","[P]","[Q]","[R]","[S]","[T]","[U]","[V]","[W]","[X]",
/* 190-199 */ "[Y]","[Z]","\"\"","AT ","TAB ",NAK,"CODE ","VAL ","LEN ","SIN ",
/* 200-209 */ "COS ","TAN ","ASN ","ACS ","ATN ","LN ","EXP ",
"INT ","SQR ","SGN ",
/* 210-219 */ "ABS ","PEEK ","USR ","STR$ ","CHR$ ","NOT ",
"**"," OR "," AND ","<=",
/* 220-229 */ ">=","<>"," THEN"," TO "," STEP "," LPRINT ",
" LLIST "," STOP"," SLOW"," FAST",
/* 230-239 */ " NEW"," SCROLL"," CONT "," DIM "," REM "," FOR "," GOTO ",
" GOSUB "," INPUT "," LOAD ",
/* 240-249 */ " LIST "," LET "," PAUSE "," NEXT "," POKE ",
" PRINT "," PLOT "," RUN "," SAVE ",
" RAND ",
/* 250-255 */ " IF "," CLS"," UNPLOT "," CLEAR"," RETURN"," COPY"
};
/* Definitions for a character mapping compatible with Zmakebas in charset_zmb
that uses the escape sequences that zmakebas uses for non-ASCII characters
and lowercase letters for inverse letters with a backslash before other
inverse characters.
*/
/* Block Graphics escapes used by Zmakebas and listbasic:
*
* "\' " 001 Top-left corner
* "\ '" 002 Top-right corner
* "\''" 003 Top half
* "\. " 004 Bottom-left corner
* "\: " 005 Left half
* "\.'" 006 Bottom-left, top-right corners
* "\:'" 007 Bottom-right empty
* "\!:" 008 "Gray" solid
* "\!." 009 "Gray" bottom half
* "\!'" 010 "Gray top half
* "\::" 128 Solid (inverse space)
* "\.:" 129 Top-left bank (inverse of "\' ")
* "\:." 130 Top-right blank (inverse of "\ '")
* "\.." 131 Bottom half (inverse of "\''")
* "\':" 132 Bottom-left blank (inverse of "\. ")
* "\ :" 133 Right half (inverse of "\: ")
* "\'." 134 Top-left, bottom-right (inverse of "\.'")
* "\ ." 135 Bottom-right corner (inverse of "\:'")
* "\|:" 136 Inverse "gray" (inverse pixels of "\!:")
* "\|." 137 Black top, gray bottom (inverse of "\!.")
* "\|'" 138 Black bottom, gray top (inverse of "\!'")
*
* Special Characters:
*
* "\\" 012 Pound Sterling symbol (its not # since that is a comment char)
* "\@" 140 Inverse Pound Sterling symbol
* "`" 192 Backtick for the Quote Image "" character (has no inverse)
* Lowercase letters for inverse letters
* Backslash before other characters for their inverse versions
*
*/
char *charset_zmb[] =
{
/* 000-009 */ " ","\\' ","\\ '","\\''","\\. ","\\: ","\\.'","\\:'","\\!:","\\!.",
/* 010-019 */ "\\!'","\"","\\\\","$",":","?","(",")",">","<",
/* 020-029 */ "=","+","-","*","/",";",",",".","0","1",
/* 030-039 */ "2","3","4","5","6","7","8","9","A","B",
/* 040-049 */ "C","D","E","F","G","H","I","J","K","L",
/* 050-059 */ "M","N","O","P","Q","R","S","T","U","V",
/* 060-069 */ "W","X","Y","Z","RND","INKEY$ ","PI",NAK,NAK,NAK,
/* 070-079 */ NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,
/* 080-089 */ NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,
/* 090-099 */ NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,
/* 100-109 */ NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,
/* 110-119 */ NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,
/* 120-129 */ NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,"\\::","\\.:",
/* 130-139 */ "\\:.","\\..","\\':","\\ :","\\'.", "\\ .","\\|:","\\|.","\\|'","\\\"",
/* 140-149 */ "\\@","\\$","\\:","\\?","\\(","\\)","\\>","\\<","\\=","\\+",
/* 150-159 */ "\\-","\\*","\\/","\\;","\\,","\\.","\\0","\\1","\\2","\\3",
/* 160-169 */ "\\4","\\5","\\6","\\7","\\8","\\9","a","b","c","d",
/* 170-179 */ "e","f","g","h","i","j","k","l","m","n",
/* 180-189 */ "o","p","q","r","s","t","u","v","w","x",
/* 190-199 */ "y","z","`","AT ","TAB ",NAK,"CODE ","VAL ","LEN ","SIN ",
/* 200-209 */ "COS ","TAN ","ASN ","ACS ","ATN ","LN ","EXP ",
"INT ","SQR ","SGN ",
/* 210-219 */ "ABS ","PEEK ","USR ","STR$ ","CHR$ ","NOT ",
"**"," OR "," AND ","<=",
/* 220-229 */ ">=","<>"," THEN"," TO "," STEP "," LPRINT ",
" LLIST "," STOP"," SLOW"," FAST",
/* 230-239 */ " NEW"," SCROLL"," CONT "," DIM "," REM "," FOR "," GOTO ",
" GOSUB "," INPUT "," LOAD ",
/* 240-249 */ " LIST "," LET "," PAUSE "," NEXT "," POKE ",
" PRINT "," PLOT "," RUN "," SAVE ",
" RAND ",
/* 250-255 */ " IF "," CLS"," UNPLOT "," CLEAR"," RETURN"," COPY"
};
/* Definitions for a character mapping compatible with the ZXText2P utility in
charset_zxtext2p that uses the escape sequences that ZXText2P uses for non-
ASCII characters and a percent sign before inverse characters.
*/
#define NAK2 "!" /* Not A Kharacter for ZXText2P */
char *charset_zxtext2p[] =
{
/* 000-009 */ " ","\\' ","\\ '","\\''","\\. ","\\: ","\\.'","\\:'","\\##","\\,,",
/* 010-019 */ "\\~~","\"","#","$",":","?","(",")",">","<",
/* 020-029 */ "=","+","-","*","/",";",",",".","0","1",
/* 030-039 */ "2","3","4","5","6","7","8","9","A","B",
/* 040-049 */ "C","D","E","F","G","H","I","J","K","L",
/* 050-059 */ "M","N","O","P","Q","R","S","T","U","V",
/* 060-069 */ "W","X","Y","Z","RND","INKEY$ ","PI",NAK2,NAK2,NAK2,
/* 070-079 */ NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,
/* 080-089 */ NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,
/* 090-099 */ NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,
/* 100-109 */ NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,
/* 110-119 */ NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,
/* 120-129 */ NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,NAK2,"\\::","\\.:",
/* 130-139 */ "\\:.","\\..","\\':","\\ :","\\'.", "\\ .","\\@@","\\;;","\\!!","%\"",
/* 140-149 */ "%#","%$","%:","%?","%(","%)","%>","%<","%=","%+",
/* 150-159 */ "%-","%*","%/","%;","%,","%.","%0","%1","%2","%3",
/* 160-169 */ "%4","%5","%6","%7","%8","%9","%A","%B","%C","%D",
/* 170-179 */ "%E","%F","%G","%H","%I","%J","%K","%L","%M","%N",
/* 180-189 */ "%O","%P","%Q","%R","%S","%T","%U","%V","%W","%X",
/* 190-199 */ "%Y","%Z","\\\"","AT ","TAB ",NAK2,"CODE ","VAL ","LEN ","SIN ",
/* 200-209 */ "COS ","TAN ","ASN ","ACS ","ATN ","LN ","EXP ",
"INT ","SQR ","SGN ",
/* 210-219 */ "ABS ","PEEK ","USR ","STR$ ","CHR$ ","NOT ",
"**"," OR "," AND ","<=",
/* 220-229 */ ">=","<>"," THEN"," TO "," STEP "," LPRINT ",
" LLIST "," STOP"," SLOW"," FAST",
/* 230-239 */ " NEW"," SCROLL"," CONT "," DIM "," REM "," FOR "," GOTO ",
" GOSUB "," INPUT "," LOAD ",
/* 240-249 */ " LIST "," LET "," PAUSE "," NEXT "," POKE ",
" PRINT "," PLOT "," RUN "," SAVE ",
" RAND ",
/* 250-255 */ " IF "," CLS"," UNPLOT "," CLEAR"," RETURN"," COPY"
};
char **charset = charset_read;
/* get a single ZX81 program line & line number & length */
void zxgetline(FILE *in, int *linenum, int *linelen, int *t)
{
int b1, b2, f;
b1 = fgetc(in);
b2 = fgetc(in);
*linenum = b1 * 256 + b2;
(*t) -= 2;
b1 = fgetc(in);
b2 = fgetc(in);
*linelen = b1 + 256 * b2;
(*t) -= 2;
for (f = 0; f < *linelen; f++)
{
linebuf[f] = fgetc(in);
(*t)--;
}
}
/* translate line into keywords using the charset array */
void xlatline(int linelen)
{
int f, inQuotes = 0;
unsigned char c, keyword = linebuf[0];
char *x;
for (f = 0; f < linelen - 1; f++)
{
c = linebuf[f]; /* Character code */
x = charset[c]; /* Translated code */
if ( (keyword != REM_code) && (c == QUOTE_code) )
inQuotes = !inQuotes;
if ( (keyword != REM_code) && (c == NUM_code) )
f += 5; /* avoid inline FP numbers - but ok for REMs */
else if ( (keyword == REM_code && f > 0) || inQuotes)
{
if ( style == OUT_ZMAKEBAS && (!onlyFirstLineREM || inFirstLineREM) &&
((strcmp(x, NAK) == 0) || ((strlen(x) > 1) && (x[0] != '\\') && (x[0]!='`'))) )
printf("\\{%d}", c); /* Print escaped as char code */
else if ( style == OUT_ZXTEXT2P && inFirstLineREM && ((c>63 && c<128) || c>191) )
printf("\\%02X", c); /* Print as backslash-escaped hex code */
else
printf("%s", x); /* Print translated char */
}
else
printf("%s", x); /* Print translated char */
}
printf("\n");
}
/* process (opened) .P file to stdout */
void thrashfile (FILE *in)
{
int b1, b2;
int f, linelen, linenum, d_file, total;
for (f = 1; f <= 3; f++)
fgetc(in); /* ignore sys vars */
b1 = fgetc(in); b2=fgetc(in); /* except d_file */
d_file = b1 + 256 * b2;
for (f = 1; f <= 111; f++)
fgetc(in); /* ignore more sys vars */
total = d_file - 16509; /* if d_file is after, prog is 16509 to d_file-1 right? */
/* run through 'total' bytes, interpreting them as ZX81 program lines */
zxgetline(in, &linenum, &linelen, &total);
inFirstLineREM = (linebuf[0] == REM_code);
while (total >= 0)
{
printf("%4d", linenum);
xlatline(linelen);
inFirstLineREM = 0;
zxgetline(in, &linenum, &linelen, &total);
}
}
void printUsage ()
{
printf("p2txt %s by Ryan Gray, from original by Russell Marks \n", VERSION);
printf(" for improbabledesigns.\n");
printf("Lists ZX81 .P files to stdout.\n");
printf("Usage: p2txt [options] infile.p > outfile.txt\n");
printf("Options are:\n");
printf(" -z Output Zmakebas compatible markup\n");
printf(" -r Output a more readable markup (default).\n");
printf(" Inverse characters in square brackets, most block graphics.\n");
printf(" -1 Output Zmakebas markup but only use codes in a first line that is a REM.\n");
printf(" -2 Output ZXText2P compatible markup\n");
printf(" -? Print this help.\n");
printf("The Zmakebas output will use \\{xxx} codes in REMs and quotes to preserve\n");
printf("the non-printable and token character codes, whereas in readable mode, these\n");
printf("will give a hash (#) character. Zmakebas mode also inserts inverse and true\n");
printf("video codes where inverse characters appear in REMs and strings.\n");
}
void parse_options(int argc, char *argv[])
{
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'z':
style = OUT_ZMAKEBAS;
charset = charset_zmb;
onlyFirstLineREM = 0;
break;
case 'r':
style = OUT_READABLE;
charset = charset_read;
onlyFirstLineREM = 0;
break;
case '1':
style = OUT_ZMAKEBAS;
charset = charset_zmb;
onlyFirstLineREM = 1;
break;
case '2':
style = OUT_ZXTEXT2P;
charset = charset_zxtext2p;
onlyFirstLineREM = 0;
break;
case '?':
printUsage();
exit(EXIT_SUCCESS);
default:
printUsage();
fprintf(stderr, "unknown option: %c\n", argv[1][1]);
exit(EXIT_FAILURE);
}
++argv;
--argc;
}
if (argc <= 1)
{
printUsage();
exit(EXIT_FAILURE);
}
infile = argv[argc-1];
}
int main(int argc, char *argv[])
{
FILE *in;
parse_options(argc, argv);
if ( (in = fopen(infile, "rb")) == NULL )
{
fprintf(stderr, "Error: couldn't open file '%s'\n", infile);
exit(1);
}
thrashfile(in); /* process it */
fclose(in);
exit(0);
}