~rabbits/lin6

ref: 6f5ac0e6cce46545ac8556594aa2864f695b8972 lin6/lin6.c -rw-r--r-- 7.0 KiB
6f5ac0e6Devine Lu Linvega * 1 year, 27 days ago
                                                                                
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
/* 
  Lin6
  Version 1.3
  https://wiki.xxiivv.com/lin6

  Variable names must always be lowercase.
  Variable names are always padded to tab1.
  Variable length are always padded to col25.
  Variable comments are always padded to col32.
  Constant names must always be uppercase.
  Constant names are always padded to tab1.
  Constant length are always padded to col21.
  Constant comments are always padded to col32.
  Label names must always be capitalized.
  Label names always end with :.
  Label names are always preceeded with a linebreak.
  Label comments are never padded.
  Directive names are always padded to tab1.
  Directive names are always lowercase.
  Directive comments are never padded.
  Opcode names are always uppercase.
  Opcode names are always padded to tab1.
  Opcode comments are always padded to col32.
  Inline comments are always padded to tab1.
  Spacing comments are always preceeded and followed with a linebreak.
*/

#include <stdio.h>

#define VERSION "1.3"
#define BUFLEN 512

char* OPCODES[] = {"ADC", "AND", "ASL", "BCC", "BCS", "BEQ", "BIT", "BMI",
                   "BNE", "BPL", "BRK", "BVC", "BVS", "CLC", "CLD", "CLI",
                   "CLV", "CMP", "CPX", "CPY", "DEC", "DEX", "DEY", "EOR",
                   "INC", "INX", "INY", "JMP", "JSR", "LDA", "LDX", "LDY",
                   "LSR", "NOP", "ORA", "PHA", "PHP", "PLA", "PLP", "ROL",
                   "ROR", "RTI", "RTS", "SBC", "SEC", "SED", "SEI", "STA",
                   "STX", "STY", "TAX", "TAY", "TSX", "TXA", "TXS", "TYA"};

int skipped = 0;

int
cisp(char c)
{
	return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

int
clca(int c)
{
	return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
}

int
cuca(char c)
{
	return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
}

int
ciuc(char c)
{
	return c == cuca(c);
}

int
cilc(char c)
{
	return c == clca(c);
}

char*
sstr(char* src, char* dest, int from, int to)
{
	int i;
	char *a = (char*)src + from, *b = (char*)dest;
	for(i = 0; i < to; i++)
		b[i] = a[i];
	dest[to] = '\0';
	return dest;
}

int
slen(char* s)
{
	int n = 0;
	while(s[n] != '\0' && s[++n])
		;
	return n;
}

int
silc(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(!cilc(s[i]))
			return 0;
	return 1;
}

int
siuc(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(!ciuc(s[i]))
			return 0;
	return 1;
}

int
cpos(char* s, char c)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(s[i] == c)
			return i;
	return -1;
}

char*
scat(char* dest, const char* src)
{
	char* ptr = dest + slen(dest);
	while(*src != '\0')
		*ptr++ = *src++;
	*ptr = '\0';
	return dest;
}

int
scmp(char* a, char* b)
{
	int i, l = slen(a);
	if(l != slen(b))
		return 0;
	for(i = 0; i < l; ++i)
		if(a[i] != b[i])
			return 0;
	return 1;
}

char*
suca(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = cuca(s[i]);
	return s;
}

char*
slca(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = clca(s[i]);
	return s;
}

char*
strm(char* s)
{
	char* end;
	while(cisp(*s))
		s++;
	if(*s == 0)
		return s;
	end = s + slen(s) - 1;
	while(end > s && cisp(*end))
		end--;
	end[1] = '\0';
	return s;
}

int
spos(char* a, char* b)
{
	int i, j, alen = slen(a), blen = slen(b);
	for(i = 0; i < alen; i++) {
		for(j = 0; j < blen; j++) {
			if(a[i + j] == '\0')
				return -1;
			if(a[i + j] != b[j])
				break;
			if(j == blen - 1)
				return i;
		}
	}
	return -1;
}

char*
scpy(char* src, char* dest)
{
	int i = 0;
	while((dest[i] = src[i]) != '\0')
		i++;
	return dest;
}

void
setkey(char* src, char* dst)
{
	sstr(src, dst, 0, cpos(src, ' ') > 0 ? cpos(src, ' ') : slen(src));
}

void
setval(char* src, char* dst)
{
	int step = 0, i, comment = cpos(src, ';'), len = slen(src);
	if(comment > -1)
		len = comment;
	for(i = 0; i < len; i++) {
		if(src[i] != ' ' && step == 1)
			break;
		else if(src[i] == ' ')
			step = 1;
	}
	sstr(src, dst, i, len - i);
	scpy(strm(dst), dst);
}

void
setcmt(char* src, char* dst)
{
	if(cpos(src, ';') > 0)
		sstr(src, dst, cpos(src, ';'), slen(src) - cpos(src, ';'));
	else
		scpy("", dst);
	scpy(strm(dst), dst);
}

int
islabel(char* line)
{
	int i;
	for(i = 0; i < slen(line); i++) {
		if(line[i] == ' ' || line[i] == ';')
			return 0;
		if(line[i] == ':')
			return 1;
	}
	return 0;
}

int
isopcode(char* line)
{
	int i;
	char opcode[4];
	suca(sstr(line, opcode, 0, 3));
	for(i = 0; i < 56; i++)
		if(scmp(OPCODES[i], opcode))
			return 1;
	return 0;
}

int
iscomment(char* line)
{
	return line[0] == ';';
}

int
isdirective(char* line)
{
	return line[0] == '.';
}

int
isvariable(char* line)
{
	return spos(line, ".dsb") > 0;
}

int
isconstant(char* line)
{
	return spos(line, ".equ") > 0;
}

void
dolabel(char* src)
{
	char key[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	setcmt(src, cmt);
	key[0] = cuca(key[0]);
	if(key[0] == '@' || skipped)
		printf("%s %s\n", key, cmt);
	else
		printf("\n%s %s\n", key, cmt);
	skipped = 0;
}

void
doopcode(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	suca(key);
	setval(src, val);
	setcmt(src, cmt);
	if(slen(cmt) > 1)
		printf("\t%s %-30s %s\n", key, val, cmt);
	else
		printf("\t%s %s\n", key, val);
	skipped = 0;
}

void
docomment(char* src)
{
	if(src[0] == src[1]) {
		printf("\n%s\n\n", src);
		skipped = 1;
	} else {
		printf("\t%s\n", src);
		skipped = 0;
	}
}

void
dodirective(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	slca(key);
	setval(src, val);
	setcmt(src, cmt);
	if(slen(cmt) > 1)
		printf("\t%s %s %s\n", key, val, cmt);
	else
		printf("\t%s %s\n", key, val);
	skipped = 0;
}

void
dovariable(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	slca(key);
	setval(src, val);
	setcmt(src, cmt);
	if(slen(cmt) > 1)
		printf("\t%-23s %-10s %s\n", key, val, cmt);
	else
		printf("\t%-23s %s\n", key, val);
	skipped = 0;
}

void
doconstant(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	suca(key);
	setval(src, val);
	setcmt(src, cmt);
	printf("\t%-23s %-10s %s\n", key, val, cmt);
	skipped = 0;
}

void
lint(FILE* f)
{
	char line[BUFLEN], trimed[BUFLEN];
	while(fgets(line, BUFLEN, f)) {
		scpy(strm(line), trimed);
		if(slen(trimed) < 3)
			continue;
		if(islabel(trimed))
			dolabel(trimed);
		else if(isopcode(trimed))
			doopcode(trimed);
		else if(iscomment(trimed))
			docomment(trimed);
		else if(isdirective(trimed))
			dodirective(trimed);
		else if(isvariable(trimed))
			dovariable(trimed);
		else if(isconstant(trimed))
			doconstant(trimed);
		else
			printf("%s\n", line);
	}
	fclose(f);
}

int
version(void)
{
	puts("Lin6 " VERSION);
	return 0;
}

int
help(void)
{
	version();
	puts("");
	puts("A tool to format 6502 assembly code.\n");
	puts("Usage:  Lin6 [-options] [<file> ...]\n");
	puts("\t-?\tShow this help");
	puts("\t-v\tShow version");
	puts("See README for more info.\n");
	return 0;
}

int
main(int argc, char* argv[])
{
	FILE* f;
	int i;
	if(argc < 2)
		return help();
	for(i = 1; i < argc; i++) {
		if(argv[i][1] == '?')
			return help();
		else if(argv[i][1] == 'v')
			return version();
		else {
			f = fopen(argv[i], "r");
			if(f != NULL)
				lint(f);
		}
	}
	return 0;
}