#include #include #include #include "glk.h" #include "cheapglk.h" #include "glkstart.h" int gli_screenwidth = 80; int gli_screenheight = 24; int gli_utf8output = FALSE; int gli_utf8input = FALSE; 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; ixargtype != 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; jxargtype != 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); } } return 1; } /* Initialize things. */ gli_initialize_misc(); inittime = TRUE; if (!glkunix_startup_code(&startdata)) { glk_exit(); } inittime = FALSE; if (display_version) { printf("Welcome to the Cheap Glk Implementation, library version %s.\n\n", LIBRARY_VERSION); } 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; } /* 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); }