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