Debian Codegraph @

terps/tads/tads2/tdd.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/TDD.C,v 1.4 1999/07/11 00:46:30 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 tdd.c - TADS 2 debugger main
15 Function
16 Main entrypoint for TADS 2 debugger
17 Notes
18 None
19 Modified
20 04/11/99 CNebel - Use new headers.
21 04/12/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 "prs.h"
40 #include "tok.h"
41 #include "emt.h"
42 #include "ply.h"
43 #include "oem.h"
44 #include "cmap.h"
45 #include "regex.h"
46
47
48 static void tddlogerr(void *ctx, char *fac, int err, int argc, erradef *argv);
49
50 /* printf-style formatter */
51 static void tddptf(const char *fmt, ...)
52 {
53 char buf[256];
54 va_list va;
55
56 /* format the string */
57 va_start(va, fmt);
58 vsprintf(buf, fmt, va);
59 va_end(va);
60
61 /* print the formatted buffer */
62 os_printz(buf);
63 }
64
65
66 static void tddusage_range(errcxdef *ec, int first_msg, int last_msg)
67 {
68 int i;
69 char buf[128];
70
71 for (i = first_msg ; i <= last_msg ; ++i)
72 {
73 errmsg(ec, buf, (uint)sizeof(buf), i);
74 tddptf("%s\n", buf);
75 }
76
77 errsig(ec, ERR_USAGE);
78 }
79
80 static void tddusage(errcxdef *ctx)
81 {
82 if (ctx->errcxappctx != 0 && ctx->errcxappctx->usage_app_name != 0)
83 {
84 char buf[128];
85 char buf2[128];
86 erradef argv[1];
87
88 /* get the parameterized usage message */
89 errmsg(ctx, buf, (uint)sizeof(buf), ERR_TRUSPARM);
90
91 /* format in the application name */
92 argv[0].errastr = (char *)ctx->errcxappctx->usage_app_name;;
93 errfmt(buf2, (int)sizeof(buf2), buf, 1, argv);
94
95 /* display it */
96 tddptf("%s\n", buf2);
97
98 /* display the rest of the messages */
99 tddusage_range(ctx, ERR_TDBUS1+1, ERR_TDBUSL);
100 }
101 else
102 {
103 /* display the default messages */
104 tddusage_range(ctx, ERR_TDBUS1, ERR_TDBUSL);
105 }
106 }
107
108 static void tddusage_s(errcxdef *ctx)
109 {
110 tddusage_range(ctx, ERR_TRSUS1, ERR_TRSUSL);
111 }
112
113
114 static void tddmain1(errcxdef *ec, int argc, char **argv, appctxdef *appctx,
115 char *save_ext)
116 {
117 osfildef *swapfp = (osfildef *)0;
118 runcxdef runctx;
119 bifcxdef bifctx;
120 voccxdef vocctx;
121 void (*bif[100])(struct bifcxdef *ctx, int argc);
122 mcmcxdef *mctx;
123 mcmcx1def *globalctx;
124 dbgcxdef dbg;
125 supcxdef supctx;
126 char *swapname = 0;
127 char swapbuf[OSFNMAX];
128 char **argp;
129 char *arg;
130 char *infile;
131 char infile_abs[OSFNMAX]; /* fully-qualified input file name */
132 char infile_path[OSFNMAX]; /* absolute path to input file */
133 ulong swapsize = 0xffffffffL; /* allow unlimited swap space */
134 int swapena = OS_DEFAULT_SWAP_ENABLED; /* swapping enabled? */
135 int i;
136 int pause = FALSE; /* pause after finishing game */
137 fiolcxdef fiolctx;
138 noreg int loadopen = FALSE;
139 char inbuf[OSFNMAX];
140 ulong cachelimit = 0xffffffff;
141 ushort undosiz = TDD_UNDOSIZ; /* default undo context size 16k */
142 objucxdef *undoptr;
143 uint flags; /* flags read from game file */
144 objnum preinit; /* preinit function object number */
145 prscxdef *pctx;
146 tokcxdef *tc;
147 emtcxdef *ectx;
148 uint heapsiz = TDD_HEAPSIZ;
149 uint stksiz = TDD_STKSIZ;
150 runsdef *mystack;
151 uchar *myheap;
152 uint lclsiz = TDD_LCLSIZ;
153 uint poolsiz = TDD_POOLSIZ;
154 int safety_read, safety_write; /* file safety level setting */
155 char *restore_file = 0; /* saved game file to restore */
156 char *charmap = 0; /* character mapping file */
157 int charmap_none = FALSE; /* use no character mapping */
158
159 NOREG((&loadopen))
160
161 /* initialize the output formatter */
162 out_init();
163
164 /* initialize lexical analysis context */
165 tc = tokcxini(ec, (mcmcxdef *)0, supsctab);
166 tc->tokcxdbg = &dbg;
167 tc->tokcxsst = (ushort (*)(void *))prsxsst;
168 tc->tokcxsad = (void (*)(void *, char *, ushort))prsxsad;
169 tc->tokcxsend = (void (*)(void *))prsxsend;
170
171 /* add current directory as first entry in search path */
172 tokaddinc(tc, "", 0);
173
174 /* set safety level 2 by default */
175 safety_read = safety_write = 2;
176
177 /* parse arguments */
178 for (i = 1, argp = argv + 1 ; i < argc ; ++argp, ++i)
179 {
180 arg = *argp;
181 if (*arg == '-')
182 {
183 switch(*(arg+1))
184 {
185 case 'c':
186 if (!strcmp(arg+1, "ctab-"))
187 {
188 /* do not use any character mapping table */
189 charmap_none = TRUE;
190 }
191 else if (!strcmp(arg+1, "ctab"))
192 {
193 /* get the character mapping table */
194 charmap = cmdarg(ec, &argp, &i, argc, 4, tddusage);
195 }
196 else
197 tddusage(ec);
198 break;
199
200 case 'r':
201 /* restore a game */
202 restore_file = cmdarg(ec, &argp, &i, argc, 1, tddusage);
203 break;
204
205 case 's':
206 {
207 char *p;
208
209 /* get the option */
210 p = cmdarg(ec, &argp, &i, argc, 1, tddusage);
211
212 /* if they're asking for help, display detailed usage */
213 if (*p == '?')
214 tddusage_s(ec);
215
216 /* get the safety level from the argument */
217 safety_read = *p - '0';
218 safety_write = (*(p+1) != '\0' ? *(p+1) - '0' :
219 safety_read);
220
221 /* range-check the values */
222 if (safety_read < 0 || safety_read > 4
223 || safety_write < 0 || safety_write > 4)
224 tddusage_s(ec);
225
226 /* tell the host system about the settings */
227 if (appctx != 0 && appctx->set_io_safety_level != 0)
228 (*appctx->set_io_safety_level)
229 (appctx->io_safety_level_ctx,
230 safety_read, safety_write);
231 }
232 break;
233
234 case 'm':
235 switch(*(arg + 2))
236 {
237 case 's':
238 stksiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tddusage));
239 break;
240
241 case 'h':
242 heapsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tddusage));
243 break;
244
245 case 'p':
246 poolsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tddusage));
247 break;
248
249 default:
250 cachelimit = atol(cmdarg(ec, &argp, &i, argc, 1,
251 tddusage));
252 break;
253 }
254 break;
255
256 case 't':
257 /* swap file options: -tf file, -ts size, -t- (no swap) */
258 switch(*(arg+2))
259 {
260 case 'f':
261 swapname = cmdarg(ec, &argp, &i, argc, 2, tddusage);
262 break;
263
264 case 's':
265 swapsize = atol(cmdarg(ec, &argp, &i, argc, 2, tddusage));
266 break;
267
268 default:
269 swapena = cmdtog(ec, swapena, arg, 1, tddusage);
270 break;
271 }
272 break;
273
274 case 'u':
275 undosiz = atoi(cmdarg(ec, &argp, &i, argc, 1, tddusage));
276 break;
277
278 case 'i':
279 {
280 char *path = cmdarg(ec, &argp, &i, argc, 1, tddusage);
281 tokaddinc(tc, path, (int)strlen(path));
282 break;
283 }
284
285 default:
286 tddusage(ec);
287 }
288 }
289 else break;
290 }
291
292 /* get input name argument */
293 if (i == argc)
294 {
295 /*
296 * no file was provided - check to see if the saved game file
297 * we're to restore specifies a game file; if that fails, see if
298 * the host system wants to provide a file
299 */
300 infile = 0;
301 if (restore_file != 0)
302 {
303 /* see if we can find the game name from the restore file */
304 if (fiorso_getgame(restore_file, inbuf, sizeof(inbuf)))
305 {
306 /* got it - use this file */
307 infile = inbuf;
308 }
309 }
310
311 /*
312 * if that didn't work, see if the host system can get us a file
313 */
314 if (infile == 0 && appctx != 0 && appctx->get_game_name != 0
315 && (*appctx->get_game_name)(appctx->get_game_name_ctx,
316 inbuf, sizeof(inbuf)))
317 {
318 /* the host system provided a name - use it */
319 infile = inbuf;
320 }
321 else
322 {
323 /*
324 * we can't find an input file - give the usage message and
325 * give up
326 */
327 tddusage(ec);
328 }
329 }
330 else
331 {
332 /* use the last argument as the filename */
333 infile = *argp;
334
335 /*
336 * make sure this is really the last argument - abort with the
337 * usage message if not
338 */
339 if (i + 1 != argc)
340 tddusage(ec);
341
342 /* if the original name exists, use it; otherwise, try adding .GAM */
343 if (osfacc(infile))
344 {
345 strcpy(inbuf, infile);
346 os_defext(inbuf, "gam");
347 infile = inbuf;
348 }
349 }
350
351 /* open up the swap file */
352 if (swapena && swapsize)
353 {
354 swapfp = os_create_tempfile(swapname, swapbuf);
355 if (swapname == 0) swapname = swapbuf;
356 if (swapfp == 0) errsig(ec, ERR_OPSWAP);
357 }
358
359 /* load the character map */
360 if (charmap_none)
361 cmap_override();
362 else if (cmap_load(charmap))
363 errsig(ec, ERR_INVCMAP);
364
365 ERRBEGIN(ec)
366
367 /* initialize cache manager context */
368 globalctx = mcmini(cachelimit, 128, swapsize, swapfp, 0, ec);
369 mctx = mcmcini(globalctx, 128, fioldobj, &fiolctx,
370 objrevert, (void *)0);
371 mctx->mcmcxrvc = mctx;
372
373 /* allocate and initialize parsing context */
374 pctx = (prscxdef *)mchalo(ec, (sizeof(prscxdef) + poolsiz +
375 lclsiz), "tcdmain");
376 pctx->prscxerr = ec;
377 pctx->prscxtok = tc;
378 pctx->prscxmem = mctx;
379 pctx->prscxprp = 0;
380 pctx->prscxext = 0;
381 pctx->prscxnsiz = pctx->prscxrrst = poolsiz;
382 pctx->prscxnrst = pctx->prscxpool;
383 pctx->prscxplcl = &pctx->prscxpool[poolsiz];
384 pctx->prscxslcl = lclsiz;
385 pctx->prscxflg = 0;
386 pctx->prscxnode = &pctx->prscxpool[0];
387 pctx->prscxgtab = (toktdef *)0;
388 pctx->prscxvoc = &vocctx;
389 pctx->prscxcpp = (char *)0;
390 pctx->prscxcps = 0;
391 pctx->prscxcpf = 0;
392 pctx->prscxfsp = (uchar *)0;
393 pctx->prscxfsf = 0;
394 pctx->prscxfss = 0;
395 pctx->prscxstab = (toktdef *)0;
396
397 tc->tokcxscx = (void *)pctx;
398
399 /* allocate a code generator context */
400 ectx = (emtcxdef *)mchalo(ec, (sizeof(emtcxdef) +
401 511*sizeof(emtldef)), "tcdmain");
402 ectx->emtcxerr = ec;
403 ectx->emtcxmem = pctx->prscxmem;
404 ectx->emtcxptr = 0; /* mcmalo(pctx->prscxmem, 1024, &ectx->emtcxobj); */
405 ectx->emtcxlcnt = 512;
406 ectx->emtcxfrob = MCMONINV;
407 emtlini(ectx);
408
409 pctx->prscxemt = ectx;
410
411 /* set up an undo context */
412 if (undosiz)
413 undoptr = objuini(mctx, undosiz, vocdundo, vocdusz, &vocctx);
414 else
415 undoptr = (objucxdef *)0;
416
417 /* set up vocabulary context */
418 vocini(&vocctx, ec, mctx, &runctx, undoptr, 50, 50, 100);
419
420 /* allocate stack and heap */
421 mystack = (runsdef *)mchalo(ec, (stksiz * sizeof(runsdef)),
422 "runtime stack");
423 myheap = mchalo(ec, heapsiz, "runtime heap");
424
425 /* get the absolute path for the input file */
426 os_get_abs_filename(infile_abs, sizeof(infile_abs), infile);
427 os_get_path_name(infile_path, sizeof(infile_path), infile_abs);
428
429 /* set up execution context */
430 runctx.runcxerr = ec;
431 runctx.runcxmem = mctx;
432 runctx.runcxstk = mystack;
433 runctx.runcxstop = &mystack[stksiz];
434 runctx.runcxsp = mystack;
435 runctx.runcxbp = mystack;
436 runctx.runcxheap = myheap;
437 runctx.runcxhp = myheap;
438 runctx.runcxhtop = &myheap[heapsiz];
439 runctx.runcxundo = undoptr;
440 runctx.runcxbcx = &bifctx;
441 runctx.runcxbi = bif;
442 runctx.runcxtio = (tiocxdef *)0;
443 runctx.runcxdbg = &dbg;
444 runctx.runcxvoc = &vocctx;
445 runctx.runcxdmd = supcont;
446 runctx.runcxdmc = &supctx;
447 runctx.runcxext = 0;
448 runctx.runcxgamename = infile;
449 runctx.runcxgamepath = infile_path;
450
451 /* set up setup context */
452 supctx.supcxerr = ec;
453 supctx.supcxmem = mctx;
454 supctx.supcxtab = (tokthdef *)0;
455 supctx.supcxbuf = (uchar *)0;
456 supctx.supcxlen = 0;
457 supctx.supcxvoc = &vocctx;
458 supctx.supcxrun = &runctx;
459
460 /* set up debug context */
461 dbg.dbgcxtio = (tiocxdef *)0;
462 dbg.dbgcxmem = mctx;
463 dbg.dbgcxerr = ec;
464 dbg.dbgcxtab = (tokthdef *)0;
465 dbg.dbgcxfcn = 0;
466 dbg.dbgcxdep = 0;
467 dbg.dbgcxflg = DBGCXFSS + DBGCXFOK; /* start off in single-step mode */
468 dbg.dbgcxprs = pctx;
469 dbg.dbgcxrun = &runctx;
470 dbg.dbgcxlin = (lindef *)0;
471 dbg.dbgcxnams = 2048;
472 dbg.dbgcxnam = (char *)mchalo(ec, dbg.dbgcxnams, "bp names");
473 dbg.dbgcxnamf = 0;
474 dbg.dbgcxhstl = 4096;
475 dbg.dbgcxhstf = 0;
476 dbg.dbgcxui = 0;
477 dbg.dbgcxhstp = (char *)mchalo(ec, dbg.dbgcxhstl, "history buf");
478
479 memset(dbg.dbgcxbp, 0, sizeof(dbg.dbgcxbp)); /* clear breakpoints */
480 memset(dbg.dbgcxwx, 0, sizeof(dbg.dbgcxwx)); /* clear watches */
481
482 /* set up built-in function context */
483 CLRSTRUCT(bifctx);
484 bifctx.bifcxerr = ec;
485 bifctx.bifcxrun = &runctx;
486 bifctx.bifcxtio = (tiocxdef *)0;
487 bifctx.bifcxrnd = 0;
488 bifctx.bifcxrndset = FALSE;
489 bifctx.bifcxappctx = appctx;
490 bifctx.bifcxsafetyr = safety_read;
491 bifctx.bifcxsafetyw = safety_write;
492 bifctx.bifcxsavext = save_ext;
493
494 /* initialize the regular expression parser context */
495 re_init(&bifctx.bifcxregex, ec);
496
497 ec->errcxlog = tddlogerr;
498 ec->errcxlgc = &dbg;
499
500 /* initialize debugger user interface */
501 dbguini(&dbg, infile);
502
503 /* read the game from the binary file */
504 fiord(mctx, &vocctx, (struct tokcxdef *)0,
505 infile, (char *)0, &fiolctx, &preinit, &flags,
506 tc->tokcxinc, (uchar **)0, (uint *)0, (uint *)0, FALSE, appctx,
507 argv[0]);
508 loadopen = TRUE;
509
510 /* use same case sensitivity as original compilation */
511 if (flags & FIOFCASE)
512 tc->tokcxflg |= TOKCXCASEFOLD;
513 else
514 tc->tokcxflg &= ~TOKCXCASEFOLD;
515
516 /* make sure the game was compiled for debugging */
517 if ((flags & (FIOFSYM + FIOFLIN + FIOFPRE)) !=
518 (FIOFSYM + FIOFLIN + FIOFPRE))
519 errsig(ec, ERR_NODBG);
520
521 /* add the built-in functions, keywords, etc */
522 suprsrv(&supctx, bif, &dbg.dbgcxtab->tokthsc,
523 (int)(sizeof(bif)/sizeof(bif[0])),
524 FALSE, (char *)0, tc->tokcxflg & TOKCXCASEFOLD);
525 tc->tokcxstab = (toktdef *)dbg.dbgcxtab;
526
527 /* do the second phase of debugger initialization */
528 dbguini2(&dbg);
529
530 /* set up status line hack */
531 runistat(&vocctx, &runctx, (tiocxdef *)0);
532
533 /* play the game, running preinit if necessary */
534 plygo(&runctx, &vocctx, (tiocxdef *)0, preinit, restore_file);
535
536 if (pause)
537 {
538 tddptf("[strike a key to exit]");
539 os_waitc();
540 tddptf("\n");
541 }
542
543 /* close load file */
544 fiorcls(&fiolctx);
545 loadopen = FALSE;
546
547 /* close and delete swapfile, if one was opened */
548 if (swapfp != 0)
549 {
550 osfcls(swapfp);
551 swapfp = (osfildef *)0;
552 osfdel_temp(swapname);
553 }
554
555 /* tell the debugger user interface that we're terminating */
556 dbguterm(&dbg);
557
558 ERRCLEAN(ec)
559 /* close and delete swapfile, if one was opened */
560 if (swapfp != 0)
561 {
562 osfcls(swapfp);
563 swapfp = (osfildef *)0;
564 osfdel_temp(swapname);
565 }
566
567 /* close the load file if one was opened */
568 if (loadopen)
569 fiorcls(&fiolctx);
570
571 /* tell the debugger user interface that we're terminating */
572 dbguterm(&dbg);
573
574 /* delete the voc context */
575 vocterm(&vocctx);
576
577 /* delete the undo context */
578 if (undoptr != 0)
579 objuterm(undoptr);
580
581 /* release the object cache structures */
582 mcmcterm(mctx);
583 mcmterm(globalctx);
584
585 ERRENDCLN(ec)
586
587 /* delete the voc context */
588 vocterm(&vocctx);
589
590 /* delete the undo context */
591 if (undoptr != 0)
592 objuterm(undoptr);
593
594 /* release the object cache structures */
595 mcmcterm(mctx);
596 mcmterm(globalctx);
597 }
598
599 /* log an error */
600 static void tddlogerr(void *ctx, char *fac, int err, int argc, erradef *argv)
601 {
602 char buf[512];
603 char msg[512];
604
605 errmsg(((dbgcxdef *)ctx)->dbgcxerr, msg, (uint)sizeof(msg), err);
606 errfmt(buf, (int)sizeof(buf), msg, argc, argv);
607 dbguerr((dbgcxdef *)ctx, err, buf);
608 }
609
610 /* log an error - used before debugger context is set up */
611 static void tddlogerr1(void *ctx0, char *fac, int err, int argc,
612 erradef *argv)
613 {
614 errcxdef *ctx = (errcxdef *)ctx0;
615 char buf[512];
616 char msg[512];
617
618 errmsg(ctx, msg, (uint)sizeof(msg), err);
619 errfmt(buf, (int)sizeof(buf), msg, argc, argv);
620 tddptf("%s-%d: %s\n", fac, err, buf);
621 }
622
623 /* main - called by os main after setting up arguments */
624 int tddmain(int argc, char **argv, appctxdef *appctx, char *save_ext)
625 {
626 errcxdef errctx;
627 int err;
628 osfildef *fp;
629
630 errctx.errcxlog = tddlogerr1;
631 errctx.errcxlgc = &errctx;
632 errctx.errcxfp = (osfildef *)0;
633 errctx.errcxofs = 0;
634 errctx.errcxappctx = appctx;
635 fp = oserrop(argv[0]);
636 errini(&errctx, fp);
637
638 /* copyright-date-string */
639 tddptf("%s - A %s TADS %s Debugger.\n"
640 "%sopyright (c) 1993, 2012 Michael J. Roberts\n",
641 G_tads_oem_dbg_name, G_tads_oem_display_mode, TADS_RUNTIME_VERSION,
642 G_tads_oem_copyright_prefix ? "TADS c" : "C");
643 tddptf("%s\n", G_tads_oem_author);
644
645 /* display any special port-specific startup message */
646 tddptf(OS_TDB_STARTUP_MSG);
647
648 ERRBEGIN(&errctx)
649 tddmain1(&errctx, argc, argv, appctx, save_ext);
650 ERRCATCH(&errctx, err)
651 /*
652 * log the error, unless it's usage (in which case we logged it
653 * already) or we're simply quitting the game
654 */
655 if (err != ERR_USAGE && err != ERR_RUNQUIT)
656 errclog(&errctx);
657
658 /* close the error file */
659 if (errctx.errcxfp != 0)
660 osfcls(errctx.errcxfp);
661
662 /* return failure unless we're simply quitting the game */
663 return (err == ERR_RUNQUIT ? OSEXSUCC : OSEXFAIL);
664 ERREND(&errctx)
665
666 /* close the error file if we opened it */
667 if (errctx.errcxfp != 0)
668 osfcls(errctx.errcxfp);
669
670 /* successful completion */
671 return OSEXSUCC;
672 }
673
674