aboutsummaryrefslogtreecommitdiff
path: root/unmac65.c
blob: 42875bb2b9280c57ebe07c25f911518ebe7a884c (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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#ifndef VERSION
# define VERSION "???"
#endif

#ifndef TAG
# define TAG ""
#endif

#define SELF "unmac65"
#define BANNER SELF " v" VERSION " by B. Watson (WTFPL)\n\n"

#ifdef __CC65__
# ifdef __ATARI__
#  define ATARI8
# else
#  error "This program only supports Atari 8-bit when built with cc65"
/* Feel free to add support for other systems, if you need to. The main
	differences will be in fix_filename() and atari8_get_opts(). */
# endif
#else
# undef ATARI8
#endif

#define lsbmsb(lo, hi) ( (lo) | (hi << 8) )

/* use static filename buffers on A8, since we (probably) won't be
	getting them passed with argv. FIXME: I really ought to do bounds
	checking... */
#ifdef ATARI8
char infile[128], outfile[128];
#else
char *infile = NULL, *outfile = NULL;
#endif

FILE *input = NULL, *output = NULL;
int using_stdout = 1;

/* see handle_cli_opts() and/or atari8_get_opts() for these: */
char nl = '\n';
char no_numbers = 0;
char leading_tabs = 0;
char all_tabs = 0;
char omit_dots = 0;
char lcase_opcodes = 0;
char lcase_all = 0;
char dump_tokens = 0;
char add_quote = 0;

unsigned int renum_start, renum_line;
int renum_incr = 0;

typedef enum { CC_NONE, CC_UNPRINT, CC_ALL } chconst_opt_t;
chconst_opt_t chconsts_hex = 0;

/* options only available in the non-Atari8 ports */
#ifndef ATARI8
char deinverse = 0;
char found_inverse = 0;
char found_unprint = 0;
char inv_underscore = 0;
char inv_ansi = 0;
char unicode = 0;
#endif

/* dumpbuf[] really should be local to parse_one_line(), but
	cc65 won't let us make this a local var, it's too big. 1000 bytes
	is plenty (max line length is 256 bytes, we dump them in hex at
	3 chars each, plus 10-12 chars worth of formatting) */
char dumpbuf[1000];

/* number of bytes left to read (initialized from 4-byte m65 header,
	decremented by next_byte()). If this ever reaches 0 while there's
	more input, or if we get EOF while prog_bytes != 0, it's an error. */
unsigned int prog_bytes;

/* Guess what these are for? */
int line_number, old_line_number = -1;

char *opcode_tokens[] = {
	"ERROR -", /* 0x00 */
	".IF",
	".ELSE",
	".ENDIF",
	".MACRO",
	".ENDM",
	".TITLE",
	" ",
	".PAGE",
	".WORD",
	".ERROR",
	".BYTE",
	".SBYTE",
	".DBYTE",
	".END",
	".OPT",
	".TAB", /* 0x10 */
	".INCLUDE",
	".DS",
	".ORG",
	".EQU",
	"BRA",
	"TRB",
	"TSB",
	".FLOAT",
	".CBYTE",
	";",
	".LOCAL",
	".SET",
	"*=",
	"=",
	".=",
	"JSR", /* 0x20 */
	"JMP",
	"DEC",
	"INC",
	"LDX",
	"LDY",
	"STX",
	"STY",
	"CPX",
	"CPY",
	"BIT",
	"BRK",
	"CLC",
	"CLD",
	"CLI",
	"CLV",
	"DEX", /* 0x30 */
	"DEY",
	"INX",
	"INY",
	"NOP",
	"PHA",
	"PHP",
	"PLA",
	"PLP",
	"RTI",
	"RTS",
	"SEC",
	"SED",
	"SEI",
	"TAX",
	"TAY",
	"TSX", /* 0x40 */
	"TXA",
	"TXS",
	"TYA",
	"BCC",
	"BCS",
	"BEQ",
	"BMI",
	"BNE",
	"BPL",
	"BVC",
	"BVS",
	"ORA",
	"AND",
	"EOR",
	"ADC",
	"STA", /* 0x50 */
	"LDA",
	"CMP",
	"SBC",
	"ASL",
	"ROL",
	"LSR",
	"ROR",
	"",    /* 0x58 - the null opcode */
	"STZ",
	"DEA",
	"INA",
	"PHX",
	"PHY",
	"PLX",
	"PLY" /* 0x5f */
};

/* Special opcodes */
#define MAX_OPCODE 0x5f
#define NO_OPCODE 0x58

char *operand_tokens[] = {
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,  /* actually "'", special handling tho */ /* 0x0a, 10 decimal */
	"%$",
	"%",
	"*",
	" ",
	" ",
	"a", /* 0x10 */
	"q",
	"+",
	"-",
	"*", /* 0x14, 20 decimal */
	"/",
	"&",
	".DEF",
	"=",
	"<=",
	">=",
	"<>",
	">",
	"<",
	"-", /* 0x1e, 30 dec */
	"[",
	"]", /* 0x20 */
	".OR",
	".AND",
	".NOT",
	"!",
	"^",
	".REF",
	"\\",
	NULL, /* 0x28, 40 dec */
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	".REF",
	".DEF ", /* 0x30 */
	".NOT ",
	" .AND ", /* 0x32, 50 dec */
	" .OR ",
	" <",
	" >",
	",X)",
	"),Y",
	",Y",
	",X",
	")",
	",",    /* 0x3b, 59, the null operand */
	"\x1b", /* 0x3c, 60, ASCII escape, chr$(27)? */
	",",
	"#",
	"A",
	"(", /* 0x40, 64 dec */
	"\"",
	"$",
	"Q",
	"NO",
	"NO ",
	"OBJ", /* 0x46, 70 dec */
	"ERR",
	"EJECT",
	"LIST",
	"XREF",
	"MLIST",
	"CLIST",
	"NUM", /* 0x4d, 77 dec */
	/* "M", */ /* maybe? I think not... */
};

/* Special operands */
#define MAX_OPERAND 0x4d
#define NO_OPERAND 0x3b

#define HEXWORD_PREFIX 5
#define HEXBYTE_PREFIX 6

#define DECWORD_PREFIX 7
#define DECBYTE_PREFIX 8

#define CHAR_CONST_PREFIX 0x0a

/* Functions */
void print_label_byte(unsigned char byte, FILE *output) {
	putc((lcase_all ? tolower(byte) : byte), output);
}

#ifdef ATARI8
void print_string_byte(unsigned char byte, FILE *output) {
	putc(byte, output);
}
#else
char *unicode_table[] = {
	"♥", "┣", "┃", "┛", "┫", "┓", "╱", "╲", "◢", "▗", "◣", "▝", "▘", "▔", "▁", "▖",
	"♣", "┏", "━", "╋", "⚫ ", "▄", "▎", "┳", "┻", "▌", "┗", "␛", "↑", "↓", "←", "→",
	" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
	"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
	"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_",
	"◆", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
	"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "♠", "|", "↰", "◀", "▶"
};

/* This is probably more complex than it needs to be. */
void start_inverse(FILE *output) {
	if(inv_underscore) {
		fputs("_\x08", output);
	} else if(inv_ansi) {
		fputs("\x1b[7m", output);
	}
}

void end_inverse(FILE *output) {
	if(inv_underscore) {
		/* do nothing */;
	} else if(inv_ansi) {
		fputs("\x1b[0m", output);
	}
}

void print_string_byte(unsigned char byte, FILE *output) {
	int inverse;

	inverse = (byte >= 0x80);
	byte &= 0x7f;
	if(inverse && deinverse) {
		found_inverse++;
		inverse = 0;
	}

	if(inverse) start_inverse(output);
	if(unicode) {
		fputs(unicode_table[byte], output);
	} else {
		if(inv_underscore || inv_ansi) {
			putc(byte, output);
		} else {
			putc(inverse ? (byte | 0x80) : byte, output);
			found_inverse += inverse;
		}
		if(byte < 0x20) found_unprint++;
	}
	if(inverse) end_inverse(output);
}

#endif

#define print_comment_byte(x, y) print_string_byte((lcase_all ? tolower(x) : x), y)

void print_hex_byte(unsigned char byte, FILE *output) {
	fprintf(output, (lcase_opcodes ? "$%02x" : "$%02X"), byte);
}

void print_hex_word(int word, FILE *output) {
	fprintf(output, (lcase_opcodes ? "$%04x" : "$%04X"), word);
}


void closeall() {
	if(output && !using_stdout) {
		fclose(output);
		output = NULL;
	}

	if(input) {
		fclose(input);
		input = NULL;
	}
}

void exit_cleanly(int status) {
#ifdef ATARI8
	if(status) {
		puts("Press Return to exit:");
		fflush(stdout);
		fgets(infile, 80, stdin);
	}
#endif

	closeall();
	exit(status);
}

unsigned char next_byte() {
	int c;

	if(prog_bytes <= 0) {
		fprintf(stderr, SELF ": corrupt or truncated file?\n");
		exit_cleanly(1);
	}

	--prog_bytes;
	c = getc(input);
	if(c == EOF && prog_bytes != 0) {
		fprintf(stderr, SELF ": unexpected EOF\n");
		exit_cleanly(1);
	}

	if(prog_bytes == 0 && getc(input) != EOF) {
		fprintf(stderr, SELF ": ignoring extra junk at EOF\n");
	}

	return (unsigned char)c;
}

void parse_header(void) {
	unsigned char inbuf[4];
	int bytes = fread(inbuf, 1, 4, input);
	if(bytes < 4) {
		if(ferror(input)) {
			perror(infile);
		} else {
			fprintf(stderr, SELF ": file is too short (%d bytes)\n", bytes);
		}
		exit_cleanly(1);
	}

	if(inbuf[0] != 0xfe || inbuf[1] != 0xfe) {
		fprintf(stderr, SELF ": not a mac/65 file (missing $FEFE header)\n");
		exit_cleanly(1);
	}

	prog_bytes = lsbmsb(inbuf[2], inbuf[3]);
	if(!prog_bytes) fprintf(stderr, SELF ": file is valid but contains no lines of code\n");

	if(dump_tokens) {
		fprintf(output, ";; Mac/65 header: ");
		for(bytes = 0; bytes < 4; bytes++)
			fprintf(output, "%02X ", inbuf[bytes]);
		fprintf(output, "length %d\n", prog_bytes);
	}
}

void print_opcode(unsigned char byte) { 
	char *opc = opcode_tokens[byte]; 

	putc((leading_tabs ? '\t' : ' '), output); 
	if(omit_dots && opc[0] == '.' && isalpha(opc[1])) 
		++opc; 

	if(lcase_opcodes) { 
		char buf[10]; 
		char *p = buf; 
		strcpy(buf, opc); 
		while(*p) { 
			*p = tolower(*p); 
			++p; 
		} 
		opc = buf; 
	} 

	fputs(opc, output); 
	putc((all_tabs ? '\t' : ' '), output); 
}

/* Parser states. The parser is hand-rolled and kind of ugly. */
#define ST_NEED_OPCODE 1
#define ST_IN_LABEL 2
#define ST_IN_OPERAND 3
#define ST_IN_OPSTRING 4
#define ST_IN_COMMENT 5
#define ST_ERROR 6
#define ST_IN_HEXBYTE 7
#define ST_IN_HEXWORD_LSB 8
#define ST_IN_HEXWORD_MSB 9
#define ST_IN_DECBYTE 10
#define ST_IN_DECWORD_LSB 11
#define ST_IN_DECWORD_MSB 12
#define ST_IN_CHAR_CONST 13

int handle_operand(unsigned char byte) {
	char *operand = NULL;

	if(byte > 0x80)
		return ST_IN_OPSTRING;

	if(byte == NO_OPERAND) {
		putc((all_tabs ? '\t' : ' '), output);
		return ST_IN_COMMENT;
	}

	if(byte > MAX_OPERAND)
		return ST_ERROR;

	operand = operand_tokens[byte];
	if(operand) {
		fputs(operand, output);
		return ST_IN_OPERAND;
	}

	switch(byte) {
		case HEXWORD_PREFIX:
			return ST_IN_HEXWORD_LSB;

		case HEXBYTE_PREFIX:
			return ST_IN_HEXBYTE;

		case DECWORD_PREFIX:
			return ST_IN_DECWORD_LSB;

		case DECBYTE_PREFIX:
			return ST_IN_DECBYTE;

		case CHAR_CONST_PREFIX:
			return ST_IN_CHAR_CONST;

		/* TODO: find out if any other specials exist */

		default:
			fprintf(output, "[$%02X?]", byte);
			return ST_IN_OPERAND;
	}
}

void parse_one_line() {
	int state = ST_NEED_OPCODE;
	int string_len = 0;
	int line_bytes;
	int lsb = 0;
	unsigned char byte, linenum_lo, linenum_hi;
	char hexbuf[20];

#ifndef ATARI8
	found_inverse = found_unprint = 0;
#endif

	linenum_lo = next_byte();
	linenum_hi = next_byte();

	if(renum_incr) {
		line_number = renum_line;
		renum_line += renum_incr;
	} else {
		line_number = lsbmsb(linenum_lo, linenum_hi);
	}

	if(line_number <= old_line_number) {
		fprintf(stderr, SELF ": line #%d <= prev line #%d\n",
				line_number, old_line_number);
	}

	line_bytes = next_byte() - 3;

	if(dump_tokens)
		sprintf(dumpbuf, ";; %d (%02X %02X, len %02X):",
				line_number, linenum_lo, linenum_hi, line_bytes + 3);

	if(!no_numbers) {
		char *format = "%06d ";

		/* duplicate mac65's weird line number formatting */
		if(line_number < 100)
			format = "%02d ";
		else if(line_number < 10000)
			format = "%04d ";

		fprintf(output, format, line_number);
	}

	while(line_bytes) {
		byte = next_byte();
		--line_bytes;

		if(dump_tokens) {
			sprintf(hexbuf, " %02X", byte);
			strcat(dumpbuf, hexbuf);
		}

		switch(state) {
			case ST_NEED_OPCODE:
				if(byte > 0x80) {
					string_len = byte & 0x7f;
					state = ST_IN_LABEL;
				} else if(byte == NO_OPCODE) {
					state = ST_IN_COMMENT;
				} else if(byte <= MAX_OPCODE) {
					print_opcode(byte);
					state = ST_IN_OPERAND;
				} else {
					state = ST_ERROR;
				}
				break;

			case ST_IN_LABEL:
				print_label_byte(byte, output);
				if(--string_len == 0)
					state = ST_NEED_OPCODE; /* TODO: 2 labels is error, detect */
				break;

			case ST_IN_OPERAND:
				state = handle_operand(byte);
				if(state == ST_IN_OPSTRING) string_len = byte & 0x7f;
				break;

			case ST_IN_OPSTRING:
				print_string_byte(byte, output);
				if(--string_len == 0)
					state = ST_IN_OPERAND;
				break;

			case ST_IN_COMMENT:
				print_comment_byte(byte, output);
				break;

			case ST_ERROR:
				fprintf(output, "<$%02X?>", byte);
				break;

			case ST_IN_HEXBYTE:
				print_hex_byte(byte, output);
				state = ST_IN_OPERAND;
				break;

			case ST_IN_HEXWORD_LSB:
				lsb = byte;
				state = ST_IN_HEXWORD_MSB;
				break;

			case ST_IN_HEXWORD_MSB:
				print_hex_word(lsbmsb(lsb, byte), output);
				state = ST_IN_OPERAND;
				break;

			case ST_IN_DECBYTE:
				fprintf(output, "%d", byte);
				state = ST_IN_OPERAND;
				break;

			case ST_IN_DECWORD_LSB:
				lsb = byte;
				state = ST_IN_DECWORD_MSB;
				break;

			case ST_IN_DECWORD_MSB:
				fprintf(output, "%d", lsbmsb(lsb, byte));
				state = ST_IN_OPERAND;
				break;

			case ST_IN_CHAR_CONST:
#ifndef ATARI8
				if(
						chconsts_hex == CC_ALL ||
						(chconsts_hex == CC_UNPRINT && (byte < 0x20 || byte > 0x7f))
					)
				{
					print_hex_byte(byte, output);
					state = ST_IN_OPERAND;
					break;
				}
#endif
				putc('\'', output);
				print_string_byte(byte, output);
				if(add_quote)
					putc('\'', output);
				state = ST_IN_OPERAND;
				break;

			default:
				fprintf(stderr, SELF ": internal error, state %d\n", state);
				state = ST_ERROR;
				break;
		}
	}

#ifndef ATARI8
	if(found_inverse) {
		fprintf(stderr, SELF ": line %d contains %d inverse ATASCII characters >= $80\n", line_number, found_inverse);
		if(deinverse) printf("; XXX inverse (%d chars)", found_inverse);
	}
	if(found_unprint) fprintf(stderr, SELF ": line %d contains %d non-printable ATASCII characters <= $1F\n", line_number, found_unprint);
#endif

#ifdef CYGWIN_NEWLINE_HACK
	if(nl == '\n') putc('\r', output);
#endif
	putc(nl, output);

	if(dump_tokens) {
		fputs(dumpbuf, output);
#ifdef CYGWIN_NEWLINE_HACK
		if(nl == '\n') putc('\r', output);
#endif
		putc(nl, output);
	}

	if(ferror(output)) {
		perror(SELF);
		exit_cleanly(1);
	}
}

void parse_lines() {
	old_line_number = -1;

	while(prog_bytes)
		parse_one_line();

	closeall();
}

#ifdef ATARI8
/* get rid of trailing newline, make uppercase... */
void cleanstring(char *s) {
	while(*s) {
		*s = toupper(*s);
		if(*s == '\n') *s = 0;
		++s;
	}
}

/* add .M65 if no extension entered, add leading D: if
	no device name. */
void fix_filename(char *src, char *dst, char *ext) {
	if(!strrchr(src, '.'))
		strcat(src, ext);
 
	dst[0] = '\0';

	if(!strrchr(src, ':'))
		strcat(dst, "D:");

	strcat(dst, src);
}

void prompt_for_opt(char *prompt, char *opt) {
	char buffer[10];

	printf("%s [%c/%c]?",
			prompt,
			(*opt ? 'Y' : 'y'),
			(*opt ? 'n' : 'N'));

	fflush(stdout);
	fgets(buffer, 10, stdin);
	cleanstring(buffer);

	if(buffer[0] == 'Y' || buffer[0] == 'y')
		*opt = 1;
	else if(buffer[0] == 'N' || buffer[0] == 'n')
		*opt = 0;
	/* else leave unchanged */
}

void prompt_for_str(char *prompt, char *buf) {
	fputs(prompt, stdout);
	putc('?', stdout);
	fflush(stdout);
	fgets(buf, 120, stdin);
	cleanstring(buf);
}

void atari8_get_opts() {
	char buffer[128];
	char other = 0;

	prompt_for_str("M65 file or Return to quit", buffer);

	if(!buffer[0]) {
		prompt_for_opt("Really quit", &other);
		if(other)
			exit_cleanly(0);
		else
			other = 0;
	}

	fix_filename(buffer, infile, ".M65");

	prompt_for_str("Output file (Return for E:)", buffer);

	if(!buffer[0]) {
		using_stdout = 1;
	} else {
		using_stdout = 0;
		fix_filename(buffer, outfile, ".TXT");
	}

	prompt_for_opt("Set other options", &other);
	if(other) {
		prompt_for_opt("Omit line numbers", &no_numbers);
		if(!no_numbers) {
			prompt_for_opt("Renumber lines", &other);
			if(other) {
				prompt_for_str("Starting line number", buffer);
				renum_start = atoi(buffer);
				prompt_for_str("Line num increment (Return = 10)", buffer);
				renum_incr = atoi(buffer);
				if(renum_incr < 1) renum_incr = 10;
			} else {
				renum_incr = 0;
			}
		}
		prompt_for_opt("Omit . (dots) from pseudo-ops", &omit_dots);

		prompt_for_opt("Lowercase everything", &lcase_all);
		if(!lcase_all)
			prompt_for_opt("Lowercase mnemonics, hex", &lcase_opcodes);

		prompt_for_opt("Replace leading spaces w/tabs", &leading_tabs);
		if(!leading_tabs)
			prompt_for_opt("Replace lead+inner spaces w/tabs", &all_tabs);

		other = (chconsts_hex == CC_UNPRINT);
		prompt_for_opt("Unprintable char consts to hex", &other);
		if(other) {
			chconsts_hex = CC_UNPRINT;
		} else {
			other = (chconsts_hex == CC_ALL);
			prompt_for_opt("All char consts to hex", &other);
			if(other)
				chconsts_hex = CC_ALL;
			else
				chconsts_hex = CC_NONE;
		}

		if(chconsts_hex != CC_ALL)
			prompt_for_opt("Close quote ' for char consts", &add_quote);

		prompt_for_opt("Dump tokens in hex", &dump_tokens);
	}

	fflush(stdout);
}
#endif

void usage() {
	fprintf(stderr, "usage: " SELF " [options] inputfile\n\n");
	fprintf(stderr, "options:\n");
#ifndef ATARI8
	fprintf(stderr, " -a  Use ATASCII EOLs\n");
	fprintf(stderr, " -c  Convert non-printable char constants to hex\n");
	fprintf(stderr, "-cc  Convert all char constants to hex\n");
#endif
	fprintf(stderr, " -e nnn[,i]   Renumber starting with nnn [increment i]\n");
	fprintf(stderr, " -h  Help (this text)\n");
#ifndef ATARI8
	fprintf(stderr, " -i  Convert inverse video to normal\n");
#endif
	fprintf(stderr, " -l  Lowercase mnemonics, hex constants\n");
	fprintf(stderr, "-la  Lowercase all, including comments\n");
	fprintf(stderr, " -n  No line numbers in output\n");
	fprintf(stderr, " -o [file]   Output to file (default = stdout)\n");
	fprintf(stderr, " -p  Omit leading . (period) from pseudo-ops\n");
	fprintf(stderr, " -q  Add closing quote (') to character constants\n");
	fprintf(stderr, " -t  Replace leading spaces with tabs\n");
	fprintf(stderr, "-ta  Replace spaces between all fields with tabs\n");
	fprintf(stderr, " -v  Verbose output (dump tokens)\n");
#ifndef ATARI8
	fprintf(stderr, " -m  Print inverse video as underlined\n");
	fprintf(stderr, " -r  Print inverse video as ANSI reverse video\n");
	fprintf(stderr, " -u  Print ATASCII as Unicode/UTF-8\n");
#endif
	exit(1);
}

void get_renum_args(char *arg) {
	renum_incr = 10;
	/* atoi() doesn't detect errors, so: */
	if(!arg || arg[0] > '9' || arg[0] < '0')
		usage();
	renum_start = atoi(arg);
	arg = strchr(arg, ',');
	if(arg) renum_incr = atoi(++arg);
	if(!renum_incr || renum_incr < 0) usage();
}

/* TODO: support a few more -options
	A lot of the fancier options I wanted to add, would require
	a full parser for the grammar. I've avoided this partly because
	it's more work, and partly because I dunno how well yacc/bison would
	play with cc65...
*/
void handle_cli_opts(int argc, char **argv) {
#ifdef ATARI8
	infile[0] = '\0';
#endif

	while(++argv, --argc) {
		if(argv[0][0] == '-') {
			switch(tolower(argv[0][1])) {
#ifndef ATARI8
				case 'a':
					nl = 0x9b;
					if(argv[0][2]) usage();
					break;

				case 'i':
					chconsts_hex = CC_UNPRINT;
					deinverse = 1;
					if(argv[0][2]) usage();
					break;

				case 'm':
					inv_underscore = 1;
					if(argv[0][2]) usage();
					break;

				case 'r':
					inv_ansi = 1;
					if(argv[0][2]) usage();
					break;

				case 'u':
					unicode = 1;
					if(argv[0][2]) usage();
					break;
#endif
				case 'c':
					chconsts_hex = CC_UNPRINT;
					if(argv[0][2] == 'C' || argv[0][2] == 'c')
						chconsts_hex = CC_ALL;
					else if(argv[0][2]) usage();
					break;

				case 'e':
					if(argv[0][2]) usage();
					if(!argv[1]) usage();
					get_renum_args(argv[1]);
					argv++, argc--;
					break;

				case 'n':
					no_numbers = 1;
					if(argv[0][2]) usage();
					break;

				case 'h':
					usage();
					break;

				case 'l':
					lcase_opcodes = 1;
					if(argv[0][2] == 'A' || argv[0][2] == 'a')
						lcase_all = 1;
					else if(argv[0][2]) usage();
					break;

				case 'p':
					omit_dots = 1;
					if(argv[0][2]) usage();
					break;

				case 'q':
					add_quote = 1;
					if(argv[0][2]) usage();
					break;

				case 't':
					leading_tabs = 1;
					if(argv[0][2] == 'A' || argv[0][2] == 'a')
						all_tabs = 1;
					else if(argv[0][2]) usage();
					break;

				case 'v':
					dump_tokens = 1;
					if(argv[0][2]) usage();
					break;

				case 'o':
					if(argv[0][2]) {
#ifdef ATARI8
						strcpy(outfile, &argv[0][2]);
#else
						outfile = &argv[0][2];
#endif
					} else if(argc == 1) {
						usage();
					} else {
						++argv, --argc;
#ifdef ATARI8
						strcpy(outfile, argv[0]);
#else
						outfile = argv[0];
#endif
					}
					using_stdout = 0;
					break;

				default:
					usage();
					break;
			}
		} else {
#ifdef ATARI8
			if(infile[0])
				usage();
			else
				strcpy(infile, argv[0]);
#else
			if(infile)
				usage();
			else
				infile = argv[0];
#endif
		}
	}

	if(!infile) usage();
}

int main(int argc, char **argv) {
	fputs(BANNER, stderr);

#ifdef ATARI8
	while(1) {
	if(argc < 2) {
		atari8_get_opts();
	} else {
		handle_cli_opts(argc, argv);
		argc = 1;
	}
#else
	handle_cli_opts(argc, argv);
#endif

	input = fopen(infile, "rb");
	if(!input) {
		perror(infile);
		exit_cleanly(1);
	}

	if(using_stdout) {
		output = stdout;
	} else {
		output = fopen(outfile, "w");
		if(!output) {
			perror(outfile);
			exit_cleanly(1);
		}
	}

	if(renum_incr) renum_line = renum_start;
	parse_header();
	parse_lines();

#ifdef ATARI8
	}
#endif

	exit_cleanly(0);
	return 0; /* to shut gcc up... */
}