Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
268 lines (241 loc) · 6.55 KB
/
Copy pathmain.c
File metadata and controls
268 lines (241 loc) · 6.55 KB
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
/* main.c
* - main routines for bastext
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "inmode.h"
#include "outmode.h"
#include "tokenize.h"
#include "utility.h"
#if defined __EMX__
#define strcasecmp stricmp
#elif defined _MSC_VER
#define strcasecmp _stricmp
#endif
typedef enum runmode_e { None, In, Out } runmode_t;
void helpscreen(const char *progname);
/* main
* - main routine
* evaluates arguments and call the appropriate routines
*/
int main(int argc, char *argv[])
{
int option;
bool allfiles = false;
outmode_t outmode = Prg;
bool strict = false;
bool uppercase = false;
runmode_t mode = None;
basic_t force = Any;
char *outfile = "-";
FILE *output;
#ifdef __EMX__
/* OS/2 uses '/' as switch character */
optswchar = SWITCH;
#endif
/* Operation modes:
* i (in) - convert from binary to text
* o (out) - convert from text to binary
* h (help) - print help page
*
* File format:
* f (format) - select format for input/output file:
* -f t64 - T64 mode (legacy: -t)
* -f d64 - D64 mode (out mode only)
* -f p00 - P00 mode (out mode only; autodetecet in in mode)
*
* Source format:
* b (basic) - force BASIC mode
* -b 2.0 - BASIC 2.0 (legacy: -2)
* -b 3.5 - BASIC 3.5
* -b 4.0 - BASIC 4.0
* -b 7.0 - BASIC 7.0 (legacy: -7)
* -b 7.1 - BASIC 7.1 (legacy: -1)
* -b 52 - Graphics52 (legacy: -5)
* -b TFC3 - TFC 3 (legacy: -3)
* -b X16 - X16 BASIC
* -b Super - VIC-20 SuperExpander
*
* Modifiers:
* a (all) - convert all programs, not only those with recognized start
* address (0401/0801/1001/1201/132D/1C01/4001)
* s (strict) - strict tok64 encoding
* d (dest) - destination filename
* u (upper) - use uppercase mode for text
* ? (help)
*/
while (-1 != (option = getopt(argc, argv, "iohf:b:asd:ut23571?"))) {
switch (option) {
case 'i':
mode = In;
break;
case 'o':
mode = Out;
break;
case 'f':
if (0 == strcasecmp(optarg, "t64")) {
case 't':
outmode = T64;
}
else if (0 == strcasecmp(optarg, "d64")) {
outmode = D64;
}
else if (0 == strcasecmp(optarg, "p00")) {
outmode = P00;
}
break;
case 'b':
if (0 == strcmp(optarg, "2.0")) {
case '2':
force = Basic2;
break;
}
else if (0 == strcmp(optarg, "3.5")) {
force = Basic35;
}
else if (0 == strcmp(optarg, "4.0")) {
force = Basic4;
}
else if (0 == strcmp(optarg, "7.0")) {
case '7':
force = Basic7;
}
else if (0 == strcmp(optarg, "7.1")) {
case '1':
force = Basic71;
}
else if (0 == strcmp(optarg, "52")) {
case '5':
force = Graphics52;
}
else if (0 == strcasecmp(optarg, "TFC3")) {
case '3':
force = TFC3;
}
else if (0 == strcasecmp(optarg, "Super")) {
force = VicSuper;
}
else if (0 == strcasecmp(optarg, "X16")) {
force = X16;
}
else {
fprintf(stderr, "Unrecognized BASIC dialect: %s\n", optarg);
return 1;
}
break;
case 'a':
allfiles = true;
break;
case 's':
strict = true;
break;
case 'd':
outfile = optarg;
break;
case 'u':
uppercase = true;
break;
case 'h':
case '?':
case ':':
helpscreen(argv[0]);
return 0;
default:
fprintf(stderr, "getopt error: %d\n", option);
break;
}
}
if (None == mode) { /* missing mode option */
helpscreen(argv[0]);
return 1;
}
if (argc - optind < 1) { /* missing filenames */
fprintf(stderr, "Filename missing\n");
return 1;
}
/* If in input mode, and destination file is other than '-' (stdout),
* open the output file, else set it to stdout
*/
if (0 != strcmp(outfile, "-")) {
if (In == mode) {
output = fopen(outfile, "at");
if (NULL == output) {
output = fopen(outfile, "wt");
if (NULL == output) {
fprintf(stderr, "%s: Unable to open output file: %s\n",
argv[0], outfile);
exit(1);
}
}
} else {
fprintf(stderr, "%s: Cannot use -d in output mode\n",
argv[0]);
exit(1);
}
}
else {
output = stdout;
}
/* Filename to read first is in argv[optind] */
for (int i = optind; i < argc; i ++) {
fprintf(stderr, "Processing: %s\n", argv[i]);
switch (mode) {
case In:
switch (outmode) {
case T64:
t642txt(argv[i], output, allfiles, strict, force, uppercase);
break;
case D64:
d642txt(argv[i], output, allfiles, strict, force, uppercase);
break;
default:
bas2txt(argv[i], output, allfiles, strict, force, uppercase);
break;
}
break;
case Out:
txt2bas(argv[i], force, outmode, uppercase);
break;
case None:
break;
}
}
/* Close output file, if any */
if (output != stdout) fclose(output);
}
/* Output the help screen */
void helpscreen(const char *progname)
{
fprintf(stderr,
"Usage:\n"
" %s " SWITCH "i|" SWITCH "o [" SWITCH "f TYPE] [" SWITCH "b MODE] [" SWITCH "a] [" SWITCH "s] [" SWITCH "d filename] filename(s)\n"
" %s " SWITCH "h\n"
"\n Mode (one of these required):\n"
" " SWITCH "i\tInput mode (binary to text)\n"
" " SWITCH "o\tOutput mode (text to binary)\n"
" " SWITCH "h\tPrint help page\n"
"\n File format selection:\n"
" " SWITCH "ft64\tT64 mode (in: reads from specified T64 archive(s)\n"
" \t out: creates/appends to bastext.t64)\n"
" " SWITCH "fp00\tP00 mode (out: wraps output in .p00 container)\n"
" " SWITCH "fd64\tD64 mode (in: reads from specifiied D64 image(s))\n"
"\n BASIC dialect selection:\n"
" " SWITCH "b2.0\tForce C64 BASIC 2.0 interpretation\n"
" " SWITCH "b3.5\tForce C16/+4 BASIC 3.5 interpretation\n"
" " SWITCH "b7.0\tForce C128 BASIC 7.0 interpretation\n"
" " SWITCH "b7.1\tForce C128 BASIC 7.1 interpretation\n"
" " SWITCH "bTFC3\n\tForce C64 TFC3 interpretation\n"
" " SWITCH "b52\tForce C64 Graphics52 interpretation\n"
" " SWITCH "bX16\tEnable Commander X16 BASIC support\n"
" " SWITCH "bSuper\n\tForce VIC-20 SuperExpander BASIC interpretation\n"
"\n Behavioural options:\n"
" " SWITCH "a\tConvert all, not just recognized start addresses\n"
" \t (0401/0801/1001/1201/132D/1C01/4001)\n"
" " SWITCH "s\tStrict tok64 compatibility\n"
" " SWITCH "d fn\tSend output to file fn\n"
" " SWITCH "u\tUse upper case for strings\n",
progname,
progname);
}