-/* $NetBSD: input.c,v 1.18 2005/07/01 00:48:34 jmc Exp $ */
+/* $NetBSD: input.c,v 1.29 2015/06/19 06:02:31 dholland Exp $ */
/*-
* Copyright (c) 1990, 1993
#if 0
static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: input.c,v 1.18 2005/07/01 00:48:34 jmc Exp $");
+__RCSID("$NetBSD: input.c,v 1.29 2015/06/19 06:02:31 dholland Exp $");
#endif
#endif /* not lint */
-#include "include.h"
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <termios.h>
+#include <ctype.h>
+#include <assert.h>
+#include <math.h>
+
#include "pathnames.h"
+#include "def.h"
+#include "struct.h"
+#include "extern.h"
+#include "tunable.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 NUMSTATES NUMELS(st)
-
+static
RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
{ RETTOKEN, -1, "", NULL },
{ HELPTOKEN, 12, " [a-z]<ret>", 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 dir;
+static enum places dest_type;
+static unsigned dest_no;
-int
+static int
pop(void)
{
if (level == 0)
ioclrtoeol(T_POS);
- strcpy(T_STR, "");
+ (void)strcpy(T_STR, "");
T_RULE = -1;
T_CH = -1;
return (0);
}
-void
+static void
rezero(void)
{
iomove(0);
T_RULE = -1;
T_CH = -1;
T_POS = 0;
- strcpy(T_STR, "");
+ (void)strcpy(T_STR, "");
}
-void
+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
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
+static void
noise(void)
{
- putchar('\07');
- fflush(stdout);
+ (void)putchar('\07');
+ (void)fflush(stdout);
}
-int
+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;
base = shell;
else
base++;
- execl(shell, base, (char *) 0);
+ (void)execl(shell, base, (char *) 0);
}
else
- execl(_PATH_BSHELL, "sh", (char *) 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);
}
-const char *
+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);
}
-const char *
-turn(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+turn(int c __unused)
{
if (p.altitude == 0)
return ("Planes at airports may not change direction");
return (NULL);
}
-const char *
-circle(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+circle(int c __unused)
{
if (p.altitude == 0)
return ("Planes cannot circle on the ground");
return (NULL);
}
-const char *
-left(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+left(int c __unused)
{
dir = D_LEFT;
p.new_dir = p.dir - 1;
return (NULL);
}
-const char *
-right(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+right(int c __unused)
{
dir = D_RIGHT;
p.new_dir = p.dir + 1;
return (NULL);
}
-const char *
-Left(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+Left(int c __unused)
{
p.new_dir = p.dir - 2;
if (p.new_dir < 0)
return (NULL);
}
-const char *
-Right(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+Right(int c __unused)
{
p.new_dir = p.dir + 2;
if (p.new_dir >= MAXDIR)
return (NULL);
}
-const char *
-delayb(int 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[(int)c].x - p.xpos;
+ xdiff = sp->beacon[bn].x - p.xpos;
xdiff = SGN(xdiff);
- ydiff = sp->beacon[(int)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[(int)c].x;
- ydiff = sp->beacon[dest_no].y - sp->beacon[(int)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[(int)c].x;
- ydiff = sp->exit[dest_no].y - sp->beacon[(int)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[(int)c].x;
- ydiff = sp->airport[dest_no].y - sp->beacon[(int)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);
}
-const char *
-beacon(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+beacon(int c __unused)
{
dest_type = T_BEACON;
return (NULL);
}
-const char *
-ex_it(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+ex_it(int c __unused)
{
dest_type = T_EXIT;
return (NULL);
}
-const char *
-airport(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+airport(int c __unused)
{
dest_type = T_AIRPORT;
return (NULL);
}
-const char *
-climb(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+climb(int c __unused)
{
dir = D_UP;
return (NULL);
}
-const char *
-descend(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+descend(int c __unused)
{
dir = D_DOWN;
return (NULL);
}
-const char *
+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);
}
-const char *
+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);
}
-const char *
-benum(int 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[(int)c].x - p.xpos,
- sp->beacon[(int)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[(int)c].x - p.xpos,
- sp->exit[(int)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[(int)c].x - p.xpos,
- sp->airport[(int)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);
}
-const char *
+static const char *
to_dir(int c)
{
p.new_dir = dir_no(c);
return (NULL);
}
-const char *
+static const char *
rel_dir(int c)
{
int angle;
break;
default:
return ("Bizarre direction in rel_dir! Get help!");
- break;
}
return (NULL);
}
-const char *
-mark(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+mark(int c __unused)
{
if (p.altitude == 0)
return ("Cannot mark planes on the ground");
return (NULL);
}
-const char *
-unmark(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+unmark(int c __unused)
{
if (p.altitude == 0)
return ("Cannot unmark planes on the ground");
return (NULL);
}
-const char *
-ignore(int c __attribute__((__unused__)))
+/* ARGSUSED */
+static const char *
+ignore(int c __unused)
{
if (p.altitude == 0)
return ("Cannot ignore planes on the ground");
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 (dirno);