aboutsummaryrefslogtreecommitdiff
path: root/dumpbas.c
blob: 2888b52736a20968991a6a3ecb9bdfad3c6338bd (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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "bas.h"

int startlineno = 0;
int endlineno = 32768;

/* dump tokens for each line in a BASIC program. easier to read than
   a plain hex dump. */
void print_help(void) {
	fprintf(stderr, "Usage: %s [-v] [-s start-lineno] [-e end-lineno] <inputfile>\n", self);
}

unsigned short getlineno(char opt, const char *arg) {
	int lineno;
	char *e;

	lineno = (int)strtol(arg, &e, 10);

	if(*e) {
		fprintf(stderr, "%s: Invalid line number for -%c option: %s is not a number.\n",
				self, opt, arg);
		exit(1);
	}

	if(lineno < 0 || lineno > 32767) {
		fprintf(stderr, "%s: Invalid line number for -%c option: %d > 32767.\n",
				self, opt, lineno);
		exit(1);
	}

	return ((unsigned short)lineno);
}

void parse_args(int argc, char **argv) {
	int opt;

	while( (opt = getopt(argc, argv, "vs:e:l:")) != -1) {
		switch(opt) {
			case 'v': verbose = 1; break;
			case 's': startlineno = getlineno(opt, optarg); break;
			case 'e': endlineno   = getlineno(opt, optarg); break;
			case 'l': startlineno = getlineno(opt, optarg);
						 endlineno   = startlineno; break;
			default:
				print_help();
				exit(1);
		}
	}

	if(optind >= argc)
		die("No input file given (use - for stdin).");
	else
		open_input(argv[optind]);
}

void print_atascii(unsigned char c) {
	if(c & 0x80) {
		putchar('|');
		c &= 0x7f;
	}

	if(c < 32) {
		putchar('^');
		c += 0x40;
	}

	if(c == 0x7f)
		printf("del");
	else
		putchar(c);
	putchar('/');
}

/* REM, DATA, ERROR lines are terminated by $9B, a real EOL, not
   the BASIC token. Since they're strings, print them in ASCII too. */
int handle_text_stmt(int pos) {
	unsigned char c;

	do {
		c = program[pos];
		print_atascii(c);
		printf("%02x ", c);
		pos++;
	} while(c != 0x9b);

	return pos;
}

int handle_cmd(int pos) {
	unsigned char tok = program[pos];

	printf("!%02x ", tok);
	switch(tok) {
		case CMD_REM:
		case CMD_DATA:
		case CMD_ERROR:
			return handle_text_stmt(pos + 1);
		default:
			return pos + 1;
	}
}

void handle_string(int pos) {
	int i, len;
	len = program[pos + 1];
	printf("$%02x =%02x \"", program[pos], len);
	for(i = pos; i < pos + len; i++) {
		unsigned char c = program[i + 2];
		print_atascii(c);
		printf("%02x%c", c, (i == (pos + len - 1) ? '"' : ' '));
	}
	putchar(' ');
}

void handle_num(int pos) {
	int i;
	printf("#%02x [", program[pos]);
	for(i = 0; i < 6; i++)
		printf("%02x%c", program[pos + 1 + i], (i == 5 ? ']' : ' '));
	putchar(' ');
}

/* sorry, this is horrid, more like assembly than C. */
int main(int argc, char **argv) {
	int linepos, nextpos, offset, soffset, lineno, pos, end, tok;

	set_self(*argv);
	parse_general_args(argc, argv, print_help);
	parse_args(argc, argv);

	readfile();
	parse_header();

	linepos = codestart;
	while(linepos < filelen) { /* loop over lines */
		lineno = getword(linepos);
		offset = program[linepos + 2];
		nextpos = linepos + offset;

		if(offset < 6)
			die("Can't dump a protected program, unprotect it first.");

		if(lineno < startlineno) {
			linepos = nextpos;
			continue;
		}

		/* line header */
		printf("%5d@%04x (%02x %02x): ^%02x ",
				lineno, linepos, program[linepos], program[linepos + 1], offset);

		pos = linepos + 3;
		while(pos < nextpos) { /* loop over statements within a line */
			soffset = program[pos];
			end = linepos + soffset;

			while(pos < end) {  /* loop over tokens within a statement */
				printf("\n    >%02x ", program[pos++]); /* offset */
				pos = handle_cmd(pos++); /* 1st token is the command */

				while(pos < end) {     /* loop over operators */
					tok = program[pos];
					switch(tok) {
						case OP_NUMCONST:
							handle_num(pos);
							pos += 7;
							break;
						case OP_STRCONST:
							handle_string(pos);
							pos += program[pos + 1] + 2;
							break;
						default:
							printf("%02x", program[pos]);
							if(pos == (end - 1) && tok == OP_EOS)
								putchar(':');
							else
								putchar(' ');
							pos++;
							break;
					}
				}
			}
		}

		putchar('\n');

		if(lineno == endlineno) break;
		linepos = nextpos;
	}

	return 0;
}