]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/table.c
This adds a check for memory allocation failure to one place in
[bsdgames-darwin.git] / backgammon / common_source / table.c
1 /* $NetBSD: table.c,v 1.6 1999/02/10 12:29:48 hubertf 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: table.c,v 1.6 1999/02/10 12:29:48 hubertf Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "back.h"
46
47 const char *const help2[] = {
48 " Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
49 "position, <f> is the finishing position, and <r> is the roll.",
50 "Remember, each die roll must be moved separately.",
51 0
52 };
53
54 struct state {
55 char ch;
56 int fcode;
57 int newst;
58 };
59
60 static const struct state atmata[] = {
61
62 {'R', 1, 0}, {'?', 7, 0}, {'Q', 0, -3}, {'B', 8, 25},
63 {'9', 2, 25}, {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25},
64 {'5', 2, 25}, {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 19},
65 {'1', 2, 15}, {'0', 2, 25}, {'.', 0, 0}, {'9', 2, 25},
66 {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25}, {'5', 2, 25},
67
68 {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 25}, {'1', 2, 25},
69 {'0', 2, 25}, {'/', 0, 32}, {'-', 0, 39}, {'.', 0, 0},
70 {'/', 5, 32}, {' ', 6, 3}, {',', 6, 3}, {'\n', 0, -1},
71 {'6', 3, 28}, {'5', 3, 28}, {'4', 3, 28}, {'3', 3, 28},
72 {'2', 3, 28}, {'1', 3, 28}, {'.', 0, 0}, {'H', 9, 61},
73
74 {'9', 4, 61}, {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61},
75 {'5', 4, 61}, {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 53},
76 {'1', 4, 51}, {'0', 4, 61}, {'.', 0, 0}, {'9', 4, 61},
77 {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61}, {'5', 4, 61},
78 {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 61}, {'1', 4, 61},
79
80 {'0', 4, 61}, {' ', 6, 3}, {',', 6, 3}, {'-', 5, 39},
81 {'\n', 0, -1}, {'.', 0, 0}
82 };
83
84 int
85 checkmove(ist)
86 int ist;
87 {
88 int j, n;
89 char c;
90
91 domove:
92 if (ist == 0) {
93 if (tflag)
94 curmove(curr, 32);
95 else
96 writel("\t\t");
97 writel("Move: ");
98 }
99 ist = mvl = ncin = 0;
100 for (j = 0; j < 5; j++)
101 p[j] = g[j] = -1;
102
103 dochar:
104 c = readc();
105
106 if (c == 'S') {
107 raflag = 0;
108 save(1);
109 if (tflag) {
110 curmove(cturn == -1 ? 18 : 19, 39);
111 ist = -1;
112 goto domove;
113 } else {
114 proll();
115 ist = 0;
116 goto domove;
117 }
118 }
119 if (c == old.c_cc[VERASE] && ncin > 0) {
120 if (tflag)
121 curmove(curr, curc - 1);
122 else {
123 if (old.c_cc[VERASE] == '\010')
124 writel("\010 \010");
125 else
126 writec(cin[ncin - 1]);
127 }
128 ncin--;
129 n = rsetbrd();
130 if (n == 0) {
131 n = -1;
132 if (tflag)
133 refresh();
134 }
135 if ((ist = n) > 0)
136 goto dochar;
137 goto domove;
138 }
139 if (c == old.c_cc[VKILL] && ncin > 0) {
140 if (tflag) {
141 refresh();
142 curmove(curr, 39);
143 ist = -1;
144 goto domove;
145 } else
146 if (old.c_cc[VERASE] == '\010') {
147 for (j = 0; j < ncin; j++)
148 writel("\010 \010");
149 ist = -1;
150 goto domove;
151 } else {
152 writec('\\');
153 writec('\n');
154 proll();
155 ist = 0;
156 goto domove;
157 }
158 }
159 n = dotable(c, ist);
160 if (n >= 0) {
161 cin[ncin++] = c;
162 if (n > 2)
163 if ((!tflag) || c != '\n')
164 writec(c);
165 ist = n;
166 if (n)
167 goto dochar;
168 else
169 goto domove;
170 }
171 if (n == -1 && mvl >= mvlim)
172 return (0);
173 if (n == -1 && mvl < mvlim - 1)
174 return (-4);
175
176 if (n == -6) {
177 if (!tflag) {
178 if (movokay(mvl + 1)) {
179 wrboard();
180 movback(mvl + 1);
181 }
182 proll();
183 writel("\t\tMove: ");
184 for (j = 0; j < ncin;)
185 writec(cin[j++]);
186 } else {
187 if (movokay(mvl + 1)) {
188 refresh();
189 movback(mvl + 1);
190 } else
191 curmove(cturn == -1 ? 18 : 19, ncin + 39);
192 }
193 ist = n = rsetbrd();
194 goto dochar;
195 }
196 if (n != -5)
197 return (n);
198 writec('\007');
199 goto dochar;
200 }
201
202 int
203 dotable(c, i)
204 char c;
205 int i;
206 {
207 int a;
208 int test;
209
210 test = (c == 'R');
211
212 while ((a = atmata[i].ch) != '.') {
213 if (a == c || (test && a == '\n')) {
214 switch (atmata[i].fcode) {
215
216 case 1:
217 wrboard();
218 if (tflag) {
219 curmove(cturn == -1 ? 18 : 19, 0);
220 proll();
221 writel("\t\t");
222 } else
223 proll();
224 break;
225
226 case 2:
227 if (p[mvl] == -1)
228 p[mvl] = c - '0';
229 else
230 p[mvl] = p[mvl] * 10 + c - '0';
231 break;
232
233 case 3:
234 if (g[mvl] != -1) {
235 if (mvl < mvlim)
236 mvl++;
237 p[mvl] = p[mvl - 1];
238 }
239 g[mvl] = p[mvl] + cturn * (c - '0');
240 if (g[mvl] < 0)
241 g[mvl] = 0;
242 if (g[mvl] > 25)
243 g[mvl] = 25;
244 break;
245
246 case 4:
247 if (g[mvl] == -1)
248 g[mvl] = c - '0';
249 else
250 g[mvl] = g[mvl] * 10 + c - '0';
251 break;
252
253 case 5:
254 if (mvl < mvlim)
255 mvl++;
256 p[mvl] = g[mvl - 1];
257 break;
258
259 case 6:
260 if (mvl < mvlim)
261 mvl++;
262 break;
263
264 case 7:
265 if (tflag)
266 curmove(20, 0);
267 else
268 writec('\n');
269 (void) text(help2);
270 if (tflag) {
271 curmove(cturn == -1 ? 18 : 19, 39);
272 } else {
273 writec('\n');
274 proll();
275 writel("\t\tMove: ");
276 }
277 break;
278
279 case 8:
280 p[mvl] = bar;
281 break;
282
283 case 9:
284 g[mvl] = home;
285 }
286
287 if (!test || a != '\n')
288 return (atmata[i].newst);
289 else
290 return (-6);
291 }
292 i++;
293 }
294
295 return (-5);
296 }
297
298 int
299 rsetbrd()
300 {
301 int i, j, n;
302
303 n = 0;
304 mvl = 0;
305 for (i = 0; i < 4; i++)
306 p[i] = g[i] = -1;
307 for (j = 0; j < ncin; j++)
308 n = dotable(cin[j], n);
309 return (n);
310 }