]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/input.c
2 * Copyright (c) 1990 The Regents of the University of California.
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
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.
47 static char sccsid
[] = "@(#)input.c 5.4 (Berkeley) 4/30/90";
51 #include "pathnames.h"
60 #define REDRAWTOKEN '\014' /* CTRL(L) */
61 #define SHELLTOKEN '!'
63 #define ALPHATOKEN 256
86 #define T_RULE stack[level].rule
87 #define T_STATE stack[level].state
88 #define T_STR stack[level].str
89 #define T_POS stack[level].pos
90 #define T_CH stack[level].ch
92 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
94 #define NUMSTATES NUMELS(st)
96 char *setplane(), *circle(), *left(), *right(), *Left(), *Right(),
97 *beacon(), *ex_it(), *climb(), *descend(), *setalt(), *setrelalt(),
98 *benum(), *to_dir(), *rel_dir(), *delayb(), *mark(), *unmark(),
99 *airport(), *turn(), *ignore();
101 RULE state0
[] = { { ALPHATOKEN
, 1, "%c:", setplane
},
102 { RETTOKEN
, -1, "", NULL
},
104 { CRTOKEN
, -1, "", NULL
},
106 { HELPTOKEN
, 12, " [a-z]<ret>", NULL
}},
107 state1
[] = { { 't', 2, " turn", turn
},
108 { 'a', 3, " altitude:", NULL
},
109 { 'c', 4, " circle", circle
},
110 { 'm', 7, " mark", mark
},
111 { 'u', 7, " unmark", unmark
},
112 { 'i', 7, " ignore", ignore
},
113 { HELPTOKEN
, 12, " tacmui", NULL
}},
114 state2
[] = { { 'l', 6, " left", left
},
115 { 'r', 6, " right", right
},
116 { 'L', 4, " left 90", Left
},
117 { 'R', 4, " right 90", Right
},
118 { 't', 11, " towards", NULL
},
119 { 'w', 4, " to 0", to_dir
},
120 { 'e', 4, " to 45", to_dir
},
121 { 'd', 4, " to 90", to_dir
},
122 { 'c', 4, " to 135", to_dir
},
123 { 'x', 4, " to 180", to_dir
},
124 { 'z', 4, " to 225", to_dir
},
125 { 'a', 4, " to 270", to_dir
},
126 { 'q', 4, " to 315", to_dir
},
127 { HELPTOKEN
, 12, " lrLRt<dir>", NULL
}},
128 state3
[] = { { '+', 10, " climb", climb
},
129 { 'c', 10, " climb", climb
},
130 { '-', 10, " descend", descend
},
131 { 'd', 10, " descend", descend
},
132 { NUMTOKEN
, 7, " %c000 feet", setalt
},
133 { HELPTOKEN
, 12, " +-cd[0-9]", NULL
}},
134 state4
[] = { { '@', 9, " at", NULL
},
135 { 'a', 9, " at", NULL
},
136 { RETTOKEN
, -1, "", NULL
},
138 { CRTOKEN
, -1, "", NULL
},
140 { HELPTOKEN
, 12, " @a<ret>", NULL
}},
141 state5
[] = { { NUMTOKEN
, 7, "%c", delayb
},
142 { HELPTOKEN
, 12, " [0-9]", NULL
}},
143 state6
[] = { { '@', 9, " at", NULL
},
144 { 'a', 9, " at", NULL
},
145 { 'w', 4, " 0", rel_dir
},
146 { 'e', 4, " 45", rel_dir
},
147 { 'd', 4, " 90", rel_dir
},
148 { 'c', 4, " 135", rel_dir
},
149 { 'x', 4, " 180", rel_dir
},
150 { 'z', 4, " 225", rel_dir
},
151 { 'a', 4, " 270", rel_dir
},
152 { 'q', 4, " 315", rel_dir
},
153 { RETTOKEN
, -1, "", NULL
},
155 { CRTOKEN
, -1, "", NULL
},
157 { HELPTOKEN
, 12, " @a<dir><ret>",NULL
}},
158 state7
[] = { { RETTOKEN
, -1, "", NULL
},
160 { CRTOKEN
, -1, "", NULL
},
162 { HELPTOKEN
, 12, " <ret>", NULL
}},
163 state8
[] = { { NUMTOKEN
, 4, "%c", benum
},
164 { HELPTOKEN
, 12, " [0-9]", NULL
}},
165 state9
[] = { { 'b', 5, " beacon #", NULL
},
166 { '*', 5, " beacon #", NULL
},
167 { HELPTOKEN
, 12, " b*", NULL
}},
168 state10
[] = { { NUMTOKEN
, 7, " %c000 ft", setrelalt
},
169 { HELPTOKEN
, 12, " [0-9]", NULL
}},
170 state11
[] = { { 'b', 8, " beacon #", beacon
},
171 { '*', 8, " beacon #", beacon
},
172 { 'e', 8, " exit #", ex_it
},
173 { 'a', 8, " airport #", airport
},
174 { HELPTOKEN
, 12, " b*ea", NULL
}},
175 state12
[] = { { -1, -1, "", NULL
}};
177 #define DEF_STATE(s) { NUMELS(s), (s) }
180 DEF_STATE(state0
), DEF_STATE(state1
), DEF_STATE(state2
),
181 DEF_STATE(state3
), DEF_STATE(state4
), DEF_STATE(state5
),
182 DEF_STATE(state6
), DEF_STATE(state7
), DEF_STATE(state8
),
183 DEF_STATE(state9
), DEF_STATE(state10
), DEF_STATE(state11
),
188 STACK stack
[MAXDEPTH
];
191 int dest_type
, dest_no
, dir
;
221 int newstate
, newpos
;
223 (void)sprintf(T_STR
, st
[T_STATE
].rule
[ruleno
].str
, tval
);
226 newstate
= st
[T_STATE
].rule
[ruleno
].to_state
;
227 newpos
= T_POS
+ strlen(T_STR
);
229 ioaddstr(T_POS
, T_STR
);
250 if (c
== tty_new
.sg_erase
) {
253 } else if (c
== tty_new
.sg_kill
) {
258 for (i
= 0; i
< st
[T_STATE
].num_rules
; i
++) {
259 if (st
[T_STATE
].rule
[i
].token
== c
||
260 st
[T_STATE
].rule
[i
].token
== tval
) {
261 push(i
, (c
>= ALPHATOKEN
) ? tval
: c
);
269 } while (T_STATE
!= -1);
272 return (1); /* forced update */
274 dest_type
= T_NODEST
;
276 for (i
= 0; i
< level
; i
++) {
277 func
= st
[stack
[i
].state
].rule
[stack
[i
].rule
].func
;
279 if ((s
= (*func
)(stack
[i
].ch
)) != NULL
) {
280 ioerror(stack
[i
].pos
, strlen(stack
[i
].str
), s
);
285 pp
= findplane(p
.plane_no
);
286 if (pp
->new_altitude
!= p
.new_altitude
)
287 pp
->new_altitude
= p
.new_altitude
;
288 else if (pp
->status
!= p
.status
)
289 pp
->status
= p
.status
;
291 pp
->new_dir
= p
.new_dir
;
292 pp
->delayd
= p
.delayd
;
293 pp
->delayd_no
= p
.delayd_no
;
306 while ((tval
= getAChar()) == REDRAWTOKEN
|| tval
== SHELLTOKEN
)
308 if (tval
== SHELLTOKEN
)
311 struct itimerval itv
;
312 itv
.it_value
.tv_sec
= 0;
313 itv
.it_value
.tv_usec
= 0;
314 setitimer(ITIMER_REAL
, &itv
, NULL
);
320 if (fork() == 0) /* child */
322 char *shell
, *base
, *getenv(), *strrchr();
324 setuid(getuid()); /* turn off setuid bit */
327 /* run user's favorite shell */
328 if ((shell
= getenv("SHELL")) != NULL
)
330 base
= strrchr(shell
, '/');
335 execl(shell
, base
, 0);
338 execl(_PATH_BSHELL
, "sh", 0);
345 ioctl(fileno(stdin
), TIOCSETP
, &tty_new
);
346 itv
.it_value
.tv_sec
= 0;
347 itv
.it_value
.tv_usec
= 1;
348 itv
.it_interval
.tv_sec
= sp
->update_secs
;
349 itv
.it_interval
.tv_usec
= 0;
350 setitimer(ITIMER_REAL
, &itv
, NULL
);
353 ioctl(fileno(stdin
), TCSETAW
, &tty_new
);
362 else if (isalpha(tval
))
373 pp
= findplane(number(c
));
375 return ("Unknown Plane");
376 bcopy(pp
, &p
, sizeof (p
));
385 return ("Planes at airports may not change direction");
393 return ("Planes cannot circle on the ground");
402 p
.new_dir
= p
.dir
- 1;
412 p
.new_dir
= p
.dir
+ 1;
413 if (p
.new_dir
> MAXDIR
)
421 p
.new_dir
= p
.dir
- 2;
430 p
.new_dir
= p
.dir
+ 2;
431 if (p
.new_dir
> MAXDIR
)
443 if (c
>= sp
->num_beacons
)
444 return ("Unknown beacon");
445 xdiff
= sp
->beacon
[c
].x
- p
.xpos
;
447 ydiff
= sp
->beacon
[c
].y
- p
.ypos
;
449 if (xdiff
!= displacement
[p
.dir
].dx
|| ydiff
!= displacement
[p
.dir
].dy
)
450 return ("Beacon is not in flight path");
454 if (dest_type
!= T_NODEST
) {
457 xdiff
= sp
->beacon
[dest_no
].x
- sp
->beacon
[c
].x
;
458 ydiff
= sp
->beacon
[dest_no
].y
- sp
->beacon
[c
].y
;
461 xdiff
= sp
->exit
[dest_no
].x
- sp
->beacon
[c
].x
;
462 ydiff
= sp
->exit
[dest_no
].y
- sp
->beacon
[c
].y
;
465 xdiff
= sp
->airport
[dest_no
].x
- sp
->beacon
[c
].x
;
466 ydiff
= sp
->airport
[dest_no
].y
- sp
->beacon
[c
].y
;
469 return ("Bad case in delayb! Get help!");
472 if (xdiff
== 0 && ydiff
== 0)
473 return ("Would already be there");
474 p
.new_dir
= DIR_FROM_DXDY(xdiff
, ydiff
);
475 if (p
.new_dir
== p
.dir
)
476 return ("Already going in that direction");
484 dest_type
= T_BEACON
;
498 dest_type
= T_AIRPORT
;
519 if ((p
.altitude
== c
- '0') && (p
.new_altitude
== p
.altitude
))
520 return ("Already at that altitude");
521 p
.new_altitude
= c
- '0';
529 return ("altitude not changed");
533 p
.new_altitude
= p
.altitude
+ c
- '0';
536 p
.new_altitude
= p
.altitude
- (c
- '0');
539 return ("Unknown case in setrelalt! Get help!");
542 if (p
.new_altitude
< 0)
543 return ("Altitude would be too low");
544 else if (p
.new_altitude
> 9)
545 return ("Altitude would be too high");
556 if (c
>= sp
->num_beacons
)
557 return ("Unknown beacon");
558 p
.new_dir
= DIR_FROM_DXDY(sp
->beacon
[c
].x
- p
.xpos
,
559 sp
->beacon
[c
].y
- p
.ypos
);
562 if (c
>= sp
->num_exits
)
563 return ("Unknown exit");
564 p
.new_dir
= DIR_FROM_DXDY(sp
->exit
[c
].x
- p
.xpos
,
565 sp
->exit
[c
].y
- p
.ypos
);
568 if (c
>= sp
->num_airports
)
569 return ("Unknown airport");
570 p
.new_dir
= DIR_FROM_DXDY(sp
->airport
[c
].x
- p
.xpos
,
571 sp
->airport
[c
].y
- p
.ypos
);
574 return ("Unknown case in benum! Get help!");
583 p
.new_dir
= dir_no(c
);
595 p
.new_dir
= p
.dir
- angle
;
600 p
.new_dir
= p
.dir
+ angle
;
601 if (p
.new_dir
>= MAXDIR
)
605 return ("Bizarre direction in rel_dir! Get help!");
615 return ("Cannot mark planes on the ground");
616 if (p
.status
== S_MARKED
)
617 return ("Already marked");
626 return ("Cannot unmark planes on the ground");
627 if (p
.status
== S_UNMARKED
)
628 return ("Already unmarked");
629 p
.status
= S_UNMARKED
;
637 return ("Cannot ignore planes on the ground");
638 if (p
.status
== S_IGNORED
)
639 return ("Already ignored");
640 p
.status
= S_IGNORED
;
650 case 'w': dir
= 0; break;
651 case 'e': dir
= 1; break;
652 case 'd': dir
= 2; break;
653 case 'c': dir
= 3; break;
654 case 'x': dir
= 4; break;
655 case 'z': dir
= 5; break;
656 case 'a': dir
= 6; break;
657 case 'q': dir
= 7; break;
659 fprintf(stderr
, "bad character in dir_no\n");