aboutsummaryrefslogtreecommitdiff
path: root/listamsb.c
blob: 5d9b4259167ff9c85bf75c91eb774783d8443dcd (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "amsbtok.h"

#define MIN_STD_TOK 0x80 /* END */
#define MAX_STD_TOK 0xf8 /* <   */

#define MIN_EXT_TOK 0xa3 /* SGN */
#define MAX_EXT_TOK 0xc5 /* STACK */

#ifndef BUFSIZ
#define BUFSIZ 4096
#endif

#define EOL 0x9b

#define MAX_LINENO 63999

/* a program bigger than this can't possibly fit into memory,
   even with cart-based AMSB2 and no DOS loaded. */
#define MAX_PROGLEN 30000

/* a program whose header has a length less than this can't be
   a real AMSB program. The minimum size is what you get if you
   SAVE when there's no program in memory (right after boot or
   a NEW). The minimum size for a program that actually contains
   code seems to be 5 (for 10 PRINT) */
#define MIN_PROGLEN 2

const char *self;

char pipe_command[BUFSIZ + 1] = { "a8cat" };

int verbose = 0;     /* -v */
int raw_output = 0;  /* -a */
int check_only = 0;  /* -c */

int need_pclose = 0;
int bytes_read = 0;
int unknowns = 0;

FILE *infile;
FILE *outfile;

void set_self(const char *argv0) {
   char *p;

   self = argv0;
   p = strrchr(self, '/');
   if(p) self = p + 1;
}

void die(const char *msg) {
	fprintf(stderr, "%s: %s\n", self, msg);
	exit(1);
}

unsigned char read_byte(void) {
	int c;

	c = fgetc(infile);

	if(c < 0)
		die("unexpected EOF, file truncated?");

	bytes_read++;
	return (unsigned char)c;
}

int read_word(void) {
	int w;

	w = read_byte();
	w |= (read_byte() << 8);

	return w;
}

/* return value is program length */
int read_header(void) {
	unsigned char b;
	int proglen;

	b = read_byte();
	if(b) die("not an AMSB file: first byte not $00");

	proglen = read_word();

	if(verbose)
		fprintf(stderr, "proglen == %d (%04x)\n", proglen, proglen);

	if(proglen > MAX_PROGLEN) {
		fprintf(stderr, "%s: not an AMSB file: too big (%d bytes), won't fit in Atari memory\n",
				self, proglen);
		exit(1);
	}

	if(proglen < MIN_PROGLEN) {
		fprintf(stderr, "%s: not an AMSB file: program size in header too small (%d)\n",
				self, proglen);
		exit(1);
	}

	return proglen;
}

void unknown_token(int lineno, unsigned char byte, int ext) {
	unknowns++;
	fprintf(outfile, "<unknown %stoken ", (ext ? "extended " : ""));
	fprintf(outfile, "%s%02x>", (ext ? "ff ": ""), byte);
}

int next_line(void) {
	static int last_lineno = -1;
	int ptr, lineno, was_ff, in_string;
	unsigned char byte;

	/* pointer to last token on the line, offset by whatever MEMLO
		happened to be when the file was SAVEd. 0 means this is the
		last line, otherwise we don't use its value. */
	ptr = read_word();
	if(!ptr) {
		if(verbose)
			fprintf(stderr, "end of program\n");
		return -1;
	}

	lineno = read_word();
	if(verbose)
		fprintf(stderr, "found line number %d\n", lineno);

	if(lineno <= last_lineno) {
		fprintf(stderr, "%s: line number out of order (%d <= %d)\n",
				self, lineno, last_lineno);
	}

	if(lineno > MAX_LINENO) {
		fprintf(stderr, "%s: line number out range (%d > %d)\n",
				self, lineno, MAX_LINENO);
	}

	last_lineno = lineno;

	/* note that AMSB always puts a space after the line number in LIST */
	fprintf(outfile, "%d ", lineno);

	was_ff = 0;
	in_string = 0;

	/* walk and print the tokens. when we hit a null byte, we're done. */
	while(1) {
		byte = read_byte();

		if(in_string) {
			if(byte == 0x00 || byte == '|') {
				/* end of string */
				in_string = 0;
				putc('"', outfile);

				/* if we read a null, that means the line ends with a string
					that's missing its closing double-quote. */
				if(byte == 0x00) {
					break;
				} else {
					continue;
				}
			}
			putc(byte, outfile);
		} else if(byte == ':') {
			/* don't print the colon if the next token is a ! or ' for a comment */
			unsigned char next = read_byte();
			if( !(next == 0x9a || next == 0x9b) )
				putc(byte, outfile);
			ungetc(next, infile);
			bytes_read--;
		} else if(byte == '"') {
			/* strings start but *don't end* with a double-quote */
			in_string = 1;
			putc(byte, outfile);
		} else if(was_ff) {
			if(byte >= MIN_EXT_TOK && byte <= MAX_EXT_TOK) {
				fprintf(outfile, "%s", ext_tokens[byte - MIN_EXT_TOK]);
			} else {
				unknown_token(lineno, byte, 1);
			}
			was_ff = 0;
		} else if(byte == 0xff) {
			was_ff = 1;
		} else if(byte >= MIN_STD_TOK && byte <= MAX_STD_TOK) {
			fprintf(outfile, "%s", std_tokens[byte - MIN_STD_TOK]);
		} else if(byte >= 0x80) {
			unknown_token(lineno, byte, 0);
		} else {
			if(!byte) break;
			putc(byte, outfile);
		}
	}

	putc(EOL, outfile);

	return lineno;
}

void print_help(void) {
	printf("%s v" VERSION " - detokenize Atari Microsoft BASIC files\n", self);
	puts("By B. Watson <urchlay@slackware.uk>, released under the WTFPL");
	printf("Usage: %s [-a] [-v] [-h] [-i] [-u] [-t] [-m] [-s] [file]\n", self);
	puts("  -a: raw ATASCII output");
	puts("  -c: check only (no listing)");
	puts("  -v: verbose");
	puts("  -h: show this help");
	puts("  -i -u -t -m -s: passed to a8cat (try 'a8cat -h')");
	puts("file must be a tokenized (SAVEd) AMSB file. if not given, reads from stdin.");
}

void parse_args(int argc, char **argv) {
	char tmp[10];
	int opt;

	while( (opt = getopt(argc, argv, "cvaiutmsh")) != -1) {
		switch(opt) {
			case 'c': check_only = 1; break;
			case 'a': raw_output = 1; break;
			case 'h': print_help(); exit(0);
			case 'v': verbose = 1; break;
			case 'i':
			case 'u':
			case 't':
			case 'm':
			case 's':
				if(strlen(pipe_command) > (BUFSIZ - 10))
					die("too many a8cat options");
				sprintf(tmp, " -%c", opt);
				strcat(pipe_command, tmp);
				break;

			default: print_help(); exit(1);
		}
	}

	if(optind >= argc) {
		if(isatty(fileno(stdin))) {
			fprintf(stderr, "%s: can't read binary data from a terminal\n", self);
			print_help();
			exit(1);
		}
		freopen(NULL, "rb", stdin);
		infile = stdin;
	} else {
		infile = fopen(argv[optind], "rb");
		if(!infile) {
			fprintf(stderr, "%s: ", self);
			perror(argv[optind]);
			exit(1);
		}
	}
}

void open_output() {
	if(check_only) {
		outfile = freopen("/dev/null", "wb", stdout);
		if(!outfile) {
			fprintf(stderr, "%s: ", self);
			perror("/dev/null");
			exit(1);
		}
	}
	if(raw_output) {
		if(isatty(fileno(stdout)))
			die("refusing to write raw ATASCII to a terminal");
		outfile = stdout;
	} else {
		if(verbose)
			fprintf(stderr, "using pipe for output: %s\n", pipe_command);
		outfile = popen(pipe_command, "w");
		if(!outfile) {
			perror(pipe_command);
			exit(1);
		}
		need_pclose = 1;
	}
}

int main(int argc, char **argv) {
	int proglen, lineno, rv = 0;

	set_self(argv[0]);

	parse_args(argc, argv);

	open_output();

	proglen = read_header();

	while( (lineno = next_line()) != -1 )
		if(verbose)
			fprintf(stderr, "line %d read OK\n", lineno);

	if(verbose)
		fprintf(stderr, "read %d bytes\n", bytes_read);;

	if(proglen == (bytes_read - 3)) {
		if(verbose)
			fprintf(stderr, "file size matches proglen\n");
	} else {
		fprintf(stderr, "%s: file size doesn't match length in header\n", self);
	}

	if(fgetc(infile) != EOF) {
		fprintf(stderr, "%s: trailing garbage at end of file\n", self);
	}

	if(unknowns) {
		fprintf(stderr, "%s: input has %d unknown tokens\n", self, unknowns);;
		rv = 2;
	}

	if(need_pclose) {
		if(pclose(outfile) != 0) {
			die("a8cat child process failed, do you have a8cat on your PATH?");
		}
	}

	exit(rv);
}