xref: /dflybsd-src/games/monop/execute.c (revision 11c3b524747d8c5b8633b938fd0fceb1d5878a2e)
1*11c3b524SAaron LI /*	$NetBSD: execute.c,v 1.22 2012/06/19 05:35:32 dholland Exp $	*/
2*11c3b524SAaron LI 
3*11c3b524SAaron LI /*
4*11c3b524SAaron LI  * Copyright (c) 1980, 1993
5*11c3b524SAaron LI  *	The Regents of the University of California.  All rights reserved.
6*11c3b524SAaron LI  *
7*11c3b524SAaron LI  * Redistribution and use in source and binary forms, with or without
8*11c3b524SAaron LI  * modification, are permitted provided that the following conditions
9*11c3b524SAaron LI  * are met:
10*11c3b524SAaron LI  * 1. Redistributions of source code must retain the above copyright
11*11c3b524SAaron LI  *    notice, this list of conditions and the following disclaimer.
12*11c3b524SAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
13*11c3b524SAaron LI  *    notice, this list of conditions and the following disclaimer in the
14*11c3b524SAaron LI  *    documentation and/or other materials provided with the distribution.
15*11c3b524SAaron LI  * 3. Neither the name of the University nor the names of its contributors
16*11c3b524SAaron LI  *    may be used to endorse or promote products derived from this software
17*11c3b524SAaron LI  *    without specific prior written permission.
18*11c3b524SAaron LI  *
19*11c3b524SAaron LI  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*11c3b524SAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*11c3b524SAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*11c3b524SAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*11c3b524SAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*11c3b524SAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*11c3b524SAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*11c3b524SAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*11c3b524SAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*11c3b524SAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*11c3b524SAaron LI  * SUCH DAMAGE.
30*11c3b524SAaron LI  *
31*11c3b524SAaron LI  *	@(#)execute.c	8.1 (Berkeley) 5/31/93
32*11c3b524SAaron LI  */
33*11c3b524SAaron LI 
34*11c3b524SAaron LI #include <fcntl.h>
35*11c3b524SAaron LI #include <stdlib.h>
36*11c3b524SAaron LI #include <unistd.h>
37*11c3b524SAaron LI #include <limits.h>
38*11c3b524SAaron LI #include <sys/types.h>
39*11c3b524SAaron LI #include <sys/stat.h>
40*11c3b524SAaron LI #include <sys/time.h>
41*11c3b524SAaron LI #include <time.h>
42*11c3b524SAaron LI #include <errno.h>
43*11c3b524SAaron LI 
44*11c3b524SAaron LI #include "deck.h"
45*11c3b524SAaron LI #include "monop.h"
46*11c3b524SAaron LI 
47*11c3b524SAaron LI #define MIN_FORMAT_VERSION 1
48*11c3b524SAaron LI #define CUR_FORMAT_VERSION 1
49*11c3b524SAaron LI #define MAX_FORMAT_VERSION 1
50*11c3b524SAaron LI 
51*11c3b524SAaron LI typedef	struct stat	STAT;
52*11c3b524SAaron LI typedef	struct tm	TIME;
53*11c3b524SAaron LI 
54*11c3b524SAaron LI static char	buf[257];
55*11c3b524SAaron LI 
56*11c3b524SAaron LI static bool	new_play;	/* set if move on to new player		*/
57*11c3b524SAaron LI 
58*11c3b524SAaron LI static void show_move(void);
59*11c3b524SAaron LI 
60*11c3b524SAaron LI static void restore_reset(void);
61*11c3b524SAaron LI static int restore_parseline(char *txt);
62*11c3b524SAaron LI static int restore_toplevel_attr(const char *attribute, char *txt);
63*11c3b524SAaron LI static int restore_player_attr(const char *attribute, char *txt);
64*11c3b524SAaron LI static int restore_deck_attr(const char *attribute, char *txt);
65*11c3b524SAaron LI static int restore_square_attr(const char *attribute, char *txt);
66*11c3b524SAaron LI static int getnum(const char *what, char *txt, int min, int max, int *ret);
67*11c3b524SAaron LI static int getnum_withbrace(const char *what, char *txt, int min, int max,
68*11c3b524SAaron LI 		int *ret);
69*11c3b524SAaron LI 
70*11c3b524SAaron LI /*
71*11c3b524SAaron LI  *	This routine executes the given command by index number
72*11c3b524SAaron LI  */
73*11c3b524SAaron LI void
execute(int com_num)74*11c3b524SAaron LI execute(int com_num)
75*11c3b524SAaron LI {
76*11c3b524SAaron LI 	new_play = FALSE;	/* new_play is true if fixing	*/
77*11c3b524SAaron LI 	(*func[com_num])();
78*11c3b524SAaron LI 	notify();
79*11c3b524SAaron LI 	force_morg();
80*11c3b524SAaron LI 	if (new_play)
81*11c3b524SAaron LI 		next_play();
82*11c3b524SAaron LI 	else if (num_doub)
83*11c3b524SAaron LI 		printf("%s rolled doubles.  Goes again\n", cur_p->name);
84*11c3b524SAaron LI }
85*11c3b524SAaron LI 
86*11c3b524SAaron LI /*
87*11c3b524SAaron LI  *	This routine moves a piece around.
88*11c3b524SAaron LI  */
89*11c3b524SAaron LI void
do_move(void)90*11c3b524SAaron LI do_move(void)
91*11c3b524SAaron LI {
92*11c3b524SAaron LI 	int r1, r2;
93*11c3b524SAaron LI 	bool was_jail;
94*11c3b524SAaron LI 
95*11c3b524SAaron LI 	new_play = was_jail = FALSE;
96*11c3b524SAaron LI 	printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
97*11c3b524SAaron LI 	if (cur_p->loc == JAIL) {
98*11c3b524SAaron LI 		was_jail++;
99*11c3b524SAaron LI 		if (!move_jail(r1, r2)) {
100*11c3b524SAaron LI 			new_play++;
101*11c3b524SAaron LI 			goto ret;
102*11c3b524SAaron LI 		}
103*11c3b524SAaron LI 	}
104*11c3b524SAaron LI 	else {
105*11c3b524SAaron LI 		if (r1 == r2 && ++num_doub == 3) {
106*11c3b524SAaron LI 			printf("That's 3 doubles.  You go to jail\n");
107*11c3b524SAaron LI 			goto_jail();
108*11c3b524SAaron LI 			new_play++;
109*11c3b524SAaron LI 			goto ret;
110*11c3b524SAaron LI 		}
111*11c3b524SAaron LI 		move(r1+r2);
112*11c3b524SAaron LI 	}
113*11c3b524SAaron LI 	if (r1 != r2 || was_jail)
114*11c3b524SAaron LI 		new_play++;
115*11c3b524SAaron LI ret:
116*11c3b524SAaron LI 	return;
117*11c3b524SAaron LI }
118*11c3b524SAaron LI 
119*11c3b524SAaron LI /*
120*11c3b524SAaron LI  *	This routine moves a normal move
121*11c3b524SAaron LI  */
122*11c3b524SAaron LI void
move(int rl)123*11c3b524SAaron LI move(int rl)
124*11c3b524SAaron LI {
125*11c3b524SAaron LI 	int old_loc;
126*11c3b524SAaron LI 
127*11c3b524SAaron LI 	old_loc = cur_p->loc;
128*11c3b524SAaron LI 	cur_p->loc = (cur_p->loc + rl) % N_SQRS;
129*11c3b524SAaron LI 	if (cur_p->loc < old_loc && rl > 0) {
130*11c3b524SAaron LI 		cur_p->money += 200;
131*11c3b524SAaron LI 		printf("You pass %s and get $200\n", board[0].name);
132*11c3b524SAaron LI 	}
133*11c3b524SAaron LI 	show_move();
134*11c3b524SAaron LI }
135*11c3b524SAaron LI 
136*11c3b524SAaron LI /*
137*11c3b524SAaron LI  *	This routine shows the results of a move
138*11c3b524SAaron LI  */
139*11c3b524SAaron LI static void
show_move(void)140*11c3b524SAaron LI show_move(void)
141*11c3b524SAaron LI {
142*11c3b524SAaron LI 	SQUARE *sqp;
143*11c3b524SAaron LI 
144*11c3b524SAaron LI 	sqp = &board[cur_p->loc];
145*11c3b524SAaron LI 	printf("That puts you on %s\n", sqp->name);
146*11c3b524SAaron LI 	switch (sqp->type) {
147*11c3b524SAaron LI 	  case SAFE:
148*11c3b524SAaron LI 		printf("That is a safe place\n");
149*11c3b524SAaron LI 		break;
150*11c3b524SAaron LI 	  case CC:
151*11c3b524SAaron LI 		cc();
152*11c3b524SAaron LI 		break;
153*11c3b524SAaron LI 	  case CHANCE:
154*11c3b524SAaron LI 		chance();
155*11c3b524SAaron LI 		break;
156*11c3b524SAaron LI 	  case INC_TAX:
157*11c3b524SAaron LI 		inc_tax();
158*11c3b524SAaron LI 		break;
159*11c3b524SAaron LI 	  case GOTO_J:
160*11c3b524SAaron LI 		goto_jail();
161*11c3b524SAaron LI 		break;
162*11c3b524SAaron LI 	  case LUX_TAX:
163*11c3b524SAaron LI 		lux_tax();
164*11c3b524SAaron LI 		break;
165*11c3b524SAaron LI 	  case PRPTY:
166*11c3b524SAaron LI 	  case RR:
167*11c3b524SAaron LI 	  case UTIL:
168*11c3b524SAaron LI 		if (sqp->owner < 0) {
169*11c3b524SAaron LI 			printf("That would cost $%d\n", sqp->cost);
170*11c3b524SAaron LI 			if (getyn("Do you want to buy? ") == 0) {
171*11c3b524SAaron LI 				buy(player, sqp);
172*11c3b524SAaron LI 				cur_p->money -= sqp->cost;
173*11c3b524SAaron LI 			}
174*11c3b524SAaron LI 			else if (num_play > 2)
175*11c3b524SAaron LI 				bid();
176*11c3b524SAaron LI 		}
177*11c3b524SAaron LI 		else if (sqp->owner == player)
178*11c3b524SAaron LI 			printf("You own it.\n");
179*11c3b524SAaron LI 		else
180*11c3b524SAaron LI 			rent(sqp);
181*11c3b524SAaron LI 	}
182*11c3b524SAaron LI }
183*11c3b524SAaron LI 
184*11c3b524SAaron LI /*
185*11c3b524SAaron LI  * Reset the game state.
186*11c3b524SAaron LI  */
187*11c3b524SAaron LI static void
reset_game(void)188*11c3b524SAaron LI reset_game(void)
189*11c3b524SAaron LI {
190*11c3b524SAaron LI 	int i;
191*11c3b524SAaron LI 
192*11c3b524SAaron LI 	for (i = 0; i < N_SQRS; i++) {
193*11c3b524SAaron LI 		board[i].owner = -1;
194*11c3b524SAaron LI 		if (board[i].type == PRPTY) {
195*11c3b524SAaron LI 			board[i].desc->morg = 0;
196*11c3b524SAaron LI 			board[i].desc->houses = 0;
197*11c3b524SAaron LI 		} else if (board[i].type == RR || board[i].type == UTIL) {
198*11c3b524SAaron LI 			board[i].desc->morg = 0;
199*11c3b524SAaron LI 		}
200*11c3b524SAaron LI 	}
201*11c3b524SAaron LI 
202*11c3b524SAaron LI 	for (i = 0; i < 2; i++) {
203*11c3b524SAaron LI 		deck[i].top_card = 0;
204*11c3b524SAaron LI 		deck[i].gojf_used = FALSE;
205*11c3b524SAaron LI 	}
206*11c3b524SAaron LI 
207*11c3b524SAaron LI 	if (play) {
208*11c3b524SAaron LI 		for (i = 0; i < num_play; i++) {
209*11c3b524SAaron LI 			free(play[i].name);
210*11c3b524SAaron LI 			play[i].name = NULL;
211*11c3b524SAaron LI 		}
212*11c3b524SAaron LI 		free(play);
213*11c3b524SAaron LI 		play = NULL;
214*11c3b524SAaron LI 	}
215*11c3b524SAaron LI 
216*11c3b524SAaron LI 	for (i = 0; i < MAX_PL+2; i++) {
217*11c3b524SAaron LI 		name_list[i] = NULL;
218*11c3b524SAaron LI 	}
219*11c3b524SAaron LI 
220*11c3b524SAaron LI 	cur_p = NULL;
221*11c3b524SAaron LI 	num_play = 0;
222*11c3b524SAaron LI 	player = 0;
223*11c3b524SAaron LI 	num_doub = 0;
224*11c3b524SAaron LI 	fixing = FALSE;
225*11c3b524SAaron LI 	trading = FALSE;
226*11c3b524SAaron LI 	told_em = FALSE;
227*11c3b524SAaron LI 	spec = FALSE;
228*11c3b524SAaron LI }
229*11c3b524SAaron LI 
230*11c3b524SAaron LI 
231*11c3b524SAaron LI /*
232*11c3b524SAaron LI  *	This routine saves the current game for use at a later date
233*11c3b524SAaron LI  */
234*11c3b524SAaron LI void
save(void)235*11c3b524SAaron LI save(void)
236*11c3b524SAaron LI {
237*11c3b524SAaron LI 	char *sp;
238*11c3b524SAaron LI 	FILE *outf;
239*11c3b524SAaron LI 	time_t t;
240*11c3b524SAaron LI 	struct stat sb;
241*11c3b524SAaron LI 	int i, j;
242*11c3b524SAaron LI 
243*11c3b524SAaron LI 	printf("Which file do you wish to save it in? ");
244*11c3b524SAaron LI 	fgets(buf, sizeof(buf), stdin);
245*11c3b524SAaron LI 	if (feof(stdin))
246*11c3b524SAaron LI 		return;
247*11c3b524SAaron LI 	sp = strchr(buf, '\n');
248*11c3b524SAaron LI 	if (sp)
249*11c3b524SAaron LI 		*sp = '\0';
250*11c3b524SAaron LI 
251*11c3b524SAaron LI 	/*
252*11c3b524SAaron LI 	 * check for existing files, and confirm overwrite if needed
253*11c3b524SAaron LI 	 */
254*11c3b524SAaron LI 
255*11c3b524SAaron LI 	if (stat(buf, &sb) == 0
256*11c3b524SAaron LI 	    && getyn("File exists.  Do you wish to overwrite? ") > 0)
257*11c3b524SAaron LI 		return;
258*11c3b524SAaron LI 
259*11c3b524SAaron LI 	outf = fopen(buf, "w");
260*11c3b524SAaron LI 	if (outf == NULL) {
261*11c3b524SAaron LI 		warn("%s", buf);
262*11c3b524SAaron LI 		return;
263*11c3b524SAaron LI 	}
264*11c3b524SAaron LI 	printf("\"%s\" ", buf);
265*11c3b524SAaron LI 	time(&t);			/* get current time		*/
266*11c3b524SAaron LI 
267*11c3b524SAaron LI 	/* Header */
268*11c3b524SAaron LI 	fprintf(outf, "NetBSD monop format v%d\n", CUR_FORMAT_VERSION);
269*11c3b524SAaron LI 	fprintf(outf, "time %s", ctime(&t));  /* ctime includes a \n */
270*11c3b524SAaron LI 	fprintf(outf, "numplayers %d\n", num_play);
271*11c3b524SAaron LI 	fprintf(outf, "currentplayer %d\n", player);
272*11c3b524SAaron LI 	fprintf(outf, "doubles %d\n", num_doub);
273*11c3b524SAaron LI 
274*11c3b524SAaron LI 	/* Players */
275*11c3b524SAaron LI 	for (i = 0; i < num_play; i++) {
276*11c3b524SAaron LI 		fprintf(outf, "player %d {\n", i);
277*11c3b524SAaron LI 		fprintf(outf, "    name %s\n", name_list[i]);
278*11c3b524SAaron LI 		fprintf(outf, "    money %d\n", play[i].money);
279*11c3b524SAaron LI 		fprintf(outf, "    loc %d\n", play[i].loc);
280*11c3b524SAaron LI 		fprintf(outf, "    num_gojf %d\n", play[i].num_gojf);
281*11c3b524SAaron LI 		fprintf(outf, "    in_jail %d\n", play[i].in_jail);
282*11c3b524SAaron LI 		fprintf(outf, "}\n");
283*11c3b524SAaron LI 	}
284*11c3b524SAaron LI 
285*11c3b524SAaron LI 	/* Decks */
286*11c3b524SAaron LI 	for (i = 0; i < 2; i++) {
287*11c3b524SAaron LI 		fprintf(outf, "deck %d {\n", i);
288*11c3b524SAaron LI 		fprintf(outf, "    numcards %d\n", deck[i].num_cards);
289*11c3b524SAaron LI 		fprintf(outf, "    topcard %d\n", deck[i].top_card);
290*11c3b524SAaron LI 		fprintf(outf, "    gojf_used %d\n", deck[i].gojf_used);
291*11c3b524SAaron LI 		fprintf(outf, "    cards");
292*11c3b524SAaron LI 		for (j = 0; j < deck[i].num_cards; j++)
293*11c3b524SAaron LI 			fprintf(outf, " %d", deck[i].cards[j]);
294*11c3b524SAaron LI 		fprintf(outf, "\n");
295*11c3b524SAaron LI 		fprintf(outf, "}\n");
296*11c3b524SAaron LI 	}
297*11c3b524SAaron LI 
298*11c3b524SAaron LI 	/* Board */
299*11c3b524SAaron LI 	for (i = 0; i < N_SQRS; i++) {
300*11c3b524SAaron LI 		fprintf(outf, "square %d {\n", i);
301*11c3b524SAaron LI 		fprintf(outf, "owner %d\n", board[i].owner);
302*11c3b524SAaron LI 		if (board[i].owner < 0) {
303*11c3b524SAaron LI 			/* nothing */
304*11c3b524SAaron LI 		} else if (board[i].type == PRPTY) {
305*11c3b524SAaron LI 			fprintf(outf, "morg %d\n", board[i].desc->morg);
306*11c3b524SAaron LI 			fprintf(outf, "houses %d\n", board[i].desc->houses);
307*11c3b524SAaron LI 		} else if (board[i].type == RR || board[i].type == UTIL) {
308*11c3b524SAaron LI 			fprintf(outf, "morg %d\n", board[i].desc->morg);
309*11c3b524SAaron LI 		}
310*11c3b524SAaron LI 		fprintf(outf, "}\n");
311*11c3b524SAaron LI 	}
312*11c3b524SAaron LI 	if (ferror(outf) || fflush(outf))
313*11c3b524SAaron LI 		warnx("write error");
314*11c3b524SAaron LI 	fclose(outf);
315*11c3b524SAaron LI 
316*11c3b524SAaron LI 	strcpy(buf, ctime(&t));
317*11c3b524SAaron LI 	for (sp = buf; *sp != '\n'; sp++)
318*11c3b524SAaron LI 		continue;
319*11c3b524SAaron LI 	*sp = '\0';
320*11c3b524SAaron LI 	printf("[%s]\n", buf);
321*11c3b524SAaron LI }
322*11c3b524SAaron LI 
323*11c3b524SAaron LI /*
324*11c3b524SAaron LI  *	This routine restores an old game from a file
325*11c3b524SAaron LI  */
326*11c3b524SAaron LI void
restore(void)327*11c3b524SAaron LI restore(void)
328*11c3b524SAaron LI {
329*11c3b524SAaron LI 	char *sp;
330*11c3b524SAaron LI 
331*11c3b524SAaron LI 	for (;;) {
332*11c3b524SAaron LI 		printf("Which file do you wish to restore from? ");
333*11c3b524SAaron LI 		fgets(buf, sizeof(buf), stdin);
334*11c3b524SAaron LI 		if (feof(stdin))
335*11c3b524SAaron LI 			return;
336*11c3b524SAaron LI 		sp = strchr(buf, '\n');
337*11c3b524SAaron LI 		if (sp)
338*11c3b524SAaron LI 			*sp = '\0';
339*11c3b524SAaron LI 		if (rest_f(buf) == 0)
340*11c3b524SAaron LI 			break;
341*11c3b524SAaron LI 	}
342*11c3b524SAaron LI }
343*11c3b524SAaron LI 
344*11c3b524SAaron LI /*
345*11c3b524SAaron LI  * This does the actual restoring.  It returns zero on success,
346*11c3b524SAaron LI  * and -1 on failure.
347*11c3b524SAaron LI  */
348*11c3b524SAaron LI int
rest_f(const char * file)349*11c3b524SAaron LI rest_f(const char *file)
350*11c3b524SAaron LI {
351*11c3b524SAaron LI 	char *sp;
352*11c3b524SAaron LI 	FILE *inf;
353*11c3b524SAaron LI 	char xbuf[80];
354*11c3b524SAaron LI 	STAT sbuf;
355*11c3b524SAaron LI 	char readbuf[512];
356*11c3b524SAaron LI 	int ret = 0;
357*11c3b524SAaron LI 
358*11c3b524SAaron LI 	inf = fopen(file, "r");
359*11c3b524SAaron LI 	if (inf == NULL) {
360*11c3b524SAaron LI 		warn("%s", file);
361*11c3b524SAaron LI 		return -1;
362*11c3b524SAaron LI 	}
363*11c3b524SAaron LI 	printf("\"%s\" ", file);
364*11c3b524SAaron LI 	if (fstat(fileno(inf), &sbuf) < 0) {
365*11c3b524SAaron LI 		err(1, "%s: fstat", file);
366*11c3b524SAaron LI 	}
367*11c3b524SAaron LI 
368*11c3b524SAaron LI 	/* Clear the game state to prevent brokenness on misordered files. */
369*11c3b524SAaron LI 	reset_game();
370*11c3b524SAaron LI 
371*11c3b524SAaron LI 	/* Reset the parser */
372*11c3b524SAaron LI 	restore_reset();
373*11c3b524SAaron LI 
374*11c3b524SAaron LI 	/* Note: can't use buf[], file might point at it. (Lame...) */
375*11c3b524SAaron LI 	while (fgets(readbuf, sizeof(readbuf), inf)) {
376*11c3b524SAaron LI 		/*
377*11c3b524SAaron LI 		 * The input buffer is long enough to handle anything
378*11c3b524SAaron LI 		 * that's supposed to be in the output buffer, so if
379*11c3b524SAaron LI 		 * we get a partial line, complain.
380*11c3b524SAaron LI 		 */
381*11c3b524SAaron LI 		sp = strchr(readbuf, '\n');
382*11c3b524SAaron LI 		if (sp == NULL) {
383*11c3b524SAaron LI 			printf("file is corrupt: long lines.\n");
384*11c3b524SAaron LI 			ret = -1;
385*11c3b524SAaron LI 			break;
386*11c3b524SAaron LI 		}
387*11c3b524SAaron LI 		*sp = '\0';
388*11c3b524SAaron LI 
389*11c3b524SAaron LI 		if (restore_parseline(readbuf)) {
390*11c3b524SAaron LI 			ret = -1;
391*11c3b524SAaron LI 			break;
392*11c3b524SAaron LI 		}
393*11c3b524SAaron LI 	}
394*11c3b524SAaron LI 
395*11c3b524SAaron LI 	if (ferror(inf))
396*11c3b524SAaron LI 		warnx("%s: read error", file);
397*11c3b524SAaron LI 	fclose(inf);
398*11c3b524SAaron LI 
399*11c3b524SAaron LI 	if (ret < 0)
400*11c3b524SAaron LI 		return -1;
401*11c3b524SAaron LI 
402*11c3b524SAaron LI 	name_list[num_play] = "done";
403*11c3b524SAaron LI 
404*11c3b524SAaron LI 	if (play == NULL || cur_p == NULL || num_play < 2) {
405*11c3b524SAaron LI 		printf("save file is incomplete.\n");
406*11c3b524SAaron LI 		return -1;
407*11c3b524SAaron LI 	}
408*11c3b524SAaron LI 
409*11c3b524SAaron LI 	/*
410*11c3b524SAaron LI 	 * We could at this point crosscheck the following:
411*11c3b524SAaron LI 	 *    - there are only two GOJF cards floating around
412*11c3b524SAaron LI 	 *    - total number of houses and hotels does not exceed maximums
413*11c3b524SAaron LI 	 *    - no props are both built and mortgaged
414*11c3b524SAaron LI 	 * but for now we don't.
415*11c3b524SAaron LI 	 */
416*11c3b524SAaron LI 
417*11c3b524SAaron LI 	strcpy(xbuf, ctime(&sbuf.st_mtime));
418*11c3b524SAaron LI 	for (sp = xbuf; *sp != '\n'; sp++)
419*11c3b524SAaron LI 		continue;
420*11c3b524SAaron LI 	*sp = '\0';
421*11c3b524SAaron LI 	printf("[%s]\n", xbuf);
422*11c3b524SAaron LI 	return 0;
423*11c3b524SAaron LI }
424*11c3b524SAaron LI 
425*11c3b524SAaron LI /*
426*11c3b524SAaron LI  * State of the restore parser
427*11c3b524SAaron LI  */
428*11c3b524SAaron LI static int restore_version;
429*11c3b524SAaron LI static enum {
430*11c3b524SAaron LI 	RI_NONE,
431*11c3b524SAaron LI 	RI_PLAYER,
432*11c3b524SAaron LI 	RI_DECK,
433*11c3b524SAaron LI 	RI_SQUARE
434*11c3b524SAaron LI } restore_item;
435*11c3b524SAaron LI static int restore_itemnum;
436*11c3b524SAaron LI 
437*11c3b524SAaron LI /*
438*11c3b524SAaron LI  * Reset the restore parser
439*11c3b524SAaron LI  */
440*11c3b524SAaron LI static void
restore_reset(void)441*11c3b524SAaron LI restore_reset(void)
442*11c3b524SAaron LI {
443*11c3b524SAaron LI 	restore_version = -1;
444*11c3b524SAaron LI 	restore_item = RI_NONE;
445*11c3b524SAaron LI 	restore_itemnum = -1;
446*11c3b524SAaron LI }
447*11c3b524SAaron LI 
448*11c3b524SAaron LI /*
449*11c3b524SAaron LI  * Handle one line of the save file
450*11c3b524SAaron LI  */
451*11c3b524SAaron LI static int
restore_parseline(char * txt)452*11c3b524SAaron LI restore_parseline(char *txt)
453*11c3b524SAaron LI {
454*11c3b524SAaron LI 	char *attribute;
455*11c3b524SAaron LI 	char *s;
456*11c3b524SAaron LI 
457*11c3b524SAaron LI 	if (restore_version < 0) {
458*11c3b524SAaron LI 		/* Haven't seen the header yet. Demand it right away. */
459*11c3b524SAaron LI 		if (!strncmp(txt, "NetBSD monop format v", 21)) {
460*11c3b524SAaron LI 			return getnum("format version", txt+21,
461*11c3b524SAaron LI 				      MIN_FORMAT_VERSION,
462*11c3b524SAaron LI 				      MAX_FORMAT_VERSION,
463*11c3b524SAaron LI 				      &restore_version);
464*11c3b524SAaron LI 		}
465*11c3b524SAaron LI 		printf("file is not a monop save file.\n");
466*11c3b524SAaron LI 		return -1;
467*11c3b524SAaron LI 	}
468*11c3b524SAaron LI 
469*11c3b524SAaron LI 	/* Check for lines that are right braces. */
470*11c3b524SAaron LI 	if (!strcmp(txt, "}")) {
471*11c3b524SAaron LI 		if (restore_item == RI_NONE) {
472*11c3b524SAaron LI 			printf("mismatched close brace.\n");
473*11c3b524SAaron LI 			return -1;
474*11c3b524SAaron LI 		}
475*11c3b524SAaron LI 		restore_item = RI_NONE;
476*11c3b524SAaron LI 		restore_itemnum = -1;
477*11c3b524SAaron LI 		return 0;
478*11c3b524SAaron LI 	}
479*11c3b524SAaron LI 
480*11c3b524SAaron LI 	/* Any other line must begin with a word, which is the attribute. */
481*11c3b524SAaron LI 	s = txt;
482*11c3b524SAaron LI 	while (*s==' ')
483*11c3b524SAaron LI 		s++;
484*11c3b524SAaron LI 	attribute = s;
485*11c3b524SAaron LI 	s = strchr(attribute, ' ');
486*11c3b524SAaron LI 	if (s == NULL) {
487*11c3b524SAaron LI 		printf("file is corrupt: attribute %s lacks value.\n",
488*11c3b524SAaron LI 		    attribute);
489*11c3b524SAaron LI 		return -1;
490*11c3b524SAaron LI 	}
491*11c3b524SAaron LI 	*(s++) = '\0';
492*11c3b524SAaron LI 	while (*s==' ')
493*11c3b524SAaron LI 		s++;
494*11c3b524SAaron LI 	/* keep the remaining text for further handling */
495*11c3b524SAaron LI 	txt = s;
496*11c3b524SAaron LI 
497*11c3b524SAaron LI 	switch (restore_item) {
498*11c3b524SAaron LI 	    case RI_NONE:
499*11c3b524SAaron LI 		/* toplevel attributes */
500*11c3b524SAaron LI 		return restore_toplevel_attr(attribute, txt);
501*11c3b524SAaron LI 
502*11c3b524SAaron LI 	    case RI_PLAYER:
503*11c3b524SAaron LI 		/* player attributes */
504*11c3b524SAaron LI 		return restore_player_attr(attribute, txt);
505*11c3b524SAaron LI 
506*11c3b524SAaron LI 	    case RI_DECK:
507*11c3b524SAaron LI 		/* deck attributes */
508*11c3b524SAaron LI 		return restore_deck_attr(attribute, txt);
509*11c3b524SAaron LI 
510*11c3b524SAaron LI 	    case RI_SQUARE:
511*11c3b524SAaron LI 		/* board square attributes */
512*11c3b524SAaron LI 		return restore_square_attr(attribute, txt);
513*11c3b524SAaron LI 	}
514*11c3b524SAaron LI 	/* NOTREACHED */
515*11c3b524SAaron LI 	printf("internal logic error\n");
516*11c3b524SAaron LI 	return -1;
517*11c3b524SAaron LI }
518*11c3b524SAaron LI 
519*11c3b524SAaron LI static int
restore_toplevel_attr(const char * attribute,char * txt)520*11c3b524SAaron LI restore_toplevel_attr(const char *attribute, char *txt)
521*11c3b524SAaron LI {
522*11c3b524SAaron LI 	if (!strcmp(attribute, "time")) {
523*11c3b524SAaron LI 		/* nothing */
524*11c3b524SAaron LI 	} else if (!strcmp(attribute, "numplayers")) {
525*11c3b524SAaron LI 		if (getnum("numplayers", txt, 2, MAX_PL, &num_play) < 0) {
526*11c3b524SAaron LI 			return -1;
527*11c3b524SAaron LI 		}
528*11c3b524SAaron LI 		if (play != NULL) {
529*11c3b524SAaron LI 			printf("numplayers: multiple settings\n");
530*11c3b524SAaron LI 			return -1;
531*11c3b524SAaron LI 		}
532*11c3b524SAaron LI 		play = calloc((size_t)num_play, sizeof(play[0]));
533*11c3b524SAaron LI 		if (play == NULL) {
534*11c3b524SAaron LI 			err(1, "calloc");
535*11c3b524SAaron LI 		}
536*11c3b524SAaron LI 	} else if (!strcmp(attribute, "currentplayer")) {
537*11c3b524SAaron LI 		if (getnum("currentplayer", txt, 0, num_play-1, &player) < 0) {
538*11c3b524SAaron LI 			return -1;
539*11c3b524SAaron LI 		}
540*11c3b524SAaron LI 		if (play == NULL) {
541*11c3b524SAaron LI 			printf("currentplayer: before numplayers\n");
542*11c3b524SAaron LI 			return -1;
543*11c3b524SAaron LI 		}
544*11c3b524SAaron LI 		cur_p = &play[player];
545*11c3b524SAaron LI 	} else if (!strcmp(attribute, "doubles")) {
546*11c3b524SAaron LI 		if (getnum("doubles", txt, 0, 2, &num_doub) < 0) {
547*11c3b524SAaron LI 			return -1;
548*11c3b524SAaron LI 		}
549*11c3b524SAaron LI 	} else if (!strcmp(attribute, "player")) {
550*11c3b524SAaron LI 		if (getnum_withbrace("player", txt, 0, num_play-1,
551*11c3b524SAaron LI 		    &restore_itemnum) < 0) {
552*11c3b524SAaron LI 			return -1;
553*11c3b524SAaron LI 		}
554*11c3b524SAaron LI 		restore_item = RI_PLAYER;
555*11c3b524SAaron LI 	} else if (!strcmp(attribute, "deck")) {
556*11c3b524SAaron LI 		if (getnum_withbrace("deck", txt, 0, 1,
557*11c3b524SAaron LI 		    &restore_itemnum) < 0) {
558*11c3b524SAaron LI 			return -1;
559*11c3b524SAaron LI 		}
560*11c3b524SAaron LI 		restore_item = RI_DECK;
561*11c3b524SAaron LI 	} else if (!strcmp(attribute, "square")) {
562*11c3b524SAaron LI 		if (getnum_withbrace("square", txt, 0, N_SQRS-1,
563*11c3b524SAaron LI 		    &restore_itemnum) < 0) {
564*11c3b524SAaron LI 			return -1;
565*11c3b524SAaron LI 		}
566*11c3b524SAaron LI 		restore_item = RI_SQUARE;
567*11c3b524SAaron LI 	} else {
568*11c3b524SAaron LI 		printf("unknown attribute %s\n", attribute);
569*11c3b524SAaron LI 		return -1;
570*11c3b524SAaron LI 	}
571*11c3b524SAaron LI 	return 0;
572*11c3b524SAaron LI }
573*11c3b524SAaron LI 
574*11c3b524SAaron LI static int
restore_player_attr(const char * attribute,char * txt)575*11c3b524SAaron LI restore_player_attr(const char *attribute, char *txt)
576*11c3b524SAaron LI {
577*11c3b524SAaron LI 	PLAY *pp;
578*11c3b524SAaron LI 	int tmp;
579*11c3b524SAaron LI 
580*11c3b524SAaron LI 	if (play == NULL) {
581*11c3b524SAaron LI 		printf("player came before numplayers.\n");
582*11c3b524SAaron LI 		return -1;
583*11c3b524SAaron LI 	}
584*11c3b524SAaron LI 	pp = &play[restore_itemnum];
585*11c3b524SAaron LI 
586*11c3b524SAaron LI 	if (!strcmp(attribute, "name")) {
587*11c3b524SAaron LI 		if (pp->name != NULL) {
588*11c3b524SAaron LI 			printf("player has multiple names.\n");
589*11c3b524SAaron LI 			return -1;
590*11c3b524SAaron LI 		}
591*11c3b524SAaron LI 		/* XXX should really systematize the max name length */
592*11c3b524SAaron LI 		if (strlen(txt) > 256) {
593*11c3b524SAaron LI 			txt[256] = 0;
594*11c3b524SAaron LI 		}
595*11c3b524SAaron LI 		pp->name = strdup(txt);
596*11c3b524SAaron LI 		if (pp->name == NULL)
597*11c3b524SAaron LI 			err(1, "strdup");
598*11c3b524SAaron LI 		name_list[restore_itemnum] = pp->name;
599*11c3b524SAaron LI 	} else if (!strcmp(attribute, "money")) {
600*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, INT_MAX, &pp->money) < 0) {
601*11c3b524SAaron LI 			return -1;
602*11c3b524SAaron LI 		}
603*11c3b524SAaron LI 	} else if (!strcmp(attribute, "loc")) {
604*11c3b524SAaron LI 		/* note: not N_SQRS-1 */
605*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, N_SQRS, &tmp) < 0) {
606*11c3b524SAaron LI 			return -1;
607*11c3b524SAaron LI 		}
608*11c3b524SAaron LI 		pp->loc = tmp;
609*11c3b524SAaron LI 	} else if (!strcmp(attribute, "num_gojf")) {
610*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, 2, &tmp) < 0) {
611*11c3b524SAaron LI 			return -1;
612*11c3b524SAaron LI 		}
613*11c3b524SAaron LI 		pp->num_gojf = tmp;
614*11c3b524SAaron LI 	} else if (!strcmp(attribute, "in_jail")) {
615*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, 3, &tmp) < 0) {
616*11c3b524SAaron LI 			return -1;
617*11c3b524SAaron LI 		}
618*11c3b524SAaron LI 		pp->in_jail = tmp;
619*11c3b524SAaron LI 		if (pp->in_jail > 0 && pp->loc != JAIL) {
620*11c3b524SAaron LI 			printf("player escaped from jail?\n");
621*11c3b524SAaron LI 			return -1;
622*11c3b524SAaron LI 		}
623*11c3b524SAaron LI 	} else {
624*11c3b524SAaron LI 		printf("unknown attribute %s\n", attribute);
625*11c3b524SAaron LI 		return -1;
626*11c3b524SAaron LI 	}
627*11c3b524SAaron LI 	return 0;
628*11c3b524SAaron LI }
629*11c3b524SAaron LI 
630*11c3b524SAaron LI static int
restore_deck_attr(const char * attribute,char * txt)631*11c3b524SAaron LI restore_deck_attr(const char *attribute, char *txt)
632*11c3b524SAaron LI {
633*11c3b524SAaron LI 	int tmp, j;
634*11c3b524SAaron LI 	char *s;
635*11c3b524SAaron LI 	DECK *dp;
636*11c3b524SAaron LI 
637*11c3b524SAaron LI 	dp = &deck[restore_itemnum];
638*11c3b524SAaron LI 
639*11c3b524SAaron LI 	if (!strcmp(attribute, "numcards")) {
640*11c3b524SAaron LI 		if (getnum(attribute, txt, dp->num_cards, dp->num_cards,
641*11c3b524SAaron LI 		    &tmp) < 0) {
642*11c3b524SAaron LI 			return -1;
643*11c3b524SAaron LI 		}
644*11c3b524SAaron LI 	} else if (!strcmp(attribute, "topcard")) {
645*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, dp->num_cards,
646*11c3b524SAaron LI 		    &dp->top_card) < 0) {
647*11c3b524SAaron LI 			return -1;
648*11c3b524SAaron LI 		}
649*11c3b524SAaron LI 	} else if (!strcmp(attribute, "gojf_used")) {
650*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
651*11c3b524SAaron LI 			return -1;
652*11c3b524SAaron LI 		}
653*11c3b524SAaron LI 		dp->gojf_used = tmp;
654*11c3b524SAaron LI 	} else if (!strcmp(attribute, "cards")) {
655*11c3b524SAaron LI 		errno = 0;
656*11c3b524SAaron LI 		s = txt;
657*11c3b524SAaron LI 		for (j = 0; j<dp->num_cards; j++) {
658*11c3b524SAaron LI 			tmp = strtol(s, &s, 10);
659*11c3b524SAaron LI 			if (tmp < 0 || tmp >= dp->num_cards) {
660*11c3b524SAaron LI 				printf("cards: out of range value\n");
661*11c3b524SAaron LI 				return -1;
662*11c3b524SAaron LI 			}
663*11c3b524SAaron LI 			dp->cards[j] = tmp;
664*11c3b524SAaron LI 		}
665*11c3b524SAaron LI 		if (errno) {
666*11c3b524SAaron LI 			printf("cards: invalid values\n");
667*11c3b524SAaron LI 			return -1;
668*11c3b524SAaron LI 		}
669*11c3b524SAaron LI 	} else {
670*11c3b524SAaron LI 		printf("unknown attribute %s\n", attribute);
671*11c3b524SAaron LI 		return -1;
672*11c3b524SAaron LI 	}
673*11c3b524SAaron LI 	return 0;
674*11c3b524SAaron LI }
675*11c3b524SAaron LI 
676*11c3b524SAaron LI static int
restore_square_attr(const char * attribute,char * txt)677*11c3b524SAaron LI restore_square_attr(const char *attribute, char *txt)
678*11c3b524SAaron LI {
679*11c3b524SAaron LI 	SQUARE *sp = &board[restore_itemnum];
680*11c3b524SAaron LI 	int tmp;
681*11c3b524SAaron LI 
682*11c3b524SAaron LI 	if (!strcmp(attribute, "owner")) {
683*11c3b524SAaron LI 		if (getnum(attribute, txt, -1, num_play-1, &tmp) < 0) {
684*11c3b524SAaron LI 			return -1;
685*11c3b524SAaron LI 		}
686*11c3b524SAaron LI 		sp->owner = tmp;
687*11c3b524SAaron LI 		if (tmp >= 0)
688*11c3b524SAaron LI 			add_list(tmp, &play[tmp].own_list, restore_itemnum);
689*11c3b524SAaron LI 	} else if (!strcmp(attribute, "morg")) {
690*11c3b524SAaron LI 		if (sp->type != PRPTY && sp->type != RR && sp->type != UTIL) {
691*11c3b524SAaron LI 			printf("unownable property is mortgaged.\n");
692*11c3b524SAaron LI 			return -1;
693*11c3b524SAaron LI 		}
694*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
695*11c3b524SAaron LI 			return -1;
696*11c3b524SAaron LI 		}
697*11c3b524SAaron LI 		sp->desc->morg = tmp;
698*11c3b524SAaron LI 	} else if (!strcmp(attribute, "houses")) {
699*11c3b524SAaron LI 		if (sp->type != PRPTY) {
700*11c3b524SAaron LI 			printf("unbuildable property has houses.\n");
701*11c3b524SAaron LI 			return -1;
702*11c3b524SAaron LI 		}
703*11c3b524SAaron LI 		if (getnum(attribute, txt, 0, 5, &tmp) < 0) {
704*11c3b524SAaron LI 			return -1;
705*11c3b524SAaron LI 		}
706*11c3b524SAaron LI 		sp->desc->houses = tmp;
707*11c3b524SAaron LI 	} else {
708*11c3b524SAaron LI 		printf("unknown attribute %s\n", attribute);
709*11c3b524SAaron LI 		return -1;
710*11c3b524SAaron LI 	}
711*11c3b524SAaron LI 	return 0;
712*11c3b524SAaron LI }
713*11c3b524SAaron LI 
714*11c3b524SAaron LI static int
getnum(const char * what,char * txt,int min,int max,int * ret)715*11c3b524SAaron LI getnum(const char *what, char *txt, int min, int max, int *ret)
716*11c3b524SAaron LI {
717*11c3b524SAaron LI 	char *s;
718*11c3b524SAaron LI 	long l;
719*11c3b524SAaron LI 
720*11c3b524SAaron LI 	errno = 0;
721*11c3b524SAaron LI 	l = strtol(txt, &s, 10);
722*11c3b524SAaron LI 	if (errno || strlen(s)>0) {
723*11c3b524SAaron LI 		printf("%s: not a number.\n", what);
724*11c3b524SAaron LI 		return -1;
725*11c3b524SAaron LI 	}
726*11c3b524SAaron LI 	if (l < min || l > max) {
727*11c3b524SAaron LI 		printf("%s: out of range.\n", what);
728*11c3b524SAaron LI 	}
729*11c3b524SAaron LI 	*ret = l;
730*11c3b524SAaron LI 	return 0;
731*11c3b524SAaron LI }
732*11c3b524SAaron LI 
733*11c3b524SAaron LI static int
getnum_withbrace(const char * what,char * txt,int min,int max,int * ret)734*11c3b524SAaron LI getnum_withbrace(const char *what, char *txt, int min, int max, int *ret)
735*11c3b524SAaron LI {
736*11c3b524SAaron LI 	char *s;
737*11c3b524SAaron LI 	s = strchr(txt, ' ');
738*11c3b524SAaron LI 	if (s == NULL) {
739*11c3b524SAaron LI 		printf("%s: expected open brace\n", what);
740*11c3b524SAaron LI 		return -1;
741*11c3b524SAaron LI 	}
742*11c3b524SAaron LI 	*(s++) = '\0';
743*11c3b524SAaron LI 	while (*s == ' ')
744*11c3b524SAaron LI 		s++;
745*11c3b524SAaron LI 	if (*s != '{') {
746*11c3b524SAaron LI 		printf("%s: expected open brace\n", what);
747*11c3b524SAaron LI 		return -1;
748*11c3b524SAaron LI 	}
749*11c3b524SAaron LI 	if (s[1] != 0) {
750*11c3b524SAaron LI 		printf("%s: garbage after open brace\n", what);
751*11c3b524SAaron LI 		return -1;
752*11c3b524SAaron LI 	}
753*11c3b524SAaron LI 	return getnum(what, txt, min, max, ret);
754*11c3b524SAaron LI }
755