-
Notifications
You must be signed in to change notification settings - Fork 1
/
bif.c
4326 lines (3671 loc) · 114 KB
/
bif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/TADS2/BIF.C,v 1.4 1999/07/11 00:46:29 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
bif.c - built-in function implementation
Function
Implements built-in functions for TADS
Notes
None
Modified
04/11/99 CNebel - fix signed/unsigned mismatches
12/16/92 MJRoberts - add TADS/Graphic functions
12/26/91 MJRoberts - creation
*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "os.h"
#include "std.h"
#include "bif.h"
#include "tio.h"
#include "run.h"
#include "voc.h"
#include "fio.h"
#include "dbg.h"
#include "prp.h"
#include "lst.h"
#include "trd.h"
#include "regex.h"
#include "res.h"
#include "cmap.h"
/* yorn - yes or no */
void bifyon(bifcxdef *ctx, int argc)
{
char rsp[128];
char *p;
runsdef val;
char yesbuf[64];
char nobuf[64];
re_context rectx;
int match_yes;
int match_no;
bifcntargs(ctx, 0, argc); /* check for proper argument count */
/* load the "yes" and "no" reply patterns */
if (os_get_str_rsc(RESID_YORN_YES, yesbuf, sizeof(yesbuf)))
strcpy(yesbuf, "[Yy].*");
if (os_get_str_rsc(RESID_YORN_NO, nobuf, sizeof(nobuf)))
strcpy(nobuf, "[Nn].*");
/* if we're in HTML mode, switch to input font */
if (tio_is_html_mode())
tioputs(ctx->bifcxtio, "<font face='TADS-Input'>");
/* ensure the prompt is displayed */
tioflushn(ctx->bifcxtio, 0);
/* reset count of lines since the last keyboard input */
tioreset(ctx->bifcxtio);
/* read a line of text */
if (tiogets(ctx->bifcxtio, (char *)0, rsp, (int)sizeof(rsp)))
runsig(ctx->bifcxrun, ERR_RUNQUIT);
/* if we're in HTML mode, close the input font tag */
if (tio_is_html_mode())
tioputs(ctx->bifcxtio, "</font>");
/* scan off leading spaces */
for (p = rsp ; t_isspace(*p) ; ++p) ;
/* set up our regex context */
re_init(&rectx, ctx->bifcxerr);
/* check for a "yes" response */
match_yes = re_compile_and_match(&rectx, yesbuf, strlen(yesbuf),
p, strlen(p));
/* check for a "no" response */
match_no = re_compile_and_match(&rectx, nobuf, strlen(nobuf),
p, strlen(p));
/* check the result */
if (match_yes == (int)strlen(p))
val.runsv.runsvnum = 1;
else if (match_no == (int)strlen(p))
val.runsv.runsvnum = 0;
else
val.runsv.runsvnum = -1;
/* delete our regex context */
re_delete(&rectx);
/* push the result */
runpush(ctx->bifcxrun, DAT_NUMBER, &val);
}
/* setfuse */
void bifsfs(bifcxdef *ctx, int argc)
{
objnum func;
uint tm;
runsdef val;
voccxdef *voc = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 3, argc); /* check for proper argument count */
func = runpopfn(ctx->bifcxrun);
tm = runpopnum(ctx->bifcxrun);
runpop(ctx->bifcxrun, &val);
/* limitation: don't allow string or list for value */
if (val.runstyp == DAT_LIST || val.runstyp == DAT_SSTRING)
runsig(ctx->bifcxrun, ERR_FUSEVAL);
vocsetfd(voc, voc->voccxfus, func, (prpnum)0,
tm, &val, ERR_MANYFUS);
}
/* remfuse */
void bifrfs(bifcxdef *ctx, int argc)
{
objnum func;
runsdef val;
voccxdef *voc = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 2, argc);
func = runpopfn(ctx->bifcxrun);
runpop(ctx->bifcxrun, &val);
vocremfd(voc, voc->voccxfus, func, (prpnum)0,
&val, ERR_NOFUSE);
}
/* setdaemon */
void bifsdm(bifcxdef *ctx, int argc)
{
objnum func;
runsdef val;
voccxdef *voc = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 2, argc); /* check for proper argument count */
func = runpopfn(ctx->bifcxrun);
runpop(ctx->bifcxrun, &val);
/* limitation: don't allow string or list for value */
if (val.runstyp == DAT_LIST || val.runstyp == DAT_SSTRING)
runsig(ctx->bifcxrun, ERR_FUSEVAL);
vocsetfd(voc, voc->voccxdmn, func, (prpnum)0, 0,
&val, ERR_MANYDMN);
}
/* remdaemon */
void bifrdm(bifcxdef *ctx, int argc)
{
objnum func;
runsdef val;
voccxdef *voc = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 2, argc);
func = runpopfn(ctx->bifcxrun);
runpop(ctx->bifcxrun, &val);
vocremfd(voc, voc->voccxdmn, func, (prpnum)0,
&val, ERR_NODMN);
}
/* incturn */
void bifinc(bifcxdef *ctx, int argc)
{
int turncnt;
if (argc == 1)
{
/* get the number of turns to skip */
turncnt = runpopnum(ctx->bifcxrun);
if (turncnt < 1)
runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "incturn");
}
else
{
/* no arguments -> increment by one turn */
bifcntargs(ctx, 0, argc);
turncnt = 1;
}
/* skip the given number of turns */
vocturn(ctx->bifcxrun->runcxvoc, turncnt, TRUE);
}
/* skipturn */
void bifskt(bifcxdef *ctx, int argc)
{
int turncnt;
bifcntargs(ctx, 1, argc);
turncnt = runpopnum(ctx->bifcxrun);
if (turncnt < 1)
runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "skipturn");
vocturn(ctx->bifcxrun->runcxvoc, turncnt, FALSE);
}
/* quit */
void bifqui(bifcxdef *ctx, int argc)
{
/* check for proper argument count */
bifcntargs(ctx, 0, argc);
/* flush output buffer, and signal the end of the game */
tioflush(ctx->bifcxtio);
errsig(ctx->bifcxerr, ERR_RUNQUIT);
}
/* internal function to convert a TADS string into a C-string */
static void bifcstr(bifcxdef *ctx, char *buf, size_t bufsiz, uchar *str)
{
size_t srcrem;
size_t dstrem;
uchar *src;
char *dst;
/* get the length and text portion of the string */
srcrem = osrp2(str) - 2;
str += 2;
/* scan the string, and convert escapes */
for (src = str, dst = buf, dstrem = bufsiz ;
srcrem != 0 && dstrem != 0 ; ++src, --srcrem)
{
/* if we have an escape sequence, convert it */
if (*src == '\\')
{
/* skip the backslash in the input */
++src;
--srcrem;
/* if there's nothing left, store the backslash */
if (srcrem == 0)
{
/* store the backslash */
*dst++ = '\\';
--dstrem;
/* there's nothing left to scan */
break;
}
/* see what the second half of the escape sequence is */
switch(*src)
{
case 'n':
/* store a C-style newline character */
*dst++ = '\n';
--dstrem;
break;
case 't':
/* store a C-style tab */
*dst++ = '\t';
--dstrem;
break;
case '(':
case ')':
/* entirely omit the highlighting sequences */
break;
default:
/* store everything else unchanged */
*dst++ = *src;
--dstrem;
break;
}
}
else
{
/* copy this character unchanged */
*dst++ = *src;
--dstrem;
}
}
/* if the buffer wasn't big enough, signal an error */
if (dstrem == 0)
runsig(ctx->bifcxrun, ERR_BIFCSTR);
/* null-terminate the result string */
*dst = '\0';
}
/* save */
void bifsav(bifcxdef *ctx, int argc)
{
uchar *fn;
char buf[OSFNMAX];
int err;
runsdef val;
bifcntargs(ctx, 1, argc);
fn = runpopstr(ctx->bifcxrun);
bifcstr(ctx, buf, (size_t)sizeof(buf), fn);
os_defext(buf, ctx->bifcxsavext != 0 ? ctx->bifcxsavext : "sav");
err = fiosav(ctx->bifcxrun->runcxvoc, buf, ctx->bifcxrun->runcxgamename);
runpush(ctx->bifcxrun, runclog(err), &val);
}
/* restore */
void bifrso(bifcxdef *ctx, int argc)
{
uchar *fn;
char buf[OSFNMAX];
int err;
voccxdef *vctx = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 1, argc);
/* check for special restore(nil) - restore game given as parameter */
if (runtostyp(ctx->bifcxrun) == DAT_NIL)
{
/* get filename from startup parameter, if any */
if (!os_paramfile(buf))
{
/* no startup parameter */
runpnum(ctx->bifcxrun, FIORSO_NO_PARAM_FILE);
return;
}
}
else
{
/* get string parameter - it's the filename */
fn = runpopstr(ctx->bifcxrun);
bifcstr(ctx, buf, (size_t)sizeof(buf), fn);
os_defext(buf, ctx->bifcxsavext != 0 ? ctx->bifcxsavext : "sav");
}
/* try restoring the file */
err = fiorso(vctx, buf);
/* blow away all undo records */
objulose(vctx->voccxundo);
/* return the result code from fiorso */
runpnum(ctx->bifcxrun, err);
/* note that the rest of the command line is to be ignored */
vctx->voccxflg |= VOCCXFCLEAR;
}
/* logging */
void biflog(bifcxdef *ctx, int argc)
{
char buf[OSFNMAX];
uchar *str;
bifcntargs(ctx, 1, argc);
if (runtostyp(ctx->bifcxrun) == DAT_NIL)
{
rundisc(ctx->bifcxrun);
tiologcls(ctx->bifcxtio);
}
else
{
str = runpopstr(ctx->bifcxrun);
bifcstr(ctx, buf, (size_t)sizeof(buf), str);
tiologopn(ctx->bifcxtio, buf);
}
}
/* restart */
void bifres(bifcxdef *ctx, int argc)
{
voccxdef *vctx = ctx->bifcxrun->runcxvoc;
objnum fn;
if (argc == 2)
fn = runpopfn(ctx->bifcxrun); /* get function if present */
else
{
bifcntargs(ctx, 0, argc); /* check for proper argument count */
fn = MCMONINV; /* no function was specified */
}
objulose(vctx->voccxundo); /* blow away all undo records */
vocrevert(vctx); /* revert all objects to original state */
vocdmnclr(vctx); /* clear out fuses/deamons/notifiers */
/* restore the original "Me" object */
vctx->voccxme = vctx->voccxme_init;
/* call preinit if necessary (call it before invoking the user callback) */
if (vctx->voccxpreinit != MCMONINV)
runfn(ctx->bifcxrun, vctx->voccxpreinit, 0);
/*
* If a restart function was provided, call it. Note that we left
* the argument for the function on the stack, so there's no need to
* re-push it!
*/
if (fn != MCMONINV) runfn(ctx->bifcxrun, fn, 1);
/* restart the game */
errsig(ctx->bifcxerr, ERR_RUNRESTART);
}
/* input - get a line of input from the keyboard */
void bifinp(bifcxdef *ctx, int argc)
{
char inbuf[128];
/* check for proper argument count */
bifcntargs(ctx, 0, argc);
/* make sure the prompt is displayed */
tioflushn(ctx->bifcxtio, 0);
/* reset count of lines since the last keyboard input */
tioreset(ctx->bifcxtio);
/* read a line of text */
if (tiogets(ctx->bifcxtio, (char *)0, inbuf, (int)sizeof(inbuf)))
runsig(ctx->bifcxrun, ERR_RUNQUIT);
/* push the string, converting escapes */
runpushcstr(ctx->bifcxrun, inbuf, strlen(inbuf), 0);
}
/* notify */
void bifnfy(bifcxdef *ctx, int argc)
{
objnum objn;
prpnum prp;
uint tm;
voccxdef *voc = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 3, argc); /* check for proper argument count */
objn = runpopobj(ctx->bifcxrun);
prp = runpopprp(ctx->bifcxrun);
tm = runpopnum(ctx->bifcxrun);
/* a time of zero means every turn */
if (tm == 0)
tm = VOCDTIM_EACH_TURN;
vocsetfd(voc, voc->voccxalm, objn, prp, tm,
(runsdef *)0, ERR_MANYNFY);
}
/* unnotify */
void bifunn(bifcxdef *ctx, int argc)
{
objnum objn;
prpnum prop;
voccxdef *voc = ctx->bifcxrun->runcxvoc;
bifcntargs(ctx, 2, argc);
objn = runpopobj(ctx->bifcxrun);
prop = runpopprp(ctx->bifcxrun);
vocremfd(voc, voc->voccxalm, objn, prop,
(runsdef *)0, ERR_NONFY);
}
/* trace on/off */
void biftrc(bifcxdef *ctx, int argc)
{
runsdef val;
int n;
int flag;
if (argc == 2)
{
/* get the type indicator and the on/off status */
n = runpopnum(ctx->bifcxrun);
flag = runpoplog(ctx->bifcxrun);
/* see what type of debugging they want to turn on or off */
switch(n)
{
case 1:
/* turn on parser tracing */
if (flag)
ctx->bifcxrun->runcxvoc->voccxflg |= VOCCXFDBG;
else
ctx->bifcxrun->runcxvoc->voccxflg &= ~VOCCXFDBG;
break;
default:
/* ignore other requests */
runsig1(ctx->bifcxrun, ERR_INVVBIF, ERRTSTR, "debugTrace");
}
}
else
{
/* break into debugger; return whether debugger is present */
bifcntargs(ctx, 0, argc);
runpush(ctx->bifcxrun, runclog(dbgstart(ctx->bifcxrun->runcxdbg)),
&val);
}
}
/* say */
void bifsay(bifcxdef *ctx, int argc)
{
uchar *str;
long num;
char numbuf[30];
if (argc != 2) bifcntargs(ctx, 1, argc);
switch(runtostyp(ctx->bifcxrun))
{
case DAT_NUMBER:
num = runpopnum(ctx->bifcxrun);
sprintf(numbuf, "%ld", num);
tioputs(ctx->bifcxtio, numbuf);
break;
case DAT_SSTRING:
str = runpopstr(ctx->bifcxrun);
outfmt(ctx->bifcxtio, str);
break;
case DAT_NIL:
(void)runpoplog(ctx->bifcxrun);
break;
default:
runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "say");
}
}
/* car */
void bifcar(bifcxdef *ctx, int argc)
{
uchar *lstp;
uint lstsiz;
runsdef val;
bifcntargs(ctx, 1, argc);
bifchkarg(ctx, DAT_LIST);
lstp = runpoplst(ctx->bifcxrun);
/* get list's size, and point to its data string */
lstsiz = osrp2(lstp) - 2;
lstp += 2;
/* push first element if one is present, otherwise push nil */
if (lstsiz)
runpbuf(ctx->bifcxrun, *lstp, lstp+1);
else
runpush(ctx->bifcxrun, DAT_NIL, &val);
}
/* cdr */
void bifcdr(bifcxdef *ctx, int argc)
{
uchar *lstp;
uint siz;
uint lstsiz;
runsdef val;
runsdef stkval;
bifcntargs(ctx, 1, argc);
bifchkarg(ctx, DAT_LIST);
lstp = runpoplst(ctx->bifcxrun);
stkval.runstyp = DAT_LIST;
stkval.runsv.runsvstr = lstp;
/* get list's size, and point to its data string */
lstsiz = osrp2(lstp) - 2;
lstp += 2;
if (lstsiz != 0)
{
/* deduct size of first element from size of list */
siz = datsiz(*lstp, lstp+1) + 1;
lstsiz -= siz;
/* add in the size prefix for our new list size */
lstsiz += 2;
/* allocate space for new list containing rest of list */
runhres1(ctx->bifcxrun, lstsiz, 1, &stkval);
lstp = stkval.runsv.runsvstr + siz + 2;
/* write out size followed by list value string */
oswp2(ctx->bifcxrun->runcxhp, lstsiz);
memcpy(ctx->bifcxrun->runcxhp+2, lstp, (size_t)(lstsiz-2));
val.runsv.runsvstr = ctx->bifcxrun->runcxhp;
val.runstyp = DAT_LIST;
ctx->bifcxrun->runcxhp += lstsiz;
runrepush(ctx->bifcxrun, &val);
}
else
runpush(ctx->bifcxrun, DAT_NIL, &val); /* empty list - cdr is nil */
}
/* caps */
void bifcap(bifcxdef *ctx, int argc)
{
bifcntargs(ctx, 0, argc);
tiocaps(ctx->bifxtio); /* set output driver next-char-capitalized flag */
}
/* nocaps */
void bifnoc(bifcxdef *ctx, int argc)
{
bifcntargs(ctx, 0, argc);
tionocaps(ctx->bifxtio); /* set next-not-capitalized flag */
}
/* length */
void biflen(bifcxdef *ctx, int argc)
{
uchar *p;
runsdef val;
long len;
int l;
bifcntargs(ctx, 1, argc);
switch(runtostyp(ctx->bifcxrun))
{
case DAT_SSTRING:
p = (uchar *)runpopstr(ctx->bifcxrun);
len = osrp2(p) - 2;
break;
case DAT_LIST:
p = runpoplst(ctx->bifcxrun);
l = osrp2(p) - 2;
p += 2;
/* count all elements in list */
for (len = 0 ; l ; ++len)
{
int cursiz;
/* get size of this element, and move past it */
cursiz = datsiz(*p, p+1) + 1;
l -= cursiz;
p += cursiz;
}
break;
default:
runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "length");
}
val.runsv.runsvnum = len;
runpush(ctx->bifcxrun, DAT_NUMBER, &val);
}
/* find */
void biffnd(bifcxdef *ctx, int argc)
{
uchar *p1, *p2;
int len1, len2;
int outv;
runsdef val;
int typ;
int siz;
bifcntargs(ctx, 2, argc);
switch(runtostyp(ctx->bifcxrun))
{
case DAT_SSTRING:
p1 = runpopstr(ctx->bifcxrun);
len1 = osrp2(p1) - 2;
p1 += 2;
p2 = runpopstr(ctx->bifcxrun);
len2 = osrp2(p2) - 2;
p2 += 2;
/* look for p2 within p1 */
for (typ = DAT_NIL, outv = 1 ; len1 >= len2 ; ++p1, --len1, ++outv)
{
if (!memcmp(p1, p2, (size_t)len2))
{
typ = DAT_NUMBER; /* use number in outv after all */
break; /* that's it - we've found it */
}
}
break;
case DAT_LIST:
p1 = runpoplst(ctx->bifcxrun);
len1 = osrp2(p1) - 2;
p1 += 2;
/* get second item: any old datatype */
runpop(ctx->bifcxrun, &val);
for (typ = DAT_NIL, outv = 1 ; len1 ; ++outv, p1 += siz, len1 -= siz)
{
siz = datsiz(*p1, p1 + 1) + 1; /* get size of this element */
if (val.runstyp != *p1) continue; /* types don't match */
switch(val.runstyp)
{
case DAT_NUMBER:
if (val.runsv.runsvnum != osrp4s(p1 + 1)) continue;
break;
case DAT_SSTRING:
case DAT_LIST:
if (osrp2(p1 + 1) != osrp2(val.runsv.runsvstr) ||
memcmp(p1 + 3, val.runsv.runsvstr + 2,
(size_t)(osrp2(p1 + 1) - 2)))
continue;
break;
case DAT_PROPNUM:
if (osrp2(p1 + 1) != val.runsv.runsvprp) continue;
break;
case DAT_OBJECT:
case DAT_FNADDR:
if (osrp2(p1 + 1) != val.runsv.runsvobj) continue;
break;
default:
break;
}
/* if we got here, it means we found a match */
typ = DAT_NUMBER; /* use the value in outv */
break; /* that's it - we've found it */
}
break;
default:
runsig1(ctx->bifcxrun, ERR_INVTBIF, ERRTSTR, "find");
}
/* push the value given by typ and outv */
val.runsv.runsvnum = outv;
runpush(ctx->bifcxrun, typ, &val);
}
/* setit - set current 'it' */
void bifsit(bifcxdef *ctx, int argc)
{
objnum obj;
int typ;
voccxdef *vcx = ctx->bifcxrun->runcxvoc;
/* check for extended version that allows setting him/her */
if (argc == 2)
{
if (runtostyp(ctx->bifcxrun) == DAT_NIL)
{
rundisc(ctx->bifcxrun); /* discard the nil */
obj = MCMONINV; /* use invalid object */
}
else
obj = runpopobj(ctx->bifcxrun); /* get the object */
typ = runpopnum(ctx->bifcxrun); /* get the code */
vcx->voccxthc = 0; /* clear the 'them' list */
switch(typ)
{
case 0: /* set "it" */
vcx->voccxit = obj;
break;
case 1: /* set "him" */
vcx->voccxhim = obj;
break;
case 2: /* set "her" */
vcx->voccxher = obj;
break;
}
return;
}
/* "setit classic" has one argument only */
bifcntargs(ctx, 1, argc);
/* check to see if we're setting 'it' or 'them' */
if (runtostyp(ctx->bifcxrun) == DAT_LIST)
{
uchar *lst;
uint siz;
int cnt;
lst = runpoplst(ctx->bifcxrun);
siz = osrp2(lst);
lst += 2;
siz -= 2;
for (cnt = 0 ; siz ; )
{
/* if this is an object, add to 'them' list (otherwise ignore) */
if (*lst == DAT_OBJECT)
vcx->voccxthm[cnt++] = osrp2(lst+1);
lstadv(&lst, &siz);
}
vcx->voccxthc = cnt;
vcx->voccxit = MCMONINV;
}
else
{
/* set 'it', and delete 'them' list */
if (runtostyp(ctx->bifcxrun) == DAT_NIL)
{
vcx->voccxit = MCMONINV;
rundisc(ctx->bifcxrun);
}
else
vcx->voccxit = runpopobj(ctx->bifcxrun);
vcx->voccxthc = 0;
}
}
/* randomize - seed random number generator */
void bifsrn(bifcxdef *ctx, int argc)
{
bifcntargs(ctx, 0, argc);
os_rand(&ctx->bifcxrnd);
ctx->bifcxrndset = TRUE;
}
/* rand - get a random number */
void bifrnd(bifcxdef *ctx, int argc)
{
unsigned long result, max, randseed;
int tmp;
runsdef val;
/* get argument - number giving upper bound of generated number */
bifcntargs(ctx, 1, argc);
bifchkarg(ctx, DAT_NUMBER);
max = runpopnum(ctx->bifcxrun);
/* if the max is zero, just return zero */
if (max == 0)
{
runpnum(ctx->bifcxrun, 0);
return;
}
/*
* If the random number generator has been seeded by a call to
* randomize(), use the new, improved random number generator. If
* not, use the old random number generator to ensure that the same
* sequence of numbers is generated as always (to prevent breaking
* existing test scripts based on the old sequence).
*/
if (!ctx->bifcxrndset)
{
/* compute the next number in sequence, using old cheesy generator */
randseed = ctx->bifcxrnd;
randseed *= 1033;
randseed += 5;
tmp = randseed / 16384;
randseed %= 16384;
result = tmp / 7;
/* adjust the result to be in the requested range */
result = ( randseed % max ) + 1;
/* save the new seed value, and return the value */
ctx->bifcxrnd = randseed;
val.runsv.runsvnum = result;
runpush(ctx->bifcxrun, DAT_NUMBER, &val);
}
else
{
#define BIF_RAND_M ((ulong)2147483647)
#define BIF_RAND_Q ((ulong)127773)
#define BIF_RAND_A ((ulong)16807)
#define BIF_RAND_R ((ulong)2836)
long lo, hi, test;
lo = ctx->bifcxrnd / BIF_RAND_Q;
hi = ctx->bifcxrnd % BIF_RAND_Q;
test = BIF_RAND_A*lo - BIF_RAND_R*hi;
ctx->bifcxrnd = test;
if (test > 0)
ctx->bifcxrnd = test;
else
ctx->bifcxrnd = test + BIF_RAND_M;
runpnum(ctx->bifcxrun, (((ulong)ctx->bifcxrnd) % max) + 1);
}
}
/*
* case-insensitive substring matching
*/
static char *bif_stristr(const char *s1, const char *s2)
{
size_t s1len;
size_t s2len;
/* scan for a match */
for (s1len = strlen(s1), s2len = strlen(s2) ; s1len >= s2len ;
++s1, --s1len)
{
/* if this is a match, return this substring */
if (memicmp(s1, s2, s2len) == 0)
return (char *)s1;
}
return 0;
}
/*
* askfile flags
*/
#define BIF_ASKF_EXT_RET 1 /* extended return codes */
/*
* askfile
*/
void bifask(bifcxdef *ctx, int argc)
{
uchar *prompt;
char buf[OSFNMAX + 2];
char pbuf[128];
int err;
int prompt_type;
int file_type;
ulong flags;
/* make sure we have an acceptable number of arguments */
if (argc != 1 && argc != 3 && argc != 4)
runsig(ctx->bifcxrun, ERR_BIFARGC);
/* get the first argument - the prompt string */
prompt = runpopstr(ctx->bifcxrun);
bifcstr(ctx, pbuf, (size_t)sizeof(pbuf), prompt);
/* presume we will have no flags */
flags = 0;
/* if we have the prompt type and file type parameters, get them */
if (argc >= 3)
{
/* get the prompt-type and the file-type arguments */
prompt_type = (int)runpopnum(ctx->bifcxrun);
file_type = (int)runpopnum(ctx->bifcxrun);
/* if we have a fourth argument, it's the flags */
if (argc == 4)
flags = runpopnum(ctx->bifcxrun);
}
else
{
static const char *save_strs[] =
{
"save",
"write",
0
};
static const char *game_strs[] =
{
"restore",
"game",
0
};
const char **sp;
/*
* No prompt type or file type were specified. Try to infer the
* dialog type and file type from the text of the prompt. (This
* is mostly to support older games, in particular those based
* on older versions of adv.t, since newer games should always
* provide explicit values for the file type and dialog type.
* We are thus inferring the types based on the prompt strings
* that older adv.t's used when calling askfile.)
*
* If the prompt contains any substring such as "save" or
* "write", specify that we're saving; otherwise, assume that
* we're opening an existing file for reading.
*
* If the prompt contains the substrings "restore" AND "game",
* assume that we're opening a game file; otherwise, don't make
* any assumptions, and use the "unknown" file type.