X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/77e3814f0c0e3dea4d0032e25666f77e6f83bfff..b67924875ae1497bb26d5968768fdbf9947bd1fe:/atc/input.c diff --git a/atc/input.c b/atc/input.c index 693a7a3a..44310133 100644 --- a/atc/input.c +++ b/atc/input.c @@ -1,6 +1,8 @@ +/* $NetBSD: input.c,v 1.21 2006/10/07 18:29:02 elad Exp $ */ + /*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Ed James. @@ -13,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -43,9 +41,14 @@ * For more info on this and all of my stuff, mail edjames@berkeley.edu. */ +#include #ifndef lint -static char sccsid[] = "@(#)input.c 5.4 (Berkeley) 4/30/90"; -#endif not lint +#if 0 +static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: input.c,v 1.21 2006/10/07 18:29:02 elad Exp $"); +#endif +#endif /* not lint */ #include "include.h" #include "pathnames.h" @@ -54,9 +57,6 @@ static char sccsid[] = "@(#)input.c 5.4 (Berkeley) 4/30/90"; #define MAXDEPTH 15 #define RETTOKEN '\n' -#ifdef SYSV -#define CRTOKEN '\r' -#endif #define REDRAWTOKEN '\014' /* CTRL(L) */ #define SHELLTOKEN '!' #define HELPTOKEN '?' @@ -66,8 +66,8 @@ static char sccsid[] = "@(#)input.c 5.4 (Berkeley) 4/30/90"; typedef struct { int token; int to_state; - char *str; - char *(*func)(); + const char *str; + const char *(*func)(int); } RULE; typedef struct { @@ -93,16 +93,9 @@ typedef struct { #define NUMSTATES NUMELS(st) -char *setplane(), *circle(), *left(), *right(), *Left(), *Right(), - *beacon(), *ex_it(), *climb(), *descend(), *setalt(), *setrelalt(), - *benum(), *to_dir(), *rel_dir(), *delayb(), *mark(), *unmark(), - *airport(), *turn(), *ignore(); RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane}, { RETTOKEN, -1, "", NULL }, -#ifdef SYSV - { CRTOKEN, -1, "", NULL }, -#endif { HELPTOKEN, 12, " [a-z]", NULL }}, state1[] = { { 't', 2, " turn", turn }, { 'a', 3, " altitude:", NULL }, @@ -134,9 +127,6 @@ RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane}, state4[] = { { '@', 9, " at", NULL }, { 'a', 9, " at", NULL }, { RETTOKEN, -1, "", NULL }, -#ifdef SYSV - { CRTOKEN, -1, "", NULL }, -#endif { HELPTOKEN, 12, " @a", NULL }}, state5[] = { { NUMTOKEN, 7, "%c", delayb }, { HELPTOKEN, 12, " [0-9]", NULL }}, @@ -151,14 +141,8 @@ RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane}, { 'a', 4, " 270", rel_dir }, { 'q', 4, " 315", rel_dir }, { RETTOKEN, -1, "", NULL }, -#ifdef SYSV - { CRTOKEN, -1, "", NULL }, -#endif { HELPTOKEN, 12, " @a",NULL }}, state7[] = { { RETTOKEN, -1, "", NULL }, -#ifdef SYSV - { CRTOKEN, -1, "", NULL }, -#endif { HELPTOKEN, 12, " ", NULL }}, state8[] = { { NUMTOKEN, 4, "%c", benum }, { HELPTOKEN, 12, " [0-9]", NULL }}, @@ -190,7 +174,8 @@ int level; int tval; int dest_type, dest_no, dir; -pop() +int +pop(void) { if (level == 0) return (-1); @@ -198,13 +183,14 @@ pop() ioclrtoeol(T_POS); - strcpy(T_STR, ""); + (void)strcpy(T_STR, ""); T_RULE = -1; T_CH = -1; return (0); } -rezero() +void +rezero(void) { iomove(0); @@ -213,13 +199,15 @@ rezero() T_RULE = -1; T_CH = -1; T_POS = 0; - strcpy(T_STR, ""); + (void)strcpy(T_STR, ""); } -push(ruleno, ch) +void +push(int ruleno, int ch) { int newstate, newpos; + assert(level < (MAXDEPTH - 1)); (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval); T_RULE = ruleno; T_CH = ch; @@ -234,23 +222,24 @@ push(ruleno, ch) T_STATE = newstate; T_POS = newpos; T_RULE = -1; - strcpy(T_STR, ""); + (void)strcpy(T_STR, ""); } -getcommand() +int +getcommand(void) { int c, i, done; - char *s, *(*func)(); + const char *s, *(*func)(int); PLANE *pp; rezero(); do { c = gettoken(); - if (c == tty_new.sg_erase) { + if (c == tty_new.c_cc[VERASE]) { if (pop() < 0) noise(); - } else if (c == tty_new.sg_kill) { + } else if (c == tty_new.c_cc[VKILL]) { while (pop() >= 0) ; } else { @@ -277,7 +266,8 @@ getcommand() func = st[stack[i].state].rule[stack[i].rule].func; if (func != NULL) if ((s = (*func)(stack[i].ch)) != NULL) { - ioerror(stack[i].pos, strlen(stack[i].str), s); + ioerror(stack[i].pos, + (int)strlen(stack[i].str), s); return (-1); } } @@ -295,13 +285,15 @@ getcommand() return (0); } -noise() +void +noise(void) { - putchar('\07'); - fflush(stdout); + (void)putchar('\07'); + (void)fflush(stdout); } -gettoken() +int +gettoken(void) { while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN) { @@ -311,7 +303,7 @@ gettoken() struct itimerval itv; itv.it_value.tv_sec = 0; itv.it_value.tv_usec = 0; - setitimer(ITIMER_REAL, &itv, NULL); + (void)setitimer(ITIMER_REAL, &itv, NULL); #endif #ifdef SYSV int aval; @@ -319,9 +311,8 @@ gettoken() #endif if (fork() == 0) /* child */ { - char *shell, *base, *getenv(), *strrchr(); + char *shell, *base; - setuid(getuid()); /* turn off setuid bit */ done_screen(); /* run user's favorite shell */ @@ -332,29 +323,29 @@ gettoken() base = shell; else base++; - execl(shell, base, 0); + (void)execl(shell, base, (char *) 0); } else - execl(_PATH_BSHELL, "sh", 0); + (void)execl(_PATH_BSHELL, "sh", + (char *) 0); exit(0); /* oops */ } - wait(0); + (void)wait(0); + (void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new); #ifdef BSD - ioctl(fileno(stdin), TIOCSETP, &tty_new); itv.it_value.tv_sec = 0; itv.it_value.tv_usec = 1; itv.it_interval.tv_sec = sp->update_secs; itv.it_interval.tv_usec = 0; - setitimer(ITIMER_REAL, &itv, NULL); + (void)setitimer(ITIMER_REAL, &itv, NULL); #endif #ifdef SYSV - ioctl(fileno(stdin), TCSETAW, &tty_new); alarm(aval); #endif } - redraw(); + (void)redraw(); } if (isdigit(tval)) @@ -365,29 +356,31 @@ gettoken() return (tval); } -char * -setplane(c) +const char * +setplane(int c) { PLANE *pp; pp = findplane(number(c)); if (pp == NULL) return ("Unknown Plane"); - bcopy(pp, &p, sizeof (p)); + (void)memcpy(&p, pp, sizeof (p)); p.delayd = 0; return (NULL); } -char * -turn(c) +/* ARGSUSED */ +const char * +turn(int c __attribute__((__unused__))) { if (p.altitude == 0) return ("Planes at airports may not change direction"); return (NULL); } -char * -circle(c) +/* ARGSUSED */ +const char * +circle(int c __attribute__((__unused__))) { if (p.altitude == 0) return ("Planes cannot circle on the ground"); @@ -395,8 +388,9 @@ circle(c) return (NULL); } -char * -left(c) +/* ARGSUSED */ +const char * +left(int c __attribute__((__unused__))) { dir = D_LEFT; p.new_dir = p.dir - 1; @@ -405,18 +399,20 @@ left(c) return (NULL); } -char * -right(c) +/* ARGSUSED */ +const char * +right(int c __attribute__((__unused__))) { dir = D_RIGHT; p.new_dir = p.dir + 1; - if (p.new_dir > MAXDIR) + if (p.new_dir >= MAXDIR) p.new_dir -= MAXDIR; return (NULL); } -char * -Left(c) +/* ARGSUSED */ +const char * +Left(int c __attribute__((__unused__))) { p.new_dir = p.dir - 2; if (p.new_dir < 0) @@ -424,17 +420,18 @@ Left(c) return (NULL); } -char * -Right(c) +/* ARGSUSED */ +const char * +Right(int c __attribute__((__unused__))) { p.new_dir = p.dir + 2; - if (p.new_dir > MAXDIR) + if (p.new_dir >= MAXDIR) p.new_dir -= MAXDIR; return (NULL); } -char * -delayb(c) +const char * +delayb(int c) { int xdiff, ydiff; @@ -442,9 +439,9 @@ delayb(c) if (c >= sp->num_beacons) return ("Unknown beacon"); - xdiff = sp->beacon[c].x - p.xpos; + xdiff = sp->beacon[(int)c].x - p.xpos; xdiff = SGN(xdiff); - ydiff = sp->beacon[c].y - p.ypos; + ydiff = sp->beacon[(int)c].y - p.ypos; ydiff = SGN(ydiff); if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy) return ("Beacon is not in flight path"); @@ -454,20 +451,19 @@ delayb(c) if (dest_type != T_NODEST) { switch (dest_type) { case T_BEACON: - xdiff = sp->beacon[dest_no].x - sp->beacon[c].x; - ydiff = sp->beacon[dest_no].y - sp->beacon[c].y; + xdiff = sp->beacon[dest_no].x - sp->beacon[(int)c].x; + ydiff = sp->beacon[dest_no].y - sp->beacon[(int)c].y; break; case T_EXIT: - xdiff = sp->exit[dest_no].x - sp->beacon[c].x; - ydiff = sp->exit[dest_no].y - sp->beacon[c].y; + xdiff = sp->exit[dest_no].x - sp->beacon[(int)c].x; + ydiff = sp->exit[dest_no].y - sp->beacon[(int)c].y; break; case T_AIRPORT: - xdiff = sp->airport[dest_no].x - sp->beacon[c].x; - ydiff = sp->airport[dest_no].y - sp->beacon[c].y; + xdiff = sp->airport[dest_no].x - sp->beacon[(int)c].x; + ydiff = sp->airport[dest_no].y - sp->beacon[(int)c].y; break; default: return ("Bad case in delayb! Get help!"); - break; } if (xdiff == 0 && ydiff == 0) return ("Would already be there"); @@ -478,67 +474,83 @@ delayb(c) return (NULL); } -char * -beacon(c) +/* ARGSUSED */ +const char * +beacon(int c __attribute__((__unused__))) { dest_type = T_BEACON; return (NULL); } -char * -ex_it(c) +/* ARGSUSED */ +const char * +ex_it(int c __attribute__((__unused__))) { dest_type = T_EXIT; return (NULL); } -char * -airport(c) +/* ARGSUSED */ +const char * +airport(int c __attribute__((__unused__))) { dest_type = T_AIRPORT; return (NULL); } -char * -climb(c) +/* ARGSUSED */ +const char * +climb(int c __attribute__((__unused__))) { dir = D_UP; return (NULL); } -char * -descend(c) +/* ARGSUSED */ +const char * +descend(int c __attribute__((__unused__))) { dir = D_DOWN; return (NULL); } -char * -setalt(c) +const char * +setalt(int c) { - if ((p.altitude == c - '0') && (p.new_altitude == p.altitude)) + int newalt = c - '0'; + if ((p.altitude == newalt) && (p.new_altitude == p.altitude)) return ("Already at that altitude"); - p.new_altitude = c - '0'; + if (p.new_altitude == newalt) { + return ("Already going to that altitude"); + } + p.new_altitude = newalt; return (NULL); } -char * -setrelalt(c) +const char * +setrelalt(int c) { + int newalt; + if (c == 0) return ("altitude not changed"); switch (dir) { case D_UP: - p.new_altitude = p.altitude + c - '0'; + newalt = p.altitude + c - '0'; break; case D_DOWN: - p.new_altitude = p.altitude - (c - '0'); + newalt = p.altitude - (c - '0'); break; default: return ("Unknown case in setrelalt! Get help!"); - break; } + + if (p.new_altitude == newalt) + return ("Already going to that altitude"); + + p.new_altitude = newalt; + if (p.new_altitude < 0) return ("Altitude would be too low"); else if (p.new_altitude > 9) @@ -546,8 +558,8 @@ setrelalt(c) return (NULL); } -char * -benum(c) +const char * +benum(int c) { dest_no = c -= '0'; @@ -555,37 +567,36 @@ benum(c) case T_BEACON: if (c >= sp->num_beacons) return ("Unknown beacon"); - p.new_dir = DIR_FROM_DXDY(sp->beacon[c].x - p.xpos, - sp->beacon[c].y - p.ypos); + p.new_dir = DIR_FROM_DXDY(sp->beacon[(int)c].x - p.xpos, + sp->beacon[(int)c].y - p.ypos); break; case T_EXIT: if (c >= sp->num_exits) return ("Unknown exit"); - p.new_dir = DIR_FROM_DXDY(sp->exit[c].x - p.xpos, - sp->exit[c].y - p.ypos); + p.new_dir = DIR_FROM_DXDY(sp->exit[(int)c].x - p.xpos, + sp->exit[(int)c].y - p.ypos); break; case T_AIRPORT: if (c >= sp->num_airports) return ("Unknown airport"); - p.new_dir = DIR_FROM_DXDY(sp->airport[c].x - p.xpos, - sp->airport[c].y - p.ypos); + p.new_dir = DIR_FROM_DXDY(sp->airport[(int)c].x - p.xpos, + sp->airport[(int)c].y - p.ypos); break; default: return ("Unknown case in benum! Get help!"); - break; } return (NULL); } -char * -to_dir(c) +const char * +to_dir(int c) { p.new_dir = dir_no(c); return (NULL); } -char * -rel_dir(c) +const char * +rel_dir(int c) { int angle; @@ -603,13 +614,13 @@ rel_dir(c) break; default: return ("Bizarre direction in rel_dir! Get help!"); - break; } return (NULL); } -char * -mark(c) +/* ARGSUSED */ +const char * +mark(int c __attribute__((__unused__))) { if (p.altitude == 0) return ("Cannot mark planes on the ground"); @@ -619,8 +630,9 @@ mark(c) return (NULL); } -char * -unmark(c) +/* ARGSUSED */ +const char * +unmark(int c __attribute__((__unused__))) { if (p.altitude == 0) return ("Cannot unmark planes on the ground"); @@ -630,8 +642,9 @@ unmark(c) return (NULL); } -char * -ignore(c) +/* ARGSUSED */ +const char * +ignore(int c __attribute__((__unused__))) { if (p.altitude == 0) return ("Cannot ignore planes on the ground"); @@ -641,23 +654,24 @@ ignore(c) return (NULL); } -dir_no(ch) - char ch; +int +dir_no(int ch) { - int dir; + int dirno; + dirno = -1; switch (ch) { - case 'w': dir = 0; break; - case 'e': dir = 1; break; - case 'd': dir = 2; break; - case 'c': dir = 3; break; - case 'x': dir = 4; break; - case 'z': dir = 5; break; - case 'a': dir = 6; break; - case 'q': dir = 7; break; + case 'w': dirno = 0; break; + case 'e': dirno = 1; break; + case 'd': dirno = 2; break; + case 'c': dirno = 3; break; + case 'x': dirno = 4; break; + case 'z': dirno = 5; break; + case 'a': dirno = 6; break; + case 'q': dirno = 7; break; default: - fprintf(stderr, "bad character in dir_no\n"); + (void)fprintf(stderr, "bad character in dir_no\n"); break; } - return (dir); + return (dirno); }