summaryrefslogtreecommitdiff
path: root/games/adventure/io.c
blob: deff7e487e73877bfb8da6c8d0e9cc04d222dd05 (plain)
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
/*	$OpenBSD: io.c,v 1.20 2015/08/22 14:47:40 deraadt Exp $	*/
/*	$NetBSD: io.c,v 1.3 1995/04/24 12:21:37 cgd Exp $	*/

/*-
 * Copyright (c) 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * The game adventure was originally written in Fortran by Will Crowther
 * and Don Woods.  It was later translated to C and enhanced by Jim
 * Gillogly.  This code is derived from software contributed to Berkeley
 * by Jim Gillogly at The Rand Corporation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*	Re-coding of advent in C: file i/o and user i/o			*/

#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hdr.h"
#include "extern.h"

/* Get command from user. No prompt, usually.	*/
void
getin(char *wrd1, size_t siz1, char *wrd2, size_t siz2)	
{
	char   *s, *slast;
	int     ch, first;

	*wrd2 = 0;		/* in case it isn't set here	*/
	for (s = wrd1, first = 1, slast = wrd1 + siz1 - 1;;) {
		if ((ch = getchar()) >= 'A' && ch <= 'Z')
			ch = ch - ('A' - 'a');
		/* convert to upper case	*/
		switch (ch) {	/* start reading from user	*/
		case '\n':
			*s = 0;
			return;
		case ' ':
			if (s == wrd1 || s == wrd2)	/* initial blank  */
				continue;
			*s = 0;
			if (first) {		/* finished 1st wd; start 2nd */
				first = 0;
				s = wrd2;
				slast = wrd2 + siz2 - 1;
				break;
			} else {		/* finished 2nd word */
				FLUSHLINE;
				*s = 0;
				return;
			}
		case EOF:
			printf("user closed input stream, quitting...\n");
			exit(0);
		default:
			if (s == slast) {	/* string too long */
				printf("Give me a break!!\n");
				*wrd1 = *wrd2 = 0;
				FLUSHLINE;
				return;
			}
			*s++ = ch;
		}
	}
}


#if 0		/* Not used */
int
confirm(char *mesg)		/* confirm irreversible action	*/
{
	int     result;
	int     ch;

	printf("%s", mesg);	/* tell him what he did		*/
	if ((ch = getchar()) == 'y')	/* was his first letter a 'y'?	*/
		result = 1;
	else if (ch == EOF) {
		printf("user closed input stream, quitting...\n");
		exit(0);
	} else
		result = 0;
	FLUSHLINE;
	return (result);
}
#endif

int
yes(int x, int y, int z)	/* confirm with rspeak		*/
{
	int     result;
	int     ch;

	for (;;) {
		rspeak(x);	/* tell him what we want*/
		if ((ch = getchar())=='y')
			result = TRUE;
		else if (ch=='n')
			result = FALSE;
		else if (ch == EOF) {
			printf("user closed input stream, quitting...\n");
			exit(0);
		}
		if (ch != '\n')
			FLUSHLINE;
		if (ch == 'y' || ch == 'n')
			break;
		printf("Please answer the question.\n");
	}
	if (result == TRUE)
		rspeak(y);
	if (result == FALSE)
		rspeak(z);
	return (result);
}

int
yesm(int x, int y, int z)	/* confirm with mspeak		*/
{
	int     result;
	int    ch;

	for (;;) {
		mspeak(x);	/* tell him what we want	*/
		if ((ch = getchar()) == 'y')
			result = TRUE;
		else if (ch == 'n')
			result = FALSE;
		else if (ch == EOF) {
			printf("user closed input stream, quitting...\n");
			exit(0);
		}
		if (ch != '\n')
			FLUSHLINE;
		if (ch == 'y' || ch == 'n')
			break;
		printf("Please answer the question.\n");
	}
	if (result == TRUE)
		mspeak(y);
	if (result == FALSE)
		mspeak(z);
	return (result);
}

/* FILE *inbuf,*outbuf; */

char   *inptr;			/* Pointer into virtual disk	*/

int     outsw = 0;		/* putting stuff to data file?	*/

const char iotape[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
const char *tape = iotape;	/* pointer to obfuscation tape	*/

int
next(void)			/* next virtual char, bump adr	*/
{
	int ch;

	ch=(*inptr ^ random()) & 0xFF;	/* Deobfuscate input data	*/
	if (outsw) {			/* putting data in tmp file	*/
		if (*tape == 0)
			tape = iotape;	/* rewind obfuscation tape	*/
		*inptr = ch ^ *tape++;	/* re-obfuscate and replace value */
	}
	inptr++;
	return (ch);
}

char	breakch;		/* tell which char ended rnum	*/

void
rdata(void)			/* "read" data from virtual file */
{
	int     sect;
	char    ch;

	inptr = data_file;	/* Pointer to virtual data file */

	clsses = 1;
	for (;;) {		/* read data sections		*/
		sect = next() - '0';	/* 1st digit of section number	*/
#ifdef VERBOSE
		printf("Section %c", sect + '0');
#endif
		if ((ch = next()) != LF) {	/* is there a second digit?	*/
			FLUSHLF;
#ifdef VERBOSE
			putchar(ch);
#endif
			sect = 10 * sect + ch - '0';
		}
#ifdef VERBOSE
		putchar('\n');
#endif
		switch (sect) {
		case 0:		/* finished reading database	*/
			return;
		case 1:		/* long form descriptions	*/
			rdesc(1);
			break;
		case 2:		/* short form descriptions	*/
			rdesc(2);
			break;
		case 3:		/* travel table			*/
			rtrav();
			break;
		case 4:		/* vocabulary			*/
			rvoc();
			break;
		case 5:		/* object descriptions		*/
			rdesc(5);
			break;
		case 6:		/* arbitrary messages		*/
			rdesc(6);
			break;
		case 7:		/* object locations		*/
			rlocs();
			break;
		case 8:		/* action defaults		*/
			rdflt();
			break;
		case 9:		/* liquid assets		*/
			rliq();
			break;
		case 10:	/* class messages		*/
			rdesc(10);
			break;
		case 11:	/* hints			*/
			rhints();
			break;
		case 12:	/* magic messages		*/
			rdesc(12);
			break;
		default:
			printf("Invalid data section number: %d\n", sect);
			for (;;)
				putchar(next());
		}
		if (breakch != LF)	/* routines return after "-1"	*/
			FLUSHLF;
	}
}

char	nbf[12];


int
rnum(void)			/* read initial location num	*/
{
	char	*s;

	tape = iotape;		/* restart obfuscation tape	*/
	for (s = nbf, *s = 0;; s++)
		if ((*s = next()) == TAB || *s == '\n' || *s == LF)
			break;
	breakch = *s;		/* save char for rtrav()	*/
	*s = 0;			/* got the number as ascii	*/
	if (nbf[0] == '-')
		return (-1);	/* end of data			*/
	return (atoi(nbf));	/* convert it to integer	*/
}

char	*seekhere;

void
rdesc(int sect)			/* read description-format msgs */
{
	int     locc;
	char   *seekstart, *maystart;

	seekhere = inptr;	/* Where are we in virtual file?*/
	outsw = 1;		/* these msgs go into tmp file	*/
	for (oldloc = -1, seekstart = seekhere;;) {
		maystart = inptr;	/* maybe starting new entry	*/
		if ((locc = rnum()) != oldloc && oldloc >= 0  /* finished msg */
		    && !(sect == 5 && (locc == 0 || locc >= 100)))/* unless sect 5*/
		{
			switch (sect) {	/* now put it into right table  */
			case 1:	/* long descriptions		*/
				ltext[oldloc].seekadr = seekhere;
				ltext[oldloc].txtlen = maystart - seekstart;
				break;
			case 2:	/* short descriptions		*/
				stext[oldloc].seekadr = seekhere;
				stext[oldloc].txtlen = maystart - seekstart;
				break;
			case 5:	/* object descriptions		*/
				ptext[oldloc].seekadr = seekhere;
				ptext[oldloc].txtlen = maystart - seekstart;
				break;
			case 6:	/* random messages		*/
				if (oldloc >= RTXSIZ)
					errx(1, "Too many random msgs");
				rtext[oldloc].seekadr = seekhere;
				rtext[oldloc].txtlen = maystart - seekstart;
				break;
			case 10:/* class messages		*/
				ctext[clsses].seekadr = seekhere;
				ctext[clsses].txtlen = maystart - seekstart;
				cval[clsses++] = oldloc;
				break;
			case 12:/* magic messages		*/
				if (oldloc >= MAGSIZ)
					errx(1, "Too many magic msgs");
				mtext[oldloc].seekadr = seekhere;
				mtext[oldloc].txtlen = maystart - seekstart;
				break;
			default:
				errx(1, "rdesc called with bad section");
			}
			seekhere += maystart - seekstart;
		}
		if (locc < 0) {
			outsw = 0;	/* turn off output		*/
			seekhere += 3;	/* -1<delimiter>		*/
			return;
		}
		if (sect != 5 || (locc > 0 && locc < 100)) {
			if (oldloc != locc)/* starting a new message	*/
				seekstart = maystart;
			oldloc = locc;
		}
		FLUSHLF;		/* scan the line		*/
	}
}


void
rtrav(void)				/* read travel table		*/
{
	int     locc;
	struct travlist *t;
	char   *s;
	char    buf[12];
	int     len, m, n, entries;

	for (oldloc = -1;;) {		/* get another line		*/
		if ((locc = rnum()) != oldloc && oldloc >= 0) { /* end of entry */
			t->next = NULL;	/* terminate the old entry	*/
		/*	printf("%d:%d entries\n", oldloc, entries);	*/
		/*	twrite(oldloc);					*/
		}
		if (locc == -1)
			return;
		if (locc != oldloc) {	/* getting a new entry		*/
			t = travel[locc] = calloc(1, sizeof(*t));
			if (t == NULL)
				err(1, NULL);
		/*	printf("New travel list for %d\n", locc);	*/
			entries = 0;
			oldloc = locc;
		}
		for (s = buf; ; *s++)	/* get the newloc number /ASCII */
			if ((*s = next()) == TAB || *s == LF)
				break;
		*s = 0;
		len = length(buf) - 1;	/* quad long number handling	*/
	/*	printf("Newloc: %s (%d chars)\n", buf, len);		*/
		if (len < 4) {		/* no "m" conditions		*/
			m = 0;
			n = atoi(buf);	/* newloc mod 1000 = newloc	*/
		} else {		/* a long integer		*/
			n = atoi(buf + len - 3);
			buf[len - 3] = 0;	/* terminate newloc/1000*/
			m = atoi(buf);
		}
		while (breakch != LF) {	/* only do one line at a time	*/
			if (t ==  NULL)
				errx(1, "corrupt file");
			if (entries++) {
				t->next = calloc(1, sizeof (*t->next));
				if (t->next == NULL)
					err(1, NULL);
				t = t->next;
			}
			t->tverb = rnum();/* get verb from the file	*/
			t->tloc = n;	/* table entry mod 1000		*/
			t->conditions = m;/* table entry / 1000		*/
		/*	printf("entry %d for %d\n", entries, locc);	*/
		}
	}
}

#ifdef DEBUG

void
twrite(int loq)			/* travel options from this loc */
{
	struct	travlist *t;

	printf("If");
	speak(&ltext[loq]);
	printf("then\n");
	for (t = travel[loq]; t != 0; t = t->next) {
		printf("verb %d takes you to ", t->tverb);
		if (t->tloc <= 300)
			speak(&ltext[t->tloc]);
		else if (t->tloc <= 500)
			printf("special code %d\n", t->tloc - 300);
		else
			rspeak(t->tloc - 500);
		printf("under conditions %d\n", t->conditions);
	}
}
#endif /* DEBUG */

void
rvoc(void)
{
	char   *s;		/* read the vocabulary		*/
	int     index;
	char    buf[6];

	for (;;) {
		index = rnum();
		if (index < 0)
			break;
		for (s = buf, *s = 0;; s++)	/* get the word		*/
			if ((*s = next()) == TAB || *s == '\n' || *s == LF
				|| *s == ' ')
				break;
			/* terminate word with newline, LF, tab, blank	*/
		if (*s != '\n' && *s != LF)
			FLUSHLF;	/* can be comments	*/
		*s = 0;
	/*	printf("\"%s\"=%d\n", buf, index);*/
		vocab(buf, -2, index);
	}
/*	prht();	*/
}


void
rlocs(void)				/* initial object locations	*/
{
	for (;;) {
		if ((obj = rnum()) < 0)
			break;
		plac[obj] = rnum();	/* initial loc for this obj	*/
		if (breakch == TAB)	/* there's another entry	*/
			fixd[obj] = rnum();
		else
			fixd[obj] = 0;
	}
}

void
rdflt(void)			/* default verb messages	*/
{
	for (;;) {
		if ((verb = rnum()) < 0)
			break;
		actspk[verb] = rnum();
	}
}

void
rliq(void)			/* liquid assets &c: cond bits	*/
{
	int bitnum;

	for (;;) {		/* read new bit list		*/
		if ((bitnum = rnum()) < 0)
			break;
		for (;;) {	/* read locs for bits		*/
			cond[rnum()] |= setbit[bitnum];
			if (breakch == LF)
				break;
		}
	}
}

void
rhints(void)
{
	int     hintnum, i;

	hntmax = 0;
	for (;;) {
		if ((hintnum = rnum()) < 0)
			break;
		for (i = 1; i < 5; i++)
			hints[hintnum][i] = rnum();
		if (hintnum > hntmax)
			hntmax = hintnum;
	}
}


void
rspeak(int msg)
{
	if (msg != 0)
		speak(&rtext[msg]);
}


void
mspeak(int msg)
{
	if (msg != 0)
		speak(&mtext[msg]);
}

/*
 * Read, deobfuscate, and print a message (not ptext)
 * msg is a pointer to seek address and length of mess
 */
void
speak(const struct text *msg)	
{
	char   *s, nonfirst;

	s = msg->seekadr;
	nonfirst = 0;
	while (s - msg->seekadr < msg->txtlen) { /* read a line at a time */
		tape = iotape;		/* restart deobfuscation tape	*/
		while ((*s++ ^ *tape++) != TAB); /* read past loc num	*/
		/* assume tape is longer than location number		*/
		/*  plus the lookahead put together			*/
		if ((*s ^ *tape) == '>' &&
		    (*(s + 1) ^ *(tape + 1)) == '$' &&
		    (*(s + 2) ^ *(tape + 2)) == '<')
			break;
		if (blklin && !nonfirst++)
			putchar('\n');
		do {
			if (*tape == 0)
				tape = iotape;/* rewind decryp tape */
			putchar(*s ^ *tape);
		} while ((*s++ ^ *tape++) != LF);	/* better end with LF */
	}
}

/*
 * Read, deobfuscate, and print a ptext message
 * msg is the number of all the p msgs for this place
 * assumes object 1 doesn't have prop 1, obj 2 no prop 2 &c
 */
void
pspeak(int m, int skip) 
{
	char   *s, nonfirst;
	char   *numst, save;
	struct text *msg;
	char   *tbuf;

	msg = &ptext[m];
	if ((tbuf = malloc(msg->txtlen + 1)) == 0)
		err(1, NULL);
	memcpy(tbuf, msg->seekadr, msg->txtlen + 1);	/* Room to null */
	s = tbuf;

	nonfirst = 0;
	while (s - tbuf < msg->txtlen) {	/* read line at a time	*/
		tape = iotape;			/* restart dobfuscation tape */
		for (numst = s; (*s ^= *tape++) != TAB; s++)
			; /* get number	*/

		save = *s; /* Temporarily trash the string (cringe)	*/
		*s++ = 0; /* deobfuscation number within the string	*/

		if (atoi(numst) != 100 * skip && skip >= 0) {
			while ((*s++ ^ * tape++) != LF) /* flush the line */
				if (*tape == 0)
					tape = iotape;
			continue;
		}
		if ((*s^ * tape) == '>' && (*(s + 1) ^ * (tape + 1)) == '$' &&
			(*(s + 2) ^ * (tape + 2)) == '<')
			break;
		if (blklin && !nonfirst++)
			putchar('\n');
		do {
			if (*tape == 0)
				tape = iotape;
			putchar(*s^ * tape);
		} while ((*s++ ^ * tape++) != LF);	/* better end with LF */
		if (skip < 0)
			break;
	}
	free(tbuf);
}