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 $
34 * $DragonFly: src/games/atc/input.c,v 1.4 2006/09/09 02:21:49 pavalos Exp $
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.
51 #include "pathnames.h"
57 #define REDRAWTOKEN '\014' /* CTRL(L) */
58 #define SHELLTOKEN '!'
60 #define ALPHATOKEN 256
67 const char *(*func
)(int);
83 #define T_RULE stack[level].rule
84 #define T_STATE stack[level].state
85 #define T_STR stack[level].str
86 #define T_POS stack[level].pos
87 #define T_CH stack[level].ch
89 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
91 #define NUMSTATES NUMELS(st)
94 static void rezero(void);
95 static void push(int, int);
96 static void noise(void);
97 static int gettoken(void);
98 static const char *setplane(int);
99 static const char *turn(int);
100 static const char *circle(int);
101 static const char *left(int);
102 static const char *right(int);
103 static const char *Left(int);
104 static const char *Right(int);
105 static const char *delayb(int);
106 static const char *beacon(int);
107 static const char *ex_it(int);
108 static const char *airport(int);
109 static const char *climb(int);
110 static const char *descend(int);
111 static const char *setalt(int);
112 static const char *setrelalt(int);
113 static const char *benum(int);
114 static const char *to_dir(int);
115 static const char *rel_dir(int);
116 static const char *mark(int);
117 static const char *unmark(int);
118 static const char *ignore(int);
120 RULE state0
[] = { { ALPHATOKEN
, 1, "%c:", setplane
},
121 { RETTOKEN
, -1, "", NULL
},
122 { HELPTOKEN
, 12, " [a-z]<ret>", NULL
}},
123 state1
[] = { { 't', 2, " turn", turn
},
124 { 'a', 3, " altitude:", NULL
},
125 { 'c', 4, " circle", circle
},
126 { 'm', 7, " mark", mark
},
127 { 'u', 7, " unmark", unmark
},
128 { 'i', 7, " ignore", ignore
},
129 { HELPTOKEN
, 12, " tacmui", NULL
}},
130 state2
[] = { { 'l', 6, " left", left
},
131 { 'r', 6, " right", right
},
132 { 'L', 4, " left 90", Left
},
133 { 'R', 4, " right 90", Right
},
134 { 't', 11, " towards", NULL
},
135 { 'w', 4, " to 0", to_dir
},
136 { 'e', 4, " to 45", to_dir
},
137 { 'd', 4, " to 90", to_dir
},
138 { 'c', 4, " to 135", to_dir
},
139 { 'x', 4, " to 180", to_dir
},
140 { 'z', 4, " to 225", to_dir
},
141 { 'a', 4, " to 270", to_dir
},
142 { 'q', 4, " to 315", to_dir
},
143 { HELPTOKEN
, 12, " lrLRt<dir>", NULL
}},
144 state3
[] = { { '+', 10, " climb", climb
},
145 { 'c', 10, " climb", climb
},
146 { '-', 10, " descend", descend
},
147 { 'd', 10, " descend", descend
},
148 { NUMTOKEN
, 7, " %c000 feet", setalt
},
149 { HELPTOKEN
, 12, " +-cd[0-9]", NULL
}},
150 state4
[] = { { '@', 9, " at", NULL
},
151 { 'a', 9, " at", NULL
},
152 { RETTOKEN
, -1, "", NULL
},
153 { HELPTOKEN
, 12, " @a<ret>", NULL
}},
154 state5
[] = { { NUMTOKEN
, 7, "%c", delayb
},
155 { HELPTOKEN
, 12, " [0-9]", NULL
}},
156 state6
[] = { { '@', 9, " at", NULL
},
157 { 'a', 9, " at", NULL
},
158 { 'w', 4, " 0", rel_dir
},
159 { 'e', 4, " 45", rel_dir
},
160 { 'd', 4, " 90", rel_dir
},
161 { 'c', 4, " 135", rel_dir
},
162 { 'x', 4, " 180", rel_dir
},
163 { 'z', 4, " 225", rel_dir
},
164 { 'a', 4, " 270", rel_dir
},
165 { 'q', 4, " 315", rel_dir
},
166 { RETTOKEN
, -1, "", NULL
},
167 { HELPTOKEN
, 12, " @a<dir><ret>",NULL
}},
168 state7
[] = { { RETTOKEN
, -1, "", NULL
},
169 { HELPTOKEN
, 12, " <ret>", NULL
}},
170 state8
[] = { { NUMTOKEN
, 4, "%c", benum
},
171 { HELPTOKEN
, 12, " [0-9]", NULL
}},
172 state9
[] = { { 'b', 5, " beacon #", NULL
},
173 { '*', 5, " beacon #", NULL
},
174 { HELPTOKEN
, 12, " b*", NULL
}},
175 state10
[] = { { NUMTOKEN
, 7, " %c000 ft", setrelalt
},
176 { HELPTOKEN
, 12, " [0-9]", NULL
}},
177 state11
[] = { { 'b', 8, " beacon #", beacon
},
178 { '*', 8, " beacon #", beacon
},
179 { 'e', 8, " exit #", ex_it
},
180 { 'a', 8, " airport #", airport
},
181 { HELPTOKEN
, 12, " b*ea", NULL
}},
182 state12
[] = { { -1, -1, "", NULL
}};
184 #define DEF_STATE(s) { NUMELS(s), (s) }
187 DEF_STATE(state0
), DEF_STATE(state1
), DEF_STATE(state2
),
188 DEF_STATE(state3
), DEF_STATE(state4
), DEF_STATE(state5
),
189 DEF_STATE(state6
), DEF_STATE(state7
), DEF_STATE(state8
),
190 DEF_STATE(state9
), DEF_STATE(state10
), DEF_STATE(state11
),
195 STACK stack
[MAXDEPTH
];
198 int dest_type
, dest_no
, dir
;
229 push(int ruleno
, int ch
)
231 int newstate
, newpos
;
233 (void)sprintf(T_STR
, st
[T_STATE
].rule
[ruleno
].str
, tval
);
236 newstate
= st
[T_STATE
].rule
[ruleno
].to_state
;
237 newpos
= T_POS
+ strlen(T_STR
);
239 ioaddstr(T_POS
, T_STR
);
254 const char *s
, *(*func
)(int);
261 if (c
== tty_new
.c_cc
[VERASE
]) {
264 } else if (c
== tty_new
.c_cc
[VKILL
]) {
269 for (i
= 0; i
< st
[T_STATE
].num_rules
; i
++) {
270 if (st
[T_STATE
].rule
[i
].token
== c
||
271 st
[T_STATE
].rule
[i
].token
== tval
) {
272 push(i
, (c
>= ALPHATOKEN
) ? tval
: c
);
280 } while (T_STATE
!= -1);
283 return (1); /* forced update */
285 dest_type
= T_NODEST
;
287 for (i
= 0; i
< level
; i
++) {
288 func
= st
[stack
[i
].state
].rule
[stack
[i
].rule
].func
;
290 if ((s
= (*func
)(stack
[i
].ch
)) != NULL
) {
291 ioerror(stack
[i
].pos
, strlen(stack
[i
].str
), s
);
296 pp
= findplane(p
.plane_no
);
297 if (pp
->new_altitude
!= p
.new_altitude
)
298 pp
->new_altitude
= p
.new_altitude
;
299 else if (pp
->status
!= p
.status
)
300 pp
->status
= p
.status
;
302 pp
->new_dir
= p
.new_dir
;
303 pp
->delayd
= p
.delayd
;
304 pp
->delayd_no
= p
.delayd_no
;
319 while ((tval
= getAChar()) == REDRAWTOKEN
|| tval
== SHELLTOKEN
)
321 if (tval
== SHELLTOKEN
)
323 struct itimerval itv
;
324 itv
.it_value
.tv_sec
= 0;
325 itv
.it_value
.tv_usec
= 0;
326 setitimer(ITIMER_REAL
, &itv
, NULL
);
327 if (fork() == 0) /* child */
335 /* run user's favorite shell */
336 if ((shell
= getenv("SHELL")) != NULL
)
338 base
= strrchr(shell
, '/');
343 execl(shell
, base
, NULL
);
346 execl(_PATH_BSHELL
, "sh", NULL
);
352 tcsetattr(fileno(stdin
), TCSANOW
, &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
);
364 else if (isalpha(tval
))
375 pp
= findplane(number(c
));
377 return ("Unknown Plane");
378 bcopy(pp
, &p
, sizeof (p
));
387 return ("Planes at airports may not change direction");
392 circle(int c __unused
)
395 return ("Planes cannot circle on the ground");
404 p
.new_dir
= p
.dir
- 1;
411 right(int c __unused
)
414 p
.new_dir
= p
.dir
+ 1;
415 if (p
.new_dir
>= MAXDIR
)
423 p
.new_dir
= p
.dir
- 2;
430 Right(int c __unused
)
432 p
.new_dir
= p
.dir
+ 2;
433 if (p
.new_dir
>= MAXDIR
)
445 if (c
>= sp
->num_beacons
)
446 return ("Unknown beacon");
447 xdiff
= sp
->beacon
[c
].x
- p
.xpos
;
449 ydiff
= sp
->beacon
[c
].y
- p
.ypos
;
451 if (xdiff
!= displacement
[p
.dir
].dx
|| ydiff
!= displacement
[p
.dir
].dy
)
452 return ("Beacon is not in flight path");
456 if (dest_type
!= T_NODEST
) {
459 xdiff
= sp
->beacon
[dest_no
].x
- sp
->beacon
[c
].x
;
460 ydiff
= sp
->beacon
[dest_no
].y
- sp
->beacon
[c
].y
;
463 xdiff
= sp
->exit
[dest_no
].x
- sp
->beacon
[c
].x
;
464 ydiff
= sp
->exit
[dest_no
].y
- sp
->beacon
[c
].y
;
467 xdiff
= sp
->airport
[dest_no
].x
- sp
->beacon
[c
].x
;
468 ydiff
= sp
->airport
[dest_no
].y
- sp
->beacon
[c
].y
;
471 return ("Bad case in delayb! Get help!");
474 if (xdiff
== 0 && ydiff
== 0)
475 return ("Would already be there");
476 p
.new_dir
= DIR_FROM_DXDY(xdiff
, ydiff
);
477 if (p
.new_dir
== p
.dir
)
478 return ("Already going in that direction");
484 beacon(int c __unused
)
486 dest_type
= T_BEACON
;
491 ex_it(int c __unused
)
498 airport(int c __unused
)
500 dest_type
= T_AIRPORT
;
505 climb(int c __unused
)
512 descend(int c __unused
)
521 if ((p
.altitude
== c
- '0') && (p
.new_altitude
== p
.altitude
))
522 return ("Already at that altitude");
523 if (p
.new_altitude
== c
- '0')
524 return ("Already going to that altitude");
525 p
.new_altitude
= c
- '0';
535 return ("altitude not changed");
539 new_altitude
= p
.altitude
+ c
- '0';
542 new_altitude
= p
.altitude
- (c
- '0');
545 return ("Unknown case in setrelalt! Get help!");
548 if (new_altitude
< 0)
549 return ("Altitude would be too low");
550 else if (new_altitude
> 9)
551 return ("Altitude would be too high");
552 else if (new_altitude
== p
.new_altitude
)
553 return ("Already going to that altitude");
555 p
.new_altitude
= new_altitude
;
566 if (c
>= sp
->num_beacons
)
567 return ("Unknown beacon");
568 p
.new_dir
= DIR_FROM_DXDY(sp
->beacon
[c
].x
- p
.xpos
,
569 sp
->beacon
[c
].y
- p
.ypos
);
572 if (c
>= sp
->num_exits
)
573 return ("Unknown exit");
574 p
.new_dir
= DIR_FROM_DXDY(sp
->exit
[c
].x
- p
.xpos
,
575 sp
->exit
[c
].y
- p
.ypos
);
578 if (c
>= sp
->num_airports
)
579 return ("Unknown airport");
580 p
.new_dir
= DIR_FROM_DXDY(sp
->airport
[c
].x
- p
.xpos
,
581 sp
->airport
[c
].y
- p
.ypos
);
584 return ("Unknown case in benum! Get help!");
593 p
.new_dir
= dir_no(c
);
605 p
.new_dir
= p
.dir
- angle
;
610 p
.new_dir
= p
.dir
+ angle
;
611 if (p
.new_dir
>= MAXDIR
)
615 return ("Bizarre direction in rel_dir! Get help!");
625 return ("Cannot mark planes on the ground");
626 if (p
.status
== S_MARKED
)
627 return ("Already marked");
633 unmark(int c __unused
)
636 return ("Cannot unmark planes on the ground");
637 if (p
.status
== S_UNMARKED
)
638 return ("Already unmarked");
639 p
.status
= S_UNMARKED
;
644 ignore(int c __unused
)
647 return ("Cannot ignore planes on the ground");
648 if (p
.status
== S_IGNORED
)
649 return ("Already ignored");
650 p
.status
= S_IGNORED
;
661 case 'w': dirno
= 0; break;
662 case 'e': dirno
= 1; break;
663 case 'd': dirno
= 2; break;
664 case 'c': dirno
= 3; break;
665 case 'x': dirno
= 4; break;
666 case 'z': dirno
= 5; break;
667 case 'a': dirno
= 6; break;
668 case 'q': dirno
= 7; break;
670 fprintf(stderr
, "bad character in dir_no\n");