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