-/* $NetBSD: input.c,v 1.6 1997/10/10 02:07:18 lukem Exp $ */
+/* $NetBSD: input.c,v 1.26 2014/03/22 22:45:05 dholland Exp $ */
/*-
* Copyright (c) 1990, 1993
* 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.
*
#if 0
static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: input.c,v 1.6 1997/10/10 02:07:18 lukem Exp $");
+__RCSID("$NetBSD: input.c,v 1.26 2014/03/22 22:45:05 dholland Exp $");
#endif
-#endif not lint
+#endif /* not lint */
#include "include.h"
#include "pathnames.h"
+static void rezero(void);
+static void noise(void);
+static int gettoken(void);
+static const char *setplane(int);
+static const char *turn(int);
+static const char *circle(int);
+static const char *left(int);
+static const char *right(int);
+static const char *Left(int);
+static const char *Right(int);
+static const char *delayb(int);
+static const char *beacon(int);
+static const char *ex_it(int);
+static const char *airport(int);
+static const char *climb(int);
+static const char *descend(int);
+static const char *setalt(int);
+static const char *setrelalt(int);
+static const char *benum(int);
+static const char *to_dir(int);
+static const char *rel_dir(int);
+static const char *mark(int);
+static const char *unmark(int);
+static const char *ignore(int);
+
+
+
#define MAXRULES 6
#define MAXDEPTH 15
#define RETTOKEN '\n'
-#ifdef SYSV
-#define CRTOKEN '\r'
-#endif
#define REDRAWTOKEN '\014' /* CTRL(L) */
#define SHELLTOKEN '!'
#define HELPTOKEN '?'
typedef struct {
int token;
int to_state;
- char *str;
- char *(*func) __P((char));
+ const char *str;
+ const char *(*func)(int);
} RULE;
typedef struct {
#define NUMSTATES NUMELS(st)
-
+static
RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
{ RETTOKEN, -1, "", NULL },
-#ifdef SYSV
- { CRTOKEN, -1, "", NULL },
-#endif
{ HELPTOKEN, 12, " [a-z]<ret>", NULL }},
state1[] = { { 't', 2, " turn", turn },
{ 'a', 3, " altitude:", NULL },
state4[] = { { '@', 9, " at", NULL },
{ 'a', 9, " at", NULL },
{ RETTOKEN, -1, "", NULL },
-#ifdef SYSV
- { CRTOKEN, -1, "", NULL },
-#endif
{ HELPTOKEN, 12, " @a<ret>", NULL }},
state5[] = { { NUMTOKEN, 7, "%c", delayb },
{ HELPTOKEN, 12, " [0-9]", NULL }},
{ 'a', 4, " 270", rel_dir },
{ 'q', 4, " 315", rel_dir },
{ RETTOKEN, -1, "", NULL },
-#ifdef SYSV
- { CRTOKEN, -1, "", NULL },
-#endif
{ HELPTOKEN, 12, " @a<dir><ret>",NULL }},
state7[] = { { RETTOKEN, -1, "", NULL },
-#ifdef SYSV
- { CRTOKEN, -1, "", NULL },
-#endif
{ HELPTOKEN, 12, " <ret>", NULL }},
state8[] = { { NUMTOKEN, 4, "%c", benum },
{ HELPTOKEN, 12, " [0-9]", NULL }},
#define DEF_STATE(s) { NUMELS(s), (s) }
-STATE st[] = {
+static STATE st[] = {
DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
DEF_STATE(state12)
};
-PLANE p;
-STACK stack[MAXDEPTH];
-int level;
-int tval;
-int dest_type, dest_no, dir;
+static PLANE p;
+static STACK stack[MAXDEPTH];
+static int level;
+static int tval;
+static int dest_type, dest_no, dir;
-int
-pop()
+static int
+pop(void)
{
if (level == 0)
return (-1);
ioclrtoeol(T_POS);
- strcpy(T_STR, "");
+ (void)strcpy(T_STR, "");
T_RULE = -1;
T_CH = -1;
return (0);
}
-void
-rezero()
+static void
+rezero(void)
{
iomove(0);
T_RULE = -1;
T_CH = -1;
T_POS = 0;
- strcpy(T_STR, "");
+ (void)strcpy(T_STR, "");
}
-void
-push(ruleno, ch)
- int ruleno, ch;
+static void
+push(int ruleno, int ch)
{
int newstate, newpos;
- (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
+ assert(level < (MAXDEPTH - 1));
+ (void)snprintf(T_STR, sizeof(T_STR),
+ st[T_STATE].rule[ruleno].str, tval);
T_RULE = ruleno;
T_CH = ch;
newstate = st[T_STATE].rule[ruleno].to_state;
T_STATE = newstate;
T_POS = newpos;
T_RULE = -1;
- strcpy(T_STR, "");
+ (void)strcpy(T_STR, "");
}
int
-getcommand()
+getcommand(void)
{
int c, i, done;
- char *s, *(*func) __P((char));
+ const char *s, *(*func)(int);
PLANE *pp;
rezero();
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);
}
}
return (0);
}
-void
-noise()
+static void
+noise(void)
{
- putchar('\07');
- fflush(stdout);
+ (void)putchar('\07');
+ (void)fflush(stdout);
}
-int
-gettoken()
+static int
+gettoken(void)
{
while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
{
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;
{
char *shell, *base;
- setuid(getuid()); /* turn off setuid bit */
done_screen();
/* run user's favorite shell */
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);
- tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
+ (void)wait(0);
+ (void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
#ifdef BSD
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
alarm(aval);
#endif
}
- redraw();
+ (void)redraw();
}
if (isdigit(tval))
return (tval);
}
-char *
-setplane(c)
- char c;
+static const char *
+setplane(int c)
{
PLANE *pp;
pp = findplane(number(c));
if (pp == NULL)
return ("Unknown Plane");
- memcpy(&p, pp, sizeof (p));
- p.delayd = 0;
+ (void)memcpy(&p, pp, sizeof (p));
+ p.delayd = false;
return (NULL);
}
-char *
-turn(c)
- char c;
+/* ARGSUSED */
+static const char *
+turn(int c __unused)
{
if (p.altitude == 0)
return ("Planes at airports may not change direction");
return (NULL);
}
-char *
-circle(c)
- char c;
+/* ARGSUSED */
+static const char *
+circle(int c __unused)
{
if (p.altitude == 0)
return ("Planes cannot circle on the ground");
return (NULL);
}
-char *
-left(c)
- char c;
+/* ARGSUSED */
+static const char *
+left(int c __unused)
{
dir = D_LEFT;
p.new_dir = p.dir - 1;
return (NULL);
}
-char *
-right(c)
- char c;
+/* ARGSUSED */
+static const char *
+right(int c __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)
- char c;
+/* ARGSUSED */
+static const char *
+Left(int c __unused)
{
p.new_dir = p.dir - 2;
if (p.new_dir < 0)
return (NULL);
}
-char *
-Right(c)
- char c;
+/* ARGSUSED */
+static const char *
+Right(int c __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)
- char c;
+static const char *
+delayb(int ch)
{
int xdiff, ydiff;
+ unsigned bn;
- c -= '0';
+ bn = ch -= '0';
- if (c >= sp->num_beacons)
+ if (bn >= sp->num_beacons)
return ("Unknown beacon");
- xdiff = sp->beacon[c].x - p.xpos;
+ xdiff = sp->beacon[bn].x - p.xpos;
xdiff = SGN(xdiff);
- ydiff = sp->beacon[c].y - p.ypos;
+ ydiff = sp->beacon[bn].y - p.ypos;
ydiff = SGN(ydiff);
if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
return ("Beacon is not in flight path");
- p.delayd = 1;
- p.delayd_no = c;
+ p.delayd = true;
+ p.delayd_no = bn;
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[bn].x;
+ ydiff = sp->beacon[dest_no].y - sp->beacon[bn].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[bn].x;
+ ydiff = sp->exit[dest_no].y - sp->beacon[bn].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[bn].x;
+ ydiff = sp->airport[dest_no].y - sp->beacon[bn].y;
break;
default:
return ("Bad case in delayb! Get help!");
- break;
}
if (xdiff == 0 && ydiff == 0)
return ("Would already be there");
return (NULL);
}
-char *
-beacon(c)
- char c;
+/* ARGSUSED */
+static const char *
+beacon(int c __unused)
{
dest_type = T_BEACON;
return (NULL);
}
-char *
-ex_it(c)
- char c;
+/* ARGSUSED */
+static const char *
+ex_it(int c __unused)
{
dest_type = T_EXIT;
return (NULL);
}
-char *
-airport(c)
- char c;
+/* ARGSUSED */
+static const char *
+airport(int c __unused)
{
dest_type = T_AIRPORT;
return (NULL);
}
-char *
-climb(c)
- char c;
+/* ARGSUSED */
+static const char *
+climb(int c __unused)
{
dir = D_UP;
return (NULL);
}
-char *
-descend(c)
- char c;
+/* ARGSUSED */
+static const char *
+descend(int c __unused)
{
dir = D_DOWN;
return (NULL);
}
-char *
-setalt(c)
- char c;
+static 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)
- char c;
+static 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)
return (NULL);
}
-char *
-benum(c)
- char c;
+static const char *
+benum(int ch)
{
- dest_no = c -= '0';
+ unsigned n;
+
+ n = ch - '0';
+ dest_no = n;
switch (dest_type) {
case T_BEACON:
- if (c >= sp->num_beacons)
+ if (n >= 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[n].x - p.xpos,
+ sp->beacon[n].y - p.ypos);
break;
case T_EXIT:
- if (c >= sp->num_exits)
+ if (n >= 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[n].x - p.xpos,
+ sp->exit[n].y - p.ypos);
break;
case T_AIRPORT:
- if (c >= sp->num_airports)
+ if (n >= 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[n].x - p.xpos,
+ sp->airport[n].y - p.ypos);
break;
default:
return ("Unknown case in benum! Get help!");
- break;
}
return (NULL);
}
-char *
-to_dir(c)
- char c;
+static const char *
+to_dir(int c)
{
p.new_dir = dir_no(c);
return (NULL);
}
-char *
-rel_dir(c)
- char c;
+static const char *
+rel_dir(int c)
{
int angle;
break;
default:
return ("Bizarre direction in rel_dir! Get help!");
- break;
}
return (NULL);
}
-char *
-mark(c)
- char c;
+/* ARGSUSED */
+static const char *
+mark(int c __unused)
{
if (p.altitude == 0)
return ("Cannot mark planes on the ground");
return (NULL);
}
-char *
-unmark(c)
- char c;
+/* ARGSUSED */
+static const char *
+unmark(int c __unused)
{
if (p.altitude == 0)
return ("Cannot unmark planes on the ground");
return (NULL);
}
-char *
-ignore(c)
- char c;
+/* ARGSUSED */
+static const char *
+ignore(int c __unused)
{
if (p.altitude == 0)
return ("Cannot ignore planes on the ground");
}
int
-dir_no(ch)
- char ch;
+dir_no(int ch)
{
- int dir;
+ int dirno;
- dir = -1;
+ 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);
}