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