xref: /netbsd-src/games/monop/misc.c (revision ce63d6c20fc4ec8ddc95c84bb229e3c4ecf82b69)
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[] = "@(#)misc.c	5.5 (Berkeley) 2/28/91";
36 #endif /* not lint */
37 
38 # include	"monop.ext"
39 # include	<ctype.h>
40 # include	<signal.h>
41 
42 # define	execsh(sh)	execl(sh, shell_name[roll(1, num_names)-1], 0)
43 
44 static char	*shell_def	= "/bin/csh",
45 		*shell_name[]	= {
46 			".Hi Mom!",
47 			".Kick Me",
48 			".I'm really the next process down",
49 			".Hi Kids!",
50 			".This space for rent",
51 			".Singin' in the rain....",
52 			".I am but a Cog in the Wheel of Life",
53 			".Look out!!! Behind you!!!!!",
54 			".Looking for a good time, sailor?",
55 			".I don't get NO respect...",
56 			".Augghh!  You peeked!"
57 		};
58 
59 static int	num_names	= sizeof shell_name / sizeof (char *);;
60 
61 char	*shell_in();
62 
63 /*
64  *	This routine executes a truncated set of commands until a
65  * "yes or "no" answer is gotten.
66  */
67 getyn(prompt)
68 reg char	*prompt; {
69 
70 	reg int	com;
71 
72 	for (;;)
73 		if ((com=getinp(prompt, yn)) < 2)
74 			return com;
75 		else
76 			(*func[com-2])();
77 }
78 /*
79  *	This routine tells the player if he's out of money.
80  */
81 notify() {
82 
83 	if (cur_p->money < 0)
84 		printf("That leaves you $%d in debt\n", -cur_p->money);
85 	else if (cur_p->money == 0)
86 		printf("that leaves you broke\n");
87 	else if (fixing && !told_em && cur_p->money > 0) {
88 		printf("-- You are now Solvent ---\n");
89 		told_em = TRUE;
90 	}
91 }
92 /*
93  *	This routine switches to the next player
94  */
95 next_play() {
96 
97 	player = ++player % num_play;
98 	cur_p = &play[player];
99 	num_doub = 0;
100 }
101 /*
102  *	This routine gets an integer from the keyboard after the
103  * given prompt.
104  */
105 get_int(prompt)
106 reg char	*prompt; {
107 
108 	reg int		num;
109 	reg char	*sp;
110 	char		buf[257];
111 
112 	for (;;) {
113 inter:
114 		printf(prompt);
115 		num = 0;
116 		for (sp = buf; (*sp=getchar()) != '\n'; sp++)
117 			if (*sp == -1)	/* check for interrupted system call */
118 				goto inter;
119 		if (sp == buf)
120 			continue;
121 		for (sp = buf; isspace(*sp); sp++)
122 			continue;
123 		for (; isdigit(*sp); sp++)
124 			num = num * 10 + *sp - '0';
125 		if (*sp == '\n')
126 			return num;
127 		else
128 			printf("I can't understand that\n");
129 	}
130 }
131 /*
132  *	This routine sets the monopoly flag from the list given.
133  */
134 set_ownlist(pl)
135 int	pl; {
136 
137 	reg int	num;		/* general counter		*/
138 	reg MON	*orig;		/* remember starting monop ptr	*/
139 	reg OWN	*op;		/* current owned prop		*/
140 	OWN	*orig_op;		/* origianl prop before loop	*/
141 
142 	op = play[pl].own_list;
143 #ifdef DEBUG
144 	printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
145 #endif
146 	while (op) {
147 #ifdef DEBUG
148 		printf("op->sqr->type = %d\n", op->sqr->type);
149 #endif
150 		switch (op->sqr->type) {
151 		  case UTIL:
152 #ifdef DEBUG
153 			printf("  case UTIL:\n");
154 #endif
155 			for (num = 0; op && op->sqr->type == UTIL; op = op->next)
156 				num++;
157 			play[pl].num_util = num;
158 #ifdef DEBUG
159 			printf("play[pl].num_util = num [%d];\n", num);
160 #endif
161 			break;
162 		  case RR:
163 #ifdef DEBUG
164 			printf("  case RR:\n");
165 #endif
166 			for (num = 0; op && op->sqr->type == RR; op = op->next) {
167 #ifdef DEBUG
168 				printf("iter: %d\n", num);
169 				printf("op = %d, op->sqr = %d, op->sqr->type = %d\n", op, op->sqr, op->sqr->type);
170 #endif
171 				num++;
172 			}
173 			play[pl].num_rr = num;
174 #ifdef DEBUG
175 			printf("play[pl].num_rr = num [%d];\n", num);
176 #endif
177 			break;
178 		  case PRPTY:
179 #ifdef DEBUG
180 			printf("  case PRPTY:\n");
181 #endif
182 			orig = op->sqr->desc->mon_desc;
183 			orig_op = op;
184 			num = 0;
185 			while (op && op->sqr->desc->mon_desc == orig) {
186 #ifdef DEBUG
187 				printf("iter: %d\n", num);
188 #endif
189 				num++;
190 #ifdef DEBUG
191 				printf("op = op->next ");
192 #endif
193 				op = op->next;
194 #ifdef DEBUG
195 				printf("[%d];\n", op);
196 #endif
197 			}
198 #ifdef DEBUG
199 			printf("num = %d\n");
200 #endif
201 			if (orig == 0) {
202 				printf("panic:  bad monopoly descriptor: orig = %d\n", orig);
203 				printf("player # %d\n", pl+1);
204 				printhold(pl);
205 				printf("orig_op = %d\n", orig_op);
206 				printf("orig_op->sqr->type = %d (PRPTY)\n", op->sqr->type);
207 				printf("orig_op->next = %d\n", op->next);
208 				printf("orig_op->sqr->desc = %d\n", op->sqr->desc);
209 				printf("op = %d\n", op);
210 				printf("op->sqr->type = %d (PRPTY)\n", op->sqr->type);
211 				printf("op->next = %d\n", op->next);
212 				printf("op->sqr->desc = %d\n", op->sqr->desc);
213 				printf("num = %d\n", num);
214 			}
215 #ifdef DEBUG
216 			printf("orig->num_in = %d\n", orig->num_in);
217 #endif
218 			if (num == orig->num_in)
219 				is_monop(orig, pl);
220 			else
221 				isnot_monop(orig);
222 			break;
223 		}
224 	}
225 }
226 /*
227  *	This routine sets things up as if it is a new monopoly
228  */
229 is_monop(mp, pl)
230 reg MON	*mp;
231 int	pl; {
232 
233 	reg char	*sp;
234 	reg int		i;
235 
236 	mp->owner = pl;
237 	mp->num_own = mp->num_in;
238 	for (i = 0; i < mp->num_in; i++)
239 		mp->sq[i]->desc->monop = TRUE;
240 	mp->name = mp->mon_n;
241 }
242 /*
243  *	This routine sets things up as if it is no longer a monopoly
244  */
245 isnot_monop(mp)
246 reg MON	*mp; {
247 
248 	reg char	*sp;
249 	reg int		i;
250 
251 	mp->owner = -1;
252 	for (i = 0; i < mp->num_in; i++)
253 		mp->sq[i]->desc->monop = FALSE;
254 	mp->name = mp->not_m;
255 }
256 /*
257  *	This routine gives a list of the current player's routine
258  */
259 list() {
260 
261 	printhold(player);
262 }
263 /*
264  *	This routine gives a list of a given players holdings
265  */
266 list_all() {
267 
268 	reg int	pl;
269 
270 	while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play)
271 		printhold(pl);
272 }
273 /*
274  *	This routine gives the players a chance before it exits.
275  */
276 void
277 quit() {
278 
279 	putchar('\n');
280 	if (getyn("Do you all really want to quit? ", yn) == 0)
281 		exit(0);
282 	signal(SIGINT, quit);
283 }
284 /*
285  *	This routine copies one structure to another
286  */
287 cpy_st(s1, s2, size)
288 reg int	*s1, *s2, size; {
289 
290 	size /= 2;
291 	while (size--)
292 		*s1++ = *s2++;
293 }
294 /*
295  *	This routine forks off a shell.  It uses the users login shell
296  */
297 shell_out() {
298 
299 	static char	*shell = NULL;
300 
301 	printline();
302 	if (shell == NULL)
303 		shell = shell_in();
304 	fflush(stdout);
305 	if (!fork()) {
306 		signal(SIGINT, SIG_DFL);
307 		execsh(shell);
308 	}
309 	ignoresigs();
310 	wait();
311 	resetsigs();
312 	putchar('\n');
313 	printline();
314 }
315 /*
316  *	This routine looks up the users login shell
317  */
318 # include	<sys/types.h>
319 # include	<pwd.h>
320 
321 char		*getenv();
322 
323 char *
324 shell_in() {
325 
326 	reg struct passwd	*pp;
327 	reg char		*sp;
328 
329 	if ((sp = getenv("SHELL")) == NULL) {
330 		pp = getpwuid(getuid());
331 		if (pp->pw_shell[0] != '\0')
332 			return pp->pw_shell;
333 		else
334 			return shell_def;
335 		/*return (*(pp->pw_shell) != '\0' ? pp->pw_shell : shell_def);*/
336 	}
337 	return sp;
338 }
339 /*
340  *	This routine sets things up to ignore all the signals.
341  */
342 ignoresigs() {
343 
344 	reg int	i;
345 
346 	for (i = 0; i < NSIG; i++)
347 		signal(i, SIG_IGN);
348 }
349 /*
350  *	This routine sets up things as they were before.
351  */
352 resetsigs() {
353 
354 	reg int	i;
355 
356 	for (i = 0; i < NSIG; i++)
357 		signal(i, SIG_DFL);
358 	signal(SIGINT, quit);
359 }
360