table.c revision 1.3 1 /* $NetBSD: table.c,v 1.3 1995/03/21 15:05:58 cgd 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: table.c,v 1.3 1995/03/21 15:05:58 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include "back.h"
45
46 char *help2[] = {
47 " Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
48 "position, <f> is the finishing position, and <r> is the roll.",
49 "Remember, each die roll must be moved separately.",
50 0
51 };
52
53 struct state {
54 char ch;
55 int fcode;
56 int newst;
57 };
58
59 struct state atmata[] = {
60
61 'R', 1, 0, '?', 7, 0, 'Q', 0, -3, 'B', 8, 25,
62 '9', 2, 25, '8', 2, 25, '7', 2, 25, '6', 2, 25,
63 '5', 2, 25, '4', 2, 25, '3', 2, 25, '2', 2, 19,
64 '1', 2, 15, '0', 2, 25, '.', 0, 0, '9', 2, 25,
65 '8', 2, 25, '7', 2, 25, '6', 2, 25, '5', 2, 25,
66
67 '4', 2, 25, '3', 2, 25, '2', 2, 25, '1', 2, 25,
68 '0', 2, 25, '/', 0, 32, '-', 0, 39, '.', 0, 0,
69 '/', 5, 32, ' ', 6, 3, ',', 6, 3, '\n', 0, -1,
70 '6', 3, 28, '5', 3, 28, '4', 3, 28, '3', 3, 28,
71 '2', 3, 28, '1', 3, 28, '.', 0, 0, 'H', 9, 61,
72
73 '9', 4, 61, '8', 4, 61, '7', 4, 61, '6', 4, 61,
74 '5', 4, 61, '4', 4, 61, '3', 4, 61, '2', 4, 53,
75 '1', 4, 51, '0', 4, 61, '.', 0, 0, '9', 4, 61,
76 '8', 4, 61, '7', 4, 61, '6', 4, 61, '5', 4, 61,
77 '4', 4, 61, '3', 4, 61, '2', 4, 61, '1', 4, 61,
78
79 '0', 4, 61, ' ', 6, 3, ',', 6, 3, '-', 5, 39,
80 '\n', 0, -1, '.', 0, 0
81 };
82
83 checkmove (ist)
84
85 int ist;
86
87 {
88 register int j, n;
89 register char c;
90 char a;
91
92 domove:
93 if (ist == 0) {
94 if (tflag)
95 curmove (curr,32);
96 else
97 writel ("\t\t");
98 writel ("Move: ");
99 }
100 ist = mvl = ncin = 0;
101 for (j = 0; j < 5; j++)
102 p[j] = g[j] = -1;
103
104 dochar:
105 c = readc();
106
107 if (c == 'S') {
108 raflag = 0;
109 save (1);
110 if (tflag) {
111 curmove (cturn == -1? 18: 19,39);
112 ist = -1;
113 goto domove;
114 } else {
115 proll ();
116 ist = 0;
117 goto domove;
118 }
119 }
120
121 if (c == tty.sg_erase && ncin > 0) {
122 if (tflag)
123 curmove (curr,curc-1);
124 else {
125 if (tty.sg_erase == '\010')
126 writel ("\010 \010");
127 else
128 writec (cin[ncin-1]);
129 }
130 ncin--;
131 n = rsetbrd();
132 if (n == 0) {
133 n = -1;
134 if (tflag)
135 refresh();
136 }
137 if ((ist = n) > 0)
138 goto dochar;
139 goto domove;
140 }
141
142 if (c == tty.sg_kill && ncin > 0) {
143 if (tflag) {
144 refresh();
145 curmove (curr,39);
146 ist = -1;
147 goto domove;
148 } else if (tty.sg_erase == '\010') {
149 for (j = 0; j < ncin; j++)
150 writel ("\010 \010");
151 ist = -1;
152 goto domove;
153 } else {
154 writec ('\\');
155 writec ('\n');
156 proll ();
157 ist = 0;
158 goto domove;
159 }
160 }
161
162 n = dotable(c,ist);
163 if (n >= 0) {
164 cin[ncin++] = c;
165 if (n > 2)
166 if ((! tflag) || c != '\n')
167 writec (c);
168 ist = n;
169 if (n)
170 goto dochar;
171 else
172 goto domove;
173 }
174
175 if (n == -1 && mvl >= mvlim)
176 return(0);
177 if (n == -1 && mvl < mvlim-1)
178 return(-4);
179
180 if (n == -6) {
181 if (! tflag) {
182 if (movokay(mvl+1)) {
183 wrboard();
184 movback (mvl+1);
185 }
186 proll ();
187 writel ("\t\tMove: ");
188 for (j = 0; j < ncin;)
189 writec (cin[j++]);
190 } else {
191 if (movokay(mvl+1)) {
192 refresh();
193 movback (mvl+1);
194 } else
195 curmove (cturn == -1? 18:19,ncin+39);
196 }
197 ist = n = rsetbrd();
198 goto dochar;
199 }
200
201 if (n != -5)
202 return(n);
203 writec ('\007');
204 goto dochar;
205 }
206
207 dotable (c,i)
209 char c;
210 register int i;
211
212 {
213 register int a, j;
214 int test;
215
216 test = (c == 'R');
217
218 while ( (a = atmata[i].ch) != '.') {
219 if (a == c || (test && a == '\n')) {
220 switch (atmata[i].fcode) {
221
222 case 1:
223 wrboard();
224 if (tflag) {
225 curmove (cturn == -1? 18: 19,0);
226 proll ();
227 writel ("\t\t");
228 } else
229 proll ();
230 break;
231
232 case 2:
233 if (p[mvl] == -1)
234 p[mvl] = c-'0';
235 else
236 p[mvl] = p[mvl]*10+c-'0';
237 break;
238
239 case 3:
240 if (g[mvl] != -1) {
241 if (mvl < mvlim)
242 mvl++;
243 p[mvl] = p[mvl-1];
244 }
245 g[mvl] = p[mvl]+cturn*(c-'0');
246 if (g[mvl] < 0)
247 g[mvl] = 0;
248 if (g[mvl] > 25)
249 g[mvl] = 25;
250 break;
251
252 case 4:
253 if (g[mvl] == -1)
254 g[mvl] = c-'0';
255 else
256 g[mvl] = g[mvl]*10+c-'0';
257 break;
258
259 case 5:
260 if (mvl < mvlim)
261 mvl++;
262 p[mvl] = g[mvl-1];
263 break;
264
265 case 6:
266 if (mvl < mvlim)
267 mvl++;
268 break;
269
270 case 7:
271 if (tflag)
272 curmove (20,0);
273 else
274 writec ('\n');
275 text (help2);
276 if (tflag) {
277 curmove (cturn == -1? 18: 19,39);
278 } else {
279 writec ('\n');
280 proll();
281 writel ("\t\tMove: ");
282 }
283 break;
284
285 case 8:
286 p[mvl] = bar;
287 break;
288
289 case 9:
290 g[mvl] = home;
291 }
292
293 if (! test || a != '\n')
294 return (atmata[i].newst);
295 else
296 return (-6);
297 }
298
299 i++;
300 }
301
302 return (-5);
303 }
304
305 rsetbrd () {
307 register int i, j, n;
308
309 n = 0;
310 mvl = 0;
311 for (i = 0; i < 4; i++)
312 p[i] = g[i] = -1;
313 for (j = 0; j < ncin; j++)
314 n = dotable (cin[j],n);
315 return (n);
316 }
317