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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)input.c 8.1 (Berkeley) 5/31/93
33 * $FreeBSD: src/games/atc/input.c,v 1.7 2000/02/27 23:02:47 mph Exp $
37 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
39 * Copy permission is hereby granted provided that this notice is
40 * retained on all partial or complete copies.
42 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
50 #include "pathnames.h"
56 #define REDRAWTOKEN '\014' /* CTRL(L) */
57 #define SHELLTOKEN '!'
59 #define ALPHATOKEN 256
66 const char *(*func)(int);
82 #define T_RULE stack[level].rule
83 #define T_STATE stack[level].state
84 #define T_STR stack[level].str
85 #define T_POS stack[level].pos
86 #define T_CH stack[level].ch
88 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
90 #define NUMSTATES NUMELS(st)
93 static void rezero(void);
94 static void push(int, int);
95 static void noise(void);
96 static int gettoken(void);
97 static const char *setplane(int);
98 static const char *turn(int);
99 static const char *circle(int);
100 static const char *left(int);
101 static const char *right(int);
102 static const char *Left(int);
103 static const char *Right(int);
104 static const char *delayb(int);
105 static const char *beacon(int);
106 static const char *ex_it(int);
107 static const char *airport(int);
108 static const char *climb(int);
109 static const char *descend(int);
110 static const char *setalt(int);
111 static const char *setrelalt(int);
112 static const char *benum(int);
113 static const char *to_dir(int);
114 static const char *rel_dir(int);
115 static const char *mark(int);
116 static const char *unmark(int);
117 static const char *ignore(int);
119 RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
120 { RETTOKEN, -1, "", NULL },
121 { HELPTOKEN, 12, " [a-z]<ret>", NULL }},
122 state1[] = { { 't', 2, " turn", turn },
123 { 'a', 3, " altitude:", NULL },
124 { 'c', 4, " circle", circle },
125 { 'm', 7, " mark", mark },
126 { 'u', 7, " unmark", unmark },
127 { 'i', 7, " ignore", ignore },
128 { HELPTOKEN, 12, " tacmui", NULL }},
129 state2[] = { { 'l', 6, " left", left },
130 { 'r', 6, " right", right },
131 { 'L', 4, " left 90", Left },
132 { 'R', 4, " right 90", Right },
133 { 't', 11, " towards", NULL },
134 { 'w', 4, " to 0", to_dir },
135 { 'e', 4, " to 45", to_dir },
136 { 'd', 4, " to 90", to_dir },
137 { 'c', 4, " to 135", to_dir },
138 { 'x', 4, " to 180", to_dir },
139 { 'z', 4, " to 225", to_dir },
140 { 'a', 4, " to 270", to_dir },
141 { 'q', 4, " to 315", to_dir },
142 { HELPTOKEN, 12, " lrLRt<dir>", NULL }},
143 state3[] = { { '+', 10, " climb", climb },
144 { 'c', 10, " climb", climb },
145 { '-', 10, " descend", descend },
146 { 'd', 10, " descend", descend },
147 { NUMTOKEN, 7, " %c000 feet", setalt },
148 { HELPTOKEN, 12, " +-cd[0-9]", NULL }},
149 state4[] = { { '@', 9, " at", NULL },
150 { 'a', 9, " at", NULL },
151 { RETTOKEN, -1, "", NULL },
152 { HELPTOKEN, 12, " @a<ret>", NULL }},
153 state5[] = { { NUMTOKEN, 7, "%c", delayb },
154 { HELPTOKEN, 12, " [0-9]", NULL }},
155 state6[] = { { '@', 9, " at", NULL },
156 { 'a', 9, " at", NULL },
157 { 'w', 4, " 0", rel_dir },
158 { 'e', 4, " 45", rel_dir },
159 { 'd', 4, " 90", rel_dir },
160 { 'c', 4, " 135", rel_dir },
161 { 'x', 4, " 180", rel_dir },
162 { 'z', 4, " 225", rel_dir },
163 { 'a', 4, " 270", rel_dir },
164 { 'q', 4, " 315", rel_dir },
165 { RETTOKEN, -1, "", NULL },
166 { HELPTOKEN, 12, " @a<dir><ret>",NULL }},
167 state7[] = { { RETTOKEN, -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;
228 push(int ruleno, int ch)
230 int newstate, newpos;
232 sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
235 newstate = st[T_STATE].rule[ruleno].to_state;
236 newpos = T_POS + strlen(T_STR);
238 ioaddstr(T_POS, T_STR);
253 const char *s, *(*func)(int);
260 if (c == tty_new.c_cc[VERASE]) {
263 } else if (c == tty_new.c_cc[VKILL]) {
268 for (i = 0; i < st[T_STATE].num_rules; i++) {
269 if (st[T_STATE].rule[i].token == c ||
270 st[T_STATE].rule[i].token == tval) {
271 push(i, (c >= ALPHATOKEN) ? tval : c);
279 } while (T_STATE != -1);
282 return (1); /* forced update */
284 dest_type = T_NODEST;
286 for (i = 0; i < level; i++) {
287 func = st[stack[i].state].rule[stack[i].rule].func;
289 if ((s = (*func)(stack[i].ch)) != NULL) {
290 ioerror(stack[i].pos, strlen(stack[i].str), s);
295 pp = findplane(p.plane_no);
296 if (pp->new_altitude != p.new_altitude)
297 pp->new_altitude = p.new_altitude;
298 else if (pp->status != p.status)
299 pp->status = p.status;
301 pp->new_dir = p.new_dir;
302 pp->delayd = p.delayd;
303 pp->delayd_no = p.delayd_no;
318 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
320 if (tval == SHELLTOKEN)
322 struct itimerval itv;
323 itv.it_value.tv_sec = 0;
324 itv.it_value.tv_usec = 0;
325 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, NULL);
345 execl(_PATH_BSHELL, "sh", NULL);
351 tcsetattr(fileno(stdin), TCSANOW, &tty_new);
352 itv.it_value.tv_sec = 0;
353 itv.it_value.tv_usec = 1;
354 itv.it_interval.tv_sec = sp->update_secs;
355 itv.it_interval.tv_usec = 0;
356 setitimer(ITIMER_REAL, &itv, NULL);
363 else if (isalpha(tval))
374 pp = findplane(number(c));
376 return ("Unknown Plane");
377 bcopy(pp, &p, sizeof (p));
386 return ("Planes at airports may not change direction");
391 circle(int c __unused)
394 return ("Planes cannot circle on the ground");
403 p.new_dir = p.dir - 1;
410 right(int c __unused)
413 p.new_dir = p.dir + 1;
414 if (p.new_dir >= MAXDIR)
422 p.new_dir = p.dir - 2;
429 Right(int c __unused)
431 p.new_dir = p.dir + 2;
432 if (p.new_dir >= MAXDIR)
444 if (c >= sp->num_beacons)
445 return ("Unknown beacon");
446 xdiff = sp->beacon[c].x - p.xpos;
448 ydiff = sp->beacon[c].y - p.ypos;
450 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
451 return ("Beacon is not in flight path");
455 if (dest_type != T_NODEST) {
458 xdiff = sp->beacon[dest_no].x - sp->beacon[c].x;
459 ydiff = sp->beacon[dest_no].y - sp->beacon[c].y;
462 xdiff = sp->exit[dest_no].x - sp->beacon[c].x;
463 ydiff = sp->exit[dest_no].y - sp->beacon[c].y;
466 xdiff = sp->airport[dest_no].x - sp->beacon[c].x;
467 ydiff = sp->airport[dest_no].y - sp->beacon[c].y;
470 return ("Bad case in delayb! Get help!");
473 if (xdiff == 0 && ydiff == 0)
474 return ("Would already be there");
475 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
476 if (p.new_dir == p.dir)
477 return ("Already going in that direction");
483 beacon(int c __unused)
485 dest_type = T_BEACON;
490 ex_it(int c __unused)
497 airport(int c __unused)
499 dest_type = T_AIRPORT;
504 climb(int c __unused)
511 descend(int c __unused)
520 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
521 return ("Already at that altitude");
522 if (p.new_altitude == c - '0')
523 return ("Already going to that altitude");
524 p.new_altitude = c - '0';
534 return ("altitude not changed");
538 new_altitude = p.altitude + c - '0';
541 new_altitude = p.altitude - (c - '0');
544 return ("Unknown case in setrelalt! Get help!");
547 if (new_altitude < 0)
548 return ("Altitude would be too low");
549 else if (new_altitude > 9)
550 return ("Altitude would be too high");
551 else if (new_altitude == p.new_altitude)
552 return ("Already going to that altitude");
554 p.new_altitude = new_altitude;
565 if (c >= sp->num_beacons)
566 return ("Unknown beacon");
567 p.new_dir = DIR_FROM_DXDY(sp->beacon[c].x - p.xpos,
568 sp->beacon[c].y - p.ypos);
571 if (c >= sp->num_exits)
572 return ("Unknown exit");
573 p.new_dir = DIR_FROM_DXDY(sp->exit[c].x - p.xpos,
574 sp->exit[c].y - p.ypos);
577 if (c >= sp->num_airports)
578 return ("Unknown airport");
579 p.new_dir = DIR_FROM_DXDY(sp->airport[c].x - p.xpos,
580 sp->airport[c].y - p.ypos);
583 return ("Unknown case in benum! Get help!");
592 p.new_dir = dir_no(c);
604 p.new_dir = p.dir - angle;
609 p.new_dir = p.dir + angle;
610 if (p.new_dir >= MAXDIR)
614 return ("Bizarre direction in rel_dir! Get help!");
624 return ("Cannot mark planes on the ground");
625 if (p.status == S_MARKED)
626 return ("Already marked");
632 unmark(int c __unused)
635 return ("Cannot unmark planes on the ground");
636 if (p.status == S_UNMARKED)
637 return ("Already unmarked");
638 p.status = S_UNMARKED;
643 ignore(int c __unused)
646 return ("Cannot ignore planes on the ground");
647 if (p.status == S_IGNORED)
648 return ("Already ignored");
649 p.status = S_IGNORED;
660 case 'w': dirno = 0; break;
661 case 'e': dirno = 1; break;
662 case 'd': dirno = 2; break;
663 case 'c': dirno = 3; break;
664 case 'x': dirno = 4; break;
665 case 'z': dirno = 5; break;
666 case 'a': dirno = 6; break;
667 case 'q': dirno = 7; break;
669 fprintf(stderr, "bad character in dir_no\n");