]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/input.c
3b44be094b269a6206a1b43b045c358bcacabee7
1 /* $NetBSD: input.c,v 1.29 2015/06/19 06:02:31 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.29 2015/06/19 06:02:31 dholland Exp $");
53 #include <sys/types.h>
64 #include "pathnames.h"
70 static void rezero(void);
71 static void noise(void);
72 static int gettoken(void);
73 static const char *setplane(int);
74 static const char *turn(int);
75 static const char *circle(int);
76 static const char *left(int);
77 static const char *right(int);
78 static const char *Left(int);
79 static const char *Right(int);
80 static const char *delayb(int);
81 static const char *beacon(int);
82 static const char *ex_it(int);
83 static const char *airport(int);
84 static const char *climb(int);
85 static const char *descend(int);
86 static const char *setalt(int);
87 static const char *setrelalt(int);
88 static const char *benum(int);
89 static const char *to_dir(int);
90 static const char *rel_dir(int);
91 static const char *mark(int);
92 static const char *unmark(int);
93 static const char *ignore(int);
100 #define RETTOKEN '\n'
101 #define REDRAWTOKEN '\014' /* CTRL(L) */
102 #define SHELLTOKEN '!'
103 #define HELPTOKEN '?'
104 #define ALPHATOKEN 256
111 const char *(*func
)(int);
127 #define T_RULE stack[level].rule
128 #define T_STATE stack[level].state
129 #define T_STR stack[level].str
130 #define T_POS stack[level].pos
131 #define T_CH stack[level].ch
133 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
135 #define NUMSTATES NUMELS(st)
138 RULE state0
[] = { { ALPHATOKEN
, 1, "%c:", setplane
},
139 { RETTOKEN
, -1, "", NULL
},
140 { HELPTOKEN
, 12, " [a-z]<ret>", NULL
}},
141 state1
[] = { { 't', 2, " turn", turn
},
142 { 'a', 3, " altitude:", NULL
},
143 { 'c', 4, " circle", circle
},
144 { 'm', 7, " mark", mark
},
145 { 'u', 7, " unmark", unmark
},
146 { 'i', 7, " ignore", ignore
},
147 { HELPTOKEN
, 12, " tacmui", NULL
}},
148 state2
[] = { { 'l', 6, " left", left
},
149 { 'r', 6, " right", right
},
150 { 'L', 4, " left 90", Left
},
151 { 'R', 4, " right 90", Right
},
152 { 't', 11, " towards", NULL
},
153 { 'w', 4, " to 0", to_dir
},
154 { 'e', 4, " to 45", to_dir
},
155 { 'd', 4, " to 90", to_dir
},
156 { 'c', 4, " to 135", to_dir
},
157 { 'x', 4, " to 180", to_dir
},
158 { 'z', 4, " to 225", to_dir
},
159 { 'a', 4, " to 270", to_dir
},
160 { 'q', 4, " to 315", to_dir
},
161 { HELPTOKEN
, 12, " lrLRt<dir>", NULL
}},
162 state3
[] = { { '+', 10, " climb", climb
},
163 { 'c', 10, " climb", climb
},
164 { '-', 10, " descend", descend
},
165 { 'd', 10, " descend", descend
},
166 { NUMTOKEN
, 7, " %c000 feet", setalt
},
167 { HELPTOKEN
, 12, " +-cd[0-9]", NULL
}},
168 state4
[] = { { '@', 9, " at", NULL
},
169 { 'a', 9, " at", NULL
},
170 { RETTOKEN
, -1, "", NULL
},
171 { HELPTOKEN
, 12, " @a<ret>", NULL
}},
172 state5
[] = { { NUMTOKEN
, 7, "%c", delayb
},
173 { HELPTOKEN
, 12, " [0-9]", NULL
}},
174 state6
[] = { { '@', 9, " at", NULL
},
175 { 'a', 9, " at", NULL
},
176 { 'w', 4, " 0", rel_dir
},
177 { 'e', 4, " 45", rel_dir
},
178 { 'd', 4, " 90", rel_dir
},
179 { 'c', 4, " 135", rel_dir
},
180 { 'x', 4, " 180", rel_dir
},
181 { 'z', 4, " 225", rel_dir
},
182 { 'a', 4, " 270", rel_dir
},
183 { 'q', 4, " 315", rel_dir
},
184 { RETTOKEN
, -1, "", NULL
},
185 { HELPTOKEN
, 12, " @a<dir><ret>",NULL
}},
186 state7
[] = { { RETTOKEN
, -1, "", NULL
},
187 { HELPTOKEN
, 12, " <ret>", NULL
}},
188 state8
[] = { { NUMTOKEN
, 4, "%c", benum
},
189 { HELPTOKEN
, 12, " [0-9]", NULL
}},
190 state9
[] = { { 'b', 5, " beacon #", NULL
},
191 { '*', 5, " beacon #", NULL
},
192 { HELPTOKEN
, 12, " b*", NULL
}},
193 state10
[] = { { NUMTOKEN
, 7, " %c000 ft", setrelalt
},
194 { HELPTOKEN
, 12, " [0-9]", NULL
}},
195 state11
[] = { { 'b', 8, " beacon #", beacon
},
196 { '*', 8, " beacon #", beacon
},
197 { 'e', 8, " exit #", ex_it
},
198 { 'a', 8, " airport #", airport
},
199 { HELPTOKEN
, 12, " b*ea", NULL
}},
200 state12
[] = { { -1, -1, "", NULL
}};
202 #define DEF_STATE(s) { NUMELS(s), (s) }
204 static STATE st
[] = {
205 DEF_STATE(state0
), DEF_STATE(state1
), DEF_STATE(state2
),
206 DEF_STATE(state3
), DEF_STATE(state4
), DEF_STATE(state5
),
207 DEF_STATE(state6
), DEF_STATE(state7
), DEF_STATE(state8
),
208 DEF_STATE(state9
), DEF_STATE(state10
), DEF_STATE(state11
),
213 static STACK stack
[MAXDEPTH
];
217 static enum places dest_type
;
218 static unsigned dest_no
;
229 (void)strcpy(T_STR
, "");
245 (void)strcpy(T_STR
, "");
249 push(int ruleno
, int ch
)
251 int newstate
, newpos
;
253 assert(level
< (MAXDEPTH
- 1));
254 (void)snprintf(T_STR
, sizeof(T_STR
),
255 st
[T_STATE
].rule
[ruleno
].str
, tval
);
258 newstate
= st
[T_STATE
].rule
[ruleno
].to_state
;
259 newpos
= T_POS
+ strlen(T_STR
);
261 ioaddstr(T_POS
, T_STR
);
269 (void)strcpy(T_STR
, "");
276 const char *s
, *(*func
)(int);
283 if (c
== tty_new
.c_cc
[VERASE
]) {
286 } else if (c
== tty_new
.c_cc
[VKILL
]) {
291 for (i
= 0; i
< st
[T_STATE
].num_rules
; i
++) {
292 if (st
[T_STATE
].rule
[i
].token
== c
||
293 st
[T_STATE
].rule
[i
].token
== tval
) {
294 push(i
, (c
>= ALPHATOKEN
) ? tval
: c
);
302 } while (T_STATE
!= -1);
305 return (1); /* forced update */
307 dest_type
= T_NODEST
;
309 for (i
= 0; i
< level
; i
++) {
310 func
= st
[stack
[i
].state
].rule
[stack
[i
].rule
].func
;
312 if ((s
= (*func
)(stack
[i
].ch
)) != NULL
) {
313 ioerror(stack
[i
].pos
,
314 (int)strlen(stack
[i
].str
), s
);
319 pp
= findplane(p
.plane_no
);
320 if (pp
->new_altitude
!= p
.new_altitude
)
321 pp
->new_altitude
= p
.new_altitude
;
322 else if (pp
->status
!= p
.status
)
323 pp
->status
= p
.status
;
325 pp
->new_dir
= p
.new_dir
;
326 pp
->delayd
= p
.delayd
;
327 pp
->delayd_no
= p
.delayd_no
;
335 (void)putchar('\07');
336 (void)fflush(stdout
);
342 while ((tval
= getAChar()) == REDRAWTOKEN
|| tval
== SHELLTOKEN
)
344 if (tval
== SHELLTOKEN
)
347 struct itimerval itv
;
348 itv
.it_value
.tv_sec
= 0;
349 itv
.it_value
.tv_usec
= 0;
350 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
356 if (fork() == 0) /* child */
362 /* run user's favorite shell */
363 if ((shell
= getenv("SHELL")) != NULL
)
365 base
= strrchr(shell
, '/');
370 (void)execl(shell
, base
, (char *) 0);
373 (void)execl(_PATH_BSHELL
, "sh",
380 (void)tcsetattr(fileno(stdin
), TCSADRAIN
, &tty_new
);
382 itv
.it_value
.tv_sec
= 0;
383 itv
.it_value
.tv_usec
= 1;
384 itv
.it_interval
.tv_sec
= sp
->update_secs
;
385 itv
.it_interval
.tv_usec
= 0;
386 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
397 else if (isalpha(tval
))
408 pp
= findplane(number(c
));
410 return ("Unknown Plane");
411 (void)memcpy(&p
, pp
, sizeof (p
));
421 return ("Planes at airports may not change direction");
427 circle(int c __unused
)
430 return ("Planes cannot circle on the ground");
440 p
.new_dir
= p
.dir
- 1;
448 right(int c __unused
)
451 p
.new_dir
= p
.dir
+ 1;
452 if (p
.new_dir
>= MAXDIR
)
461 p
.new_dir
= p
.dir
- 2;
469 Right(int c __unused
)
471 p
.new_dir
= p
.dir
+ 2;
472 if (p
.new_dir
>= MAXDIR
)
485 if (bn
>= sp
->num_beacons
)
486 return ("Unknown beacon");
487 xdiff
= sp
->beacon
[bn
].x
- p
.xpos
;
489 ydiff
= sp
->beacon
[bn
].y
- p
.ypos
;
491 if (xdiff
!= displacement
[p
.dir
].dx
|| ydiff
!= displacement
[p
.dir
].dy
)
492 return ("Beacon is not in flight path");
496 if (dest_type
!= T_NODEST
) {
499 xdiff
= sp
->beacon
[dest_no
].x
- sp
->beacon
[bn
].x
;
500 ydiff
= sp
->beacon
[dest_no
].y
- sp
->beacon
[bn
].y
;
503 xdiff
= sp
->exit
[dest_no
].x
- sp
->beacon
[bn
].x
;
504 ydiff
= sp
->exit
[dest_no
].y
- sp
->beacon
[bn
].y
;
507 xdiff
= sp
->airport
[dest_no
].x
- sp
->beacon
[bn
].x
;
508 ydiff
= sp
->airport
[dest_no
].y
- sp
->beacon
[bn
].y
;
511 return ("Bad case in delayb! Get help!");
513 if (xdiff
== 0 && ydiff
== 0)
514 return ("Would already be there");
515 p
.new_dir
= DIR_FROM_DXDY(xdiff
, ydiff
);
516 if (p
.new_dir
== p
.dir
)
517 return ("Already going in that direction");
524 beacon(int c __unused
)
526 dest_type
= T_BEACON
;
532 ex_it(int c __unused
)
540 airport(int c __unused
)
542 dest_type
= T_AIRPORT
;
548 climb(int c __unused
)
556 descend(int c __unused
)
565 int newalt
= c
- '0';
566 if ((p
.altitude
== newalt
) && (p
.new_altitude
== p
.altitude
))
567 return ("Already at that altitude");
568 if (p
.new_altitude
== newalt
) {
569 return ("Already going to that altitude");
571 p
.new_altitude
= newalt
;
581 return ("altitude not changed");
585 newalt
= p
.altitude
+ c
- '0';
588 newalt
= p
.altitude
- (c
- '0');
591 return ("Unknown case in setrelalt! Get help!");
594 if (p
.new_altitude
== newalt
)
595 return ("Already going to that altitude");
597 p
.new_altitude
= newalt
;
599 if (p
.new_altitude
< 0)
600 return ("Altitude would be too low");
601 else if (p
.new_altitude
> 9)
602 return ("Altitude would be too high");
616 if (n
>= sp
->num_beacons
)
617 return ("Unknown beacon");
618 p
.new_dir
= DIR_FROM_DXDY(sp
->beacon
[n
].x
- p
.xpos
,
619 sp
->beacon
[n
].y
- p
.ypos
);
622 if (n
>= sp
->num_exits
)
623 return ("Unknown exit");
624 p
.new_dir
= DIR_FROM_DXDY(sp
->exit
[n
].x
- p
.xpos
,
625 sp
->exit
[n
].y
- p
.ypos
);
628 if (n
>= sp
->num_airports
)
629 return ("Unknown airport");
630 p
.new_dir
= DIR_FROM_DXDY(sp
->airport
[n
].x
- p
.xpos
,
631 sp
->airport
[n
].y
- p
.ypos
);
634 return ("Unknown case in benum! Get help!");
642 p
.new_dir
= dir_no(c
);
654 p
.new_dir
= p
.dir
- angle
;
659 p
.new_dir
= p
.dir
+ angle
;
660 if (p
.new_dir
>= MAXDIR
)
664 return ("Bizarre direction in rel_dir! Get help!");
674 return ("Cannot mark planes on the ground");
675 if (p
.status
== S_MARKED
)
676 return ("Already marked");
683 unmark(int c __unused
)
686 return ("Cannot unmark planes on the ground");
687 if (p
.status
== S_UNMARKED
)
688 return ("Already unmarked");
689 p
.status
= S_UNMARKED
;
695 ignore(int c __unused
)
698 return ("Cannot ignore planes on the ground");
699 if (p
.status
== S_IGNORED
)
700 return ("Already ignored");
701 p
.status
= S_IGNORED
;
712 case 'w': dirno
= 0; break;
713 case 'e': dirno
= 1; break;
714 case 'd': dirno
= 2; break;
715 case 'c': dirno
= 3; break;
716 case 'x': dirno
= 4; break;
717 case 'z': dirno
= 5; break;
718 case 'a': dirno
= 6; break;
719 case 'q': dirno
= 7; break;
721 (void)fprintf(stderr
, "bad character in dir_no\n");