1 /* printcbm -- A part of xa65 - 65xx/65816 cross-assembler and utility suite
2  * list CBM BASIC programs
3  *
4  * Copyright (C) 1989-1997 Andr� Fachat (a.fachat@physik.tu-chemnitz.de)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "version.h"
26 
27 #define programname	"printcbm"
28 #define progversion	"v1.0.0"
29 #define author		"Written by Andre Fachat"
30 #define copyright	"Copyright (C) 1997-2002 Andre Fachat."
31 
32 char *cmd[] = {
33 	"end", "for", "next", "data", "input#", "input", "dim", "read",
34 	"let", "goto", "run", "if", "restore", "gosub", "return",
35 	"rem", "stop", "on", "wait", "load", "save", "verify", "def",
36 	"poke", "print#", "print", "cont", "list", "clr", "cmd", "sys",
37 	"open", "close", "get", "new", "tab(", "to", "fn", "spc(",
38 	"then", "not", "step", "+", "-", "*", "/", "^", "and", "or",
39 	">", "=", "<", "sgn", "int", "abs", "usr", "fre", "pos", "sqr",
40 	"rnd", "log", "exp", "cos", "sin", "tan", "atn", "peek", "len",
41 	"str$", "val", "asc", "chr$", "left$", "right$", "mid$", "go"
42 };
43 
usage(FILE * fp)44 void usage(FILE *fp)
45 {
46 	fprintf(fp,
47 		"Usage: %s [OPTION]... [FILE]...\n"
48 		"List CBM BASIC programs\n"
49 		"\n"
50 		"  --version  output version information and exit\n"
51 		"  --help     display this help and exit\n",
52 		programname);
53 }
54 
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57 	FILE *fp;
58 	int a, b, c;
59 
60 	if (argc < 2) {
61 		usage(stderr);
62 		exit(1);
63 	}
64 
65 	if (strstr(argv[1], "--help")) {
66 		usage(stdout);
67 		exit(0);
68 	}
69 
70 	if (strstr(argv[1], "--version")) {
71 		version(programname, progversion, author, copyright);
72 		exit(0);
73 	}
74 
75 	fp = fopen(argv[1], "rb");
76 
77 	if (fp) {
78 		b = fgetc(fp);
79 		b = fgetc(fp);
80 
81 		while (b != EOF) {
82 			a = fgetc(fp);
83 			a = a + 256 * fgetc(fp);
84 
85 			if (a) {
86 				a = fgetc(fp);
87 				a = a + 256 * fgetc(fp);
88 				printf("%d ", a);
89 
90 				while ((c = fgetc(fp))) {
91 					if (c == EOF)
92 						break;
93 					if (c >= 0x80 && c < 0xcc)
94 						printf("%s", cmd[c - 0x80]);
95 					else
96 						printf("%c", c);
97 				}
98 				printf("\n");
99 			} else {
100 				break;
101 			}
102 		}
103 		fclose(fp);
104 	} else {
105 		printf("File %s not found!\n", argv[1]);
106 	}
107 
108 	return 0;
109 }
110