aboutsummaryrefslogtreecommitdiff
path: root/a8xd.c
blob: 1a4831249d0ceb518b853ae35caf1967e6cd9cae (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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <wchar.h>
#include <errno.h>

#include "atables.h"

/* printf() formats for lower/uppercase hex. */
#define LC_WORD_FMT "%04x"
#define LC_BYTE_FMT "%02x"
#define UC_WORD_FMT "%04X"
#define UC_BYTE_FMT "%02X"

/* colors. don't use black or white: one or the other is likely
   the terminal's text color already.
   don't use blue because it's hard to read. */
#define C_RED     1
#define C_GREEN   2
#define C_YELLOW  3
#define C_PURPLE  5
#define C_CYAN    6

/* translation table (see atables.c), choices are
   ata2utf, ics2utf, or magazine. */
const char **table = ata2utf;

/* hardcoded ANSI-style escape sequences, work fine on
	all modern terminal emulators. */
const char *inverse_on  = "\x1b[7m";
const char *inverse_off = "\x1b[0m";
const char *color_off = "\x1b[0m";

/* -u option changes these. */
const char *word_format = LC_WORD_FMT;
const char *byte_format = LC_BYTE_FMT;

/* command line options change these. */
int verbose = 0, color = 1, disp_offset = 0, maxlen = 0;
int seek_whence = 0, seekpos = 0, filepos = 0, limit = 0;
int graphics = 0, screencodes = 0, high_font = 0;

const char *self;

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);
}

void print_help(void) {
	printf("Usage:\n  %s [-a] [-f] [-g] [-i] [-l limit] [-m] [-o offset] [-s [-]seek] [-u] [-v] [file]\n", self);
	printf("   -a: Input is ANTIC screencodes (default is ATASCII).\n");
	printf("   -g: GR.1/2 style colorization.\n");
	printf("   -i: Input is XL intl charset (default is ATASCII).\n");
	printf("   -l: Stop after <limit> bytes.\n");
	printf("   -m: Monochrome (color off).\n");
	printf("   -o: Add <offset> to displayed byte positions.\n");
	printf("   -s: Seek <seek> bytes into input (with -, seek back from EOF).\n");
	printf("   -u: Uppercase hex digits (default is lowercase).\n");
	printf("   -v: Verbose debugging info.\n");
	printf("With no [file], or -, reads from stdin.\n");
}

int parse_num_arg(const char *arg) {
	int got;

	if(sscanf(arg, "0x%x", &got) != 1)
		if(sscanf(arg, "$%x", &got) != 1)
			if(sscanf(arg, "%d", &got) != 1) {
				fprintf(stderr, "%s: invalid numeric argument '%s'.", self, arg);
				exit(1);
			}

	return got;
}

void parse_seek_arg(const char *arg) {
	seek_whence = SEEK_SET;

	if(*arg == '-') {
		seek_whence = SEEK_END;
	}

	seekpos = parse_num_arg(arg);
}

int parse_offset_arg(const char *arg) {
	int got;

	got = parse_num_arg(arg);

	return got;
}

int parse_limit_arg(const char *arg) {
	int got;

	got = parse_num_arg(arg);
	if(got < 0)
		die("negative arguments to -l (limit) are not supported.");

	return got;
}

FILE *open_input(const char *file) {
	FILE *input;

	if(file[0] == '-' && file[1] == 0) {
		if(freopen(NULL, "rb", stdin)) {
			input = stdin;
		} else {
			fprintf(stderr, "%s: ", self);
			perror("(standard input)");
			return NULL;
		}
		if(verbose)
			fprintf(stderr, "%s: reading from standard input.\n", self);
	} else {
		if(!(input = fopen(file, "rb"))) {
			perror(file);
			return NULL;
		}
		if(verbose)
			fprintf(stderr, "%s: reading from file '%s'.\n", self, file);
	}

	return input;
}

int get_text_color(unsigned char c) {
	unsigned char c7 = c & 0x7f;

	if(c == 0 || c == 0x9b) {
		return C_RED;
	} else if((c7 >= 0x1b && c7 <= 0x1f) || (c7 >= 0x7d)) {
		/* cursor control characters:
			- 0x1b (esc)
			- 0x1c-0x1f (arrows), 0x9c-0x9f
			- 0x7d/0xf3 (cls, bell)
			- 0x7e, 0xfe (delete line, char)
			- 0x7f, 0xff (tab, insert char)
			*/
		return C_PURPLE;
	} else if(c7 < 32 || c7 == 0x60 || c7 == 0x7b) {
		return C_YELLOW;
	} else {
		return C_GREEN;
	}
}

int get_graphics_color(unsigned char c) {
	if(c < 0x20)
		return C_GREEN;
	else if(c >= 0x20 && c < 0x60)
		return C_YELLOW;
	else if(c >= 0x60 && c < 0x80)
		return C_GREEN;
	else if(c >= 0x80 && c < 0xa0)
		return C_RED;
	else if(c >= 0xa0 && c < 0xe0)
		return C_CYAN;
	else /* c >= 0xe0 */
		return C_RED;
}

char *get_color(unsigned char c) {
	int color;
	static char outbuf[32];

	if(graphics) {
		color = get_graphics_color(c);
	} else {
		color = get_text_color(c);
	}

	if(color) {
		sprintf(outbuf, "\x1b[0;3%dm", color);
	} else {
		outbuf[0] = '\0';
	}

	return outbuf;
}

/* displaying ATASCII (not screencodes) in GR.1 or GR.2:
   $00-$1F get displayed in green, as $20-$2F.
   $20-$5F get displayed in orange, as themselves.
   $60-$7F get displayed in green, as $40-$5F.
   for $80-$FF, same thing, but green is red, and orange is blue.
*/
unsigned char get_graphics(unsigned char c) {
	c &= 0x7f;
	if(c < 0x20)
		c += 0x20;
	else if(c >= 0x60)
		c -= 0x20;
	return c;
}

unsigned char get_high_graphics(unsigned char c) {
	c &= 0x7f;
	if(c >= 0x20 && c < 0x40)
		c -= 0x20;
	else if(c >= 0x40)
		c += 0x20;
	return c;
}

unsigned char screen2ata(unsigned char c) {
	unsigned char c7 = c & 0x7f;

	if(c7 < 0x40)
		return c + 0x20;
	if(c7 >= 0x40 && c7 < 0x60)
		return c - 0x40;
	return c;
}

void fake_seek(FILE *input) {
	char junkbuf[1024];
	int pos = 0, chunksize;

	if(verbose)
		fprintf(stderr, "%s: faking fseek() for stdin, skipping %d bytes.\n", self, seekpos);

	while(pos < seekpos) {
		chunksize = seekpos - pos;
		if(chunksize > 1024) chunksize = 1024;
		if(fread(junkbuf, 1, chunksize, input) != chunksize)
			die("can't seek past end of input");
		pos += chunksize;
	}
}

void seek_input(FILE *input) {
	if(verbose)
		fprintf(stderr, "%s: seeking %d bytes (whence = %d) in input.\n", self, seekpos, seek_whence);
	if(input == stdin) {
		/* can't fseek() a stream, fake it */
		if(seek_whence != SEEK_SET)
			die("can't seek from the end of standard input.");
		fake_seek(input);
	} else {
		if(fseek(input, seekpos, seek_whence) < 0) {
			fprintf(stderr, "%s: ", self);
			perror("fseek() failed");
			exit(1);
		}
		if((filepos = ftell(input)) < 0) {
			fprintf(stderr, "%s: ", self);
			perror("ftell() failed");
			exit(1);
		}
	}
}

void append_str_f(char *buf, int *pos, const char *fmt, const char *str) {
	*pos += sprintf(buf + *pos, fmt, str);
}

void append_str(char *buf, int *pos, const char *str) {
	append_str_f(buf, pos, "%s", str);
}

void append_hex(char *buf, int *pos, const unsigned char byte) {
	*pos += sprintf(buf + *pos, byte_format, byte);
}


void dump_line(const unsigned char *buf, int len) {
	char hex[1024], asc[1024];
	int hpos = 0, apos = 0, count = len;
	unsigned char c, inv;

	memset(hex, 0, sizeof(hex));
	memset(asc, 0, sizeof(asc));

	printf(word_format, filepos + disp_offset);
	fputs(":  ", stdout);

	while(len) {
		c = *buf;
		inv = !graphics && (c & 0x80);
		if(screencodes) c = screen2ata(c);

		hpos += printf("%s", get_color(c));
		if(inv) hpos += printf("%s", inverse_on);
		hpos += printf(byte_format, *buf); /* *buf here, not c! */
		if(color || inv) hpos += printf("%s", color_off);
		putchar(' ');
		hpos++;
		if(count - len == 7) {
			putchar(' ');
			hpos++;
		}

		if(color) append_str(asc, &apos, get_color(c));
		if(inv) append_str(asc, &apos, inverse_on);
		if(graphics) {
			if(high_font)
				c = get_high_graphics(c);
			else
				c = get_graphics(c);
		}
		apos += sprintf(asc + apos, "%s", table[c & 0x7f]);
		if(color || inv) append_str(asc, &apos, color_off);

		filepos++;
		buf++;
		len--;
	}

	/* what shall we use to fill the empty spaces? */
	if(count < 8) putchar(' ');
	for(; count < 16; count++) fputs("   ", stdout);

	printf(" %s\n", asc);
}

int a8xd(const char *file) {
	FILE *input;
	int c, len = 0, count = 0;
	unsigned char buf[16];

	if( !(input = open_input(file)) )
		return 1;

	if(seekpos) seek_input(input);

	while( (c = fgetc(input)) != EOF ) {
		if(limit && (count == limit)) break;
		count++;
		if(len && (len % 16 == 0)) {
			dump_line(buf, len);
			len = 0;
		}
		buf[len++] = c;
	}

	if(len) dump_line(buf, len);

	if(verbose)
		fprintf(stderr, "%s: dumped %d (0x%04x) bytes from '%s'.\n", self, count, count, file);

	fclose(input);
	return 0;
}

int main(int argc, char **argv) {
	int opt, result = 0;

	set_self(argv[0]);

	if(argc > 1 && strcmp(argv[1], "--help") == 0) {
		print_help();
		exit(0);
	}

	if(argc > 1 && strcmp(argv[1], "--version") == 0) {
		printf("%s %s\n", self, VERSION);
		exit(0);
	}

	while( (opt = getopt(argc, argv, "vhimus:o:l:gaf")) != -1) {
		switch(opt) {
			case 'v': verbose = 1; break;
			case 'h': print_help(); exit(0); break;
			case 'i': table = ics2utf; break;
			case 'm': color = 0; break;
			case 'u': word_format = UC_WORD_FMT; byte_format = UC_BYTE_FMT; break;
			case 's': parse_seek_arg(optarg); break;
			case 'o': disp_offset = parse_offset_arg(optarg); break;
			case 'l': limit = parse_limit_arg(optarg); break;
			case 'g': graphics = 1; break;
			case 'a': screencodes = 1; break;
			case 'f': graphics = high_font = 1; break;
			default: print_help(); exit(1); break;
		}
	}

	if(optind < argc - 1)
		die("Only one filename is supported.");

	if(optind >= argc)
		result = a8xd("-");
	else
		result = a8xd(argv[optind]);

	exit(result);
}