| 1 |
#ifdef RCSID |
| 2 |
static char RCSid[] = |
| 3 |
"$Header: d:/cvsroot/tads/TADS2/TRD.C,v 1.3 1999/05/29 15:51:02 MJRoberts Exp $"; |
| 4 |
#endif |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdarg.h> |
| 26 |
#include "os.h" |
| 27 |
#include "std.h" |
| 28 |
#include "trd.h" |
| 29 |
#include "err.h" |
| 30 |
#include "mch.h" |
| 31 |
#include "obj.h" |
| 32 |
#include "run.h" |
| 33 |
#include "voc.h" |
| 34 |
#include "bif.h" |
| 35 |
#include "dbg.h" |
| 36 |
#include "sup.h" |
| 37 |
#include "cmd.h" |
| 38 |
#include "fio.h" |
| 39 |
#include "oem.h" |
| 40 |
#include "ply.h" |
| 41 |
#include "cmap.h" |
| 42 |
#include "regex.h" |
| 43 |
|
| 44 |
|
| 45 |
void supgnam(char *buf, tokthdef *tab, objnum sc) |
| 46 |
{ |
| 47 |
strcpy(buf, "???"); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
void tok_read_defines(tokcxdef *tctx, osfildef *fp, errcxdef *ec) |
| 52 |
{ |
| 53 |
errsig(ec, ERR_UNKRSC); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
void trchid(void) {} |
| 58 |
void trcsho(void) {} |
| 59 |
|
| 60 |
struct runsdef *dbgfrfind(dbgcxdef *ctx, objnum frobj, uint frofs) |
| 61 |
{ |
| 62 |
VARUSED(frobj); |
| 63 |
VARUSED(frofs); |
| 64 |
errsig(ctx->dbgcxerr, ERR_INACTFR); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
void dbgss(struct dbgcxdef *ctx, uint ofs, int instr, int err, |
| 69 |
uchar *noreg *p) |
| 70 |
{ |
| 71 |
VARUSED(ctx); |
| 72 |
VARUSED(ofs); |
| 73 |
VARUSED(instr); |
| 74 |
VARUSED(err); |
| 75 |
VARUSED(p); |
| 76 |
} |
| 77 |
|
| 78 |
int dbgstart(struct dbgcxdef *ctx) |
| 79 |
{ |
| 80 |
VARUSED(ctx); |
| 81 |
return TRUE; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static void trdptf(const char *fmt, ...) |
| 86 |
{ |
| 87 |
char buf[256]; |
| 88 |
va_list va; |
| 89 |
|
| 90 |
|
| 91 |
va_start(va, fmt); |
| 92 |
vsprintf(buf, fmt, va); |
| 93 |
va_end(va); |
| 94 |
|
| 95 |
|
| 96 |
os_printz(buf); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static void trdusage_show_range(errcxdef *ec, int msg_first, int msg_last) |
| 104 |
{ |
| 105 |
int i; |
| 106 |
char buf[128]; |
| 107 |
|
| 108 |
for (i = msg_first ; i <= msg_last ; ++i) |
| 109 |
{ |
| 110 |
errmsg(ec, buf, (uint)sizeof(buf), i); |
| 111 |
trdptf("%s\n", buf); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static void trdusage_range(errcxdef *ec, int msg_first, int msg_last) |
| 120 |
{ |
| 121 |
|
| 122 |
trdusage_show_range(ec, msg_first, msg_last); |
| 123 |
|
| 124 |
|
| 125 |
errsig(ec, ERR_USAGE); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void trdusage(errcxdef *ec) |
| 132 |
{ |
| 133 |
int first; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
first = ERR_TRUS1; |
| 140 |
if (ec->errcxappctx != 0 && ec->errcxappctx->usage_app_name != 0) |
| 141 |
{ |
| 142 |
char buf[128]; |
| 143 |
char buf2[128]; |
| 144 |
erradef argv[1]; |
| 145 |
|
| 146 |
|
| 147 |
errmsg(ec, buf, (uint)sizeof(buf), ERR_TRUSPARM); |
| 148 |
|
| 149 |
|
| 150 |
argv[0].errastr = (char *)ec->errcxappctx->usage_app_name; |
| 151 |
errfmt(buf2, (int)sizeof(buf2), buf, 1, argv); |
| 152 |
|
| 153 |
|
| 154 |
trdptf("%s\n", buf2); |
| 155 |
|
| 156 |
|
| 157 |
++first; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
trdusage_show_range(ec, first, ERR_TRUSL); |
| 162 |
|
| 163 |
|
| 164 |
trdusage_show_range(ec, ERR_TRUS_OS_FIRST, ERR_TRUS_OS_LAST); |
| 165 |
|
| 166 |
|
| 167 |
trdusage_range(ec, ERR_TRUSFT1, ERR_TRUSFTL); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static void trdusage_s(errcxdef *ec) |
| 174 |
{ |
| 175 |
trdusage_range(ec, ERR_TRSUS1, ERR_TRSUSL); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
static void trdmain1(errcxdef *ec, int argc, char *argv[], |
| 180 |
appctxdef *appctx, char *save_ext) |
| 181 |
{ |
| 182 |
osfildef *swapfp = (osfildef *)0; |
| 183 |
runcxdef runctx; |
| 184 |
bifcxdef bifctx; |
| 185 |
voccxdef vocctx; |
| 186 |
void (*bif[100])(struct bifcxdef *, int); |
| 187 |
mcmcxdef *mctx = 0; |
| 188 |
mcmcx1def *globalctx = 0; |
| 189 |
dbgcxdef dbg; |
| 190 |
supcxdef supctx; |
| 191 |
char *swapname = 0; |
| 192 |
char swapbuf[OSFNMAX]; |
| 193 |
char **argp; |
| 194 |
char *arg; |
| 195 |
char *infile; |
| 196 |
char infile_abs[OSFNMAX]; |
| 197 |
char infile_path[OSFNMAX]; |
| 198 |
char *exefile; |
| 199 |
ulong swapsize = 0xffffffffL; |
| 200 |
int swapena = OS_DEFAULT_SWAP_ENABLED; |
| 201 |
int i; |
| 202 |
int pause = FALSE; |
| 203 |
fiolcxdef fiolctx; |
| 204 |
noreg int loadopen = FALSE; |
| 205 |
char inbuf[OSFNMAX]; |
| 206 |
ulong cachelimit = 0xffffffff; |
| 207 |
ushort undosiz = TRD_UNDOSIZ; |
| 208 |
objucxdef *undoptr = 0; |
| 209 |
uint flags; |
| 210 |
objnum preinit; |
| 211 |
uint heapsiz = TRD_HEAPSIZ; |
| 212 |
uint stksiz = TRD_STKSIZ; |
| 213 |
runsdef *mystack; |
| 214 |
uchar *myheap; |
| 215 |
extern osfildef *cmdfile; |
| 216 |
extern osfildef *logfp; |
| 217 |
int preload = FALSE; |
| 218 |
ulong totsize; |
| 219 |
extern voccxdef *main_voc_ctx; |
| 220 |
int safety_read, safety_write; |
| 221 |
char *restore_file = 0; |
| 222 |
char *charmap = 0; |
| 223 |
int charmap_none; |
| 224 |
int doublespace = TRUE; |
| 225 |
|
| 226 |
NOREG((&loadopen)) |
| 227 |
|
| 228 |
|
| 229 |
out_init(); |
| 230 |
|
| 231 |
|
| 232 |
safety_read = safety_write = 2; |
| 233 |
|
| 234 |
|
| 235 |
charmap_none = FALSE; |
| 236 |
|
| 237 |
|
| 238 |
for (i = 1, argp = argv + 1 ; i < argc ; ++argp, ++i) |
| 239 |
{ |
| 240 |
arg = *argp; |
| 241 |
if (*arg == '-') |
| 242 |
{ |
| 243 |
switch(*(arg+1)) |
| 244 |
{ |
| 245 |
case 'c': |
| 246 |
if (!strcmp(arg+1, "ctab")) |
| 247 |
{ |
| 248 |
|
| 249 |
charmap = cmdarg(ec, &argp, &i, argc, 4, trdusage); |
| 250 |
} |
| 251 |
else if (!strcmp(arg+1, "ctab-")) |
| 252 |
{ |
| 253 |
|
| 254 |
charmap_none = TRUE; |
| 255 |
} |
| 256 |
else |
| 257 |
trdusage(ec); |
| 258 |
break; |
| 259 |
|
| 260 |
case 'r': |
| 261 |
|
| 262 |
restore_file = cmdarg(ec, &argp, &i, argc, 1, trdusage); |
| 263 |
break; |
| 264 |
|
| 265 |
case 'i': |
| 266 |
qasopn(cmdarg(ec, &argp, &i, argc, 1, trdusage), TRUE); |
| 267 |
break; |
| 268 |
|
| 269 |
case 'o': |
| 270 |
cmdfile = osfopwt(cmdarg(ec, &argp, &i, argc, 1, trdusage), |
| 271 |
OSFTCMD); |
| 272 |
break; |
| 273 |
|
| 274 |
case 'l': |
| 275 |
logfp = osfopwt(cmdarg(ec, &argp, &i, argc, 1, trdusage), |
| 276 |
OSFTCMD); |
| 277 |
break; |
| 278 |
|
| 279 |
case 'p': |
| 280 |
if (!stricmp(arg, "-plain")) |
| 281 |
{ |
| 282 |
os_plain(); |
| 283 |
break; |
| 284 |
} |
| 285 |
pause = cmdtog(ec, pause, arg, 1, trdusage); |
| 286 |
break; |
| 287 |
|
| 288 |
case 'd': |
| 289 |
if (!strnicmp(arg, "-double", 7)) |
| 290 |
{ |
| 291 |
|
| 292 |
doublespace = cmdtog(ec, doublespace, arg, 6, trdusage); |
| 293 |
|
| 294 |
|
| 295 |
out_set_doublespace(doublespace); |
| 296 |
break; |
| 297 |
} |
| 298 |
break; |
| 299 |
|
| 300 |
case 's': |
| 301 |
{ |
| 302 |
char *p; |
| 303 |
|
| 304 |
|
| 305 |
p = cmdarg(ec, &argp, &i, argc, 1, trdusage); |
| 306 |
|
| 307 |
|
| 308 |
if (*p == '?') |
| 309 |
trdusage_s(ec); |
| 310 |
|
| 311 |
|
| 312 |
safety_read = *p - '0'; |
| 313 |
safety_write = (*(p+1) != '\0' ? *(p+1) - '0' : |
| 314 |
safety_read); |
| 315 |
|
| 316 |
|
| 317 |
if (safety_read < 0 || safety_read > 4 |
| 318 |
|| safety_write < 0 || safety_write > 4) |
| 319 |
trdusage_s(ec); |
| 320 |
|
| 321 |
|
| 322 |
if (appctx != 0 && appctx->set_io_safety_level != 0) |
| 323 |
(*appctx->set_io_safety_level) |
| 324 |
(appctx->io_safety_level_ctx, |
| 325 |
safety_read, safety_write); |
| 326 |
} |
| 327 |
break; |
| 328 |
|
| 329 |
case 'm': |
| 330 |
switch(*(arg + 2)) |
| 331 |
{ |
| 332 |
case 's': |
| 333 |
stksiz = atoi(cmdarg(ec, &argp, &i, argc, 2, trdusage)); |
| 334 |
break; |
| 335 |
|
| 336 |
case 'h': |
| 337 |
heapsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, trdusage)); |
| 338 |
break; |
| 339 |
|
| 340 |
default: |
| 341 |
cachelimit = atol(cmdarg(ec, &argp, &i, argc, 1, |
| 342 |
trdusage)); |
| 343 |
break; |
| 344 |
} |
| 345 |
break; |
| 346 |
|
| 347 |
case 't': |
| 348 |
|
| 349 |
switch(*(arg+2)) |
| 350 |
{ |
| 351 |
case 'f': |
| 352 |
swapname = cmdarg(ec, &argp, &i, argc, 2, trdusage); |
| 353 |
break; |
| 354 |
|
| 355 |
case 's': |
| 356 |
swapsize = atol(cmdarg(ec, &argp, &i, argc, 2, trdusage)); |
| 357 |
break; |
| 358 |
|
| 359 |
case 'p': |
| 360 |
preload = cmdtog(ec, preload, arg, 2, trdusage); |
| 361 |
break; |
| 362 |
|
| 363 |
default: |
| 364 |
swapena = cmdtog(ec, swapena, arg, 1, trdusage); |
| 365 |
break; |
| 366 |
} |
| 367 |
break; |
| 368 |
|
| 369 |
case 'u': |
| 370 |
undosiz = atoi(cmdarg(ec, &argp, &i, argc, 1, trdusage)); |
| 371 |
break; |
| 372 |
|
| 373 |
default: |
| 374 |
trdusage(ec); |
| 375 |
} |
| 376 |
} |
| 377 |
else break; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
exefile = 0; |
| 382 |
|
| 383 |
|
| 384 |
if (i == argc) |
| 385 |
{ |
| 386 |
osfildef *fp; |
| 387 |
ulong curpos; |
| 388 |
ulong endpos; |
| 389 |
int use_exe; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
infile = 0; |
| 403 |
use_exe = FALSE; |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if (restore_file != 0) |
| 410 |
{ |
| 411 |
|
| 412 |
if (fiorso_getgame(restore_file, inbuf, sizeof(inbuf))) |
| 413 |
{ |
| 414 |
|
| 415 |
infile = inbuf; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
if (infile == 0) |
| 424 |
{ |
| 425 |
|
| 426 |
exefile = (argv && argv[0] ? argv[0] : "TRX"); |
| 427 |
fp = os_exeseek(exefile, "TGAM"); |
| 428 |
if (fp != 0) |
| 429 |
{ |
| 430 |
|
| 431 |
curpos = osfpos(fp); |
| 432 |
osfseek(fp, 0L, OSFSK_END); |
| 433 |
endpos = osfpos(fp); |
| 434 |
osfcls(fp); |
| 435 |
|
| 436 |
|
| 437 |
if (endpos != curpos) |
| 438 |
use_exe = TRUE; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if (infile == 0 && !use_exe) |
| 447 |
{ |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
if (appctx != 0 && appctx->get_game_name != 0) |
| 453 |
{ |
| 454 |
|
| 455 |
if ((*appctx->get_game_name)(appctx->get_game_name_ctx, |
| 456 |
inbuf, sizeof(inbuf))) |
| 457 |
{ |
| 458 |
|
| 459 |
infile = inbuf; |
| 460 |
} |
| 461 |
else |
| 462 |
{ |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
trdptf("\n"); |
| 469 |
trdptf("(No game has been selected.)\n"); |
| 470 |
return; |
| 471 |
} |
| 472 |
} |
| 473 |
else |
| 474 |
{ |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
trdusage(ec); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
infile = *argp; |
| 486 |
if (i + 1 != argc) |
| 487 |
trdusage(ec); |
| 488 |
|
| 489 |
#ifndef OS_HATES_EXTENSIONS |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
if (osfacc(infile)) |
| 497 |
{ |
| 498 |
strcpy(inbuf, infile); |
| 499 |
os_defext(inbuf, "gam"); |
| 500 |
infile = inbuf; |
| 501 |
} |
| 502 |
#endif |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
if (swapena && swapsize) |
| 507 |
{ |
| 508 |
swapfp = os_create_tempfile(swapname, swapbuf); |
| 509 |
if (swapname == 0) swapname = swapbuf; |
| 510 |
if (swapfp == 0) errsig(ec, ERR_OPSWAP); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
if (charmap_none) |
| 515 |
cmap_override(); |
| 516 |
else if (cmap_load(charmap)) |
| 517 |
errsig(ec, ERR_INVCMAP); |
| 518 |
|
| 519 |
ERRBEGIN(ec) |
| 520 |
|
| 521 |
|
| 522 |
globalctx = mcmini(cachelimit, 128, swapsize, swapfp, swapname, ec); |
| 523 |
mctx = mcmcini(globalctx, 128, fioldobj, &fiolctx, |
| 524 |
objrevert, (void *)0); |
| 525 |
mctx->mcmcxrvc = mctx; |
| 526 |
|
| 527 |
|
| 528 |
if (undosiz) |
| 529 |
undoptr = objuini(mctx, undosiz, vocdundo, vocdusz, &vocctx); |
| 530 |
else |
| 531 |
undoptr = (objucxdef *)0; |
| 532 |
|
| 533 |
|
| 534 |
vocini(&vocctx, ec, mctx, &runctx, undoptr, 100, 100, 200); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
main_voc_ctx = &vocctx; |
| 542 |
|
| 543 |
|
| 544 |
totsize = (ulong)stksiz * (ulong)sizeof(runsdef); |
| 545 |
if (totsize != (size_t)totsize) |
| 546 |
errsig1(ec, ERR_STKSIZE, ERRTINT, (uint)(65535/sizeof(runsdef))); |
| 547 |
mystack = (runsdef *)mchalo(ec, (size_t)totsize, "runtime stack"); |
| 548 |
myheap = mchalo(ec, heapsiz, "runtime heap"); |
| 549 |
|
| 550 |
|
| 551 |
if (infile != 0) |
| 552 |
os_get_abs_filename(infile_abs, sizeof(infile_abs), infile); |
| 553 |
else if (exefile != 0) |
| 554 |
os_get_abs_filename(infile_abs, sizeof(infile_abs), exefile); |
| 555 |
else |
| 556 |
infile_abs[0] = '\0'; |
| 557 |
os_get_path_name(infile_path, sizeof(infile_path), infile_abs); |
| 558 |
|
| 559 |
|
| 560 |
runctx.runcxerr = ec; |
| 561 |
runctx.runcxmem = mctx; |
| 562 |
runctx.runcxstk = mystack; |
| 563 |
runctx.runcxstop = &mystack[stksiz]; |
| 564 |
runctx.runcxsp = mystack; |
| 565 |
runctx.runcxbp = mystack; |
| 566 |
runctx.runcxheap = myheap; |
| 567 |
runctx.runcxhp = myheap; |
| 568 |
runctx.runcxhtop = &myheap[heapsiz]; |
| 569 |
runctx.runcxundo = undoptr; |
| 570 |
runctx.runcxbcx = &bifctx; |
| 571 |
runctx.runcxbi = bif; |
| 572 |
runctx.runcxtio = (tiocxdef *)0; |
| 573 |
runctx.runcxdbg = &dbg; |
| 574 |
runctx.runcxvoc = &vocctx; |
| 575 |
runctx.runcxdmd = supcont; |
| 576 |
runctx.runcxdmc = &supctx; |
| 577 |
runctx.runcxext = 0; |
| 578 |
runctx.runcxgamename = infile; |
| 579 |
runctx.runcxgamepath = infile_path; |
| 580 |
|
| 581 |
|
| 582 |
supctx.supcxerr = ec; |
| 583 |
supctx.supcxmem = mctx; |
| 584 |
supctx.supcxtab = (tokthdef *)0; |
| 585 |
supctx.supcxbuf = (uchar *)0; |
| 586 |
supctx.supcxlen = 0; |
| 587 |
supctx.supcxvoc = &vocctx; |
| 588 |
supctx.supcxrun = &runctx; |
| 589 |
|
| 590 |
|
| 591 |
dbg.dbgcxtio = (tiocxdef *)0; |
| 592 |
dbg.dbgcxmem = mctx; |
| 593 |
dbg.dbgcxerr = ec; |
| 594 |
dbg.dbgcxtab = (tokthdef *)0; |
| 595 |
dbg.dbgcxfcn = 0; |
| 596 |
dbg.dbgcxdep = 0; |
| 597 |
dbg.dbgcxflg = 0; |
| 598 |
dbg.dbgcxlin = (lindef *)0; |
| 599 |
|
| 600 |
|
| 601 |
CLRSTRUCT(bifctx); |
| 602 |
bifctx.bifcxerr = ec; |
| 603 |
bifctx.bifcxrun = &runctx; |
| 604 |
bifctx.bifcxtio = (tiocxdef *)0; |
| 605 |
bifctx.bifcxrnd = 0; |
| 606 |
bifctx.bifcxrndset = FALSE; |
| 607 |
bifctx.bifcxappctx = appctx; |
| 608 |
bifctx.bifcxsafetyr = safety_read; |
| 609 |
bifctx.bifcxsafetyw = safety_write; |
| 610 |
bifctx.bifcxsavext = save_ext; |
| 611 |
|
| 612 |
|
| 613 |
re_init(&bifctx.bifcxregex, ec); |
| 614 |
|
| 615 |
|
| 616 |
supbif(&supctx, bif, (int)(sizeof(bif)/sizeof(bif[0]))); |
| 617 |
|
| 618 |
|
| 619 |
runistat(&vocctx, &runctx, (tiocxdef *)0); |
| 620 |
|
| 621 |
|
| 622 |
os_csr_busy(TRUE); |
| 623 |
|
| 624 |
|
| 625 |
fiord(mctx, &vocctx, (struct tokcxdef *)0, |
| 626 |
infile, exefile, &fiolctx, &preinit, &flags, |
| 627 |
(struct tokpdef *)0, (uchar **)0, (uint *)0, (uint *)0, |
| 628 |
(preload ? 2 : 0), appctx, argv[0]); |
| 629 |
loadopen = TRUE; |
| 630 |
|
| 631 |
|
| 632 |
os_csr_busy(FALSE); |
| 633 |
|
| 634 |
|
| 635 |
plygo(&runctx, &vocctx, (tiocxdef *)0, preinit, restore_file); |
| 636 |
|
| 637 |
|
| 638 |
fiorcls(&fiolctx); |
| 639 |
|
| 640 |
if (pause) |
| 641 |
{ |
| 642 |
trdptf("[press a key to exit]"); |
| 643 |
os_waitc(); |
| 644 |
trdptf("\n"); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
trd_close_swapfile(&runctx); |
| 649 |
|
| 650 |
|
| 651 |
qasclose(); |
| 652 |
|
| 653 |
ERRCLEAN(ec) |
| 654 |
|
| 655 |
trd_close_swapfile(&runctx); |
| 656 |
|
| 657 |
|
| 658 |
if (loadopen) |
| 659 |
fiorcls(&fiolctx); |
| 660 |
|
| 661 |
|
| 662 |
main_voc_ctx = 0; |
| 663 |
|
| 664 |
|
| 665 |
vocterm(&vocctx); |
| 666 |
|
| 667 |
|
| 668 |
if (undoptr != 0) |
| 669 |
objuterm(undoptr); |
| 670 |
|
| 671 |
|
| 672 |
if (mctx != 0) |
| 673 |
mcmcterm(mctx); |
| 674 |
if (globalctx != 0) |
| 675 |
mcmterm(globalctx); |
| 676 |
ERRENDCLN(ec) |
| 677 |
|
| 678 |
|
| 679 |
main_voc_ctx = 0; |
| 680 |
|
| 681 |
|
| 682 |
vocterm(&vocctx); |
| 683 |
|
| 684 |
|
| 685 |
if (undoptr != 0) |
| 686 |
objuterm(undoptr); |
| 687 |
|
| 688 |
|
| 689 |
mcmcterm(mctx); |
| 690 |
mcmterm(globalctx); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
#ifdef OS_SKIP_ERROR_CODES |
| 701 |
# ifdef ERR_LINK_MESSAGES |
| 702 |
# define TRDLOGERR_PREFIX "\n[An error has occurred within TADS: " |
| 703 |
# endif |
| 704 |
#endif |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
#ifndef TRDLOGERR_PREFIX |
| 711 |
# define TRDLOGERR_PREFIX "\n[%s-%d: " |
| 712 |
#endif |
| 713 |
|
| 714 |
|
| 715 |
static void trdlogerr(void *ctx0, char *fac, int err, |
| 716 |
int argc, erradef *argv) |
| 717 |
{ |
| 718 |
errcxdef *ctx = (errcxdef *)ctx0; |
| 719 |
char buf[256]; |
| 720 |
char msg[256]; |
| 721 |
|
| 722 |
|
| 723 |
sprintf(buf, TRDLOGERR_PREFIX, fac, err); |
| 724 |
trdptf("%s", buf); |
| 725 |
out_logfile_print(buf, FALSE); |
| 726 |
|
| 727 |
|
| 728 |
errmsg(ctx, msg, (uint)sizeof(msg), err); |
| 729 |
errfmt(buf, (int)sizeof(buf), msg, argc, argv); |
| 730 |
trdptf("%s]\n", buf); |
| 731 |
out_logfile_print(buf, FALSE); |
| 732 |
out_logfile_print("]", TRUE); |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
void trd_close_swapfile(runcxdef *runctx) |
| 740 |
{ |
| 741 |
extern voccxdef *main_voc_ctx; |
| 742 |
mcmcxdef *mctx; |
| 743 |
mcmcx1def *globalctx; |
| 744 |
mcscxdef *mcsctx; |
| 745 |
|
| 746 |
|
| 747 |
if (runctx == 0) |
| 748 |
{ |
| 749 |
|
| 750 |
if (main_voc_ctx == 0) |
| 751 |
return; |
| 752 |
|
| 753 |
|
| 754 |
runctx = main_voc_ctx->voccxrun; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
mctx = runctx->runcxmem; |
| 759 |
globalctx = mctx->mcmcxgl; |
| 760 |
mcsctx = &globalctx->mcmcxswc; |
| 761 |
|
| 762 |
|
| 763 |
if (mcsctx->mcscxfp != 0) |
| 764 |
{ |
| 765 |
|
| 766 |
osfcls(mcsctx->mcscxfp); |
| 767 |
|
| 768 |
|
| 769 |
mcsctx->mcscxfp = (osfildef *)0; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
if (mcsctx->mcscxfname != 0) |
| 774 |
{ |
| 775 |
|
| 776 |
osfdel_temp(mcsctx->mcscxfname); |
| 777 |
|
| 778 |
|
| 779 |
mchfre(mcsctx->mcscxfname); |
| 780 |
mcsctx->mcscxfname = 0; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
int trdmain(int argc, char *argv[], appctxdef *appctx, char *save_ext) |
| 786 |
{ |
| 787 |
errcxdef errctx; |
| 788 |
int err; |
| 789 |
osfildef *fp; |
| 790 |
|
| 791 |
errctx.errcxlog = trdlogerr; |
| 792 |
errctx.errcxlgc = &errctx; |
| 793 |
errctx.errcxfp = (osfildef *)0; |
| 794 |
errctx.errcxofs = 0; |
| 795 |
errctx.errcxappctx = appctx; |
| 796 |
fp = oserrop(argv[0]); |
| 797 |
errini(&errctx, fp); |
| 798 |
|
| 799 |
|
| 800 |
#ifndef NO_T2_COPYRIGHT_NOTICE |
| 801 |
trdptf("%s - A %s TADS %s Interpreter.\n", |
| 802 |
G_tads_oem_app_name, G_tads_oem_display_mode, |
| 803 |
TADS_RUNTIME_VERSION); |
| 804 |
trdptf("%sopyright (c) 1993, 2012 by Michael J. Roberts.\n", |
| 805 |
G_tads_oem_copyright_prefix ? "TADS c" : "C"); |
| 806 |
trdptf("%s\n", G_tads_oem_author); |
| 807 |
#endif |
| 808 |
|
| 809 |
ERRBEGIN(&errctx) |
| 810 |
trdmain1(&errctx, argc, argv, appctx, save_ext); |
| 811 |
ERRCATCH(&errctx, err) |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
if (err != ERR_USAGE && err != ERR_RUNQUIT) |
| 817 |
errclog(&errctx); |
| 818 |
|
| 819 |
|
| 820 |
if (errctx.errcxfp != 0) |
| 821 |
osfcls(errctx.errcxfp); |
| 822 |
|
| 823 |
|
| 824 |
os_expause(); |
| 825 |
|
| 826 |
|
| 827 |
return (err == ERR_RUNQUIT ? OSEXSUCC : OSEXFAIL); |
| 828 |
ERREND(&errctx) |
| 829 |
|
| 830 |
|
| 831 |
if (errctx.errcxfp != 0) |
| 832 |
osfcls(errctx.errcxfp); |
| 833 |
|
| 834 |
|
| 835 |
return(OSEXSUCC); |
| 836 |
} |