1 /* $NetBSD: input.c,v 1.22 2007/12/15 19:44:38 perry 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.22 2007/12/15 19:44:38 perry 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 assert(level < (MAXDEPTH - 1)); 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 (void)strcpy(T_STR, ""); 226 } 227 228 int 229 getcommand(void) 230 { 231 int c, i, done; 232 const char *s, *(*func)(int); 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, 270 (int)strlen(stack[i].str), s); 271 return (-1); 272 } 273 } 274 275 pp = findplane(p.plane_no); 276 if (pp->new_altitude != p.new_altitude) 277 pp->new_altitude = p.new_altitude; 278 else if (pp->status != p.status) 279 pp->status = p.status; 280 else { 281 pp->new_dir = p.new_dir; 282 pp->delayd = p.delayd; 283 pp->delayd_no = p.delayd_no; 284 } 285 return (0); 286 } 287 288 void 289 noise(void) 290 { 291 (void)putchar('\07'); 292 (void)fflush(stdout); 293 } 294 295 int 296 gettoken(void) 297 { 298 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN) 299 { 300 if (tval == SHELLTOKEN) 301 { 302 #ifdef BSD 303 struct itimerval itv; 304 itv.it_value.tv_sec = 0; 305 itv.it_value.tv_usec = 0; 306 (void)setitimer(ITIMER_REAL, &itv, NULL); 307 #endif 308 #ifdef SYSV 309 int aval; 310 aval = alarm(0); 311 #endif 312 if (fork() == 0) /* child */ 313 { 314 char *shell, *base; 315 316 done_screen(); 317 318 /* run user's favorite shell */ 319 if ((shell = getenv("SHELL")) != NULL) 320 { 321 base = strrchr(shell, '/'); 322 if (base == NULL) 323 base = shell; 324 else 325 base++; 326 (void)execl(shell, base, (char *) 0); 327 } 328 else 329 (void)execl(_PATH_BSHELL, "sh", 330 (char *) 0); 331 332 exit(0); /* oops */ 333 } 334 335 (void)wait(0); 336 (void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new); 337 #ifdef BSD 338 itv.it_value.tv_sec = 0; 339 itv.it_value.tv_usec = 1; 340 itv.it_interval.tv_sec = sp->update_secs; 341 itv.it_interval.tv_usec = 0; 342 (void)setitimer(ITIMER_REAL, &itv, NULL); 343 #endif 344 #ifdef SYSV 345 alarm(aval); 346 #endif 347 } 348 (void)redraw(); 349 } 350 351 if (isdigit(tval)) 352 return (NUMTOKEN); 353 else if (isalpha(tval)) 354 return (ALPHATOKEN); 355 else 356 return (tval); 357 } 358 359 const char * 360 setplane(int c) 361 { 362 PLANE *pp; 363 364 pp = findplane(number(c)); 365 if (pp == NULL) 366 return ("Unknown Plane"); 367 (void)memcpy(&p, pp, sizeof (p)); 368 p.delayd = 0; 369 return (NULL); 370 } 371 372 /* ARGSUSED */ 373 const char * 374 turn(int c __unused) 375 { 376 if (p.altitude == 0) 377 return ("Planes at airports may not change direction"); 378 return (NULL); 379 } 380 381 /* ARGSUSED */ 382 const char * 383 circle(int c __unused) 384 { 385 if (p.altitude == 0) 386 return ("Planes cannot circle on the ground"); 387 p.new_dir = MAXDIR; 388 return (NULL); 389 } 390 391 /* ARGSUSED */ 392 const char * 393 left(int c __unused) 394 { 395 dir = D_LEFT; 396 p.new_dir = p.dir - 1; 397 if (p.new_dir < 0) 398 p.new_dir += MAXDIR; 399 return (NULL); 400 } 401 402 /* ARGSUSED */ 403 const char * 404 right(int c __unused) 405 { 406 dir = D_RIGHT; 407 p.new_dir = p.dir + 1; 408 if (p.new_dir >= MAXDIR) 409 p.new_dir -= MAXDIR; 410 return (NULL); 411 } 412 413 /* ARGSUSED */ 414 const char * 415 Left(int c __unused) 416 { 417 p.new_dir = p.dir - 2; 418 if (p.new_dir < 0) 419 p.new_dir += MAXDIR; 420 return (NULL); 421 } 422 423 /* ARGSUSED */ 424 const char * 425 Right(int c __unused) 426 { 427 p.new_dir = p.dir + 2; 428 if (p.new_dir >= MAXDIR) 429 p.new_dir -= MAXDIR; 430 return (NULL); 431 } 432 433 const char * 434 delayb(int 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 } 468 if (xdiff == 0 && ydiff == 0) 469 return ("Would already be there"); 470 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff); 471 if (p.new_dir == p.dir) 472 return ("Already going in that direction"); 473 } 474 return (NULL); 475 } 476 477 /* ARGSUSED */ 478 const char * 479 beacon(int c __unused) 480 { 481 dest_type = T_BEACON; 482 return (NULL); 483 } 484 485 /* ARGSUSED */ 486 const char * 487 ex_it(int c __unused) 488 { 489 dest_type = T_EXIT; 490 return (NULL); 491 } 492 493 /* ARGSUSED */ 494 const char * 495 airport(int c __unused) 496 { 497 dest_type = T_AIRPORT; 498 return (NULL); 499 } 500 501 /* ARGSUSED */ 502 const char * 503 climb(int c __unused) 504 { 505 dir = D_UP; 506 return (NULL); 507 } 508 509 /* ARGSUSED */ 510 const char * 511 descend(int c __unused) 512 { 513 dir = D_DOWN; 514 return (NULL); 515 } 516 517 const char * 518 setalt(int c) 519 { 520 int newalt = c - '0'; 521 if ((p.altitude == newalt) && (p.new_altitude == p.altitude)) 522 return ("Already at that altitude"); 523 if (p.new_altitude == newalt) { 524 return ("Already going to that altitude"); 525 } 526 p.new_altitude = newalt; 527 return (NULL); 528 } 529 530 const char * 531 setrelalt(int c) 532 { 533 int newalt; 534 535 if (c == 0) 536 return ("altitude not changed"); 537 538 switch (dir) { 539 case D_UP: 540 newalt = p.altitude + c - '0'; 541 break; 542 case D_DOWN: 543 newalt = p.altitude - (c - '0'); 544 break; 545 default: 546 return ("Unknown case in setrelalt! Get help!"); 547 } 548 549 if (p.new_altitude == newalt) 550 return ("Already going to that altitude"); 551 552 p.new_altitude = newalt; 553 554 if (p.new_altitude < 0) 555 return ("Altitude would be too low"); 556 else if (p.new_altitude > 9) 557 return ("Altitude would be too high"); 558 return (NULL); 559 } 560 561 const char * 562 benum(int c) 563 { 564 dest_no = c -= '0'; 565 566 switch (dest_type) { 567 case T_BEACON: 568 if (c >= sp->num_beacons) 569 return ("Unknown beacon"); 570 p.new_dir = DIR_FROM_DXDY(sp->beacon[(int)c].x - p.xpos, 571 sp->beacon[(int)c].y - p.ypos); 572 break; 573 case T_EXIT: 574 if (c >= sp->num_exits) 575 return ("Unknown exit"); 576 p.new_dir = DIR_FROM_DXDY(sp->exit[(int)c].x - p.xpos, 577 sp->exit[(int)c].y - p.ypos); 578 break; 579 case T_AIRPORT: 580 if (c >= sp->num_airports) 581 return ("Unknown airport"); 582 p.new_dir = DIR_FROM_DXDY(sp->airport[(int)c].x - p.xpos, 583 sp->airport[(int)c].y - p.ypos); 584 break; 585 default: 586 return ("Unknown case in benum! Get help!"); 587 } 588 return (NULL); 589 } 590 591 const char * 592 to_dir(int c) 593 { 594 p.new_dir = dir_no(c); 595 return (NULL); 596 } 597 598 const char * 599 rel_dir(int c) 600 { 601 int angle; 602 603 angle = dir_no(c); 604 switch (dir) { 605 case D_LEFT: 606 p.new_dir = p.dir - angle; 607 if (p.new_dir < 0) 608 p.new_dir += MAXDIR; 609 break; 610 case D_RIGHT: 611 p.new_dir = p.dir + angle; 612 if (p.new_dir >= MAXDIR) 613 p.new_dir -= MAXDIR; 614 break; 615 default: 616 return ("Bizarre direction in rel_dir! Get help!"); 617 } 618 return (NULL); 619 } 620 621 /* ARGSUSED */ 622 const char * 623 mark(int c __unused) 624 { 625 if (p.altitude == 0) 626 return ("Cannot mark planes on the ground"); 627 if (p.status == S_MARKED) 628 return ("Already marked"); 629 p.status = S_MARKED; 630 return (NULL); 631 } 632 633 /* ARGSUSED */ 634 const char * 635 unmark(int c __unused) 636 { 637 if (p.altitude == 0) 638 return ("Cannot unmark planes on the ground"); 639 if (p.status == S_UNMARKED) 640 return ("Already unmarked"); 641 p.status = S_UNMARKED; 642 return (NULL); 643 } 644 645 /* ARGSUSED */ 646 const char * 647 ignore(int c __unused) 648 { 649 if (p.altitude == 0) 650 return ("Cannot ignore planes on the ground"); 651 if (p.status == S_IGNORED) 652 return ("Already ignored"); 653 p.status = S_IGNORED; 654 return (NULL); 655 } 656 657 int 658 dir_no(int ch) 659 { 660 int dirno; 661 662 dirno = -1; 663 switch (ch) { 664 case 'w': dirno = 0; break; 665 case 'e': dirno = 1; break; 666 case 'd': dirno = 2; break; 667 case 'c': dirno = 3; break; 668 case 'x': dirno = 4; break; 669 case 'z': dirno = 5; break; 670 case 'a': dirno = 6; break; 671 case 'q': dirno = 7; break; 672 default: 673 (void)fprintf(stderr, "bad character in dir_no\n"); 674 break; 675 } 676 return (dirno); 677 } 678