Home | History | Annotate | Line # | Download | only in atc
input.c revision 1.18
      1 /*	$NetBSD: input.c,v 1.18 2005/07/01 00:48:34 jmc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Ed James.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
     37  *
     38  * Copy permission is hereby granted provided that this notice is
     39  * retained on all partial or complete copies.
     40  *
     41  * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)input.c	8.1 (Berkeley) 5/31/93";
     48 #else
     49 __RCSID("$NetBSD: input.c,v 1.18 2005/07/01 00:48:34 jmc Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include "include.h"
     54 #include "pathnames.h"
     55 
     56 #define MAXRULES	6
     57 #define MAXDEPTH	15
     58 
     59 #define RETTOKEN	'\n'
     60 #define REDRAWTOKEN	'\014'	/* CTRL(L) */
     61 #define	SHELLTOKEN	'!'
     62 #define HELPTOKEN	'?'
     63 #define ALPHATOKEN	256
     64 #define NUMTOKEN	257
     65 
     66 typedef struct {
     67 	int	token;
     68 	int	to_state;
     69 	const char	*str;
     70 	const char	*(*func)(int);
     71 } RULE;
     72 
     73 typedef struct {
     74 	int	num_rules;
     75 	RULE	*rule;
     76 } STATE;
     77 
     78 typedef struct {
     79 	char	str[20];
     80 	int	state;
     81 	int	rule;
     82 	int	ch;
     83 	int	pos;
     84 } STACK;
     85 
     86 #define T_RULE		stack[level].rule
     87 #define T_STATE		stack[level].state
     88 #define T_STR		stack[level].str
     89 #define T_POS		stack[level].pos
     90 #define	T_CH		stack[level].ch
     91 
     92 #define NUMELS(a)	(sizeof (a) / sizeof (*(a)))
     93 
     94 #define NUMSTATES	NUMELS(st)
     95 
     96 
     97 RULE	state0[] = {	{ ALPHATOKEN,	1,	"%c:",		setplane},
     98 			{ RETTOKEN,	-1,	"",		NULL	},
     99 			{ HELPTOKEN,	12,	" [a-z]<ret>",	NULL	}},
    100 	state1[] = {	{ 't',		2,	" turn",	turn	},
    101 			{ 'a',		3,	" altitude:",	NULL	},
    102 			{ 'c',		4,	" circle",	circle	},
    103 			{ 'm',		7,	" mark",	mark	},
    104 			{ 'u',		7,	" unmark",	unmark	},
    105 			{ 'i',		7,	" ignore",	ignore	},
    106 			{ HELPTOKEN,	12,	" tacmui",	NULL	}},
    107 	state2[] = {	{ 'l',		6,	" left",	left	},
    108 			{ 'r',		6,	" right",	right	},
    109 			{ 'L',		4,	" left 90",	Left	},
    110 			{ 'R',		4,	" right 90",	Right	},
    111 			{ 't',		11,	" towards",	NULL	},
    112 			{ 'w',		4,	" to 0",	to_dir	},
    113 			{ 'e',		4,	" to 45",	to_dir	},
    114 			{ 'd',		4,	" to 90",	to_dir	},
    115 			{ 'c',		4,	" to 135",	to_dir	},
    116 			{ 'x',		4,	" to 180",	to_dir	},
    117 			{ 'z',		4,	" to 225",	to_dir	},
    118 			{ 'a',		4,	" to 270",	to_dir	},
    119 			{ 'q',		4,	" to 315",	to_dir	},
    120 			{ HELPTOKEN,	12,	" lrLRt<dir>",	NULL	}},
    121 	state3[] = {	{ '+',		10,	" climb",	climb	},
    122 			{ 'c',		10,	" climb",	climb	},
    123 			{ '-',		10,	" descend",	descend	},
    124 			{ 'd',		10,	" descend",	descend	},
    125 			{ NUMTOKEN,	7,	" %c000 feet",	setalt	},
    126 			{ HELPTOKEN,	12,	" +-cd[0-9]",	NULL	}},
    127 	state4[] = {	{ '@',		9,	" at",		NULL	},
    128 			{ 'a',		9,	" at",		NULL	},
    129 			{ RETTOKEN,	-1,	"",		NULL	},
    130 			{ HELPTOKEN,	12,	" @a<ret>",	NULL	}},
    131 	state5[] = {	{ NUMTOKEN,	7,	"%c",		delayb	},
    132 			{ HELPTOKEN,	12,	" [0-9]",	NULL	}},
    133 	state6[] = {	{ '@',		9,	" at",		NULL	},
    134 			{ 'a',		9,	" at",		NULL	},
    135 			{ 'w',		4,	" 0",		rel_dir	},
    136 			{ 'e',		4,	" 45",		rel_dir	},
    137 			{ 'd',		4,	" 90",		rel_dir	},
    138 			{ 'c',		4,	" 135",		rel_dir	},
    139 			{ 'x',		4,	" 180",		rel_dir	},
    140 			{ 'z',		4,	" 225",		rel_dir	},
    141 			{ 'a',		4,	" 270",		rel_dir	},
    142 			{ 'q',		4,	" 315",		rel_dir	},
    143 			{ RETTOKEN,	-1,	"",		NULL	},
    144 			{ HELPTOKEN,	12,	" @a<dir><ret>",NULL	}},
    145 	state7[] = {	{ RETTOKEN,	-1,	"",		NULL	},
    146 			{ HELPTOKEN,	12,	" <ret>",	NULL	}},
    147 	state8[] = {	{ NUMTOKEN,	4,	"%c",		benum	},
    148 			{ HELPTOKEN,	12,	" [0-9]",	NULL	}},
    149 	state9[] = {	{ 'b',		5,	" beacon #",	NULL	},
    150 			{ '*',		5,	" beacon #",	NULL	},
    151 			{ HELPTOKEN,	12,	" b*",		NULL	}},
    152 	state10[] = {	{ NUMTOKEN,	7,	" %c000 ft",	setrelalt},
    153 			{ HELPTOKEN,	12,	" [0-9]",	NULL	}},
    154 	state11[] = {	{ 'b',		8,	" beacon #",	beacon	},
    155 			{ '*',		8,	" beacon #",	beacon	},
    156 			{ 'e',		8,	" exit #",	ex_it	},
    157 			{ 'a',		8,	" airport #",	airport	},
    158 			{ HELPTOKEN,	12,	" b*ea",	NULL	}},
    159 	state12[] = {	{ -1,		-1,	"",		NULL	}};
    160 
    161 #define DEF_STATE(s)	{ NUMELS(s),	(s)	}
    162 
    163 STATE	st[] = {
    164 	DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
    165 	DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
    166 	DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
    167 	DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
    168 	DEF_STATE(state12)
    169 };
    170 
    171 PLANE	p;
    172 STACK	stack[MAXDEPTH];
    173 int	level;
    174 int	tval;
    175 int	dest_type, dest_no, dir;
    176 
    177 int
    178 pop(void)
    179 {
    180 	if (level == 0)
    181 		return (-1);
    182 	level--;
    183 
    184 	ioclrtoeol(T_POS);
    185 
    186 	strcpy(T_STR, "");
    187 	T_RULE = -1;
    188 	T_CH = -1;
    189 	return (0);
    190 }
    191 
    192 void
    193 rezero(void)
    194 {
    195 	iomove(0);
    196 
    197 	level = 0;
    198 	T_STATE = 0;
    199 	T_RULE = -1;
    200 	T_CH = -1;
    201 	T_POS = 0;
    202 	strcpy(T_STR, "");
    203 }
    204 
    205 void
    206 push(int ruleno, int ch)
    207 {
    208 	int	newstate, newpos;
    209 
    210 	(void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
    211 	T_RULE = ruleno;
    212 	T_CH = ch;
    213 	newstate = st[T_STATE].rule[ruleno].to_state;
    214 	newpos = T_POS + strlen(T_STR);
    215 
    216 	ioaddstr(T_POS, T_STR);
    217 
    218 	if (level == 0)
    219 		ioclrtobot();
    220 	level++;
    221 	T_STATE = newstate;
    222 	T_POS = newpos;
    223 	T_RULE = -1;
    224 	strcpy(T_STR, "");
    225 }
    226 
    227 int
    228 getcommand(void)
    229 {
    230 	int	c, i, done;
    231 	const char	*s, *(*func)(int);
    232 	PLANE	*pp;
    233 
    234 	rezero();
    235 
    236 	do {
    237 		c = gettoken();
    238 		if (c == tty_new.c_cc[VERASE]) {
    239 			if (pop() < 0)
    240 				noise();
    241 		} else if (c == tty_new.c_cc[VKILL]) {
    242 			while (pop() >= 0)
    243 				;
    244 		} else {
    245 			done = 0;
    246 			for (i = 0; i < st[T_STATE].num_rules; i++) {
    247 				if (st[T_STATE].rule[i].token == c ||
    248 				    st[T_STATE].rule[i].token == tval) {
    249 					push(i, (c >= ALPHATOKEN) ? tval : c);
    250 					done = 1;
    251 					break;
    252 				}
    253 			}
    254 			if (!done)
    255 				noise();
    256 		}
    257 	} while (T_STATE != -1);
    258 
    259 	if (level == 1)
    260 		return (1);	/* forced update */
    261 
    262 	dest_type = T_NODEST;
    263 
    264 	for (i = 0; i < level; i++) {
    265 		func = st[stack[i].state].rule[stack[i].rule].func;
    266 		if (func != NULL)
    267 			if ((s = (*func)(stack[i].ch)) != NULL) {
    268 				ioerror(stack[i].pos, strlen(stack[i].str), s);
    269 				return (-1);
    270 			}
    271 	}
    272 
    273 	pp = findplane(p.plane_no);
    274 	if (pp->new_altitude != p.new_altitude)
    275 		pp->new_altitude = p.new_altitude;
    276 	else if (pp->status != p.status)
    277 		pp->status = p.status;
    278 	else {
    279 		pp->new_dir = p.new_dir;
    280 		pp->delayd = p.delayd;
    281 		pp->delayd_no = p.delayd_no;
    282 	}
    283 	return (0);
    284 }
    285 
    286 void
    287 noise(void)
    288 {
    289 	putchar('\07');
    290 	fflush(stdout);
    291 }
    292 
    293 int
    294 gettoken(void)
    295 {
    296 	while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
    297 	{
    298 		if (tval == SHELLTOKEN)
    299 		{
    300 #ifdef BSD
    301 			struct itimerval	itv;
    302 			itv.it_value.tv_sec = 0;
    303 			itv.it_value.tv_usec = 0;
    304 			setitimer(ITIMER_REAL, &itv, NULL);
    305 #endif
    306 #ifdef SYSV
    307 			int aval;
    308 			aval = alarm(0);
    309 #endif
    310 			if (fork() == 0)	/* child */
    311 			{
    312 				char *shell, *base;
    313 
    314 				done_screen();
    315 
    316 						 /* run user's favorite shell */
    317 				if ((shell = getenv("SHELL")) != NULL)
    318 				{
    319 					base = strrchr(shell, '/');
    320 					if (base == NULL)
    321 						base = shell;
    322 					else
    323 						base++;
    324 					execl(shell, base, (char *) 0);
    325 				}
    326 				else
    327 					execl(_PATH_BSHELL, "sh", (char *) 0);
    328 
    329 				exit(0);	/* oops */
    330 			}
    331 
    332 			wait(0);
    333 			tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
    334 #ifdef BSD
    335 			itv.it_value.tv_sec = 0;
    336 			itv.it_value.tv_usec = 1;
    337 			itv.it_interval.tv_sec = sp->update_secs;
    338 			itv.it_interval.tv_usec = 0;
    339 			setitimer(ITIMER_REAL, &itv, NULL);
    340 #endif
    341 #ifdef SYSV
    342 			alarm(aval);
    343 #endif
    344 		}
    345 		redraw();
    346 	}
    347 
    348 	if (isdigit(tval))
    349 		return (NUMTOKEN);
    350 	else if (isalpha(tval))
    351 		return (ALPHATOKEN);
    352 	else
    353 		return (tval);
    354 }
    355 
    356 const char *
    357 setplane(int c)
    358 {
    359 	PLANE	*pp;
    360 
    361 	pp = findplane(number(c));
    362 	if (pp == NULL)
    363 		return ("Unknown Plane");
    364 	memcpy(&p, pp, sizeof (p));
    365 	p.delayd = 0;
    366 	return (NULL);
    367 }
    368 
    369 const char *
    370 turn(int c __attribute__((__unused__)))
    371 {
    372 	if (p.altitude == 0)
    373 		return ("Planes at airports may not change direction");
    374 	return (NULL);
    375 }
    376 
    377 const char *
    378 circle(int c __attribute__((__unused__)))
    379 {
    380 	if (p.altitude == 0)
    381 		return ("Planes cannot circle on the ground");
    382 	p.new_dir = MAXDIR;
    383 	return (NULL);
    384 }
    385 
    386 const char *
    387 left(int c __attribute__((__unused__)))
    388 {
    389 	dir = D_LEFT;
    390 	p.new_dir = p.dir - 1;
    391 	if (p.new_dir < 0)
    392 		p.new_dir += MAXDIR;
    393 	return (NULL);
    394 }
    395 
    396 const char *
    397 right(int c __attribute__((__unused__)))
    398 {
    399 	dir = D_RIGHT;
    400 	p.new_dir = p.dir + 1;
    401 	if (p.new_dir >= MAXDIR)
    402 		p.new_dir -= MAXDIR;
    403 	return (NULL);
    404 }
    405 
    406 const char *
    407 Left(int c __attribute__((__unused__)))
    408 {
    409 	p.new_dir = p.dir - 2;
    410 	if (p.new_dir < 0)
    411 		p.new_dir += MAXDIR;
    412 	return (NULL);
    413 }
    414 
    415 const char *
    416 Right(int c __attribute__((__unused__)))
    417 {
    418 	p.new_dir = p.dir + 2;
    419 	if (p.new_dir >= MAXDIR)
    420 		p.new_dir -= MAXDIR;
    421 	return (NULL);
    422 }
    423 
    424 const char *
    425 delayb(int c)
    426 {
    427 	int	xdiff, ydiff;
    428 
    429 	c -= '0';
    430 
    431 	if (c >= sp->num_beacons)
    432 		return ("Unknown beacon");
    433 	xdiff = sp->beacon[(int)c].x - p.xpos;
    434 	xdiff = SGN(xdiff);
    435 	ydiff = sp->beacon[(int)c].y - p.ypos;
    436 	ydiff = SGN(ydiff);
    437 	if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
    438 		return ("Beacon is not in flight path");
    439 	p.delayd = 1;
    440 	p.delayd_no = c;
    441 
    442 	if (dest_type != T_NODEST) {
    443 		switch (dest_type) {
    444 		case T_BEACON:
    445 			xdiff = sp->beacon[dest_no].x - sp->beacon[(int)c].x;
    446 			ydiff = sp->beacon[dest_no].y - sp->beacon[(int)c].y;
    447 			break;
    448 		case T_EXIT:
    449 			xdiff = sp->exit[dest_no].x - sp->beacon[(int)c].x;
    450 			ydiff = sp->exit[dest_no].y - sp->beacon[(int)c].y;
    451 			break;
    452 		case T_AIRPORT:
    453 			xdiff = sp->airport[dest_no].x - sp->beacon[(int)c].x;
    454 			ydiff = sp->airport[dest_no].y - sp->beacon[(int)c].y;
    455 			break;
    456 		default:
    457 			return ("Bad case in delayb!  Get help!");
    458 			break;
    459 		}
    460 		if (xdiff == 0 && ydiff == 0)
    461 			return ("Would already be there");
    462 		p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
    463 		if (p.new_dir == p.dir)
    464 			return ("Already going in that direction");
    465 	}
    466 	return (NULL);
    467 }
    468 
    469 const char *
    470 beacon(int c __attribute__((__unused__)))
    471 {
    472 	dest_type = T_BEACON;
    473 	return (NULL);
    474 }
    475 
    476 const char *
    477 ex_it(int c __attribute__((__unused__)))
    478 {
    479 	dest_type = T_EXIT;
    480 	return (NULL);
    481 }
    482 
    483 const char *
    484 airport(int c __attribute__((__unused__)))
    485 {
    486 	dest_type = T_AIRPORT;
    487 	return (NULL);
    488 }
    489 
    490 const char *
    491 climb(int c __attribute__((__unused__)))
    492 {
    493 	dir = D_UP;
    494 	return (NULL);
    495 }
    496 
    497 const char *
    498 descend(int c __attribute__((__unused__)))
    499 {
    500 	dir = D_DOWN;
    501 	return (NULL);
    502 }
    503 
    504 const char *
    505 setalt(int c)
    506 {
    507 	if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
    508 		return ("Already at that altitude");
    509 	p.new_altitude = c - '0';
    510 	return (NULL);
    511 }
    512 
    513 const char *
    514 setrelalt(int c)
    515 {
    516 	if (c == 0)
    517 		return ("altitude not changed");
    518 
    519 	switch (dir) {
    520 	case D_UP:
    521 		p.new_altitude = p.altitude + c - '0';
    522 		break;
    523 	case D_DOWN:
    524 		p.new_altitude = p.altitude - (c - '0');
    525 		break;
    526 	default:
    527 		return ("Unknown case in setrelalt!  Get help!");
    528 		break;
    529 	}
    530 	if (p.new_altitude < 0)
    531 		return ("Altitude would be too low");
    532 	else if (p.new_altitude > 9)
    533 		return ("Altitude would be too high");
    534 	return (NULL);
    535 }
    536 
    537 const char *
    538 benum(int c)
    539 {
    540 	dest_no = c -= '0';
    541 
    542 	switch (dest_type) {
    543 	case T_BEACON:
    544 		if (c >= sp->num_beacons)
    545 			return ("Unknown beacon");
    546 		p.new_dir = DIR_FROM_DXDY(sp->beacon[(int)c].x - p.xpos,
    547 			sp->beacon[(int)c].y - p.ypos);
    548 		break;
    549 	case T_EXIT:
    550 		if (c >= sp->num_exits)
    551 			return ("Unknown exit");
    552 		p.new_dir = DIR_FROM_DXDY(sp->exit[(int)c].x - p.xpos,
    553 			sp->exit[(int)c].y - p.ypos);
    554 		break;
    555 	case T_AIRPORT:
    556 		if (c >= sp->num_airports)
    557 			return ("Unknown airport");
    558 		p.new_dir = DIR_FROM_DXDY(sp->airport[(int)c].x - p.xpos,
    559 			sp->airport[(int)c].y - p.ypos);
    560 		break;
    561 	default:
    562 		return ("Unknown case in benum!  Get help!");
    563 		break;
    564 	}
    565 	return (NULL);
    566 }
    567 
    568 const char *
    569 to_dir(int c)
    570 {
    571 	p.new_dir = dir_no(c);
    572 	return (NULL);
    573 }
    574 
    575 const char *
    576 rel_dir(int c)
    577 {
    578 	int	angle;
    579 
    580 	angle = dir_no(c);
    581 	switch (dir) {
    582 	case D_LEFT:
    583 		p.new_dir = p.dir - angle;
    584 		if (p.new_dir < 0)
    585 			p.new_dir += MAXDIR;
    586 		break;
    587 	case D_RIGHT:
    588 		p.new_dir = p.dir + angle;
    589 		if (p.new_dir >= MAXDIR)
    590 			p.new_dir -= MAXDIR;
    591 		break;
    592 	default:
    593 		return ("Bizarre direction in rel_dir!  Get help!");
    594 		break;
    595 	}
    596 	return (NULL);
    597 }
    598 
    599 const char *
    600 mark(int c __attribute__((__unused__)))
    601 {
    602 	if (p.altitude == 0)
    603 		return ("Cannot mark planes on the ground");
    604 	if (p.status == S_MARKED)
    605 		return ("Already marked");
    606 	p.status = S_MARKED;
    607 	return (NULL);
    608 }
    609 
    610 const char *
    611 unmark(int c __attribute__((__unused__)))
    612 {
    613 	if (p.altitude == 0)
    614 		return ("Cannot unmark planes on the ground");
    615 	if (p.status == S_UNMARKED)
    616 		return ("Already unmarked");
    617 	p.status = S_UNMARKED;
    618 	return (NULL);
    619 }
    620 
    621 const char *
    622 ignore(int c __attribute__((__unused__)))
    623 {
    624 	if (p.altitude == 0)
    625 		return ("Cannot ignore planes on the ground");
    626 	if (p.status == S_IGNORED)
    627 		return ("Already ignored");
    628 	p.status = S_IGNORED;
    629 	return (NULL);
    630 }
    631 
    632 int
    633 dir_no(int ch)
    634 {
    635 	int	dirno;
    636 
    637 	dirno = -1;
    638 	switch (ch) {
    639 	case 'w':	dirno = 0;	break;
    640 	case 'e':	dirno = 1;	break;
    641 	case 'd':	dirno = 2;	break;
    642 	case 'c':	dirno = 3;	break;
    643 	case 'x':	dirno = 4;	break;
    644 	case 'z':	dirno = 5;	break;
    645 	case 'a':	dirno = 6;	break;
    646 	case 'q':	dirno = 7;	break;
    647 	default:
    648 		fprintf(stderr, "bad character in dir_no\n");
    649 		break;
    650 	}
    651 	return (dirno);
    652 }
    653