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
|
/* main.c: Top-level source file
for GlkTerm, curses.h implementation of the Glk API.
Glk API which this implements: version 0.7.1.
Designed by Andrew Plotkin <erkyrath@eblong.com>
http://eblong.com/zarf/glk/
*/
#include "gtoption.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <curses.h>
#include <locale.h>
#include "glk.h"
#include "glkterm.h"
#include "glkstart.h"
/* Declarations of preferences flags. */
int pref_printversion = FALSE;
int pref_screenwidth = 0;
int pref_screenheight = 0;
int pref_messageline = TRUE;
int pref_reverse_textgrids = FALSE;
int pref_override_window_borders = FALSE;
int pref_window_borders = FALSE;
int pref_precise_timing = FALSE;
int pref_historylen = 20;
int pref_prompt_defaults = TRUE;
/* Some constants for my wacky little command-line option parser. */
#define ex_Void (0)
#define ex_Int (1)
#define ex_Bool (2)
static int errflag = FALSE;
static int inittime = FALSE;
static int extract_value(int argc, char *argv[], char *optname, int type,
int *argnum, int *result, int defval);
static int string_to_bool(char *str);
int main(int argc, char *argv[])
{
int ix, jx, val;
glkunix_startup_t startdata;
setlocale (LC_CTYPE, "");
/* 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",
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",
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] != '-') {
printf("%s: unwanted argument: %s\n", argv[0], argv[ix]);
errflag = TRUE;
break;
}
if (extract_value(argc, argv, "?", ex_Void, &ix, &val, FALSE))
errflag = TRUE;
else if (extract_value(argc, argv, "help", ex_Void, &ix, &val, FALSE))
errflag = TRUE;
else if (extract_value(argc, argv, "version", ex_Void, &ix, &val, FALSE))
pref_printversion = val;
else if (extract_value(argc, argv, "v", ex_Void, &ix, &val, FALSE))
pref_printversion = val;
else if (extract_value(argc, argv, "historylen", ex_Int, &ix, &val, 20))
pref_historylen = val;
else if (extract_value(argc, argv, "hl", ex_Int, &ix, &val, 20))
pref_historylen = val;
else if (extract_value(argc, argv, "width", ex_Int, &ix, &val, 80))
pref_screenwidth = val;
else if (extract_value(argc, argv, "w", ex_Int, &ix, &val, 80))
pref_screenwidth = val;
else if (extract_value(argc, argv, "height", ex_Int, &ix, &val, 24))
pref_screenheight = val;
else if (extract_value(argc, argv, "h", ex_Int, &ix, &val, 24))
pref_screenheight = val;
else if (extract_value(argc, argv, "ml", ex_Bool, &ix, &val, pref_messageline))
pref_messageline = val;
else if (extract_value(argc, argv, "revgrid", ex_Bool, &ix, &val, pref_reverse_textgrids))
pref_reverse_textgrids = val;
else if (extract_value(argc, argv, "border", ex_Bool, &ix, &val, pref_window_borders)) {
pref_window_borders = val;
pref_override_window_borders = TRUE;
}
else if (extract_value(argc, argv, "defprompt", ex_Bool, &ix, &val, pref_prompt_defaults))
pref_prompt_defaults = val;
#ifdef OPT_TIMED_INPUT
else if (extract_value(argc, argv, "precise", ex_Bool, &ix, &val, pref_precise_timing))
pref_precise_timing = val;
#endif /* !OPT_TIMED_INPUT */
else {
printf("%s: unknown option: %s\n", argv[0], argv[ix]);
errflag = TRUE;
}
}
if (errflag) {
printf("usage: %s [ options ... ]\n", argv[0]);
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(" -width NUM: manual screen width (if not specified, will try to measure)\n");
printf(" -height NUM: manual screen height (ditto)\n");
printf(" -ml BOOL: use message line (default 'yes')\n");
printf(" -historylen NUM: length of command history (default 20)\n");
printf(" -revgrid BOOL: reverse text in grid (status) windows (default 'no')\n");
printf(" -border BOOL: force borders/no borders between windows\n");
printf(" -defprompt BOOL: provide defaults for file prompts (default 'yes')\n");
#ifdef OPT_TIMED_INPUT
printf(" -precise BOOL: more precise timing for timed input (burns more CPU time) (default 'no')\n");
#endif /* !OPT_TIMED_INPUT */
printf(" -version: display Glk library version\n");
printf(" -help: display this list\n");
printf("NUM values can be any number. BOOL values can be 'yes' or 'no', or no value to toggle.\n");
return 1;
}
if (pref_printversion) {
printf("GlkTerm, library version %s (%s).\n",
LIBRARY_VERSION, LIBRARY_PORT);
printf("For more information, see http://eblong.com/zarf/glk/\n");
return 1;
}
/* We now start up curses. From now on, the program must exit through
glk_exit(), so that endwin() is called. */
gli_setup_curses();
/* Initialize things. */
gli_initialize_misc();
gli_initialize_windows();
gli_initialize_events();
inittime = TRUE;
if (!glkunix_startup_code(&startdata)) {
glk_exit();
}
inittime = FALSE;
/* Call the program main entry point, and then exit. */
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 is my own parsing system for command-line options. It's nothing
special, but it works.
Given argc and argv, check to see if option argnum matches the string
optname. If so, parse its value according to the type flag. Store the
result in result if it matches, and return TRUE; return FALSE if it
doesn't match. argnum is a pointer so that it can be incremented in
cases like "-width 80". defval is the default value, which is only
meaningful for boolean options (so that just "-ml" can toggle the
value of the ml option.) */
static int extract_value(int argc, char *argv[], char *optname, int type,
int *argnum, int *result, int defval)
{
int optlen, val;
char *cx, *origcx, firstch;
optlen = strlen(optname);
origcx = argv[*argnum];
cx = origcx;
firstch = *cx;
cx++;
if (strncmp(cx, optname, optlen))
return FALSE;
cx += optlen;
switch (type) {
case ex_Void:
if (*cx)
return FALSE;
*result = TRUE;
return TRUE;
case ex_Int:
if (*cx == '\0') {
if ((*argnum)+1 >= argc) {
cx = "";
}
else {
(*argnum) += 1;
cx = argv[*argnum];
}
}
val = atoi(cx);
if (val == 0 && cx[0] != '0') {
printf("%s: %s must be followed by a number\n",
argv[0], origcx);
errflag = TRUE;
return FALSE;
}
*result = val;
return TRUE;
case ex_Bool:
if (*cx == '\0') {
if ((*argnum)+1 >= argc) {
val = -1;
}
else {
char *cx2 = argv[(*argnum)+1];
val = string_to_bool(cx2);
if (val != -1)
(*argnum) += 1;
}
}
else {
val = string_to_bool(cx);
if (val == -1) {
printf("%s: %s must be followed by a boolean value\n",
argv[0], origcx);
errflag = TRUE;
return FALSE;
}
}
if (val == -1)
val = !defval;
*result = val;
return TRUE;
}
return FALSE;
}
static int string_to_bool(char *str)
{
if (!strcmp(str, "y") || !strcmp(str, "yes"))
return TRUE;
if (!strcmp(str, "n") || !strcmp(str, "no"))
return FALSE;
if (!strcmp(str, "on"))
return TRUE;
if (!strcmp(str, "off"))
return FALSE;
if (!strcmp(str, "+"))
return TRUE;
if (!strcmp(str, "-"))
return FALSE;
return -1;
}
/* 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);
}
|