Debian Codegraph @

terps/tads/tads2/bif.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/BIF.C,v 1.4 1999/07/11 00:46:29 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 bif.c - built-in function implementation
15 Function
16 Implements built-in functions for TADS
17 Notes
18 None
19 Modified
20 04/11/99 CNebel - fix signed/unsigned mismatches
21 12/16/92 MJRoberts - add TADS/Graphic functions
22 12/26/91 MJRoberts - creation
23 */
24
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <math.h>
30 #include <time.h>
31 #include "os.h"
32 #include "std.h"
33 #include "bif.h"
34 #include "tio.h"
35 #include "run.h"
36 #include "voc.h"
37 #include "fio.h"
38 #include "dbg.h"
39 #include "prp.h"
40 #include "lst.h"
41 #include "trd.h"
42 #include "regex.h"
43 #include "res.h"
44 #include "cmap.h"
45
46
47 /* yorn - yes or no */
48 void bifyon(bifcxdef *ctx, int argc)
49 {
50 char rsp[128];
51 char *p;
52 runsdef val;
53 char yesbuf[64];
54 char nobuf[64];
55 re_context rectx;
56 int match_yes;
57 int match_no;
58
59 bifcntargs(ctx, 0, argc); /* check for proper argument count */
60
61 /* load the "yes" and "no" reply patterns */
62 if (os_get_str_rsc(RESID_YORN_YES, yesbuf, sizeof(yesbuf)))
63 strcpy(yesbuf, "[Yy].*");
64 if (os_get_str_rsc(RESID_YORN_NO, nobuf, sizeof(nobuf)))
65 strcpy(nobuf, "[Nn].*");
66
67 /* if we're in HTML mode, switch to input font */
68 if (tio_is_html_mode())
69 tioputs(ctx->bifcxtio, "<font face='TADS-Input'>");
70
71 /* ensure the prompt is displayed */
72 tioflushn(ctx->bifcxtio, 0);
73
74 /* reset count of lines since the last keyboard input */
75 tioreset(ctx->bifcxtio);
76
77 /* read a line of text */
78 if (tiogets(ctx->bifcxtio, (char *)0, rsp, (int)sizeof(rsp)))
79 runsig(ctx->bifcxrun, ERR_RUNQUIT);
80
81 /* if we're in HTML mode, close the input font tag */
82 if (tio_is_html_mode())
83 tioputs(ctx->bifcxtio, "</font>");
84
85 /* scan off leading spaces */
86 for (p = rsp ; t_isspace(*p) ; ++p) ;
87
88 /* set up our regex context */
89 re_init(&rectx, ctx->bifcxerr);
90
91 /* check for a "yes" response */
92 match_yes = re_compile_and_match(&rectx, yesbuf, strlen(yesbuf),
93 p, strlen(p));
94
95 /* check for a "no" response */
96 match_no = re_compile_and_match(&rectx, nobuf, strlen(nobuf),
97 p, strlen(p));
98
99 /* check the result */
100 if (match_yes == (int)strlen(p))
101 val.runsv.runsvnum = 1;
102 else if (match_no == (int)strlen(p))
103 val.runsv.runsvnum = 0;
104 else
105 val.runsv.runsvnum = -1;
106
107 /* delete our regex context */
108 re_delete(&rectx);
109
110 /* push the result */
111 runpush(ctx->bifcxrun, DAT_NUMBER, &val);
112 }
113
114 /* setfuse */
115 void bifsfs(bifcxdef *ctx, int argc)
116 {
117 objnum func;
118 uint tm;
119 runsdef val;
120 voccxdef *voc = ctx->bifcxrun->runcxvoc;
121
122 bifcntargs(ctx, 3, argc); /* check for proper argument count */
123 func = runpopfn(ctx->bifcxrun);
124 tm = runpopnum(ctx->bifcxrun);
125 runpop(ctx->bifcxrun, &val);
126
127 /* limitation: don't allow string or list for value */
128 if (val.runstyp == DAT_LIST || val.runstyp == DAT_SSTRING)
129 runsig(ctx->bifcxrun, ERR_FUSEVAL);
130
131 vocsetfd(voc, voc->voccxfus, func, (prpnum)0,
132 tm, &val, ERR_MANYFUS);
133 }
134
135 /* remfuse */
136 void bifrfs(bifcxdef *ctx, int argc)
137 {
138 objnum func;
139 runsdef val;
140 voccxdef *voc = ctx->bifcxrun->runcxvoc;
141
142 bifcntargs(ctx, 2, argc);
143 func = runpopfn(ctx->bifcxrun);
144 runpop(ctx->bifcxrun, &val);
145 vocremfd(voc, voc->voccxfus, func, (prpnum)0,
146 &val, ERR_NOFUSE);
147 }
148
149 /* setdaemon */
150 void bifsdm(bifcxdef *ctx, int argc)
151 {
152 objnum func;
153 runsdef val;
154 voccxdef *voc = ctx->bifcxrun->runcxvoc;
155
156 bifcntargs(ctx, 2, argc); /* check for proper argument count */
157 func = runpopfn(ctx->bifcxrun);
158 runpop(ctx->bifcxrun, &val);
159
160 /* limitation: don't allow string or list for value */
161 if (val.runstyp == DAT_LIST || val.runstyp == DAT_SSTRING)
162 runsig(ctx->bifcxrun, ERR_FUSEVAL);
163
164 vocsetfd(voc, voc->voccxdmn, func, (prpnum)0, 0,
165 &val, ERR_MANYDMN);
166 }
167
168 /* remdaemon */
169 void bifrdm(bifcxdef *ctx, int argc)
170 {
171 objnum func;
172 runsdef val;
173 voccxdef *voc = ctx->bifcxrun->runcxvoc;
174
175 bifcntargs(ctx, 2, argc);
176 func = runpopfn(ctx->bifcxrun);
177 runpop(ctx->bifcxrun, &val);
178 vocremfd(voc, voc->voccxdmn, func, (prpnum)0,
179 &val, ERR_NODMN);
180 }
181
182 /* incturn */
183 void bifinc(bifcxdef *ctx, int argc)
184 {
185 int turncnt;
186
187 if (argc == 1)
188 {
189 /* get the number of turns to skip */
190 turncnt = runpopnum(ctx->bifcxrun);
191 if (turncnt < 1)
192 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "incturn");
193 }
194 else
195 {
196 /* no arguments -> increment by one turn */
197 bifcntargs(ctx, 0, argc);
198 turncnt = 1;
199 }
200
201 /* skip the given number of turns */
202 vocturn(ctx->bifcxrun->runcxvoc, turncnt, TRUE);
203 }
204
205 /* skipturn */
206 void bifskt(bifcxdef *ctx, int argc)
207 {
208 int turncnt;
209
210 bifcntargs(ctx, 1, argc);
211 turncnt = runpopnum(ctx->bifcxrun);
212 if (turncnt < 1)
213 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "skipturn");
214 vocturn(ctx->bifcxrun->runcxvoc, turncnt, FALSE);
215 }
216
217 /* quit */
218 void bifqui(bifcxdef *ctx, int argc)
219 {
220 /* check for proper argument count */
221 bifcntargs(ctx, 0, argc);
222
223 /* flush output buffer, and signal the end of the game */
224 tioflush(ctx->bifcxtio);
225 errsig(ctx->bifcxerr, ERR_RUNQUIT);
226 }
227
228 /* internal function to convert a TADS string into a C-string */
229 static void bifcstr(bifcxdef *ctx, char *buf, size_t bufsiz, uchar *str)
230 {
231 size_t srcrem;
232 size_t dstrem;
233 uchar *src;
234 char *dst;
235
236 /* get the length and text portion of the string */
237 srcrem = osrp2(str) - 2;
238 str += 2;
239
240 /* scan the string, and convert escapes */
241 for (src = str, dst = buf, dstrem = bufsiz ;
242 srcrem != 0 && dstrem != 0 ; ++src, --srcrem)
243 {
244 /* if we have an escape sequence, convert it */
245 if (*src == '\\')
246 {
247 /* skip the backslash in the input */
248 ++src;
249 --srcrem;
250
251 /* if there's nothing left, store the backslash */
252 if (srcrem == 0)
253 {
254 /* store the backslash */
255 *dst++ = '\\';
256 --dstrem;
257
258 /* there's nothing left to scan */
259 break;
260 }
261
262 /* see what the second half of the escape sequence is */
263 switch(*src)
264 {
265 case 'n':
266 /* store a C-style newline character */
267 *dst++ = '\n';
268 --dstrem;
269 break;
270
271 case 't':
272 /* store a C-style tab */
273 *dst++ = '\t';
274 --dstrem;
275 break;
276
277 case '(':
278 case ')':
279 /* entirely omit the highlighting sequences */
280 break;
281
282 default:
283 /* store everything else unchanged */
284 *dst++ = *src;
285 --dstrem;
286 break;
287 }
288 }
289 else
290 {
291 /* copy this character unchanged */
292 *dst++ = *src;
293 --dstrem;
294 }
295 }
296
297 /* if the buffer wasn't big enough, signal an error */
298 if (dstrem == 0)
299 runsig(ctx->bifcxrun, ERR_BIFCSTR);
300
301 /* null-terminate the result string */
302 *dst = '\0';
303 }
304
305 /* save */
306 void bifsav(bifcxdef *ctx, int argc)
307 {
308 uchar *fn;
309 char buf[OSFNMAX];
310 int err;
311 runsdef val;
312
313 bifcntargs(ctx, 1, argc);
314 fn = runpopstr(ctx->bifcxrun);
315 bifcstr(ctx, buf, (size_t)sizeof(buf), fn);
316 os_defext(buf, ctx->bifcxsavext != 0 ? ctx->bifcxsavext : "sav");
317 err = fiosav(ctx->bifcxrun->runcxvoc, buf, ctx->bifcxrun->runcxgamename);
318 runpush(ctx->bifcxrun, runclog(err), &val);
319 }
320
321 /* restore */
322 void bifrso(bifcxdef *ctx, int argc)
323 {
324 uchar *fn;
325 char buf[OSFNMAX];
326 int err;
327 voccxdef *vctx = ctx->bifcxrun->runcxvoc;
328
329 bifcntargs(ctx, 1, argc);
330
331 /* check for special restore(nil) - restore game given as parameter */
332 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
333 {
334 /* get filename from startup parameter, if any */
335 if (!os_paramfile(buf))
336 {
337 /* no startup parameter */
338 runpnum(ctx->bifcxrun, FIORSO_NO_PARAM_FILE);
339 return;
340 }
341 }
342 else
343 {
344 /* get string parameter - it's the filename */
345 fn = runpopstr(ctx->bifcxrun);
346 bifcstr(ctx, buf, (size_t)sizeof(buf), fn);
347 os_defext(buf, ctx->bifcxsavext != 0 ? ctx->bifcxsavext : "sav");
348 }
349
350 /* try restoring the file */
351 err = fiorso(vctx, buf);
352
353 /* blow away all undo records */
354 objulose(vctx->voccxundo);
355
356 /* return the result code from fiorso */
357 runpnum(ctx->bifcxrun, err);
358
359 /* note that the rest of the command line is to be ignored */
360 vctx->voccxflg |= VOCCXFCLEAR;
361 }
362
363 /* logging */
364 void biflog(bifcxdef *ctx, int argc)
365 {
366 char buf[OSFNMAX];
367 uchar *str;
368
369 bifcntargs(ctx, 1, argc);
370 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
371 {
372 rundisc(ctx->bifcxrun);
373 tiologcls(ctx->bifcxtio);
374 }
375 else
376 {
377 str = runpopstr(ctx->bifcxrun);
378 bifcstr(ctx, buf, (size_t)sizeof(buf), str);
379 tiologopn(ctx->bifcxtio, buf);
380 }
381 }
382
383 /* restart */
384 void bifres(bifcxdef *ctx, int argc)
385 {
386 voccxdef *vctx = ctx->bifcxrun->runcxvoc;
387 objnum fn;
388
389 if (argc == 2)
390 fn = runpopfn(ctx->bifcxrun); /* get function if present */
391 else
392 {
393 bifcntargs(ctx, 0, argc); /* check for proper argument count */
394 fn = MCMONINV; /* no function was specified */
395 }
396
397 objulose(vctx->voccxundo); /* blow away all undo records */
398 vocrevert(vctx); /* revert all objects to original state */
399 vocdmnclr(vctx); /* clear out fuses/deamons/notifiers */
400
401 /* restore the original "Me" object */
402 vctx->voccxme = vctx->voccxme_init;
403
404 /* call preinit if necessary (call it before invoking the user callback) */
405 if (vctx->voccxpreinit != MCMONINV)
406 runfn(ctx->bifcxrun, vctx->voccxpreinit, 0);
407
408 /*
409 * If a restart function was provided, call it. Note that we left
410 * the argument for the function on the stack, so there's no need to
411 * re-push it!
412 */
413 if (fn != MCMONINV) runfn(ctx->bifcxrun, fn, 1);
414
415 /* restart the game */
416 errsig(ctx->bifcxerr, ERR_RUNRESTART);
417 }
418
419 /* input - get a line of input from the keyboard */
420 void bifinp(bifcxdef *ctx, int argc)
421 {
422 char inbuf[128];
423
424 /* check for proper argument count */
425 bifcntargs(ctx, 0, argc);
426
427 /* make sure the prompt is displayed */
428 tioflushn(ctx->bifcxtio, 0);
429
430 /* reset count of lines since the last keyboard input */
431 tioreset(ctx->bifcxtio);
432
433 /* read a line of text */
434 if (tiogets(ctx->bifcxtio, (char *)0, inbuf, (int)sizeof(inbuf)))
435 runsig(ctx->bifcxrun, ERR_RUNQUIT);
436
437 /* push the string, converting escapes */
438 runpushcstr(ctx->bifcxrun, inbuf, strlen(inbuf), 0);
439 }
440
441 /* notify */
442 void bifnfy(bifcxdef *ctx, int argc)
443 {
444 objnum objn;
445 prpnum prp;
446 uint tm;
447 voccxdef *voc = ctx->bifcxrun->runcxvoc;
448
449 bifcntargs(ctx, 3, argc); /* check for proper argument count */
450 objn = runpopobj(ctx->bifcxrun);
451 prp = runpopprp(ctx->bifcxrun);
452 tm = runpopnum(ctx->bifcxrun);
453
454 /* a time of zero means every turn */
455 if (tm == 0)
456 tm = VOCDTIM_EACH_TURN;
457
458 vocsetfd(voc, voc->voccxalm, objn, prp, tm,
459 (runsdef *)0, ERR_MANYNFY);
460 }
461
462
463 /* unnotify */
464 void bifunn(bifcxdef *ctx, int argc)
465 {
466 objnum objn;
467 prpnum prop;
468 voccxdef *voc = ctx->bifcxrun->runcxvoc;
469
470 bifcntargs(ctx, 2, argc);
471 objn = runpopobj(ctx->bifcxrun);
472 prop = runpopprp(ctx->bifcxrun);
473 vocremfd(voc, voc->voccxalm, objn, prop,
474 (runsdef *)0, ERR_NONFY);
475 }
476
477 /* trace on/off */
478 void biftrc(bifcxdef *ctx, int argc)
479 {
480 runsdef val;
481 int n;
482 int flag;
483
484 if (argc == 2)
485 {
486 /* get the type indicator and the on/off status */
487 n = runpopnum(ctx->bifcxrun);
488 flag = runpoplog(ctx->bifcxrun);
489
490 /* see what type of debugging they want to turn on or off */
491 switch(n)
492 {
493 case 1:
494 /* turn on parser tracing */
495 if (flag)
496 ctx->bifcxrun->runcxvoc->voccxflg |= VOCCXFDBG;
497 else
498 ctx->bifcxrun->runcxvoc->voccxflg &= ~VOCCXFDBG;
499 break;
500
501 default:
502 /* ignore other requests */
503 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "debugTrace");
504 }
505 }
506 else
507 {
508 /* break into debugger; return whether debugger is present */
509 bifcntargs(ctx, 0, argc);
510 runpush(ctx->bifcxrun, runclog(dbgstart(ctx->bifcxrun->runcxdbg)),
511 &val);
512 }
513 }
514
515 /* say */
516 void bifsay(bifcxdef *ctx, int argc)
517 {
518 uchar *str;
519 long num;
520 char numbuf[30];
521
522 if (argc != 2) bifcntargs(ctx, 1, argc);
523
524 switch(runtostyp(ctx->bifcxrun))
525 {
526 case DAT_NUMBER:
527 num = runpopnum(ctx->bifcxrun);
528 sprintf(numbuf, "%ld", num);
529 tioputs(ctx->bifcxtio, numbuf);
530 break;
531
532 case DAT_SSTRING:
533 str = runpopstr(ctx->bifcxrun);
534 outfmt(ctx->bifcxtio, str);
535 break;
536
537 case DAT_NIL:
538 (void)runpoplog(ctx->bifcxrun);
539 break;
540
541 default:
542 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "say");
543 }
544 }
545
546 /* car */
547 void bifcar(bifcxdef *ctx, int argc)
548 {
549 uchar *lstp;
550 uint lstsiz;
551 runsdef val;
552
553 bifcntargs(ctx, 1, argc);
554 bifchkarg(ctx, DAT_LIST);
555
556 lstp = runpoplst(ctx->bifcxrun);
557
558 /* get list's size, and point to its data string */
559 lstsiz = osrp2(lstp) - 2;
560 lstp += 2;
561
562 /* push first element if one is present, otherwise push nil */
563 if (lstsiz)
564 runpbuf(ctx->bifcxrun, *lstp, lstp+1);
565 else
566 runpush(ctx->bifcxrun, DAT_NIL, &val);
567 }
568
569 /* cdr */
570 void bifcdr(bifcxdef *ctx, int argc)
571 {
572 uchar *lstp;
573 uint siz;
574 uint lstsiz;
575 runsdef val;
576 runsdef stkval;
577
578 bifcntargs(ctx, 1, argc);
579 bifchkarg(ctx, DAT_LIST);
580
581 lstp = runpoplst(ctx->bifcxrun);
582 stkval.runstyp = DAT_LIST;
583 stkval.runsv.runsvstr = lstp;
584
585 /* get list's size, and point to its data string */
586 lstsiz = osrp2(lstp) - 2;
587 lstp += 2;
588
589 if (lstsiz != 0)
590 {
591 /* deduct size of first element from size of list */
592 siz = datsiz(*lstp, lstp+1) + 1;
593 lstsiz -= siz;
594
595 /* add in the size prefix for our new list size */
596 lstsiz += 2;
597
598 /* allocate space for new list containing rest of list */
599 runhres1(ctx->bifcxrun, lstsiz, 1, &stkval);
600 lstp = stkval.runsv.runsvstr + siz + 2;
601
602 /* write out size followed by list value string */
603 oswp2(ctx->bifcxrun->runcxhp, lstsiz);
604 memcpy(ctx->bifcxrun->runcxhp+2, lstp, (size_t)(lstsiz-2));
605
606 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
607 val.runstyp = DAT_LIST;
608 ctx->bifcxrun->runcxhp += lstsiz;
609 runrepush(ctx->bifcxrun, &val);
610 }
611 else
612 runpush(ctx->bifcxrun, DAT_NIL, &val); /* empty list - cdr is nil */
613 }
614
615 /* caps */
616 void bifcap(bifcxdef *ctx, int argc)
617 {
618 bifcntargs(ctx, 0, argc);
619 tiocaps(ctx->bifxtio); /* set output driver next-char-capitalized flag */
620 }
621
622 /* nocaps */
623 void bifnoc(bifcxdef *ctx, int argc)
624 {
625 bifcntargs(ctx, 0, argc);
626 tionocaps(ctx->bifxtio); /* set next-not-capitalized flag */
627 }
628
629 /* length */
630 void biflen(bifcxdef *ctx, int argc)
631 {
632 uchar *p;
633 runsdef val;
634 long len;
635 int l;
636
637 bifcntargs(ctx, 1, argc);
638 switch(runtostyp(ctx->bifcxrun))
639 {
640 case DAT_SSTRING:
641 p = (uchar *)runpopstr(ctx->bifcxrun);
642 len = osrp2(p) - 2;
643 break;
644
645 case DAT_LIST:
646 p = runpoplst(ctx->bifcxrun);
647 l = osrp2(p) - 2;
648 p += 2;
649
650 /* count all elements in list */
651 for (len = 0 ; l ; ++len)
652 {
653 int cursiz;
654
655 /* get size of this element, and move past it */
656 cursiz = datsiz(*p, p+1) + 1;
657 l -= cursiz;
658 p += cursiz;
659 }
660 break;
661
662 default:
663 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "length");
664 }
665
666 val.runsv.runsvnum = len;
667 runpush(ctx->bifcxrun, DAT_NUMBER, &val);
668 }
669
670 /* find */
671 void biffnd(bifcxdef *ctx, int argc)
672 {
673 uchar *p1, *p2;
674 int len1, len2;
675 int outv;
676 runsdef val;
677 int typ;
678 int siz;
679
680 bifcntargs(ctx, 2, argc);
681 switch(runtostyp(ctx->bifcxrun))
682 {
683 case DAT_SSTRING:
684 p1 = runpopstr(ctx->bifcxrun);
685 len1 = osrp2(p1) - 2;
686 p1 += 2;
687
688 p2 = runpopstr(ctx->bifcxrun);
689 len2 = osrp2(p2) - 2;
690 p2 += 2;
691
692 /* look for p2 within p1 */
693 for (typ = DAT_NIL, outv = 1 ; len1 >= len2 ; ++p1, --len1, ++outv)
694 {
695 if (!memcmp(p1, p2, (size_t)len2))
696 {
697 typ = DAT_NUMBER; /* use number in outv after all */
698 break; /* that's it - we've found it */
699 }
700 }
701 break;
702
703 case DAT_LIST:
704 p1 = runpoplst(ctx->bifcxrun);
705 len1 = osrp2(p1) - 2;
706 p1 += 2;
707
708 /* get second item: any old datatype */
709 runpop(ctx->bifcxrun, &val);
710
711 for (typ = DAT_NIL, outv = 1 ; len1 ; ++outv, p1 += siz, len1 -= siz)
712 {
713 siz = datsiz(*p1, p1 + 1) + 1; /* get size of this element */
714 if (val.runstyp != *p1) continue; /* types don't match */
715
716 switch(val.runstyp)
717 {
718 case DAT_NUMBER:
719 if (val.runsv.runsvnum != osrp4s(p1 + 1)) continue;
720 break;
721
722 case DAT_SSTRING:
723 case DAT_LIST:
724 if (osrp2(p1 + 1) != osrp2(val.runsv.runsvstr) ||
725 memcmp(p1 + 3, val.runsv.runsvstr + 2,
726 (size_t)(osrp2(p1 + 1) - 2)))
727 continue;
728 break;
729
730 case DAT_PROPNUM:
731 if (osrp2(p1 + 1) != val.runsv.runsvprp) continue;
732 break;
733
734 case DAT_OBJECT:
735 case DAT_FNADDR:
736 if (osrp2(p1 + 1) != val.runsv.runsvobj) continue;
737 break;
738
739 default:
740 break;
741 }
742
743 /* if we got here, it means we found a match */
744 typ = DAT_NUMBER; /* use the value in outv */
745 break; /* that's it - we've found it */
746 }
747 break;
748
749 default:
750 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "find");
751 }
752
753 /* push the value given by typ and outv */
754 val.runsv.runsvnum = outv;
755 runpush(ctx->bifcxrun, typ, &val);
756 }
757
758 /* setit - set current 'it' */
759 void bifsit(bifcxdef *ctx, int argc)
760 {
761 objnum obj;
762 int typ;
763 voccxdef *vcx = ctx->bifcxrun->runcxvoc;
764
765 /* check for extended version that allows setting him/her */
766 if (argc == 2)
767 {
768 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
769 {
770 rundisc(ctx->bifcxrun); /* discard the nil */
771 obj = MCMONINV; /* use invalid object */
772 }
773 else
774 obj = runpopobj(ctx->bifcxrun); /* get the object */
775
776 typ = runpopnum(ctx->bifcxrun); /* get the code */
777 vcx->voccxthc = 0; /* clear the 'them' list */
778
779 switch(typ)
780 {
781 case 0: /* set "it" */
782 vcx->voccxit = obj;
783 break;
784
785 case 1: /* set "him" */
786 vcx->voccxhim = obj;
787 break;
788
789 case 2: /* set "her" */
790 vcx->voccxher = obj;
791 break;
792 }
793 return;
794 }
795
796 /* "setit classic" has one argument only */
797 bifcntargs(ctx, 1, argc);
798
799 /* check to see if we're setting 'it' or 'them' */
800 if (runtostyp(ctx->bifcxrun) == DAT_LIST)
801 {
802 uchar *lst;
803 uint siz;
804 int cnt;
805
806 lst = runpoplst(ctx->bifcxrun);
807 siz = osrp2(lst);
808 lst += 2;
809 siz -= 2;
810
811 for (cnt = 0 ; siz ; )
812 {
813 /* if this is an object, add to 'them' list (otherwise ignore) */
814 if (*lst == DAT_OBJECT)
815 vcx->voccxthm[cnt++] = osrp2(lst+1);
816
817 lstadv(&lst, &siz);
818 }
819 vcx->voccxthc = cnt;
820 vcx->voccxit = MCMONINV;
821 }
822 else
823 {
824 /* set 'it', and delete 'them' list */
825 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
826 {
827 vcx->voccxit = MCMONINV;
828 rundisc(ctx->bifcxrun);
829 }
830 else
831 vcx->voccxit = runpopobj(ctx->bifcxrun);
832 vcx->voccxthc = 0;
833 }
834 }
835
836 /* randomize - seed random number generator */
837 void bifsrn(bifcxdef *ctx, int argc)
838 {
839 bifcntargs(ctx, 0, argc);
840 os_rand(&ctx->bifcxrnd);
841 ctx->bifcxrndset = TRUE;
842 }
843
844 /* rand - get a random number */
845 void bifrnd(bifcxdef *ctx, int argc)
846 {
847 unsigned long result, max, randseed;
848 int tmp;
849 runsdef val;
850
851 /* get argument - number giving upper bound of generated number */
852 bifcntargs(ctx, 1, argc);
853 bifchkarg(ctx, DAT_NUMBER);
854 max = runpopnum(ctx->bifcxrun);
855
856 /* if the max is zero, just return zero */
857 if (max == 0)
858 {
859 runpnum(ctx->bifcxrun, 0);
860 return;
861 }
862
863 /*
864 * If the random number generator has been seeded by a call to
865 * randomize(), use the new, improved random number generator. If
866 * not, use the old random number generator to ensure that the same
867 * sequence of numbers is generated as always (to prevent breaking
868 * existing test scripts based on the old sequence).
869 */
870 if (!ctx->bifcxrndset)
871 {
872 /* compute the next number in sequence, using old cheesy generator */
873 randseed = ctx->bifcxrnd;
874 randseed *= 1033;
875 randseed += 5;
876 tmp = randseed / 16384;
877 randseed %= 16384;
878 result = tmp / 7;
879
880 /* adjust the result to be in the requested range */
881 result = ( randseed % max ) + 1;
882
883 /* save the new seed value, and return the value */
884 ctx->bifcxrnd = randseed;
885 val.runsv.runsvnum = result;
886 runpush(ctx->bifcxrun, DAT_NUMBER, &val);
887 }
888 else
889 {
890 #define BIF_RAND_M ((ulong)2147483647)
891 #define BIF_RAND_Q ((ulong)127773)
892 #define BIF_RAND_A ((ulong)16807)
893 #define BIF_RAND_R ((ulong)2836)
894
895 long lo, hi, test;
896
897 lo = ctx->bifcxrnd / BIF_RAND_Q;
898 hi = ctx->bifcxrnd % BIF_RAND_Q;
899 test = BIF_RAND_A*lo - BIF_RAND_R*hi;
900 ctx->bifcxrnd = test;
901 if (test > 0)
902 ctx->bifcxrnd = test;
903 else
904 ctx->bifcxrnd = test + BIF_RAND_M;
905 runpnum(ctx->bifcxrun, (((ulong)ctx->bifcxrnd) % max) + 1);
906 }
907 }
908
909 /*
910 * case-insensitive substring matching
911 */
912 static char *bif_stristr(const char *s1, const char *s2)
913 {
914 size_t s1len;
915 size_t s2len;
916
917 /* scan for a match */
918 for (s1len = strlen(s1), s2len = strlen(s2) ; s1len >= s2len ;
919 ++s1, --s1len)
920 {
921 /* if this is a match, return this substring */
922 if (memicmp(s1, s2, s2len) == 0)
923 return (char *)s1;
924 }
925
926 return 0;
927 }
928
929 /*
930 * askfile flags
931 */
932 #define BIF_ASKF_EXT_RET 1 /* extended return codes */
933
934 /*
935 * askfile
936 */
937 void bifask(bifcxdef *ctx, int argc)
938 {
939 uchar *prompt;
940 char buf[OSFNMAX + 2];
941 char pbuf[128];
942 int err;
943 int prompt_type;
944 int file_type;
945 ulong flags;
946
947 /* make sure we have an acceptable number of arguments */
948 if (argc != 1 && argc != 3 && argc != 4)
949 runsig(ctx->bifcxrun, ERR_BIFARGC);
950
951 /* get the first argument - the prompt string */
952 prompt = runpopstr(ctx->bifcxrun);
953 bifcstr(ctx, pbuf, (size_t)sizeof(pbuf), prompt);
954
955 /* presume we will have no flags */
956 flags = 0;
957
958 /* if we have the prompt type and file type parameters, get them */
959 if (argc >= 3)
960 {
961 /* get the prompt-type and the file-type arguments */
962 prompt_type = (int)runpopnum(ctx->bifcxrun);
963 file_type = (int)runpopnum(ctx->bifcxrun);
964
965 /* if we have a fourth argument, it's the flags */
966 if (argc == 4)
967 flags = runpopnum(ctx->bifcxrun);
968 }
969 else
970 {
971 static const char *save_strs[] =
972 {
973 "save",
974 "write",
975 0
976 };
977 const char **sp;
978
979 /*
980 * No prompt type or file type were specified. Try to infer the
981 * dialog type and file type from the text of the prompt. (This
982 * is mostly to support older games, in particular those based
983 * on older versions of adv.t, since newer games should always
984 * provide explicit values for the file type and dialog type.
985 * We are thus inferring the types based on the prompt strings
986 * that older adv.t's used when calling askfile.)
987 *
988 * If the prompt contains any substring such as "save" or
989 * "write", specify that we're saving; otherwise, assume that
990 * we're opening an existing file for reading.
991 *
992 * If the prompt contains the substrings "restore" or "save",
993 * assume that we're opening a save file; otherwise, don't make
994 * any assumptions, and use the "unknown" file type.
995 */
996
997 /* presume we're going to open an existing file of unknown type */
998 prompt_type = OS_AFP_OPEN;
999 file_type = OSFTUNK;
1000
1001 /* look for any one of the "save" substrings */
1002 for (sp = save_strs ; *sp != 0 ; ++sp)
1003 {
1004 /* check to see if this substring matches */
1005 if (bif_stristr(pbuf, *sp))
1006 {
1007 /* found it - use the "save" prompt */
1008 prompt_type = OS_AFP_SAVE;
1009
1010 /* no need to look any further */
1011 break;
1012 }
1013 }
1014
1015 /* look for either the substring "restore" or "save". */
1016 if (bif_stristr(pbuf, "restore") || bif_stristr(pbuf, "save"))
1017 {
1018 file_type = OSFTSAVE;
1019 }
1020
1021 /* check for a transcript */
1022 if (file_type == OSFTUNK
1023 && prompt_type == OS_AFP_SAVE
1024 && bif_stristr(pbuf, "script") != 0)
1025 {
1026 /* looks like a log file */
1027 file_type = OSFTLOG;
1028 }
1029 }
1030
1031 /* ask for a file */
1032 err = tio_askfile(pbuf, buf, (int)sizeof(buf), prompt_type, file_type);
1033
1034 /*
1035 * if the caller requested extended return codes, return a list
1036 * containing the return code as the first element and, if
1037 * successful, the string as the second element
1038 */
1039 if ((flags & BIF_ASKF_EXT_RET) != 0)
1040 {
1041 ushort len;
1042 runsdef val;
1043 uchar *p;
1044
1045 /*
1046 * Allocate space for the starter list - if we have a string to
1047 * return, just allocate space for the number element for now;
1048 * otherwise, allocate space for the number plus a nil second
1049 * element (one byte).
1050 */
1051 len = 2 + (1 + 4);
1052 if (err != OS_AFE_SUCCESS)
1053 ++len;
1054
1055 /* allocate the space */
1056 runhres(ctx->bifcxrun, len, 0);
1057
1058 /* set up our list pointer */
1059 val.runstyp = DAT_LIST;
1060 val.runsv.runsvstr = p = ctx->bifcxrun->runcxhp;
1061
1062 /* write the length prefix */
1063 oswp2(p, len);
1064 p += 2;
1065
1066 /* write the return code as the first element */
1067 *p++ = DAT_NUMBER;
1068 oswp4s(p, err);
1069 p += 4;
1070
1071 /* write the 'nil' second element if there's an error */
1072 if (err != OS_AFE_SUCCESS)
1073 *p++ = DAT_NIL;
1074
1075 /* commit the list's memory */
1076 ctx->bifcxrun->runcxhp = p;
1077
1078 /* push the list */
1079 runrepush(ctx->bifcxrun, &val);
1080
1081 /* if we were successful, add the string to the list */
1082 if (err == OS_AFE_SUCCESS)
1083 {
1084 runsdef val2;
1085
1086 /* push the string value, converting to our string format */
1087 runpushcstr(ctx->bifcxrun, buf, strlen(buf), 1);
1088
1089 /* add it to the list already on the stack */
1090 runpop(ctx->bifcxrun, &val2);
1091 runpop(ctx->bifcxrun, &val);
1092 runadd(ctx->bifcxrun, &val, &val2, 2);
1093
1094 /* re-push the result */
1095 runrepush(ctx->bifcxrun, &val);
1096 }
1097 }
1098 else
1099 {
1100 /*
1101 * use the traditional return codes - if askfile failed, return
1102 * nil; otherwise, return the filename
1103 */
1104 if (err)
1105 runpnil(ctx->bifcxrun);
1106 else
1107 runpushcstr(ctx->bifcxrun, buf, strlen(buf), 0);
1108 }
1109 }
1110
1111 /* setscore */
1112 void bifssc(bifcxdef *ctx, int argc)
1113 {
1114 int s1, s2;
1115
1116 /* optional new way - string argument */
1117 if (argc == 1 && runtostyp(ctx->bifcxrun) == DAT_SSTRING)
1118 {
1119 char buf[80];
1120 uchar *p;
1121
1122 p = runpopstr(ctx->bifcxrun);
1123 bifcstr(ctx, buf, (size_t)sizeof(buf), p);
1124 tiostrsc(ctx->bifcxtio, buf);
1125 }
1126 else
1127 {
1128 /* old way - two numeric arguments (displays: x/y) */
1129 bifcntargs(ctx, 2, argc);
1130 s1 = runpopnum(ctx->bifcxrun);
1131 s2 = runpopnum(ctx->bifcxrun);
1132 tioscore(ctx->bifcxtio, s1, s2);
1133 }
1134 }
1135
1136 /* substr */
1137 void bifsub(bifcxdef *ctx, int argc)
1138 {
1139 uchar *p;
1140 int ofs;
1141 int asklen;
1142 int outlen;
1143 int len;
1144
1145 bifcntargs(ctx, 3, argc);
1146
1147 /* get the string argument */
1148 bifchkarg(ctx, DAT_SSTRING);
1149 p = runpopstr(ctx->bifcxrun);
1150 len = osrp2(p) - 2;
1151 p += 2;
1152
1153 /* get the offset argument */
1154 bifchkarg(ctx, DAT_NUMBER);
1155 ofs = runpopnum(ctx->bifcxrun);
1156 if (ofs < 1) runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "substr");
1157
1158 /* get the length argument */
1159 bifchkarg(ctx, DAT_NUMBER);
1160 asklen = runpopnum(ctx->bifcxrun);
1161 if (asklen < 0) runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "substr");
1162
1163 --ofs; /* convert offset to a zero bias (user provided 1-bias) */
1164 p += ofs; /* advance string pointer by offset */
1165
1166 if (ofs >= len)
1167 outlen = 0; /* offset is past end of string */
1168 else if (asklen > len - ofs)
1169 outlen = len - ofs; /* just use rest of string */
1170 else
1171 outlen = asklen; /* requested length can be provided */
1172
1173 runpstr(ctx->bifcxrun, (char *)p, outlen, 3);
1174 }
1175
1176 /* cvtstr - convert value to a string */
1177 void bifcvs(bifcxdef *ctx, int argc)
1178 {
1179 char *p;
1180 int len;
1181 char buf[30];
1182
1183 bifcntargs(ctx, 1, argc);
1184 switch(runtostyp(ctx->bifcxrun))
1185 {
1186 case DAT_NIL:
1187 p = "nil";
1188 len = 3;
1189 (void)runpoplog(ctx->bifcxrun);
1190 break;
1191
1192 case DAT_TRUE:
1193 p = "true";
1194 len = 4;
1195 (void)runpoplog(ctx->bifcxrun);
1196 break;
1197
1198 case DAT_NUMBER:
1199 sprintf(buf, "%ld", runpopnum(ctx->bifcxrun));
1200 p = buf;
1201 len = strlen(buf);
1202 break;
1203
1204 case DAT_SSTRING:
1205 /* leave the string value on the stack unchanged */
1206 return;
1207
1208 default:
1209 /* throw the RUNEXITOBJ error */
1210 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "cvtstr");
1211 }
1212
1213 runpstr(ctx->bifcxrun, p, len, 0);
1214 }
1215
1216 /* cvtnum - convert a value to a number */
1217 void bifcvn(bifcxdef *ctx, int argc)
1218 {
1219 runsdef val;
1220 uchar *p;
1221 int len;
1222 int typ;
1223 long acc;
1224 int neg;
1225
1226 bifcntargs(ctx, 1, argc);
1227 p = runpopstr(ctx->bifcxrun);
1228 len = osrp2(p) - 2;
1229 p += 2;
1230
1231 if (len == 3 && !memcmp(p, "nil", (size_t)3))
1232 typ = DAT_NIL;
1233 else if (len == 4 && !memcmp(p, "true", (size_t)4))
1234 typ = DAT_TRUE;
1235 else
1236 {
1237 typ = DAT_NUMBER;
1238 for ( ; len != 0 && t_isspace(*p) ; ++p, --len) ;
1239 if (len != 0 && *p == '-')
1240 {
1241 neg = TRUE;
1242 for (++p, --len ; len != 0 && t_isspace(*p) ; ++p, --len) ;
1243 }
1244 else neg = FALSE;
1245
1246 /* accumulate the number digit by digit */
1247 for (acc = 0 ; len != 0 && isdigit(*p) ; ++p, --len)
1248 acc = (acc << 3) + (acc << 1) + ((*p) - '0');
1249
1250 if (neg) acc = -acc;
1251 val.runsv.runsvnum = acc;
1252 }
1253
1254 runpush(ctx->bifcxrun, typ, &val);
1255 }
1256
1257 /* general string conversion function */
1258 static void bifcvtstr(bifcxdef *ctx, void (*cvtfn)(uchar *, int), int argc)
1259 {
1260 uchar *p;
1261 int len;
1262 runsdef val;
1263 runsdef stkval;
1264
1265 bifcntargs(ctx, 1, argc);
1266 bifchkarg(ctx, DAT_SSTRING);
1267
1268 p = runpopstr(ctx->bifcxrun);
1269 stkval.runstyp = DAT_SSTRING;
1270 stkval.runsv.runsvstr = p;
1271 len = osrp2(p);
1272
1273 /* allocate space in heap for the string and convert */
1274 runhres1(ctx->bifcxrun, len, 1, &stkval);
1275 p = stkval.runsv.runsvstr;
1276 memcpy(ctx->bifcxrun->runcxhp, p, (size_t)len);
1277 (*cvtfn)(ctx->bifcxrun->runcxhp + 2, len - 2);
1278
1279 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
1280 val.runstyp = DAT_SSTRING;
1281 ctx->bifcxrun->runcxhp += len;
1282 runrepush(ctx->bifcxrun, &val);
1283 }
1284
1285 /* routine to convert a counted-length string to uppercase */
1286 static void bifstrupr(uchar *str, int len)
1287 {
1288 for ( ; len ; --len, ++str)
1289 {
1290 if (*str == '\\' && len > 1)
1291 --len, ++str;
1292 else if (islower(*str))
1293 *str = toupper(*str);
1294 }
1295 }
1296
1297 /* upper */
1298 void bifupr(bifcxdef *ctx, int argc)
1299 {
1300 bifcvtstr(ctx, bifstrupr, argc);
1301 }
1302
1303 /* convert a counted-length string to lowercase */
1304 static void bifstrlwr(uchar *str, int len)
1305 {
1306 for ( ; len ; --len, ++str)
1307 {
1308 if (*str == '\\' && len > 1)
1309 --len, ++str;
1310 else if (isupper(*str))
1311 *str = tolower(*str);
1312 }
1313 }
1314
1315 /* lower */
1316 void biflwr(bifcxdef *ctx, int argc)
1317 {
1318 bifcvtstr(ctx, bifstrlwr, argc);
1319 }
1320
1321 /* internal check to determine if object is of a class */
1322 int bifinh(voccxdef *voc, vocidef *v, objnum cls)
1323 {
1324 int i;
1325 objnum *sc;
1326
1327 if (!v) return(FALSE);
1328 for (i = v->vocinsc, sc = v->vocisc ; i ; ++sc, --i)
1329 {
1330 if (*sc == cls
1331 || bifinh(voc, vocinh(voc, *sc), cls))
1332 return(TRUE);
1333 }
1334 return(FALSE);
1335 }
1336
1337 /* isclass(obj, cls) */
1338 void bifisc(bifcxdef *ctx, int argc)
1339 {
1340 objnum obj;
1341 objnum cls;
1342 runsdef val;
1343 voccxdef *voc = ctx->bifcxrun->runcxvoc;
1344
1345 bifcntargs(ctx, 2, argc);
1346
1347 /* if checking for nil, return nil */
1348 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
1349 {
1350 rundisc(ctx->bifcxrun);
1351 rundisc(ctx->bifcxrun);
1352 runpnil(ctx->bifcxrun);
1353 return;
1354 }
1355
1356 /* get the arguments: object, class */
1357 obj = runpopobj(ctx->bifcxrun);
1358 cls = runpopobj(ctx->bifcxrun);
1359
1360 /* return the result from bifinh() */
1361 runpush(ctx->bifcxrun, runclog(bifinh(voc, vocinh(voc, obj), cls)), &val);
1362 }
1363
1364 /* firstsc(obj) - get the first superclass of an object */
1365 void bif1sc(bifcxdef *ctx, int argc)
1366 {
1367 objnum obj;
1368 objnum sc;
1369
1370 bifcntargs(ctx, 1, argc);
1371 obj = runpopobj(ctx->bifcxrun);
1372 sc = objget1sc(ctx->bifcxrun->runcxmem, obj);
1373 runpobj(ctx->bifcxrun, sc);
1374 }
1375
1376 /* firstobj */
1377 void biffob(bifcxdef *ctx, int argc)
1378 {
1379 vocidef ***vpg;
1380 vocidef **v;
1381 objnum obj;
1382 int i;
1383 int j;
1384 objnum cls;
1385 voccxdef *voc = ctx->bifcxrun->runcxvoc;
1386
1387 /* get class to search for, if one is specified */
1388 if (argc == 0)
1389 cls = MCMONINV;
1390 else if (argc == 1)
1391 cls = runpopobj(ctx->bifcxrun);
1392 else
1393 runsig(ctx->bifcxrun, ERR_BIFARGC);
1394
1395 for (vpg = voc->voccxinh, i = 0 ; i < VOCINHMAX ; ++vpg, ++i)
1396 {
1397 if (!*vpg) continue;
1398 for (v = *vpg, obj = (i << 8), j = 0 ; j < 256 ; ++v, ++obj, ++j)
1399 {
1400 if (!*v || ((*v)->vociflg & VOCIFCLASS)
1401 || (cls != MCMONINV && !bifinh(voc, *v, cls)))
1402 continue;
1403
1404 /* this is an object we can use - push it */
1405 runpobj(ctx->bifcxrun, obj);
1406 return;
1407 }
1408 }
1409
1410 /* no objects found at all - return nil */
1411 runpnil(ctx->bifcxrun);
1412 }
1413
1414 /* nextobj */
1415 void bifnob(bifcxdef *ctx, int argc)
1416 {
1417 objnum prv;
1418 vocidef ***vpg;
1419 vocidef **v;
1420 objnum obj;
1421 int i;
1422 int j;
1423 objnum cls;
1424 voccxdef *voc = ctx->bifcxrun->runcxvoc;
1425
1426 /* get last position in search */
1427 prv = runpopobj(ctx->bifcxrun);
1428
1429 /* get class to search for, if one is specified */
1430 if (argc == 1)
1431 cls = MCMONINV;
1432 else if (argc == 2)
1433 cls = runpopobj(ctx->bifcxrun);
1434 else
1435 runsig(ctx->bifcxrun, ERR_BIFARGC);
1436
1437 /* start at previous object plus 1 */
1438 i = (prv >> 8);
1439 vpg = voc->voccxinh + i;
1440 j = (prv & 255);
1441 obj = prv;
1442 v = (*vpg) + j;
1443
1444 for (;;)
1445 {
1446 ++j;
1447 ++obj;
1448 ++v;
1449 if (j == 256)
1450 {
1451 j = 0;
1452 ++i;
1453 ++vpg;
1454 if (!*vpg)
1455 {
1456 obj += 255;
1457 j += 255;
1458 continue;
1459 }
1460 v = (*vpg);
1461 }
1462 if (i >= VOCINHMAX)
1463 {
1464 runpnil(ctx->bifcxrun);
1465 return;
1466 }
1467
1468 if (!*v || ((*v)->vociflg & VOCIFCLASS)
1469 || (cls != MCMONINV && !bifinh(voc, *v, cls)))
1470 continue;
1471
1472 /* this is an object we can use - push it */
1473 runpobj(ctx->bifcxrun, obj);
1474 return;
1475 }
1476 }
1477
1478 /* setversion */
1479 void bifsvn(bifcxdef *ctx, int argc)
1480 {
1481 bifcntargs(ctx, 1, argc);
1482 (void)runpopstr(ctx->bifcxrun);
1483 /* note - setversion doesn't do anything in v2; uses timestamp instead */
1484 }
1485
1486 /* getarg */
1487 void bifarg(bifcxdef *ctx, int argc)
1488 {
1489 int argnum;
1490
1491 bifcntargs(ctx, 1, argc);
1492 bifchkarg(ctx, DAT_NUMBER);
1493
1494 /* get and verify argument number */
1495 argnum = runpopnum(ctx->bifcxrun);
1496 if (argnum < 1) runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "getarg");
1497
1498 runrepush(ctx->bifcxrun, ctx->bifcxrun->runcxbp - argnum - 1);
1499 }
1500
1501 /* datatype */
1502 void biftyp(bifcxdef *ctx, int argc)
1503 {
1504 runsdef val;
1505
1506 bifcntargs(ctx, 1, argc);
1507
1508 /* get whatever it is, and push the type */
1509 runpop(ctx->bifcxrun, &val);
1510 val.runsv.runsvnum = val.runstyp; /* new value is the datatype */
1511 runpush(ctx->bifcxrun, DAT_NUMBER, &val);
1512 }
1513
1514 /* undo */
1515 void bifund(bifcxdef *ctx, int argc)
1516 {
1517 objucxdef *ucx = ctx->bifcxrun->runcxvoc->voccxundo;
1518 mcmcxdef *mcx = ctx->bifcxrun->runcxmem;
1519 errcxdef *ec = ctx->bifcxerr;
1520 int err;
1521 int undone;
1522 runsdef val;
1523
1524 bifcntargs(ctx, 0, argc); /* no arguments */
1525
1526 ERRBEGIN(ec)
1527 if (ucx)
1528 {
1529 objundo(mcx, ucx); /* try to undo to previous savepoint */
1530 undone = TRUE; /* looks like we succeeded */
1531 }
1532 else
1533 undone = FALSE; /* no undo context; can't undo */
1534 ERRCATCH(ec, err)
1535 if (err == ERR_NOUNDO || err == ERR_ICUNDO)
1536 undone = FALSE;
1537 else
1538 errrse(ec); /* don't know how to handle other errors */
1539 ERREND(ec)
1540
1541 /* return a value indicating whether the undo operation succeeded */
1542 runpush(ctx->bifcxrun, runclog(undone), &val);
1543
1544 /* note that the rest of the command line is to be ignored */
1545 ctx->bifcxrun->runcxvoc->voccxflg |= VOCCXFCLEAR;
1546 }
1547
1548 /* flags for defined() function */
1549 #define BIFDEF_DEFINED_ANY 1
1550 #define BIFDEF_DEFINED_DIRECTLY 2
1551 #define BIFDEF_DEFINED_INHERITS 3
1552 #define BIFDEF_DEFINED_GET_CLASS 4
1553
1554 /* defined */
1555 void bifdef(bifcxdef *ctx, int argc)
1556 {
1557 prpnum prpn;
1558 objnum objn;
1559 uint ofs;
1560 runsdef val;
1561 objnum def_objn;
1562 int flag;
1563
1564 /* get object and property arguments */
1565 objn = runpopobj(ctx->bifcxrun);
1566 prpn = runpopprp(ctx->bifcxrun);
1567
1568 /* if there's a flag argument, get it as well */
1569 if (argc == 3)
1570 {
1571 /* get the flag */
1572 flag = (int)runpopnum(ctx->bifcxrun);
1573 }
1574 else
1575 {
1576 /* check the argument count */
1577 bifcntargs(ctx, 2, argc);
1578
1579 /* use the default flag value (DEFINES_OR_INHERITS) */
1580 flag = BIFDEF_DEFINED_ANY;
1581 }
1582
1583 /* get the offset of the property and the defining object */
1584 ofs = objgetap(ctx->bifcxrun->runcxmem, objn, prpn, &def_objn, FALSE);
1585
1586 /* determine the type of information they want */
1587 switch(flag)
1588 {
1589 case BIFDEF_DEFINED_ANY:
1590 /* if the property is defined, return true, else return nil */
1591 runpush(ctx->bifcxrun, runclog(ofs != 0), &val);
1592 break;
1593
1594 case BIFDEF_DEFINED_DIRECTLY:
1595 /* if the property is defined directly by the object, return true */
1596 runpush(ctx->bifcxrun, runclog(ofs != 0 && def_objn == objn), &val);
1597 break;
1598
1599 case BIFDEF_DEFINED_INHERITS:
1600 /* if the property is inherited, return true */
1601 runpush(ctx->bifcxrun, runclog(ofs != 0 && def_objn != objn), &val);
1602 break;
1603
1604 case BIFDEF_DEFINED_GET_CLASS:
1605 /* if it's defined, return the defining object, otherwise nil */
1606 if (ofs == 0)
1607 runpnil(ctx->bifcxrun);
1608 else
1609 runpobj(ctx->bifcxrun, def_objn);
1610 break;
1611
1612 default:
1613 /* invalid flag value */
1614 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "defined");
1615 }
1616 }
1617
1618 /* proptype */
1619 void bifpty(bifcxdef *ctx, int argc)
1620 {
1621 prpnum prpn;
1622 objnum objn;
1623 uint ofs;
1624 runsdef val;
1625 objnum orn;
1626 objdef *objptr;
1627 prpdef *propptr;
1628
1629 bifcntargs(ctx, 2, argc);
1630
1631 /* get offset of obj.prop */
1632 objn = runpopobj(ctx->bifcxrun);
1633 prpn = runpopprp(ctx->bifcxrun);
1634 ofs = objgetap(ctx->bifcxrun->runcxmem, objn, prpn, &orn, FALSE);
1635
1636 if (ofs)
1637 {
1638 /* lock the object, read the prpdef, and unlock it */
1639 objptr = (objdef *)mcmlck(ctx->bifcxrun->runcxmem, (mcmon)orn);
1640 propptr = objofsp(objptr, ofs);
1641 val.runsv.runsvnum = prptype(propptr);
1642 mcmunlck(ctx->bifcxrun->runcxmem, (mcmon)orn);
1643 }
1644 else
1645 {
1646 /* property is not defined by object - indicate that type is nil */
1647 val.runsv.runsvnum = DAT_NIL;
1648 }
1649
1650 /* special case: DAT_DEMAND -> DAT_LIST (for contents properties) */
1651 if (val.runsv.runsvnum == DAT_DEMAND)
1652 val.runsv.runsvnum = DAT_LIST;
1653
1654 /* return the property type as a number */
1655 runpush(ctx->bifcxrun, DAT_NUMBER, &val);
1656 }
1657
1658 /* outhide */
1659 void bifoph(bifcxdef *ctx, int argc)
1660 {
1661 runsdef val;
1662 int hidden, output_occurred;
1663
1664 bifcntargs(ctx, 1, argc);
1665 outstat(&hidden, &output_occurred);
1666 if (runtostyp(ctx->bifcxrun) == DAT_TRUE)
1667 {
1668 /* throw away the flag */
1669 rundisc(ctx->bifcxrun);
1670
1671 /* figure out appropriate return value */
1672 if (!hidden)
1673 val.runsv.runsvnum = 0;
1674 else if (!output_occurred)
1675 val.runsv.runsvnum = 1;
1676 else
1677 val.runsv.runsvnum = 2;
1678 runpush(ctx->bifcxrun, DAT_NUMBER, &val);
1679
1680 /* actually hide the output, resetting count flag */
1681 outhide();
1682 }
1683 else if (runtostyp(ctx->bifcxrun) == DAT_NIL)
1684 {
1685 /* throw away the flag */
1686 rundisc(ctx->bifcxrun);
1687
1688 /* show output, returning status */
1689 runpush(ctx->bifcxrun, runclog(outshow()), &val);
1690 }
1691 else if (runtostyp(ctx->bifcxrun) == DAT_NUMBER)
1692 {
1693 int n = runpopnum(ctx->bifcxrun);
1694
1695 if (n == 0)
1696 {
1697 /* output was not hidden - show output and return status */
1698 runpush(ctx->bifcxrun, runclog(outshow()), &val);
1699 }
1700 else if (n == 1)
1701 {
1702 /*
1703 * Output was hidden, but no output had occurred yet.
1704 * Leave output hidden and return whether any output has
1705 * occurred.
1706 */
1707 runpush(ctx->bifcxrun, runclog(output_occurred), &val);
1708 }
1709 else if (n == 2)
1710 {
1711 /*
1712 * Output was hidden, and output had already occurred. If
1713 * more output has occurred, return true, else return nil.
1714 * In either case, set the output_occurred flag back to
1715 * true, since it was true before the outhide(true).
1716 */
1717 runpush(ctx->bifcxrun, runclog(output_occurred), &val);
1718 outsethidden();
1719 }
1720 else
1721 errsig1(ctx->bifcxerr, ERR_INVVBIF, ERRTSTR, "outhide");
1722 }
1723 else
1724 errsig(ctx->bifcxerr, ERR_REQNUM);
1725 }
1726
1727 /* put a numeric value in a list */
1728 static uchar *bifputnum(uchar *lstp, uint val)
1729 {
1730 *lstp++ = DAT_NUMBER;
1731 oswp4s(lstp, (long)val);
1732 return(lstp + 4);
1733 }
1734
1735 /* gettime */
1736 void biftim(bifcxdef *ctx, int argc)
1737 {
1738 time_t timer;
1739 struct tm *tblock;
1740 uchar ret[80];
1741 uchar *p;
1742 runsdef val;
1743 int typ;
1744
1745 if (argc == 1)
1746 {
1747 /* get the time type */
1748 typ = (int)runpopnum(ctx->bifcxrun);
1749 }
1750 else
1751 {
1752 /* make sure no arguments are specified */
1753 bifcntargs(ctx, 0, argc);
1754
1755 /* use the default time type */
1756 typ = 1;
1757 }
1758
1759 switch(typ)
1760 {
1761 case 1:
1762 /*
1763 * default information format - list format with current system
1764 * time and date
1765 */
1766
1767 /* make sure the time zone is set up properly */
1768 os_tzset();
1769
1770 /* get the local time information */
1771 timer = time(NULL);
1772 tblock = localtime(&timer);
1773
1774 /* adjust values for return format */
1775 tblock->tm_year += 1900;
1776 tblock->tm_mon++;
1777 tblock->tm_wday++;
1778 tblock->tm_yday++;
1779
1780 /* build return list value */
1781 oswp2(ret, 47);
1782 p = ret + 2;
1783 p = bifputnum(p, tblock->tm_year);
1784 p = bifputnum(p, tblock->tm_mon);
1785 p = bifputnum(p, tblock->tm_mday);
1786 p = bifputnum(p, tblock->tm_wday);
1787 p = bifputnum(p, tblock->tm_yday);
1788 p = bifputnum(p, tblock->tm_hour);
1789 p = bifputnum(p, tblock->tm_min);
1790 p = bifputnum(p, tblock->tm_sec);
1791 *p++ = DAT_NUMBER;
1792 oswp4s(p, (long)timer);
1793
1794 val.runstyp = DAT_LIST;
1795 val.runsv.runsvstr = ret;
1796 runpush(ctx->bifcxrun, DAT_LIST, &val);
1797 break;
1798
1799 case 2:
1800 /*
1801 * High-precision system timer value - returns the system time
1802 * in milliseconds, relative to an arbitrary zero point
1803 */
1804 runpnum(ctx->bifcxrun, os_get_sys_clock_ms());
1805 break;
1806
1807 default:
1808 /* other types are invalid */
1809 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "gettime");
1810 break;
1811 }
1812 }
1813
1814 /* getfuse */
1815 void bifgfu(bifcxdef *ctx, int argc)
1816 {
1817 vocddef *daem;
1818 objnum func;
1819 runsdef val;
1820 runcxdef *rcx = ctx->bifcxrun;
1821 int slots;
1822 prpnum prop;
1823 voccxdef *vcx = ctx->bifcxrun->runcxvoc;
1824
1825 bifcntargs(ctx, 2, argc);
1826
1827 if (runtostyp(rcx) == DAT_FNADDR)
1828 {
1829 /* check on a setfuse()-style fuse: get fnaddr, parm */
1830 func = runpopfn(rcx);
1831 runpop(rcx, &val);
1832
1833 for (slots = vcx->voccxfuc, daem = vcx->voccxfus ;
1834 slots ; ++daem, --slots)
1835 {
1836 if (daem->vocdfn == func
1837 && daem->vocdarg.runstyp == val.runstyp
1838 && !memcmp(&val.runsv, &daem->vocdarg.runsv,
1839 (size_t)datsiz(val.runstyp, &val.runsv)))
1840 goto ret_num;
1841 }
1842 }
1843 else
1844 {
1845 /* check on a notify()-style fuse: get object, &message */
1846 func = runpopobj(rcx);
1847 prop = runpopprp(rcx);
1848
1849 for (slots = vcx->voccxalc, daem = vcx->voccxalm ;
1850 slots ; ++daem, --slots)
1851 {
1852 if (daem->vocdfn == func && daem->vocdprp == prop)
1853 goto ret_num;
1854 }
1855 }
1856
1857 /* didn't find anything - return nil */
1858 runpush(rcx, DAT_NIL, &val);
1859 return;
1860
1861 ret_num:
1862 /* return current daem->vocdtim */
1863 runpnum(rcx, (long)daem->vocdtim);
1864 return;
1865 }
1866
1867 /* runfuses */
1868 void bifruf(bifcxdef *ctx, int argc)
1869 {
1870 int ret;
1871 runsdef val;
1872
1873 bifcntargs(ctx, 0, argc);
1874 ret = exefuse(ctx->bifcxrun->runcxvoc, TRUE);
1875 runpush(ctx->bifcxrun, runclog(ret), &val);
1876 }
1877
1878 /* rundaemons */
1879 void bifrud(bifcxdef *ctx, int argc)
1880 {
1881 bifcntargs(ctx, 0, argc);
1882 exedaem(ctx->bifcxrun->runcxvoc);
1883 }
1884
1885 /* intersect */
1886 void bifsct(bifcxdef *bifctx, int argc)
1887 {
1888 runcxdef *ctx = bifctx->bifcxrun;
1889 uchar *l1;
1890 uchar *l2;
1891 uchar *l3;
1892 uint siz1;
1893 uint siz2;
1894 uint siz3;
1895 uchar *p;
1896 uint l;
1897 uint dsz1;
1898 uint dsz2;
1899 runsdef val;
1900 runsdef stk1, stk2;
1901
1902 bifcntargs(bifctx, 2, argc);
1903 l1 = runpoplst(ctx);
1904 siz1 = osrp2(l1);
1905 l2 = runpoplst(ctx);
1906 siz2 = osrp2(l2);
1907
1908 /* make sure the first list is smaller - if not, switch them */
1909 if (siz1 > siz2)
1910 l3 = l1, l1 = l2, l2 = l3, siz3 = siz1, siz1 = siz2, siz2 = siz3;
1911
1912 /* size of result is at most size of smaller list (which is now siz1) */
1913 stk1.runstyp = stk2.runstyp = DAT_LIST;
1914 stk1.runsv.runsvstr = l1;
1915 stk2.runsv.runsvstr = l2;
1916 runhres2(ctx, siz1, 2, &stk1, &stk2);
1917 l1 = stk1.runsv.runsvstr;
1918 l2 = stk2.runsv.runsvstr;
1919 l3 = ctx->runcxhp + 2;
1920
1921 /* go through list1, and copy each element that is found in list2 */
1922 for (l1 += 2, l2 += 2, siz1 -= 2, siz2 -= 2 ; siz1 ; lstadv(&l1, &siz1))
1923 {
1924 dsz1 = datsiz(*l1, l1 + 1) + 1;
1925 for (l = siz2, p = l2 ; l ; lstadv(&p, &l))
1926 {
1927 dsz2 = datsiz(*p, p + 1) + 1;
1928 #ifndef AMIGA
1929 if (dsz1 == dsz2 && !memcmp(l1, p, (size_t)dsz1))
1930 #else /* AMIGA */
1931 if (!memcmp(l1, p, (size_t)dsz1) && (dsz1 == dsz2) )
1932 #endif /* AMIGA */
1933 {
1934 memcpy(l3, p, (size_t)dsz1);
1935 l3 += dsz1;
1936 break;
1937 }
1938 }
1939 }
1940
1941 /* set up return value, take it out of the heap, and push value */
1942 val.runsv.runsvstr = ctx->runcxhp;
1943 val.runstyp = DAT_LIST;
1944 oswp2(ctx->runcxhp, (uint)(l3 - ctx->runcxhp));
1945 ctx->runcxhp = l3;
1946 runrepush(ctx, &val);
1947 }
1948
1949 /*
1950 * Portable keystroke mappings. We map the extended key codes to these
1951 * strings, so that the TADS code can access arrow keys and the like.
1952 */
1953 static char *ext_key_names[] =
1954 {
1955 "[up]", /* CMD_UP - 1 */
1956 "[down]", /* CMD_DOWN - 2 */
1957 "[right]", /* CMD_RIGHT - 3 */
1958 "[left]", /* CMD_LEFT - 4 */
1959 "[end]", /* CMD_END - 5 */
1960 "[home]", /* CMD_HOME - 6 */
1961 "[del-eol]", /* CMD_DEOL - 7 */
1962 "[del-line]", /* CMD_KILL - 8 */
1963 "[del]", /* CMD_DEL - 9 */
1964 "[scroll]", /* CMD_SCR - 10 */
1965 "[page up]", /* CMD_PGUP - 11 */
1966 "[page down]", /* CMD_PGDN - 12 */
1967 "[top]", /* CMD_TOP - 13 */
1968 "[bottom]", /* CMD_BOT - 14 */
1969 "[f1]", /* CMD_F1 - 15 */
1970 "[f2]", /* CMD_F2 - 16 */
1971 "[f3]", /* CMD_F3 - 17 */
1972 "[f4]", /* CMD_F4 - 18 */
1973 "[f5]", /* CMD_F5 - 19 */
1974 "[f6]", /* CMD_F6 - 20 */
1975 "[f7]", /* CMD_F7 - 21 */
1976 "[f8]", /* CMD_F8 - 22 */
1977 "[f9]", /* CMD_F9 - 23 */
1978 "[f10]", /* CMD_F10 - 24 */
1979 "[?]", /* invalid key - CMD_CHOME - 25 */
1980 "[tab]", /* CMD_TAB - 26 */
1981 "[?]", /* invalid key - shift-F2 - 27 */
1982 "[?]", /* not used (obsolete) - 28 */
1983 "[word-left]", /* CMD_WORD_LEFT - 29 */
1984 "[word-right]", /* CMD_WORD_RIGHT - 30 */
1985 "[del-word]", /* CMD_WORDKILL - 31 */
1986 "[eof]", /* CMD_EOF - 32 */
1987 "[break]" /* CMD_BREAK - 33 */
1988 };
1989
1990 /*
1991 * Get the name of a keystroke. Pass in the one or two characters
1992 * returned by os_getc(), and we'll fill in the buffer with the
1993 * inputkey() name of the keystroke. Returns true if the key was valid,
1994 * false if not. 'c' is the first character returned by os_getc() for
1995 * the keystroke; if 'c' is zero, then 'extc' is the character returned
1996 * by the second call to os_getc() to get the CMD_xxx code for the
1997 * keystroke.
1998 *
1999 * The name buffer should be 20 characters long - this will ensure that
2000 * any name will fit.
2001 *
2002 * For ordinary, printable characters, we'll simply return the
2003 * character; the letter 'a', for example, is returned as the string "a".
2004 *
2005 * For extended keys, we'll look up the CMD_xxx code and return the name
2006 * of the command, enclosed in square brackets; see the ext_key_names
2007 * table for the mappings. The left-arrow cursor key, for example,
2008 * returns "[left]".
2009 *
2010 * For control characters, we'll generate a name like "[ctrl-a]", except
2011 * for the following characters:
2012 *
2013 *. ascii 10 returns "\n"
2014 *. ascii 13 returns "\n"
2015 *. ascii 9 returns "\t"
2016 *. ascii 8 returns "[bksp]"
2017 */
2018 static int get_ext_key_name(char *namebuf, int c, int extc)
2019 {
2020 /* if it's a control character, translate it */
2021 if (c >= 1 && c <= 27)
2022 {
2023 switch(c)
2024 {
2025 case 10:
2026 case 13:
2027 /* return '\n' for LF and CR characters */
2028 strcpy(namebuf, "\\n");
2029 return TRUE;
2030
2031 case 9:
2032 /* return '\t' for TAB characters */
2033 strcpy(namebuf, "\\t");
2034 return TRUE;
2035
2036 case 8:
2037 /* return '[bksp]' for backspace characters */
2038 strcpy(namebuf, "[bksp]");
2039 return TRUE;
2040
2041 case 27:
2042 /* return '[esc]' for the escape key */
2043 strcpy(namebuf, "[esc]");
2044 return TRUE;
2045
2046 default:
2047 /* return '[ctrl-X]' for other control characters */
2048 strcpy(namebuf, "[ctrl-X]");
2049 namebuf[6] = (char)(c + 'a' - 1);
2050 return TRUE;
2051 }
2052 }
2053
2054 /* if it's any other non-extended key, return it as-is */
2055 if (c != 0)
2056 {
2057 namebuf[0] = c;
2058 namebuf[1] = '\0';
2059 return TRUE;
2060 }
2061
2062 /* if it's in the key name array, use the array entry */
2063 if (extc >= 1
2064 && extc <= (int)(sizeof(ext_key_names)/sizeof(ext_key_names[0])))
2065 {
2066 /* use the array name */
2067 strcpy(namebuf, ext_key_names[extc - 1]);
2068 return TRUE;
2069 }
2070
2071 /* if it's in the ALT key range, generate an ALT key name */
2072 if (extc >= CMD_ALT && extc <= CMD_ALT + 25)
2073 {
2074 /* generate an ALT key name */
2075 strcpy(namebuf, "[alt-X]");
2076 namebuf[5] = (char)(extc - CMD_ALT + 'a');
2077 return TRUE;
2078 }
2079
2080 /* it's not a valid key - use '[?]' as the name */
2081 strcpy(namebuf, "[?]");
2082 return FALSE;
2083 }
2084
2085
2086 /* inputkey */
2087 void bifink(bifcxdef *ctx, int argc)
2088 {
2089 int c;
2090 int extc;
2091 char str[20];
2092 size_t len;
2093
2094 bifcntargs(ctx, 0, argc);
2095 tioflushn(ctx->bifcxtio, 0);
2096
2097 /* get a key */
2098 c = os_getc_raw();
2099
2100 /* if it's extended, get the second part of the extended sequence */
2101 extc = (c == 0 ? os_getc_raw() : 0);
2102
2103 /* map the extended key name */
2104 get_ext_key_name(str, c, extc);
2105
2106 /* get the length of the name */
2107 len = strlen(str);
2108
2109 /* reset the [more] counter */
2110 outreset();
2111
2112 /* return the string, translating escapes */
2113 runpstr(ctx->bifcxrun, str, len, 0);
2114 }
2115
2116 /* get direct/indirect object word list */
2117 void bifwrd(bifcxdef *ctx, int argc)
2118 {
2119 int ob;
2120 vocoldef *v;
2121 uchar buf[128];
2122 uchar *dst;
2123 uchar *src;
2124 uint len;
2125 runsdef val;
2126
2127 bifcntargs(ctx, 1, argc);
2128
2129 /* figure out what word list to get */
2130 ob = runpopnum(ctx->bifcxrun);
2131 switch(ob)
2132 {
2133 case 1:
2134 v = ctx->bifcxrun->runcxvoc->voccxdobj;
2135 break;
2136
2137 case 2:
2138 v = ctx->bifcxrun->runcxvoc->voccxiobj;
2139 break;
2140
2141 default:
2142 runpnil(ctx->bifcxrun);
2143 return;
2144 }
2145
2146 /* now build a list of strings from the words, if there are any */
2147 if (v != 0 && voclistlen(v) != 0 && v->vocolfst != 0 && v->vocollst != 0)
2148 {
2149 for (dst = buf + 2, src = (uchar *)v->vocolfst ;
2150 src <= (uchar *)v->vocollst ; src += len + 1)
2151 {
2152 *dst++ = DAT_SSTRING;
2153 len = strlen((char *)src);
2154 oswp2(dst, len + 2);
2155 strcpy((char *)dst + 2, (char *)src);
2156 dst += len + 2;
2157 }
2158 }
2159 else
2160 dst = buf + 2;
2161
2162 /* finish setting up the list length and return it */
2163 len = dst - buf;
2164 oswp2(buf, len);
2165 val.runsv.runsvstr = buf;
2166 val.runstyp = DAT_LIST;
2167 runpush(ctx->bifcxrun, DAT_LIST, &val);
2168 }
2169
2170 /* add a vocabulary word to an object */
2171 void bifadw(bifcxdef *ctx, int argc)
2172 {
2173 uchar *wrd;
2174 objnum objn;
2175 prpnum prpn;
2176 vocidef *voci;
2177 int classflg;
2178 voccxdef *voc = ctx->bifcxrun->runcxvoc;
2179
2180 bifcntargs(ctx, 3, argc);
2181
2182 /* get the arguments */
2183 objn = runpopobj(ctx->bifcxrun);
2184 prpn = runpopprp(ctx->bifcxrun);
2185 wrd = runpopstr(ctx->bifcxrun);
2186
2187 /* make sure the property is a valid part of speech property */
2188 if (!prpisvoc(prpn))
2189 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "addword");
2190
2191 /* get the vocidef for the object, and see if it's a class object */
2192 voci = vocinh(voc, objn);
2193
2194 classflg = VOCFNEW;
2195 if (voci->vociflg & VOCIFCLASS) classflg |= VOCFCLASS;
2196
2197 /* add the word */
2198 vocadd(voc, prpn, objn, classflg, (char *)wrd);
2199
2200 /* generate undo for the operation */
2201 vocdusave_addwrd(voc, objn, prpn, classflg, (char *)wrd);
2202 }
2203
2204 /* delete a vocabulary word from an object */
2205 void bifdlw(bifcxdef *ctx, int argc)
2206 {
2207 uchar *wrd;
2208 objnum objn;
2209 prpnum prpn;
2210 voccxdef *voc = ctx->bifcxrun->runcxvoc;
2211
2212 bifcntargs(ctx, 3, argc);
2213
2214 /* get the arguments */
2215 objn = runpopobj(ctx->bifcxrun);
2216 prpn = runpopprp(ctx->bifcxrun);
2217 wrd = runpopstr(ctx->bifcxrun);
2218
2219 /* make sure the property is a valid part of speech property */
2220 if (!prpisvoc(prpn))
2221 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "delword");
2222
2223 /* delete the word */
2224 vocdel1(voc, objn, (char *)wrd, prpn, FALSE, FALSE, TRUE);
2225 }
2226
2227 /* callback context for word list builder */
2228 struct bifgtw_cb_ctx
2229 {
2230 uchar *p;
2231 int typ;
2232 };
2233
2234 /* callback for word list builder */
2235 static void bifgtw_cb(void *ctx0, vocdef *voc, vocwdef *vocw)
2236 {
2237 struct bifgtw_cb_ctx *ctx = (struct bifgtw_cb_ctx *)ctx0;
2238
2239 /* ignore deleted objects */
2240 if (vocw->vocwflg & VOCFDEL)
2241 return;
2242
2243 /* ignore objects of the inappropriate type */
2244 if (vocw->vocwtyp != ctx->typ)
2245 return;
2246
2247 /* the datatype is string */
2248 *ctx->p = DAT_SSTRING;
2249
2250 /* copy the first word */
2251 memcpy(ctx->p + 3, voc->voctxt, (size_t)voc->voclen);
2252
2253 /* if there are two words, add a space and the second word */
2254 if (voc->vocln2)
2255 {
2256 *(ctx->p + 3 + voc->voclen) = ' ';
2257 memcpy(ctx->p + 4 + voc->voclen, voc->voctxt + voc->voclen,
2258 (size_t)voc->vocln2);
2259 oswp2(ctx->p + 1, voc->voclen + voc->vocln2 + 3);
2260 ctx->p += voc->voclen + voc->vocln2 + 4;
2261 }
2262 else
2263 {
2264 oswp2(ctx->p + 1, voc->voclen+2);
2265 ctx->p += voc->voclen + 3;
2266 }
2267 }
2268
2269 /* get the list of words for an object for a particular part of speech */
2270 void bifgtw(bifcxdef *ctx, int argc)
2271 {
2272 objnum objn;
2273 prpnum prpn;
2274 voccxdef *voc = ctx->bifcxrun->runcxvoc;
2275 int cnt;
2276 int siz;
2277 runsdef val;
2278 struct bifgtw_cb_ctx fnctx;
2279
2280 bifcntargs(ctx, 2, argc);
2281
2282 /* get the arguments */
2283 objn = runpopobj(ctx->bifcxrun);
2284 prpn = runpopprp(ctx->bifcxrun);
2285
2286 /* make sure the property is a valid part of speech property */
2287 if (!prpisvoc(prpn))
2288 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "delword");
2289
2290 /* get the size of the list we'll need to build */
2291 voc_count(voc, objn, prpn, &cnt, &siz);
2292
2293 /*
2294 * calculate how much space it will take to make a list out of all
2295 * these words: 2 bytes for the list length header; plus, for each
2296 * entry, 1 byte for the type header, 2 bytes for the string size
2297 * header, and possibly one extra byte for the two-word separator --
2298 * a total of 4 bytes extra per word.
2299 */
2300 siz += 2 + 4*cnt;
2301
2302 /* reserve the space */
2303 runhres(ctx->bifcxrun, siz, 0);
2304
2305 /* set up our callback context, and build the list */
2306 fnctx.p = ctx->bifcxrun->runcxhp + 2;
2307 fnctx.typ = prpn;
2308 voc_iterate(voc, objn, bifgtw_cb, &fnctx);
2309
2310 /* set up the return value */
2311 val.runstyp = DAT_LIST;
2312 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
2313
2314 /* write the list length, and advance past the space we used */
2315 oswp2(ctx->bifcxrun->runcxhp, fnctx.p - ctx->bifcxrun->runcxhp);
2316 ctx->bifcxrun->runcxhp = fnctx.p;
2317
2318 /* return the list */
2319 runrepush(ctx->bifcxrun, &val);
2320 }
2321
2322 /* verbinfo service routine - add an object to the output list */
2323 static uchar *bifvin_putprpn(uchar *p, prpnum prpn)
2324 {
2325 *p++ = DAT_PROPNUM;
2326 oswp2(p, prpn);
2327 return p + 2;
2328 }
2329
2330 /* verbinfo */
2331 void bifvin(bifcxdef *ctx, int argc)
2332 {
2333 objnum verb;
2334 objnum prep;
2335 uchar tplbuf[VOCTPL2SIZ];
2336 int newstyle;
2337
2338 /* get the verb */
2339 verb = runpopobj(ctx->bifcxrun);
2340
2341 /* check for the presence of a preposition */
2342 if (argc == 1)
2343 {
2344 /* no preposition */
2345 prep = MCMONINV;
2346 }
2347 else
2348 {
2349 /* the second argument is the preposition */
2350 bifcntargs(ctx, 2, argc);
2351 prep = runpopobj(ctx->bifcxrun);
2352 }
2353
2354 /* look up the template */
2355 if (voctplfnd(ctx->bifcxrun->runcxvoc, verb, prep, tplbuf, &newstyle))
2356 {
2357 prpnum prp_do, prp_verdo, prp_io, prp_verio;
2358 int flg_dis_do;
2359 ushort siz;
2360 uchar *p;
2361 runsdef val;
2362
2363 /* get the information from the template */
2364 prp_do = voctpldo(tplbuf);
2365 prp_verdo = voctplvd(tplbuf);
2366 prp_io = voctplio(tplbuf);
2367 prp_verio = voctplvi(tplbuf);
2368 flg_dis_do = (voctplflg(tplbuf) & VOCTPLFLG_DOBJ_FIRST) != 0;
2369
2370 /*
2371 * figure space for the return value: if there's a prep, three
2372 * property pointers plus a boolean, otherwise just two property
2373 * pointers
2374 */
2375 siz = 2 + 2*(2+1);
2376 if (prep != MCMONINV)
2377 siz += (2+1) + 1;
2378
2379 /* reserve the space */
2380 runhres(ctx->bifcxrun, siz, 0);
2381
2382 /* build the output list */
2383 p = ctx->bifcxrun->runcxhp;
2384 oswp2(p, siz);
2385 p += 2;
2386
2387 p = bifvin_putprpn(p, prp_verdo);
2388 if (prep == MCMONINV)
2389 {
2390 p = bifvin_putprpn(p, prp_do);
2391 }
2392 else
2393 {
2394 p = bifvin_putprpn(p, prp_verio);
2395 p = bifvin_putprpn(p, prp_io);
2396 *p++ = runclog(flg_dis_do);
2397 }
2398
2399 /* build the return value */
2400 val.runstyp = DAT_LIST;
2401 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
2402
2403 /* consume the space */
2404 ctx->bifcxrun->runcxhp += siz;
2405
2406 /* return the list */
2407 runrepush(ctx->bifcxrun, &val);
2408 }
2409 else
2410 {
2411 /* no template for this verb - return nil */
2412 runpnil(ctx->bifcxrun);
2413 }
2414 }
2415
2416
2417 /* clearscreen */
2418 void bifcls(bifcxdef *ctx, int argc)
2419 {
2420 /* this takes no arguments */
2421 bifcntargs(ctx, 0, argc);
2422
2423 /* flush any pending output */
2424 tioflushn(ctx->bifcxtio, 0);
2425
2426 /* clear the screen */
2427 oscls();
2428 }
2429
2430 /*
2431 * File operations
2432 */
2433
2434 /*
2435 * fopen(file, mode).
2436 *
2437 * Operations are allowed only if they conform to the current I/O safety
2438 * level. The safety level can be set by the user on the command line
2439 * when running the game, and some implementations may allow the setting
2440 * to be saved as a preference. The possible levels are:
2441 *
2442 *. 0 - minimum safety - read and write in any directory
2443 *. 1 - read in any directory, write in current directory
2444 *. 2 - read/write access in current directory only
2445 *. 3 - read-only access in current directory only
2446 *. 4 - maximum safety - no file I/O allowed
2447 *
2448 * When operations are allowed only in the current directory, the
2449 * operations will fail if the filename contains any sort of path
2450 * specifier (for example, on Unix, any file that contains a '/' is
2451 * considered to have a path specifier, and will always fail if
2452 * operations are only allowed in the current directory).
2453 */
2454 void biffopen(bifcxdef *ctx, int argc)
2455 {
2456 char fname[OSFNMAX];
2457 uchar *p;
2458 uchar *mode;
2459 int modelen;
2460 int fnum;
2461 osfildef *fp;
2462 int bin_mode = TRUE; /* flag: mode is binary (rather than text) */
2463 int rw_mode = FALSE; /* flag: both read and write are allowed */
2464 char main_mode; /* 'r' for read, 'w' for write */
2465 int in_same_dir; /* flag: file is in current directory */
2466 appctxdef *appctx;
2467
2468 bifcntargs(ctx, 2, argc);
2469
2470 /* get the filename */
2471 p = runpopstr(ctx->bifcxrun);
2472 bifcstr(ctx, fname, (size_t)sizeof(fname), p);
2473
2474 /*
2475 * If it's a relative path, combine it with the game file path to form
2476 * the absolute path. This ensures that relative paths are always
2477 * relative to the original working directory if the OS-level working
2478 * directory has changed.
2479 */
2480 if (!os_is_file_absolute(fname))
2481 {
2482 /* combine the game file path with the relative filename */
2483 char newname[OSFNMAX];
2484 os_build_full_path(newname, sizeof(newname),
2485 ctx->bifcxrun->runcxgamepath, fname);
2486
2487 /* replace the original filename with the full path */
2488 strcpy(fname, newname);
2489 }
2490
2491 /* get the mode string */
2492 mode = runpopstr(ctx->bifcxrun);
2493 modelen = osrp2(mode) - 2;
2494 mode += 2;
2495 if (modelen < 1)
2496 goto bad_mode;
2497
2498 /* allocate a filenum for the file */
2499 for (fnum = 0 ; fnum < BIFFILMAX ; ++fnum)
2500 {
2501 if (ctx->bifcxfile[fnum].fp == 0)
2502 break;
2503 }
2504 if (fnum == BIFFILMAX)
2505 {
2506 /* return nil to indicate failure */
2507 runpnil(ctx->bifcxrun);
2508 return;
2509 }
2510
2511 /* parse the main mode */
2512 switch(*mode)
2513 {
2514 case 'w':
2515 case 'W':
2516 main_mode = 'w';
2517 break;
2518
2519 case 'r':
2520 case 'R':
2521 main_mode = 'r';
2522 break;
2523
2524 default:
2525 goto bad_mode;
2526 }
2527
2528 /* skip the main mode, and check for a '+' flag */
2529 ++mode;
2530 --modelen;
2531 if (modelen > 0 && *mode == '+')
2532 {
2533 /* note the read/write mode */
2534 rw_mode = TRUE;
2535
2536 /* skip the speciifer */
2537 ++mode;
2538 --modelen;
2539 }
2540
2541 /* check for a binary/text specifier */
2542 if (modelen > 0)
2543 {
2544 switch(*mode)
2545 {
2546 case 'b':
2547 case 'B':
2548 bin_mode = TRUE;
2549 break;
2550
2551 case 't':
2552 case 'T':
2553 bin_mode = FALSE;
2554 break;
2555
2556 default:
2557 goto bad_mode;
2558 }
2559
2560 /* skip the binary/text specifier */
2561 ++mode;
2562 --modelen;
2563 }
2564
2565 /* it's an error if there's anything left unparsed */
2566 if (modelen > 0)
2567 goto bad_mode;
2568
2569 /*
2570 * If we have a host application context, and it provides a file
2571 * safety level callback function, ask the host system for its
2572 * current file safety level, which overrides our current setting.
2573 */
2574 appctx = ctx->bifcxappctx;
2575 if (appctx != 0 && appctx->get_io_safety_level != 0)
2576 {
2577 /*
2578 * ask the host system for the current level, and override any
2579 * setting we previously had
2580 */
2581 (*appctx->get_io_safety_level)(
2582 appctx->io_safety_level_ctx,
2583 &ctx->bifcxsafetyr, &ctx->bifcxsafetyw);
2584 }
2585
2586 /*
2587 * Check to see if the file is in the current working directory - if
2588 * not, we may have to disallow the operation based on safety level
2589 * settings.
2590 */
2591 in_same_dir = os_is_file_in_dir(
2592 fname, ctx->bifcxrun->runcxgamepath, TRUE, FALSE);
2593
2594 /* check file safety settings */
2595 switch(main_mode)
2596 {
2597 case 'w':
2598 /*
2599 * writing - we must be at a safety level no higher than 2
2600 * (read/write current directory) to write at all, and we must be
2601 * level 0 to write a file that's not in the current directory
2602 */
2603 if (ctx->bifcxsafetyw > 2
2604 || (!in_same_dir && ctx->bifcxsafetyw > 0))
2605 {
2606 /* this operation is not allowed - return failure */
2607 runpnil(ctx->bifcxrun);
2608 return;
2609 }
2610 break;
2611
2612 case 'r':
2613 /*
2614 * reading - we must be at a safety level no higher than 3 (read
2615 * current directory) to read at all, and we must be at safety
2616 * level 1 (read any directory) or lower to read a file that's not
2617 * in the current directory
2618 */
2619 if (ctx->bifcxsafetyr > 3
2620 || (!in_same_dir && ctx->bifcxsafetyr > 1))
2621 {
2622 /* this operation is not allowed - return failure */
2623 runpnil(ctx->bifcxrun);
2624 return;
2625 }
2626 break;
2627
2628 default:
2629 /*
2630 * fail the operation, as a code maintenance measure to make
2631 * sure that we add appropriate cases to this switch (even if
2632 * merely to allow the operation unconditionally) in the event
2633 * that more modes are added in the future
2634 */
2635 goto bad_mode;
2636 }
2637
2638 /* try opening the file */
2639 switch(main_mode)
2640 {
2641 case 'w':
2642 /* check for binary vs text mode */
2643 if (bin_mode)
2644 {
2645 /*
2646 * binary mode -- allow read/write or just writing, but in
2647 * either case truncate the file if it already exists, and
2648 * create a new file if it doesn't exist
2649 */
2650 if (rw_mode)
2651 fp = osfoprwtb(fname, OSFTDATA);
2652 else
2653 fp = osfopwb(fname, OSFTDATA);
2654 }
2655 else
2656 {
2657 /* text mode - don't allow read/write on a text file */
2658 if (rw_mode)
2659 goto bad_mode;
2660
2661 /* open the file */
2662 fp = osfopwt(fname, OSFTTEXT);
2663 }
2664 break;
2665
2666 case 'r':
2667 /* check for binary vs text mode */
2668 if (bin_mode)
2669 {
2670 /*
2671 * Binary mode -- allow read/write or just reading; leave
2672 * any existing file intact.
2673 */
2674 if (rw_mode)
2675 {
2676 /* open for reading and writing, keeping existing data */
2677 fp = osfoprwb(fname, OSFTDATA);
2678 }
2679 else
2680 {
2681 /* open for read-only */
2682 fp = osfoprb(fname, OSFTDATA);
2683 }
2684 }
2685 else
2686 {
2687 /* text mode -- only allow reading */
2688 if (rw_mode)
2689 goto bad_mode;
2690
2691 /* open the file */
2692 fp = osfoprt(fname, OSFTTEXT);
2693 }
2694 break;
2695
2696 default:
2697 goto bad_mode;
2698 }
2699
2700 /* if we couldn't open it, return nil */
2701 if (fp == 0)
2702 {
2703 runpnil(ctx->bifcxrun);
2704 return;
2705 }
2706
2707 /* store the flags */
2708 ctx->bifcxfile[fnum].flags = 0;
2709 if (bin_mode)
2710 ctx->bifcxfile[fnum].flags |= BIFFIL_F_BINARY;
2711
2712 /* remember the file handle */
2713 ctx->bifcxfile[fnum].fp = fp;
2714
2715 /* return the file number (i.e., the slot number) */
2716 runpnum(ctx->bifcxrun, (long)fnum);
2717 return;
2718
2719
2720 /* come here on a mode error */
2721 bad_mode:
2722 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "fopen");
2723 }
2724
2725 /* service routine for file routines - get and validate a file number */
2726 static osfildef *bif_get_file(bifcxdef *ctx, int *fnump, int *bin_modep)
2727 {
2728 long fnum;
2729
2730 /* get the file number and make sure it's valid */
2731 fnum = runpopnum(ctx->bifcxrun);
2732 if (fnum < 0 || fnum >= BIFFILMAX || ctx->bifcxfile[fnum].fp == 0)
2733 runsig(ctx->bifcxrun, ERR_BADFILE);
2734
2735 /* put the validated file number, if the caller wants it */
2736 if (fnump != 0)
2737 *fnump = (int)fnum;
2738
2739 /* set the binary-mode flag, if the caller wants it */
2740 if (bin_modep != 0)
2741 *bin_modep = ((ctx->bifcxfile[fnum].flags & BIFFIL_F_BINARY) != 0);
2742
2743 /* return the file array pointer */
2744 return ctx->bifcxfile[fnum].fp;
2745 }
2746
2747 void biffclose(bifcxdef *ctx, int argc)
2748 {
2749 int fnum;
2750 osfildef *fp;
2751
2752 /* get the file number */
2753 bifcntargs(ctx, 1, argc);
2754 fp = bif_get_file(ctx, &fnum, 0);
2755
2756 /* close the file and release the slot */
2757 osfcls(fp);
2758 ctx->bifcxfile[fnum].fp = 0;
2759 }
2760
2761 void bifftell(bifcxdef *ctx, int argc)
2762 {
2763 osfildef *fp;
2764
2765 /* get the file number */
2766 bifcntargs(ctx, 1, argc);
2767 fp = bif_get_file(ctx, (int *)0, 0);
2768
2769 /* return the seek position */
2770 runpnum(ctx->bifcxrun, osfpos(fp));
2771 }
2772
2773 void biffseek(bifcxdef *ctx, int argc)
2774 {
2775 osfildef *fp;
2776 long pos;
2777
2778 /* get the file pointer */
2779 bifcntargs(ctx, 2, argc);
2780 fp = bif_get_file(ctx, (int *)0, 0);
2781
2782 /* get the seek position, and seek there */
2783 pos = runpopnum(ctx->bifcxrun);
2784 osfseek(fp, pos, OSFSK_SET);
2785 }
2786
2787 void biffseekeof(bifcxdef *ctx, int argc)
2788 {
2789 osfildef *fp;
2790
2791 /* get the file pointer */
2792 bifcntargs(ctx, 1, argc);
2793 fp = bif_get_file(ctx, (int *)0, 0);
2794
2795 /* seek to the end */
2796 osfseek(fp, 0L, OSFSK_END);
2797 }
2798
2799 void biffwrite(bifcxdef *ctx, int argc)
2800 {
2801 osfildef *fp;
2802 char typ;
2803 char buf[32];
2804 runsdef val;
2805 int bin_mode;
2806
2807 /* get the file */
2808 bifcntargs(ctx, 2, argc);
2809 fp = bif_get_file(ctx, (int *)0, &bin_mode);
2810
2811 /* get the value to write */
2812 runpop(ctx->bifcxrun, &val);
2813 typ = val.runstyp;
2814
2815 if (bin_mode)
2816 {
2817 /* put a byte indicating the type */
2818 if (osfwb(fp, &typ, 1))
2819 goto ret_error;
2820
2821 /* see what type of data we want to put */
2822 switch(typ)
2823 {
2824 case DAT_NUMBER:
2825 oswp4s(buf, val.runsv.runsvnum);
2826 if (osfwb(fp, buf, 4))
2827 goto ret_error;
2828 break;
2829
2830 case DAT_SSTRING:
2831 /* write the string, including the length prefix */
2832 if (osfwb(fp, val.runsv.runsvstr, osrp2(val.runsv.runsvstr)))
2833 goto ret_error;
2834 break;
2835
2836 case DAT_TRUE:
2837 /* all we need for this is the type prefix */
2838 break;
2839
2840 default:
2841 /* other types are not acceptable */
2842 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "fwrite");
2843 }
2844 }
2845 else
2846 {
2847 uint rem;
2848 uchar *p;
2849
2850 switch(typ)
2851 {
2852 case DAT_SSTRING:
2853 /*
2854 * Copy and translate the string to our buffer, in pieces if
2855 * the size of the string exceeds that of our buffer. If we
2856 * encounter any escape codes, translate them.
2857 */
2858 rem = osrp2(val.runsv.runsvstr) - 2;
2859 p = val.runsv.runsvstr + 2;
2860 while (rem > 0)
2861 {
2862 uchar *dst;
2863 uchar buf[256];
2864
2865 /* fill up the buffer */
2866 for (dst = buf ;
2867 rem != 0 && (size_t)(dst - buf) < sizeof(buf) - 1 ;
2868 ++p, --rem)
2869 {
2870 /* if we have an escape character, translate it */
2871 if (*p == '\\' && rem > 1)
2872 {
2873 /* skip the opening slash */
2874 ++p;
2875 --rem;
2876
2877 /* translate it */
2878 switch(*p)
2879 {
2880 case 'n':
2881 *dst++ = '\n';
2882 break;
2883
2884 case 't':
2885 *dst++ = '\t';
2886 break;
2887
2888 default:
2889 *dst++ = *p;
2890 break;
2891 }
2892 }
2893 else
2894 {
2895 /* copy this character directly */
2896 *dst++ = *p;
2897 }
2898 }
2899
2900 /* null-terminate the buffer */
2901 *dst = '\0';
2902
2903 /* write it out */
2904 if (osfputs((char *)buf, fp) == EOF)
2905 goto ret_error;
2906 }
2907
2908 /* done */
2909 break;
2910
2911 default:
2912 /* other types are not allowed */
2913 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "fwrite");
2914 }
2915 }
2916
2917 /* success */
2918 runpnil(ctx->bifcxrun);
2919 return;
2920
2921 ret_error:
2922 val.runstyp = DAT_TRUE;
2923 runpush(ctx->bifcxrun, DAT_TRUE, &val);
2924 }
2925
2926 void biffread(bifcxdef *ctx, int argc)
2927 {
2928 osfildef *fp;
2929 char typ;
2930 char buf[32];
2931 runsdef val;
2932 ushort len;
2933 int bin_mode;
2934
2935 /* get the file pointer */
2936 bifcntargs(ctx, 1, argc);
2937 fp = bif_get_file(ctx, (int *)0, &bin_mode);
2938
2939 if (bin_mode)
2940 {
2941 /* binary file - read the type byte */
2942 if (osfrb(fp, &typ, 1))
2943 goto ret_error;
2944
2945 /* read the data according to the type */
2946 switch(typ)
2947 {
2948 case DAT_NUMBER:
2949 if (osfrb(fp, buf, 4))
2950 goto ret_error;
2951 runpnum(ctx->bifcxrun, osrp4s(buf));
2952 break;
2953
2954 case DAT_SSTRING:
2955 /* get the size */
2956 if (osfrb(fp, buf, 2))
2957 goto ret_error;
2958 len = osrp2(buf);
2959
2960 /* reserve space */
2961 runhres(ctx->bifcxrun, len, 0);
2962
2963 /* read the string into the reserved space */
2964 if (osfrb(fp, ctx->bifcxrun->runcxhp + 2, len - 2))
2965 goto ret_error;
2966
2967 /* set up the string */
2968 oswp2(ctx->bifcxrun->runcxhp, len);
2969 val.runstyp = DAT_SSTRING;
2970 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
2971
2972 /* consume the space */
2973 ctx->bifcxrun->runcxhp += len;
2974
2975 /* push the value */
2976 runrepush(ctx->bifcxrun, &val);
2977 break;
2978
2979 case DAT_TRUE:
2980 val.runstyp = DAT_TRUE;
2981 runpush(ctx->bifcxrun, DAT_TRUE, &val);
2982 break;
2983
2984 default:
2985 goto ret_error;
2986 }
2987 }
2988 else
2989 {
2990 uchar buf[257];
2991 uchar *dst;
2992 uchar *src;
2993 uint len;
2994 uint res_total;
2995 int found_nl;
2996
2997 /*
2998 * reserve some space in the heap - we'll initially reserve
2999 * space for twice our buffer, in case every single character
3000 * needs to be expanded into an escape sequence
3001 */
3002 res_total = sizeof(buf) * 2;
3003 runhres(ctx->bifcxrun, res_total, 0);
3004
3005 /* set up our output value */
3006 val.runstyp = DAT_SSTRING;
3007 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
3008 dst = ctx->bifcxrun->runcxhp + 2;
3009
3010 /* keep going until we find a newline or run out of data */
3011 for (found_nl = FALSE ; !found_nl ; )
3012 {
3013 /* text-mode - read the result into our buffer */
3014 if (!osfgets((char *)buf, sizeof(buf) - 1, fp))
3015 {
3016 /*
3017 * if we found a newline, return what we have;
3018 * otherwise, return an error
3019 */
3020 if (found_nl)
3021 break;
3022 else
3023 goto ret_error;
3024 }
3025
3026 /*
3027 * make sure it's null-terminated, in case the buffer was
3028 * full
3029 */
3030 buf[256] = '\0';
3031
3032 /* translate into the heap area we've reserved */
3033 for (src = buf ; *src != '\0' ; ++src, ++dst)
3034 {
3035 /* determine if we need translations */
3036 switch(*src)
3037 {
3038 case '\n':
3039 case '\r':
3040 /* translate to a newline sequence */
3041 *dst++ = '\\';
3042 *dst = 'n';
3043
3044 /* note that we've found our newline */
3045 found_nl = TRUE;
3046 break;
3047
3048 case '\t':
3049 /* translate to a tab sequence */
3050 *dst++ = '\\';
3051 *dst = 't';
3052 break;
3053
3054 case '\\':
3055 /* expand to a double-backslash sequence */
3056 *dst++ = '\\';
3057 *dst = '\\';
3058 break;
3059
3060 default:
3061 /* leave other characters intact */
3062 *dst = *src;
3063 break;
3064 }
3065 }
3066
3067 /*
3068 * If we didn't find the newline, we'll need more space.
3069 * This is a bit tricky, because the space we've already set
3070 * up may move if we compact the heap while asking for more
3071 * space. So, remember our current length, reserve another
3072 * buffer-full of space, and set everything up at the new
3073 * output location if necessary.
3074 */
3075 if (!found_nl)
3076 {
3077 /* reserve another buffer-full (double for expansion) */
3078 res_total += sizeof(buf) * 2;
3079
3080 /* note our current offset */
3081 len = dst - val.runsv.runsvstr;
3082 oswp2(val.runsv.runsvstr, len);
3083
3084 /* ask for the space */
3085 runhres(ctx->bifcxrun, res_total, 0);
3086
3087 /*
3088 * Since we were at the top of the heap before, we
3089 * should still be at the top of the heap. If not,
3090 * we'll have to copy from our old location to the new
3091 * top of the heap.
3092 */
3093 if (val.runsv.runsvstr != ctx->bifcxrun->runcxhp)
3094 {
3095 /* copy our existing text to our new location */
3096 memmove(ctx->bifcxrun->runcxhp, val.runsv.runsvstr, len);
3097
3098 /* fix up our pointer */
3099 val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
3100 }
3101
3102 /* re-establish our output pointer at our new location */
3103 dst = val.runsv.runsvstr + len;
3104 }
3105 }
3106
3107 /* finish setting up the string */
3108 len = dst - val.runsv.runsvstr;
3109 oswp2(val.runsv.runsvstr, len);
3110
3111 /* consume the space */
3112 ctx->bifcxrun->runcxhp += len;
3113
3114 /* push the value */
3115 runrepush(ctx->bifcxrun, &val);
3116 }
3117
3118 /* success - we've already pushed the return value */
3119 return;
3120
3121 ret_error:
3122 runpnil(ctx->bifcxrun);
3123 }
3124
3125 void bifcapture(bifcxdef *ctx, int argc)
3126 {
3127 mcmcxdef *mcx = ctx->bifcxrun->runcxmem;
3128 mcmon obj;
3129 uint siz;
3130 uint ofs;
3131 uchar *p;
3132
3133 /* get the capture on/off flag */
3134 bifcntargs(ctx, 1, argc);
3135 switch(runtostyp(ctx->bifcxrun))
3136 {
3137 case DAT_TRUE:
3138 /* turn on capturing */
3139 tiocapture(ctx->bifcxtio, mcx, TRUE);
3140
3141 /*
3142 * The return value is a status code used to restore the
3143 * original status on the bracketing call to turn off output.
3144 * The only status necessary is the current output size.
3145 */
3146 siz = tiocapturesize(ctx->bifcxtio);
3147 runpnum(ctx->bifcxrun, (long)siz);
3148 break;
3149
3150 case DAT_NUMBER:
3151 /* get the original offset */
3152 ofs = runpopnum(ctx->bifcxrun);
3153
3154 /* get the capture object and size */
3155 obj = tiogetcapture(ctx->bifcxtio);
3156 siz = tiocapturesize(ctx->bifcxtio);
3157 if (obj == MCMONINV)
3158 {
3159 runpnil(ctx->bifcxrun);
3160 return;
3161 }
3162
3163 /* turn off capturing and reset the buffer on the outermost call */
3164 if (ofs == 0)
3165 {
3166 tiocapture(ctx->bifcxtio, mcx, FALSE);
3167 tioclrcapture(ctx->bifcxtio);
3168 }
3169
3170 /* lock the object */
3171 p = mcmlck(mcx, obj);
3172
3173 /* include only the part that happened after the matching call */
3174 p += ofs;
3175 siz = (ofs > siz) ? 0 : siz - ofs;
3176
3177 ERRBEGIN(ctx->bifcxerr)
3178
3179 /* push the string onto the stack */
3180 runpstr(ctx->bifcxrun, (char *)p, siz, 0);
3181
3182 ERRCLEAN(ctx->bifcxerr)
3183 /* done with the object - unlock it */
3184 mcmunlck(mcx, obj);
3185 ERRENDCLN(ctx->bifcxerr)
3186
3187 /* done with the object - unlock it */
3188 mcmunlck(mcx, obj);
3189 break;
3190
3191 default:
3192 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "outcapture");
3193 }
3194 }
3195
3196 /*
3197 * systemInfo
3198 */
3199 void bifsysinfo(bifcxdef *ctx, int argc)
3200 {
3201 runsdef val;
3202 int id;
3203 long result;
3204
3205 /* see what we have */
3206 switch(id = (int)runpopnum(ctx->bifcxrun))
3207 {
3208 case SYSINFO_SYSINFO:
3209 /* systemInfo call is supported in this version - return true */
3210 bifcntargs(ctx, 1, argc);
3211 val.runstyp = DAT_TRUE;
3212 runpush(ctx->bifcxrun, DAT_TRUE, &val);
3213 break;
3214
3215 case SYSINFO_VERSION:
3216 /* get the run-time version string */
3217 bifcntargs(ctx, 1, argc);
3218 runpushcstr(ctx->bifcxrun, TADS_RUNTIME_VERSION,
3219 strlen(TADS_RUNTIME_VERSION), 0);
3220 break;
3221
3222 case SYSINFO_OS_NAME:
3223 /* get the operating system name */
3224 bifcntargs(ctx, 1, argc);
3225 runpushcstr(ctx->bifcxrun, OS_SYSTEM_NAME, strlen(OS_SYSTEM_NAME), 0);
3226 break;
3227
3228 case SYSINFO_HTML:
3229 case SYSINFO_JPEG:
3230 case SYSINFO_PNG:
3231 case SYSINFO_WAV:
3232 case SYSINFO_MIDI:
3233 case SYSINFO_WAV_MIDI_OVL:
3234 case SYSINFO_WAV_OVL:
3235 case SYSINFO_PREF_IMAGES:
3236 case SYSINFO_PREF_SOUNDS:
3237 case SYSINFO_PREF_MUSIC:
3238 case SYSINFO_PREF_LINKS:
3239 case SYSINFO_MPEG:
3240 case SYSINFO_MPEG1:
3241 case SYSINFO_MPEG2:
3242 case SYSINFO_MPEG3:
3243 case SYSINFO_LINKS_HTTP:
3244 case SYSINFO_LINKS_FTP:
3245 case SYSINFO_LINKS_NEWS:
3246 case SYSINFO_LINKS_MAILTO:
3247 case SYSINFO_LINKS_TELNET:
3248 case SYSINFO_PNG_TRANS:
3249 case SYSINFO_PNG_ALPHA:
3250 case SYSINFO_OGG:
3251 case SYSINFO_MNG:
3252 case SYSINFO_MNG_TRANS:
3253 case SYSINFO_MNG_ALPHA:
3254 case SYSINFO_TEXT_HILITE:
3255 case SYSINFO_INTERP_CLASS:
3256 /*
3257 * these information types are all handled by the OS layer, and
3258 * take no additional arguments
3259 */
3260 bifcntargs(ctx, 1, argc);
3261 if (os_get_sysinfo(id, 0, &result))
3262 {
3263 /* we got a valid result - return it */
3264 runpnum(ctx->bifcxrun, result);
3265 }
3266 else
3267 {
3268 /* the code was unknown - return nil */
3269 runpnil(ctx->bifcxrun);
3270 }
3271 break;
3272
3273 case SYSINFO_HTML_MODE:
3274 /* ask the output formatter for its current HTML setting */
3275 bifcntargs(ctx, 1, argc);
3276 val.runstyp = runclog(tio_is_html_mode());
3277 runpush(ctx->bifcxrun, val.runstyp, &val);
3278 break;
3279
3280 case SYSINFO_TEXT_COLORS:
3281 /*
3282 * Text colors are only supported in full HTML interpreters. If
3283 * this is an HTML interpreter, ask the underlying OS layer about
3284 * color support; otherwise, colors are not available, since we
3285 * don't handle colors in our text-only HTML subset.
3286 *
3287 * Colors are NOT supported in the HTML mini-parser in text-only
3288 * interpreters in TADS 2. So, even if we're running in HTML
3289 * mode, if this is a text-only interpreter, we can't display text
3290 * colors.
3291 */
3292 bifcntargs(ctx, 1, argc);
3293 if (os_get_sysinfo(SYSINFO_HTML, 0, &result) && result != 0)
3294 {
3295 /*
3296 * we're in HTML mode, so ask the underlying HTML OS
3297 * implementation for its level of text color support
3298 */
3299 if (os_get_sysinfo(id, 0, &result))
3300 {
3301 /* push the OS-level result */
3302 runpnum(ctx->bifcxrun, result);
3303 }
3304 else
3305 {
3306 /* the OS code doesn't recognize it; assume no support */
3307 runpnum(ctx->bifcxrun, SYSINFO_TXC_NONE);
3308 }
3309 }
3310 else
3311 {
3312 /* we're a text-only interpreter - no color support */
3313 runpnum(ctx->bifcxrun, SYSINFO_TXC_NONE);
3314 }
3315 break;
3316
3317 case SYSINFO_BANNERS:
3318 /* TADS 2 does not offer banner support */
3319 bifcntargs(ctx, 1, argc);
3320 runpnum(ctx->bifcxrun, 0);
3321 break;
3322
3323 default:
3324 /*
3325 * Other codes fail harmlessly with a nil return value. Pop all
3326 * remaining arguments and return nil.
3327 */
3328 for ( ; argc > 1 ; --argc)
3329 rundisc(ctx->bifcxrun);
3330 runpnil(ctx->bifcxrun);
3331 break;
3332 }
3333 }
3334
3335 /*
3336 * morePrompt - display the more prompt and wait for the user to respond
3337 */
3338 void bifmore(bifcxdef *ctx, int argc)
3339 {
3340 /* this function takes no arguments */
3341 bifcntargs(ctx, 0, argc);
3342
3343 /* display the MORE prompt */
3344 tioflushn(ctx->bifcxtio, 1);
3345 out_more_prompt();
3346 }
3347
3348 /*
3349 * parserSetMe
3350 */
3351 void bifsetme(bifcxdef *ctx, int argc)
3352 {
3353 objnum new_me;
3354
3355 /* this function takes one argument */
3356 bifcntargs(ctx, 1, argc);
3357
3358 /* get the new "Me" object */
3359 new_me = runpopobj(ctx->bifcxrun);
3360
3361 /* "Me" cannot be nil */
3362 if (new_me == MCMONINV)
3363 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "parserSetMe");
3364
3365 /* set the current "Me" object in the parser */
3366 voc_set_me(ctx->bifcxrun->runcxvoc, new_me);
3367 }
3368
3369 /*
3370 * parserGetMe
3371 */
3372 void bifgetme(bifcxdef *ctx, int argc)
3373 {
3374 /* this function takes no arguments */
3375 bifcntargs(ctx, 0, argc);
3376
3377 /* return the current Me object */
3378 runpobj(ctx->bifcxrun, ctx->bifcxrun->runcxvoc->voccxme);
3379 }
3380
3381 /*
3382 * reSearch
3383 */
3384 void bifresearch(bifcxdef *ctx, int argc)
3385 {
3386 uchar *patstr;
3387 size_t patlen;
3388 uchar *searchstr;
3389 size_t searchlen;
3390 int result_len;
3391 int match_ofs;
3392
3393 /* this function takes two parameters: pattern, string */
3394 bifcntargs(ctx, 2, argc);
3395
3396 /* get the pattern string */
3397 patstr = runpopstr(ctx->bifcxrun);
3398 patlen = osrp2(patstr) - 2;
3399 patstr += 2;
3400
3401 /* get the search string */
3402 searchstr = runpopstr(ctx->bifcxrun);
3403 searchlen = osrp2(searchstr) - 2;
3404 searchstr += 2;
3405
3406 /* search for the pattern in the string */
3407 match_ofs = re_compile_and_search(&ctx->bifcxregex,
3408 (char *)patstr, patlen,
3409 (char *)searchstr, searchlen,
3410 &result_len);
3411
3412 /*
3413 * if we didn't match, return nil; otherwise, return a list with the
3414 * match offset and length
3415 */
3416 if (match_ofs < 0)
3417 {
3418 /* no match - return nil */
3419 runpnil(ctx->bifcxrun);
3420 }
3421 else
3422 {
3423 ushort listsiz;
3424 runsdef val;
3425 uchar *p;
3426
3427 /*
3428 * build a list consisting of two numbers and a string: two
3429 * bytes for the list header, then two elements at (one byte for
3430 * the datatype header, four bytes for the number), then the
3431 * string element with (one byte for the datatype, two bytes for
3432 * the string length prefix, and the bytes of the string)
3433 */
3434 listsiz = 2 + (1+4)*2 + (1 + 2 + (ushort)(result_len));
3435
3436 /* allocate the space */
3437 runhres(ctx->bifcxrun, listsiz, 0);
3438
3439 /* set up the list stack item */
3440 val.runstyp = DAT_LIST;
3441 p = val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
3442
3443 /* set the list's length */
3444 oswp2(p, listsiz);
3445 p += 2;
3446
3447 /*
3448 * Add the offset element. For consistency with TADS
3449 * conventions, use 1 as the offset of the first character in
3450 * the string - this makes it easy to use the offset value with
3451 * substr().
3452 */
3453 *p++ = DAT_NUMBER;
3454 oswp4s(p, match_ofs + 1);
3455 p += 4;
3456
3457 /* add the length element */
3458 *p++ = DAT_NUMBER;
3459 oswp4s(p, result_len);
3460 p += 4;
3461
3462 /* add the result string */
3463 *p++ = DAT_SSTRING;
3464 oswp2(p, result_len + 2);
3465 p += 2;
3466 memcpy(p, ctx->bifcxregex.strbuf + match_ofs, result_len);
3467
3468 /* reserve the space in the heap */
3469 ctx->bifcxrun->runcxhp += listsiz;
3470
3471 /* return the list */
3472 runrepush(ctx->bifcxrun, &val);
3473 }
3474 }
3475
3476 /* reGetGroup */
3477 void bifregroup(bifcxdef *ctx, int argc)
3478 {
3479 int grp;
3480 size_t len;
3481 re_group_register *reg;
3482 ushort hplen;
3483 runsdef val;
3484 uchar *p;
3485 long numval;
3486
3487 /* this function takes one parameter: the group number to retrieve */
3488 bifcntargs(ctx, 1, argc);
3489
3490 /* get the group number */
3491 grp = (int)runpopnum(ctx->bifcxrun);
3492
3493 /* make sure it's within range */
3494 if (grp < 1 || grp > RE_GROUP_REG_CNT)
3495 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "reGetGroup");
3496
3497 /* adjust from a 1-bias to an array index */
3498 --grp;
3499
3500 /* if the group was never set, return nil */
3501 if (grp >= ctx->bifcxregex.cur_group)
3502 {
3503 runpnil(ctx->bifcxrun);
3504 return;
3505 }
3506
3507 /* get the register */
3508 reg = &ctx->bifcxregex.regs[grp];
3509
3510 /* if the group wasn't set, return nil */
3511 if (reg->start_ofs == 0 || reg->end_ofs == 0)
3512 {
3513 runpnil(ctx->bifcxrun);
3514 return;
3515 }
3516
3517 /* calculate the length of the string in this group */
3518 len = reg->end_ofs - reg->start_ofs;
3519
3520 /*
3521 * reserve the necessary heap space: two bytes for the list length
3522 * prefix, two number elements (one byte each for the type, four
3523 * bytes each for the value), and the string element (one byte for
3524 * the type, two bytes for the length prefix, plus the string
3525 * itself).
3526 */
3527 hplen = (ushort)(2 + 2*(1+4) + (1 + 2 + len));
3528 runhres(ctx->bifcxrun, hplen, 0);
3529
3530 /* set up the stack value */
3531 val.runstyp = DAT_LIST;
3532 p = val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
3533
3534 /* put in the list length prefix */
3535 oswp2(p, hplen);
3536 p += 2;
3537
3538 /* add the starting character position of the group - adjust to 1-bias */
3539 *p++ = DAT_NUMBER;
3540 numval = (long)(reg->start_ofs - ctx->bifcxregex.strbuf) + 1;
3541 oswp4s(p, numval);
3542 p += 4;
3543
3544 /* add the length of the group */
3545 *p++ = DAT_NUMBER;
3546 numval = (long)(reg->end_ofs - reg->start_ofs);
3547 oswp4s(p, numval);
3548 p += 4;
3549
3550 /* set up the string */
3551 *p++ = DAT_SSTRING;
3552 oswp2(p, len+2);
3553 p += 2;
3554 memcpy(p, reg->start_ofs, len);
3555
3556 /* consume the heap space */
3557 ctx->bifcxrun->runcxhp += hplen;
3558
3559 /* push the result */
3560 runrepush(ctx->bifcxrun, &val);
3561 }
3562
3563
3564 /*
3565 * inputevent
3566 */
3567 void bifinpevt(bifcxdef *ctx, int argc)
3568 {
3569 unsigned long timeout;
3570 int use_timeout;
3571 os_event_info_t info;
3572 int evt;
3573 uchar *p;
3574 ushort lstsiz;
3575 runsdef val;
3576 size_t paramlen;
3577 char keyname[20];
3578
3579 /* check for a timeout value */
3580 if (argc == 0)
3581 {
3582 /* there's no timeout */
3583 use_timeout = FALSE;
3584 timeout = 0;
3585 }
3586 else if (argc >= 1)
3587 {
3588 /* get the timeout value */
3589 use_timeout = TRUE;
3590 timeout = (unsigned long)runpopnum(ctx->bifcxrun);
3591 }
3592
3593 /* ensure we don't have too many arguments */
3594 if (argc > 1)
3595 runsig(ctx->bifcxrun, ERR_BIFARGC);
3596
3597 /* flush any pending output */
3598 tioflushn(ctx->bifcxtio, 0);
3599
3600 /* reset count of lines since keyboard input */
3601 tioreset(ctx->bifcxtio);
3602
3603 /* ask the OS code for an event */
3604 evt = os_get_event(timeout, use_timeout, &info);
3605
3606 /*
3607 * the list always minimally needs two bytes of length prefix plus a
3608 * number with the event code (one byte for the type, four bytes for
3609 * the value)
3610 */
3611 lstsiz = 2 + (1 + 4);
3612
3613 /* figure out how much space we'll need based on the event type */
3614 switch(evt)
3615 {
3616 case OS_EVT_KEY:
3617 /*
3618 * we need space for a string with one or two bytes (depending
3619 * on whether or not we have an extended key code) - 1 byte for
3620 * type code, 2 for length prefix, and 1 or 2 for the string's
3621 * contents
3622 */
3623 paramlen = (info.key[0] == 0 ? 2 : 1);
3624
3625 /* map the extended key */
3626 get_ext_key_name(keyname, info.key[0], info.key[1]);
3627
3628 /* determine the length we need for the string */
3629 paramlen = strlen(keyname);
3630
3631 /* add it into the list */
3632 lstsiz += 1 + 2 + paramlen;
3633 break;
3634
3635 case OS_EVT_HREF:
3636 /*
3637 * we need space for the href string - 1 byte for type code, 2
3638 * for length prefix, plus the string's contents
3639 */
3640 paramlen = strlen(info.href);
3641 lstsiz += 1 + 2 + (ushort)paramlen;
3642 break;
3643
3644 default:
3645 /* other event types have no extra data */
3646 break;
3647 }
3648
3649 /* allocate space for the list */
3650 runhres(ctx->bifcxrun, lstsiz, 0);
3651
3652 /* set up the stack value */
3653 val.runstyp = DAT_LIST;
3654 p = val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
3655
3656 /* set up the list length prefix */
3657 oswp2(p, lstsiz);
3658 p += 2;
3659
3660 /* set up the event type element */
3661 *p++ = DAT_NUMBER;
3662 oswp4s(p, evt);
3663 p += 4;
3664
3665 /* add the event parameters, if any */
3666 switch(evt)
3667 {
3668 case OS_EVT_KEY:
3669 /* set up the string for the key */
3670 *p++ = DAT_SSTRING;
3671 oswp2(p, paramlen + 2);
3672 p += 2;
3673
3674 /* add the characters to the string */
3675 memcpy(p, keyname, paramlen);
3676 p += paramlen;
3677 break;
3678
3679 case OS_EVT_HREF:
3680 /* add the string for the href */
3681 *p++ = DAT_SSTRING;
3682 oswp2(p, paramlen + 2);
3683 memcpy(p + 2, info.href, paramlen);
3684 break;
3685 }
3686
3687 /* consume the heap space */
3688 ctx->bifcxrun->runcxhp += lstsiz;
3689
3690 /* push the result */
3691 runrepush(ctx->bifcxrun, &val);
3692 }
3693
3694 /* timeDelay */
3695 void bifdelay(bifcxdef *ctx, int argc)
3696 {
3697 long delay;
3698
3699 /* ensure we have the right number of arguments */
3700 bifcntargs(ctx, 1, argc);
3701
3702 /* flush any pending output */
3703 tioflushn(ctx->bifcxtio, 0);
3704
3705 /* get the delay time */
3706 delay = runpopnum(ctx->bifcxrun);
3707
3708 /* let the system perform the delay */
3709 os_sleep_ms(delay);
3710 }
3711
3712 /* setOutputFilter */
3713 void bifsetoutfilter(bifcxdef *ctx, int argc)
3714 {
3715 /* ensure we have the right number of arguments */
3716 bifcntargs(ctx, 1, argc);
3717
3718 /* see what we have */
3719 switch(runtostyp(ctx->bifcxrun))
3720 {
3721 case DAT_NIL:
3722 /* remove the current filter */
3723 out_set_filter(MCMONINV);
3724
3725 /* discard the argument */
3726 rundisc(ctx->bifcxrun);
3727 break;
3728
3729 case DAT_FNADDR:
3730 /* set the filter to the given function */
3731 out_set_filter(runpopfn(ctx->bifcxrun));
3732 break;
3733
3734 default:
3735 /* anything else is invalid */
3736 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "setOutputFilter");
3737 }
3738 }
3739
3740 /*
3741 * Get an optional object argument. If the next argument is not an
3742 * object value, or we're out of arguments, we'll return MCMONINV.
3743 * Otherwise, we'll pop the object value and return it, decrementing the
3744 * remaining argument counter provided.
3745 */
3746 static objnum bif_get_optional_obj_arg(bifcxdef *ctx, int *rem_argc)
3747 {
3748 /* if we're out of arguments, there's no object value */
3749 if (*rem_argc == 0)
3750 return MCMONINV;
3751
3752 /*
3753 * if the next argument is not an object or nil, we're out of object
3754 * arguments
3755 */
3756 if (runtostyp(ctx->bifcxrun) != DAT_OBJECT
3757 && runtostyp(ctx->bifcxrun) != DAT_NIL)
3758 return MCMONINV;
3759
3760 /* we have an object - remove it from the remaining argument count */
3761 --(*rem_argc);
3762
3763 /* pop and return the object value */
3764 return runpopobjnil(ctx->bifcxrun);
3765 }
3766
3767 /*
3768 * execCommand flag values
3769 */
3770 #define EC_HIDE_SUCCESS 0x00000001
3771 #define EC_HIDE_ERROR 0x00000002
3772 #define EC_SKIP_VALIDDO 0x00000004
3773 #define EC_SKIP_VALIDIO 0x00000008
3774
3775 /*
3776 * execCommand - execute a recursive command
3777 */
3778 void bifexec(bifcxdef *ctx, int argc)
3779 {
3780 objnum actor;
3781 objnum verb;
3782 objnum dobj;
3783 objnum prep;
3784 objnum iobj;
3785 int err;
3786 uint capture_start;
3787 uint capture_end;
3788 objnum capture_obj;
3789 ulong flags;
3790 int hide_any;
3791 int rem_argc;
3792
3793 /*
3794 * Check for the correct argument count. The first two arguments
3795 * are required; additional arguments are optional.
3796 */
3797 if (argc < 2 || argc > 6)
3798 runsig(ctx->bifcxrun, ERR_BIFARGC);
3799
3800 /* pop the arguments - actor, verb, dobj, prep, iobj */
3801 actor = runpopobjnil(ctx->bifcxrun);
3802 verb = runpopobjnil(ctx->bifcxrun);
3803
3804 /*
3805 * The other object arguments are optional. If we run into a
3806 * numeric argument, it's the flags value, in which case we're out
3807 * of objects.
3808 */
3809 rem_argc = argc - 2;
3810 dobj = bif_get_optional_obj_arg(ctx, &rem_argc);
3811 prep = bif_get_optional_obj_arg(ctx, &rem_argc);
3812 iobj = bif_get_optional_obj_arg(ctx, &rem_argc);
3813
3814 /* if we have a flags argument, pop it */
3815 if (rem_argc > 0)
3816 {
3817 /* the last argument is the flags - pop the numeric value */
3818 flags = runpopnum(ctx->bifcxrun);
3819
3820 /* remove it from the remaining argument counter */
3821 --rem_argc;
3822 }
3823 else
3824 {
3825 /* no flags specified - use zero by default */
3826 flags = 0;
3827 }
3828
3829 /*
3830 * make sure we don't have any arguments left - if we do, then it
3831 * means that we got an incorrect type and skipped an argument when
3832 * we were trying to sense the meanings of the arguments from their
3833 * types
3834 */
3835 if (rem_argc != 0)
3836 runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "execCommand");
3837
3838 /* if we're hiding any output, start output capture */
3839 hide_any = ((flags & (EC_HIDE_SUCCESS | EC_HIDE_ERROR)) != 0);
3840 if (hide_any)
3841 {
3842 /* start capturing */
3843 tiocapture(ctx->bifcxtio, ctx->bifcxrun->runcxmem, TRUE);
3844
3845 /* note the current output position */
3846 capture_start = tiocapturesize(ctx->bifcxtio);
3847 }
3848
3849 /* execute the command */
3850 err = execmd_recurs(ctx->bifcxrun->runcxvoc,
3851 actor, verb, dobj, prep, iobj,
3852 (flags & EC_SKIP_VALIDDO) == 0,
3853 (flags & EC_SKIP_VALIDIO) == 0);
3854
3855 /* if we're hiding any output, end hiding */
3856 if (hide_any)
3857 {
3858 uchar *p;
3859 int hide;
3860
3861 /* get the capture buffer size */
3862 capture_end = tiocapturesize(ctx->bifcxtio);
3863
3864 /* turn off capture if it wasn't already on when we started */
3865 if (capture_start == 0)
3866 tiocapture(ctx->bifcxtio, ctx->bifcxrun->runcxmem, FALSE);
3867
3868 /* determine whether we're hiding or showing the result */
3869 if (err == 0)
3870 hide = ((flags & EC_HIDE_SUCCESS) != 0);
3871 else
3872 hide = ((flags & EC_HIDE_ERROR) != 0);
3873
3874 /* show or hide the result, as appropriate */
3875 if (hide)
3876 {
3877 /*
3878 * We're hiding this result, so do not display the string.
3879 * If there's an enclosing capture, remove the string from
3880 * the enclosing capture.
3881 */
3882 if (capture_start != 0)
3883 tiopopcapture(ctx->bifcxtio, capture_start);
3884 }
3885 else
3886 {
3887 /*
3888 * We're showing the text. If we're in an enclosing
3889 * capture, do nothing - simply leave the string in the
3890 * enclosing capture buffer; otherwise, actually display it
3891 */
3892 if (capture_start == 0)
3893 {
3894 /* lock the capture object */
3895 capture_obj = tiogetcapture(ctx->bifcxtio);
3896 p = mcmlck(ctx->bifcxrun->runcxmem, capture_obj);
3897
3898 ERRBEGIN(ctx->bifcxerr)
3899 {
3900 /* display the string */
3901 outformatlen((char *)p + capture_start,
3902 capture_end - capture_start);
3903 }
3904 ERRCLEAN(ctx->bifcxerr)
3905 {
3906 /* unlock the capture object before signalling out */
3907 mcmunlck(ctx->bifcxrun->runcxmem, capture_obj);
3908 }
3909 ERRENDCLN(ctx->bifcxerr);
3910
3911 /* unlock the capture object */
3912 mcmunlck(ctx->bifcxrun->runcxmem, capture_obj);
3913 }
3914 }
3915
3916 /* clear the capture buffer if it wasn't on when we started */
3917 if (capture_start == 0)
3918 tioclrcapture(ctx->bifcxtio);
3919 }
3920
3921 /* push the result code */
3922 runpnum(ctx->bifcxrun, err);
3923 }
3924
3925 /*
3926 * parserGetObj - get one of the objects associated with the command
3927 */
3928 void bifgetobj(bifcxdef *ctx, int argc)
3929 {
3930 int id;
3931 objnum obj;
3932 voccxdef *voc = ctx->bifcxrun->runcxvoc;
3933
3934 /* check the argument count */
3935 bifcntargs(ctx, 1, argc);
3936
3937 /* get the argument */
3938 id = (int)runpopnum(ctx->bifcxrun);
3939
3940 /* get the appropriate object */
3941 switch(id)
3942 {
3943 case 1:
3944 /* get the current actor */
3945 obj = voc->voccxactor;
3946
3947 /* if there's no current actor, use the current 'me' by default */
3948 if (obj == MCMONINV)
3949 obj = voc->voccxme;
3950
3951 /* done */
3952 break;
3953
3954 case 2:
3955 /* verb */
3956 obj = voc->voccxverb;
3957 break;
3958
3959 case 3:
3960 /* direct object */
3961 obj = (voc->voccxdobj == 0 ? MCMONINV : voc->voccxdobj->vocolobj);
3962 break;
3963
3964 case 4:
3965 /* preposition */
3966 obj = voc->voccxprep;
3967 break;
3968
3969 case 5:
3970 /* indirect object */
3971 obj = (voc->voccxiobj == 0 ? MCMONINV : voc->voccxiobj->vocolobj);
3972 break;
3973
3974 case 6:
3975 /* "it" */
3976 obj = voc->voccxit;
3977 break;
3978
3979 case 7:
3980 /* "him" */
3981 obj = voc->voccxhim;
3982 break;
3983
3984 case 8:
3985 /* "her" */
3986 obj = voc->voccxher;
3987 break;
3988
3989 case 9:
3990 /* them */
3991 voc_push_objlist(voc, voc->voccxthm, voc->voccxthc);
3992
3993 /*
3994 * return directly, since we've already pushed the result (it's
3995 * a list, not an object)
3996 */
3997 return;
3998
3999 default:
4000 /* invalid argument */
4001 runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "parserGetObj");
4002 break;
4003 }
4004
4005 /* return the object */
4006 runpobj(ctx->bifcxrun, obj);
4007 }
4008
4009 /*
4010 * parseNounList - parse a noun list. Call like this:
4011 *
4012 * parserParseNounList(wordlist, typelist, starting_index, complain,
4013 * multi, check_actor);
4014 */
4015 void bifparsenl(bifcxdef *ctx, int argc)
4016 {
4017 /* check the argument count */
4018 bifcntargs(ctx, 6, argc);
4019
4020 /* call the parser */
4021 voc_parse_np(ctx->bifcxrun->runcxvoc);
4022 }
4023
4024 /*
4025 * parserTokenize - given a string, produce a list of tokens. Returns
4026 * nil on error, or a list of token strings.
4027 *
4028 * parserTokenize(commandString);
4029 */
4030 void bifprstok(bifcxdef *ctx, int argc)
4031 {
4032 /* check arguments */
4033 bifcntargs(ctx, 1, argc);
4034
4035 /* call the parser */
4036 voc_parse_tok(ctx->bifcxrun->runcxvoc);
4037 }
4038
4039 /*
4040 * parserGetTokTypes - given a list of tokens (represented as strings),
4041 * get a corresponding list of token types.
4042 *
4043 * parserGetTokTypes(tokenList);
4044 */
4045 void bifprstoktyp(bifcxdef *ctx, int argc)
4046 {
4047 /* check arguments */
4048 bifcntargs(ctx, 1, argc);
4049
4050 /* call the parser */
4051 voc_parse_types(ctx->bifcxrun->runcxvoc);
4052 }
4053
4054 /*
4055 * parserDictLookup - given a list of tokens and their types, produce a
4056 * list of all of the objects that match all of the words.
4057 *
4058 * parserDictLookup(tokenList, typeList);
4059 */
4060 void bifprsdict(bifcxdef *ctx, int argc)
4061 {
4062 /* check arguments */
4063 bifcntargs(ctx, 2, argc);
4064
4065 /* call the parser */
4066 voc_parse_dict_lookup(ctx->bifcxrun->runcxvoc);
4067 }
4068
4069 /*
4070 * parserResolveObjects - resolve an object list of the sort returned by
4071 * parseNounList. Validates and disambiguates the objects.
4072 *
4073 * parserResolveObjects(actor, verb, prep, otherobj, usageType,
4074 * verprop, tokenList, objList, silent)
4075 */
4076 void bifprsrslv(bifcxdef *ctx, int argc)
4077 {
4078 /* check arguments */
4079 bifcntargs(ctx, 9, argc);
4080
4081 /* call the parser */
4082 voc_parse_disambig(ctx->bifcxrun->runcxvoc);
4083 }
4084
4085 /*
4086 * parserReplaceCommand - replace the current command line with a new
4087 * string. Aborts the current command.
4088 */
4089 void bifprsrplcmd(bifcxdef *ctx, int argc)
4090 {
4091 /* check arguments */
4092 bifcntargs(ctx, 1, argc);
4093
4094 /* call the parser */
4095 voc_parse_replace_cmd(ctx->bifcxrun->runcxvoc);
4096 }
4097
4098 /*
4099 * exitobj - throw a RUNEXITOBJ error
4100 */
4101 void bifexitobj(bifcxdef *ctx, int argc)
4102 {
4103 /* no arguments are allowed */
4104 bifcntargs(ctx, 0, argc);
4105
4106 /* throw the RUNEXITOBJ error */
4107 errsig(ctx->bifcxerr, ERR_RUNEXITOBJ);
4108 }
4109
4110 /*
4111 * Standard system button labels for bifinpdlg()
4112 */
4113 #define BIFINPDLG_LBL_OK 1
4114 #define BIFINPDLG_LBL_CANCEL 2
4115 #define BIFINPDLG_LBL_YES 3
4116 #define BIFINPDLG_LBL_NO 4
4117
4118 /*
4119 * inputdialog
4120 */
4121 void bifinpdlg(bifcxdef *ctx, int argc)
4122 {
4123 uchar *p;
4124 char prompt[256];
4125 char lblbuf[256];
4126 char *labels[10];
4127 char *dst;
4128 char *xp;
4129 uint len;
4130 size_t bcnt;
4131 int default_resp, cancel_resp;
4132 int resp;
4133 int std_btns;
4134 int icon_id;
4135
4136 /* check for proper arguments */
4137 bifcntargs(ctx, 5, argc);
4138
4139 /* get the icon number */
4140 icon_id = runpopnum(ctx->bifcxrun);
4141
4142 /* get the prompt string */
4143 p = runpopstr(ctx->bifcxrun);
4144 bifcstr(ctx, prompt, (size_t)sizeof(prompt), p);
4145
4146 /* translate from internal to local characters */
4147 for (xp = prompt ; *xp != '\0' ; xp++)
4148 *xp = (char)cmap_i2n(*xp);
4149
4150 /* check for a standard button set selection */
4151 if (runtostyp(ctx->bifcxrun) == DAT_NUMBER)
4152 {
4153 /* get the standard button set ID */
4154 std_btns = runpopnum(ctx->bifcxrun);
4155
4156 /* there are no actual buttons */
4157 bcnt = 0;
4158 }
4159 else
4160 {
4161 /* we're not using standard buttons */
4162 std_btns = 0;
4163
4164 /* get the response string list */
4165 p = runpoplst(ctx->bifcxrun);
4166 len = osrp2(p);
4167 p += 2;
4168
4169 /* build our internal button list */
4170 for (bcnt = 0, dst = lblbuf ; len != 0 ; lstadv(&p, &len))
4171 {
4172 /* see what we have */
4173 if (*p == DAT_SSTRING)
4174 {
4175 /* it's a label string - convert to a C string */
4176 bifcstr(ctx, dst, sizeof(lblbuf) - (dst - lblbuf), p + 1);
4177
4178 /* translate from internal to local characters */
4179 for (xp = dst ; *xp != '\0' ; xp++)
4180 *xp = (char)cmap_i2n(*xp);
4181
4182 /* set this button to point to the converted text */
4183 labels[bcnt++] = dst;
4184
4185 /* move past this label in the button buffer */
4186 dst += strlen(dst) + 1;
4187 }
4188 else if (*p == DAT_NUMBER)
4189 {
4190 int id;
4191 int resid;
4192
4193 /* it's a standard system label ID - get the ID */
4194 id = (int)osrp4s(p + 1);
4195
4196 /* translate it to the appropriate string resource */
4197 switch(id)
4198 {
4199 case BIFINPDLG_LBL_OK:
4200 resid = RESID_BTN_OK;
4201 break;
4202
4203 case BIFINPDLG_LBL_CANCEL:
4204 resid = RESID_BTN_CANCEL;
4205 break;
4206
4207 case BIFINPDLG_LBL_YES:
4208 resid = RESID_BTN_YES;
4209 break;
4210
4211 case BIFINPDLG_LBL_NO:
4212 resid = RESID_BTN_NO;
4213 break;
4214
4215 default:
4216 resid = 0;
4217 break;
4218 }
4219
4220 /*
4221 * if we got a valid resource ID, load the resource;
4222 * otherwise, skip this button
4223 */
4224 if (resid != 0
4225 && !os_get_str_rsc(resid, dst,
4226 sizeof(lblbuf) - (dst - lblbuf)))
4227 {
4228 /* set this button to point to the converted text */
4229 labels[bcnt++] = dst;
4230
4231 /* move past this label in the button buffer */
4232 dst += strlen(dst) + 1;
4233 }
4234 }
4235
4236 /* if we have exhausted our label array, stop now */
4237 if (bcnt >= sizeof(labels)/sizeof(labels[0])
4238 || dst >= lblbuf + sizeof(lblbuf))
4239 break;
4240 }
4241 }
4242
4243 /* get the default response */
4244 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
4245 {
4246 rundisc(ctx->bifcxrun);
4247 default_resp = 0;
4248 }
4249 else
4250 default_resp = runpopnum(ctx->bifcxrun);
4251
4252 /* get the cancel response */
4253 if (runtostyp(ctx->bifcxrun) == DAT_NIL)
4254 {
4255 rundisc(ctx->bifcxrun);
4256 cancel_resp = 0;
4257 }
4258 else
4259 cancel_resp = runpopnum(ctx->bifcxrun);
4260
4261 /* flush output before showing the dialog */
4262 tioflushn(ctx->bifcxtio, 0);
4263
4264 /* show the dialog */
4265 resp = tio_input_dialog(icon_id, prompt, std_btns,
4266 (const char **)labels, bcnt,
4267 default_resp, cancel_resp);
4268
4269 /* return the result */
4270 runpnum(ctx->bifcxrun, resp);
4271 }
4272
4273 /*
4274 * Determine if a resource exists
4275 */
4276 void bifresexists(bifcxdef *ctx, int argc)
4277 {
4278 uchar *p;
4279 char resname[OSFNMAX];
4280 appctxdef *appctx;
4281 int found;
4282 runsdef val;
4283
4284 /* check for proper arguments */
4285 bifcntargs(ctx, 1, argc);
4286
4287 /* get the resource name string */
4288 p = runpopstr(ctx->bifcxrun);
4289 bifcstr(ctx, resname, (size_t)sizeof(resname), p);
4290
4291 /*
4292 * if we have a host application context, and it provides a resource
4293 * finder function, ask the resource finder if the resource is
4294 * available; otherwise, report that the resource is not loadable,
4295 * since we must not be running a version of the interpreter that
4296 * supports external resource loading
4297 */
4298 appctx = ctx->bifcxappctx;
4299 found = (appctx != 0
4300 && appctx->resfile_exists != 0
4301 && (*appctx->resfile_exists)(appctx->resfile_exists_ctx,
4302 resname, strlen(resname)));
4303
4304 /* push the result */
4305 runpush(ctx->bifcxrun, runclog(found), &val);
4306 }