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