input.c revision 1.6 1 /* $NetBSD: input.c,v 1.6 1997/10/10 02:07:18 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ed James.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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
36 * SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
41 *
42 * Copy permission is hereby granted provided that this notice is
43 * retained on all partial or complete copies.
44 *
45 * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
46 */
47
48 #include <sys/cdefs.h>
49 #ifndef lint
50 #if 0
51 static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
52 #else
53 __RCSID("$NetBSD: input.c,v 1.6 1997/10/10 02:07:18 lukem Exp $");
54 #endif
55 #endif not lint
56
57 #include "include.h"
58 #include "pathnames.h"
59
60 #define MAXRULES 6
61 #define MAXDEPTH 15
62
63 #define RETTOKEN '\n'
64 #ifdef SYSV
65 #define CRTOKEN '\r'
66 #endif
67 #define REDRAWTOKEN '\014' /* CTRL(L) */
68 #define SHELLTOKEN '!'
69 #define HELPTOKEN '?'
70 #define ALPHATOKEN 256
71 #define NUMTOKEN 257
72
73 typedef struct {
74 int token;
75 int to_state;
76 char *str;
77 char *(*func) __P((char));
78 } RULE;
79
80 typedef struct {
81 int num_rules;
82 RULE *rule;
83 } STATE;
84
85 typedef struct {
86 char str[20];
87 int state;
88 int rule;
89 int ch;
90 int pos;
91 } STACK;
92
93 #define T_RULE stack[level].rule
94 #define T_STATE stack[level].state
95 #define T_STR stack[level].str
96 #define T_POS stack[level].pos
97 #define T_CH stack[level].ch
98
99 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
100
101 #define NUMSTATES NUMELS(st)
102
103
104 RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
105 { RETTOKEN, -1, "", NULL },
106 #ifdef SYSV
107 { CRTOKEN, -1, "", NULL },
108 #endif
109 { HELPTOKEN, 12, " [a-z]<ret>", NULL }},
110 state1[] = { { 't', 2, " turn", turn },
111 { 'a', 3, " altitude:", NULL },
112 { 'c', 4, " circle", circle },
113 { 'm', 7, " mark", mark },
114 { 'u', 7, " unmark", unmark },
115 { 'i', 7, " ignore", ignore },
116 { HELPTOKEN, 12, " tacmui", NULL }},
117 state2[] = { { 'l', 6, " left", left },
118 { 'r', 6, " right", right },
119 { 'L', 4, " left 90", Left },
120 { 'R', 4, " right 90", Right },
121 { 't', 11, " towards", NULL },
122 { 'w', 4, " to 0", to_dir },
123 { 'e', 4, " to 45", to_dir },
124 { 'd', 4, " to 90", to_dir },
125 { 'c', 4, " to 135", to_dir },
126 { 'x', 4, " to 180", to_dir },
127 { 'z', 4, " to 225", to_dir },
128 { 'a', 4, " to 270", to_dir },
129 { 'q', 4, " to 315", to_dir },
130 { HELPTOKEN, 12, " lrLRt<dir>", NULL }},
131 state3[] = { { '+', 10, " climb", climb },
132 { 'c', 10, " climb", climb },
133 { '-', 10, " descend", descend },
134 { 'd', 10, " descend", descend },
135 { NUMTOKEN, 7, " %c000 feet", setalt },
136 { HELPTOKEN, 12, " +-cd[0-9]", NULL }},
137 state4[] = { { '@', 9, " at", NULL },
138 { 'a', 9, " at", NULL },
139 { RETTOKEN, -1, "", NULL },
140 #ifdef SYSV
141 { CRTOKEN, -1, "", NULL },
142 #endif
143 { HELPTOKEN, 12, " @a<ret>", NULL }},
144 state5[] = { { NUMTOKEN, 7, "%c", delayb },
145 { HELPTOKEN, 12, " [0-9]", NULL }},
146 state6[] = { { '@', 9, " at", NULL },
147 { 'a', 9, " at", NULL },
148 { 'w', 4, " 0", rel_dir },
149 { 'e', 4, " 45", rel_dir },
150 { 'd', 4, " 90", rel_dir },
151 { 'c', 4, " 135", rel_dir },
152 { 'x', 4, " 180", rel_dir },
153 { 'z', 4, " 225", rel_dir },
154 { 'a', 4, " 270", rel_dir },
155 { 'q', 4, " 315", rel_dir },
156 { RETTOKEN, -1, "", NULL },
157 #ifdef SYSV
158 { CRTOKEN, -1, "", NULL },
159 #endif
160 { HELPTOKEN, 12, " @a<dir><ret>",NULL }},
161 state7[] = { { RETTOKEN, -1, "", NULL },
162 #ifdef SYSV
163 { CRTOKEN, -1, "", NULL },
164 #endif
165 { HELPTOKEN, 12, " <ret>", NULL }},
166 state8[] = { { NUMTOKEN, 4, "%c", benum },
167 { HELPTOKEN, 12, " [0-9]", NULL }},
168 state9[] = { { 'b', 5, " beacon #", NULL },
169 { '*', 5, " beacon #", NULL },
170 { HELPTOKEN, 12, " b*", NULL }},
171 state10[] = { { NUMTOKEN, 7, " %c000 ft", setrelalt},
172 { HELPTOKEN, 12, " [0-9]", NULL }},
173 state11[] = { { 'b', 8, " beacon #", beacon },
174 { '*', 8, " beacon #", beacon },
175 { 'e', 8, " exit #", ex_it },
176 { 'a', 8, " airport #", airport },
177 { HELPTOKEN, 12, " b*ea", NULL }},
178 state12[] = { { -1, -1, "", NULL }};
179
180 #define DEF_STATE(s) { NUMELS(s), (s) }
181
182 STATE st[] = {
183 DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
184 DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
185 DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
186 DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
187 DEF_STATE(state12)
188 };
189
190 PLANE p;
191 STACK stack[MAXDEPTH];
192 int level;
193 int tval;
194 int dest_type, dest_no, dir;
195
196 int
197 pop()
198 {
199 if (level == 0)
200 return (-1);
201 level--;
202
203 ioclrtoeol(T_POS);
204
205 strcpy(T_STR, "");
206 T_RULE = -1;
207 T_CH = -1;
208 return (0);
209 }
210
211 void
212 rezero()
213 {
214 iomove(0);
215
216 level = 0;
217 T_STATE = 0;
218 T_RULE = -1;
219 T_CH = -1;
220 T_POS = 0;
221 strcpy(T_STR, "");
222 }
223
224 void
225 push(ruleno, ch)
226 int ruleno, ch;
227 {
228 int newstate, newpos;
229
230 (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
231 T_RULE = ruleno;
232 T_CH = ch;
233 newstate = st[T_STATE].rule[ruleno].to_state;
234 newpos = T_POS + strlen(T_STR);
235
236 ioaddstr(T_POS, T_STR);
237
238 if (level == 0)
239 ioclrtobot();
240 level++;
241 T_STATE = newstate;
242 T_POS = newpos;
243 T_RULE = -1;
244 strcpy(T_STR, "");
245 }
246
247 int
248 getcommand()
249 {
250 int c, i, done;
251 char *s, *(*func) __P((char));
252 PLANE *pp;
253
254 rezero();
255
256 do {
257 c = gettoken();
258 if (c == tty_new.c_cc[VERASE]) {
259 if (pop() < 0)
260 noise();
261 } else if (c == tty_new.c_cc[VKILL]) {
262 while (pop() >= 0)
263 ;
264 } else {
265 done = 0;
266 for (i = 0; i < st[T_STATE].num_rules; i++) {
267 if (st[T_STATE].rule[i].token == c ||
268 st[T_STATE].rule[i].token == tval) {
269 push(i, (c >= ALPHATOKEN) ? tval : c);
270 done = 1;
271 break;
272 }
273 }
274 if (!done)
275 noise();
276 }
277 } while (T_STATE != -1);
278
279 if (level == 1)
280 return (1); /* forced update */
281
282 dest_type = T_NODEST;
283
284 for (i = 0; i < level; i++) {
285 func = st[stack[i].state].rule[stack[i].rule].func;
286 if (func != NULL)
287 if ((s = (*func)(stack[i].ch)) != NULL) {
288 ioerror(stack[i].pos, strlen(stack[i].str), s);
289 return (-1);
290 }
291 }
292
293 pp = findplane(p.plane_no);
294 if (pp->new_altitude != p.new_altitude)
295 pp->new_altitude = p.new_altitude;
296 else if (pp->status != p.status)
297 pp->status = p.status;
298 else {
299 pp->new_dir = p.new_dir;
300 pp->delayd = p.delayd;
301 pp->delayd_no = p.delayd_no;
302 }
303 return (0);
304 }
305
306 void
307 noise()
308 {
309 putchar('\07');
310 fflush(stdout);
311 }
312
313 int
314 gettoken()
315 {
316 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
317 {
318 if (tval == SHELLTOKEN)
319 {
320 #ifdef BSD
321 struct itimerval itv;
322 itv.it_value.tv_sec = 0;
323 itv.it_value.tv_usec = 0;
324 setitimer(ITIMER_REAL, &itv, NULL);
325 #endif
326 #ifdef SYSV
327 int aval;
328 aval = alarm(0);
329 #endif
330 if (fork() == 0) /* child */
331 {
332 char *shell, *base;
333
334 setuid(getuid()); /* turn off setuid bit */
335 done_screen();
336
337 /* run user's favorite shell */
338 if ((shell = getenv("SHELL")) != NULL)
339 {
340 base = strrchr(shell, '/');
341 if (base == NULL)
342 base = shell;
343 else
344 base++;
345 execl(shell, base, 0);
346 }
347 else
348 execl(_PATH_BSHELL, "sh", 0);
349
350 exit(0); /* oops */
351 }
352
353 wait(0);
354 tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
355 #ifdef BSD
356 itv.it_value.tv_sec = 0;
357 itv.it_value.tv_usec = 1;
358 itv.it_interval.tv_sec = sp->update_secs;
359 itv.it_interval.tv_usec = 0;
360 setitimer(ITIMER_REAL, &itv, NULL);
361 #endif
362 #ifdef SYSV
363 alarm(aval);
364 #endif
365 }
366 redraw();
367 }
368
369 if (isdigit(tval))
370 return (NUMTOKEN);
371 else if (isalpha(tval))
372 return (ALPHATOKEN);
373 else
374 return (tval);
375 }
376
377 char *
378 setplane(c)
379 char c;
380 {
381 PLANE *pp;
382
383 pp = findplane(number(c));
384 if (pp == NULL)
385 return ("Unknown Plane");
386 memcpy(&p, pp, sizeof (p));
387 p.delayd = 0;
388 return (NULL);
389 }
390
391 char *
392 turn(c)
393 char c;
394 {
395 if (p.altitude == 0)
396 return ("Planes at airports may not change direction");
397 return (NULL);
398 }
399
400 char *
401 circle(c)
402 char c;
403 {
404 if (p.altitude == 0)
405 return ("Planes cannot circle on the ground");
406 p.new_dir = MAXDIR;
407 return (NULL);
408 }
409
410 char *
411 left(c)
412 char c;
413 {
414 dir = D_LEFT;
415 p.new_dir = p.dir - 1;
416 if (p.new_dir < 0)
417 p.new_dir += MAXDIR;
418 return (NULL);
419 }
420
421 char *
422 right(c)
423 char c;
424 {
425 dir = D_RIGHT;
426 p.new_dir = p.dir + 1;
427 if (p.new_dir > MAXDIR)
428 p.new_dir -= MAXDIR;
429 return (NULL);
430 }
431
432 char *
433 Left(c)
434 char c;
435 {
436 p.new_dir = p.dir - 2;
437 if (p.new_dir < 0)
438 p.new_dir += MAXDIR;
439 return (NULL);
440 }
441
442 char *
443 Right(c)
444 char c;
445 {
446 p.new_dir = p.dir + 2;
447 if (p.new_dir > MAXDIR)
448 p.new_dir -= MAXDIR;
449 return (NULL);
450 }
451
452 char *
453 delayb(c)
454 char c;
455 {
456 int xdiff, ydiff;
457
458 c -= '0';
459
460 if (c >= sp->num_beacons)
461 return ("Unknown beacon");
462 xdiff = sp->beacon[c].x - p.xpos;
463 xdiff = SGN(xdiff);
464 ydiff = sp->beacon[c].y - p.ypos;
465 ydiff = SGN(ydiff);
466 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
467 return ("Beacon is not in flight path");
468 p.delayd = 1;
469 p.delayd_no = c;
470
471 if (dest_type != T_NODEST) {
472 switch (dest_type) {
473 case T_BEACON:
474 xdiff = sp->beacon[dest_no].x - sp->beacon[c].x;
475 ydiff = sp->beacon[dest_no].y - sp->beacon[c].y;
476 break;
477 case T_EXIT:
478 xdiff = sp->exit[dest_no].x - sp->beacon[c].x;
479 ydiff = sp->exit[dest_no].y - sp->beacon[c].y;
480 break;
481 case T_AIRPORT:
482 xdiff = sp->airport[dest_no].x - sp->beacon[c].x;
483 ydiff = sp->airport[dest_no].y - sp->beacon[c].y;
484 break;
485 default:
486 return ("Bad case in delayb! Get help!");
487 break;
488 }
489 if (xdiff == 0 && ydiff == 0)
490 return ("Would already be there");
491 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
492 if (p.new_dir == p.dir)
493 return ("Already going in that direction");
494 }
495 return (NULL);
496 }
497
498 char *
499 beacon(c)
500 char c;
501 {
502 dest_type = T_BEACON;
503 return (NULL);
504 }
505
506 char *
507 ex_it(c)
508 char c;
509 {
510 dest_type = T_EXIT;
511 return (NULL);
512 }
513
514 char *
515 airport(c)
516 char c;
517 {
518 dest_type = T_AIRPORT;
519 return (NULL);
520 }
521
522 char *
523 climb(c)
524 char c;
525 {
526 dir = D_UP;
527 return (NULL);
528 }
529
530 char *
531 descend(c)
532 char c;
533 {
534 dir = D_DOWN;
535 return (NULL);
536 }
537
538 char *
539 setalt(c)
540 char c;
541 {
542 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
543 return ("Already at that altitude");
544 p.new_altitude = c - '0';
545 return (NULL);
546 }
547
548 char *
549 setrelalt(c)
550 char c;
551 {
552 if (c == 0)
553 return ("altitude not changed");
554
555 switch (dir) {
556 case D_UP:
557 p.new_altitude = p.altitude + c - '0';
558 break;
559 case D_DOWN:
560 p.new_altitude = p.altitude - (c - '0');
561 break;
562 default:
563 return ("Unknown case in setrelalt! Get help!");
564 break;
565 }
566 if (p.new_altitude < 0)
567 return ("Altitude would be too low");
568 else if (p.new_altitude > 9)
569 return ("Altitude would be too high");
570 return (NULL);
571 }
572
573 char *
574 benum(c)
575 char c;
576 {
577 dest_no = c -= '0';
578
579 switch (dest_type) {
580 case T_BEACON:
581 if (c >= sp->num_beacons)
582 return ("Unknown beacon");
583 p.new_dir = DIR_FROM_DXDY(sp->beacon[c].x - p.xpos,
584 sp->beacon[c].y - p.ypos);
585 break;
586 case T_EXIT:
587 if (c >= sp->num_exits)
588 return ("Unknown exit");
589 p.new_dir = DIR_FROM_DXDY(sp->exit[c].x - p.xpos,
590 sp->exit[c].y - p.ypos);
591 break;
592 case T_AIRPORT:
593 if (c >= sp->num_airports)
594 return ("Unknown airport");
595 p.new_dir = DIR_FROM_DXDY(sp->airport[c].x - p.xpos,
596 sp->airport[c].y - p.ypos);
597 break;
598 default:
599 return ("Unknown case in benum! Get help!");
600 break;
601 }
602 return (NULL);
603 }
604
605 char *
606 to_dir(c)
607 char c;
608 {
609 p.new_dir = dir_no(c);
610 return (NULL);
611 }
612
613 char *
614 rel_dir(c)
615 char c;
616 {
617 int angle;
618
619 angle = dir_no(c);
620 switch (dir) {
621 case D_LEFT:
622 p.new_dir = p.dir - angle;
623 if (p.new_dir < 0)
624 p.new_dir += MAXDIR;
625 break;
626 case D_RIGHT:
627 p.new_dir = p.dir + angle;
628 if (p.new_dir >= MAXDIR)
629 p.new_dir -= MAXDIR;
630 break;
631 default:
632 return ("Bizarre direction in rel_dir! Get help!");
633 break;
634 }
635 return (NULL);
636 }
637
638 char *
639 mark(c)
640 char c;
641 {
642 if (p.altitude == 0)
643 return ("Cannot mark planes on the ground");
644 if (p.status == S_MARKED)
645 return ("Already marked");
646 p.status = S_MARKED;
647 return (NULL);
648 }
649
650 char *
651 unmark(c)
652 char c;
653 {
654 if (p.altitude == 0)
655 return ("Cannot unmark planes on the ground");
656 if (p.status == S_UNMARKED)
657 return ("Already unmarked");
658 p.status = S_UNMARKED;
659 return (NULL);
660 }
661
662 char *
663 ignore(c)
664 char c;
665 {
666 if (p.altitude == 0)
667 return ("Cannot ignore planes on the ground");
668 if (p.status == S_IGNORED)
669 return ("Already ignored");
670 p.status = S_IGNORED;
671 return (NULL);
672 }
673
674 int
675 dir_no(ch)
676 char ch;
677 {
678 int dir;
679
680 dir = -1;
681 switch (ch) {
682 case 'w': dir = 0; break;
683 case 'e': dir = 1; break;
684 case 'd': dir = 2; break;
685 case 'c': dir = 3; break;
686 case 'x': dir = 4; break;
687 case 'z': dir = 5; break;
688 case 'a': dir = 6; break;
689 case 'q': dir = 7; break;
690 default:
691 fprintf(stderr, "bad character in dir_no\n");
692 break;
693 }
694 return (dir);
695 }
696