Home | History | Annotate | Line # | Download | only in monop
misc.c revision 1.2
      1 /*
      2  * Copyright (c) 1980 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)misc.c	5.5 (Berkeley) 2/28/91";*/
     36 static char rcsid[] = "$Id: misc.c,v 1.2 1993/08/01 18:53:37 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 # include	"monop.ext"
     40 # include	<ctype.h>
     41 # include	<signal.h>
     42 
     43 # define	execsh(sh)	execl(sh, shell_name[roll(1, num_names)-1], 0)
     44 
     45 static char	*shell_def	= "/bin/csh",
     46 		*shell_name[]	= {
     47 			".Hi Mom!",
     48 			".Kick Me",
     49 			".I'm really the next process down",
     50 			".Hi Kids!",
     51 			".This space for rent",
     52 			".Singin' in the rain....",
     53 			".I am but a Cog in the Wheel of Life",
     54 			".Look out!!! Behind you!!!!!",
     55 			".Looking for a good time, sailor?",
     56 			".I don't get NO respect...",
     57 			".Augghh!  You peeked!"
     58 		};
     59 
     60 static int	num_names	= sizeof shell_name / sizeof (char *);;
     61 
     62 char	*shell_in();
     63 
     64 /*
     65  *	This routine executes a truncated set of commands until a
     66  * "yes or "no" answer is gotten.
     67  */
     68 getyn(prompt)
     69 reg char	*prompt; {
     70 
     71 	reg int	com;
     72 
     73 	for (;;)
     74 		if ((com=getinp(prompt, yn)) < 2)
     75 			return com;
     76 		else
     77 			(*func[com-2])();
     78 }
     79 /*
     80  *	This routine tells the player if he's out of money.
     81  */
     82 notify() {
     83 
     84 	if (cur_p->money < 0)
     85 		printf("That leaves you $%d in debt\n", -cur_p->money);
     86 	else if (cur_p->money == 0)
     87 		printf("that leaves you broke\n");
     88 	else if (fixing && !told_em && cur_p->money > 0) {
     89 		printf("-- You are now Solvent ---\n");
     90 		told_em = TRUE;
     91 	}
     92 }
     93 /*
     94  *	This routine switches to the next player
     95  */
     96 next_play() {
     97 
     98 	player = ++player % num_play;
     99 	cur_p = &play[player];
    100 	num_doub = 0;
    101 }
    102 /*
    103  *	This routine gets an integer from the keyboard after the
    104  * given prompt.
    105  */
    106 get_int(prompt)
    107 reg char	*prompt; {
    108 
    109 	reg int		num;
    110 	reg char	*sp;
    111 	char		buf[257];
    112 
    113 	for (;;) {
    114 inter:
    115 		printf(prompt);
    116 		num = 0;
    117 		for (sp = buf; (*sp=getchar()) != '\n'; sp++)
    118 			if (*sp == -1)	/* check for interrupted system call */
    119 				goto inter;
    120 		if (sp == buf)
    121 			continue;
    122 		for (sp = buf; isspace(*sp); sp++)
    123 			continue;
    124 		for (; isdigit(*sp); sp++)
    125 			num = num * 10 + *sp - '0';
    126 		if (*sp == '\n')
    127 			return num;
    128 		else
    129 			printf("I can't understand that\n");
    130 	}
    131 }
    132 /*
    133  *	This routine sets the monopoly flag from the list given.
    134  */
    135 set_ownlist(pl)
    136 int	pl; {
    137 
    138 	reg int	num;		/* general counter		*/
    139 	reg MON	*orig;		/* remember starting monop ptr	*/
    140 	reg OWN	*op;		/* current owned prop		*/
    141 	OWN	*orig_op;		/* origianl prop before loop	*/
    142 
    143 	op = play[pl].own_list;
    144 #ifdef DEBUG
    145 	printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
    146 #endif
    147 	while (op) {
    148 #ifdef DEBUG
    149 		printf("op->sqr->type = %d\n", op->sqr->type);
    150 #endif
    151 		switch (op->sqr->type) {
    152 		  case UTIL:
    153 #ifdef DEBUG
    154 			printf("  case UTIL:\n");
    155 #endif
    156 			for (num = 0; op && op->sqr->type == UTIL; op = op->next)
    157 				num++;
    158 			play[pl].num_util = num;
    159 #ifdef DEBUG
    160 			printf("play[pl].num_util = num [%d];\n", num);
    161 #endif
    162 			break;
    163 		  case RR:
    164 #ifdef DEBUG
    165 			printf("  case RR:\n");
    166 #endif
    167 			for (num = 0; op && op->sqr->type == RR; op = op->next) {
    168 #ifdef DEBUG
    169 				printf("iter: %d\n", num);
    170 				printf("op = %d, op->sqr = %d, op->sqr->type = %d\n", op, op->sqr, op->sqr->type);
    171 #endif
    172 				num++;
    173 			}
    174 			play[pl].num_rr = num;
    175 #ifdef DEBUG
    176 			printf("play[pl].num_rr = num [%d];\n", num);
    177 #endif
    178 			break;
    179 		  case PRPTY:
    180 #ifdef DEBUG
    181 			printf("  case PRPTY:\n");
    182 #endif
    183 			orig = op->sqr->desc->mon_desc;
    184 			orig_op = op;
    185 			num = 0;
    186 			while (op && op->sqr->desc->mon_desc == orig) {
    187 #ifdef DEBUG
    188 				printf("iter: %d\n", num);
    189 #endif
    190 				num++;
    191 #ifdef DEBUG
    192 				printf("op = op->next ");
    193 #endif
    194 				op = op->next;
    195 #ifdef DEBUG
    196 				printf("[%d];\n", op);
    197 #endif
    198 			}
    199 #ifdef DEBUG
    200 			printf("num = %d\n");
    201 #endif
    202 			if (orig == 0) {
    203 				printf("panic:  bad monopoly descriptor: orig = %d\n", orig);
    204 				printf("player # %d\n", pl+1);
    205 				printhold(pl);
    206 				printf("orig_op = %d\n", orig_op);
    207 				printf("orig_op->sqr->type = %d (PRPTY)\n", op->sqr->type);
    208 				printf("orig_op->next = %d\n", op->next);
    209 				printf("orig_op->sqr->desc = %d\n", op->sqr->desc);
    210 				printf("op = %d\n", op);
    211 				printf("op->sqr->type = %d (PRPTY)\n", op->sqr->type);
    212 				printf("op->next = %d\n", op->next);
    213 				printf("op->sqr->desc = %d\n", op->sqr->desc);
    214 				printf("num = %d\n", num);
    215 			}
    216 #ifdef DEBUG
    217 			printf("orig->num_in = %d\n", orig->num_in);
    218 #endif
    219 			if (num == orig->num_in)
    220 				is_monop(orig, pl);
    221 			else
    222 				isnot_monop(orig);
    223 			break;
    224 		}
    225 	}
    226 }
    227 /*
    228  *	This routine sets things up as if it is a new monopoly
    229  */
    230 is_monop(mp, pl)
    231 reg MON	*mp;
    232 int	pl; {
    233 
    234 	reg char	*sp;
    235 	reg int		i;
    236 
    237 	mp->owner = pl;
    238 	mp->num_own = mp->num_in;
    239 	for (i = 0; i < mp->num_in; i++)
    240 		mp->sq[i]->desc->monop = TRUE;
    241 	mp->name = mp->mon_n;
    242 }
    243 /*
    244  *	This routine sets things up as if it is no longer a monopoly
    245  */
    246 isnot_monop(mp)
    247 reg MON	*mp; {
    248 
    249 	reg char	*sp;
    250 	reg int		i;
    251 
    252 	mp->owner = -1;
    253 	for (i = 0; i < mp->num_in; i++)
    254 		mp->sq[i]->desc->monop = FALSE;
    255 	mp->name = mp->not_m;
    256 }
    257 /*
    258  *	This routine gives a list of the current player's routine
    259  */
    260 list() {
    261 
    262 	printhold(player);
    263 }
    264 /*
    265  *	This routine gives a list of a given players holdings
    266  */
    267 list_all() {
    268 
    269 	reg int	pl;
    270 
    271 	while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play)
    272 		printhold(pl);
    273 }
    274 /*
    275  *	This routine gives the players a chance before it exits.
    276  */
    277 void
    278 quit() {
    279 
    280 	putchar('\n');
    281 	if (getyn("Do you all really want to quit? ", yn) == 0)
    282 		exit(0);
    283 	signal(SIGINT, quit);
    284 }
    285 /*
    286  *	This routine copies one structure to another
    287  */
    288 cpy_st(s1, s2, size)
    289 reg int	*s1, *s2, size; {
    290 
    291 	size /= 2;
    292 	while (size--)
    293 		*s1++ = *s2++;
    294 }
    295 /*
    296  *	This routine forks off a shell.  It uses the users login shell
    297  */
    298 shell_out() {
    299 
    300 	static char	*shell = NULL;
    301 
    302 	printline();
    303 	if (shell == NULL)
    304 		shell = shell_in();
    305 	fflush(stdout);
    306 	if (!fork()) {
    307 		signal(SIGINT, SIG_DFL);
    308 		execsh(shell);
    309 	}
    310 	ignoresigs();
    311 	wait();
    312 	resetsigs();
    313 	putchar('\n');
    314 	printline();
    315 }
    316 /*
    317  *	This routine looks up the users login shell
    318  */
    319 # include	<sys/types.h>
    320 # include	<pwd.h>
    321 
    322 char		*getenv();
    323 
    324 char *
    325 shell_in() {
    326 
    327 	reg struct passwd	*pp;
    328 	reg char		*sp;
    329 
    330 	if ((sp = getenv("SHELL")) == NULL) {
    331 		pp = getpwuid(getuid());
    332 		if (pp->pw_shell[0] != '\0')
    333 			return pp->pw_shell;
    334 		else
    335 			return shell_def;
    336 		/*return (*(pp->pw_shell) != '\0' ? pp->pw_shell : shell_def);*/
    337 	}
    338 	return sp;
    339 }
    340 /*
    341  *	This routine sets things up to ignore all the signals.
    342  */
    343 ignoresigs() {
    344 
    345 	reg int	i;
    346 
    347 	for (i = 0; i < NSIG; i++)
    348 		signal(i, SIG_IGN);
    349 }
    350 /*
    351  *	This routine sets up things as they were before.
    352  */
    353 resetsigs() {
    354 
    355 	reg int	i;
    356 
    357 	for (i = 0; i < NSIG; i++)
    358 		signal(i, SIG_DFL);
    359 	signal(SIGINT, quit);
    360 }
    361