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
36 * @(#)input.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/atc/input.c,v 1.7 2000/02/27 23:02:47 mph Exp $
38 * $DragonFly: src/games/atc/input.c,v 1.4 2006/09/09 02:21:49 pavalos Exp $
42 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
44 * Copy permission is hereby granted provided that this notice is
45 * retained on all partial or complete copies.
47 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
53 #include "pathnames.h"
62 #define REDRAWTOKEN '\014' /* CTRL(L) */
63 #define SHELLTOKEN '!'
65 #define ALPHATOKEN 256
72 const char *(*func)(int);
88 #define T_RULE stack[level].rule
89 #define T_STATE stack[level].state
90 #define T_STR stack[level].str
91 #define T_POS stack[level].pos
92 #define T_CH stack[level].ch
94 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
96 #define NUMSTATES NUMELS(st)
99 static void rezero(void);
100 static void push(int, int);
101 static void noise(void);
102 static int gettoken(void);
103 static const char *setplane(int);
104 static const char *turn(int);
105 static const char *circle(int);
106 static const char *left(int);
107 static const char *right(int);
108 static const char *Left(int);
109 static const char *Right(int);
110 static const char *delayb(int);
111 static const char *beacon(int);
112 static const char *ex_it(int);
113 static const char *airport(int);
114 static const char *climb(int);
115 static const char *descend(int);
116 static const char *setalt(int);
117 static const char *setrelalt(int);
118 static const char *benum(int);
119 static const char *to_dir(int);
120 static const char *rel_dir(int);
121 static const char *mark(int);
122 static const char *unmark(int);
123 static const char *ignore(int);
125 RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
126 { RETTOKEN, -1, "", NULL },
128 { CRTOKEN, -1, "", NULL },
130 { HELPTOKEN, 12, " [a-z]<ret>", NULL }},
131 state1[] = { { 't', 2, " turn", turn },
132 { 'a', 3, " altitude:", NULL },
133 { 'c', 4, " circle", circle },
134 { 'm', 7, " mark", mark },
135 { 'u', 7, " unmark", unmark },
136 { 'i', 7, " ignore", ignore },
137 { HELPTOKEN, 12, " tacmui", NULL }},
138 state2[] = { { 'l', 6, " left", left },
139 { 'r', 6, " right", right },
140 { 'L', 4, " left 90", Left },
141 { 'R', 4, " right 90", Right },
142 { 't', 11, " towards", NULL },
143 { 'w', 4, " to 0", to_dir },
144 { 'e', 4, " to 45", to_dir },
145 { 'd', 4, " to 90", to_dir },
146 { 'c', 4, " to 135", to_dir },
147 { 'x', 4, " to 180", to_dir },
148 { 'z', 4, " to 225", to_dir },
149 { 'a', 4, " to 270", to_dir },
150 { 'q', 4, " to 315", to_dir },
151 { HELPTOKEN, 12, " lrLRt<dir>", NULL }},
152 state3[] = { { '+', 10, " climb", climb },
153 { 'c', 10, " climb", climb },
154 { '-', 10, " descend", descend },
155 { 'd', 10, " descend", descend },
156 { NUMTOKEN, 7, " %c000 feet", setalt },
157 { HELPTOKEN, 12, " +-cd[0-9]", NULL }},
158 state4[] = { { '@', 9, " at", NULL },
159 { 'a', 9, " at", NULL },
160 { RETTOKEN, -1, "", NULL },
162 { CRTOKEN, -1, "", NULL },
164 { HELPTOKEN, 12, " @a<ret>", NULL }},
165 state5[] = { { NUMTOKEN, 7, "%c", delayb },
166 { HELPTOKEN, 12, " [0-9]", NULL }},
167 state6[] = { { '@', 9, " at", NULL },
168 { 'a', 9, " at", NULL },
169 { 'w', 4, " 0", rel_dir },
170 { 'e', 4, " 45", rel_dir },
171 { 'd', 4, " 90", rel_dir },
172 { 'c', 4, " 135", rel_dir },
173 { 'x', 4, " 180", rel_dir },
174 { 'z', 4, " 225", rel_dir },
175 { 'a', 4, " 270", rel_dir },
176 { 'q', 4, " 315", rel_dir },
177 { RETTOKEN, -1, "", NULL },
179 { CRTOKEN, -1, "", NULL },
181 { HELPTOKEN, 12, " @a<dir><ret>",NULL }},
182 state7[] = { { RETTOKEN, -1, "", NULL },
184 { CRTOKEN, -1, "", NULL },
186 { HELPTOKEN, 12, " <ret>", NULL }},
187 state8[] = { { NUMTOKEN, 4, "%c", benum },
188 { HELPTOKEN, 12, " [0-9]", NULL }},
189 state9[] = { { 'b', 5, " beacon #", NULL },
190 { '*', 5, " beacon #", NULL },
191 { HELPTOKEN, 12, " b*", NULL }},
192 state10[] = { { NUMTOKEN, 7, " %c000 ft", setrelalt},
193 { HELPTOKEN, 12, " [0-9]", NULL }},
194 state11[] = { { 'b', 8, " beacon #", beacon },
195 { '*', 8, " beacon #", beacon },
196 { 'e', 8, " exit #", ex_it },
197 { 'a', 8, " airport #", airport },
198 { HELPTOKEN, 12, " b*ea", NULL }},
199 state12[] = { { -1, -1, "", NULL }};
201 #define DEF_STATE(s) { NUMELS(s), (s) }
204 DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
205 DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
206 DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
207 DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
212 STACK stack[MAXDEPTH];
215 int dest_type, dest_no, dir;
247 push(int ruleno, int ch)
249 int newstate, newpos;
251 (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
254 newstate = st[T_STATE].rule[ruleno].to_state;
255 newpos = T_POS + strlen(T_STR);
257 ioaddstr(T_POS, T_STR);
272 const char *s, *(*func)(int);
279 if (c == tty_new.sg_erase) {
282 } else if (c == tty_new.sg_kill) {
287 for (i = 0; i < st[T_STATE].num_rules; i++) {
288 if (st[T_STATE].rule[i].token == c ||
289 st[T_STATE].rule[i].token == tval) {
290 push(i, (c >= ALPHATOKEN) ? tval : c);
298 } while (T_STATE != -1);
301 return (1); /* forced update */
303 dest_type = T_NODEST;
305 for (i = 0; i < level; i++) {
306 func = st[stack[i].state].rule[stack[i].rule].func;
308 if ((s = (*func)(stack[i].ch)) != NULL) {
309 ioerror(stack[i].pos, strlen(stack[i].str), s);
314 pp = findplane(p.plane_no);
315 if (pp->new_altitude != p.new_altitude)
316 pp->new_altitude = p.new_altitude;
317 else if (pp->status != p.status)
318 pp->status = p.status;
320 pp->new_dir = p.new_dir;
321 pp->delayd = p.delayd;
322 pp->delayd_no = p.delayd_no;
337 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
339 if (tval == SHELLTOKEN)
342 struct itimerval itv;
343 itv.it_value.tv_sec = 0;
344 itv.it_value.tv_usec = 0;
345 setitimer(ITIMER_REAL, &itv, NULL);
351 if (fork() == 0) /* child */
359 /* run user's favorite shell */
360 if ((shell = getenv("SHELL")) != NULL)
362 base = strrchr(shell, '/');
367 execl(shell, base, (char *) NULL);
370 execl(_PATH_BSHELL, "sh", (char *) NULL);
377 ioctl(fileno(stdin), TIOCSETP, &tty_new);
378 itv.it_value.tv_sec = 0;
379 itv.it_value.tv_usec = 1;
380 itv.it_interval.tv_sec = sp->update_secs;
381 itv.it_interval.tv_usec = 0;
382 setitimer(ITIMER_REAL, &itv, NULL);
385 ioctl(fileno(stdin), TCSETAW, &tty_new);
394 else if (isalpha(tval))
405 pp = findplane(number(c));
407 return ("Unknown Plane");
408 bcopy(pp, &p, sizeof (p));
417 return ("Planes at airports may not change direction");
422 circle(__unused int c)
425 return ("Planes cannot circle on the ground");
434 p.new_dir = p.dir - 1;
441 right(__unused int c)
444 p.new_dir = p.dir + 1;
445 if (p.new_dir >= MAXDIR)
453 p.new_dir = p.dir - 2;
460 Right(__unused int c)
462 p.new_dir = p.dir + 2;
463 if (p.new_dir >= MAXDIR)
475 if (c >= sp->num_beacons)
476 return ("Unknown beacon");
477 xdiff = sp->beacon[c].x - p.xpos;
479 ydiff = sp->beacon[c].y - p.ypos;
481 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
482 return ("Beacon is not in flight path");
486 if (dest_type != T_NODEST) {
489 xdiff = sp->beacon[dest_no].x - sp->beacon[c].x;
490 ydiff = sp->beacon[dest_no].y - sp->beacon[c].y;
493 xdiff = sp->exit[dest_no].x - sp->beacon[c].x;
494 ydiff = sp->exit[dest_no].y - sp->beacon[c].y;
497 xdiff = sp->airport[dest_no].x - sp->beacon[c].x;
498 ydiff = sp->airport[dest_no].y - sp->beacon[c].y;
501 return ("Bad case in delayb! Get help!");
504 if (xdiff == 0 && ydiff == 0)
505 return ("Would already be there");
506 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
507 if (p.new_dir == p.dir)
508 return ("Already going in that direction");
514 beacon(__unused int c)
516 dest_type = T_BEACON;
521 ex_it(__unused int c)
528 airport(__unused int c)
530 dest_type = T_AIRPORT;
535 climb(__unused int c)
542 descend(__unused int c)
551 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
552 return ("Already at that altitude");
553 p.new_altitude = c - '0';
561 return ("altitude not changed");
565 p.new_altitude = p.altitude + c - '0';
568 p.new_altitude = p.altitude - (c - '0');
571 return ("Unknown case in setrelalt! Get help!");
574 if (p.new_altitude < 0)
575 return ("Altitude would be too low");
576 else if (p.new_altitude > 9)
577 return ("Altitude would be too high");
588 if (c >= sp->num_beacons)
589 return ("Unknown beacon");
590 p.new_dir = DIR_FROM_DXDY(sp->beacon[c].x - p.xpos,
591 sp->beacon[c].y - p.ypos);
594 if (c >= sp->num_exits)
595 return ("Unknown exit");
596 p.new_dir = DIR_FROM_DXDY(sp->exit[c].x - p.xpos,
597 sp->exit[c].y - p.ypos);
600 if (c >= sp->num_airports)
601 return ("Unknown airport");
602 p.new_dir = DIR_FROM_DXDY(sp->airport[c].x - p.xpos,
603 sp->airport[c].y - p.ypos);
606 return ("Unknown case in benum! Get help!");
615 p.new_dir = dir_no(c);
627 p.new_dir = p.dir - angle;
632 p.new_dir = p.dir + angle;
633 if (p.new_dir >= MAXDIR)
637 return ("Bizarre direction in rel_dir! Get help!");
647 return ("Cannot mark planes on the ground");
648 if (p.status == S_MARKED)
649 return ("Already marked");
655 unmark(__unused int c)
658 return ("Cannot unmark planes on the ground");
659 if (p.status == S_UNMARKED)
660 return ("Already unmarked");
661 p.status = S_UNMARKED;
666 ignore(__unused int c)
669 return ("Cannot ignore planes on the ground");
670 if (p.status == S_IGNORED)
671 return ("Already ignored");
672 p.status = S_IGNORED;
682 case 'w': dirno = 0; break;
683 case 'e': dirno = 1; break;
684 case 'd': dirno = 2; break;
685 case 'c': dirno = 3; break;
686 case 'x': dirno = 4; break;
687 case 'z': dirno = 5; break;
688 case 'a': dirno = 6; break;
689 case 'q': dirno = 7; break;
691 fprintf(stderr, "bad character in dir_no\n");