]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/input.c
1 /* $NetBSD: input.c,v 1.10 1998/09/13 15:20:31 hubertf 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
42 * Copy permission is hereby granted provided that this notice is
43 * retained on all partial or complete copies.
45 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
48 #include <sys/cdefs.h>
51 static char sccsid
[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
53 __RCSID("$NetBSD: input.c,v 1.10 1998/09/13 15:20:31 hubertf Exp $");
58 #include "pathnames.h"
64 #define REDRAWTOKEN '\014' /* CTRL(L) */
65 #define SHELLTOKEN '!'
67 #define ALPHATOKEN 256
74 char *(*func
) __P((char));
90 #define T_RULE stack[level].rule
91 #define T_STATE stack[level].state
92 #define T_STR stack[level].str
93 #define T_POS stack[level].pos
94 #define T_CH stack[level].ch
96 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
98 #define NUMSTATES NUMELS(st)
101 RULE state0
[] = { { ALPHATOKEN
, 1, "%c:", setplane
},
102 { RETTOKEN
, -1, "", NULL
},
103 { HELPTOKEN
, 12, " [a-z]<ret>", NULL
}},
104 state1
[] = { { 't', 2, " turn", turn
},
105 { 'a', 3, " altitude:", NULL
},
106 { 'c', 4, " circle", circle
},
107 { 'm', 7, " mark", mark
},
108 { 'u', 7, " unmark", unmark
},
109 { 'i', 7, " ignore", ignore
},
110 { HELPTOKEN
, 12, " tacmui", NULL
}},
111 state2
[] = { { 'l', 6, " left", left
},
112 { 'r', 6, " right", right
},
113 { 'L', 4, " left 90", Left
},
114 { 'R', 4, " right 90", Right
},
115 { 't', 11, " towards", NULL
},
116 { 'w', 4, " to 0", to_dir
},
117 { 'e', 4, " to 45", to_dir
},
118 { 'd', 4, " to 90", to_dir
},
119 { 'c', 4, " to 135", to_dir
},
120 { 'x', 4, " to 180", to_dir
},
121 { 'z', 4, " to 225", to_dir
},
122 { 'a', 4, " to 270", to_dir
},
123 { 'q', 4, " to 315", to_dir
},
124 { HELPTOKEN
, 12, " lrLRt<dir>", NULL
}},
125 state3
[] = { { '+', 10, " climb", climb
},
126 { 'c', 10, " climb", climb
},
127 { '-', 10, " descend", descend
},
128 { 'd', 10, " descend", descend
},
129 { NUMTOKEN
, 7, " %c000 feet", setalt
},
130 { HELPTOKEN
, 12, " +-cd[0-9]", NULL
}},
131 state4
[] = { { '@', 9, " at", NULL
},
132 { 'a', 9, " at", NULL
},
133 { RETTOKEN
, -1, "", NULL
},
134 { HELPTOKEN
, 12, " @a<ret>", NULL
}},
135 state5
[] = { { NUMTOKEN
, 7, "%c", delayb
},
136 { HELPTOKEN
, 12, " [0-9]", NULL
}},
137 state6
[] = { { '@', 9, " at", NULL
},
138 { 'a', 9, " at", NULL
},
139 { 'w', 4, " 0", rel_dir
},
140 { 'e', 4, " 45", rel_dir
},
141 { 'd', 4, " 90", rel_dir
},
142 { 'c', 4, " 135", rel_dir
},
143 { 'x', 4, " 180", rel_dir
},
144 { 'z', 4, " 225", rel_dir
},
145 { 'a', 4, " 270", rel_dir
},
146 { 'q', 4, " 315", rel_dir
},
147 { RETTOKEN
, -1, "", NULL
},
148 { HELPTOKEN
, 12, " @a<dir><ret>",NULL
}},
149 state7
[] = { { RETTOKEN
, -1, "", NULL
},
150 { HELPTOKEN
, 12, " <ret>", NULL
}},
151 state8
[] = { { NUMTOKEN
, 4, "%c", benum
},
152 { HELPTOKEN
, 12, " [0-9]", NULL
}},
153 state9
[] = { { 'b', 5, " beacon #", NULL
},
154 { '*', 5, " beacon #", NULL
},
155 { HELPTOKEN
, 12, " b*", NULL
}},
156 state10
[] = { { NUMTOKEN
, 7, " %c000 ft", setrelalt
},
157 { HELPTOKEN
, 12, " [0-9]", NULL
}},
158 state11
[] = { { 'b', 8, " beacon #", beacon
},
159 { '*', 8, " beacon #", beacon
},
160 { 'e', 8, " exit #", ex_it
},
161 { 'a', 8, " airport #", airport
},
162 { HELPTOKEN
, 12, " b*ea", NULL
}},
163 state12
[] = { { -1, -1, "", NULL
}};
165 #define DEF_STATE(s) { NUMELS(s), (s) }
168 DEF_STATE(state0
), DEF_STATE(state1
), DEF_STATE(state2
),
169 DEF_STATE(state3
), DEF_STATE(state4
), DEF_STATE(state5
),
170 DEF_STATE(state6
), DEF_STATE(state7
), DEF_STATE(state8
),
171 DEF_STATE(state9
), DEF_STATE(state10
), DEF_STATE(state11
),
176 STACK stack
[MAXDEPTH
];
179 int dest_type
, dest_no
, dir
;
213 int newstate
, newpos
;
215 (void)sprintf(T_STR
, st
[T_STATE
].rule
[ruleno
].str
, tval
);
218 newstate
= st
[T_STATE
].rule
[ruleno
].to_state
;
219 newpos
= T_POS
+ strlen(T_STR
);
221 ioaddstr(T_POS
, T_STR
);
236 char *s
, *(*func
) __P((char));
243 if (c
== tty_new
.c_cc
[VERASE
]) {
246 } else if (c
== tty_new
.c_cc
[VKILL
]) {
251 for (i
= 0; i
< st
[T_STATE
].num_rules
; i
++) {
252 if (st
[T_STATE
].rule
[i
].token
== c
||
253 st
[T_STATE
].rule
[i
].token
== tval
) {
254 push(i
, (c
>= ALPHATOKEN
) ? tval
: c
);
262 } while (T_STATE
!= -1);
265 return (1); /* forced update */
267 dest_type
= T_NODEST
;
269 for (i
= 0; i
< level
; i
++) {
270 func
= st
[stack
[i
].state
].rule
[stack
[i
].rule
].func
;
272 if ((s
= (*func
)(stack
[i
].ch
)) != NULL
) {
273 ioerror(stack
[i
].pos
, strlen(stack
[i
].str
), s
);
278 pp
= findplane(p
.plane_no
);
279 if (pp
->new_altitude
!= p
.new_altitude
)
280 pp
->new_altitude
= p
.new_altitude
;
281 else if (pp
->status
!= p
.status
)
282 pp
->status
= p
.status
;
284 pp
->new_dir
= p
.new_dir
;
285 pp
->delayd
= p
.delayd
;
286 pp
->delayd_no
= p
.delayd_no
;
301 while ((tval
= getAChar()) == REDRAWTOKEN
|| tval
== SHELLTOKEN
)
303 if (tval
== SHELLTOKEN
)
306 struct itimerval itv
;
307 itv
.it_value
.tv_sec
= 0;
308 itv
.it_value
.tv_usec
= 0;
309 setitimer(ITIMER_REAL
, &itv
, NULL
);
315 if (fork() == 0) /* child */
319 setuid(getuid()); /* turn off setuid bit */
322 /* run user's favorite shell */
323 if ((shell
= getenv("SHELL")) != NULL
)
325 base
= strrchr(shell
, '/');
330 execl(shell
, base
, 0);
333 execl(_PATH_BSHELL
, "sh", 0);
339 tcsetattr(fileno(stdin
), TCSADRAIN
, &tty_new
);
341 itv
.it_value
.tv_sec
= 0;
342 itv
.it_value
.tv_usec
= 1;
343 itv
.it_interval
.tv_sec
= sp
->update_secs
;
344 itv
.it_interval
.tv_usec
= 0;
345 setitimer(ITIMER_REAL
, &itv
, NULL
);
356 else if (isalpha(tval
))
368 pp
= findplane(number(c
));
370 return ("Unknown Plane");
371 memcpy(&p
, pp
, sizeof (p
));
381 return ("Planes at airports may not change direction");
390 return ("Planes cannot circle on the ground");
400 p
.new_dir
= p
.dir
- 1;
411 p
.new_dir
= p
.dir
+ 1;
412 if (p
.new_dir
>= MAXDIR
)
421 p
.new_dir
= p
.dir
- 2;
431 p
.new_dir
= p
.dir
+ 2;
432 if (p
.new_dir
>= MAXDIR
)
445 if (c
>= sp
->num_beacons
)
446 return ("Unknown beacon");
447 xdiff
= sp
->beacon
[(int)c
].x
- p
.xpos
;
449 ydiff
= sp
->beacon
[(int)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
[(int)c
].x
;
460 ydiff
= sp
->beacon
[dest_no
].y
- sp
->beacon
[(int)c
].y
;
463 xdiff
= sp
->exit
[dest_no
].x
- sp
->beacon
[(int)c
].x
;
464 ydiff
= sp
->exit
[dest_no
].y
- sp
->beacon
[(int)c
].y
;
467 xdiff
= sp
->airport
[dest_no
].x
- sp
->beacon
[(int)c
].x
;
468 ydiff
= sp
->airport
[dest_no
].y
- sp
->beacon
[(int)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");
487 dest_type
= T_BEACON
;
503 dest_type
= T_AIRPORT
;
527 if ((p
.altitude
== c
- '0') && (p
.new_altitude
== p
.altitude
))
528 return ("Already at that altitude");
529 p
.new_altitude
= c
- '0';
538 return ("altitude not changed");
542 p
.new_altitude
= p
.altitude
+ c
- '0';
545 p
.new_altitude
= p
.altitude
- (c
- '0');
548 return ("Unknown case in setrelalt! Get help!");
551 if (p
.new_altitude
< 0)
552 return ("Altitude would be too low");
553 else if (p
.new_altitude
> 9)
554 return ("Altitude would be too high");
566 if (c
>= sp
->num_beacons
)
567 return ("Unknown beacon");
568 p
.new_dir
= DIR_FROM_DXDY(sp
->beacon
[(int)c
].x
- p
.xpos
,
569 sp
->beacon
[(int)c
].y
- p
.ypos
);
572 if (c
>= sp
->num_exits
)
573 return ("Unknown exit");
574 p
.new_dir
= DIR_FROM_DXDY(sp
->exit
[(int)c
].x
- p
.xpos
,
575 sp
->exit
[(int)c
].y
- p
.ypos
);
578 if (c
>= sp
->num_airports
)
579 return ("Unknown airport");
580 p
.new_dir
= DIR_FROM_DXDY(sp
->airport
[(int)c
].x
- p
.xpos
,
581 sp
->airport
[(int)c
].y
- p
.ypos
);
584 return ("Unknown case in benum! Get help!");
594 p
.new_dir
= dir_no(c
);
607 p
.new_dir
= p
.dir
- angle
;
612 p
.new_dir
= p
.dir
+ angle
;
613 if (p
.new_dir
>= MAXDIR
)
617 return ("Bizarre direction in rel_dir! Get help!");
628 return ("Cannot mark planes on the ground");
629 if (p
.status
== S_MARKED
)
630 return ("Already marked");
640 return ("Cannot unmark planes on the ground");
641 if (p
.status
== S_UNMARKED
)
642 return ("Already unmarked");
643 p
.status
= S_UNMARKED
;
652 return ("Cannot ignore planes on the ground");
653 if (p
.status
== S_IGNORED
)
654 return ("Already ignored");
655 p
.status
= S_IGNORED
;
667 case 'w': dir
= 0; break;
668 case 'e': dir
= 1; break;
669 case 'd': dir
= 2; break;
670 case 'c': dir
= 3; break;
671 case 'x': dir
= 4; break;
672 case 'z': dir
= 5; break;
673 case 'a': dir
= 6; break;
674 case 'q': dir
= 7; break;
676 fprintf(stderr
, "bad character in dir_no\n");