Debian Codegraph @

terps/tads/tads2/tcd.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/TCD.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 tcd.c - tads2 compiler driver
15 Function
16 compiles a game and writes to a binary file
17 Notes
18 This is essentially for the command line interface; main() calls
19 tcdmain(), which compiles the game and writes to a binary file.
20 Modified
21 04/04/98 CNebel - Use new headers.
22 12/16/92 MJRoberts - add TADS/Graphic extensions
23 04/02/92 MJRoberts - creation
24 */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include "os.h"
32 #include "std.h"
33 #include "lin.h"
34 #include "linf.h"
35 #include "err.h"
36 #include "tok.h"
37 #include "mch.h"
38 #include "mcm.h"
39 #include "prs.h"
40 #include "emt.h"
41 #include "opc.h"
42 #include "run.h"
43 #include "voc.h"
44 #include "bif.h"
45 #include "dbg.h"
46 #include "sup.h"
47 #include "cmd.h"
48 #include "fio.h"
49 #include "oem.h"
50 #include "cmap.h"
51 #include "tcg.h"
52
53 /* this must be included after os.h, because of memory size #defines */
54 #include "tcd.h"
55
56 /* debugger not present */
57 int dbgpresent()
58 {
59 return FALSE;
60 }
61
62 /* dummy debugger functions */
63 void trchid() {}
64 void trcsho() {}
65
66 int dbgu_err_resume(dbgcxdef *ctx)
67 {
68 VARUSED(ctx);
69 return FALSE;
70 }
71
72 void dbguquitting(dbgcxdef *ctx)
73 {
74 VARUSED(ctx);
75 }
76
77 int dbgu_find_src(const char *origname, int origlen,
78 char *fullname, size_t full_len, int must_find_file)
79 {
80 VARUSED(origname);
81 VARUSED(origlen);
82 VARUSED(fullname);
83 VARUSED(full_len);
84 VARUSED(must_find_file);
85
86 /* indicate failure */
87 return FALSE;
88 }
89
90 struct runsdef *dbgfrfind(dbgcxdef *ctx, objnum frobj, uint frofs)
91 {
92 VARUSED(frobj);
93 VARUSED(frofs);
94 errsig(ctx->dbgcxerr, ERR_INACTFR);
95 return 0;
96 }
97
98 void dbgss(struct dbgcxdef *ctx, uint ofs, int instr, int err,
99 uchar *noreg *instrp)
100 {
101 VARUSED(ctx);
102 VARUSED(ofs);
103 VARUSED(instr);
104 VARUSED(err);
105 return;
106 }
107
108 int dbgstart(struct dbgcxdef *ctx)
109 {
110 VARUSED(ctx);
111 return(TRUE);
112 }
113
114 /* dummy functions to satisfy built-ins that can't be used by preinit */
115
116 void vocinialo(voccxdef *ctx, vocddef **what, int cnt)
117 {
118 VARUSED(ctx);
119 VARUSED(what);
120 VARUSED(cnt);
121 }
122
123 void vocremfd(voccxdef *ctx, vocddef *what, objnum func, prpnum prop,
124 runsdef *val, int err)
125 {
126 VARUSED(ctx);
127 VARUSED(what);
128 VARUSED(func);
129 VARUSED(prop);
130 VARUSED(val);
131 VARUSED(err);
132 }
133
134 void vocsetfd(voccxdef *ctx, vocddef *what, objnum func, prpnum prop,
135 uint tm, runsdef *val, int err)
136 {
137 VARUSED(ctx);
138 VARUSED(what);
139 VARUSED(func);
140 VARUSED(prop);
141 VARUSED(tm);
142 VARUSED(val);
143 VARUSED(err);
144 }
145
146 void vocturn(voccxdef *ctx, int cnt, int do_fuses)
147 {
148 VARUSED(ctx);
149 VARUSED(cnt);
150 VARUSED(do_fuses);
151 }
152
153 void vocdmnclr(voccxdef *ctx)
154 {
155 VARUSED(ctx);
156 }
157
158 int voclistlen(vocoldef *lst)
159 {
160 VARUSED(lst);
161 return 0;
162 }
163
164 void exedaem(voccxdef *ctx)
165 {
166 VARUSED(ctx);
167 }
168
169 int exefuse(voccxdef *ctx, int do_run)
170 {
171 VARUSED(ctx);
172 VARUSED(do_run);
173 return FALSE;
174 }
175
176 int execmd_recurs(voccxdef *ctx, objnum actor, objnum verb,
177 objnum dobj, objnum prep, objnum iobj,
178 int validate_dobj, int validate_iobj)
179 {
180 VARUSED(ctx);
181 VARUSED(actor);
182 VARUSED(verb);
183 VARUSED(dobj);
184 VARUSED(prep);
185 VARUSED(iobj);
186 return 0;
187 }
188
189 void voc_parse_tok(voccxdef *ctx) { VARUSED(ctx); }
190 void voc_parse_types(voccxdef *ctx) { VARUSED(ctx); }
191 void voc_parse_dict_lookup(voccxdef *ctx) { VARUSED(ctx); }
192 void voc_parse_np(voccxdef *ctx) { VARUSED(ctx); }
193 void voc_parse_disambig(voccxdef *ctx) { VARUSED(ctx); }
194 void voc_parse_replace_cmd(voccxdef *ctx) { VARUSED(ctx); }
195 void voc_push_objlist(voccxdef *ctx, objnum objlist[], int cnt)
196 {
197 VARUSED(ctx);
198 VARUSED(objlist);
199 VARUSED(cnt);
200 }
201
202 /* more stub items for run-time */
203 FILE *scrfp;
204 int scrquiet;
205
206 char *qasgets(char *buf, int bufl)
207 {
208 VARUSED(buf);
209 return((char *)0);
210 }
211
212 void qasclose(void)
213 {
214 }
215
216 void vocdusave_newobj(voccxdef *ctx, objnum objn) {}
217 void vocdusave_delobj(voccxdef *ctx, objnum objn) {}
218 void vocdusave_addwrd(voccxdef *ctx, objnum objn, prpnum typ, int flags,
219 char *wrd) {}
220 void vocdusave_delwrd(voccxdef *ctx, objnum objn, prpnum typ, int flags,
221 char *wrd) {}
222 void vocdusave_me(voccxdef *ctx, objnum old_me) {}
223
224 /*
225 * dummy runstat - we have no status line when running preinit under the
226 * compiler, so this doesn't need to do anything
227 */
228 void runstat(void)
229 {
230 }
231
232 /* printf-style formatting */
233 static void tcdptf(const char *fmt, ...)
234 {
235 char buf[256];
236 va_list va;
237
238 /* format the string */
239 va_start(va, fmt);
240 vsprintf(buf, fmt, va);
241 va_end(va);
242
243 /* print the formatted buffer */
244 os_printz(buf);
245 }
246
247 static void tcduspt(errcxdef *ec, int first, int last)
248 {
249 int i;
250 char buf[128];
251
252 for (i = first ; i <= last ; ++i)
253 {
254 errmsg(ec, buf, (uint)sizeof(buf), i);
255 tcdptf("%s\n", buf);
256 }
257 }
258
259 static void tcdtogusage(errcxdef *ec)
260 {
261 tcduspt(ec, ERR_TCTGUS1, ERR_TCTGUSL);
262 }
263
264 static void tcdusage(errcxdef *ec)
265 {
266 tcduspt(ec, ERR_TCUS1, ERR_TCUSL);
267 tcdtogusage(ec);
268 errsig(ec, ERR_USAGE);
269 }
270
271 static void tcdzusage(errcxdef *ec)
272 {
273 tcduspt(ec, ERR_TCZUS1, ERR_TCZUSL);
274 tcdtogusage(ec);
275 errsig(ec, ERR_USAGE);
276 }
277
278 static void tcd1usage(errcxdef *ec)
279 {
280 tcduspt(ec, ERR_TC1US1, ERR_TC1USL);
281 tcdtogusage(ec);
282 errsig(ec, ERR_USAGE);
283 }
284
285 static void tcdmusage(errcxdef *ec)
286 {
287 tcduspt(ec, ERR_TCMUS1, ERR_TCMUSL);
288 errsig(ec, ERR_USAGE);
289 }
290
291 static void tcdvusage(errcxdef *ec)
292 {
293 tcduspt(ec, ERR_TCVUS1, ERR_TCVUSL);
294 errsig(ec, ERR_USAGE);
295 }
296
297 /* context for tcdchkundef */
298 typedef struct tcdchkctx
299 {
300 mcmcxdef *mem;
301 errcxdef *ec;
302 int cnt;
303 } tcdchkctx;
304
305 /*
306 * Callback to check for undefined objects
307 */
308 static void tcdchkundef(void *ctx0, toksdef *t)
309 {
310 tcdchkctx *ctx = (tcdchkctx *)ctx0;
311 int err;
312
313 switch(t->tokstyp)
314 {
315 case TOKSTFWDOBJ:
316 /* undefined function */
317 err = ERR_UNDEFO;
318 goto log_the_error;
319
320 case TOKSTFWDFN:
321 /* undefined object */
322 err = ERR_UNDEFF;
323
324 log_the_error:
325 /* display the error */
326 sup_log_undefobj(ctx->mem, ctx->ec, err,
327 t->toksnam, (int)t->tokslen, (objnum)t->toksval);
328
329 /* count the symbol */
330 ++(ctx->cnt);
331 break;
332
333 default:
334 /* ignore defined objects */
335 break;
336 }
337 }
338
339
340 /*
341 * Error callback context
342 */
343 struct tcderrdef
344 {
345 FILE *tcderrfil; /* error logging file */
346 tokcxdef *tcderrtok; /* token parsing context */
347 int tcderrlvl; /* warning level */
348 int tcderrcnt; /* number of non-warning errors encountered */
349 int tcdwrncnt; /* number of warnings encountered */
350 int tcdfatal; /* a fatal error has occurred */
351 char tcdwrn_AMBIGBIN; /* warn on ERR_AMBIGBIN (-v-abin hides it) */
352 };
353 typedef struct tcderrdef tcderrdef;
354
355 /*
356 * Version string information. This information is used by both the
357 * pre-defined #define's that are set up by the compiler and by the
358 * version string display (keeping them common increases the likelihood
359 * that they'll remain in sync).
360 */
361 static char vsn_major[] = "2";
362 static char vsn_minor[] = "5";
363 static char vsn_maint[] = "17";
364
365
366 /*
367 * Default memory sizes, if previously defined
368 */
369 #ifndef TCD_SETTINGS_DEFINED
370 # define TCD_POOLSIZ (6 * 1024)
371 # define TCD_LCLSIZ 2048
372 # define TCD_HEAPSIZ 4096
373 # define TCD_STKSIZ 50
374 # define TCD_LABSIZ 1024
375 #endif
376
377 /* TRUE if pause after compile/run */
378 static int ex_pause = FALSE;
379
380 /* compiler main */
381 static void tcdmain1(errcxdef *ec, int argc, char *argv[], tcderrdef *errcbcx,
382 char *save_ext)
383 {
384 tokcxdef *tc;
385 prscxdef *pctx;
386 osfildef *swapfp = (osfildef *)0;
387 emtcxdef *ectx;
388 tokthdef symtab; /* hashed symbol table */
389 toktldef labtab; /* table for 'goto' labels */
390 runcxdef runctx;
391 bifcxdef bifctx;
392 uchar *labmem;
393 mcmon evalobj;
394 voccxdef vocctx;
395 void (*bif[100])(struct bifcxdef *, int);
396 mcmcxdef *mctx = 0;
397 mcmcx1def *globalctx;
398 objnum preinit;
399 dbgcxdef dbg;
400 supcxdef supctx;
401 int err;
402 char *swapname = 0;
403 char swapbuf[OSFNMAX];
404 char **argp;
405 char *arg;
406 char *outfile = (char *)0; /* .gam file */
407 char outfile_abs[OSFNMAX]; /* fully-qualified .gam file name */
408 char outfile_path[OSFNMAX]; /* absolute path to .gam file */
409 char *infile;
410 ulong swapsize = 0xffffffffL; /* allow unlimited swap space */
411 int swapena = OS_DEFAULT_SWAP_ENABLED; /* swapping enabled? */
412 linfdef *noreg linf = 0; /* input file line source */
413 int i;
414 char inbuf[OSFNMAX];
415 char outbuf[OSFNMAX];
416 ulong cachelimit = 0xffffffffL;
417 int argchecking = TRUE; /* enable run-time arg checking */
418 int v1kw = FALSE; /* v1 keyword compatibility mode */
419 int v1else = FALSE; /* v1 'else' compat - ignore ';' after '}' */
420 int stats = FALSE; /* show statistics when done */
421 int symdeb = FALSE; /* generate source-level debug info */
422 int symdeb2 = FALSE; /* new-style source-level debug info */
423 int genoutput = TRUE; /* FALSE if output file is suppressed */
424 uint poolsiz = TCD_POOLSIZ; /* default space for parse node pool */
425 uint lclsiz = TCD_LCLSIZ; /* default space for local variables */
426 uint heapsiz = TCD_HEAPSIZ;
427 uint stksiz = TCD_STKSIZ;
428 uint labsiz = TCD_LABSIZ;
429 char *binout = (char *)0; /* precompiled header out */
430 char *binin = (char *)0; /* precompiled header in */
431 fiolcxdef fiolctx; /* load context */
432 noreg int loadopen = FALSE;
433 uint inflags;
434 char *do_kw = (char *)0; /* replacement keyword for "do" */
435 int warnlevel = 0; /* warning level */
436 char *filever = "*"; /* .GAM file compatibility setting */
437 int c_mode = FALSE; /* use C-style operators */
438 char *sym;
439 int symlen;
440 char *expan;
441 int explen;
442 static char vsn_major_nm[] = "__TADS_VERSION_MAJOR";
443 static char vsn_minor_nm[] = "__TADS_VERSION_MINOR";
444 static char sys_name[] = "__TADS_SYSTEM_NAME";
445 static char oem_name[] = "__TADS_OEM_NAME";
446 static char dbg_name[] = "__DEBUG";
447 static char date_name[] = "__DATE__";
448 static char time_name[] = "__TIME__";
449 static char line_name[] = "__LINE__";
450 static char file_name[] = "__FILE__";
451 static char si_sysinfo[] = "__SYSINFO_SYSINFO";
452 static char si_version[] = "__SYSINFO_VERSION";
453 static char si_osname[] = "__SYSINFO_OS_NAME";
454 static char si_html[] = "__SYSINFO_HTML";
455 static char si_jpeg[] = "__SYSINFO_JPEG";
456 static char si_png[] = "__SYSINFO_PNG";
457 static char si_wav[] = "__SYSINFO_WAV";
458 static char si_midi[] = "__SYSINFO_MIDI";
459 static char si_wav_midi_ovl[] = "__SYSINFO_WAV_MIDI_OVL";
460 static char si_wav_ovl[] = "__SYSINFO_WAV_OVL";
461 static char si_pref_images[] = "__SYSINFO_PREF_IMAGES";
462 static char si_pref_sounds[] = "__SYSINFO_PREF_SOUNDS";
463 static char si_pref_music[] = "__SYSINFO_PREF_MUSIC";
464 static char si_pref_links[] = "__SYSINFO_PREF_LINKS";
465 static char si_mpeg[] = "__SYSINFO_MPEG_AUDIO";
466 static char si_mpeg1[] = "__SYSINFO_MPEG_AUDIO_1";
467 static char si_mpeg2[] = "__SYSINFO_MPEG_AUDIO_2";
468 static char si_mpeg3[] = "__SYSINFO_MPEG_AUDIO_3";
469 static char si_htmlmode[] = "__SYSINFO_HTML_MODE";
470 static char si_links_http[] = "__SYSINFO_LINKS_HTTP";
471 static char si_links_ftp[] = "__SYSINFO_LINKS_FTP";
472 static char si_links_news[] = "__SYSINFO_LINKS_NEWS";
473 static char si_links_mailto[] = "__SYSINFO_LINKS_MAILTO";
474 static char si_links_telnet[] = "__SYSINFO_LINKS_TELNET";
475 static char si_png_trans[] = "__SYSINFO_PNG_TRANS";
476 static char si_png_alpha[] = "__SYSINFO_PNG_ALPHA";
477 static char si_ogg[] = "__SYSINFO_OGG";
478 static char si_mng[] = "__SYSINFO_MNG";
479 static char si_mng_trans[] = "__SYSINFO_MNG_TRANS";
480 static char si_mng_alpha[] = "__SYSINFO_MNG_ALPHA";
481 static char si_txtcol[] = "__SYSINFO_TEXT_COLORS";
482 static char si_txthi[] = "__SYSINFO_TEXT_HILITE";
483 static char si_banners[] = "__SYSINFO_BANNERS";
484 static char si_interp_class[] = "__SYSINFO_INTERP_CLASS";
485 static char si_ic_text[] = "__SYSINFO_ICLASS_TEXT";
486 static char si_ic_textgui[] = "__SYSINFO_ICLASS_TEXTGUI";
487 static char si_ic_html[] = "__SYSINFO_ICLASS_HTML";
488 static char si_audio_fade[] = "__SYSINFO_AUDIO_FADE";
489 static char si_audio_crossfade[] = "__SYSINFO_AUDIO_CROSSFADE";
490 static char si_af_wav[] = "__SYSINFO_AUDIOFADE_WAV";
491 static char si_af_mpeg[] = "__SYSINFO_AUDIOFADE_MPEG";
492 static char si_af_ogg[] = "__SYSINFO_AUDIOFADE_OGG";
493 static char si_af_midi[] = "__SYSINFO_AUDIOFADE_MIDI";
494 time_t timer;
495 struct tm *tblock;
496 char *datetime;
497 char datebuf[15];
498 ulong totsize;
499 uchar *myheap;
500 runsdef *mystack;
501 char *strfile = 0; /* name of string capture file */
502 char *charmap = 0; /* character mapping file */
503 int checked_undefs = FALSE; /* checked for undefined symbols */
504
505 NOREG((&loadopen))
506 NOREG((&linf))
507
508 /* initialize the output formatter */
509 out_init();
510
511 /* presume no exit pause */
512 ex_pause = FALSE;
513
514 /* initialize lexical analysis context */
515 tc = tokcxini(ec, (mcmcxdef *)0, supsctab);
516 tc->tokcxdbg = &dbg;
517 ec->errcxlgc = errcbcx;
518 errcbcx->tcderrfil = 0; /* no error capture file yet */
519 errcbcx->tcderrtok = tc; /* to allow error logger to figure file/line */
520 errcbcx->tcderrlvl = 0; /* presume a warning level of 0 */
521
522 /* add current directory as first entry in include path */
523 tokaddinc(tc, "", 0);
524
525 /* parse arguments */
526 for (i = 1, argp = argv + 1 ; i < argc ; ++argp, ++i)
527 {
528 arg = *argp;
529 if (*arg == '-')
530 {
531 switch(*(arg+1))
532 {
533 case 'D':
534 /* get the symbol */
535 sym = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
536
537 /* look to see if there's a '=' specifying the expansion */
538 for (expan = sym ; *expan && *expan != '=' ; ++expan) ;
539
540 /* provide a default expansion of "1" if none was provided */
541 if (*expan == '\0')
542 {
543 expan = "1";
544 symlen = strlen(sym);
545 }
546 else
547 {
548 symlen = expan - sym;
549 ++expan;
550 }
551 explen = strlen(expan);
552
553 /* add the symbol */
554 tok_add_define_cvtcase(tc, sym, symlen, expan, explen);
555 break;
556
557 case 'U':
558 /* get the symbol and undefine it */
559 sym = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
560 symlen = strlen(sym);
561 tok_del_define(tc, sym, symlen);
562 break;
563
564 case 'C':
565 c_mode = cmdtog(ec, c_mode, arg, 1, tcdusage);
566 break;
567
568 case 'c':
569 {
570 int case_sensitive;
571
572 if (strlen(arg+1) >= 4
573 && !memcmp(arg+1, "case", (size_t)4))
574 {
575 case_sensitive =
576 cmdtog(ec, (tc->tokcxflg & TOKCXCASEFOLD) == 0,
577 arg, 4, tcdusage);
578 if (case_sensitive)
579 tc->tokcxflg &= ~TOKCXCASEFOLD;
580 else
581 tc->tokcxflg |= TOKCXCASEFOLD;
582 }
583 else if (!strcmp(arg+1, "ctab"))
584 {
585 /* get the character mapping table */
586 charmap = cmdarg(ec, &argp, &i, argc, 4, tcdusage);
587 }
588 else
589 tcdusage(ec);
590 break;
591 }
592
593 case 'e': /* error capture file */
594 {
595 char *errfname;
596 errfname = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
597 if (!(errcbcx->tcderrfil = fopen(errfname, "w")))
598 errsig(ec, ERR_ERRFIL);
599 break;
600 }
601
602 case 'd':
603 if (arg[2] == 's')
604 {
605 if (arg[3] == '2')
606 {
607 /* new-style - turn on symdeb2 as well as symdeb */
608 symdeb2 = cmdtog(ec, symdeb, arg, 3, tcdusage);
609 symdeb = symdeb2;
610 }
611 else
612 {
613 /* old-style - turn on only symdeb */
614 symdeb = cmdtog(ec, symdeb, arg, 2, tcdusage);
615 }
616 }
617 else
618 tcdusage(ec);
619 break;
620
621 case 'm':
622 switch(*(arg+2))
623 {
624 case '?':
625 tcdmusage(ec);
626
627 case 'g':
628 labsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tcdusage));
629 break;
630
631 case 'p':
632 poolsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tcdusage));
633 break;
634
635 case 'l':
636 lclsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tcdusage));
637 break;
638
639 case 's':
640 stksiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tcdusage));
641 break;
642
643 case 'h':
644 heapsiz = atoi(cmdarg(ec, &argp, &i, argc, 2, tcdusage));
645 break;
646
647 default:
648 cachelimit = atol(cmdarg(ec, &argp, &i, argc,
649 1, tcdusage));
650 break;
651 }
652 break;
653
654 case 'o':
655 if (arg[2] == '-')
656 genoutput = FALSE;
657 else
658 outfile = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
659 break;
660
661 case 's':
662 stats = cmdtog(ec, stats, arg, 1, tcdusage);
663 break;
664
665 case 't':
666 /* swap file options: -tf file, -ts size, -t- (no swap) */
667 switch(*(arg+2))
668 {
669 case 'f':
670 swapname = cmdarg(ec, &argp, &i, argc, 2, tcdusage);
671 break;
672
673 case 's':
674 swapsize = atol(cmdarg(ec, &argp, &i, argc, 2, tcdusage));
675 break;
676
677 default:
678 swapena = cmdtog(ec, swapena, arg, 1, tcdusage);
679 break;
680 }
681 break;
682
683 case 'i':
684 {
685 char *path = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
686 tokaddinc(tc, path, (int)strlen(path));
687 break;
688 }
689
690 case 'l':
691 binin = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
692 break;
693
694 case 'p':
695 ex_pause = cmdtog(ec, ex_pause, arg, 1, tcdusage);
696 break;
697
698 case 'w':
699 binout = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
700 break;
701
702 case 'f':
703 switch(arg[2])
704 {
705 case 'v': /* file version */
706 filever = cmdarg(ec, &argp, &i, argc, 2, tcdusage);
707 if (filever[1] != '\0' ||
708 (*filever != 'a' && *filever != 'b' &&
709 *filever != 'c' && *filever != '*'))
710 tcdusage(ec);
711 break;
712
713 default:
714 tcdusage(ec);
715 }
716 break;
717
718 case 'F':
719 switch(arg[2])
720 {
721 case 's':
722 strfile = cmdarg(ec, &argp, &i, argc, 2, tcdusage);
723 break;
724
725 default:
726 tcdusage(ec);
727 }
728 break;
729
730 case '1':
731 switch(arg[2])
732 {
733 case '?':
734 tcd1usage(ec);
735
736 case 'k':
737 v1kw = cmdtog(ec, v1kw, arg, 2, tcdusage);
738 break;
739
740 case 'e':
741 v1else = cmdtog(ec, v1else, arg, 2, tcdusage);
742 break;
743
744 case 'a':
745 argchecking = cmdtog(ec, argchecking, arg, 2, tcdusage);
746 break;
747
748 case 'd':
749 do_kw = cmdarg(ec, &argp, &i, argc, 2, tcdusage);
750 break;
751
752 case '\0':
753 /* no suboption - invert all v1 options */
754 argchecking = !argchecking;
755 v1else = !v1else;
756 v1kw = !v1kw;
757 break;
758
759 default:
760 tcdusage(ec);
761 }
762 break;
763
764 case 'v': /* verbosity (warning) level */
765 {
766 char *p;
767
768 /* get the argument, and see what we have */
769 p = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
770 if (isdigit((uchar)*p))
771 {
772 /* it's a numeric warning verbosity level */
773 warnlevel = atoi(p);
774 errcbcx->tcderrlvl = warnlevel;
775 }
776 else if (*p == '-' || *p == '+')
777 {
778 if (!strcmp(p+1, "abin"))
779 errcbcx->tcdwrn_AMBIGBIN = (*p == '+');
780 else
781 tcdvusage(ec);
782 }
783 else if (*p == '?')
784 tcdvusage(ec);
785 else
786 tcdusage(ec);
787 }
788 break;
789
790 case 'Z':
791 switch(arg[2])
792 {
793 case '?':
794 tcdzusage(ec);
795
796 case 'a':
797 argchecking = cmdtog(ec, argchecking, arg, 2, tcdusage);
798 break;
799
800 default:
801 tcdusage(ec);
802 }
803 break;
804
805 default:
806 tcdusage(ec);
807 }
808 }
809 else break;
810 }
811
812 /* load the character map */
813 if (cmap_load(charmap))
814 errsig(ec, ERR_INVCMAP);
815
816 /*
817 * Add built-in #define's for the system ID. We add a symbol named
818 * TADS_SYSTEM_NAME defined to an appropriate single-quoted string
819 * for the system, and we also add a #define of the same name as the
820 * expansion of TADS_SYSTEM_NAME and give it the value 1, for
821 * testing in #ifdef's.
822 */
823 {
824 char buf[40];
825
826 sprintf(buf, "'%s'", OS_SYSTEM_NAME);
827 tok_add_define_cvtcase(tc, sys_name, (int)sizeof(sys_name) - 1,
828 buf, (int)strlen(buf));
829 }
830 tok_add_define_cvtcase(tc, OS_SYSTEM_NAME, (int)strlen(OS_SYSTEM_NAME),
831 "1", 1);
832
833 /* add built-in #define's for major and minor version numbers */
834 tok_add_define_cvtcase(tc, vsn_major_nm, (int)sizeof(vsn_major_nm) - 1,
835 vsn_major, (int)sizeof(vsn_major) - 1);
836 tok_add_define_cvtcase(tc, vsn_minor_nm, (int)sizeof(vsn_minor_nm) - 1,
837 vsn_minor, (int)sizeof(vsn_minor) - 1);
838
839 /* add built-in #define for OEM name */
840 {
841 char buf[256];
842
843 sprintf(buf, "'%s'", TADS_OEM_NAME);
844 tok_add_define_cvtcase(tc, oem_name, (int)sizeof(oem_name) - 1,
845 buf, (int)strlen(buf));
846 }
847
848 /* get the current time and date */
849 timer = time(NULL);
850 tblock = localtime(&timer);
851 datetime = asctime(tblock);
852
853 /* build the __DATE__ definition, of the form 'Jan 01 1994' */
854 datebuf[0] = '\'';
855 memcpy(datebuf + 1, datetime + 4, (size_t)7);
856 memcpy(datebuf + 8, datetime + 20, (size_t)4);
857 datebuf[12] = '\'';
858 tok_add_define_cvtcase(tc, date_name, (int)sizeof(date_name) - 1,
859 datebuf, 13);
860
861 /* build the __TIME__ definition, of the form '01:23:45' */
862 memcpy(datebuf + 1, datetime + 11, (size_t)8);
863 datebuf[9] = '\'';
864 tok_add_define_cvtcase(tc, time_name, (int)sizeof(time_name)-1,
865 datebuf, 10);
866
867 /*
868 * add placeholders for __FILE__ and __LINE__ -- the actual
869 * definitions of these will be filled in dynamically whenever they
870 * are needed
871 */
872 tok_add_define_cvtcase(tc, file_name, (int)sizeof(file_name) - 1,
873 inbuf, OSFNMAX);
874 tok_add_define_cvtcase(tc, line_name, (int)sizeof(line_name) - 1,
875 inbuf, 40);
876
877 /*
878 * add the SYSINFO codes
879 */
880 tok_add_define_num_cvtcase(tc, si_sysinfo, (int)sizeof(si_sysinfo) - 1,
881 SYSINFO_SYSINFO);
882 tok_add_define_num_cvtcase(tc, si_version, (int)sizeof(si_version) - 1,
883 SYSINFO_VERSION);
884 tok_add_define_num_cvtcase(tc, si_osname, (int)sizeof(si_osname) - 1,
885 SYSINFO_OS_NAME);
886 tok_add_define_num_cvtcase(tc, si_html, (int)sizeof(si_html) - 1,
887 SYSINFO_HTML);
888 tok_add_define_num_cvtcase(tc, si_jpeg, (int)sizeof(si_jpeg) - 1,
889 SYSINFO_JPEG);
890 tok_add_define_num_cvtcase(tc, si_png, (int)sizeof(si_png) - 1,
891 SYSINFO_PNG);
892 tok_add_define_num_cvtcase(tc, si_wav, (int)sizeof(si_wav) - 1,
893 SYSINFO_WAV);
894 tok_add_define_num_cvtcase(tc, si_midi, (int)sizeof(si_midi) - 1,
895 SYSINFO_MIDI);
896 tok_add_define_num_cvtcase(tc, si_wav_midi_ovl,
897 (int)sizeof(si_wav_midi_ovl) - 1,
898 SYSINFO_WAV_MIDI_OVL);
899 tok_add_define_num_cvtcase(tc, si_wav_ovl, (int)sizeof(si_wav_ovl) - 1,
900 SYSINFO_WAV_OVL);
901 tok_add_define_num_cvtcase(tc, si_pref_images,
902 (int)sizeof(si_pref_images) - 1,
903 SYSINFO_PREF_IMAGES);
904 tok_add_define_num_cvtcase(tc, si_pref_sounds,
905 (int)sizeof(si_pref_sounds) - 1,
906 SYSINFO_PREF_SOUNDS);
907 tok_add_define_num_cvtcase(tc, si_pref_music,
908 (int)sizeof(si_pref_music) - 1,
909 SYSINFO_PREF_MUSIC);
910 tok_add_define_num_cvtcase(tc, si_pref_links,
911 (int)sizeof(si_pref_links) - 1,
912 SYSINFO_PREF_LINKS);
913 tok_add_define_num_cvtcase(tc, si_mpeg, (int)sizeof(si_mpeg) - 1,
914 SYSINFO_MPEG);
915 tok_add_define_num_cvtcase(tc, si_mpeg1, (int)sizeof(si_mpeg1) - 1,
916 SYSINFO_MPEG1);
917 tok_add_define_num_cvtcase(tc, si_mpeg2, (int)sizeof(si_mpeg2) - 1,
918 SYSINFO_MPEG2);
919 tok_add_define_num_cvtcase(tc, si_mpeg3, (int)sizeof(si_mpeg3) - 1,
920 SYSINFO_MPEG3);
921 tok_add_define_num_cvtcase(tc, si_htmlmode, (int)sizeof(si_htmlmode) - 1,
922 SYSINFO_HTML_MODE);
923 tok_add_define_num_cvtcase(tc, si_links_http,
924 (int)sizeof(si_links_http) - 1,
925 SYSINFO_LINKS_HTTP);
926 tok_add_define_num_cvtcase(tc, si_links_ftp,
927 (int)sizeof(si_links_ftp) - 1,
928 SYSINFO_LINKS_FTP);
929 tok_add_define_num_cvtcase(tc, si_links_news,
930 (int)sizeof(si_links_news) - 1,
931 SYSINFO_LINKS_NEWS);
932 tok_add_define_num_cvtcase(tc, si_links_mailto,
933 (int)sizeof(si_links_mailto) - 1,
934 SYSINFO_LINKS_MAILTO);
935 tok_add_define_num_cvtcase(tc, si_links_telnet,
936 (int)sizeof(si_links_telnet) - 1,
937 SYSINFO_LINKS_TELNET);
938 tok_add_define_num_cvtcase(tc, si_png_trans,
939 (int)sizeof(si_png_trans) - 1,
940 SYSINFO_PNG_TRANS);
941 tok_add_define_num_cvtcase(tc, si_png_alpha,
942 (int)sizeof(si_png_alpha) - 1,
943 SYSINFO_PNG_ALPHA);
944 tok_add_define_num_cvtcase(tc, si_ogg, (int)sizeof(si_ogg) - 1,
945 SYSINFO_OGG);
946 tok_add_define_num_cvtcase(tc, si_mng, (int)sizeof(si_mng) - 1,
947 SYSINFO_MNG);
948 tok_add_define_num_cvtcase(tc, si_mng_trans,
949 (int)sizeof(si_mng_trans) - 1,
950 SYSINFO_MNG_TRANS);
951 tok_add_define_num_cvtcase(tc, si_mng_alpha,
952 (int)sizeof(si_mng_alpha) - 1,
953 SYSINFO_MNG_ALPHA);
954 tok_add_define_num_cvtcase(tc, si_txthi, (int)sizeof(si_txthi) - 1,
955 SYSINFO_TEXT_HILITE);
956 tok_add_define_num_cvtcase(tc, si_txtcol, (int)sizeof(si_txtcol) - 1,
957 SYSINFO_TEXT_COLORS);
958 tok_add_define_num_cvtcase(tc, si_banners, (int)sizeof(si_banners) - 1,
959 SYSINFO_BANNERS);
960 tok_add_define_num_cvtcase(tc, si_interp_class,
961 (int)sizeof(si_interp_class) - 1,
962 SYSINFO_INTERP_CLASS);
963 tok_add_define_num_cvtcase(tc, si_ic_text,
964 (int)sizeof(si_ic_text) - 1,
965 SYSINFO_ICLASS_TEXT);
966 tok_add_define_num_cvtcase(tc, si_ic_textgui,
967 (int)sizeof(si_ic_textgui) - 1,
968 SYSINFO_ICLASS_TEXTGUI);
969 tok_add_define_num_cvtcase(tc, si_ic_html,
970 (int)sizeof(si_ic_html) - 1,
971 SYSINFO_ICLASS_HTML);
972 tok_add_define_num_cvtcase(tc, si_audio_fade,
973 (int)sizeof(si_audio_fade) - 1,
974 SYSINFO_AUDIO_FADE);
975 tok_add_define_num_cvtcase(tc, si_audio_crossfade,
976 (int)sizeof(si_audio_crossfade) - 1,
977 SYSINFO_AUDIO_CROSSFADE);
978 tok_add_define_num_cvtcase(tc, si_af_wav, (int)sizeof(si_af_wav) - 1,
979 SYSINFO_AUDIOFADE_WAV);
980 tok_add_define_num_cvtcase(tc, si_af_mpeg, (int)sizeof(si_af_mpeg) - 1,
981 SYSINFO_AUDIOFADE_MPEG);
982 tok_add_define_num_cvtcase(tc, si_af_ogg, (int)sizeof(si_af_ogg) - 1,
983 SYSINFO_AUDIOFADE_OGG);
984 tok_add_define_num_cvtcase(tc, si_af_midi, (int)sizeof(si_af_midi) - 1,
985 SYSINFO_AUDIOFADE_MIDI);
986
987 /* turn on the __DEBUG symbol if debugging is on */
988 if (symdeb)
989 tok_add_define_cvtcase(tc, dbg_name,
990 (int)(sizeof(dbg_name) - 1), "1", 1);
991
992 /* get input name argument, and make sure it's the last argument */
993 if (i == argc) tcdusage(ec);
994 infile = *argp;
995 if (i + 1 != argc) tcdusage(ec);
996
997 /* add default .T extension to input file */
998 strcpy(inbuf, infile);
999 os_defext(inbuf, "t");
1000
1001 /*
1002 * if no output file is specified, use input file minus extension
1003 * plus GAM extension; otherwise, use specified output file
1004 */
1005 if (outfile)
1006 {
1007 /* use their name exactly as-is */
1008 strcpy(outbuf, outfile);
1009
1010 /* if it's exactly the same as the input file, apply an extension */
1011 if (!strcmp(outfile, infile))
1012 {
1013 os_remext(outbuf);
1014 os_addext(outbuf, "gam");
1015 }
1016 }
1017 else
1018 {
1019 /* get the input name minus the current extension */
1020 strcpy(outbuf, infile);
1021 os_remext(outbuf);
1022
1023 /* add .GAM extension */
1024 os_addext(outbuf, "gam");
1025 }
1026
1027 /* set input/output file pointers to refer to generated filenames */
1028 infile = inbuf;
1029 outfile = outbuf;
1030
1031 /* echo information to error logging file if present */
1032 if (errcbcx->tcderrfil)
1033 {
1034 time_t t;
1035
1036 t = time(NULL);
1037 fprintf(errcbcx->tcderrfil, "TADS Compilation of %s\n%s\n",
1038 infile, ctime(&t));
1039 }
1040
1041 /* open up the swap file */
1042 if (swapena && swapsize)
1043 {
1044 swapfp = os_create_tempfile(swapname, swapbuf);
1045 if (swapname == 0) swapname = swapbuf;
1046 if (swapfp == 0) errsig(ec, ERR_OPSWAP);
1047 }
1048
1049 ERRBEGIN(ec)
1050
1051 /* initialize cache manager context */
1052 globalctx = mcmini(cachelimit, 128, swapsize, swapfp, 0, ec);
1053 mctx = mcmcini(globalctx, 128, fioldobj, &fiolctx,
1054 (void (*)(void *, mcmon))objrevert, (void *)0);
1055 mctx->mcmcxrvc = mctx;
1056
1057 tc->tokcxmem = mctx;
1058
1059 /* allocate and initialize parsing context */
1060
1061 totsize = sizeof(prscxdef) + (long)poolsiz + (long)lclsiz;
1062 if (totsize != (size_t)totsize)
1063 errsig1(ec, ERR_PRSCXSIZ, ERRTINT, (int)(65535 - sizeof(prscxdef)));
1064 pctx = (prscxdef *)mchalo(ec, (size_t)totsize, "tcdmain");
1065 pctx->prscxerr = ec;
1066 pctx->prscxtok = tc;
1067 pctx->prscxmem = mctx;
1068 pctx->prscxprp = 0;
1069 pctx->prscxext = 0;
1070 pctx->prscxnsiz = pctx->prscxrrst = poolsiz;
1071 pctx->prscxnrst = pctx->prscxpool;
1072 pctx->prscxplcl = &pctx->prscxpool[poolsiz];
1073 pctx->prscxslcl = lclsiz;
1074 pctx->prscxflg = 0;
1075 pctx->prscxnode = &pctx->prscxpool[0];
1076 pctx->prscxgtab = (toktdef *)&labtab;
1077 pctx->prscxvoc = &vocctx;
1078 pctx->prscxcpp = (char *)0;
1079 pctx->prscxcps = 0;
1080 pctx->prscxcpf = 0;
1081 pctx->prscxspp = (char *)0;
1082 pctx->prscxspf = 0;
1083 pctx->prscxsps = 0;
1084 pctx->prscxfsp = (uchar *)0;
1085 pctx->prscxfsf = 0;
1086 pctx->prscxfss = 0;
1087 pctx->prscxextc = 0;
1088
1089 /* open the strings file, if one was specified */
1090 if (strfile != 0)
1091 {
1092 /* open the file */
1093 pctx->prscxstrfile = osfopwt(strfile, OSFTTEXT);
1094
1095 /* complain if we couldn't open it */
1096 if (pctx->prscxstrfile == 0)
1097 errsig(ec, ERR_OPNSTRFIL);
1098 }
1099 else
1100 pctx->prscxstrfile = 0;
1101
1102 /* set up flags */
1103 if (argchecking) pctx->prscxflg |= PRSCXFARC;
1104 if (symdeb) pctx->prscxflg |= (PRSCXFLIN | PRSCXFLCL);
1105 if (symdeb2) pctx->prscxflg |= (PRSCXFLIN2 | PRSCXFLCL);
1106 if (v1else) pctx->prscxflg |= PRSCXFV1E;
1107 if (c_mode) tc->tokcxflg |= TOKCXFCMODE;
1108 if (symdeb2) tc->tokcxflg |= TOKCXFLIN2;
1109 if (*filever != '*' && *filever < 'c') pctx->prscxflg |= PRSCXFTPL1;
1110
1111 /* set up vocabulary context */
1112 vocini(&vocctx, ec, mctx, &runctx, (objucxdef *)0, 50, 50, 100);
1113 vocctx.voccxflg |= VOCCXFVWARN;
1114
1115 /* allocate stack and heap */
1116 totsize = (ulong)stksiz * (ulong)sizeof(runsdef);
1117 if (totsize != (size_t)totsize)
1118 errsig1(ec, ERR_STKSIZE, ERRTINT, (uint)(65535/sizeof(runsdef)));
1119 mystack = (runsdef *)mchalo(ec, (size_t)totsize, "runtime stack");
1120 myheap = mchalo(ec, (size_t)heapsiz, "runtime heap");
1121
1122 /* set up linear symbol table for 'goto' labels */
1123 labmem = mchalo(ec, (size_t)labsiz, "main1");
1124 toktlini(ec, &labtab, labmem, labsiz);
1125 labtab.toktlsc.toktnxt = (toktdef *)&symtab; /* 2nd to last table */
1126
1127 /* set up to read from input file */
1128 tc->tokcxsst = (ushort (*)(void *))prsxsst;
1129 tc->tokcxsad = (void (*)(void *, char *, ushort))prsxsad;
1130 tc->tokcxsend = (void (*)(void *))prsxsend;
1131 tc->tokcxscx = (void *)pctx;
1132
1133 /* set up debug context */
1134 dbg.dbgcxtio = (tiocxdef *)0;
1135 dbg.dbgcxtab = &symtab;
1136 dbg.dbgcxmem = mctx;
1137 dbg.dbgcxerr = ec;
1138 dbg.dbgcxfcn = 0;
1139 dbg.dbgcxdep = 0;
1140 dbg.dbgcxfid = 0; /* start file serial numbers at zero */
1141 dbg.dbgcxflg = 0;
1142
1143 vocctx.voccxrun = &runctx;
1144 runctx.runcxdbg = &dbg;
1145 dbg.dbgcxtab = &symtab;
1146
1147 /* initialize a file line source for the input file */
1148 if (!(linf = linfini(mctx, ec, infile, (int)strlen(infile),
1149 (tokpdef *)0, TRUE, symdeb2)))
1150 errsig(ec, ERR_OPNINP);
1151
1152 linf->linflin.linpar = (lindef *)0;
1153 dbg.dbgcxlin = &linf->linflin; /* source file is start of line chain */
1154 tc->tokcxhdr = (linfdef *)&linf->linflin;
1155
1156 /* set up debug line source chain with just this record */
1157 tc->tokcxlin = &linf->linflin;
1158 linf->linflin.linid = dbg.dbgcxfid++;
1159 linf->linflin.linnxt = (lindef *)0;
1160
1161 /* set up a hashed symbol table */
1162 tokthini(ec, mctx, (toktdef *)&symtab);
1163 pctx->prscxstab = (toktdef *)&symtab; /* add symbols here */
1164 /*was: tc->tokcxstab = (toktdef *)&labtab; */
1165 tc->tokcxstab = (toktdef *)&symtab; /* search for symbols here */
1166
1167 /* allocate a code generator context */
1168 ectx = (emtcxdef *)mchalo(ec,
1169 (sizeof(emtcxdef) + 511*sizeof(emtldef)),
1170 "tcdmain");
1171 ectx->emtcxerr = ec;
1172 ectx->emtcxmem = pctx->prscxmem;
1173 ectx->emtcxptr = 0; /* mcmalo(pctx->prscxmem, 1024, &ectx->emtcxobj); */
1174 ectx->emtcxlcnt = 512;
1175 ectx->emtcxfrob = MCMONINV; /* not using debugger frame locals */
1176 emtlini(ectx);
1177
1178 /* add code generator context to parser context */
1179 pctx->prscxemt = ectx;
1180
1181 /* get the absolute path for the .gam file */
1182 os_get_abs_filename(outfile_abs, sizeof(outfile_abs), outfile);
1183 os_get_path_name(outfile_path, sizeof(outfile_path), outfile_abs);
1184
1185 /* set up execution context */
1186 runctx.runcxerr = ec;
1187 runctx.runcxmem = pctx->prscxmem;
1188 runctx.runcxstk = mystack;
1189 runctx.runcxstop = &mystack[stksiz];
1190 runctx.runcxsp = mystack;
1191 runctx.runcxbp = mystack;
1192 runctx.runcxheap = myheap;
1193 runctx.runcxhp = myheap;
1194 runctx.runcxhtop = &myheap[heapsiz];
1195 runctx.runcxundo = (objucxdef *)0;
1196 runctx.runcxbcx = &bifctx;
1197 runctx.runcxbi = bif;
1198 runctx.runcxtio = (tiocxdef *)0;
1199 runctx.runcxdbg = &dbg;
1200 runctx.runcxvoc = &vocctx;
1201 runctx.runcxdmd = (void (*)(void *, objnum, prpnum))supcont;
1202 runctx.runcxdmc = &supctx;
1203 runctx.runcxgamename = outfile;
1204 runctx.runcxgamepath = outfile_path;
1205
1206 /* set up setup context */
1207 supctx.supcxerr = ec;
1208 supctx.supcxmem = mctx;
1209 supctx.supcxtab = &symtab;
1210 supctx.supcxbuf = (uchar *)0;
1211 supctx.supcxlen = 0;
1212 supctx.supcxvoc = &vocctx;
1213 supctx.supcxrun = &runctx;
1214
1215 /* set up built-in function context */
1216 CLRSTRUCT(bifctx);
1217 bifctx.bifcxerr = ec;
1218 bifctx.bifcxrun = &runctx;
1219 bifctx.bifcxtio = (tiocxdef *)0;
1220 bifctx.bifcxsavext = save_ext;
1221
1222 /* allocate object for expression code generation */
1223 mcmalo(pctx->prscxmem, (ushort)1024, &evalobj);
1224 mcmunlck(pctx->prscxmem, evalobj);
1225
1226 /* add vocabulary properties */
1227 pctx->prscxprp = PRP_LASTRSV + 1;
1228
1229 /* add the built-in functions, keywords, etc */
1230 suprsrv(&supctx, bif, (toktdef *)&symtab,
1231 (int)(sizeof(bif)/sizeof(bif[0])), v1kw, do_kw,
1232 tc->tokcxflg & TOKCXCASEFOLD);
1233
1234 /* load pre-compiled header file if one was specified */
1235 if (binin)
1236 {
1237 dbg.dbgcxfid--; /* reset the line id counter */
1238
1239 fiord(mctx, &vocctx, tc, binin, (char *)0, &fiolctx, &preinit,
1240 &inflags, tc->tokcxinc,
1241 &pctx->prscxfsp, &pctx->prscxfsf, &pctx->prscxprp, FALSE, 0,
1242 argv[0]);
1243 tc->tokcxhdr = (linfdef *)dbg.dbgcxlin;
1244 pctx->prscxfss = pctx->prscxfsf;
1245 pctx->prscxcpp = vocctx.voccxcpp;
1246 pctx->prscxcps =
1247 pctx->prscxcpf = vocctx.voccxcpl;
1248 pctx->prscxspp = vocctx.voccxspp;
1249 pctx->prscxsps =
1250 pctx->prscxspf = vocctx.voccxspl;
1251 loadopen = TRUE;
1252
1253 linf->linflin.linid = dbg.dbgcxfid++; /* get new line id for source */
1254
1255 /* use same case sensitivity as original compilation */
1256 if (inflags & FIOFCASE)
1257 tc->tokcxflg |= TOKCXCASEFOLD;
1258 else
1259 tc->tokcxflg &= ~TOKCXCASEFOLD;
1260
1261 /*
1262 * run through the arguments again, and remove any preprocessor
1263 * symbols loaded from the file that have been explicitly
1264 * undefined with -U in this run
1265 */
1266 for (i = 1, argp = argv + 1 ; i < argc ; ++argp, ++i)
1267 {
1268 arg = *argp;
1269 if (*arg == '-')
1270 {
1271 switch(*(arg+1))
1272 {
1273 case 'U':
1274 /* get the symbol and undefine it */
1275 sym = cmdarg(ec, &argp, &i, argc, 1, tcdusage);
1276 symlen = strlen(sym);
1277 tok_del_define(tc, sym, symlen);
1278 break;
1279
1280 default:
1281 /* ignore all other arguments on this pass */
1282 break;
1283 }
1284 }
1285 }
1286
1287 }
1288
1289 /* get the first token */
1290 prsrstn(pctx);
1291 toknext(tc);
1292
1293 /* parse the entire file, stopping when we encounter EOF */
1294 parse_loop:
1295 ERRBEGIN(ec)
1296 for ( ;; )
1297 {
1298 /* reset emit settings */
1299 ectx->emtcxofs = 0;
1300
1301 /* generate progress report */
1302 os_progress(((linfdef *)tc->tokcxlin)->linfnam,
1303 ((linfdef *)tc->tokcxlin)->linfnum);
1304
1305 /* cooperatively multitask if necessary, and check for interrupt */
1306 if (os_yield())
1307 errsig(ec, ERR_USRINT);
1308
1309 /* stop on a fatal error */
1310 if (errcbcx->tcdfatal)
1311 break;
1312
1313 /* stop on end of file */
1314 if (tc->tokcxcur.toktyp == TOKTEOF)
1315 break;
1316
1317 /* parse the next object or function definition */
1318 prscode(pctx, TRUE);
1319 }
1320 ERRCATCH(ec, err)
1321 if (err >= 100 && err < 450)
1322 {
1323 int brace_cnt;
1324 int paren_cnt;
1325
1326 /*
1327 * This is some kind of syntax error, which means we can
1328 * keep going. Log the error, throw away tokens up to the
1329 * next semicolon, then try to start again.
1330 */
1331 errclog(ec);
1332 for (brace_cnt = paren_cnt = 0 ;; )
1333 {
1334 switch(tc->tokcxcur.toktyp)
1335 {
1336 case TOKTSEM:
1337 /* if within braces, keep going */
1338 if (brace_cnt || paren_cnt)
1339 break;
1340
1341 /* FALLTHROUGH */
1342 case TOKTEOF:
1343 toknext(tc);
1344 goto parse_loop;
1345
1346 case TOKTLBRACE:
1347 /* count the brace, and clear the paren count */
1348 ++brace_cnt;
1349 paren_cnt = 0;
1350 break;
1351
1352 case TOKTRBRACE:
1353 /* uncount the brace, and clear the paren count */
1354 paren_cnt = 0;
1355 if (brace_cnt)
1356 --brace_cnt;
1357 break;
1358
1359 case TOKTLPAR:
1360 ++paren_cnt;
1361 break;
1362
1363 case TOKTRPAR:
1364 --paren_cnt;
1365 break;
1366 }
1367
1368 /* skip the token */
1369 toknext(tc);
1370 }
1371 }
1372
1373 /* can't deal with this error - resignal it */
1374 errrse(ec);
1375 ERREND(ec)
1376
1377 /* set up vocab context with format string information */
1378 vocctx.voccxcpp = pctx->prscxcpp;
1379 vocctx.voccxcpl = pctx->prscxcpf;
1380
1381 /* likewise the special word information */
1382 vocctx.voccxspp = pctx->prscxspp;
1383 vocctx.voccxspl = pctx->prscxspf;
1384
1385 if (!binout && !errcbcx->tcdfatal)
1386 {
1387 tcdchkctx cbctx;
1388
1389 /* apply TADS/Graphic external resources */
1390 tcgcomp(ec, pctx, infile);
1391
1392 /* tell output subsystem about format strings */
1393 tiosetfmt((tiocxdef *)0, &runctx, pctx->prscxfsp,
1394 (uint)pctx->prscxfsf);
1395
1396 /* get required object definitions if not precompiling headers */
1397 supfind(ec, &symtab, &vocctx, &preinit, warnlevel,
1398 tc->tokcxflg & TOKCXCASEFOLD);
1399
1400 /*
1401 * inherit vocabulary - do this before calling preinit, to ensure
1402 * that preinit sees the same inherited location and vocabulary
1403 * data that would be seen during normal execution
1404 */
1405 supivoc(&supctx);
1406
1407 /* check for undefined objects before running preinit */
1408 cbctx.mem = mctx;
1409 cbctx.ec = ec;
1410 cbctx.cnt = 0;
1411 toktheach(&symtab.tokthsc, tcdchkundef, &cbctx);
1412 checked_undefs = TRUE;
1413
1414 /*
1415 * run preinit function if it's defined, and we're not generating
1416 * debug output, and there have been no errors so far
1417 */
1418 if (preinit != MCMONINV && !symdeb
1419 && errcbcx->tcderrcnt == 0 && errcbcx->tcdfatal == 0)
1420 runfn(&runctx, preinit, 0);
1421 }
1422
1423 if (binout && errcbcx->tcderrcnt == 0)
1424 {
1425 fiowrt(mctx, &vocctx, tc, &symtab, pctx->prscxfsp,
1426 (uint)pctx->prscxfsf,
1427 binout, FIOFSYM + FIOFLIN + FIOFPRE + FIOFBIN
1428 + (tc->tokcxflg & TOKCXCASEFOLD ? FIOFCASE : 0),
1429 (objnum)MCMONINV, pctx->prscxextc, pctx->prscxprp, filever);
1430 }
1431
1432 /* write game to output if desired (and not precompiling headers) */
1433 if (genoutput && !binout
1434 && errcbcx->tcderrcnt == 0 && errcbcx->tcdfatal == 0)
1435 {
1436 fiowrt(mctx, &vocctx, tc,
1437 &symtab, pctx->prscxfsp, (uint)pctx->prscxfsf,
1438 outfile,
1439 (FIOFFAST
1440 + (symdeb ? (FIOFSYM + FIOFLIN + FIOFPRE
1441 + ((tc->tokcxflg & TOKCXCASEFOLD)
1442 ? FIOFCASE : 0)
1443 )
1444 : FIOFCRYPT
1445 )
1446 + (symdeb2 ? FIOFLIN2 : 0)
1447 ),
1448 preinit, pctx->prscxextc, (uint)0, filever);
1449 }
1450 else if (!binout && !checked_undefs)
1451 {
1452 tcdchkctx cbctx;
1453
1454 /* we're not writing the file, but still check for undefined objects */
1455 cbctx.mem = mctx;
1456 cbctx.ec = ec;
1457 cbctx.cnt = 0;
1458 toktheach(&symtab.tokthsc, tcdchkundef, &cbctx);
1459 }
1460
1461 /* show statistics if desired */
1462 if (stats)
1463 {
1464 int count;
1465 int i;
1466 int j;
1467 vocdef **vhsh;
1468 vocdef *voc;
1469 vocidef ***vpg;
1470 vocidef **v;
1471
1472 IF_DEBUG(extern ulong mchtotmem;)
1473
1474 tcdptf("\n* * * Statistics * * *\n");
1475 tcdptf("Virtual Object Cache size: %lu\n", mcmcsiz(mctx));
1476 tcdptf("Global symbol table size: %lu\n",
1477 ((ulong)symtab.tokthpcnt * (ulong)TOKTHSIZE));
1478 IF_DEBUG(tcdptf("Total heap memory: %lu\n", mchtotmem);)
1479
1480 /* count vocabulary words, eliminating duplicates */
1481 count = 0;
1482 for (i = 0, vhsh = vocctx.voccxhsh ; i < VOCHASHSIZ ; ++i, ++vhsh)
1483 {
1484 for (voc = *vhsh ; voc ; voc = voc->vocnxt)
1485 {
1486 /* check for duplicates; don't count if found one */
1487 ++count;
1488 #if 0
1489 /* no need to check for duplicates with new vocwdef system */
1490 {
1491 vocdef *voc2;
1492 for (voc2 = voc->vocnxt ; voc2 ; voc2 = voc2->vocnxt)
1493 {
1494 if (voc->voclen == voc2->voclen
1495 && voc->vocln2 == voc2->vocln2
1496 && !memcmp(voc->voctxt, voc2->voctxt,
1497 (size_t)(voc->voclen + voc->vocln2)))
1498 {
1499 --count;
1500 break;
1501 }
1502 }
1503 }
1504 #endif /* NEVER */
1505
1506 }
1507 }
1508 tcdptf("Vocabulary words: %d\n", count);
1509
1510 /* count objects, using inheritance records */
1511 count = 0;
1512 for (vpg = vocctx.voccxinh, i = 0 ; i < VOCINHMAX ; ++vpg, ++i)
1513 {
1514 if (!*vpg) continue;
1515 for (v = *vpg, j = 0 ; j < 256 ; ++v, ++j)
1516 {
1517 if (*v) ++count;
1518 }
1519 }
1520 tcdptf("Objects: %d\n", count);
1521 }
1522
1523 /* close and delete swapfile, if one was opened */
1524 if (swapfp)
1525 {
1526 osfcls(swapfp);
1527 swapfp = (osfildef *)0;
1528 osfdel_temp(swapname);
1529 }
1530
1531 /* close load file if one was open */
1532 if (loadopen)
1533 fiorcls(&fiolctx);
1534
1535 /* close error echo file */
1536 if (errcbcx->tcderrfil)
1537 {
1538 fclose(errcbcx->tcderrfil);
1539 errcbcx->tcderrfil = 0;
1540 }
1541
1542 /* close the string file */
1543 if (pctx->prscxstrfile != 0)
1544 {
1545 osfcls(pctx->prscxstrfile);
1546 pctx->prscxstrfile = 0;
1547 }
1548
1549 /* close the input line source */
1550 if (linf != 0)
1551 linfcls(&linf->linflin);
1552
1553 ERRCATCH(ec, err)
1554 /* if out of memory, describe current cache condition */
1555 if (err == ERR_NOMEM)
1556 {
1557 tcdptf("*** Note for -m option:\n");
1558 tcdptf("*** Current cache size is %lu\n", mcmcsiz(mctx));
1559 }
1560
1561 /* close and delete swapfile, if one was opened */
1562 if (swapfp)
1563 {
1564 osfcls(swapfp);
1565 swapfp = (osfildef *)0;
1566 osfdel_temp(swapname);
1567 }
1568
1569 /* close load file if one was opened */
1570 if (loadopen)
1571 fiorcls(&fiolctx);
1572
1573 /* close the input line source */
1574 if (linf != 0)
1575 linfcls(&linf->linflin);
1576
1577 /* resignal the error */
1578 errrse(ec);
1579 ERREND(ec)
1580 }
1581
1582 /* log an error */
1583 static void tcdlogerr(void *ectx0, char *fac, int err, int argc,
1584 erradef *argv)
1585 {
1586 tcderrdef *ectx = (tcderrdef *)ectx0;
1587 char buf[256];
1588 char msg[256];
1589 tokcxdef *ctx = ectx->tcderrtok;
1590 FILE *fp;
1591
1592 /* ignore certain errors if the warning level is low */
1593 switch(err)
1594 {
1595 case ERR_INCRPT:
1596 if (ectx->tcderrlvl < 1) return;
1597 break;
1598 }
1599
1600 if (!ctx)
1601 {
1602 tcdptf("%s-%d: error message not available\n", buf, fac, err);
1603 return;
1604 }
1605
1606 /* figure out what kind of error we have, and increment the counter */
1607 switch(err)
1608 {
1609 case ERR_AMBIGBIN:
1610 /* check for suppression, and ignore the error if suppressed */
1611 if (!ectx->tcdwrn_AMBIGBIN) return;
1612 goto do_warning;
1613
1614 case ERR_TRUNC:
1615 case ERR_INCRPT:
1616 case ERR_WEQASI:
1617 case ERR_STREND:
1618 case ERR_RPLSPEC:
1619 case ERR_VOCREVB:
1620 case ERR_LOCNOBJ:
1621 case ERR_CNTNLST:
1622 case ERR_WRNONF:
1623 case ERR_GNOFIL:
1624 case ERR_PUNDEF:
1625 case ERR_PIA:
1626 do_warning:
1627 /* warning - increment warning count */
1628 ++(ectx->tcdwrncnt);
1629 break;
1630
1631 case ERR_NOMEM:
1632 case ERR_FSEEK:
1633 case ERR_FREAD:
1634 case ERR_FWRITE:
1635 case ERR_NOPAGE:
1636 case ERR_SWAPBIG:
1637 case ERR_SWAPPG:
1638 case ERR_CLIFULL:
1639 case ERR_NOMEM1:
1640 case ERR_NOMEM2:
1641 case ERR_NOLCLSY:
1642 case ERR_MANYSYM:
1643 case ERR_NONODE:
1644 case ERR_VOCSTK:
1645 case ERR_MANYDBG:
1646 case ERR_VOCMNPG:
1647 case ERR_NOMEMLC:
1648 case ERR_NOMEMAR:
1649 /* flag the fatal error, and increment the error count */
1650 ectx->tcdfatal = TRUE;
1651 ++(ectx->tcderrcnt);
1652 break;
1653
1654 default:
1655 /* error - increment error count */
1656 ++(ectx->tcderrcnt);
1657 break;
1658 }
1659
1660 /*
1661 * certain special errors include the line position in the argument
1662 * vector itself
1663 */
1664 switch(err)
1665 {
1666 case ERR_UNDFOBJ:
1667 case ERR_UNDEFO:
1668 case ERR_UNDEFF:
1669 /* these errors include the position as the second argument */
1670 strcpy(buf, argv[1].errastr);
1671 break;
1672
1673 default:
1674 /* for other errors, get the location from the line source */
1675 if (ctx->tokcxlin != 0)
1676 linppos(ctx->tokcxlin, buf, (uint)sizeof(buf));
1677 else
1678 buf[0] = '\0';
1679 }
1680
1681 fp = ectx->tcderrfil;
1682
1683 tcdptf("%serror %s-%d: ", buf, fac, err);
1684 if (fp) fprintf(fp, "%serror %s-%d: ", buf, fac, err);
1685
1686 errmsg(ctx->tokcxerr, msg, (uint)sizeof(msg), err);
1687 errfmt(buf, (int)sizeof(buf), msg, argc, argv);
1688 tcdptf("%s\n", buf);
1689 if (fp) fprintf(fp, "%s\n", buf);
1690
1691 #ifdef OS_ERRLINE
1692 if ((err >= ERR_INVTOK && err < ERR_VOCINUS)
1693 || err == ERR_UNDFOBJ || err == ERR_UNDEFO || err == ERR_UNDEFF)
1694 {
1695 char *p;
1696 int len;
1697
1698 switch(err)
1699 {
1700 case ERR_UNDFOBJ:
1701 case ERR_UNDEFO:
1702 case ERR_UNDEFF:
1703 p = argv[1].errastr;
1704 p += strlen(p) + 1;
1705 len = strlen(p);
1706 break;
1707
1708 default:
1709 p = ctx->tokcxlin->linbuf;
1710 len = ctx->tokcxlin->linlen;
1711 break;
1712 }
1713
1714 if (len + 1 > sizeof(buf)) len = sizeof(buf) - 1;
1715 memcpy(buf, p, (size_t)len);
1716 buf[len] = '\0';
1717 tcdptf("%s\n\n", buf);
1718 if (fp) fprintf(fp, "%s\n\n", buf);
1719 }
1720 #endif /* OS_ERRLINE */
1721 }
1722
1723 /* pause before exiting, if desired */
1724 static void maybe_pause(void)
1725 {
1726 /* pause if desired */
1727 if (ex_pause)
1728 {
1729 tcdptf("[done with compilation - press a key to continue]");
1730 os_waitc();
1731 tcdptf("\n");
1732 }
1733 }
1734
1735 /* main - called by os main after setting up arguments */
1736 int tcdmain(int argc, char **argv, char *save_ext)
1737 {
1738 errcxdef errctx;
1739 int err;
1740 osfildef *fp;
1741 extern char tcgname[];
1742 tcderrdef errcbcx; /* error callback context */
1743 char vsnbuf[128];
1744
1745 /* initialize the error structure */
1746 CLRSTRUCT(errcbcx);
1747 errcbcx.tcdwrn_AMBIGBIN = TRUE; /* -v+abin by default */
1748
1749 errctx.errcxlog = tcdlogerr;
1750 errctx.errcxlgc = (void *)0;
1751 errctx.errcxofs = 0;
1752 errctx.errcxfp = (osfildef *)0;
1753 errctx.errcxappctx = 0;
1754 fp = oserrop(argv[0]);
1755 errini(&errctx, fp);
1756
1757 /* copyright-date-string */
1758 sprintf(vsnbuf, "%s v%s.%s.%s %s\n", tcgname,
1759 vsn_major, vsn_minor, vsn_maint,
1760 "Copyright (c) 1993, 2012 Michael J. Roberts");
1761 tcdptf(vsnbuf);
1762 sprintf(vsnbuf, "TADS for %s [%s] patchlevel %s.%s\n",
1763 OS_SYSTEM_LDESC, OS_SYSTEM_NAME,
1764 TADS_OEM_VERSION, OS_SYSTEM_PATCHSUBLVL);
1765 tcdptf(vsnbuf);
1766 tcdptf("%s maintains this port.\n", TADS_OEM_NAME);
1767
1768 ERRBEGIN(&errctx)
1769 tcdmain1(&errctx, argc, argv, &errcbcx, save_ext);
1770 ERRCATCH(&errctx, err)
1771 if (err != ERR_USAGE)
1772 errclog(&errctx);
1773
1774 if (errctx.errcxfp) osfcls(errctx.errcxfp);
1775 if (errcbcx.tcderrfil) fclose(errcbcx.tcderrfil);
1776 /* os_expause(); */
1777 maybe_pause();
1778 return(OSEXFAIL);
1779 ERREND(&errctx)
1780
1781 /* close message file */
1782 if (errctx.errcxfp) osfcls(errctx.errcxfp);
1783
1784 /* close error echo file if any */
1785 if (errcbcx.tcderrfil) fclose(errcbcx.tcderrfil);
1786
1787 /* os_expause(); */
1788 maybe_pause();
1789 return(errcbcx.tcderrcnt == 0 ? OSEXSUCC : OSEXFAIL);
1790 }
1791