input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* $NetBSD: input.c,v 1.17 2005/02/15 12:56:20 jsm Exp $ */
  2. /*-
  3. * Copyright (c) 1990, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ed James.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. /*
  34. * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
  35. *
  36. * Copy permission is hereby granted provided that this notice is
  37. * retained on all partial or complete copies.
  38. *
  39. * For more info on this and all of my stuff, mail edjames@berkeley.edu.
  40. */
  41. #include <sys/cdefs.h>
  42. #ifndef lint
  43. #if 0
  44. static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
  45. #else
  46. __RCSID("$NetBSD: input.c,v 1.17 2005/02/15 12:56:20 jsm Exp $");
  47. #endif
  48. #endif /* not lint */
  49. #include "include.h"
  50. #include "pathnames.h"
  51. #define MAXRULES 6
  52. #define MAXDEPTH 15
  53. #define RETTOKEN '\n'
  54. #define REDRAWTOKEN '\014' /* CTRL(L) */
  55. #define SHELLTOKEN '!'
  56. #define HELPTOKEN '?'
  57. #define ALPHATOKEN 256
  58. #define NUMTOKEN 257
  59. typedef struct {
  60. int token;
  61. int to_state;
  62. const char *str;
  63. const char *(*func)(char);
  64. } RULE;
  65. typedef struct {
  66. int num_rules;
  67. RULE *rule;
  68. } STATE;
  69. typedef struct {
  70. char str[20];
  71. int state;
  72. int rule;
  73. int ch;
  74. int pos;
  75. } STACK;
  76. #define T_RULE stack[level].rule
  77. #define T_STATE stack[level].state
  78. #define T_STR stack[level].str
  79. #define T_POS stack[level].pos
  80. #define T_CH stack[level].ch
  81. #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
  82. #define NUMSTATES NUMELS(st)
  83. RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
  84. { RETTOKEN, -1, "", NULL },
  85. { HELPTOKEN, 12, " [a-z]<ret>", NULL }},
  86. state1[] = { { 't', 2, " turn", turn },
  87. { 'a', 3, " altitude:", NULL },
  88. { 'c', 4, " circle", circle },
  89. { 'm', 7, " mark", mark },
  90. { 'u', 7, " unmark", unmark },
  91. { 'i', 7, " ignore", ignore },
  92. { HELPTOKEN, 12, " tacmui", NULL }},
  93. state2[] = { { 'l', 6, " left", left },
  94. { 'r', 6, " right", right },
  95. { 'L', 4, " left 90", Left },
  96. { 'R', 4, " right 90", Right },
  97. { 't', 11, " towards", NULL },
  98. { 'w', 4, " to 0", to_dir },
  99. { 'e', 4, " to 45", to_dir },
  100. { 'd', 4, " to 90", to_dir },
  101. { 'c', 4, " to 135", to_dir },
  102. { 'x', 4, " to 180", to_dir },
  103. { 'z', 4, " to 225", to_dir },
  104. { 'a', 4, " to 270", to_dir },
  105. { 'q', 4, " to 315", to_dir },
  106. { HELPTOKEN, 12, " lrLRt<dir>", NULL }},
  107. state3[] = { { '+', 10, " climb", climb },
  108. { 'c', 10, " climb", climb },
  109. { '-', 10, " descend", descend },
  110. { 'd', 10, " descend", descend },
  111. { NUMTOKEN, 7, " %c000 feet", setalt },
  112. { HELPTOKEN, 12, " +-cd[0-9]", NULL }},
  113. state4[] = { { '@', 9, " at", NULL },
  114. { 'a', 9, " at", NULL },
  115. { RETTOKEN, -1, "", NULL },
  116. { HELPTOKEN, 12, " @a<ret>", NULL }},
  117. state5[] = { { NUMTOKEN, 7, "%c", delayb },
  118. { HELPTOKEN, 12, " [0-9]", NULL }},
  119. state6[] = { { '@', 9, " at", NULL },
  120. { 'a', 9, " at", NULL },
  121. { 'w', 4, " 0", rel_dir },
  122. { 'e', 4, " 45", rel_dir },
  123. { 'd', 4, " 90", rel_dir },
  124. { 'c', 4, " 135", rel_dir },
  125. { 'x', 4, " 180", rel_dir },
  126. { 'z', 4, " 225", rel_dir },
  127. { 'a', 4, " 270", rel_dir },
  128. { 'q', 4, " 315", rel_dir },
  129. { RETTOKEN, -1, "", NULL },
  130. { HELPTOKEN, 12, " @a<dir><ret>",NULL }},
  131. state7[] = { { RETTOKEN, -1, "", NULL },
  132. { HELPTOKEN, 12, " <ret>", NULL }},
  133. state8[] = { { NUMTOKEN, 4, "%c", benum },
  134. { HELPTOKEN, 12, " [0-9]", NULL }},
  135. state9[] = { { 'b', 5, " beacon #", NULL },
  136. { '*', 5, " beacon #", NULL },
  137. { HELPTOKEN, 12, " b*", NULL }},
  138. state10[] = { { NUMTOKEN, 7, " %c000 ft", setrelalt},
  139. { HELPTOKEN, 12, " [0-9]", NULL }},
  140. state11[] = { { 'b', 8, " beacon #", beacon },
  141. { '*', 8, " beacon #", beacon },
  142. { 'e', 8, " exit #", ex_it },
  143. { 'a', 8, " airport #", airport },
  144. { HELPTOKEN, 12, " b*ea", NULL }},
  145. state12[] = { { -1, -1, "", NULL }};
  146. #define DEF_STATE(s) { NUMELS(s), (s) }
  147. STATE st[] = {
  148. DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
  149. DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
  150. DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
  151. DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
  152. DEF_STATE(state12)
  153. };
  154. PLANE p;
  155. STACK stack[MAXDEPTH];
  156. int level;
  157. int tval;
  158. int dest_type, dest_no, dir;
  159. int
  160. pop()
  161. {
  162. if (level == 0)
  163. return (-1);
  164. level--;
  165. ioclrtoeol(T_POS);
  166. strcpy(T_STR, "");
  167. T_RULE = -1;
  168. T_CH = -1;
  169. return (0);
  170. }
  171. void
  172. rezero()
  173. {
  174. iomove(0);
  175. level = 0;
  176. T_STATE = 0;
  177. T_RULE = -1;
  178. T_CH = -1;
  179. T_POS = 0;
  180. strcpy(T_STR, "");
  181. }
  182. void
  183. push(ruleno, ch)
  184. int ruleno, ch;
  185. {
  186. int newstate, newpos;
  187. (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
  188. T_RULE = ruleno;
  189. T_CH = ch;
  190. newstate = st[T_STATE].rule[ruleno].to_state;
  191. newpos = T_POS + strlen(T_STR);
  192. ioaddstr(T_POS, T_STR);
  193. if (level == 0)
  194. ioclrtobot();
  195. level++;
  196. T_STATE = newstate;
  197. T_POS = newpos;
  198. T_RULE = -1;
  199. strcpy(T_STR, "");
  200. }
  201. int
  202. getcommand()
  203. {
  204. int c, i, done;
  205. const char *s, *(*func)(char);
  206. PLANE *pp;
  207. rezero();
  208. do {
  209. c = gettoken();
  210. if (c == tty_new.c_cc[VERASE]) {
  211. if (pop() < 0)
  212. noise();
  213. } else if (c == tty_new.c_cc[VKILL]) {
  214. while (pop() >= 0)
  215. ;
  216. } else {
  217. done = 0;
  218. for (i = 0; i < st[T_STATE].num_rules; i++) {
  219. if (st[T_STATE].rule[i].token == c ||
  220. st[T_STATE].rule[i].token == tval) {
  221. push(i, (c >= ALPHATOKEN) ? tval : c);
  222. done = 1;
  223. break;
  224. }
  225. }
  226. if (!done)
  227. noise();
  228. }
  229. } while (T_STATE != -1);
  230. if (level == 1)
  231. return (1); /* forced update */
  232. dest_type = T_NODEST;
  233. for (i = 0; i < level; i++) {
  234. func = st[stack[i].state].rule[stack[i].rule].func;
  235. if (func != NULL)
  236. if ((s = (*func)(stack[i].ch)) != NULL) {
  237. ioerror(stack[i].pos, strlen(stack[i].str), s);
  238. return (-1);
  239. }
  240. }
  241. pp = findplane(p.plane_no);
  242. if (pp->new_altitude != p.new_altitude)
  243. pp->new_altitude = p.new_altitude;
  244. else if (pp->status != p.status)
  245. pp->status = p.status;
  246. else {
  247. pp->new_dir = p.new_dir;
  248. pp->delayd = p.delayd;
  249. pp->delayd_no = p.delayd_no;
  250. }
  251. return (0);
  252. }
  253. void
  254. noise()
  255. {
  256. putchar('\07');
  257. fflush(stdout);
  258. }
  259. int
  260. gettoken()
  261. {
  262. while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
  263. {
  264. if (tval == SHELLTOKEN)
  265. {
  266. #ifdef BSD
  267. struct itimerval itv;
  268. itv.it_value.tv_sec = 0;
  269. itv.it_value.tv_usec = 0;
  270. setitimer(ITIMER_REAL, &itv, NULL);
  271. #endif
  272. #ifdef SYSV
  273. int aval;
  274. aval = alarm(0);
  275. #endif
  276. if (fork() == 0) /* child */
  277. {
  278. char *shell, *base;
  279. done_screen();
  280. /* run user's favorite shell */
  281. if ((shell = getenv("SHELL")) != NULL)
  282. {
  283. base = strrchr(shell, '/');
  284. if (base == NULL)
  285. base = shell;
  286. else
  287. base++;
  288. execl(shell, base, (char *) 0);
  289. }
  290. else
  291. execl(_PATH_BSHELL, "sh", (char *) 0);
  292. exit(0); /* oops */
  293. }
  294. wait(0);
  295. tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
  296. #ifdef BSD
  297. itv.it_value.tv_sec = 0;
  298. itv.it_value.tv_usec = 1;
  299. itv.it_interval.tv_sec = sp->update_secs;
  300. itv.it_interval.tv_usec = 0;
  301. setitimer(ITIMER_REAL, &itv, NULL);
  302. #endif
  303. #ifdef SYSV
  304. alarm(aval);
  305. #endif
  306. }
  307. redraw();
  308. }
  309. if (isdigit(tval))
  310. return (NUMTOKEN);
  311. else if (isalpha(tval))
  312. return (ALPHATOKEN);
  313. else
  314. return (tval);
  315. }
  316. const char *
  317. setplane(c)
  318. char c;
  319. {
  320. PLANE *pp;
  321. pp = findplane(number(c));
  322. if (pp == NULL)
  323. return ("Unknown Plane");
  324. memcpy(&p, pp, sizeof (p));
  325. p.delayd = 0;
  326. return (NULL);
  327. }
  328. const char *
  329. turn(c)
  330. char c __attribute__((__unused__));
  331. {
  332. if (p.altitude == 0)
  333. return ("Planes at airports may not change direction");
  334. return (NULL);
  335. }
  336. const char *
  337. circle(c)
  338. char c __attribute__((__unused__));
  339. {
  340. if (p.altitude == 0)
  341. return ("Planes cannot circle on the ground");
  342. p.new_dir = MAXDIR;
  343. return (NULL);
  344. }
  345. const char *
  346. left(c)
  347. char c __attribute__((__unused__));
  348. {
  349. dir = D_LEFT;
  350. p.new_dir = p.dir - 1;
  351. if (p.new_dir < 0)
  352. p.new_dir += MAXDIR;
  353. return (NULL);
  354. }
  355. const char *
  356. right(c)
  357. char c __attribute__((__unused__));
  358. {
  359. dir = D_RIGHT;
  360. p.new_dir = p.dir + 1;
  361. if (p.new_dir >= MAXDIR)
  362. p.new_dir -= MAXDIR;
  363. return (NULL);
  364. }
  365. const char *
  366. Left(c)
  367. char c __attribute__((__unused__));
  368. {
  369. p.new_dir = p.dir - 2;
  370. if (p.new_dir < 0)
  371. p.new_dir += MAXDIR;
  372. return (NULL);
  373. }
  374. const char *
  375. Right(c)
  376. char c __attribute__((__unused__));
  377. {
  378. p.new_dir = p.dir + 2;
  379. if (p.new_dir >= MAXDIR)
  380. p.new_dir -= MAXDIR;
  381. return (NULL);
  382. }
  383. const char *
  384. delayb(c)
  385. char c;
  386. {
  387. int xdiff, ydiff;
  388. c -= '0';
  389. if (c >= sp->num_beacons)
  390. return ("Unknown beacon");
  391. xdiff = sp->beacon[(int)c].x - p.xpos;
  392. xdiff = SGN(xdiff);
  393. ydiff = sp->beacon[(int)c].y - p.ypos;
  394. ydiff = SGN(ydiff);
  395. if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
  396. return ("Beacon is not in flight path");
  397. p.delayd = 1;
  398. p.delayd_no = c;
  399. if (dest_type != T_NODEST) {
  400. switch (dest_type) {
  401. case T_BEACON:
  402. xdiff = sp->beacon[dest_no].x - sp->beacon[(int)c].x;
  403. ydiff = sp->beacon[dest_no].y - sp->beacon[(int)c].y;
  404. break;
  405. case T_EXIT:
  406. xdiff = sp->exit[dest_no].x - sp->beacon[(int)c].x;
  407. ydiff = sp->exit[dest_no].y - sp->beacon[(int)c].y;
  408. break;
  409. case T_AIRPORT:
  410. xdiff = sp->airport[dest_no].x - sp->beacon[(int)c].x;
  411. ydiff = sp->airport[dest_no].y - sp->beacon[(int)c].y;
  412. break;
  413. default:
  414. return ("Bad case in delayb! Get help!");
  415. break;
  416. }
  417. if (xdiff == 0 && ydiff == 0)
  418. return ("Would already be there");
  419. p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
  420. if (p.new_dir == p.dir)
  421. return ("Already going in that direction");
  422. }
  423. return (NULL);
  424. }
  425. const char *
  426. beacon(c)
  427. char c __attribute__((__unused__));
  428. {
  429. dest_type = T_BEACON;
  430. return (NULL);
  431. }
  432. const char *
  433. ex_it(c)
  434. char c __attribute__((__unused__));
  435. {
  436. dest_type = T_EXIT;
  437. return (NULL);
  438. }
  439. const char *
  440. airport(c)
  441. char c __attribute__((__unused__));
  442. {
  443. dest_type = T_AIRPORT;
  444. return (NULL);
  445. }
  446. const char *
  447. climb(c)
  448. char c __attribute__((__unused__));
  449. {
  450. dir = D_UP;
  451. return (NULL);
  452. }
  453. const char *
  454. descend(c)
  455. char c __attribute__((__unused__));
  456. {
  457. dir = D_DOWN;
  458. return (NULL);
  459. }
  460. const char *
  461. setalt(c)
  462. char c;
  463. {
  464. if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
  465. return ("Already at that altitude");
  466. p.new_altitude = c - '0';
  467. return (NULL);
  468. }
  469. const char *
  470. setrelalt(c)
  471. char c;
  472. {
  473. if (c == 0)
  474. return ("altitude not changed");
  475. switch (dir) {
  476. case D_UP:
  477. p.new_altitude = p.altitude + c - '0';
  478. break;
  479. case D_DOWN:
  480. p.new_altitude = p.altitude - (c - '0');
  481. break;
  482. default:
  483. return ("Unknown case in setrelalt! Get help!");
  484. break;
  485. }
  486. if (p.new_altitude < 0)
  487. return ("Altitude would be too low");
  488. else if (p.new_altitude > 9)
  489. return ("Altitude would be too high");
  490. return (NULL);
  491. }
  492. const char *
  493. benum(c)
  494. char c;
  495. {
  496. dest_no = c -= '0';
  497. switch (dest_type) {
  498. case T_BEACON:
  499. if (c >= sp->num_beacons)
  500. return ("Unknown beacon");
  501. p.new_dir = DIR_FROM_DXDY(sp->beacon[(int)c].x - p.xpos,
  502. sp->beacon[(int)c].y - p.ypos);
  503. break;
  504. case T_EXIT:
  505. if (c >= sp->num_exits)
  506. return ("Unknown exit");
  507. p.new_dir = DIR_FROM_DXDY(sp->exit[(int)c].x - p.xpos,
  508. sp->exit[(int)c].y - p.ypos);
  509. break;
  510. case T_AIRPORT:
  511. if (c >= sp->num_airports)
  512. return ("Unknown airport");
  513. p.new_dir = DIR_FROM_DXDY(sp->airport[(int)c].x - p.xpos,
  514. sp->airport[(int)c].y - p.ypos);
  515. break;
  516. default:
  517. return ("Unknown case in benum! Get help!");
  518. break;
  519. }
  520. return (NULL);
  521. }
  522. const char *
  523. to_dir(c)
  524. char c;
  525. {
  526. p.new_dir = dir_no(c);
  527. return (NULL);
  528. }
  529. const char *
  530. rel_dir(c)
  531. char c;
  532. {
  533. int angle;
  534. angle = dir_no(c);
  535. switch (dir) {
  536. case D_LEFT:
  537. p.new_dir = p.dir - angle;
  538. if (p.new_dir < 0)
  539. p.new_dir += MAXDIR;
  540. break;
  541. case D_RIGHT:
  542. p.new_dir = p.dir + angle;
  543. if (p.new_dir >= MAXDIR)
  544. p.new_dir -= MAXDIR;
  545. break;
  546. default:
  547. return ("Bizarre direction in rel_dir! Get help!");
  548. break;
  549. }
  550. return (NULL);
  551. }
  552. const char *
  553. mark(c)
  554. char c __attribute__((__unused__));
  555. {
  556. if (p.altitude == 0)
  557. return ("Cannot mark planes on the ground");
  558. if (p.status == S_MARKED)
  559. return ("Already marked");
  560. p.status = S_MARKED;
  561. return (NULL);
  562. }
  563. const char *
  564. unmark(c)
  565. char c __attribute__((__unused__));
  566. {
  567. if (p.altitude == 0)
  568. return ("Cannot unmark planes on the ground");
  569. if (p.status == S_UNMARKED)
  570. return ("Already unmarked");
  571. p.status = S_UNMARKED;
  572. return (NULL);
  573. }
  574. const char *
  575. ignore(c)
  576. char c __attribute__((__unused__));
  577. {
  578. if (p.altitude == 0)
  579. return ("Cannot ignore planes on the ground");
  580. if (p.status == S_IGNORED)
  581. return ("Already ignored");
  582. p.status = S_IGNORED;
  583. return (NULL);
  584. }
  585. int
  586. dir_no(ch)
  587. char ch;
  588. {
  589. int dir;
  590. dir = -1;
  591. switch (ch) {
  592. case 'w': dir = 0; break;
  593. case 'e': dir = 1; break;
  594. case 'd': dir = 2; break;
  595. case 'c': dir = 3; break;
  596. case 'x': dir = 4; break;
  597. case 'z': dir = 5; break;
  598. case 'a': dir = 6; break;
  599. case 'q': dir = 7; break;
  600. default:
  601. fprintf(stderr, "bad character in dir_no\n");
  602. break;
  603. }
  604. return (dir);
  605. }