add sanity checks for old file systems
[unix-history] / usr / src / games / mille / misc.c
CommitLineData
dd14fdb3
KB
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)misc.c 5.1 (Berkeley) %G%";
9#endif not lint
10
11#include "mille.h"
12#ifndef unctrl
13#include "unctrl.h"
14#endif
15
16# include <sys/file.h>
17
18# ifdef attron
19# include <term.h>
20# define _tty cur_term->Nttyb
21# endif attron
22
23/*
24 * @(#)misc.c 1.2 (Berkeley) 3/28/83
25 */
26
27#define NUMSAFE 4
28
29/* VARARGS1 */
30error(str, arg)
31char *str;
32{
33 stdscr = Score;
34 mvprintw(ERR_Y, ERR_X, str, arg);
35 clrtoeol();
36 putchar('\a');
37 refresh();
38 stdscr = Board;
39 return FALSE;
40}
41
42CARD
43getcard()
44{
45 reg int c, c1;
46
47 for (;;) {
48 while ((c = readch()) == '\n' || c == '\r' || c == ' ')
49 continue;
50 if (islower(c))
51 c = toupper(c);
52 if (c == killchar() || c == erasechar())
53 return -1;
54 addstr(unctrl(c));
55 clrtoeol();
56 switch (c) {
57 case '1': case '2': case '3':
58 case '4': case '5': case '6':
59 c -= '0';
60 break;
61 case '0': case 'P': case 'p':
62 c = 0;
63 break;
64 default:
65 putchar('\a');
66 addch('\b');
67 if (!isprint(c))
68 addch('\b');
69 c = -1;
70 break;
71 }
72 refresh();
73 if (c >= 0) {
74 while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
75 if (c1 == killchar())
76 return -1;
77 else if (c1 == erasechar()) {
78 addch('\b');
79 clrtoeol();
80 refresh();
81 goto cont;
82 }
83 else
84 write(0, "\a", 1);
85 return c;
86 }
87cont: ;
88 }
89}
90
91check_ext(forcomp)
92reg bool forcomp; {
93
94
95 if (End == 700)
96 if (Play == PLAYER) {
97 if (getyn(EXTENSIONPROMPT)) {
98extend:
99 if (!forcomp)
100 End = 1000;
101 return TRUE;
102 }
103 else {
104done:
105 if (!forcomp)
106 Finished = TRUE;
107 return FALSE;
108 }
109 }
110 else {
111 reg PLAY *pp, *op;
112 reg int i, safe, miles;
113
114 pp = &Player[COMP];
115 op = &Player[PLAYER];
116 for (safe = 0, i = 0; i < NUMSAFE; i++)
117 if (pp->safety[i] != S_UNKNOWN)
118 safe++;
119 if (safe < 2)
120 goto done;
121 if (op->mileage == 0 || onecard(op)
122 || (op->can_go && op->mileage >= 500))
123 goto done;
124 for (miles = 0, i = 0; i < NUMSAFE; i++)
125 if (op->safety[i] != S_PLAYED
126 && pp->safety[i] == S_UNKNOWN)
127 miles++;
128 if (miles + safe == NUMSAFE)
129 goto extend;
130 for (miles = 0, i = 0; i < HAND_SZ; i++)
131 if ((safe = pp->hand[i]) <= C_200)
132 miles += Value[safe];
133 if (miles + (Topcard - Deck) * 3 > 1000)
134 goto extend;
135 goto done;
136 }
137 else
138 goto done;
139}
140
141/*
142 * Get a yes or no answer to the given question. Saves are
143 * also allowed. Return TRUE if the answer was yes, FALSE if no.
144 */
145getyn(promptno)
146register int promptno; {
147
148 reg char c;
149
150 Saved = FALSE;
151 for (;;) {
152 leaveok(Board, FALSE);
153 prompt(promptno);
154 clrtoeol();
155 refresh();
156 switch (c = readch()) {
157 case 'n': case 'N':
158 addch('N');
159 refresh();
160 leaveok(Board, TRUE);
161 return FALSE;
162 case 'y': case 'Y':
163 addch('Y');
164 refresh();
165 leaveok(Board, TRUE);
166 return TRUE;
167 case 's': case 'S':
168 addch('S');
169 refresh();
170 Saved = save();
171 continue;
172 default:
173 addstr(unctrl(c));
174 refresh();
175 putchar('\a');
176 break;
177 }
178 }
179}
180
181/*
182 * Check to see if more games are desired. If not, and game
183 * came from a saved file, make sure that they don't want to restore
184 * it. Exit appropriately.
185 */
186check_more() {
187
188 flush_input();
189
190 On_exit = TRUE;
191 if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
192 if (getyn(ANOTHERGAMEPROMPT))
193 return;
194 else {
195 /*
196 * must do accounting normally done in main()
197 */
198 if (Player[PLAYER].total > Player[COMP].total)
199 Player[PLAYER].games++;
200 else if (Player[PLAYER].total < Player[COMP].total)
201 Player[COMP].games++;
202 Player[COMP].total = 0;
203 Player[PLAYER].total = 0;
204 }
205 else
206 if (getyn(ANOTHERHANDPROMPT))
207 return;
208 if (!Saved && getyn(SAVEGAMEPROMPT))
209 if (!save())
210 return;
211 die();
212}
213
214readch()
215{
216 reg int cnt;
217 static char c;
218
219 for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
220 if (cnt > 100)
221 exit(1);
222 return c;
223}
224
225flush_input()
226{
227# ifdef TIOCFLUSH
228 static int ioctl_args = FREAD;
229
230 (void) ioctl(fileno(stdin), TIOCFLUSH, &ioctl_args);
231# else
232 fflush(stdin);
233# endif
234}