input.c revision 1.19 1 /* $NetBSD: input.c,v 1.19 2005/08/10 17:53:28 rpaulo 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.19 2005/08/10 17:53:28 rpaulo 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)(int);
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(void)
179 {
180 if (level == 0)
181 return (-1);
182 level--;
183
184 ioclrtoeol(T_POS);
185
186 (void)strcpy(T_STR, "");
187 T_RULE = -1;
188 T_CH = -1;
189 return (0);
190 }
191
192 void
193 rezero(void)
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 (void)strcpy(T_STR, "");
203 }
204
205 void
206 push(int ruleno, int ch)
207 {
208 int newstate, newpos;
209
210 (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
211 T_RULE = ruleno;
212 T_CH = ch;
213 newstate = st[T_STATE].rule[ruleno].to_state;
214 newpos = T_POS + strlen(T_STR);
215
216 ioaddstr(T_POS, T_STR);
217
218 if (level == 0)
219 ioclrtobot();
220 level++;
221 T_STATE = newstate;
222 T_POS = newpos;
223 T_RULE = -1;
224 (void)strcpy(T_STR, "");
225 }
226
227 int
228 getcommand(void)
229 {
230 int c, i, done;
231 const char *s, *(*func)(int);
232 PLANE *pp;
233
234 rezero();
235
236 do {
237 c = gettoken();
238 if (c == tty_new.c_cc[VERASE]) {
239 if (pop() < 0)
240 noise();
241 } else if (c == tty_new.c_cc[VKILL]) {
242 while (pop() >= 0)
243 ;
244 } else {
245 done = 0;
246 for (i = 0; i < st[T_STATE].num_rules; i++) {
247 if (st[T_STATE].rule[i].token == c ||
248 st[T_STATE].rule[i].token == tval) {
249 push(i, (c >= ALPHATOKEN) ? tval : c);
250 done = 1;
251 break;
252 }
253 }
254 if (!done)
255 noise();
256 }
257 } while (T_STATE != -1);
258
259 if (level == 1)
260 return (1); /* forced update */
261
262 dest_type = T_NODEST;
263
264 for (i = 0; i < level; i++) {
265 func = st[stack[i].state].rule[stack[i].rule].func;
266 if (func != NULL)
267 if ((s = (*func)(stack[i].ch)) != NULL) {
268 ioerror(stack[i].pos,
269 (int)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(void)
289 {
290 (void)putchar('\07');
291 (void)fflush(stdout);
292 }
293
294 int
295 gettoken(void)
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 (void)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 (void)execl(shell, base, (char *) 0);
326 }
327 else
328 (void)execl(_PATH_BSHELL, "sh",
329 (char *) 0);
330
331 exit(0); /* oops */
332 }
333
334 (void)wait(0);
335 (void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
336 #ifdef BSD
337 itv.it_value.tv_sec = 0;
338 itv.it_value.tv_usec = 1;
339 itv.it_interval.tv_sec = sp->update_secs;
340 itv.it_interval.tv_usec = 0;
341 (void)setitimer(ITIMER_REAL, &itv, NULL);
342 #endif
343 #ifdef SYSV
344 alarm(aval);
345 #endif
346 }
347 (void)redraw();
348 }
349
350 if (isdigit(tval))
351 return (NUMTOKEN);
352 else if (isalpha(tval))
353 return (ALPHATOKEN);
354 else
355 return (tval);
356 }
357
358 const char *
359 setplane(int c)
360 {
361 PLANE *pp;
362
363 pp = findplane(number(c));
364 if (pp == NULL)
365 return ("Unknown Plane");
366 (void)memcpy(&p, pp, sizeof (p));
367 p.delayd = 0;
368 return (NULL);
369 }
370
371 /* ARGSUSED */
372 const char *
373 turn(int c __attribute__((__unused__)))
374 {
375 if (p.altitude == 0)
376 return ("Planes at airports may not change direction");
377 return (NULL);
378 }
379
380 /* ARGSUSED */
381 const char *
382 circle(int 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 /* ARGSUSED */
391 const char *
392 left(int 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 /* ARGSUSED */
402 const char *
403 right(int 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 /* ARGSUSED */
413 const char *
414 Left(int 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 /* ARGSUSED */
423 const char *
424 Right(int 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(int c)
434 {
435 int xdiff, ydiff;
436
437 c -= '0';
438
439 if (c >= sp->num_beacons)
440 return ("Unknown beacon");
441 xdiff = sp->beacon[(int)c].x - p.xpos;
442 xdiff = SGN(xdiff);
443 ydiff = sp->beacon[(int)c].y - p.ypos;
444 ydiff = SGN(ydiff);
445 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
446 return ("Beacon is not in flight path");
447 p.delayd = 1;
448 p.delayd_no = c;
449
450 if (dest_type != T_NODEST) {
451 switch (dest_type) {
452 case T_BEACON:
453 xdiff = sp->beacon[dest_no].x - sp->beacon[(int)c].x;
454 ydiff = sp->beacon[dest_no].y - sp->beacon[(int)c].y;
455 break;
456 case T_EXIT:
457 xdiff = sp->exit[dest_no].x - sp->beacon[(int)c].x;
458 ydiff = sp->exit[dest_no].y - sp->beacon[(int)c].y;
459 break;
460 case T_AIRPORT:
461 xdiff = sp->airport[dest_no].x - sp->beacon[(int)c].x;
462 ydiff = sp->airport[dest_no].y - sp->beacon[(int)c].y;
463 break;
464 default:
465 return ("Bad case in delayb! Get help!");
466 }
467 if (xdiff == 0 && ydiff == 0)
468 return ("Would already be there");
469 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
470 if (p.new_dir == p.dir)
471 return ("Already going in that direction");
472 }
473 return (NULL);
474 }
475
476 /* ARGSUSED */
477 const char *
478 beacon(int c __attribute__((__unused__)))
479 {
480 dest_type = T_BEACON;
481 return (NULL);
482 }
483
484 /* ARGSUSED */
485 const char *
486 ex_it(int c __attribute__((__unused__)))
487 {
488 dest_type = T_EXIT;
489 return (NULL);
490 }
491
492 /* ARGSUSED */
493 const char *
494 airport(int c __attribute__((__unused__)))
495 {
496 dest_type = T_AIRPORT;
497 return (NULL);
498 }
499
500 /* ARGSUSED */
501 const char *
502 climb(int c __attribute__((__unused__)))
503 {
504 dir = D_UP;
505 return (NULL);
506 }
507
508 /* ARGSUSED */
509 const char *
510 descend(int c __attribute__((__unused__)))
511 {
512 dir = D_DOWN;
513 return (NULL);
514 }
515
516 const char *
517 setalt(int c)
518 {
519 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
520 return ("Already at that altitude");
521 p.new_altitude = c - '0';
522 return (NULL);
523 }
524
525 const char *
526 setrelalt(int c)
527 {
528 if (c == 0)
529 return ("altitude not changed");
530
531 switch (dir) {
532 case D_UP:
533 p.new_altitude = p.altitude + c - '0';
534 break;
535 case D_DOWN:
536 p.new_altitude = p.altitude - (c - '0');
537 break;
538 default:
539 return ("Unknown case in setrelalt! Get help!");
540 }
541 if (p.new_altitude < 0)
542 return ("Altitude would be too low");
543 else if (p.new_altitude > 9)
544 return ("Altitude would be too high");
545 return (NULL);
546 }
547
548 const char *
549 benum(int c)
550 {
551 dest_no = c -= '0';
552
553 switch (dest_type) {
554 case T_BEACON:
555 if (c >= sp->num_beacons)
556 return ("Unknown beacon");
557 p.new_dir = DIR_FROM_DXDY(sp->beacon[(int)c].x - p.xpos,
558 sp->beacon[(int)c].y - p.ypos);
559 break;
560 case T_EXIT:
561 if (c >= sp->num_exits)
562 return ("Unknown exit");
563 p.new_dir = DIR_FROM_DXDY(sp->exit[(int)c].x - p.xpos,
564 sp->exit[(int)c].y - p.ypos);
565 break;
566 case T_AIRPORT:
567 if (c >= sp->num_airports)
568 return ("Unknown airport");
569 p.new_dir = DIR_FROM_DXDY(sp->airport[(int)c].x - p.xpos,
570 sp->airport[(int)c].y - p.ypos);
571 break;
572 default:
573 return ("Unknown case in benum! Get help!");
574 }
575 return (NULL);
576 }
577
578 const char *
579 to_dir(int c)
580 {
581 p.new_dir = dir_no(c);
582 return (NULL);
583 }
584
585 const char *
586 rel_dir(int c)
587 {
588 int angle;
589
590 angle = dir_no(c);
591 switch (dir) {
592 case D_LEFT:
593 p.new_dir = p.dir - angle;
594 if (p.new_dir < 0)
595 p.new_dir += MAXDIR;
596 break;
597 case D_RIGHT:
598 p.new_dir = p.dir + angle;
599 if (p.new_dir >= MAXDIR)
600 p.new_dir -= MAXDIR;
601 break;
602 default:
603 return ("Bizarre direction in rel_dir! Get help!");
604 }
605 return (NULL);
606 }
607
608 /* ARGSUSED */
609 const char *
610 mark(int c __attribute__((__unused__)))
611 {
612 if (p.altitude == 0)
613 return ("Cannot mark planes on the ground");
614 if (p.status == S_MARKED)
615 return ("Already marked");
616 p.status = S_MARKED;
617 return (NULL);
618 }
619
620 /* ARGSUSED */
621 const char *
622 unmark(int c __attribute__((__unused__)))
623 {
624 if (p.altitude == 0)
625 return ("Cannot unmark planes on the ground");
626 if (p.status == S_UNMARKED)
627 return ("Already unmarked");
628 p.status = S_UNMARKED;
629 return (NULL);
630 }
631
632 /* ARGSUSED */
633 const char *
634 ignore(int c __attribute__((__unused__)))
635 {
636 if (p.altitude == 0)
637 return ("Cannot ignore planes on the ground");
638 if (p.status == S_IGNORED)
639 return ("Already ignored");
640 p.status = S_IGNORED;
641 return (NULL);
642 }
643
644 int
645 dir_no(int ch)
646 {
647 int dirno;
648
649 dirno = -1;
650 switch (ch) {
651 case 'w': dirno = 0; break;
652 case 'e': dirno = 1; break;
653 case 'd': dirno = 2; break;
654 case 'c': dirno = 3; break;
655 case 'x': dirno = 4; break;
656 case 'z': dirno = 5; break;
657 case 'a': dirno = 6; break;
658 case 'q': dirno = 7; break;
659 default:
660 (void)fprintf(stderr, "bad character in dir_no\n");
661 break;
662 }
663 return (dirno);
664 }
665