-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.c
369 lines (342 loc) · 13.4 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "glk.h"
#include "gi_debug.h"
#include "cheapglk.h"
#include "glkstart.h"
int gli_screenwidth = 80;
int gli_screenheight = 24;
int gli_utf8output = FALSE;
int gli_utf8input = FALSE;
#if GIDEBUG_LIBRARY_SUPPORT
int gli_debugger = FALSE;
#endif /* GIDEBUG_LIBRARY_SUPPORT */
typedef struct dataresource_struct {
int num;
int isbinary;
char *pathname;
int len;
void *ptr;
} dataresource_t;
static dataresource_t *dataresources = NULL;
static int numdataresources = 0, dataresource_size = 0;
static int inittime = FALSE;
int main(int argc, char *argv[])
{
int ix, jx, val;
int display_version = TRUE;
int errflag = FALSE;
glkunix_startup_t startdata;
/* Test for compile-time errors. If one of these spouts off, you
must edit glk.h and recompile. */
if (sizeof(glui32) != 4) {
printf("Compile-time error: glui32 is not a 32-bit value. Please fix glk.h.\n");
return 1;
}
if ((glui32)(-1) < 0) {
printf("Compile-time error: glui32 is not unsigned. Please fix glk.h.\n");
return 1;
}
/* Now some argument-parsing. This is probably going to hurt. */
startdata.argc = 0;
startdata.argv = (char **)malloc(argc * sizeof(char *));
/* Copy in the program name. */
startdata.argv[startdata.argc] = argv[0];
startdata.argc++;
for (ix=1; ix<argc && !errflag; ix++) {
glkunix_argumentlist_t *argform;
int inarglist = FALSE;
char *cx;
for (argform = glkunix_arguments;
argform->argtype != glkunix_arg_End && !errflag;
argform++) {
if (argform->name[0] == '\0') {
if (argv[ix][0] != '-') {
startdata.argv[startdata.argc] = argv[ix];
startdata.argc++;
inarglist = TRUE;
}
}
else if ((argform->argtype == glkunix_arg_NumberValue)
&& !strncmp(argv[ix], argform->name, strlen(argform->name))
&& (cx = argv[ix] + strlen(argform->name))
&& (atoi(cx) != 0 || cx[0] == '0')) {
startdata.argv[startdata.argc] = argv[ix];
startdata.argc++;
inarglist = TRUE;
}
else if (!strcmp(argv[ix], argform->name)) {
int numeat = 0;
if (argform->argtype == glkunix_arg_ValueFollows) {
if (ix+1 >= argc) {
printf("%s: %s must be followed by a value\n\n",
argv[0], argform->name);
errflag = TRUE;
break;
}
numeat = 2;
}
else if (argform->argtype == glkunix_arg_NoValue) {
numeat = 1;
}
else if (argform->argtype == glkunix_arg_ValueCanFollow) {
if (ix+1 < argc && argv[ix+1][0] != '-') {
numeat = 2;
}
else {
numeat = 1;
}
}
else if (argform->argtype == glkunix_arg_NumberValue) {
if (ix+1 >= argc
|| (atoi(argv[ix+1]) == 0 && argv[ix+1][0] != '0')) {
printf("%s: %s must be followed by a number\n\n",
argv[0], argform->name);
errflag = TRUE;
break;
}
numeat = 2;
}
else {
errflag = TRUE;
break;
}
for (jx=0; jx<numeat; jx++) {
startdata.argv[startdata.argc] = argv[ix];
startdata.argc++;
if (jx+1 < numeat)
ix++;
}
inarglist = TRUE;
break;
}
}
if (inarglist || errflag)
continue;
if (argv[ix][0] == '-') {
if (!strcmp(argv[ix]+1, "dataresource")
|| !strcmp(argv[ix]+1, "dataresourcebin")
|| !strcmp(argv[ix]+1, "dataresourcetext")) {
int isbinary = strcmp(argv[ix]+1, "dataresourcetext") != 0;
ix++;
if (ix >= argc) {
printf("%s: -dataresource option requires NUM:PATHNAME\n\n", argv[0]);
errflag = TRUE;
continue;
}
char *sep = strchr(argv[ix], ':');
if (!sep || sep == argv[ix] || *(sep+1) == '\0') {
printf("%s: -dataresource option requires NUM:PATHNAME\n\n", argv[0]);
errflag = TRUE;
continue;
}
*sep = '\0';
sep++;
val = atoi(argv[ix]);
if (!dataresources || dataresource_size == 0) {
dataresource_size = 4;
dataresources = (dataresource_t *)malloc(dataresource_size * sizeof(dataresource_t));
}
else if (numdataresources >= dataresource_size) {
dataresource_size *= 2;
dataresources = (dataresource_t *)realloc(dataresources, dataresource_size * sizeof(dataresource_t));
}
dataresources[numdataresources].num = val;
dataresources[numdataresources].isbinary = isbinary;
dataresources[numdataresources].pathname = strdup(sep);
dataresources[numdataresources].ptr = NULL;
dataresources[numdataresources].len = 0;
numdataresources++;
continue;
}
switch (argv[ix][1]) {
case 'w':
val = 0;
if (argv[ix][2])
val = atoi(argv[ix]+2);
else {
ix++;
if (ix<argc)
val = atoi(argv[ix]);
}
if (val < 8)
errflag = TRUE;
else
gli_screenwidth = val;
break;
case 'h':
val = 0;
if (argv[ix][2])
val = atoi(argv[ix]+2);
else {
ix++;
if (ix<argc)
val = atoi(argv[ix]);
}
if (val < 2)
errflag = TRUE;
else
gli_screenheight = val;
break;
case 'u':
if (argv[ix][2]) {
if (argv[ix][2] == 'i')
gli_utf8input = TRUE;
else if (argv[ix][2] == 'o')
gli_utf8output = TRUE;
else
errflag = TRUE;
}
else {
gli_utf8output = TRUE;
gli_utf8input = TRUE;
}
break;
case 'q':
display_version = FALSE;
break;
#if GIDEBUG_LIBRARY_SUPPORT
case 'D':
gli_debugger = TRUE;
break;
#endif /* GIDEBUG_LIBRARY_SUPPORT */
default:
printf("%s: unknown option: %s\n\n", argv[0], argv[ix]);
errflag = TRUE;
break;
}
}
}
if (errflag) {
#if GIDEBUG_LIBRARY_SUPPORT
char *debugoption = " -D";
#else /* GIDEBUG_LIBRARY_SUPPORT */
char *debugoption = "";
#endif /* GIDEBUG_LIBRARY_SUPPORT */
printf("usage: %s -w WIDTH -h HEIGHT -u[i|o] -q%s\n", argv[0], debugoption);
if (glkunix_arguments[0].argtype != glkunix_arg_End) {
glkunix_argumentlist_t *argform;
printf("game options:\n");
for (argform = glkunix_arguments;
argform->argtype != glkunix_arg_End;
argform++) {
if (strlen(argform->name) == 0)
printf(" %s\n", argform->desc);
else if (argform->argtype == glkunix_arg_ValueFollows)
printf(" %s val: %s\n", argform->name, argform->desc);
else if (argform->argtype == glkunix_arg_NumberValue)
printf(" %s val: %s\n", argform->name, argform->desc);
else if (argform->argtype == glkunix_arg_ValueCanFollow)
printf(" %s [val]: %s\n", argform->name, argform->desc);
else
printf(" %s: %s\n", argform->name, argform->desc);
}
}
printf("library options:\n");
printf(" -w NUM, -h NUM: pretend to be running in a terminal window of this size (default 80x24)\n");
printf(" -u: assume input and output are UTF-8 encoded (default: Latin-1)\n");
printf(" -ui, -uo: set UTF-8 mode for input and output separately\n");
printf(" -dataresource NUM:PATHNAME, -dataresourcebin NUM:PATHNAME, -dataresourcetext NUM:PATHNAME: tell where the data resource file with the given number can be read (default: search blorb if available)\n");
printf(" (file is considered binary by default, or text if -dataresourcetext is used)\n");
printf(" -q: don't display the \"Welcome to the Cheap Glk Implementation\" header line\n");
#if GIDEBUG_LIBRARY_SUPPORT
printf(" -D: turn on debug console\n");
#endif /* GIDEBUG_LIBRARY_SUPPORT */
printf(" -help: display this list\n");
return 1;
}
/* Initialize things. */
gli_initialize_misc();
inittime = TRUE;
if (!glkunix_startup_code(&startdata)) {
glk_exit();
}
inittime = FALSE;
if (display_version) {
char *debugoption = "";
#if GIDEBUG_LIBRARY_SUPPORT
if (gli_debugger)
debugoption = " Debug support is on.";
#endif /* GIDEBUG_LIBRARY_SUPPORT */
printf("Welcome to the Cheap Glk Implementation, library version %s.%s\n\n",
LIBRARY_VERSION, debugoption);
}
if (gli_debugger)
gidebug_announce_cycle(gidebug_cycle_Start);
glk_main();
glk_exit();
/* glk_exit() doesn't return, but the compiler may kvetch if main()
doesn't seem to return a value. */
return 0;
}
/* Get the data for data chunk num (as specified in command-line arguments,
if any).
The data is read from the given pathname and stashed in memory.
This is memory-hoggish, but so is the rest of glk_stream_open_resource();
see comments there.
(You might wonder why we don't call gli_stream_open_pathname() and
handle the file as a file-based stream. Turns out that doesn't work;
the handling of unicode streams is subtly different for resource
streams and the file-based code won't work. Oh well.)
*/
int gli_get_dataresource_info(int num, void **ptr, glui32 *len, int *isbinary)
{
int ix;
/* The dataresources array isn't sorted (or even checked for duplicates),
so we search it linearly. There probably aren't a lot of entries. */
for (ix=0; ix<numdataresources; ix++) {
if (dataresources[ix].num == num) {
*isbinary = dataresources[ix].isbinary;
*ptr = NULL;
*len = 0;
if (dataresources[ix].ptr) {
/* Already loaded. */
}
else {
FILE *fl = fopen(dataresources[ix].pathname, "rb");
if (!fl) {
gli_strict_warning("stream_open_resource: unable to read given pathname.");
return FALSE;
}
fseek(fl, 0, SEEK_END);
dataresources[ix].len = ftell(fl);
dataresources[ix].ptr = malloc(dataresources[ix].len+1);
fseek(fl, 0, SEEK_SET);
int got = fread(dataresources[ix].ptr, 1, dataresources[ix].len, fl);
fclose(fl);
if (got != dataresources[ix].len) {
gli_strict_warning("stream_open_resource: unable to read all resource data.");
return FALSE;
}
}
*ptr = dataresources[ix].ptr;
*len = dataresources[ix].len;
return TRUE;
}
}
return FALSE;
}
/* This opens a file for reading or writing. (You cannot open a file
for appending using this call.)
This should be used only by glkunix_startup_code().
*/
strid_t glkunix_stream_open_pathname_gen(char *pathname, glui32 writemode,
glui32 textmode, glui32 rock)
{
if (!inittime)
return 0;
return gli_stream_open_pathname(pathname, (writemode != 0), (textmode != 0), rock);
}
/* This opens a file for reading. It is a less-general form of
glkunix_stream_open_pathname_gen(), preserved for backwards
compatibility.
This should be used only by glkunix_startup_code().
*/
strid_t glkunix_stream_open_pathname(char *pathname, glui32 textmode,
glui32 rock)
{
if (!inittime)
return 0;
return gli_stream_open_pathname(pathname, FALSE, (textmode != 0), rock);
}