Debian Codegraph @

terps/tads/tads2/trd.c

raw symbols SCIP
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 * Copyright (c) 1992, 2000 by 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 trd.c - tads2 run-time driver
15 Function
16 reads a binary file and executes it
17 Notes
18 none
19 Modified
20 04/11/99 CNebel - Use new headers.
21 04/04/92 MJRoberts - creation
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 /* dummy setup function */
45 void supgnam(char *buf, tokthdef *tab, objnum sc)
46 {
47 strcpy(buf, "???");
48 }
49
50 /* dummy file read functions */
51 void tok_read_defines(tokcxdef *tctx, osfildef *fp, errcxdef *ec)
52 {
53 errsig(ec, ERR_UNKRSC);
54 }
55
56 /* dummy debugger functions */
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 /* printf-style formatting */
85 static void trdptf(const char *fmt, ...)
86 {
87 char buf[256];
88 va_list va;
89
90 /* format the string */
91 va_start(va, fmt);
92 vsprintf(buf, fmt, va);
93 va_end(va);
94
95 /* print the formatted buffer */
96 os_printz(buf);
97 }
98
99
100 /*
101 * display a range of usage messages
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 * display a range of usage messages, then throw the usage error
118 */
119 static void trdusage_range(errcxdef *ec, int msg_first, int msg_last)
120 {
121 /* show the message range */
122 trdusage_show_range(ec, msg_first, msg_last);
123
124 /* signal the usage error */
125 errsig(ec, ERR_USAGE);
126 }
127
128 /*
129 * display general run-time usage information
130 */
131 static void trdusage(errcxdef *ec)
132 {
133 int first;
134
135 /*
136 * if we have an app display name, display it instead of the
137 * hard-coded text in the message identifying the app
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 /* get the parameterized usage message */
147 errmsg(ec, buf, (uint)sizeof(buf), ERR_TRUSPARM);
148
149 /* format in the application name */
150 argv[0].errastr = (char *)ec->errcxappctx->usage_app_name;
151 errfmt(buf2, (int)sizeof(buf2), buf, 1, argv);
152
153 /* display it */
154 trdptf("%s\n", buf2);
155
156 /* start at the next message */
157 ++first;
158 }
159
160 /* display the main option list messages */
161 trdusage_show_range(ec, first, ERR_TRUSL);
162
163 /* display the OS-specific option messages, if any */
164 trdusage_show_range(ec, ERR_TRUS_OS_FIRST, ERR_TRUS_OS_LAST);
165
166 /* display the usage footer messages */
167 trdusage_range(ec, ERR_TRUSFT1, ERR_TRUSFTL);
168 }
169
170 /*
171 * display -s suboptions
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]; /* fully-qualified input file name */
197 char infile_path[OSFNMAX]; /* absolute path to input file */
198 char *exefile; /* try with executable file if no infile */
199 ulong swapsize = 0xffffffffL; /* allow unlimited swap space */
200 int swapena = OS_DEFAULT_SWAP_ENABLED; /* swapping enabled? */
201 int i;
202 int pause = FALSE; /* pause after finishing game */
203 fiolcxdef fiolctx;
204 noreg int loadopen = FALSE;
205 char inbuf[OSFNMAX];
206 ulong cachelimit = 0xffffffff;
207 ushort undosiz = TRD_UNDOSIZ; /* default undo context size 16k */
208 objucxdef *undoptr = 0;
209 uint flags; /* flags used to write the file we're reading */
210 objnum preinit; /* preinit object, if we need to execute it */
211 uint heapsiz = TRD_HEAPSIZ;
212 uint stksiz = TRD_STKSIZ;
213 runsdef *mystack;
214 uchar *myheap;
215 extern osfildef *cmdfile; /* hacky v1 qa interface - command log fp */
216 extern osfildef *logfp; /* hacky v1 qa interface - output log fp */
217 int preload = FALSE; /* TRUE => preload all objects */
218 ulong totsize;
219 extern voccxdef *main_voc_ctx;
220 int safety_read, safety_write; /* file I/O safety level */
221 char *restore_file = 0; /* .SAV file to restore */
222 char *charmap = 0; /* character map file */
223 int charmap_none; /* explicitly do not use a character set */
224 int doublespace = TRUE; /* formatter double-space setting */
225
226 NOREG((&loadopen))
227
228 /* initialize the output formatter */
229 out_init();
230
231 /* set safety level to 2 by default - read any/write current dir only */
232 safety_read = safety_write = 2;
233
234 /* no -ctab- yet */
235 charmap_none = FALSE;
236
237 /* parse arguments */
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 /* get the character mapping table */
249 charmap = cmdarg(ec, &argp, &i, argc, 4, trdusage);
250 }
251 else if (!strcmp(arg+1, "ctab-"))
252 {
253 /* use the default mapping */
254 charmap_none = TRUE;
255 }
256 else
257 trdusage(ec);
258 break;
259
260 case 'r':
261 /* restore a game */
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 /* get the argument value */
292 doublespace = cmdtog(ec, doublespace, arg, 6, trdusage);
293
294 /* set the double-space mode in the formatter */
295 out_set_doublespace(doublespace);
296 break;
297 }
298 break;
299
300 case 's':
301 {
302 char *p;
303
304 /* get the option */
305 p = cmdarg(ec, &argp, &i, argc, 1, trdusage);
306
307 /* if they're asking for help, display detailed usage */
308 if (*p == '?')
309 trdusage_s(ec);
310
311 /* get the safety level from the argument */
312 safety_read = *p - '0';
313 safety_write = (*(p+1) != '\0' ? *(p+1) - '0' :
314 safety_read);
315
316 /* range-check the values */
317 if (safety_read < 0 || safety_read > 4
318 || safety_write < 0 || safety_write > 4)
319 trdusage_s(ec);
320
321 /* tell the host system about the setting */
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 /* swap file options: -tf file, -ts size, -t- (no swap) */
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 /* presume we won't take the .gam from the application executable */
381 exefile = 0;
382
383 /* get input name argument, and make sure it's the last argument */
384 if (i == argc)
385 {
386 osfildef *fp;
387 ulong curpos;
388 ulong endpos;
389 int use_exe;
390
391 /*
392 * There's no input name argument, so we need to find the game
393 * to play some other way. First, check to see if we have a
394 * game to restore, and if so whether it has the .GAM name
395 * encoded into it. Next, look to see if there's a game
396 * attached to the executable file; if so, use it. If not, see
397 * if the host system wants to provide a name through its
398 * callback.
399 */
400
401 /* presume we won't find a game attached to the executable file */
402 infile = 0;
403 use_exe = FALSE;
404
405 /*
406 * see if we have a saved game to restore, and it specifies the
407 * GAM file that saved it
408 */
409 if (restore_file != 0)
410 {
411 /* try getting the game name from the restore file */
412 if (fiorso_getgame(restore_file, inbuf, sizeof(inbuf)))
413 {
414 /* got it - use this file */
415 infile = inbuf;
416 }
417 }
418
419 /*
420 * it that didn't work, try to read from os-dependent part of
421 * program being executed
422 */
423 if (infile == 0)
424 {
425 /* try opening the executable file */
426 exefile = (argv && argv[0] ? argv[0] : "TRX");
427 fp = os_exeseek(exefile, "TGAM");
428 if (fp != 0)
429 {
430 /* see if there's a game file attached to the executable */
431 curpos = osfpos(fp);
432 osfseek(fp, 0L, OSFSK_END);
433 endpos = osfpos(fp);
434 osfcls(fp);
435
436 /* if we found it, use it */
437 if (endpos != curpos)
438 use_exe = TRUE;
439 }
440 }
441
442 /*
443 * if we didn't find a game in the executable, try the host
444 * system callback
445 */
446 if (infile == 0 && !use_exe)
447 {
448 /*
449 * ask the host system callback what to do - if we don't
450 * have a host system callback, or the callback
451 */
452 if (appctx != 0 && appctx->get_game_name != 0)
453 {
454 /* call the host system callback */
455 if ((*appctx->get_game_name)(appctx->get_game_name_ctx,
456 inbuf, sizeof(inbuf)))
457 {
458 /* the host system provided a name - use it */
459 infile = inbuf;
460 }
461 else
462 {
463 /*
464 * the host didn't provide a name - simply display a
465 * message indicating that no game file has been
466 * chosen, and return
467 */
468 trdptf("\n");
469 trdptf("(No game has been selected.)\n");
470 return;
471 }
472 }
473 else
474 {
475 /*
476 * we've run out of ways to get a filename - give the
477 * user the usage message and quit
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 * If original name exists, use it; otherwise, try adding .GAM.
492 * Note that this code is ifdef'd so that platforms that don't
493 * use filename extensions in the manner conventional for DOS
494 * and Unix won't use this code.
495 */
496 if (osfacc(infile))
497 {
498 strcpy(inbuf, infile);
499 os_defext(inbuf, "gam");
500 infile = inbuf;
501 }
502 #endif /* !defined(OS_HATES_EXTENSIONS) */
503 }
504
505 /* open up the swap file */
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 /* load the character map */
514 if (charmap_none)
515 cmap_override();
516 else if (cmap_load(charmap))
517 errsig(ec, ERR_INVCMAP);
518
519 ERRBEGIN(ec)
520
521 /* initialize cache manager context */
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 /* set up an undo context */
528 if (undosiz)
529 undoptr = objuini(mctx, undosiz, vocdundo, vocdusz, &vocctx);
530 else
531 undoptr = (objucxdef *)0;
532
533 /* set up vocabulary context */
534 vocini(&vocctx, ec, mctx, &runctx, undoptr, 100, 100, 200);
535
536 /*
537 * save a pointer to the voc context globally, so that certain
538 * external routines (such as Unix-style signal handlers) can reach
539 * it
540 */
541 main_voc_ctx = &vocctx;
542
543 /* allocate stack and heap */
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 /* get the absolute path for the input file */
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 /* set up execution context */
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 /* set up setup context */
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 /* set up debug context */
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; /* no line sources yet */
599
600 /* set up built-in function context */
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 /* initialize the regular expression parser context */
613 re_init(&bifctx.bifcxregex, ec);
614
615 /* add the built-in functions, keywords, etc */
616 supbif(&supctx, bif, (int)(sizeof(bif)/sizeof(bif[0])));
617
618 /* set up status line hack */
619 runistat(&vocctx, &runctx, (tiocxdef *)0);
620
621 /* turn on the "busy" cursor before loading */
622 os_csr_busy(TRUE);
623
624 /* read the game from the binary file */
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 /* turn off the "busy" cursor */
632 os_csr_busy(FALSE);
633
634 /* play the game */
635 plygo(&runctx, &vocctx, (tiocxdef *)0, preinit, restore_file);
636
637 /* close load file */
638 fiorcls(&fiolctx);
639
640 if (pause)
641 {
642 trdptf("[press a key to exit]");
643 os_waitc();
644 trdptf("\n");
645 }
646
647 /* close and delete swapfile, if one was opened */
648 trd_close_swapfile(&runctx);
649
650 /* make sure the script file is closed, if we have one */
651 qasclose();
652
653 ERRCLEAN(ec)
654 /* close and delete swapfile, if one was opened */
655 trd_close_swapfile(&runctx);
656
657 /* close the load file if one was opened */
658 if (loadopen)
659 fiorcls(&fiolctx);
660
661 /* vocctx is going out of scope - forget the global reference to it */
662 main_voc_ctx = 0;
663
664 /* delete the voc context */
665 vocterm(&vocctx);
666
667 /* delete the undo context */
668 if (undoptr != 0)
669 objuterm(undoptr);
670
671 /* release the object cache structures */
672 if (mctx != 0)
673 mcmcterm(mctx);
674 if (globalctx != 0)
675 mcmterm(globalctx);
676 ERRENDCLN(ec)
677
678 /* vocctx is going out of scope - forget the global reference to it */
679 main_voc_ctx = 0;
680
681 /* delete the voc contxt */
682 vocterm(&vocctx);
683
684 /* delete the undo context */
685 if (undoptr != 0)
686 objuterm(undoptr);
687
688 /* release the object cache structures */
689 mcmcterm(mctx);
690 mcmterm(globalctx);
691 }
692
693 /*
694 * If the OS configuration so desires, use a less technical format for
695 * run-time error messages by leaving out the numeric error code. Note
696 * that we'll only do this if the error messages are linked directly
697 * into the run-time, since we need the numeric code as a last resort
698 * when the error message may not be present.
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 * If we didn't define a different error prefix format, use the default
708 * format with the numeric error code.
709 */
710 #ifndef TRDLOGERR_PREFIX
711 # define TRDLOGERR_PREFIX "\n[%s-%d: "
712 #endif
713
714 /* log an error */
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 /* display the prefix message to the console and log file */
723 sprintf(buf, TRDLOGERR_PREFIX, fac, err);
724 trdptf("%s", buf);
725 out_logfile_print(buf, FALSE);
726
727 /* display the error message text to the console and log file */
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 * close and delete the swap file
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 /* if no run context was supplied, find it from the main voc context */
747 if (runctx == 0)
748 {
749 /* if there is no main voc context, we're out of luck */
750 if (main_voc_ctx == 0)
751 return;
752
753 /* get the run context */
754 runctx = main_voc_ctx->voccxrun;
755 }
756
757 /* get the other relevant contexts */
758 mctx = runctx->runcxmem;
759 globalctx = mctx->mcmcxgl;
760 mcsctx = &globalctx->mcmcxswc;
761
762 /* if we have a swap file open, close it */
763 if (mcsctx->mcscxfp != 0)
764 {
765 /* close the file */
766 osfcls(mcsctx->mcscxfp);
767
768 /* forget about the file, so we don't try to close it again */
769 mcsctx->mcscxfp = (osfildef *)0;
770 }
771
772 /* if we have a filename, delete the file */
773 if (mcsctx->mcscxfname != 0)
774 {
775 /* delete the file */
776 osfdel_temp(mcsctx->mcscxfname);
777
778 /* forget the filename, so we don't try to delete the file again */
779 mchfre(mcsctx->mcscxfname);
780 mcsctx->mcscxfname = 0;
781 }
782 }
783
784 /* main - called by os main after setting up arguments */
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 /* copyright-date-string */
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 * log the error, unless it's usage (in which case we logged it
814 * already) or we're simply quitting the game
815 */
816 if (err != ERR_USAGE && err != ERR_RUNQUIT)
817 errclog(&errctx);
818
819 /* close the error file */
820 if (errctx.errcxfp != 0)
821 osfcls(errctx.errcxfp);
822
823 /* pause before exiting if the OS desires it */
824 os_expause();
825
826 /* return failure unless we're simply quitting the game */
827 return (err == ERR_RUNQUIT ? OSEXSUCC : OSEXFAIL);
828 ERREND(&errctx)
829
830 /* close the error file if we opened it */
831 if (errctx.errcxfp != 0)
832 osfcls(errctx.errcxfp);
833
834 /* successful completion */
835 return(OSEXSUCC);
836 }