| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmmain.cpp - T3 VM main entrypoint - execute an image file |
| 15 | Function |
| 16 | Main entrypoint for executing a T3 image file. Loads an image file |
| 17 | and begins execution. Returns when execution terminates. |
| 18 | Notes |
| 19 | |
| 20 | Modified |
| 21 | 10/07/99 MJRoberts - Creation |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | #include "os.h" |
| 30 | #include "t3std.h" |
| 31 | #include "vmerr.h" |
| 32 | #include "vmfile.h" |
| 33 | #include "vmimage.h" |
| 34 | #include "vmrun.h" |
| 35 | #include "vmimgrb.h" |
| 36 | #include "vmmain.h" |
| 37 | #include "vmhost.h" |
| 38 | #include "vmhostsi.h" |
| 39 | #include "vminit.h" |
| 40 | #include "vmpredef.h" |
| 41 | #include "vmobj.h" |
| 42 | #include "vmvsn.h" |
| 43 | #include "charmap.h" |
| 44 | #include "vmsave.h" |
| 45 | #include "vmtype.h" |
| 46 | #include "vmrunsym.h" |
| 47 | #include "vmmcreg.h" |
| 48 | #include "vmbifreg.h" |
| 49 | #include "vmbiftad.h" |
| 50 | #include "sha2.h" |
| 51 | #include "vmnet.h" |
| 52 | |
| 53 | |
| 54 | /* ------------------------------------------------------------------------ */ |
| 55 | /* |
| 56 | * Convert a directory path parameter to an absolute path, and return an |
| 57 | * allocated copy of the string. |
| 58 | */ |
| 59 | static char *path_param_to_abs(const char *param) |
| 60 | { |
| 61 | /* make sure the path is expressed in absolute format */ |
| 62 | char abuf[OSFNMAX]; |
| 63 | os_get_abs_filename(abuf, sizeof(abuf), param); |
| 64 | |
| 65 | /* return a copy of the path */ |
| 66 | return lib_copy_str(abuf); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Extract the path from a filename, convert it to an absolute path, and |
| 71 | * return an allocated copy of the string. |
| 72 | */ |
| 73 | static char *file_param_to_abs_path(const char *param) |
| 74 | { |
| 75 | /* extract the path portion */ |
| 76 | char path[OSFNMAX]; |
| 77 | os_get_path_name(path, sizeof(path), param); |
| 78 | |
| 79 | /* return it in absolute form */ |
| 80 | return path_param_to_abs(path); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* ------------------------------------------------------------------------ */ |
| 85 | /* |
| 86 | * Execute an image file. If an exception occurs, we'll display a |
| 87 | * message on the console, and we'll return the error code; we'll return |
| 88 | * zero on success. If an error occurs, we'll fill in 'errbuf' with a |
| 89 | * message describing the problem. |
| 90 | */ |
| 91 | int vm_run_image(const vm_run_image_params *params) |
| 92 | { |
| 93 | CVmFile *fp = 0; |
| 94 | CVmImageLoader *volatile loader = 0; |
| 95 | CVmImageFile *volatile imagefp = 0; |
| 96 | unsigned long image_file_base = 0; |
| 97 | int retval; |
| 98 | vm_globals *vmg__; |
| 99 | |
| 100 | /* |
| 101 | * get the full absolute path for the image file, to ensure that future |
| 102 | * references are independent of working directory changes |
| 103 | */ |
| 104 | os_get_abs_filename(G_os_gamename, sizeof(G_os_gamename), |
| 105 | params->image_file_name); |
| 106 | |
| 107 | /* presume we will return success */ |
| 108 | retval = 0; |
| 109 | |
| 110 | /* make sure the local time zone settings are initialized */ |
| 111 | os_tzset(); |
| 112 | |
| 113 | /* create the file object */ |
| 114 | fp = new CVmFile(); |
| 115 | |
| 116 | /* initialize the VM */ |
| 117 | vm_init_options opts(params->hostifc, params->clientifc, |
| 118 | params->charset, params->log_charset); |
| 119 | vm_initialize(&vmg__, &opts); |
| 120 | |
| 121 | /* catch any errors that occur during loading and running */ |
| 122 | err_try |
| 123 | { |
| 124 | /* if we have a resource root path, tell the host interface */ |
| 125 | if (params->res_dir != 0) |
| 126 | params->hostifc->set_res_dir(params->res_dir); |
| 127 | |
| 128 | /* |
| 129 | * Save the default working directory for file operations, if |
| 130 | * specified. If it's not specified, use the image file directory |
| 131 | * as the default. |
| 132 | */ |
| 133 | if (params->file_dir != 0) |
| 134 | G_file_path = path_param_to_abs(params->file_dir); |
| 135 | |
| 136 | /* set the sandbox directory (for file safety restrictions) */ |
| 137 | if (params->sandbox_dir != 0) |
| 138 | G_sandbox_path = path_param_to_abs(params->sandbox_dir); |
| 139 | else |
| 140 | G_sandbox_path = file_param_to_abs_path(params->image_file_name); |
| 141 | |
| 142 | /* set the networking configuration in the globals */ |
| 143 | G_net_config = params->netconfig; |
| 144 | |
| 145 | /* tell the client system to initialize */ |
| 146 | params->clientifc->client_init( |
| 147 | VMGLOB_ADDR, |
| 148 | params->script_file, params->script_quiet, |
| 149 | params->log_file, params->cmd_log_file, |
| 150 | params->show_banner ? T3VM_BANNER_STRING : 0, |
| 151 | params->more_mode); |
| 152 | |
| 153 | /* open the byte-code file */ |
| 154 | if (params->load_from_exe) |
| 155 | { |
| 156 | /* find the image within the executable */ |
| 157 | osfildef *exe_fp = os_exeseek(G_os_gamename, "TGAM"); |
| 158 | if (exe_fp == 0) |
| 159 | err_throw(VMERR_NO_IMAGE_IN_EXE); |
| 160 | |
| 161 | /* |
| 162 | * set up to read from the executable at the location of the |
| 163 | * embedded image file that we just found |
| 164 | */ |
| 165 | image_file_base = osfpos(exe_fp); |
| 166 | fp->set_file(exe_fp, image_file_base); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | /* reading from a normal file - open the file */ |
| 171 | fp->open_read(G_os_gamename, OSFTT3IMG); |
| 172 | } |
| 173 | |
| 174 | /* create the loader */ |
| 175 | imagefp = new CVmImageFileExt(fp); |
| 176 | loader = new CVmImageLoader(imagefp, G_os_gamename, image_file_base); |
| 177 | |
| 178 | /* load the image */ |
| 179 | loader->load(vmg0_); |
| 180 | |
| 181 | /* let the client prepare for execution */ |
| 182 | params->clientifc->pre_exec(VMGLOB_ADDR); |
| 183 | |
| 184 | /* if desired, seed the random number generator */ |
| 185 | if (params->seed_rand) |
| 186 | CVmBifTADS::randomize(vmg_ 0); |
| 187 | |
| 188 | #ifdef TADSNET |
| 189 | /* |
| 190 | * if we have a network configuration, get the game's IFID - this |
| 191 | * is needed to identify the game to the storage server |
| 192 | */ |
| 193 | if (params->netconfig != 0) |
| 194 | { |
| 195 | char *ifid = vm_get_ifid(params->hostifc); |
| 196 | if (ifid != 0) |
| 197 | { |
| 198 | params->netconfig->set("ifid", ifid); |
| 199 | t3free(ifid); |
| 200 | } |
| 201 | } |
| 202 | #endif /* TADSNET */ |
| 203 | |
| 204 | /* run the program from the main entrypoint */ |
| 205 | loader->run(vmg_ params->prog_argv, params->prog_argc, |
| 206 | 0, 0, params->saved_state); |
| 207 | |
| 208 | /* tell the client we're done with execution */ |
| 209 | params->clientifc->post_exec(VMGLOB_ADDR); |
| 210 | } |
| 211 | err_catch(exc) |
| 212 | { |
| 213 | /* |
| 214 | * report anything except DEBUG VM HALT, which is special: it |
| 215 | * indicates that the user explicitly terminated execution via the |
| 216 | * debugger UI, so there's obviously no need to report an error |
| 217 | */ |
| 218 | if (exc->get_error_code() != VMERR_DBG_HALT) |
| 219 | { |
| 220 | /* tell the client execution failed due to an error */ |
| 221 | params->clientifc->post_exec_err(VMGLOB_ADDR); |
| 222 | |
| 223 | /* note the error code for returning to the caller */ |
| 224 | retval = exc->get_error_code(); |
| 225 | |
| 226 | /* get the message for the error */ |
| 227 | char errbuf[512]; |
| 228 | CVmRun::get_exc_message(vmg_ exc, errbuf, sizeof(errbuf), TRUE); |
| 229 | |
| 230 | /* display the message */ |
| 231 | params->clientifc->display_error(VMGLOB_ADDR, exc, errbuf, FALSE); |
| 232 | } |
| 233 | } |
| 234 | err_end; |
| 235 | |
| 236 | /* done with the file base path and sandbox path */ |
| 237 | lib_free_str(G_file_path); |
| 238 | lib_free_str(G_sandbox_path); |
| 239 | |
| 240 | /* unload the image */ |
| 241 | if (loader != 0) |
| 242 | loader->unload(vmg0_); |
| 243 | |
| 244 | /* delete the loader and the image file object */ |
| 245 | if (loader != 0) |
| 246 | delete loader; |
| 247 | if (imagefp != 0) |
| 248 | delete imagefp; |
| 249 | |
| 250 | /* notify the client */ |
| 251 | params->clientifc->client_terminate(VMGLOB_ADDR); |
| 252 | |
| 253 | #ifdef TADSNET |
| 254 | /* |
| 255 | * Delete the network configuration, if we have one. Note that a |
| 256 | * network configuration might have been created dyanmically during |
| 257 | * execution even if the caller didn't originally provide one. |
| 258 | */ |
| 259 | if (G_net_config != 0) |
| 260 | delete G_net_config; |
| 261 | #endif |
| 262 | |
| 263 | /* terminate the VM */ |
| 264 | vm_terminate(vmg__, params->clientifc); |
| 265 | |
| 266 | /* delete the file */ |
| 267 | if (fp != 0) |
| 268 | delete fp; |
| 269 | |
| 270 | /* return the status code */ |
| 271 | return retval; |
| 272 | } |
| 273 | |
| 274 | /* ------------------------------------------------------------------------ */ |
| 275 | /* |
| 276 | * Read an option argument from an option that allows its arguments to be |
| 277 | * either appended directly to the end of the option (as in "-otest") or |
| 278 | * to follow as the subsequent vector item (as in "-o test"). optlen |
| 279 | * gives the length of the option prefix itself, including the hyphen |
| 280 | * prefix (so an option "-o" would have length 2). We'll increment |
| 281 | * *curarg if we consume the extra vector position. Returns null if there |
| 282 | * isn't an argument for the option. |
| 283 | */ |
| 284 | static char *get_opt_arg(int argc, char **argv, int *curarg, int optlen) |
| 285 | { |
| 286 | /* |
| 287 | * if we have an argument (i.e., any non-empty text) appended directly |
| 288 | * to the option vector item, we don't need to consume an extra vector |
| 289 | * position |
| 290 | */ |
| 291 | if (argv[*curarg][optlen] != '\0') |
| 292 | { |
| 293 | /* the argument is merely the remainder of this vector item */ |
| 294 | return &argv[*curarg][optlen]; |
| 295 | } |
| 296 | |
| 297 | /* advance to the next vector position */ |
| 298 | ++(*curarg); |
| 299 | |
| 300 | /* if we don't have any vector items left, there's no argument */ |
| 301 | if (*curarg >= argc) |
| 302 | return 0; |
| 303 | |
| 304 | /* this vector item is the next argument */ |
| 305 | return argv[*curarg]; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* ------------------------------------------------------------------------ */ |
| 310 | /* |
| 311 | * Create the network configuration object, if we haven't already. |
| 312 | */ |
| 313 | #ifdef TADSNET |
| 314 | static void create_netconfig( |
| 315 | CVmMainClientIfc *clientifc, TadsNetConfig *&netconfig, char **argv) |
| 316 | { |
| 317 | if (netconfig == 0) |
| 318 | { |
| 319 | /* create the object */ |
| 320 | netconfig = new TadsNetConfig(); |
| 321 | |
| 322 | /* build the path to the web config file */ |
| 323 | char confname[OSFNMAX], confpath[OSFNMAX]; |
| 324 | os_get_special_path(confpath, sizeof(confpath), argv[0], |
| 325 | OS_GSP_T3_SYSCONFIG); |
| 326 | os_build_full_path(confname, sizeof(confname), |
| 327 | confpath, "tadsweb.config"); |
| 328 | |
| 329 | /* if there's a net configuration file, read it */ |
| 330 | osfildef *fp = osfoprb(confname, OSFTTEXT); |
| 331 | if (fp != 0) |
| 332 | { |
| 333 | /* read the file into the configuration object */ |
| 334 | netconfig->read(fp, clientifc); |
| 335 | |
| 336 | /* done with the file */ |
| 337 | osfcls(fp); |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | /* warn that the file is missing */ |
| 342 | char *msg = t3sprintf_alloc( |
| 343 | "Warning: -webhost mode specified, but couldn't " |
| 344 | "read web configuration file \"%s\"", confname); |
| 345 | clientifc->display_error(0, 0, msg, FALSE); |
| 346 | t3free(msg); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | #endif /* TADSNET */ |
| 351 | |
| 352 | /* ------------------------------------------------------------------------ */ |
| 353 | /* |
| 354 | * Main Entrypoint for command-line invocations. For simplicity, a |
| 355 | * normal C main() or equivalent entrypoint can invoke this routine |
| 356 | * directly, using the usual argc/argv conventions. |
| 357 | * |
| 358 | * Returns a status code suitable for use with exit(): OSEXSUCC if we |
| 359 | * successfully loaded and ran an executable, OSEXFAIL on failure. If |
| 360 | * an error occurs, we'll fill in 'errbuf' with a message describing the |
| 361 | * problem. |
| 362 | */ |
| 363 | int vm_run_image_main(CVmMainClientIfc *clientifc, |
| 364 | const char *executable_name, |
| 365 | int argc, char **argv, |
| 366 | int defext, int test_mode, |
| 367 | CVmHostIfc *hostifc) |
| 368 | { |
| 369 | char image_file_name[OSFNMAX]; |
| 370 | vm_run_image_params params(clientifc, hostifc, image_file_name); |
| 371 | int curarg; |
| 372 | int stat; |
| 373 | int found_image = FALSE; |
| 374 | int hide_usage = FALSE; |
| 375 | int usage_err = FALSE; |
| 376 | #ifdef TADSNET |
| 377 | char *storage_sid = 0; |
| 378 | #endif |
| 379 | |
| 380 | /* check to see if we can load from the .exe file */ |
| 381 | { |
| 382 | /* look for an image file attached to the executable */ |
| 383 | osfildef *fp = os_exeseek(argv[0], "TGAM"); |
| 384 | if (fp != 0) |
| 385 | { |
| 386 | /* close the file */ |
| 387 | osfcls(fp); |
| 388 | |
| 389 | /* note that we want to load from the executable */ |
| 390 | params.load_from_exe = TRUE; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * if loading from the exe file, get the custom saved game suffix, |
| 395 | * if defined |
| 396 | */ |
| 397 | if (params.load_from_exe |
| 398 | && (fp = os_exeseek(argv[0], "SAVX")) != 0) |
| 399 | { |
| 400 | ushort len; |
| 401 | char buf[OSFNMAX]; |
| 402 | |
| 403 | /* read the length and the name */ |
| 404 | if (!osfrb(fp, &len, sizeof(len)) |
| 405 | && len < sizeof(buf) |
| 406 | && !osfrb(fp, buf, len)) |
| 407 | { |
| 408 | /* null-terminate the string */ |
| 409 | buf[len] = '\0'; |
| 410 | |
| 411 | /* save it in the OS layer */ |
| 412 | os_set_save_ext(buf); |
| 413 | } |
| 414 | |
| 415 | /* close the file */ |
| 416 | osfcls(fp); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* scan options */ |
| 421 | for (curarg = 1 ; curarg < argc && argv[curarg][0] == '-' ; ++curarg) |
| 422 | { |
| 423 | /* |
| 424 | * if the argument is just '-', it means we're explicitly leaving |
| 425 | * the image filename blank and skipping to the arguments to the VM |
| 426 | * program itself |
| 427 | */ |
| 428 | if (argv[curarg][1] == '\0') |
| 429 | break; |
| 430 | |
| 431 | /* check the argument */ |
| 432 | switch(argv[curarg][1]) |
| 433 | { |
| 434 | case 'b': |
| 435 | if (strcmp(argv[curarg], "-banner") == 0) |
| 436 | { |
| 437 | /* make a note to show the banner */ |
| 438 | params.show_banner = TRUE; |
| 439 | } |
| 440 | else |
| 441 | goto opt_error; |
| 442 | break; |
| 443 | |
| 444 | case 'c': |
| 445 | if (strcmp(argv[curarg], "-cs") == 0) |
| 446 | { |
| 447 | /* set the console character set name */ |
| 448 | if (++curarg < argc) |
| 449 | params.charset = argv[curarg]; |
| 450 | else |
| 451 | goto opt_error; |
| 452 | } |
| 453 | else if (strcmp(argv[curarg], "-csl") == 0) |
| 454 | { |
| 455 | /* set the log file character set name */ |
| 456 | if (++curarg < argc) |
| 457 | params.log_charset = argv[curarg]; |
| 458 | else |
| 459 | goto opt_error; |
| 460 | } |
| 461 | else |
| 462 | goto opt_error; |
| 463 | break; |
| 464 | |
| 465 | case 'n': |
| 466 | if (argv[curarg][2] == 's') |
| 467 | { |
| 468 | /* network safety level - parse the ## arguments */ |
| 469 | const char *cli = &argv[curarg][3]; |
| 470 | const char *srv = &argv[curarg][4]; |
| 471 | |
| 472 | /* if there's no server level, use the one level for both */ |
| 473 | if (*cli == '\0' || *srv == '\0') |
| 474 | srv = cli; |
| 475 | |
| 476 | /* validate the arguments */ |
| 477 | if (*cli < '0' || *cli > '2' |
| 478 | || *srv < '0' || *srv > '2') |
| 479 | goto opt_error; |
| 480 | |
| 481 | /* set the level */ |
| 482 | hostifc->set_net_safety(*cli - '0', *srv - '0'); |
| 483 | } |
| 484 | else if (strcmp(argv[curarg], "-nobanner") == 0) |
| 485 | { |
| 486 | /* make a note not to show the banner */ |
| 487 | params.show_banner = FALSE; |
| 488 | } |
| 489 | else if (strcmp(argv[curarg], "-norand") == 0 |
| 490 | || strcmp(argv[curarg], "-norandomize") == 0) |
| 491 | { |
| 492 | /* make a note not to seed the RNG */ |
| 493 | params.seed_rand = FALSE; |
| 494 | } |
| 495 | else if (strcmp(argv[curarg], "-nomore") == 0) |
| 496 | { |
| 497 | /* turn off MORE mode */ |
| 498 | params.more_mode = FALSE; |
| 499 | } |
| 500 | else |
| 501 | goto opt_error; |
| 502 | break; |
| 503 | |
| 504 | case 's': |
| 505 | if (argv[curarg][2] == 'd') |
| 506 | { |
| 507 | /* sandbox directory */ |
| 508 | if (++curarg < argc) |
| 509 | params.sandbox_dir = argv[curarg]; |
| 510 | else |
| 511 | goto opt_error; |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | /* |
| 516 | * file safety level - get the read and write levels, if |
| 517 | * specified separately |
| 518 | */ |
| 519 | const char *rd = &argv[curarg][2]; |
| 520 | const char *wrt = &argv[curarg][3]; |
| 521 | |
| 522 | /* if there's no write level, base it on the read level */ |
| 523 | if (*rd == '\0' || *wrt == '\0') |
| 524 | wrt = rd; |
| 525 | |
| 526 | /* check the ranges */ |
| 527 | if (*rd < '0' || *rd > '4' |
| 528 | || *wrt < '0' || *wrt > '4' |
| 529 | || *(wrt+1) != '\0') |
| 530 | { |
| 531 | /* invalid level */ |
| 532 | goto opt_error; |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | /* set the level in the host application */ |
| 537 | hostifc->set_io_safety(*rd - '0', *wrt - '0'); |
| 538 | } |
| 539 | } |
| 540 | break; |
| 541 | |
| 542 | case 'i': |
| 543 | case 'I': |
| 544 | /* |
| 545 | * read from a script file (little 'i' reads silently, big 'I' |
| 546 | * echoes the log as it goes) - the next argument, or the |
| 547 | * remainder of this argument, is the filename |
| 548 | */ |
| 549 | params.script_quiet = (argv[curarg][1] == 'i'); |
| 550 | params.script_file = get_opt_arg(argc, argv, &curarg, 2); |
| 551 | if (params.script_file == 0) |
| 552 | goto opt_error; |
| 553 | break; |
| 554 | |
| 555 | case 'l': |
| 556 | /* log output to file */ |
| 557 | params.log_file = get_opt_arg(argc, argv, &curarg, 2); |
| 558 | if (params.log_file == 0) |
| 559 | goto opt_error; |
| 560 | break; |
| 561 | |
| 562 | case 'o': |
| 563 | /* log commands to file */ |
| 564 | params.cmd_log_file = get_opt_arg(argc, argv, &curarg, 2); |
| 565 | if (params.cmd_log_file == 0) |
| 566 | goto opt_error; |
| 567 | break; |
| 568 | |
| 569 | case 'p': |
| 570 | /* check what follows */ |
| 571 | if (strcmp(argv[curarg], "-plain") == 0) |
| 572 | { |
| 573 | /* tell the client to set plain ASCII mode */ |
| 574 | clientifc->set_plain_mode(); |
| 575 | break; |
| 576 | } |
| 577 | else |
| 578 | goto opt_error; |
| 579 | break; |
| 580 | |
| 581 | case 'r': |
| 582 | /* get the name of the saved state file to restore */ |
| 583 | params.saved_state = get_opt_arg(argc, argv, &curarg, 2); |
| 584 | if (params.saved_state == 0) |
| 585 | goto opt_error; |
| 586 | break; |
| 587 | |
| 588 | case 'R': |
| 589 | /* note the resource root directory */ |
| 590 | params.res_dir = get_opt_arg(argc, argv, &curarg, 2); |
| 591 | if (params.res_dir == 0) |
| 592 | goto opt_error; |
| 593 | break; |
| 594 | |
| 595 | case 'd': |
| 596 | /* note the working directory for file operations */ |
| 597 | params.file_dir = get_opt_arg(argc, argv, &curarg, 2); |
| 598 | if (params.file_dir == 0) |
| 599 | goto opt_error; |
| 600 | break; |
| 601 | |
| 602 | case 'X': |
| 603 | /* special system operations */ |
| 604 | if (strcmp(argv[curarg], "-Xinterfaces") == 0 && curarg+1 < argc) |
| 605 | { |
| 606 | /* write out the interface report, then terminate */ |
| 607 | vm_interface_report(clientifc, argv[++curarg]); |
| 608 | stat = OSEXSUCC; |
| 609 | goto done; |
| 610 | } |
| 611 | else |
| 612 | goto opt_error; |
| 613 | break; |
| 614 | |
| 615 | #ifdef TADSNET |
| 616 | case 'w': |
| 617 | /* -webhost, -websid, -webimage */ |
| 618 | if (strcmp(argv[curarg], "-webhost") == 0 && curarg+1 < argc) |
| 619 | { |
| 620 | /* if necessary, create the web hosting config object */ |
| 621 | create_netconfig(clientifc, params.netconfig, argv); |
| 622 | |
| 623 | /* set the web host name */ |
| 624 | params.netconfig->set("hostname", argv[++curarg]); |
| 625 | } |
| 626 | else if (strcmp(argv[curarg], "-websid") == 0 |
| 627 | && curarg+1 < argc) |
| 628 | { |
| 629 | /* web storage server SID - save it for later */ |
| 630 | storage_sid = argv[++curarg]; |
| 631 | } |
| 632 | else if (strcmp(argv[curarg], "-webimage") == 0 |
| 633 | && curarg+1 < argc) |
| 634 | { |
| 635 | /* |
| 636 | * Original URL of image file. This is provided for |
| 637 | * logging purposes only; |
| 638 | */ |
| 639 | create_netconfig(clientifc, params.netconfig, argv); |
| 640 | params.netconfig->set("image.url", argv[++curarg]); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | /* invalid option - flag an error */ |
| 645 | goto opt_error; |
| 646 | } |
| 647 | break; |
| 648 | #endif /* TADSNET */ |
| 649 | |
| 650 | default: |
| 651 | opt_error: |
| 652 | /* discard remaining arguments */ |
| 653 | curarg = argc; |
| 654 | |
| 655 | /* note the error */ |
| 656 | usage_err = TRUE; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * If there was no usage error so far, but we don't have an image |
| 663 | * filename argument, try to find the image file some other way. If we |
| 664 | * found an image file embedded in the executable, don't even bother |
| 665 | * looking for an image-file argument - we can only run the embedded |
| 666 | * image in this case. |
| 667 | */ |
| 668 | if (usage_err) |
| 669 | { |
| 670 | /* there was a usage error - don't bother looking for an image file */ |
| 671 | } |
| 672 | else if (!params.load_from_exe |
| 673 | && curarg + 1 <= argc |
| 674 | && strcmp(argv[curarg], "-") != 0) |
| 675 | { |
| 676 | /* the last argument is the image file name */ |
| 677 | strcpy(image_file_name, argv[curarg]); |
| 678 | found_image = TRUE; |
| 679 | |
| 680 | /* |
| 681 | * If the given filename exists, use it as-is; otherwise, if |
| 682 | * we're allowed to add an extension, try applying a default |
| 683 | * extension of "t3" (formerly "t3x") to the given name. |
| 684 | */ |
| 685 | if (defext && osfacc(image_file_name)) |
| 686 | { |
| 687 | /* the given name doesn't exist - try a default extension */ |
| 688 | os_defext(image_file_name, "t3"); /* formerly "t3x" */ |
| 689 | } |
| 690 | } |
| 691 | else |
| 692 | { |
| 693 | /* |
| 694 | * if we're loading from the executable, try using the executable |
| 695 | * filename as the image file |
| 696 | */ |
| 697 | if (params.load_from_exe |
| 698 | && os_get_exe_filename(image_file_name, sizeof(image_file_name), |
| 699 | argv[0])) |
| 700 | found_image = TRUE; |
| 701 | |
| 702 | /* |
| 703 | * If we still haven't found an image file, try to get the image |
| 704 | * file from the saved state file, if one was specified. Don't |
| 705 | * attempt this if we're loading the image from the executable, as |
| 706 | * we don't want to allow running a different image file in that |
| 707 | * case. |
| 708 | */ |
| 709 | if (!params.load_from_exe && !found_image && params.saved_state != 0) |
| 710 | { |
| 711 | osfildef *save_fp; |
| 712 | |
| 713 | /* open the saved state file */ |
| 714 | save_fp = osfoprb(params.saved_state, OSFTT3SAV); |
| 715 | if (save_fp != 0) |
| 716 | { |
| 717 | /* get the name of the image file */ |
| 718 | if (CVmSaveFile::restore_get_image( |
| 719 | save_fp, image_file_name, sizeof(image_file_name)) == 0) |
| 720 | { |
| 721 | /* we successfully obtained the filename */ |
| 722 | found_image = TRUE; |
| 723 | } |
| 724 | |
| 725 | /* close the file */ |
| 726 | osfcls(save_fp); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * if we haven't found the image, and the host system provides a |
| 732 | * way of asking the user for a filename, try that |
| 733 | */ |
| 734 | if (!params.load_from_exe && !found_image) |
| 735 | { |
| 736 | /* ask the host system for a game name */ |
| 737 | switch (hostifc->get_image_name(image_file_name, |
| 738 | sizeof(image_file_name))) |
| 739 | { |
| 740 | case VMHOST_GIN_IGNORED: |
| 741 | /* no effect - we have no new information */ |
| 742 | break; |
| 743 | |
| 744 | case VMHOST_GIN_CANCEL: |
| 745 | /* |
| 746 | * the user cancelled the dialog - we don't have a |
| 747 | * filename, but we also don't want to show usage, since |
| 748 | * the user chose not to proceed |
| 749 | */ |
| 750 | hide_usage = TRUE; |
| 751 | break; |
| 752 | |
| 753 | case VMHOST_GIN_ERROR: |
| 754 | /* |
| 755 | * an error occurred showing the dialog - there's not |
| 756 | * much we can do except show the usage message |
| 757 | */ |
| 758 | break; |
| 759 | |
| 760 | case VMHOST_GIN_SUCCESS: |
| 761 | /* that was successful - we have an image file now */ |
| 762 | found_image = TRUE; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * if we don't have an image file name by this point, we can't |
| 770 | * proceed - show the usage message and terminate |
| 771 | */ |
| 772 | if (usage_err || !found_image) |
| 773 | { |
| 774 | char buf[OSFNMAX + 1024]; |
| 775 | |
| 776 | /* show the usage message if allowed */ |
| 777 | if (params.load_from_exe && !usage_err) |
| 778 | { |
| 779 | sprintf(buf, "An error occurred loading the T3 VM program from " |
| 780 | "the embedded data file. This application executable " |
| 781 | "file might be corrupted.\n"); |
| 782 | |
| 783 | /* display the message */ |
| 784 | clientifc->display_error(0, 0, buf, FALSE); |
| 785 | } |
| 786 | else if (!hide_usage) |
| 787 | { |
| 788 | /* build the usage message */ |
| 789 | sprintf(buf, |
| 790 | "%s\n" |
| 791 | "usage: %s [options] %sarguments]\n" |
| 792 | "options:\n" |
| 793 | " -banner - show the version/copyright banner\n" |
| 794 | " -cs xxx - use character set 'xxx' for keyboard " |
| 795 | "and display\n" |
| 796 | " -csl xxx - use character set 'xxx' for log files\n" |
| 797 | " -d path - set default directory for file operations\n" |
| 798 | " -i file - read command input from file (quiet mode)\n" |
| 799 | " -I file - read command input from file (echo mode)\n" |
| 800 | " -l file - log all console input/output to file\n" |
| 801 | " -norand - don't seed the random number generator\n" |
| 802 | " -ns## - set the network safety level for Client " |
| 803 | "and Server (each # is from\n" |
| 804 | " 0 to 2 - 0 is the least restrictive, 2 " |
| 805 | "allows no network access)\n" |
| 806 | " -o file - log console input to file\n" |
| 807 | " -plain - run in plain mode (no cursor positioning, " |
| 808 | "colors, etc.)\n" |
| 809 | " -r file - restore saved state from file\n" |
| 810 | " -R dir - set directory for external resources\n" |
| 811 | " -s## - set file safety level for read & write " |
| 812 | "(each # is from 0 to 4;\n" |
| 813 | " 0 is the least restrictive, 4 prohibits all " |
| 814 | "file access)\n" |
| 815 | " -sd dir - set the sandbox directory for file " |
| 816 | "safety restrictions\n" |
| 817 | "\n" |
| 818 | "If provided, the optional extra arguments are passed " |
| 819 | "to the program's\n" |
| 820 | "main entrypoint.\n", |
| 821 | T3VM_BANNER_STRING, executable_name, |
| 822 | params.load_from_exe ? "[- " : "<image-file-name> ["); |
| 823 | |
| 824 | /* display the message */ |
| 825 | clientifc->display_error(0, 0, buf, FALSE); |
| 826 | } |
| 827 | |
| 828 | /* return failure */ |
| 829 | stat = OSEXFAIL; |
| 830 | goto done; |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * if we're in test mode, replace the first argument to the program |
| 835 | * with its root name, so that we don't include any path information |
| 836 | * in the argument list |
| 837 | */ |
| 838 | if (test_mode && curarg <= argc && argv[curarg] != 0) |
| 839 | argv[curarg] = os_get_root_name(argv[curarg]); |
| 840 | |
| 841 | #ifdef TADSNET |
| 842 | /* |
| 843 | * if there's a storage server SID, add it to the net configuration |
| 844 | */ |
| 845 | if (params.netconfig != 0 && storage_sid != 0) |
| 846 | params.netconfig->set("storage.sessionid", storage_sid); |
| 847 | #endif /* TADSNET */ |
| 848 | |
| 849 | /* pass the arguments beyond the image file name to the .t3's main() */ |
| 850 | params.prog_argv = argv + curarg; |
| 851 | params.prog_argc = argc - curarg; |
| 852 | |
| 853 | /* run the program */ |
| 854 | stat = vm_run_image(¶ms); |
| 855 | |
| 856 | done: |
| 857 | /* return the status code */ |
| 858 | return stat; |
| 859 | } |
| 860 | |
| 861 | /* ------------------------------------------------------------------------ */ |
| 862 | /* |
| 863 | * Copy a filename with a given size limit. This works essentially like |
| 864 | * strncpy(), but we guarantee that the result is null-terminated even if |
| 865 | * it's truncated. |
| 866 | */ |
| 867 | static void strcpy_limit(char *dst, const char *src, size_t limit) |
| 868 | { |
| 869 | size_t copy_len; |
| 870 | |
| 871 | /* get the length of the source string */ |
| 872 | copy_len = strlen(src); |
| 873 | |
| 874 | /* |
| 875 | * if it exceeds the available space, leaving space for the null |
| 876 | * terminator, limit the copy to the available space |
| 877 | */ |
| 878 | if (copy_len > limit - 1) |
| 879 | copy_len = limit - 1; |
| 880 | |
| 881 | /* copy as much as we can */ |
| 882 | memcpy(dst, src, copy_len); |
| 883 | |
| 884 | /* null-terminate what we managed to copy */ |
| 885 | dst[copy_len] = '\0'; |
| 886 | } |
| 887 | |
| 888 | /* ------------------------------------------------------------------------ */ |
| 889 | /* |
| 890 | * Given a saved game file, try to identify the game file that created the |
| 891 | * saved game. |
| 892 | */ |
| 893 | static int vm_get_game_file_from_savefile(const char *savefile, |
| 894 | char *fname, size_t fnamelen) |
| 895 | { |
| 896 | |
| 897 | /* open the saved game file */ |
| 898 | osfildef *fp = osfoprb(savefile, OSFTBIN); |
| 899 | |
| 900 | /* if that failed, there's no way to read the game file name */ |
| 901 | if (fp == 0) |
| 902 | return FALSE; |
| 903 | |
| 904 | /* presume failure */ |
| 905 | int ret = FALSE; |
| 906 | |
| 907 | /* read the first few bytes */ |
| 908 | char buf[128]; |
| 909 | if (!osfrb(fp, buf, 16)) |
| 910 | { |
| 911 | /* check for a saved game signature we recognize */ |
| 912 | if (memcmp(buf, "TADS2 save/g\012\015\032\000", 16) == 0) |
| 913 | { |
| 914 | /* |
| 915 | * It's a TADS 2 saved game with embedded .GAM file |
| 916 | * information. The filename immediately follows the signature |
| 917 | * (the 15 bytes we just matched), with a two-byte length |
| 918 | * prefix. Seek to the length prefix and read it. |
| 919 | */ |
| 920 | size_t len; |
| 921 | if (!osfseek(fp, 16, OSFSK_SET) && !osfrb(fp, buf, 2)) |
| 922 | len = osrp2(buf); |
| 923 | else |
| 924 | len = 0; |
| 925 | |
| 926 | /* limit the read length to our caller's available buffer */ |
| 927 | if (len > fnamelen - 1) |
| 928 | len = fnamelen - 1; |
| 929 | |
| 930 | /* read the filename */ |
| 931 | if (!osfrb(fp, fname, len)) |
| 932 | { |
| 933 | /* null-terminate it */ |
| 934 | fname[len] = '\0'; |
| 935 | |
| 936 | /* success */ |
| 937 | ret = TRUE; |
| 938 | } |
| 939 | } |
| 940 | else if (memcmp(buf, "T3-state-v", 10) == 0) |
| 941 | { |
| 942 | /* |
| 943 | * It's a T3 saved state file. The image filename is always |
| 944 | * embedded in this type of file, so seek back to the start of |
| 945 | * the file and read the filename. |
| 946 | * |
| 947 | * Note that restore_get_image() returns zero on success, so we |
| 948 | * want to return true if and only if that routine returns |
| 949 | * zero. |
| 950 | */ |
| 951 | osfseek(fp, 0, OSFSK_SET); |
| 952 | ret = (CVmSaveFile::restore_get_image(fp, fname, fnamelen) == 0); |
| 953 | } |
| 954 | else |
| 955 | { |
| 956 | /* |
| 957 | * it's not a signature we know, so it must not be a saved |
| 958 | * state file (at least not one we can deal with) |
| 959 | */ |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /* we're done with the file now, so close it */ |
| 964 | osfcls(fp); |
| 965 | |
| 966 | /* return the result */ |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | |
| 971 | /* ------------------------------------------------------------------------ */ |
| 972 | /* |
| 973 | * Parse a command line to determine the name of the game file specified |
| 974 | * by the arguments. We'll return the index of the argv element that |
| 975 | * specifies the name of the game, or a negative number if there is no |
| 976 | * filename argument. |
| 977 | * |
| 978 | * Note that our parsing will work for TADS 2 or TADS 3 interpreter |
| 979 | * command lines, so this routine can be used to extract the filename from |
| 980 | * an ambiguous command line in order to check the file for its type and |
| 981 | * thereby resolve which interpreter to use. |
| 982 | */ |
| 983 | int vm_get_game_arg(int argc, const char *const *argv, |
| 984 | char *buf, size_t buflen, int *engine_type) |
| 985 | { |
| 986 | /* presume we won't find a file to restore */ |
| 987 | const char *restore_file = 0; |
| 988 | |
| 989 | /* presume we won't identify the engine type based soley on arguments */ |
| 990 | *engine_type = VM_GGT_INVALID; |
| 991 | |
| 992 | /* |
| 993 | * Scan the arguments for the union of the TADS 2 and TADS 3 command |
| 994 | * line options. Start at the second element of the argument vector, |
| 995 | * since the first element is the executable name. Keep going until |
| 996 | * we run out of options, each of which must start with a '-'. |
| 997 | * |
| 998 | * Note that we don't care about the meanings of the options - we |
| 999 | * simply want to skip past them. This is more complicated than |
| 1000 | * merely looking for the first argument without a '-' prefix, because |
| 1001 | * some of the options allow arguments, and an option argument can |
| 1002 | * sometimes - depending on the argument - take the next position in |
| 1003 | * the argument vector after the option itself. So, we must determine |
| 1004 | * the meaning of each option well enough that we can tell whether or |
| 1005 | * not the next argv element after the option is part of the option. |
| 1006 | */ |
| 1007 | int i; |
| 1008 | for (i = 1 ; i < argc && argv[i][0] == '-' ; ++i) |
| 1009 | { |
| 1010 | /* |
| 1011 | * check the first character after the hyphen to determine which |
| 1012 | * option we have |
| 1013 | */ |
| 1014 | switch(argv[i][1]) |
| 1015 | { |
| 1016 | case 'b': |
| 1017 | /* |
| 1018 | * tads 3 "-banner" (no arguments, so just consume it and |
| 1019 | * continue) |
| 1020 | */ |
| 1021 | break; |
| 1022 | |
| 1023 | case 'c': |
| 1024 | /* |
| 1025 | * tads 3 "-cs charset" or "-csl charset"; tads 2 "-ctab-" or |
| 1026 | * "-ctab tab" |
| 1027 | */ |
| 1028 | if (strcmp(argv[i], "-ctab") == 0 |
| 1029 | || strcmp(argv[i], "-cs") == 0 |
| 1030 | || strcmp(argv[i], "-csl") == 0) |
| 1031 | { |
| 1032 | /* there's another argument giving the filename */ |
| 1033 | ++i; |
| 1034 | } |
| 1035 | break; |
| 1036 | |
| 1037 | case 'd': |
| 1038 | /* tads 2 "-double[+-]" (no arguments), tads 3 -d <path> */ |
| 1039 | if (memcmp(argv[i], "-double", 7) == 0 |
| 1040 | && (argv[i][7] == '\0' |
| 1041 | || argv[i][7] == '+' |
| 1042 | || argv[i][7] == '-')) |
| 1043 | { |
| 1044 | /* tads 2 -double - no further arguments */ |
| 1045 | } |
| 1046 | else if (argv[i][2] == '\0') |
| 1047 | { |
| 1048 | /* tads 3 -d <path> */ |
| 1049 | ++i; |
| 1050 | } |
| 1051 | break; |
| 1052 | |
| 1053 | case 'm': |
| 1054 | /* tads 2 "-msSize", "-mhSize", "-mSize" */ |
| 1055 | switch(argv[i][2]) |
| 1056 | { |
| 1057 | case 's': |
| 1058 | case 'h': |
| 1059 | /* |
| 1060 | * argument required - if nothing follows the second |
| 1061 | * letter, consume the next vector item as the option |
| 1062 | * argument |
| 1063 | */ |
| 1064 | if (argv[i][3] == '\0') |
| 1065 | ++i; |
| 1066 | break; |
| 1067 | |
| 1068 | case '\0': |
| 1069 | /* |
| 1070 | * argument required, but nothing follows the "-m" - |
| 1071 | * consume the next vector item |
| 1072 | */ |
| 1073 | ++i; |
| 1074 | break; |
| 1075 | |
| 1076 | default: |
| 1077 | /* argument required and present */ |
| 1078 | break; |
| 1079 | } |
| 1080 | break; |
| 1081 | |
| 1082 | case 't': |
| 1083 | /* tads 2 "-tfFile", "-tsSize", "-tp[+-]", "-t[+0]" */ |
| 1084 | switch(argv[i][2]) |
| 1085 | { |
| 1086 | case 'f': |
| 1087 | case 's': |
| 1088 | /* |
| 1089 | * argument required - consume the next vector item if |
| 1090 | * nothing is attached to this item |
| 1091 | */ |
| 1092 | if (argv[i][3] == '\0') |
| 1093 | ++i; |
| 1094 | break; |
| 1095 | |
| 1096 | case 'p': |
| 1097 | /* no arguments */ |
| 1098 | break; |
| 1099 | |
| 1100 | default: |
| 1101 | /* no arguments */ |
| 1102 | break; |
| 1103 | } |
| 1104 | break; |
| 1105 | |
| 1106 | case 'u': |
| 1107 | /* |
| 1108 | * tads 2 "-uSize" - argument required, so consume the next |
| 1109 | * vector item if necessary |
| 1110 | */ |
| 1111 | if (argv[i][2] == '\0') |
| 1112 | ++i; |
| 1113 | break; |
| 1114 | |
| 1115 | case 'n': |
| 1116 | /* |
| 1117 | * tads3 "-ns", "-nobanner", "-norand" take no arguments (-ns |
| 1118 | * does have extra parameter characters, but they have to be |
| 1119 | * concatenated as part of the same argv entry, as in "-ns11") |
| 1120 | */ |
| 1121 | break; |
| 1122 | |
| 1123 | case 's': |
| 1124 | if (argv[i][2] == 'd') |
| 1125 | { |
| 1126 | /* tads 3 -sd <dir> - skip the <dir> parameter */ |
| 1127 | ++i; |
| 1128 | } |
| 1129 | else if (argv[i][2] == '\0') |
| 1130 | { |
| 1131 | /* |
| 1132 | * tads 2/3 "-s#" (#=0,1,2,3,4); in tads 2, the # can be |
| 1133 | * separated by a space from the -s, so we'll allow it this |
| 1134 | * way in general |
| 1135 | */ |
| 1136 | ++i; |
| 1137 | } |
| 1138 | break; |
| 1139 | |
| 1140 | case 'i': |
| 1141 | /* tads 2/3 "-iFile" - consume an argument */ |
| 1142 | if (argv[i][2] == '\0') |
| 1143 | ++i; |
| 1144 | break; |
| 1145 | |
| 1146 | case 'l': |
| 1147 | /* tads 2/3 "-lFile" - consume an argument */ |
| 1148 | if (argv[i][2] == '\0') |
| 1149 | ++i; |
| 1150 | break; |
| 1151 | |
| 1152 | case 'o': |
| 1153 | /* tads 2/3 "-oFile" - consume an argument */ |
| 1154 | if (argv[i][2] == '\0') |
| 1155 | ++i; |
| 1156 | break; |
| 1157 | |
| 1158 | case 'p': |
| 1159 | /* tads 2/3 "-plain"; tads 2 "-p[+-]" - no arguments */ |
| 1160 | break; |
| 1161 | |
| 1162 | case 'R': |
| 1163 | /* tads 3 '-Rdir' - consume an argument */ |
| 1164 | if (argv[i][2] == '\0') |
| 1165 | ++i; |
| 1166 | break; |
| 1167 | |
| 1168 | case 'r': |
| 1169 | /* tads 2/3 "-rFile" - consume an argument */ |
| 1170 | if (argv[i][2] != '\0') |
| 1171 | { |
| 1172 | /* the file to be restored is appended to the "-r" */ |
| 1173 | restore_file = argv[i] + 2; |
| 1174 | } |
| 1175 | else |
| 1176 | { |
| 1177 | /* the file to be restored is the next argument */ |
| 1178 | ++i; |
| 1179 | restore_file = argv[i]; |
| 1180 | } |
| 1181 | break; |
| 1182 | |
| 1183 | case 'X': |
| 1184 | /* TADS 3 special system commands */ |
| 1185 | if (strcmp(argv[i], "-Xinterfaces") == 0) |
| 1186 | { |
| 1187 | /* |
| 1188 | * this is a stand-alone command that doesn't require a |
| 1189 | * game file; return the engine type directly |
| 1190 | */ |
| 1191 | strcpy_limit(buf, "", buflen); |
| 1192 | *engine_type = VM_GGT_TADS3; |
| 1193 | return TRUE; |
| 1194 | } |
| 1195 | break; |
| 1196 | |
| 1197 | case 'w': |
| 1198 | /* tads 3 -webhost, -websid */ |
| 1199 | if (strcmp(argv[i], "-webhost") == 0 |
| 1200 | || strcmp(argv[i], "-websid") == 0) |
| 1201 | { |
| 1202 | /* this takes an additional argument */ |
| 1203 | ++i; |
| 1204 | } |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * We have no more options, so the next argument is the game filename. |
| 1211 | * If we're out of argv elements, then no game filename was directly |
| 1212 | * specified, in which case we'll try looking for a file spec in the |
| 1213 | * restore file, if present. |
| 1214 | */ |
| 1215 | if (i < argc) |
| 1216 | { |
| 1217 | /* there's a game file argument - copy it to the caller's buffer */ |
| 1218 | strcpy_limit(buf, argv[i], buflen); |
| 1219 | |
| 1220 | /* return success */ |
| 1221 | return TRUE; |
| 1222 | } |
| 1223 | else if (restore_file != 0) |
| 1224 | { |
| 1225 | /* |
| 1226 | * There's no game file argument, but there is a restore file |
| 1227 | * argument. Try identifying the game file from the original game |
| 1228 | * file specification stored in the saved state file. |
| 1229 | */ |
| 1230 | return vm_get_game_file_from_savefile(restore_file, buf, buflen); |
| 1231 | } |
| 1232 | else |
| 1233 | { |
| 1234 | /* |
| 1235 | * there's no game file or restore file argument, so they've given |
| 1236 | * us nothing to go on - there's no game file |
| 1237 | */ |
| 1238 | return FALSE; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /* ------------------------------------------------------------------------ */ |
| 1243 | /* |
| 1244 | * Given the name of a file, determine the engine (TADS 2 or TADS 3) for |
| 1245 | * which the file was compiled, based on the file's signature. Returns |
| 1246 | * one of the VM_GGT_xxx codes. |
| 1247 | */ |
| 1248 | static int vm_get_game_type_for_file(const char *filename) |
| 1249 | { |
| 1250 | osfildef *fp; |
| 1251 | char buf[16]; |
| 1252 | int ret; |
| 1253 | |
| 1254 | /* try opening the filename exactly as given */ |
| 1255 | fp = osfoprb(filename, OSFTBIN); |
| 1256 | |
| 1257 | /* if the file doesn't exist, tell the caller */ |
| 1258 | if (fp == 0) |
| 1259 | return VM_GGT_NOT_FOUND; |
| 1260 | |
| 1261 | /* read the first few bytes of the file, where the signature resides */ |
| 1262 | if (osfrb(fp, buf, 16)) |
| 1263 | { |
| 1264 | /* |
| 1265 | * error reading the file - any valid game file is going to be at |
| 1266 | * least long enough to hold the number of bytes we asked for, so |
| 1267 | * it must not be a valid file |
| 1268 | */ |
| 1269 | ret = VM_GGT_INVALID; |
| 1270 | } |
| 1271 | else |
| 1272 | { |
| 1273 | /* check the signature we read against the known signatures */ |
| 1274 | if (memcmp(buf, "TADS2 bin\012\015\032", 12) == 0) |
| 1275 | ret = VM_GGT_TADS2; |
| 1276 | else if (memcmp(buf, "T3-image\015\012\032", 11) == 0) |
| 1277 | ret = VM_GGT_TADS3; |
| 1278 | else |
| 1279 | ret = VM_GGT_INVALID; |
| 1280 | } |
| 1281 | |
| 1282 | /* close the file */ |
| 1283 | osfcls(fp); |
| 1284 | |
| 1285 | /* return the version identifier */ |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Given a game file argument, determine which engine (TADS 2 or TADS 3) |
| 1291 | * should be used to run the game. |
| 1292 | */ |
| 1293 | int vm_get_game_type(const char *filename, |
| 1294 | char *actual_fname, size_t actual_fname_len, |
| 1295 | const char *const *defexts, size_t defext_count) |
| 1296 | { |
| 1297 | int good_count; |
| 1298 | int last_good_ver; |
| 1299 | size_t i; |
| 1300 | |
| 1301 | /* |
| 1302 | * If the exact filename as given exists, determine the file type |
| 1303 | * directly from this file without trying any default extensions. |
| 1304 | */ |
| 1305 | if (osfacc(filename) == 0) |
| 1306 | { |
| 1307 | /* the actual filename is exactly what we were given */ |
| 1308 | if (actual_fname_len != 0) |
| 1309 | { |
| 1310 | /* copy the filename, limiting it to the buffer length */ |
| 1311 | strncpy(actual_fname, filename, actual_fname_len); |
| 1312 | actual_fname[actual_fname_len - 1] = '\0'; |
| 1313 | } |
| 1314 | |
| 1315 | /* return the type according to the file's signature */ |
| 1316 | return vm_get_game_type_for_file(filename); |
| 1317 | } |
| 1318 | |
| 1319 | /* presume we won't find any good files using default extensions */ |
| 1320 | good_count = 0; |
| 1321 | |
| 1322 | /* try each default extension supplied */ |
| 1323 | for (i = 0 ; i < defext_count ; ++i) |
| 1324 | { |
| 1325 | int cur_ver; |
| 1326 | char cur_fname[OSFNMAX]; |
| 1327 | |
| 1328 | /* |
| 1329 | * build the default filename from the given filename and the |
| 1330 | * current default suffix |
| 1331 | */ |
| 1332 | strcpy(cur_fname, filename); |
| 1333 | os_defext(cur_fname, defexts[i]); |
| 1334 | |
| 1335 | /* get the version for this file */ |
| 1336 | cur_ver = vm_get_game_type_for_file(cur_fname); |
| 1337 | |
| 1338 | /* if it's a valid code, note it and remember it */ |
| 1339 | if (vm_ggt_is_valid(cur_ver)) |
| 1340 | { |
| 1341 | /* it's a valid file - count it */ |
| 1342 | ++good_count; |
| 1343 | |
| 1344 | /* remember its version as the last good file's version */ |
| 1345 | last_good_ver = cur_ver; |
| 1346 | |
| 1347 | /* remember its name as the last good file's name */ |
| 1348 | if (actual_fname_len != 0) |
| 1349 | { |
| 1350 | /* copy the filename, limiting it to the buffer length */ |
| 1351 | strncpy(actual_fname, cur_fname, actual_fname_len); |
| 1352 | actual_fname[actual_fname_len - 1] = '\0'; |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * If we had exactly one good match, return it. We will already have |
| 1359 | * filled in actual_fname with the last good filename, so all we need |
| 1360 | * to do in this case is return the version ID for the last good file. |
| 1361 | */ |
| 1362 | if (good_count == 1) |
| 1363 | return last_good_ver; |
| 1364 | |
| 1365 | /* |
| 1366 | * If we didn't find any matches, tell the caller there is no match |
| 1367 | * for the given filename. |
| 1368 | */ |
| 1369 | if (good_count == 0) |
| 1370 | return VM_GGT_NOT_FOUND; |
| 1371 | |
| 1372 | /* we found more than one match, so the type is ambiguous */ |
| 1373 | return VM_GGT_AMBIG; |
| 1374 | } |
| 1375 | |
| 1376 | /* ------------------------------------------------------------------------ */ |
| 1377 | /* |
| 1378 | * Generate the interface report |
| 1379 | */ |
| 1380 | void vm_interface_report(CVmMainClientIfc *cli, const char *fname) |
| 1381 | { |
| 1382 | int i; |
| 1383 | char nm[256]; |
| 1384 | char buf[300]; |
| 1385 | char *p; |
| 1386 | |
| 1387 | /* open the output file */ |
| 1388 | osfildef *fp = osfopwt(fname, OSFTTEXT); |
| 1389 | if (fp == 0) |
| 1390 | { |
| 1391 | cli->display_error( |
| 1392 | 0, 0, "-Xinterfaces failed: unable to open output file", FALSE); |
| 1393 | return; |
| 1394 | } |
| 1395 | |
| 1396 | /* start the XML file */ |
| 1397 | os_fprintz(fp, "<?xml version=\"1.0\"?>\n" |
| 1398 | "<tads3 xmlns=" |
| 1399 | "\"http://www.tads.org/xmlns/tads3/UpdateData\">\n"); |
| 1400 | |
| 1401 | /* write the metaclass interface list */ |
| 1402 | os_fprintz(fp, " <metaclasses>\n"); |
| 1403 | for (i = 0 ; G_meta_reg_table[i].meta != 0 ; ++i) |
| 1404 | { |
| 1405 | /* copy the name string */ |
| 1406 | lib_strcpy(nm, sizeof(nm), |
| 1407 | (*G_meta_reg_table[i].meta)->get_meta_name()); |
| 1408 | |
| 1409 | /* null-terminate at the version delimiter */ |
| 1410 | p = strchr(nm, '/'); |
| 1411 | *p++ = '\0'; |
| 1412 | |
| 1413 | /* display the information */ |
| 1414 | t3sprintf(buf, sizeof(buf), |
| 1415 | " <metaclass>\n" |
| 1416 | " <id>%s</id>\n" |
| 1417 | " <version>%s</version>\n" |
| 1418 | " </metaclass>\n", |
| 1419 | nm, p); |
| 1420 | os_fprintz(fp, buf); |
| 1421 | } |
| 1422 | os_fprintz(fp, " </metaclasses>\n"); |
| 1423 | |
| 1424 | /* write the built-in function information */ |
| 1425 | os_fprintz(fp, " <functionsets>\n"); |
| 1426 | for (i = 0 ; G_bif_reg_table[i].func_set_id != 0 ; ++i) |
| 1427 | { |
| 1428 | /* get the function set name/version string */ |
| 1429 | lib_strcpy(nm, sizeof(nm), G_bif_reg_table[i].func_set_id); |
| 1430 | |
| 1431 | /* null-terminate at the version delimiter */ |
| 1432 | p = strchr(nm, '/'); |
| 1433 | *p++ = '\0'; |
| 1434 | |
| 1435 | /* display the information */ |
| 1436 | t3sprintf(buf, sizeof(buf), |
| 1437 | " <functionset>\n" |
| 1438 | " <id>%s</id>\n" |
| 1439 | " <version>%s</version>\n" |
| 1440 | " </functionset>\n", |
| 1441 | nm, p); |
| 1442 | os_fprintz(fp, buf); |
| 1443 | } |
| 1444 | os_fprintz(fp, " </functionsets>\n"); |
| 1445 | |
| 1446 | /* end the XML file */ |
| 1447 | os_fprintz(fp, "</tads3>\n"); |
| 1448 | |
| 1449 | /* done with the file */ |
| 1450 | osfcls(fp); |
| 1451 | } |
| 1452 | |
| 1453 | /* ------------------------------------------------------------------------ */ |
| 1454 | /* |
| 1455 | * Retrieve the IFID from the gameinfo.txt resource |
| 1456 | */ |
| 1457 | char *vm_get_ifid(CVmHostIfc *hostifc) |
| 1458 | { |
| 1459 | /* first, find the gameinfo.txt resource */ |
| 1460 | unsigned long siz; |
| 1461 | osfildef *fp = hostifc->find_resource("gameinfo.txt", 12, &siz); |
| 1462 | |
| 1463 | /* if we didn't find the resource, there's no IFID */ |
| 1464 | if (fp == 0) |
| 1465 | return 0; |
| 1466 | |
| 1467 | /* |
| 1468 | * allocate a buffer to load the gameinfo.txt contents into (leave room |
| 1469 | * for an extra newline at the end, for uniformity) |
| 1470 | */ |
| 1471 | char *buf = (char *)t3malloc(siz + 1); |
| 1472 | if (buf == 0) |
| 1473 | return 0; |
| 1474 | |
| 1475 | /* presume we won't find an IFID */ |
| 1476 | char *ifid = 0; |
| 1477 | |
| 1478 | /* load the contents */ |
| 1479 | if (!osfrb(fp, buf, siz)) |
| 1480 | { |
| 1481 | char *p, *dst; |
| 1482 | |
| 1483 | /* get the buffer end pointer */ |
| 1484 | char *endp = buf + siz; |
| 1485 | |
| 1486 | /* change CR-LF, LF-CR, and CR to '\n' */ |
| 1487 | for (p = dst = buf ; p < endp ; ) |
| 1488 | { |
| 1489 | if (*p == 10) |
| 1490 | { |
| 1491 | /* LF - store as '\n', and skip the CR in an LF-CR pair */ |
| 1492 | *dst++ = '\n'; |
| 1493 | if (++p < endp && *p == 13) |
| 1494 | ++p; |
| 1495 | } |
| 1496 | else if (*p == 13) |
| 1497 | { |
| 1498 | /* CR - store as '\n', and skip the LF in a CR-LF pair */ |
| 1499 | *dst++ = '\n'; |
| 1500 | if (++p < endp && *p == 10) |
| 1501 | ++p; |
| 1502 | } |
| 1503 | else |
| 1504 | { |
| 1505 | /* copy others as-is */ |
| 1506 | *dst++ = *p++; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | /* note the new end pointer */ |
| 1511 | endp = dst; |
| 1512 | |
| 1513 | /* remove comments and blank lines, and merge continuation lines */ |
| 1514 | for (p = dst = buf ; p < endp ; ) |
| 1515 | { |
| 1516 | /* skip leading whitespace */ |
| 1517 | for ( ; p < endp && isspace(*p) ; ++p) ; |
| 1518 | |
| 1519 | /* if that's the end of the buffer, we're done */ |
| 1520 | if (p == endp) |
| 1521 | break; |
| 1522 | |
| 1523 | /* skip blank lines */ |
| 1524 | if (*p == '\n') |
| 1525 | { |
| 1526 | ++p; |
| 1527 | continue; |
| 1528 | } |
| 1529 | |
| 1530 | /* skip comment lines */ |
| 1531 | if (*p == '#') |
| 1532 | { |
| 1533 | /* skip to the end of the line */ |
| 1534 | for ( ; p < endp && *p != '\n' ; ++p) ; |
| 1535 | continue; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * We're on a line with actual contents. Merge continuation |
| 1540 | * lines: a continuation line is a line following this line |
| 1541 | * that starts with a space and contains at least one |
| 1542 | * non-whitespace character. |
| 1543 | */ |
| 1544 | while (p < endp) |
| 1545 | { |
| 1546 | /* scan and copy to the end of this line */ |
| 1547 | for ( ; p < endp && *p != '\n' ; *dst++ = *p++) ; |
| 1548 | |
| 1549 | /* check the next line to see if it's a continuation line */ |
| 1550 | if (p < endp && *(p+1) != '\n' && isspace(*(p+1))) |
| 1551 | { |
| 1552 | /* scan ahead for non-whitespace */ |
| 1553 | char *q; |
| 1554 | for (q = p + 1 ; q < endp && *q != '\n' && isspace(*q) ; |
| 1555 | ++q) ; |
| 1556 | |
| 1557 | /* if we found something, it's a continuation line */ |
| 1558 | if (q < endp && *q != '\n') |
| 1559 | { |
| 1560 | /* convert the newline to whitespace in the output */ |
| 1561 | *dst++ = ' '; |
| 1562 | |
| 1563 | /* skip the whitespace */ |
| 1564 | p = q; |
| 1565 | |
| 1566 | /* resume scanning this line */ |
| 1567 | continue; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * It's not a continuation line. Store a newline at the |
| 1573 | * end of the line. |
| 1574 | */ |
| 1575 | *dst++ = '\n'; |
| 1576 | |
| 1577 | /* skip the newline */ |
| 1578 | if (p < endp) |
| 1579 | ++p; |
| 1580 | |
| 1581 | /* done processing this line */ |
| 1582 | break; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | /* note the new end pointer */ |
| 1587 | endp = dst; |
| 1588 | |
| 1589 | /* |
| 1590 | * Okay, the buffer is in a nice canonical format, so we can search |
| 1591 | * for the IFID name/value pair. |
| 1592 | */ |
| 1593 | for (p = buf ; p < endp ; ++p) |
| 1594 | { |
| 1595 | /* if we're at the IFID line, retrieve the value */ |
| 1596 | if (memicmp(p, "ifid:", 5) == 0) |
| 1597 | { |
| 1598 | /* this is the IFID line - skip the IFID: and whitespace */ |
| 1599 | for (p += 5 ; p < endp && isspace(*p) ; ++p) ; |
| 1600 | |
| 1601 | /* |
| 1602 | * the IFID is the portion up to the next space, newline, |
| 1603 | * or comma |
| 1604 | */ |
| 1605 | char *start; |
| 1606 | for (start = p ; p < endp && *p != '\n' && !isspace(*p) ; |
| 1607 | ++p) ; |
| 1608 | |
| 1609 | /* allocate a copy of the IFID */ |
| 1610 | ifid = lib_copy_str(start, p - start); |
| 1611 | |
| 1612 | /* we're done */ |
| 1613 | break; |
| 1614 | } |
| 1615 | |
| 1616 | /* scan ahead to the end of the line */ |
| 1617 | for ( ; p < endp && *p != '\n' ; ++p) ; |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | /* done with the gameinfo.txt file and the buffer we loaded it into */ |
| 1622 | osfcls(fp); |
| 1623 | t3free(buf); |
| 1624 | |
| 1625 | /* return the IFID, if we found one */ |
| 1626 | return ifid; |
| 1627 | } |
| 1628 | |
| 1629 |