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