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