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