Debian Codegraph @

terps/alan2/exe.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 exe.c
4
5 Amachine instruction execution unit of Alan interpreter
6
7 \*----------------------------------------------------------------------*/
8
9 #include "types.h"
10
11 #include "glk.h"
12
13 #ifdef GARGLK
14 #include "glkstart.h"
15 #endif
16
17 #ifdef USE_READLINE
18 #include "readline.h"
19 #endif
20
21 #include "main.h"
22 #include "parse.h"
23 #include "inter.h"
24 #include "stack.h"
25 #include "decode.h"
26
27 #include "exe.h"
28
29 #define WIDTH 80
30
31 #define N_EVTS 100
32
33
34 /* PUBLIC DATA */
35
36 /* The event queue */
37 EvtqElem eventq[N_EVTS]; /* Event queue */
38 int etop = 0; /* Event queue top pointer */
39
40 Boolean looking = FALSE; /* LOOKING? flag */
41
42 int dscrstkp = 0; /* Describe-stack pointer */
43
44
45 #ifdef _PROTOTYPES_
46 void dscrobjs(void);
47 void dscracts(void);
48 #else
49 void dscrobjs();
50 void dscracts();
51 #endif
52
53
54
55 #ifdef _PROTOTYPES_
56 void print(Aword fpos, Aword len)
57 #else
58 void print(fpos, len)
59 Aword fpos, len;
60 #endif
61 {
62 char str[2*WIDTH]; /* String buffer */
63 int outlen = 0; /* Current output length */
64 int ch;
65 int i;
66 long savfp; /* Temporary saved text file position */
67 static Boolean printFlag = FALSE; /* Printing already? */
68 Boolean savedPrintFlag = printFlag;
69 void *info; /* Saved decoding info */
70
71
72 if (len == 0) return;
73
74 if (isHere(HERO)) { /* Check if the player will see it */
75 if (printFlag) { /* Already printing? */
76 /* Save current text file position and/or decoding info */
77 if (header->pack)
78 info = pushDecode();
79 else
80 savfp = ftell(txtfil);
81 }
82 printFlag = TRUE; /* We're printing now! */
83 fseek(txtfil, fpos, 0); /* Position to start of text */
84 if (header->pack)
85 startDecoding();
86 for (outlen = 0; outlen != len; outlen = outlen + strlen(str)) {
87 /* Fill the buffer from the beginning */
88 for (i = 0; i <= WIDTH || (i > WIDTH && ch != ' '); i++) {
89 if (outlen + i == len) /* No more characters? */
90 break;
91 if (header->pack)
92 ch = decodeChar();
93 else
94 ch = getc(txtfil);
95 if (ch == EOFChar) /* Or end of text? */
96 break;
97 str[i] = ch;
98 }
99 str[i] = '\0';
100 #if ISO == 0
101 fromIso(str, str);
102 #endif
103 output(str);
104 }
105 /* And restore */
106 printFlag = savedPrintFlag;
107 if (printFlag) {
108 if (header->pack)
109 popDecode(info);
110 else
111 fseek(txtfil, savfp, 0);
112 }
113 }
114 }
115
116
117 #ifdef _PROTOTYPES_
118 void sys(Aword fpos, Aword len)
119 #else
120 void sys(fpos, len)
121 Aword fpos, len;
122 #endif
123 {
124 char *command;
125
126 getstr(fpos, len); /* Returns address to string on stack */
127 command = (char *)pop();
128 // Gargoyle will not allow games to run arbitrary programs.
129 #ifndef GARGLK
130 int tmp = system(command);
131 #endif
132 free(command);
133 }
134
135
136 #ifdef _PROTOTYPES_
137 void getstr(Aword fpos, Aword len)
138 #else
139 void getstr(fpos, len)
140 Aword fpos, len;
141 #endif
142 {
143 char *buf = allocate(len+1);
144
145 push((Aptr) buf); /* Push the address to the string */
146 fseek(txtfil, fpos, 0); /* Position to start of text */
147 if (header->pack)
148 startDecoding();
149 while (len--)
150 if (header->pack)
151 *(buf++) = decodeChar();
152 else
153 *(buf++) = getc(txtfil);
154 *buf = '\0';
155 }
156
157
158
159 #ifdef _PROTOTYPES_
160 void score(Aword sc)
161 #else
162 void score(sc)
163 Aword sc;
164 #endif
165 {
166 char buf[80];
167
168 if (sc == 0) {
169 prmsg(M_SCORE1);
170 sprintf(buf, "%d", cur.score);
171 output(buf);
172 prmsg(M_SCORE2);
173 sprintf(buf, "%ld.", (unsigned long) header->maxscore);
174 output(buf);
175 } else {
176 cur.score += scores[sc-1];
177 scores[sc-1] = 0;
178 }
179 }
180
181 #ifdef _PROTOTYPES_
182 void visits(Aword v)
183 #else
184 void visits(v)
185 Aword v;
186 #endif
187 {
188 cur.visits = v;
189 }
190
191
192 #ifdef _PROTOTYPES_
193 Boolean confirm(MsgKind msgno)
194 #else
195 Boolean confirm(msgno)
196 MsgKind msgno;
197 #endif
198 {
199 char buf[80];
200
201 /* This is a bit of a hack since we really want to compare the input,
202 it could be affirmative, but for now any input is NOT! */
203 prmsg(msgno);
204
205 #ifdef USE_READLINE
206 if (!readline(buf)) return TRUE;
207 #else
208 if (gets(buf) == NULL) return TRUE;
209 #endif
210 col = 1;
211
212 return (buf[0] == '\0');
213 }
214
215
216 #ifdef _PROTOTYPES_
217 void quit(void)
218 #else
219 void quit()
220 #endif
221 {
222 char buf[80];
223 char choices[10];
224
225 para();
226 while (TRUE) {
227 col = 1;
228 statusline();
229 prmsg(M_QUITACTION);
230 #ifdef USE_READLINE
231 if (!readline(buf)) terminate(0);
232 #else
233 if (gets(buf) == NULL) terminate(0);
234 #endif
235 if (strcmp(buf, "restart") == 0)
236 longjmp(restart_label, TRUE);
237 else if (strcmp(buf, "restore") == 0) {
238 restore();
239 return;
240 } else if (strcmp(buf, "quit") == 0)
241 terminate(0);
242 }
243 syserr("Fallthrough in QUIT");
244 }
245
246
247
248 #ifdef _PROTOTYPES_
249 void restart(void)
250 #else
251 void restart()
252 #endif
253 {
254 para();
255 if (confirm(M_REALLY)) {
256 longjmp(restart_label, TRUE);
257 } else
258 return;
259 syserr("Fallthrough in RESTART");
260 }
261
262
263
264 #ifdef _PROTOTYPES_
265 void cancl(Aword evt)
266 #else
267 void cancl(evt)
268 Aword evt;
269 #endif
270 {
271 int i;
272
273 for(i = etop-1; i>=0; i--)
274 if (eventq[i].event == evt) {
275 while (i < etop-1) {
276 eventq[i].event = eventq[i+1].event;
277 eventq[i].time = eventq[i+1].time;
278 eventq[i].where = eventq[i+1].where;
279 i++;
280 }
281 etop--;
282 return;
283 }
284 }
285
286
287 #ifdef _PROTOTYPES_
288 void schedule(Aword evt, Aword whr, Aword aft)
289 #else
290 void schedule(evt, whr, aft)
291 Aword evt, whr, aft;
292 #endif
293 { int i;
294 int time;
295
296 cancl(evt);
297 /* Check for overflow */
298 if (etop == N_EVTS) syserr("Out of event space.");
299
300 time = cur.tick+aft;
301
302 /* Bubble this event down */
303 for (i = etop; i >= 1 && eventq[i-1].time <= time; i--) {
304 eventq[i].event = eventq[i-1].event;
305 eventq[i].time = eventq[i-1].time;
306 eventq[i].where = eventq[i-1].where;
307 }
308
309 eventq[i].time = time;
310 eventq[i].where = whr;
311 eventq[i].event = evt;
312 etop++;
313 }
314
315
316 /*----------------------------------------------------------------------
317
318 getatr()
319
320 Get an attribute value from an attribute list
321
322 */
323 #ifdef _PROTOTYPES_
324 static Aptr getatr(
325 Aaddr atradr, /* IN - ACODE address to attribute table */
326 Aaddr atr /* IN - The attribute to read */
327 )
328 #else
329 static Aptr getatr(atradr, atr)
330 Aaddr atradr; /* IN - ACODE address to attribute table */
331 Aaddr atr; /* IN - The attribute to read */
332 #endif
333 {
334 AtrElem *at;
335
336 at = (AtrElem *) addrTo(atradr);
337 return at[atr-1].val;
338 }
339
340
341 /*----------------------------------------------------------------------
342
343 setatr()
344
345 Set a particular attribute to a value.
346
347 */
348 #ifdef _PROTOTYPES_
349 static void setatr(
350 Aaddr atradr, /* IN - ACODE address to attribute table */
351 Aword atr, /* IN - attribute code */
352 Aword val /* IN - new value */
353 )
354 #else
355 static void setatr(atradr, atr, val)
356 Aaddr atradr; /* IN - ACODE address to attribute table */
357 Aword atr; /* IN - attribute code */
358 Aword val; /* IN - new value */
359 #endif
360 {
361 AtrElem *at;
362
363 at = (AtrElem *) addrTo(atradr);
364 at[atr-1].val = val;
365 }
366
367
368 /*----------------------------------------------------------------------
369
370 make()
371
372 */
373
374 #ifdef _PROTOTYPES_
375 static void makloc(Aword loc, Aword atr, Aword val)
376 #else
377 static void makloc(loc, atr, val)
378 Aword loc, atr, val;
379 #endif
380 {
381 setatr(locs[loc-LOCMIN].atrs, atr, val);
382 }
383
384 #ifdef _PROTOTYPES_
385 static void makobj(Aword obj, Aword atr, Aword val)
386 #else
387 static void makobj(obj, atr, val)
388 Aword obj, atr, val;
389 #endif
390 {
391 setatr(objs[obj-OBJMIN].atrs, atr, val);
392 }
393
394 #ifdef _PROTOTYPES_
395 static void makact(Aword act, Aword atr, Aword val)
396 #else
397 static void makact(act, atr, val)
398 Aword act, atr, val;
399 #endif
400 {
401 setatr(acts[act-ACTMIN].atrs, atr, val);
402 }
403
404
405 #ifdef _PROTOTYPES_
406 void make(Aword id, Aword atr, Aword val)
407 #else
408 void make(id, atr, val)
409 Aword id, atr, val;
410 #endif
411 {
412 char str[80];
413
414 if (isObj(id))
415 makobj(id, atr, val);
416 else if (isLoc(id))
417 makloc(id, atr, val);
418 else if (isAct(id))
419 makact(id, atr, val);
420 else {
421 sprintf(str, "Can't MAKE item (%ld).", (unsigned long) id);
422 syserr(str);
423 }
424 }
425
426
427 /*----------------------------------------------------------------------------
428
429 set()
430
431 */
432
433 #ifdef _PROTOTYPES_
434 static void setloc(Aword loc, Aword atr, Aword val)
435 #else
436 static void setloc(loc, atr, val)
437 Aword loc, atr, val;
438 #endif
439 {
440 setatr(locs[loc-LOCMIN].atrs, atr, val);
441 locs[loc-LOCMIN].describe = 0;
442 }
443
444
445 #ifdef _PROTOTYPES_
446 static void setobj(Aword obj, Aword atr, Aword val)
447 #else
448 static void setobj(obj, atr, val)
449 Aword obj, atr, val;
450 #endif
451 {
452 setatr(objs[obj-OBJMIN].atrs, atr, val);
453 }
454
455 #ifdef _PROTOTYPES_
456 static void setact(Aword act, Aword atr, Aword val)
457 #else
458 static void setact(act, atr, val)
459 Aword act, atr, val;
460 #endif
461 {
462 setatr(acts[act-ACTMIN].atrs, atr, val);
463 }
464
465
466 #ifdef _PROTOTYPES_
467 void set(Aword id, Aword atr, Aword val)
468 #else
469 void set(id, atr, val)
470 Aword id, atr, val;
471 #endif
472 {
473 char str[80];
474
475 if (isObj(id))
476 setobj(id, atr, val);
477 else if (isLoc(id))
478 setloc(id, atr, val);
479 else if (isAct(id))
480 setact(id, atr, val);
481 else {
482 sprintf(str, "Can't SET item (%ld).", (unsigned long) id);
483 syserr(str);
484 }
485 }
486
487
488 #ifdef _PROTOTYPES_
489 void setstr(Aword id, Aword atr, Aword str)
490 #else
491 void setstr(id, atr, str)
492 Aword id, atr, str;
493 #endif
494 {
495 free((char *)attribute(id, atr));
496 set(id, atr, str);
497 }
498
499
500
501 /*-----------------------------------------------------------------------------
502
503 incr/decr
504
505 */
506
507 /*----------------------------------------------------------------------
508
509 incratr()
510
511 Increment a particular attribute by a value.
512
513 */
514 #ifdef _PROTOTYPES_
515 static void incratr(
516 Aaddr atradr, /* IN - ACODE address to attribute table */
517 Aword atr, /* IN - attribute code */
518 Aword step /* IN - step to increment by */
519 )
520 #else
521 static void incratr(atradr, atr, step)
522 Aaddr atradr, atr, step;
523 #endif
524 {
525 AtrElem *at;
526
527 at = (AtrElem *) addrTo(atradr);
528 at[atr-1].val += step;
529 }
530
531
532 #ifdef _PROTOTYPES_
533 static void incrloc(Aword loc, Aword atr, Aword step)
534 #else
535 static void incrloc(loc, atr, step)
536 Aword loc, atr, step;
537 #endif
538 {
539 incratr(locs[loc-LOCMIN].atrs, atr, step);
540 locs[loc-LOCMIN].describe = 0;
541 }
542
543
544 #ifdef _PROTOTYPES_
545 static void incrobj(Aword obj, Aword atr, Aword step)
546 #else
547 static void incrobj(obj, atr, step)
548 Aword obj, atr, step;
549 #endif
550 {
551 incratr(objs[obj-OBJMIN].atrs, atr, step);
552 }
553
554 #ifdef _PROTOTYPES_
555 static void incract(Aword act, Aword atr, Aword step)
556 #else
557 static void incract(act, atr, step)
558 Aword act, atr, step;
559 #endif
560 {
561 incratr(acts[act-ACTMIN].atrs, atr, step);
562 }
563
564
565 #ifdef _PROTOTYPES_
566 void incr(Aword id, Aword atr, Aword step)
567 #else
568 void incr(id, atr, step)
569 Aword id, atr, step;
570 #endif
571 {
572 char str[80];
573
574 if (isObj(id))
575 incrobj(id, atr, step);
576 else if (isLoc(id))
577 incrloc(id, atr, step);
578 else if (isAct(id))
579 incract(id, atr, step);
580 else {
581 sprintf(str, "Can't INCR item (%ld).", (unsigned long) id);
582 syserr(str);
583 }
584 }
585
586 #ifdef _PROTOTYPES_
587 void decr(Aword id, Aword atr, Aword step)
588 #else
589 void decr(id, atr, step)
590 Aword id, atr, step;
591 #endif
592 {
593 char str[80];
594
595 if (isObj(id))
596 incrobj(id, atr, -step);
597 else if (isLoc(id))
598 incrloc(id, atr, -step);
599 else if (isAct(id))
600 incract(id, atr, -step);
601 else {
602 sprintf(str, "Can't DECR item (%ld).", (unsigned long) id);
603 syserr(str);
604 }
605 }
606
607
608
609 /*----------------------------------------------------------------------
610
611 attribute()
612
613 */
614
615 #ifdef _PROTOTYPES_
616 static Aptr locatr(Aword loc, Aword atr)
617 #else
618 static Aptr locatr(loc, atr)
619 Aword loc, atr;
620 #endif
621 {
622 return getatr(locs[loc-LOCMIN].atrs, atr);
623 }
624
625
626
627 #ifdef _PROTOTYPES_
628 static Aptr objatr(Aword obj, Aword atr)
629 #else
630 static Aptr objatr(obj, atr)
631 Aword obj, atr;
632 #endif
633 {
634 return getatr(objs[obj-OBJMIN].atrs, atr);
635 }
636
637 #ifdef _PROTOTYPES_
638 static Aptr actatr(Aword act, Aword atr)
639 #else
640 static Aptr actatr(act, atr)
641 Aword act, atr;
642 #endif
643 {
644 return getatr(acts[act-ACTMIN].atrs, atr);
645 }
646
647 #ifdef _PROTOTYPES_
648 static Aptr litatr(Aword lit, Aword atr)
649 #else
650 static Aptr litatr(lit, atr)
651 Aword lit, atr;
652 #endif
653 {
654 char str[80];
655
656 if (atr == 1)
657 return litValues[lit-LITMIN].value;
658 else {
659 sprintf(str, "Unknown attribute for literal (%ld).", (unsigned long) atr);
660 syserr(str);
661 }
662 return(EOF);
663 }
664
665
666 #ifdef _PROTOTYPES_
667 Aptr attribute(Aword id, Aword atr)
668 #else
669 Aptr attribute(id, atr)
670 Aword id, atr;
671 #endif
672 {
673 char str[80];
674
675 if (isObj(id))
676 return objatr(id, atr);
677 else if (isLoc(id))
678 return locatr(id, atr);
679 else if (isAct(id))
680 return actatr(id, atr);
681 else if (isLit(id))
682 return litatr(id, atr);
683 else {
684 sprintf(str, "Can't ATTRIBUTE item (%ld).", (unsigned long) id);
685 syserr(str);
686 }
687 return(EOF);
688 }
689
690
691 #ifdef _PROTOTYPES_
692 Aptr strattr(Aword id, Aword atr)
693 #else
694 Aptr strattr(id, atr)
695 Aword id, atr;
696 #endif
697 {
698 return (Aptr) strdup((char *)attribute(id, atr));
699 }
700
701
702 /*----------------------------------------------------------------------
703
704 where()
705
706 */
707
708 #ifdef _PROTOTYPES_
709 static Aword objloc(Aword obj)
710 #else
711 static Aword objloc(obj)
712 Aword obj;
713 #endif
714 {
715 if (isCnt(objs[obj-OBJMIN].loc)) /* In something ? */
716 if (isObj(objs[obj-OBJMIN].loc) || isAct(objs[obj-OBJMIN].loc))
717 return(where(objs[obj-OBJMIN].loc));
718 else /* Containers not anywhere is where the hero is! */
719 return(where(HERO));
720 else
721 return(objs[obj-OBJMIN].loc);
722 }
723
724
725 #ifdef _PROTOTYPES_
726 static Aword actloc(Aword act)
727 #else
728 static Aword actloc(act)
729 Aword act;
730 #endif
731 {
732 return(acts[act-ACTMIN].loc);
733 }
734
735
736 #ifdef _PROTOTYPES_
737 Aword where(Aword id)
738 #else
739 Aword where(id)
740 Aword id;
741 #endif
742 {
743 char str[80];
744
745 if (isObj(id))
746 return objloc(id);
747 else if (isAct(id))
748 return actloc(id);
749 else {
750 sprintf(str, "Can't WHERE item (%ld).", (unsigned long) id);
751 syserr(str);
752 }
753 return(EOF);
754 }
755
756
757
758 /*----------------------------------------------------------------------
759
760 aggregates
761
762 */
763
764 #ifdef _PROTOTYPES_
765 Aint agrmax(Aword atr, Aword whr)
766 #else
767 Aint agrmax(atr, whr)
768 Aword atr, whr;
769 #endif
770 {
771 Aword i;
772 Aint max = 0;
773
774 for (i = OBJMIN; i <= OBJMAX; i++) {
775 if (isLoc(whr)) {
776 if (where(i) == whr && attribute(i, atr) > max)
777 max = attribute(i, atr);
778 } else if (objs[i-OBJMIN].loc == whr && attribute(i, atr) > max)
779 max = attribute(i, atr);
780 }
781 return(max);
782 }
783
784 #ifdef _PROTOTYPES_
785 Aint agrsum(Aword atr, Aword whr)
786 #else
787 Aint agrsum(atr, whr)
788 Aword atr, whr;
789 #endif
790 {
791 Aword i;
792 Aint sum = 0;
793
794 for (i = OBJMIN; i <= OBJMAX; i++) {
795 if (isLoc(whr)) {
796 if (where(i) == whr)
797 sum += attribute(i, atr);
798 } else if (objs[i-OBJMIN].loc == whr)
799 sum += attribute(i, atr);
800 }
801 return(sum);
802 }
803
804
805 #ifdef _PROTOTYPES_
806 Aint agrcount(Aword whr)
807 #else
808 Aint agrcount(whr)
809 Aword whr;
810 #endif
811 {
812 Aword i;
813 Aword count = 0;
814
815 for (i = OBJMIN; i <= OBJMAX; i++) {
816 if (isLoc(whr)) {
817 if (where(i) == whr)
818 count++;
819 } else if (objs[i-OBJMIN].loc == whr)
820 count++;
821 }
822 return(count);
823 }
824
825
826 /*----------------------------------------------------------------------
827
828 locate()
829
830 */
831
832 #ifdef _PROTOTYPES_
833 static void locobj(Aword obj, Aword whr)
834 #else
835 static void locobj(obj, whr)
836 Aword obj, whr;
837 #endif
838 {
839 if (isCnt(whr)) { /* Into a container */
840 if (whr == obj)
841 syserr("Locating something inside itself.");
842 if (checklim(whr, obj))
843 return;
844 else
845 objs[obj-OBJMIN].loc = whr;
846 } else {
847 objs[obj-OBJMIN].loc = whr;
848 /* Make sure the location is described since it's changed */
849 locs[whr-LOCMIN].describe = 0;
850 }
851 }
852
853
854 #ifdef _PROTOTYPES_
855 static void locact(Aword act, Aword whr)
856 #else
857 static void locact(act, whr)
858 Aword act, whr;
859 #endif
860 {
861 Aword prevact = cur.act;
862 Aword prevloc = cur.loc;
863
864 cur.loc = whr;
865 acts[act-ACTMIN].loc = whr;
866 if (act == HERO) {
867 if (locs[acts[act-ACTMIN].loc-LOCMIN].describe % (cur.visits+1) == 0)
868 look();
869 else {
870 if (anyOutput)
871 para();
872 say(where(HERO));
873 prmsg(M_AGAIN);
874 newline();
875 dscrobjs();
876 dscracts();
877 }
878 locs[where(HERO)-LOCMIN].describe++;
879 locs[where(HERO)-LOCMIN].describe %= (cur.visits+1);
880 } else
881 locs[whr-LOCMIN].describe = 0;
882 if (locs[cur.loc-LOCMIN].does != 0) {
883 cur.act = act;
884 interpret(locs[cur.loc-LOCMIN].does);
885 cur.act = prevact;
886 }
887
888 if (cur.act != act)
889 cur.loc = prevloc;
890 }
891
892
893 #ifdef _PROTOTYPES_
894 void locate(Aword id, Aword whr)
895 #else
896 void locate(id, whr)
897 Aword id, whr;
898 #endif
899 {
900 char str[80];
901
902 if (isObj(id))
903 locobj(id, whr);
904 else if (isAct(id))
905 locact(id, whr);
906 else {
907 sprintf(str, "Can't LOCATE item (%ld).", (unsigned long) id);
908 syserr(str);
909 }
910 }
911
912
913 /*----------------------------------------------------------------------
914
915 isHere()
916
917 */
918
919 #ifdef _PROTOTYPES_
920 static Abool objhere(Aword obj)
921 #else
922 static Abool objhere(obj)
923 Aword obj;
924 #endif
925 {
926 if (isCnt(objs[obj-OBJMIN].loc)) { /* In something? */
927 if (isObj(objs[obj-OBJMIN].loc) || isAct(objs[obj-OBJMIN].loc))
928 return(isHere(objs[obj-OBJMIN].loc));
929 else /* If the container wasn't anywhere, assume where HERO is! */
930 return(where(HERO) == cur.loc);
931 } else
932 return(objs[obj-OBJMIN].loc == cur.loc);
933 }
934
935
936 #ifdef _PROTOTYPES_
937 static Aword acthere(Aword act)
938 #else
939 static Aword acthere(act)
940 Aword act;
941 #endif
942 {
943 return(acts[act-ACTMIN].loc == cur.loc);
944 }
945
946
947 #ifdef _PROTOTYPES_
948 Abool isHere(Aword id)
949 #else
950 Abool isHere(id)
951 Aword id;
952 #endif
953 {
954 char str[80];
955
956 if (isObj(id))
957 return objhere(id);
958 else if (isAct(id))
959 return acthere(id);
960 else {
961 sprintf(str, "Can't HERE item (%ld).", (unsigned long) id);
962 syserr(str);
963 }
964 return(EOF);
965 }
966
967 /*----------------------------------------------------------------------
968
969 isNear()
970
971 */
972
973 #ifdef _PROTOTYPES_
974 static Aword objnear(Aword obj)
975 #else
976 static Aword objnear(obj)
977 Aword obj;
978 #endif
979 {
980 if (isCnt(objs[obj-OBJMIN].loc)) { /* In something? */
981 if (isObj(objs[obj-OBJMIN].loc) || isAct(objs[obj-OBJMIN].loc))
982 return(isNear(objs[obj-OBJMIN].loc));
983 else /* If the container wasn't anywhere, assume here, so not nearby! */
984 return(FALSE);
985 } else
986 return(exitto(where(obj), cur.loc));
987 }
988
989
990 #ifdef _PROTOTYPES_
991 static Aword actnear(Aword act)
992 #else
993 static Aword actnear(act)
994 Aword act;
995 #endif
996 {
997 return(exitto(where(act), cur.loc));
998 }
999
1000
1001 #ifdef _PROTOTYPES_
1002 Abool isNear(Aword id)
1003 #else
1004 Abool isNear(id)
1005 Aword id;
1006 #endif
1007 {
1008 char str[80];
1009
1010 if (isObj(id))
1011 return objnear(id);
1012 else if (isAct(id))
1013 return actnear(id);
1014 else {
1015 sprintf(str, "Can't NEAR item (%ld).", (unsigned long) id);
1016 syserr(str);
1017 }
1018 return(EOF);
1019 }
1020
1021
1022
1023 /*----------------------------------------------------------------------
1024
1025 in()
1026
1027 */
1028
1029 #ifdef _PROTOTYPES_
1030 Abool in(Aword obj, Aword cnt)
1031 #else
1032 Abool in(obj, cnt)
1033 Aword obj;
1034 Aword cnt;
1035 #endif
1036 {
1037 if (!isObj(obj))
1038 return(FALSE);
1039 if (!isCnt(cnt))
1040 syserr("IN in a non-container.");
1041
1042 return(objs[obj-OBJMIN].loc == cnt);
1043 }
1044
1045
1046 /*----------------------------------------------------------------------
1047
1048 say()
1049
1050 */
1051
1052 #ifdef _PROTOTYPES_
1053 static void sayloc(Aword loc)
1054 #else
1055 static void sayloc(loc)
1056 Aword loc;
1057 #endif
1058 {
1059 interpret(locs[loc-LOCMIN].nams);
1060 }
1061
1062
1063 #ifdef _PROTOTYPES_
1064 static void sayobj(Aword obj)
1065 #else
1066 static void sayobj(obj)
1067 Aword obj;
1068 #endif
1069 {
1070 interpret(objs[obj-OBJMIN].dscr2);
1071 }
1072
1073 #ifdef _PROTOTYPES_
1074 static void sayact(Aword act)
1075 #else
1076 static void sayact(act)
1077 Aword act;
1078 #endif
1079 {
1080 interpret(acts[act-ACTMIN].nam);
1081 }
1082
1083
1084 #ifdef _PROTOTYPES_
1085 void sayint(Aword val)
1086 #else
1087 void sayint(val)
1088 Aword val;
1089 #endif
1090 {
1091 char buf[25];
1092
1093 if (isHere(HERO)) {
1094 sprintf(buf, "%ld", (unsigned long) val);
1095 output(buf);
1096 }
1097 }
1098
1099
1100 #ifdef _PROTOTYPES_
1101 void saystr(char *str)
1102 #else
1103 void saystr(str)
1104 char *str;
1105 #endif
1106 {
1107 if (isHere(HERO))
1108 output(str);
1109 free(str);
1110 }
1111
1112
1113 #ifdef _PROTOTYPES_
1114 static void saylit(Aword lit)
1115 #else
1116 static void saylit(lit)
1117 Aword lit;
1118 #endif
1119 {
1120 char *str;
1121
1122 if (isNum(lit))
1123 sayint(litValues[lit-LITMIN].value);
1124 else {
1125 str = (char *)strdup((char *)litValues[lit-LITMIN].value);
1126 saystr(str);
1127 }
1128 }
1129
1130
1131 #ifdef _PROTOTYPES_
1132 void sayarticle(Aword id)
1133 #else
1134 void sayarticle(id)
1135 Aword id;
1136 #endif
1137 {
1138 if (!isObj(id))
1139 syserr("Trying to say article of something *not* an object.");
1140 if (objs[id-OBJMIN].art != 0)
1141 interpret(objs[id-OBJMIN].art);
1142 else
1143 prmsg(M_ARTICLE);
1144 }
1145
1146
1147 #ifdef _PROTOTYPES_
1148 void say(Aword id)
1149 #else
1150 void say(id)
1151 Aword id;
1152 #endif
1153 {
1154 char str[80];
1155
1156 if (isHere(HERO)) {
1157 if (isObj(id))
1158 sayobj(id);
1159 else if (isLoc(id))
1160 sayloc(id);
1161 else if (isAct(id))
1162 sayact(id);
1163 else if (isLit(id))
1164 saylit(id);
1165 else {
1166 sprintf(str, "Can't SAY item (%ld).", (unsigned long) id);
1167 syserr(str);
1168 }
1169 }
1170 }
1171
1172
1173 /*----------------------------------------------------------------------
1174
1175 describe()
1176
1177 */
1178
1179 #ifdef _PROTOTYPES_
1180 static void dscrloc(Aword loc)
1181 #else
1182 static void dscrloc(loc)
1183 Aword loc;
1184 #endif
1185 {
1186 if (locs[loc-LOCMIN].dscr != 0)
1187 interpret(locs[loc-LOCMIN].dscr);
1188 }
1189
1190
1191 #ifdef _PROTOTYPES_
1192 static void dscrobj(Aword obj)
1193 #else
1194 static void dscrobj(obj)
1195 Aword obj;
1196 #endif
1197 {
1198 objs[obj-OBJMIN].describe = FALSE;
1199 if (objs[obj-OBJMIN].dscr1 != 0)
1200 interpret(objs[obj-OBJMIN].dscr1);
1201 else {
1202 prmsg(M_SEEOBJ1);
1203 sayarticle(obj);
1204 say(obj);
1205 prmsg(M_SEEOBJ4);
1206 if (objs[obj-OBJMIN].cont != 0)
1207 list(obj);
1208 }
1209 }
1210
1211
1212 #ifdef _PROTOTYPES_
1213 static void dscract(Aword act)
1214 #else
1215 static void dscract(act)
1216 Aword act;
1217 #endif
1218 {
1219 ScrElem *scr = NULL;
1220
1221 if (acts[act-ACTMIN].script != 0) {
1222 for (scr = (ScrElem *) addrTo(acts[act-ACTMIN].scradr); !endOfTable(scr); scr++)
1223 if (scr->code == acts[act-ACTMIN].script)
1224 break;
1225 if (endOfTable(scr)) scr = NULL;
1226 }
1227 if (scr != NULL && scr->dscr != 0)
1228 interpret(scr->dscr);
1229 else if (acts[act-ACTMIN].dscr != 0)
1230 interpret(acts[act-ACTMIN].dscr);
1231 else {
1232 interpret(acts[act-ACTMIN].nam);
1233 prmsg(M_SEEACT);
1234 }
1235 acts[act-ACTMIN].describe = FALSE;
1236 }
1237
1238
1239 static Aword dscrstk[255];
1240
1241 #ifdef _PROTOTYPES_
1242 void describe(Aword id)
1243 #else
1244 void describe(id)
1245 Aword id;
1246 #endif
1247 {
1248 int i;
1249 char str[80];
1250
1251 for (i = 0; i < dscrstkp; i++)
1252 if (dscrstk[i] == id)
1253 syserr("Recursive DESCRIBE.");
1254 dscrstk[dscrstkp++] = id;
1255
1256 if (isObj(id))
1257 dscrobj(id);
1258 else if (isLoc(id))
1259 dscrloc(id);
1260 else if (isAct(id))
1261 dscract(id);
1262 else {
1263 sprintf(str, "Can't DESCRIBE item (%ld).", (unsigned long) id);
1264 syserr(str);
1265 }
1266
1267 dscrstkp--;
1268 }
1269
1270
1271 /*----------------------------------------------------------------------
1272
1273 use()
1274
1275 */
1276
1277 #ifdef _PROTOTYPES_
1278 void use(Aword act, Aword scr)
1279 #else
1280 void use(act, scr)
1281 Aword act, scr;
1282 #endif
1283 {
1284 char str[80];
1285
1286 if (!isAct(act)) {
1287 sprintf(str, "Item is not an Actor (%ld).", (unsigned long) act);
1288 syserr(str);
1289 }
1290
1291 acts[act-ACTMIN].script = scr;
1292 acts[act-ACTMIN].step = 0;
1293 }
1294
1295
1296
1297 /*----------------------------------------------------------------------
1298
1299 list()
1300
1301 */
1302
1303 #ifdef _PROTOTYPES_
1304 void list(Aword cnt)
1305 #else
1306 void list(cnt)
1307 Aword cnt;
1308 #endif
1309 {
1310 int i;
1311 Aword props;
1312 Aword prevobj;
1313 Boolean found = FALSE;
1314 Boolean multiple = FALSE;
1315
1316 /* Find container properties */
1317 if (isObj(cnt))
1318 props = objs[cnt-OBJMIN].cont;
1319 else if (isAct(cnt))
1320 props = acts[cnt-ACTMIN].cont;
1321 else
1322 props = cnt;
1323
1324 for (i = OBJMIN; i <= OBJMAX; i++) {
1325 if (in(i, cnt)) { /* Yes, it's in this container */
1326 if (!found) {
1327 found = TRUE;
1328 if (cnts[props-CNTMIN].header != 0)
1329 interpret(cnts[props-CNTMIN].header);
1330 else {
1331 prmsg(M_CONTAINS1);
1332 if (cnts[props-CNTMIN].nam != 0) /* It has it's own name */
1333 interpret(cnts[props-CNTMIN].nam);
1334 else
1335 say(cnts[props-CNTMIN].parent); /* It is actually an object or actor */
1336 prmsg(M_CONTAINS2);
1337 }
1338 } else {
1339 if (multiple) {
1340 needsp = FALSE;
1341 prmsg(M_CONTAINS3);
1342 }
1343 multiple = TRUE;
1344 sayarticle(prevobj);
1345 say(prevobj);
1346 }
1347 prevobj = i;
1348 }
1349 }
1350
1351 if (found) {
1352 if (multiple)
1353 prmsg(M_CONTAINS4);
1354 sayarticle(prevobj);
1355 say(prevobj);
1356 prmsg(M_CONTAINS5);
1357 } else {
1358 if (cnts[props-CNTMIN].empty != 0)
1359 interpret(cnts[props-CNTMIN].empty);
1360 else {
1361 prmsg(M_EMPTY1);
1362 if (cnts[props-CNTMIN].nam != 0) /* It has it's own name */
1363 interpret(cnts[props-CNTMIN].nam);
1364 else
1365 say(cnts[props-CNTMIN].parent); /* It is actually an actor or object */
1366 prmsg(M_EMPTY2);
1367 }
1368 }
1369 needsp = TRUE;
1370 }
1371
1372
1373 /*----------------------------------------------------------------------
1374
1375 empty()
1376
1377 */
1378
1379 #ifdef _PROTOTYPES_
1380 void empty(Aword cnt, Aword whr)
1381 #else
1382 void empty(cnt, whr)
1383 Aword cnt;
1384 Aword whr;
1385 #endif
1386 {
1387 int i;
1388
1389 for (i = OBJMIN; i <= OBJMAX; i++)
1390 if (in(i, cnt))
1391 locate(i, whr);
1392 }
1393
1394
1395
1396 /*----------------------------------------------------------------------*\
1397
1398 Description of current location
1399
1400 dscrobjs()
1401 dscracts()
1402 look()
1403
1404 \*----------------------------------------------------------------------*/
1405
1406 #ifdef _PROTOTYPES_
1407 void dscrobjs(void)
1408 #else
1409 void dscrobjs()
1410 #endif
1411 {
1412 int i;
1413 int prevobj;
1414 Boolean found = FALSE;
1415 Boolean multiple = FALSE;
1416
1417 /* First describe everything here with its own description */
1418 for (i = OBJMIN; i <= OBJMAX; i++)
1419 if (objs[i-OBJMIN].loc == cur.loc &&
1420 objs[i-OBJMIN].describe &&
1421 objs[i-OBJMIN].dscr1)
1422 describe(i);
1423
1424 /* Then list everything else here */
1425 for (i = OBJMIN; i <= OBJMAX; i++)
1426 if (objs[i-OBJMIN].loc == cur.loc &&
1427 objs[i-OBJMIN].describe) {
1428 if (!found) {
1429 prmsg(M_SEEOBJ1);
1430 sayarticle(i);
1431 say(i);
1432 found = TRUE;
1433 } else {
1434 if (multiple) {
1435 needsp = FALSE;
1436 prmsg(M_SEEOBJ2);
1437 sayarticle(prevobj);
1438 say(prevobj);
1439 }
1440 multiple = TRUE;
1441 }
1442 prevobj = i;
1443 }
1444
1445 if (found) {
1446 if (multiple) {
1447 prmsg(M_SEEOBJ3);
1448 sayarticle(prevobj);
1449 say(prevobj);
1450 }
1451 prmsg(M_SEEOBJ4);
1452 }
1453
1454 /* Set describe flag for all objects */
1455 for (i = OBJMIN; i <= OBJMAX; i++)
1456 objs[i-OBJMIN].describe = TRUE;
1457 }
1458
1459
1460 #ifdef _PROTOTYPES_
1461 void dscracts(void)
1462 #else
1463 void dscracts()
1464 #endif
1465 {
1466 int i;
1467
1468 for (i = HERO+1; i <= ACTMAX; i++)
1469 if (acts[i-ACTMIN].loc == cur.loc &&
1470 acts[i-ACTMIN].describe)
1471 describe(i);
1472
1473 /* Set describe flag for all actors */
1474 for (i = HERO; i <= ACTMAX; i++)
1475 acts[i-ACTMIN].describe = TRUE;
1476 }
1477
1478
1479 #ifdef _PROTOTYPES_
1480 void look(void)
1481 #else
1482 void look()
1483 #endif
1484 {
1485 int i;
1486
1487 if (looking)
1488 syserr("Recursive LOOK.");
1489
1490 looking = TRUE;
1491 /* Set describe flag for all objects and actors */
1492 for (i = OBJMIN; i <= OBJMAX; i++)
1493 objs[i-OBJMIN].describe = TRUE;
1494 for (i = ACTMIN; i <= ACTMAX; i++)
1495 acts[i-ACTMIN].describe = TRUE;
1496
1497 if (anyOutput)
1498 para();
1499
1500 glk_set_style(style_Subheader);
1501 needsp = FALSE;
1502 say(cur.loc);
1503 needsp = FALSE;
1504 output(".");
1505 glk_set_style(style_Normal);
1506 newline();
1507 needsp = FALSE;
1508 describe(cur.loc);
1509 dscrobjs();
1510 dscracts();
1511 looking = FALSE;
1512 }
1513
1514
1515
1516 static char savfnm[256];
1517
1518
1519 /*----------------------------------------------------------------------
1520
1521 save()
1522
1523 */
1524
1525 #ifdef _PROTOTYPES_
1526 void save(void)
1527 #else
1528 void save()
1529 #endif
1530 {
1531 int i;
1532 FILE *savfil;
1533 char str[256];
1534 AtrElem *atr;
1535
1536 #ifdef GLKUNIX_FILEREF_GET_FILENAME
1537
1538 frefid_t fref;
1539 fref = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Write, 0);
1540 if (fref == NULL)
1541 error(M_SAVEFAILED);
1542 strcpy(str, glkunix_fileref_get_filename(fref));
1543 glk_fileref_destroy(fref);
1544
1545 #else
1546
1547 /* First save ? */
1548 if (savfnm[0] == '\0') {
1549 strcpy(savfnm, advnam);
1550 strcat(savfnm, ".sav");
1551 }
1552 prmsg(M_SAVEWHERE);
1553 sprintf(str, "(%s) : ", savfnm);
1554 output(str);
1555
1556 #ifdef USE_READLINE
1557 readline(str);
1558 #else
1559 gets(str);
1560 #endif
1561
1562 #endif
1563
1564 if (str[0] == '\0')
1565 strcpy(str, savfnm);
1566 col = 1;
1567 if ((savfil = fopen(str, READ_MODE)) != NULL)
1568 /* It already existed */
1569 if (!confirm(M_SAVEOVERWRITE))
1570 error(MSGMAX); /* Return to player without saying anything */
1571 if ((savfil = fopen(str, WRITE_MODE)) == NULL)
1572 error(M_SAVEFAILED);
1573 strcpy(savfnm, str);
1574
1575 /* Save version of interpreter and name of game */
1576 fwrite((void *)&header->vers, sizeof(Aword), 1, savfil);
1577 fwrite((void *)advnam, strlen(advnam)+1, 1, savfil);
1578 /* Save current values */
1579 fwrite((void *)&cur, sizeof(cur), 1, savfil);
1580 /* Save actors */
1581 for (i = ACTMIN; i <= ACTMAX; i++) {
1582 fwrite((void *)&acts[i-ACTMIN].loc, sizeof(Aword), 1, savfil);
1583 fwrite((void *)&acts[i-ACTMIN].script, sizeof(Aword), 1, savfil);
1584 fwrite((void *)&acts[i-ACTMIN].step, sizeof(Aword), 1, savfil);
1585 fwrite((void *)&acts[i-ACTMIN].count, sizeof(Aword), 1, savfil);
1586 if (acts[i-ACTMIN].atrs)
1587 for (atr = (AtrElem *) addrTo(acts[i-ACTMIN].atrs); !endOfTable(atr); atr++)
1588 fwrite((void *)&atr->val, sizeof(Aword), 1, savfil);
1589 }
1590
1591 /* Save locations */
1592 for (i = LOCMIN; i <= LOCMAX; i++) {
1593 fwrite((void *)&locs[i-LOCMIN].describe, sizeof(Aword), 1, savfil);
1594 if (locs[i-LOCMIN].atrs)
1595 for (atr = (AtrElem *) addrTo(locs[i-LOCMIN].atrs); !endOfTable(atr); atr++)
1596 fwrite((void *)&atr->val, sizeof(Aword), 1, savfil);
1597 }
1598
1599 /* Save objects */
1600 for (i = OBJMIN; i <= OBJMAX; i++) {
1601 fwrite((void *)&objs[i-OBJMIN].loc, sizeof(Aword), 1, savfil);
1602 if (objs[i-OBJMIN].atrs)
1603 for (atr = (AtrElem *) addrTo(objs[i-OBJMIN].atrs); !endOfTable(atr); atr++)
1604 fwrite((void *)&atr->val, sizeof(atr->val), 1, savfil);
1605 }
1606
1607 /* Save the event queue */
1608 eventq[etop].time = 0; /* Mark the top */
1609 fwrite((void *)&eventq[0], sizeof(eventq[0]), etop+1, savfil);
1610
1611 /* Save scores */
1612 for (i = 0; scores[i] != EOF; i++)
1613 fwrite((void *)&scores[i], sizeof(Aword), 1, savfil);
1614
1615 fclose(savfil);
1616 }
1617
1618
1619 /*----------------------------------------------------------------------
1620
1621 restore()
1622
1623 */
1624
1625 #ifdef _PROTOTYPES_
1626 void restore(void)
1627 #else
1628 void restore()
1629 #endif
1630 {
1631 int i,tmp;
1632 FILE *savfil;
1633 char str[256];
1634 AtrElem *atr;
1635 char savedVersion[4];
1636 char savedName[256];
1637
1638 #ifdef GLKUNIX_FILEREF_GET_FILENAME
1639
1640 frefid_t fref;
1641 fref = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Read, 0);
1642 if (fref == NULL)
1643 error(M_SAVEFAILED);
1644 strcpy(str, glkunix_fileref_get_filename(fref));
1645 glk_fileref_destroy(fref);
1646
1647 #else
1648
1649 /* First save ? */
1650 if (savfnm[0] == '\0') {
1651 strcpy(savfnm, advnam);
1652 strcat(savfnm, ".sav");
1653 }
1654 prmsg(M_RESTOREFROM);
1655 sprintf(str, "(%s) : ", savfnm);
1656 output(str);
1657 #ifdef USE_READLINE
1658 readline(str);
1659 #else
1660 gets(str);
1661 #endif
1662
1663 #endif
1664
1665 if (str[0] == '\0')
1666 strcpy(str, savfnm);
1667 col = 1;
1668 if (str[0] == '\0')
1669 strcpy(str, savfnm); /* Use the name temporarily */
1670 if ((savfil = fopen(str, READ_MODE)) == NULL)
1671 error(M_SAVEMISSING);
1672 strcpy(savfnm, str); /* Save it for future use */
1673
1674 tmp = fread((void *)&savedVersion, sizeof(Aword), 1, savfil);
1675 /* 4f - save file version check doesn't seem to work on PC's! */
1676 if (strncmp(savedVersion, header->vers, 4)) {
1677 fclose(savfil);
1678 error(M_SAVEVERS);
1679 return;
1680 }
1681 i = 0;
1682 while ((savedName[i++] = fgetc(savfil)) != '\0');
1683 if (strcmp(savedName, advnam) != 0) {
1684 fclose(savfil);
1685 error(M_SAVENAME);
1686 return;
1687 }
1688
1689 /* Restore current values */
1690 tmp = fread((void *)&cur, sizeof(cur), 1, savfil);
1691 /* Restore actors */
1692 for (i = ACTMIN; i <= ACTMAX; i++) {
1693 tmp = fread((void *)&acts[i-ACTMIN].loc, sizeof(Aword), 1, savfil);
1694 tmp = fread((void *)&acts[i-ACTMIN].script, sizeof(Aword), 1, savfil);
1695 tmp = fread((void *)&acts[i-ACTMIN].step, sizeof(Aword), 1, savfil);
1696 tmp = fread((void *)&acts[i-ACTMIN].count, sizeof(Aword), 1, savfil);
1697 if (acts[i-ACTMIN].atrs)
1698 for (atr = (AtrElem *) addrTo(acts[i-ACTMIN].atrs); !endOfTable(atr); atr++)
1699 tmp = fread((void *)&atr->val, sizeof(Aword), 1, savfil);
1700 }
1701
1702 /* Restore locations */
1703 for (i = LOCMIN; i <= LOCMAX; i++) {
1704 tmp = fread((void *)&locs[i-LOCMIN].describe, sizeof(Aword), 1, savfil);
1705 if (locs[i-LOCMIN].atrs)
1706 for (atr = (AtrElem *) addrTo(locs[i-LOCMIN].atrs); !endOfTable(atr); atr++)
1707 tmp = fread((void *)&atr->val, sizeof(Aword), 1, savfil);
1708 }
1709
1710 /* Restore objects */
1711 for (i = OBJMIN; i <= OBJMAX; i++) {
1712 tmp = fread((void *)&objs[i-OBJMIN].loc, sizeof(Aword), 1, savfil);
1713 if (objs[i-OBJMIN].atrs)
1714 for (atr = (AtrElem *) addrTo(objs[i-OBJMIN].atrs); !endOfTable(atr); atr++)
1715 tmp = fread((void *)&atr->val, sizeof(atr->val), 1, savfil);
1716 }
1717
1718 /* Restore the eventq */
1719 etop = 0;
1720 do {
1721 tmp = fread((void *)&eventq[etop], sizeof(eventq[0]), 1, savfil);
1722 etop++;
1723 } while (eventq[etop-1].time != 0);
1724 etop--;
1725
1726 /* Restore scores */
1727 for (i = 0; scores[i] != EOF; i++)
1728 tmp = fread((void *)&scores[i], sizeof(Aword), 1, savfil);
1729
1730 fclose(savfil);
1731 }
1732
1733
1734
1735 /*----------------------------------------------------------------------
1736
1737 rnd()
1738
1739 */
1740
1741 #ifdef _PROTOTYPES_
1742 Aword rnd(Aword from, Aword to)
1743 #else
1744 Aword rnd(from, to)
1745 Aword from, to;
1746 #endif
1747 {
1748 if (to == from)
1749 return to;
1750 else if (to > from)
1751 return (rand()/10)%(to-from+1)+from;
1752 else
1753 return (rand()/10)%(from-to+1)+to;
1754 }
1755
1756
1757
1758 /*----------------------------------------------------------------------
1759
1760 btw()
1761
1762 BETWEEN
1763
1764 */
1765
1766 #ifdef _PROTOTYPES_
1767 Abool btw(Aint val, Aint low, Aint high)
1768 #else
1769 Abool btw(val, low, high)
1770 Aint val, low, high;
1771 #endif
1772 {
1773 if (high > low)
1774 return low <= val && val <= high;
1775 else
1776 return high <= val && val <= low;
1777 }
1778
1779
1780
1781 /*----------------------------------------------------------------------
1782
1783 contains()
1784
1785 */
1786
1787 #ifdef _PROTOTYPES_
1788 Aword contains(Aptr string, Aptr substring)
1789 #else
1790 Aword contains(string, substring)
1791 Aptr string, substring;;
1792 #endif
1793 {
1794 Abool found;
1795
1796 strlow((char *)string);
1797 strlow((char *)substring);
1798
1799 found = (strstr((char *)string, (char *)substring) != 0);
1800
1801 free((char *)string);
1802 free((char *)substring);
1803
1804 return(found);
1805 }
1806
1807
1808
1809 /*----------------------------------------------------------------------
1810
1811 streq()
1812
1813 Compare two strings approximately, ignore case
1814
1815 */
1816 #ifdef _PROTOTYPES_
1817 Abool streq(char a[], char b[])
1818 #else
1819 Abool streq(a, b)
1820 char a[], b[]; /* IN - Strings to compare */
1821 #endif
1822 {
1823 Boolean eq;
1824
1825 strlow(a);
1826 strlow(b);
1827
1828 eq = (strcmp(a, b) == 0);
1829
1830 free(a);
1831 free(b);
1832
1833 return(eq);
1834 }