1 /* @(#)io.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: io.c,v 1.22 2009/08/25 06:56:52 dholland Exp $ */
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * The game adventure was originally written in Fortran by Will Crowther
9 * and Don Woods. It was later translated to C and enhanced by Jim
10 * Gillogly. This code is derived from software contributed to Berkeley
11 * by Jim Gillogly at The Rand Corporation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 /* Re-coding of advent in C: file i/o and user i/o */
47 static int next(void);
48 static void rdesc(int);
49 static void rdefault(void);
50 static void rhints(void);
51 static void rliq(void);
52 static void rlocs(void);
53 static int rnum(void);
54 static void rtrav(void);
55 static void rvoc(void);
57 /* get command from user */
58 /* no prompt, usually */
60 getin(char **wrd1
, char **wrd2
)
63 static char wd1buf
[MAXSTR
], wd2buf
[MAXSTR
];
66 *wrd1
= wd1buf
; /* return ptr to internal str */
68 wd2buf
[0] = 0; /* in case it isn't set here */
69 for (s
= wd1buf
, first
= 1, numch
= 0;;) {
71 if ((*s
= (char)c
) >= 'A' && *s
<= 'Z')
72 *s
= *s
- ('A' - 'a');
73 /* convert to upper case */
74 switch (c
) { /* start reading from user */
79 if (s
== wd1buf
|| s
== wd2buf
) /* initial blank */
82 if (first
) { /* finished 1st wd; start 2nd */
86 } else { /* finished 2nd word */
92 printf("user closed input stream, quitting...\n");
95 if (++numch
>= MAXSTR
) { /* string too long */
96 printf("Give me a break!!\n");
97 wd1buf
[0] = wd2buf
[0] = 0;
106 /* confirm with rspeak */
108 yes(int x
, int y
, int z
)
110 int result
= TRUE
; /* pacify gcc */
113 rspeak(x
); /* tell him what we want */
114 if ((ch
= getchar()) == 'y')
118 else if (ch
== EOF
) {
119 printf("user closed input stream, quitting...\n");
123 if (ch
== 'y' || ch
== 'n')
125 printf("Please answer the question.\n");
134 /* confirm with mspeak */
136 yesm(int x
, int y
, int z
)
138 int result
= TRUE
; /* pacify gcc */
141 mspeak(x
); /* tell him what we want */
142 if ((ch
= getchar()) == 'y')
146 else if (ch
== EOF
) {
147 printf("user closed input stream, quitting...\n");
151 if (ch
== 'y' || ch
== 'n')
153 printf("Please answer the question.\n");
161 /* FILE *inbuf,*outbuf; */
163 static char *inptr
; /* Pointer into virtual disk */
165 static int outsw
= 0; /* putting stuff to data file? */
167 static const char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
168 static const char *tape
= iotape
; /* pointer to encryption tape */
170 /* next virtual char, bump adr */
176 ch
= (*inptr
^ random()) & 0xFF; /* Decrypt input data */
177 if (outsw
) { /* putting data in tmp file */
179 tape
= iotape
; /* rewind encryption tape */
180 *inptr
= ch
^ *tape
++; /* re-encrypt and replace value */
186 static char breakch
; /* tell which char ended rnum */
188 /* "read" data from virtual file */
195 inptr
= data_file
; /* Pointer to virtual data file */
196 srandom(SEED
); /* which is lightly encrypted. */
199 for (;;) { /* read data sections */
200 sect
= next() - '0'; /* 1st digit of section number */
202 printf("Section %c", sect
+ '0');
204 if ((ch
= next()) != LF
) { /* is there a second digit? */
209 sect
= 10 * sect
+ ch
- '0';
215 case 0: /* finished reading database */
217 case 1: /* long form descriptions */
220 case 2: /* short form descriptions */
223 case 3: /* travel table */
226 case 4: /* vocabulary */
229 case 5: /* object descriptions */
232 case 6: /* arbitrary messages */
235 case 7: /* object locations */
238 case 8: /* action defaults */
241 case 9: /* liquid assets */
244 case 10: /* class messages */
250 case 12: /* magic messages */
254 printf("Invalid data section number: %d\n", sect
);
258 if (breakch
!= LF
) /* routines return after "-1" */
265 /* read initial location num */
270 tape
= iotape
; /* restart encryption tape */
271 for (s
= nbf
, *s
= 0;; s
++)
272 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
274 breakch
= *s
; /* save char for rtrav() */
275 *s
= 0; /* got the number as ascii */
277 return (-1); /* end of data */
278 return (atoi(nbf
)); /* convert it to integer */
281 static char *seekhere
;
283 /* read description-format msgs */
288 char *seekstart
, *maystart
;
290 seekhere
= inptr
; /* Where are we in virtual file? */
291 outsw
= 1; /* these msgs go into tmp file */
292 for (oldloc
= -1, seekstart
= seekhere
;;) {
293 maystart
= inptr
; /* maybe starting new entry */
294 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
296 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) {
297 switch (sect
) { /* now put it into right table */
298 case 1:/* long descriptions */
299 ltext
[oldloc
].seekadr
= seekhere
;
300 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
302 case 2:/* short descriptions */
303 stext
[oldloc
].seekadr
= seekhere
;
304 stext
[oldloc
].txtlen
= maystart
- seekstart
;
306 case 5:/* object descriptions */
307 ptext
[oldloc
].seekadr
= seekhere
;
308 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
310 case 6:/* random messages */
311 if (oldloc
>= RTXSIZE
)
312 errx(1,"Too many random msgs");
313 rtext
[oldloc
].seekadr
= seekhere
;
314 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
316 case 10: /* class messages */
317 ctext
[classes
].seekadr
= seekhere
;
318 ctext
[classes
].txtlen
= maystart
- seekstart
;
319 cval
[classes
++] = oldloc
;
321 case 12: /* magic messages */
322 if (oldloc
>= MAGSIZE
)
323 errx(1,"Too many magic msgs");
324 mtext
[oldloc
].seekadr
= seekhere
;
325 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
328 errx(1,"rdesc called with bad section");
330 seekhere
+= maystart
- seekstart
;
333 outsw
= 0; /* turn off output */
334 seekhere
+= 3; /* -1<delimiter> */
337 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
338 if (oldloc
!= locc
) /* starting a new message */
339 seekstart
= maystart
;
342 FLUSHLF
; /* scan the line */
346 /* read travel table */
351 struct travlist
*t
= NULL
;
354 int len
, m
, n
, entries
= 0;
356 for (oldloc
= -1;;) { /* get another line */
358 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 && t
) {
359 t
->next
= 0; /* terminate the old entry */
360 /* printf("%d:%d entries\n",oldloc,entries); */
361 /* twrite(oldloc); */
365 if (locc
!= oldloc
) { /* getting a new entry */
366 t
= travel
[locc
] = calloc(1, sizeof(*t
));
369 /* printf("New travel list for %d\n",locc); */
373 for (s
= buf
;; s
++) /* get the newloc number /ASCII */
374 if ((*s
= next()) == TAB
|| *s
== LF
)
377 len
= length(buf
) - 1; /* quad long number handling */
378 /* printf("Newloc: %s (%d chars)\n",buf,len); */
379 if (len
< 4) { /* no "m" conditions */
381 n
= atoi(buf
); /* newloc mod 1000 = newloc */
382 } else { /* a long integer */
383 n
= atoi(buf
+ len
- 3);
384 buf
[len
- 3] = 0; /* terminate newloc/1000 */
387 while (breakch
!= LF
) { /* only do one line at a time */
391 t
->next
= calloc(1, sizeof(*t
));
396 t
->tverb
= rnum(); /* get verb from the file */
397 t
->tloc
= n
; /* table entry mod 1000 */
398 t
->conditions
= m
; /* table entry / 1000 */
399 /* printf("entry %d for %d\n",entries,locc); */
405 /* travel options from this loc */
413 for (t
= travel
[loq
]; t
!= 0; t
= t
->next
) {
414 printf("verb %d takes you to ", t
->tverb
);
416 speak(<ext
[t
->tloc
]);
419 printf("special code %d\n", t
->tloc
- 300);
421 rspeak(t
->tloc
- 500);
422 printf("under conditions %d\n", t
->conditions
);
427 /* read the vocabulary */
438 for (s
= buf
, *s
= 0;; s
++) /* get the word */
439 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
442 /* terminate word with newline, LF, tab, blank */
443 if (*s
!= '\n' && *s
!= LF
)
444 FLUSHLF
;/* can be comments */
446 /* printf("\"%s\"=%d\n",buf,idx); */
452 /* initial object locations */
457 if ((obj
= rnum()) < 0)
459 plac
[obj
] = rnum(); /* initial loc for this obj */
460 if (breakch
== TAB
) /* there's another entry */
467 /* default verb messages */
472 if ((verb
= rnum()) < 0)
474 actspeak
[verb
] = rnum();
478 /* liquid assets &c: cond bits */
483 for (;;) { /* read new bit list */
484 if ((bitnum
= rnum()) < 0)
486 for (;;) { /* read locs for bits */
490 cond
[n
] |= setbit
[bitnum
];
503 if ((hintnum
= rnum()) < 0)
505 for (i
= 1; i
< 5; i
++)
506 hints
[hintnum
][i
] = rnum();
507 if (hintnum
> hintmax
)
529 /* read, decrypt, and print a message (not ptext) */
530 /* msg is a pointer to seek address and length of mess */
532 speak(const struct text
*msg
)
538 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
539 tape
= iotape
; /* restart decryption tape */
540 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
541 /* assume tape is longer than location number */
542 /* plus the lookahead put together */
543 if ((*s
^ *tape
) == '>' &&
544 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
545 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
547 if (blklin
&& !nonfirst
++)
551 tape
= iotape
; /* rewind decryp tape */
553 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
557 /* read, decrypt and print a ptext message */
558 /* msg is the number of all the p msgs for this place */
559 /* assumes object 1 doesn't have prop 1, obj 2 no prop 2 &c */
561 pspeak(int m
, int skip
)
569 if ((tbuf
= (char *) malloc(msg
->txtlen
+ 1)) == NULL
)
571 memcpy(tbuf
, msg
->seekadr
, msg
->txtlen
+ 1); /* Room to null */
575 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
576 tape
= iotape
; /* restart decryption tape */
577 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++); /* get number */
579 /* Temporarily trash the string (cringe) */
580 *s
++ = 0; /* decrypting number within the string */
582 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
583 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
588 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
589 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
591 if (blklin
&& !nonfirst
++)
597 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */