2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
40 * Copy permission is hereby granted provided that this notice is
41 * retained on all partial or complete copies.
43 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
48 static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
50 static const char rcsid[] =
51 "$FreeBSD: src/games/atc/input.c,v 1.7 2000/02/27 23:02:47 mph Exp $";
57 #include "pathnames.h"
66 #define REDRAWTOKEN '\014' /* CTRL(L) */
67 #define SHELLTOKEN '!'
69 #define ALPHATOKEN 256
76 const char *(*func)();
92 #define T_RULE stack[level].rule
93 #define T_STATE stack[level].state
94 #define T_STR stack[level].str
95 #define T_POS stack[level].pos
96 #define T_CH stack[level].ch
98 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
100 #define NUMSTATES NUMELS(st)
102 const char *setplane(), *circle(), *left(), *right(), *Left(), *Right(),
103 *beacon(), *ex_it(), *climb(), *descend(), *setalt(), *setrelalt(),
104 *benum(), *to_dir(), *rel_dir(), *delayb(), *mark(), *unmark(),
105 *airport(), *turn(), *ignore();
107 RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
108 { RETTOKEN, -1, "", NULL },
110 { CRTOKEN, -1, "", NULL },
112 { HELPTOKEN, 12, " [a-z]<ret>", NULL }},
113 state1[] = { { 't', 2, " turn", turn },
114 { 'a', 3, " altitude:", NULL },
115 { 'c', 4, " circle", circle },
116 { 'm', 7, " mark", mark },
117 { 'u', 7, " unmark", unmark },
118 { 'i', 7, " ignore", ignore },
119 { HELPTOKEN, 12, " tacmui", NULL }},
120 state2[] = { { 'l', 6, " left", left },
121 { 'r', 6, " right", right },
122 { 'L', 4, " left 90", Left },
123 { 'R', 4, " right 90", Right },
124 { 't', 11, " towards", NULL },
125 { 'w', 4, " to 0", to_dir },
126 { 'e', 4, " to 45", to_dir },
127 { 'd', 4, " to 90", to_dir },
128 { 'c', 4, " to 135", to_dir },
129 { 'x', 4, " to 180", to_dir },
130 { 'z', 4, " to 225", to_dir },
131 { 'a', 4, " to 270", to_dir },
132 { 'q', 4, " to 315", to_dir },
133 { HELPTOKEN, 12, " lrLRt<dir>", NULL }},
134 state3[] = { { '+', 10, " climb", climb },
135 { 'c', 10, " climb", climb },
136 { '-', 10, " descend", descend },
137 { 'd', 10, " descend", descend },
138 { NUMTOKEN, 7, " %c000 feet", setalt },
139 { HELPTOKEN, 12, " +-cd[0-9]", NULL }},
140 state4[] = { { '@', 9, " at", NULL },
141 { 'a', 9, " at", NULL },
142 { RETTOKEN, -1, "", NULL },
144 { CRTOKEN, -1, "", NULL },
146 { HELPTOKEN, 12, " @a<ret>", NULL }},
147 state5[] = { { NUMTOKEN, 7, "%c", delayb },
148 { HELPTOKEN, 12, " [0-9]", NULL }},
149 state6[] = { { '@', 9, " at", NULL },
150 { 'a', 9, " at", NULL },
151 { 'w', 4, " 0", rel_dir },
152 { 'e', 4, " 45", rel_dir },
153 { 'd', 4, " 90", rel_dir },
154 { 'c', 4, " 135", rel_dir },
155 { 'x', 4, " 180", rel_dir },
156 { 'z', 4, " 225", rel_dir },
157 { 'a', 4, " 270", rel_dir },
158 { 'q', 4, " 315", rel_dir },
159 { RETTOKEN, -1, "", NULL },
161 { CRTOKEN, -1, "", NULL },
163 { HELPTOKEN, 12, " @a<dir><ret>",NULL }},
164 state7[] = { { RETTOKEN, -1, "", NULL },
166 { CRTOKEN, -1, "", NULL },
168 { HELPTOKEN, 12, " <ret>", NULL }},
169 state8[] = { { NUMTOKEN, 4, "%c", benum },
170 { HELPTOKEN, 12, " [0-9]", NULL }},
171 state9[] = { { 'b', 5, " beacon #", NULL },
172 { '*', 5, " beacon #", NULL },
173 { HELPTOKEN, 12, " b*", NULL }},
174 state10[] = { { NUMTOKEN, 7, " %c000 ft", setrelalt},
175 { HELPTOKEN, 12, " [0-9]", NULL }},
176 state11[] = { { 'b', 8, " beacon #", beacon },
177 { '*', 8, " beacon #", beacon },
178 { 'e', 8, " exit #", ex_it },
179 { 'a', 8, " airport #", airport },
180 { HELPTOKEN, 12, " b*ea", NULL }},
181 state12[] = { { -1, -1, "", NULL }};
183 #define DEF_STATE(s) { NUMELS(s), (s) }
186 DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
187 DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
188 DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
189 DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
194 STACK stack[MAXDEPTH];
197 int dest_type, dest_no, dir;
227 int newstate, newpos;
229 (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
232 newstate = st[T_STATE].rule[ruleno].to_state;
233 newpos = T_POS + strlen(T_STR);
235 ioaddstr(T_POS, T_STR);
249 const char *s, *(*func)();
256 if (c == tty_new.sg_erase) {
259 } else if (c == tty_new.sg_kill) {
264 for (i = 0; i < st[T_STATE].num_rules; i++) {
265 if (st[T_STATE].rule[i].token == c ||
266 st[T_STATE].rule[i].token == tval) {
267 push(i, (c >= ALPHATOKEN) ? tval : c);
275 } while (T_STATE != -1);
278 return (1); /* forced update */
280 dest_type = T_NODEST;
282 for (i = 0; i < level; i++) {
283 func = st[stack[i].state].rule[stack[i].rule].func;
285 if ((s = (*func)(stack[i].ch)) != NULL) {
286 ioerror(stack[i].pos, strlen(stack[i].str), s);
291 pp = findplane(p.plane_no);
292 if (pp->new_altitude != p.new_altitude)
293 pp->new_altitude = p.new_altitude;
294 else if (pp->status != p.status)
295 pp->status = p.status;
297 pp->new_dir = p.new_dir;
298 pp->delayd = p.delayd;
299 pp->delayd_no = p.delayd_no;
312 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
314 if (tval == SHELLTOKEN)
317 struct itimerval itv;
318 itv.it_value.tv_sec = 0;
319 itv.it_value.tv_usec = 0;
320 setitimer(ITIMER_REAL, &itv, NULL);
326 if (fork() == 0) /* child */
334 /* run user's favorite shell */
335 if ((shell = getenv("SHELL")) != NULL)
337 base = strrchr(shell, '/');
342 execl(shell, base, 0);
345 execl(_PATH_BSHELL, "sh", 0);
352 ioctl(fileno(stdin), TIOCSETP, &tty_new);
353 itv.it_value.tv_sec = 0;
354 itv.it_value.tv_usec = 1;
355 itv.it_interval.tv_sec = sp->update_secs;
356 itv.it_interval.tv_usec = 0;
357 setitimer(ITIMER_REAL, &itv, NULL);
360 ioctl(fileno(stdin), TCSETAW, &tty_new);
369 else if (isalpha(tval))
380 pp = findplane(number(c));
382 return ("Unknown Plane");
383 bcopy(pp, &p, sizeof (p));
392 return ("Planes at airports may not change direction");
400 return ("Planes cannot circle on the ground");
409 p.new_dir = p.dir - 1;
419 p.new_dir = p.dir + 1;
420 if (p.new_dir >= MAXDIR)
428 p.new_dir = p.dir - 2;
437 p.new_dir = p.dir + 2;
438 if (p.new_dir >= MAXDIR)
450 if (c >= sp->num_beacons)
451 return ("Unknown beacon");
452 xdiff = sp->beacon[c].x - p.xpos;
454 ydiff = sp->beacon[c].y - p.ypos;
456 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
457 return ("Beacon is not in flight path");
461 if (dest_type != T_NODEST) {
464 xdiff = sp->beacon[dest_no].x - sp->beacon[c].x;
465 ydiff = sp->beacon[dest_no].y - sp->beacon[c].y;
468 xdiff = sp->exit[dest_no].x - sp->beacon[c].x;
469 ydiff = sp->exit[dest_no].y - sp->beacon[c].y;
472 xdiff = sp->airport[dest_no].x - sp->beacon[c].x;
473 ydiff = sp->airport[dest_no].y - sp->beacon[c].y;
476 return ("Bad case in delayb! Get help!");
479 if (xdiff == 0 && ydiff == 0)
480 return ("Would already be there");
481 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
482 if (p.new_dir == p.dir)
483 return ("Already going in that direction");
491 dest_type = T_BEACON;
505 dest_type = T_AIRPORT;
526 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
527 return ("Already at that altitude");
528 p.new_altitude = c - '0';
536 return ("altitude not changed");
540 p.new_altitude = p.altitude + c - '0';
543 p.new_altitude = p.altitude - (c - '0');
546 return ("Unknown case in setrelalt! Get help!");
549 if (p.new_altitude < 0)
550 return ("Altitude would be too low");
551 else if (p.new_altitude > 9)
552 return ("Altitude would be too high");
563 if (c >= sp->num_beacons)
564 return ("Unknown beacon");
565 p.new_dir = DIR_FROM_DXDY(sp->beacon[c].x - p.xpos,
566 sp->beacon[c].y - p.ypos);
569 if (c >= sp->num_exits)
570 return ("Unknown exit");
571 p.new_dir = DIR_FROM_DXDY(sp->exit[c].x - p.xpos,
572 sp->exit[c].y - p.ypos);
575 if (c >= sp->num_airports)
576 return ("Unknown airport");
577 p.new_dir = DIR_FROM_DXDY(sp->airport[c].x - p.xpos,
578 sp->airport[c].y - p.ypos);
581 return ("Unknown case in benum! Get help!");
590 p.new_dir = dir_no(c);
602 p.new_dir = p.dir - angle;
607 p.new_dir = p.dir + angle;
608 if (p.new_dir >= MAXDIR)
612 return ("Bizarre direction in rel_dir! Get help!");
622 return ("Cannot mark planes on the ground");
623 if (p.status == S_MARKED)
624 return ("Already marked");
633 return ("Cannot unmark planes on the ground");
634 if (p.status == S_UNMARKED)
635 return ("Already unmarked");
636 p.status = S_UNMARKED;
644 return ("Cannot ignore planes on the ground");
645 if (p.status == S_IGNORED)
646 return ("Already ignored");
647 p.status = S_IGNORED;
657 case 'w': dir = 0; break;
658 case 'e': dir = 1; break;
659 case 'd': dir = 2; break;
660 case 'c': dir = 3; break;
661 case 'x': dir = 4; break;
662 case 'z': dir = 5; break;
663 case 'a': dir = 6; break;
664 case 'q': dir = 7; break;
666 fprintf(stderr, "bad character in dir_no\n");