]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/input.c
Replace special handling of \r by using ICRNL (PR#6078 by Joseph Myers <jsm28@cam...
[bsdgames-darwin.git] / atc / input.c
1 /* $NetBSD: input.c,v 1.10 1998/09/13 15:20:31 hubertf 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@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.10 1998/09/13 15:20:31 hubertf 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 #define REDRAWTOKEN '\014' /* CTRL(L) */
65 #define SHELLTOKEN '!'
66 #define HELPTOKEN '?'
67 #define ALPHATOKEN 256
68 #define NUMTOKEN 257
69
70 typedef struct {
71 int token;
72 int to_state;
73 char *str;
74 char *(*func) __P((char));
75 } RULE;
76
77 typedef struct {
78 int num_rules;
79 RULE *rule;
80 } STATE;
81
82 typedef struct {
83 char str[20];
84 int state;
85 int rule;
86 int ch;
87 int pos;
88 } STACK;
89
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
95
96 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
97
98 #define NUMSTATES NUMELS(st)
99
100
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 }};
164
165 #define DEF_STATE(s) { NUMELS(s), (s) }
166
167 STATE st[] = {
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),
172 DEF_STATE(state12)
173 };
174
175 PLANE p;
176 STACK stack[MAXDEPTH];
177 int level;
178 int tval;
179 int dest_type, dest_no, dir;
180
181 int
182 pop()
183 {
184 if (level == 0)
185 return (-1);
186 level--;
187
188 ioclrtoeol(T_POS);
189
190 strcpy(T_STR, "");
191 T_RULE = -1;
192 T_CH = -1;
193 return (0);
194 }
195
196 void
197 rezero()
198 {
199 iomove(0);
200
201 level = 0;
202 T_STATE = 0;
203 T_RULE = -1;
204 T_CH = -1;
205 T_POS = 0;
206 strcpy(T_STR, "");
207 }
208
209 void
210 push(ruleno, ch)
211 int ruleno, ch;
212 {
213 int newstate, newpos;
214
215 (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
216 T_RULE = ruleno;
217 T_CH = ch;
218 newstate = st[T_STATE].rule[ruleno].to_state;
219 newpos = T_POS + strlen(T_STR);
220
221 ioaddstr(T_POS, T_STR);
222
223 if (level == 0)
224 ioclrtobot();
225 level++;
226 T_STATE = newstate;
227 T_POS = newpos;
228 T_RULE = -1;
229 strcpy(T_STR, "");
230 }
231
232 int
233 getcommand()
234 {
235 int c, i, done;
236 char *s, *(*func) __P((char));
237 PLANE *pp;
238
239 rezero();
240
241 do {
242 c = gettoken();
243 if (c == tty_new.c_cc[VERASE]) {
244 if (pop() < 0)
245 noise();
246 } else if (c == tty_new.c_cc[VKILL]) {
247 while (pop() >= 0)
248 ;
249 } else {
250 done = 0;
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);
255 done = 1;
256 break;
257 }
258 }
259 if (!done)
260 noise();
261 }
262 } while (T_STATE != -1);
263
264 if (level == 1)
265 return (1); /* forced update */
266
267 dest_type = T_NODEST;
268
269 for (i = 0; i < level; i++) {
270 func = st[stack[i].state].rule[stack[i].rule].func;
271 if (func != NULL)
272 if ((s = (*func)(stack[i].ch)) != NULL) {
273 ioerror(stack[i].pos, strlen(stack[i].str), s);
274 return (-1);
275 }
276 }
277
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;
283 else {
284 pp->new_dir = p.new_dir;
285 pp->delayd = p.delayd;
286 pp->delayd_no = p.delayd_no;
287 }
288 return (0);
289 }
290
291 void
292 noise()
293 {
294 putchar('\07');
295 fflush(stdout);
296 }
297
298 int
299 gettoken()
300 {
301 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
302 {
303 if (tval == SHELLTOKEN)
304 {
305 #ifdef BSD
306 struct itimerval itv;
307 itv.it_value.tv_sec = 0;
308 itv.it_value.tv_usec = 0;
309 setitimer(ITIMER_REAL, &itv, NULL);
310 #endif
311 #ifdef SYSV
312 int aval;
313 aval = alarm(0);
314 #endif
315 if (fork() == 0) /* child */
316 {
317 char *shell, *base;
318
319 setuid(getuid()); /* turn off setuid bit */
320 done_screen();
321
322 /* run user's favorite shell */
323 if ((shell = getenv("SHELL")) != NULL)
324 {
325 base = strrchr(shell, '/');
326 if (base == NULL)
327 base = shell;
328 else
329 base++;
330 execl(shell, base, 0);
331 }
332 else
333 execl(_PATH_BSHELL, "sh", 0);
334
335 exit(0); /* oops */
336 }
337
338 wait(0);
339 tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
340 #ifdef BSD
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);
346 #endif
347 #ifdef SYSV
348 alarm(aval);
349 #endif
350 }
351 redraw();
352 }
353
354 if (isdigit(tval))
355 return (NUMTOKEN);
356 else if (isalpha(tval))
357 return (ALPHATOKEN);
358 else
359 return (tval);
360 }
361
362 char *
363 setplane(c)
364 char c;
365 {
366 PLANE *pp;
367
368 pp = findplane(number(c));
369 if (pp == NULL)
370 return ("Unknown Plane");
371 memcpy(&p, pp, sizeof (p));
372 p.delayd = 0;
373 return (NULL);
374 }
375
376 char *
377 turn(c)
378 char c;
379 {
380 if (p.altitude == 0)
381 return ("Planes at airports may not change direction");
382 return (NULL);
383 }
384
385 char *
386 circle(c)
387 char c;
388 {
389 if (p.altitude == 0)
390 return ("Planes cannot circle on the ground");
391 p.new_dir = MAXDIR;
392 return (NULL);
393 }
394
395 char *
396 left(c)
397 char c;
398 {
399 dir = D_LEFT;
400 p.new_dir = p.dir - 1;
401 if (p.new_dir < 0)
402 p.new_dir += MAXDIR;
403 return (NULL);
404 }
405
406 char *
407 right(c)
408 char c;
409 {
410 dir = D_RIGHT;
411 p.new_dir = p.dir + 1;
412 if (p.new_dir >= MAXDIR)
413 p.new_dir -= MAXDIR;
414 return (NULL);
415 }
416
417 char *
418 Left(c)
419 char c;
420 {
421 p.new_dir = p.dir - 2;
422 if (p.new_dir < 0)
423 p.new_dir += MAXDIR;
424 return (NULL);
425 }
426
427 char *
428 Right(c)
429 char c;
430 {
431 p.new_dir = p.dir + 2;
432 if (p.new_dir >= MAXDIR)
433 p.new_dir -= MAXDIR;
434 return (NULL);
435 }
436
437 char *
438 delayb(c)
439 char c;
440 {
441 int xdiff, ydiff;
442
443 c -= '0';
444
445 if (c >= sp->num_beacons)
446 return ("Unknown beacon");
447 xdiff = sp->beacon[(int)c].x - p.xpos;
448 xdiff = SGN(xdiff);
449 ydiff = sp->beacon[(int)c].y - p.ypos;
450 ydiff = SGN(ydiff);
451 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
452 return ("Beacon is not in flight path");
453 p.delayd = 1;
454 p.delayd_no = c;
455
456 if (dest_type != T_NODEST) {
457 switch (dest_type) {
458 case T_BEACON:
459 xdiff = sp->beacon[dest_no].x - sp->beacon[(int)c].x;
460 ydiff = sp->beacon[dest_no].y - sp->beacon[(int)c].y;
461 break;
462 case T_EXIT:
463 xdiff = sp->exit[dest_no].x - sp->beacon[(int)c].x;
464 ydiff = sp->exit[dest_no].y - sp->beacon[(int)c].y;
465 break;
466 case T_AIRPORT:
467 xdiff = sp->airport[dest_no].x - sp->beacon[(int)c].x;
468 ydiff = sp->airport[dest_no].y - sp->beacon[(int)c].y;
469 break;
470 default:
471 return ("Bad case in delayb! Get help!");
472 break;
473 }
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");
479 }
480 return (NULL);
481 }
482
483 char *
484 beacon(c)
485 char c;
486 {
487 dest_type = T_BEACON;
488 return (NULL);
489 }
490
491 char *
492 ex_it(c)
493 char c;
494 {
495 dest_type = T_EXIT;
496 return (NULL);
497 }
498
499 char *
500 airport(c)
501 char c;
502 {
503 dest_type = T_AIRPORT;
504 return (NULL);
505 }
506
507 char *
508 climb(c)
509 char c;
510 {
511 dir = D_UP;
512 return (NULL);
513 }
514
515 char *
516 descend(c)
517 char c;
518 {
519 dir = D_DOWN;
520 return (NULL);
521 }
522
523 char *
524 setalt(c)
525 char c;
526 {
527 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
528 return ("Already at that altitude");
529 p.new_altitude = c - '0';
530 return (NULL);
531 }
532
533 char *
534 setrelalt(c)
535 char c;
536 {
537 if (c == 0)
538 return ("altitude not changed");
539
540 switch (dir) {
541 case D_UP:
542 p.new_altitude = p.altitude + c - '0';
543 break;
544 case D_DOWN:
545 p.new_altitude = p.altitude - (c - '0');
546 break;
547 default:
548 return ("Unknown case in setrelalt! Get help!");
549 break;
550 }
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");
555 return (NULL);
556 }
557
558 char *
559 benum(c)
560 char c;
561 {
562 dest_no = c -= '0';
563
564 switch (dest_type) {
565 case T_BEACON:
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);
570 break;
571 case T_EXIT:
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);
576 break;
577 case T_AIRPORT:
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);
582 break;
583 default:
584 return ("Unknown case in benum! Get help!");
585 break;
586 }
587 return (NULL);
588 }
589
590 char *
591 to_dir(c)
592 char c;
593 {
594 p.new_dir = dir_no(c);
595 return (NULL);
596 }
597
598 char *
599 rel_dir(c)
600 char c;
601 {
602 int angle;
603
604 angle = dir_no(c);
605 switch (dir) {
606 case D_LEFT:
607 p.new_dir = p.dir - angle;
608 if (p.new_dir < 0)
609 p.new_dir += MAXDIR;
610 break;
611 case D_RIGHT:
612 p.new_dir = p.dir + angle;
613 if (p.new_dir >= MAXDIR)
614 p.new_dir -= MAXDIR;
615 break;
616 default:
617 return ("Bizarre direction in rel_dir! Get help!");
618 break;
619 }
620 return (NULL);
621 }
622
623 char *
624 mark(c)
625 char c;
626 {
627 if (p.altitude == 0)
628 return ("Cannot mark planes on the ground");
629 if (p.status == S_MARKED)
630 return ("Already marked");
631 p.status = S_MARKED;
632 return (NULL);
633 }
634
635 char *
636 unmark(c)
637 char c;
638 {
639 if (p.altitude == 0)
640 return ("Cannot unmark planes on the ground");
641 if (p.status == S_UNMARKED)
642 return ("Already unmarked");
643 p.status = S_UNMARKED;
644 return (NULL);
645 }
646
647 char *
648 ignore(c)
649 char c;
650 {
651 if (p.altitude == 0)
652 return ("Cannot ignore planes on the ground");
653 if (p.status == S_IGNORED)
654 return ("Already ignored");
655 p.status = S_IGNORED;
656 return (NULL);
657 }
658
659 int
660 dir_no(ch)
661 char ch;
662 {
663 int dir;
664
665 dir = -1;
666 switch (ch) {
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;
675 default:
676 fprintf(stderr, "bad character in dir_no\n");
677 break;
678 }
679 return (dir);
680 }