1 /* $NetBSD: input.c,v 1.23 2009/05/26 00:00:56 dholland Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
38 * Copy permission is hereby granted provided that this notice is
39 * retained on all partial or complete copies.
41 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
44 #include <sys/cdefs.h>
47 static char sccsid
[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
49 __RCSID("$NetBSD: input.c,v 1.23 2009/05/26 00:00:56 dholland Exp $");
54 #include "pathnames.h"
56 static void rezero(void);
57 static void noise(void);
58 static int gettoken(void);
59 static const char *setplane(int);
60 static const char *turn(int);
61 static const char *circle(int);
62 static const char *left(int);
63 static const char *right(int);
64 static const char *Left(int);
65 static const char *Right(int);
66 static const char *delayb(int);
67 static const char *beacon(int);
68 static const char *ex_it(int);
69 static const char *airport(int);
70 static const char *climb(int);
71 static const char *descend(int);
72 static const char *setalt(int);
73 static const char *setrelalt(int);
74 static const char *benum(int);
75 static const char *to_dir(int);
76 static const char *rel_dir(int);
77 static const char *mark(int);
78 static const char *unmark(int);
79 static const char *ignore(int);
87 #define REDRAWTOKEN '\014' /* CTRL(L) */
88 #define SHELLTOKEN '!'
90 #define ALPHATOKEN 256
97 const char *(*func
)(int);
113 #define T_RULE stack[level].rule
114 #define T_STATE stack[level].state
115 #define T_STR stack[level].str
116 #define T_POS stack[level].pos
117 #define T_CH stack[level].ch
119 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
121 #define NUMSTATES NUMELS(st)
124 RULE state0
[] = { { ALPHATOKEN
, 1, "%c:", setplane
},
125 { RETTOKEN
, -1, "", NULL
},
126 { HELPTOKEN
, 12, " [a-z]<ret>", NULL
}},
127 state1
[] = { { 't', 2, " turn", turn
},
128 { 'a', 3, " altitude:", NULL
},
129 { 'c', 4, " circle", circle
},
130 { 'm', 7, " mark", mark
},
131 { 'u', 7, " unmark", unmark
},
132 { 'i', 7, " ignore", ignore
},
133 { HELPTOKEN
, 12, " tacmui", NULL
}},
134 state2
[] = { { 'l', 6, " left", left
},
135 { 'r', 6, " right", right
},
136 { 'L', 4, " left 90", Left
},
137 { 'R', 4, " right 90", Right
},
138 { 't', 11, " towards", NULL
},
139 { 'w', 4, " to 0", to_dir
},
140 { 'e', 4, " to 45", to_dir
},
141 { 'd', 4, " to 90", to_dir
},
142 { 'c', 4, " to 135", to_dir
},
143 { 'x', 4, " to 180", to_dir
},
144 { 'z', 4, " to 225", to_dir
},
145 { 'a', 4, " to 270", to_dir
},
146 { 'q', 4, " to 315", to_dir
},
147 { HELPTOKEN
, 12, " lrLRt<dir>", NULL
}},
148 state3
[] = { { '+', 10, " climb", climb
},
149 { 'c', 10, " climb", climb
},
150 { '-', 10, " descend", descend
},
151 { 'd', 10, " descend", descend
},
152 { NUMTOKEN
, 7, " %c000 feet", setalt
},
153 { HELPTOKEN
, 12, " +-cd[0-9]", NULL
}},
154 state4
[] = { { '@', 9, " at", NULL
},
155 { 'a', 9, " at", NULL
},
156 { RETTOKEN
, -1, "", NULL
},
157 { HELPTOKEN
, 12, " @a<ret>", NULL
}},
158 state5
[] = { { NUMTOKEN
, 7, "%c", delayb
},
159 { HELPTOKEN
, 12, " [0-9]", NULL
}},
160 state6
[] = { { '@', 9, " at", NULL
},
161 { 'a', 9, " at", NULL
},
162 { 'w', 4, " 0", rel_dir
},
163 { 'e', 4, " 45", rel_dir
},
164 { 'd', 4, " 90", rel_dir
},
165 { 'c', 4, " 135", rel_dir
},
166 { 'x', 4, " 180", rel_dir
},
167 { 'z', 4, " 225", rel_dir
},
168 { 'a', 4, " 270", rel_dir
},
169 { 'q', 4, " 315", rel_dir
},
170 { RETTOKEN
, -1, "", NULL
},
171 { HELPTOKEN
, 12, " @a<dir><ret>",NULL
}},
172 state7
[] = { { RETTOKEN
, -1, "", NULL
},
173 { HELPTOKEN
, 12, " <ret>", NULL
}},
174 state8
[] = { { NUMTOKEN
, 4, "%c", benum
},
175 { HELPTOKEN
, 12, " [0-9]", NULL
}},
176 state9
[] = { { 'b', 5, " beacon #", NULL
},
177 { '*', 5, " beacon #", NULL
},
178 { HELPTOKEN
, 12, " b*", NULL
}},
179 state10
[] = { { NUMTOKEN
, 7, " %c000 ft", setrelalt
},
180 { HELPTOKEN
, 12, " [0-9]", NULL
}},
181 state11
[] = { { 'b', 8, " beacon #", beacon
},
182 { '*', 8, " beacon #", beacon
},
183 { 'e', 8, " exit #", ex_it
},
184 { 'a', 8, " airport #", airport
},
185 { HELPTOKEN
, 12, " b*ea", NULL
}},
186 state12
[] = { { -1, -1, "", NULL
}};
188 #define DEF_STATE(s) { NUMELS(s), (s) }
190 static STATE st
[] = {
191 DEF_STATE(state0
), DEF_STATE(state1
), DEF_STATE(state2
),
192 DEF_STATE(state3
), DEF_STATE(state4
), DEF_STATE(state5
),
193 DEF_STATE(state6
), DEF_STATE(state7
), DEF_STATE(state8
),
194 DEF_STATE(state9
), DEF_STATE(state10
), DEF_STATE(state11
),
199 static STACK stack
[MAXDEPTH
];
202 static int dest_type
, dest_no
, dir
;
213 (void)strcpy(T_STR
, "");
229 (void)strcpy(T_STR
, "");
233 push(int ruleno
, int ch
)
235 int newstate
, newpos
;
237 assert(level
< (MAXDEPTH
- 1));
238 (void)snprintf(T_STR
, sizeof(T_STR
),
239 st
[T_STATE
].rule
[ruleno
].str
, tval
);
242 newstate
= st
[T_STATE
].rule
[ruleno
].to_state
;
243 newpos
= T_POS
+ strlen(T_STR
);
245 ioaddstr(T_POS
, T_STR
);
253 (void)strcpy(T_STR
, "");
260 const char *s
, *(*func
)(int);
267 if (c
== tty_new
.c_cc
[VERASE
]) {
270 } else if (c
== tty_new
.c_cc
[VKILL
]) {
275 for (i
= 0; i
< st
[T_STATE
].num_rules
; i
++) {
276 if (st
[T_STATE
].rule
[i
].token
== c
||
277 st
[T_STATE
].rule
[i
].token
== tval
) {
278 push(i
, (c
>= ALPHATOKEN
) ? tval
: c
);
286 } while (T_STATE
!= -1);
289 return (1); /* forced update */
291 dest_type
= T_NODEST
;
293 for (i
= 0; i
< level
; i
++) {
294 func
= st
[stack
[i
].state
].rule
[stack
[i
].rule
].func
;
296 if ((s
= (*func
)(stack
[i
].ch
)) != NULL
) {
297 ioerror(stack
[i
].pos
,
298 (int)strlen(stack
[i
].str
), s
);
303 pp
= findplane(p
.plane_no
);
304 if (pp
->new_altitude
!= p
.new_altitude
)
305 pp
->new_altitude
= p
.new_altitude
;
306 else if (pp
->status
!= p
.status
)
307 pp
->status
= p
.status
;
309 pp
->new_dir
= p
.new_dir
;
310 pp
->delayd
= p
.delayd
;
311 pp
->delayd_no
= p
.delayd_no
;
319 (void)putchar('\07');
320 (void)fflush(stdout
);
326 while ((tval
= getAChar()) == REDRAWTOKEN
|| tval
== SHELLTOKEN
)
328 if (tval
== SHELLTOKEN
)
331 struct itimerval itv
;
332 itv
.it_value
.tv_sec
= 0;
333 itv
.it_value
.tv_usec
= 0;
334 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
340 if (fork() == 0) /* child */
346 /* run user's favorite shell */
347 if ((shell
= getenv("SHELL")) != NULL
)
349 base
= strrchr(shell
, '/');
354 (void)execl(shell
, base
, (char *) 0);
357 (void)execl(_PATH_BSHELL
, "sh",
364 (void)tcsetattr(fileno(stdin
), TCSADRAIN
, &tty_new
);
366 itv
.it_value
.tv_sec
= 0;
367 itv
.it_value
.tv_usec
= 1;
368 itv
.it_interval
.tv_sec
= sp
->update_secs
;
369 itv
.it_interval
.tv_usec
= 0;
370 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
381 else if (isalpha(tval
))
392 pp
= findplane(number(c
));
394 return ("Unknown Plane");
395 (void)memcpy(&p
, pp
, sizeof (p
));
405 return ("Planes at airports may not change direction");
411 circle(int c __unused
)
414 return ("Planes cannot circle on the ground");
424 p
.new_dir
= p
.dir
- 1;
432 right(int c __unused
)
435 p
.new_dir
= p
.dir
+ 1;
436 if (p
.new_dir
>= MAXDIR
)
445 p
.new_dir
= p
.dir
- 2;
453 Right(int c __unused
)
455 p
.new_dir
= p
.dir
+ 2;
456 if (p
.new_dir
>= MAXDIR
)
468 if (c
>= sp
->num_beacons
)
469 return ("Unknown beacon");
470 xdiff
= sp
->beacon
[(int)c
].x
- p
.xpos
;
472 ydiff
= sp
->beacon
[(int)c
].y
- p
.ypos
;
474 if (xdiff
!= displacement
[p
.dir
].dx
|| ydiff
!= displacement
[p
.dir
].dy
)
475 return ("Beacon is not in flight path");
479 if (dest_type
!= T_NODEST
) {
482 xdiff
= sp
->beacon
[dest_no
].x
- sp
->beacon
[(int)c
].x
;
483 ydiff
= sp
->beacon
[dest_no
].y
- sp
->beacon
[(int)c
].y
;
486 xdiff
= sp
->exit
[dest_no
].x
- sp
->beacon
[(int)c
].x
;
487 ydiff
= sp
->exit
[dest_no
].y
- sp
->beacon
[(int)c
].y
;
490 xdiff
= sp
->airport
[dest_no
].x
- sp
->beacon
[(int)c
].x
;
491 ydiff
= sp
->airport
[dest_no
].y
- sp
->beacon
[(int)c
].y
;
494 return ("Bad case in delayb! Get help!");
496 if (xdiff
== 0 && ydiff
== 0)
497 return ("Would already be there");
498 p
.new_dir
= DIR_FROM_DXDY(xdiff
, ydiff
);
499 if (p
.new_dir
== p
.dir
)
500 return ("Already going in that direction");
507 beacon(int c __unused
)
509 dest_type
= T_BEACON
;
515 ex_it(int c __unused
)
523 airport(int c __unused
)
525 dest_type
= T_AIRPORT
;
531 climb(int c __unused
)
539 descend(int c __unused
)
548 int newalt
= c
- '0';
549 if ((p
.altitude
== newalt
) && (p
.new_altitude
== p
.altitude
))
550 return ("Already at that altitude");
551 if (p
.new_altitude
== newalt
) {
552 return ("Already going to that altitude");
554 p
.new_altitude
= newalt
;
564 return ("altitude not changed");
568 newalt
= p
.altitude
+ c
- '0';
571 newalt
= p
.altitude
- (c
- '0');
574 return ("Unknown case in setrelalt! Get help!");
577 if (p
.new_altitude
== newalt
)
578 return ("Already going to that altitude");
580 p
.new_altitude
= newalt
;
582 if (p
.new_altitude
< 0)
583 return ("Altitude would be too low");
584 else if (p
.new_altitude
> 9)
585 return ("Altitude would be too high");
596 if (c
>= sp
->num_beacons
)
597 return ("Unknown beacon");
598 p
.new_dir
= DIR_FROM_DXDY(sp
->beacon
[(int)c
].x
- p
.xpos
,
599 sp
->beacon
[(int)c
].y
- p
.ypos
);
602 if (c
>= sp
->num_exits
)
603 return ("Unknown exit");
604 p
.new_dir
= DIR_FROM_DXDY(sp
->exit
[(int)c
].x
- p
.xpos
,
605 sp
->exit
[(int)c
].y
- p
.ypos
);
608 if (c
>= sp
->num_airports
)
609 return ("Unknown airport");
610 p
.new_dir
= DIR_FROM_DXDY(sp
->airport
[(int)c
].x
- p
.xpos
,
611 sp
->airport
[(int)c
].y
- p
.ypos
);
614 return ("Unknown case in benum! Get help!");
622 p
.new_dir
= dir_no(c
);
634 p
.new_dir
= p
.dir
- angle
;
639 p
.new_dir
= p
.dir
+ angle
;
640 if (p
.new_dir
>= MAXDIR
)
644 return ("Bizarre direction in rel_dir! Get help!");
654 return ("Cannot mark planes on the ground");
655 if (p
.status
== S_MARKED
)
656 return ("Already marked");
663 unmark(int c __unused
)
666 return ("Cannot unmark planes on the ground");
667 if (p
.status
== S_UNMARKED
)
668 return ("Already unmarked");
669 p
.status
= S_UNMARKED
;
675 ignore(int c __unused
)
678 return ("Cannot ignore planes on the ground");
679 if (p
.status
== S_IGNORED
)
680 return ("Already ignored");
681 p
.status
= S_IGNORED
;
692 case 'w': dirno
= 0; break;
693 case 'e': dirno
= 1; break;
694 case 'd': dirno
= 2; break;
695 case 'c': dirno
= 3; break;
696 case 'x': dirno
= 4; break;
697 case 'z': dirno
= 5; break;
698 case 'a': dirno
= 6; break;
699 case 'q': dirno
= 7; break;
701 (void)fprintf(stderr
, "bad character in dir_no\n");