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