]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - atc/input.c
use more suitable types
[bsdgames-darwin.git] / atc / input.c
index a311d5026dd08785d33482b4691356a379b1e793..c0d1be32c386c6bd044a5b01b35b16c36559ce2e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: input.c,v 1.9 1998/07/28 03:00:09 briggs 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.9 1998/07/28 03:00:09 briggs 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      '?'
@@ -73,8 +93,8 @@ __RCSID("$NetBSD: input.c,v 1.9 1998/07/28 03:00:09 briggs Exp $");
 typedef struct {
        int     token;
        int     to_state;
-       char    *str;
-       char    *(*func) __P((char));
+       const char      *str;
+       const char      *(*func)(int);
 } RULE;
 
 typedef struct {
@@ -100,12 +120,9 @@ 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    },      
@@ -137,9 +154,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<ret>",     NULL    }},
        state5[] = {    { NUMTOKEN,     7,      "%c",           delayb  },
                        { HELPTOKEN,    12,     " [0-9]",       NULL    }},
@@ -154,14 +168,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<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    }},
@@ -179,7 +187,7 @@ RULE        state0[] = {    { ALPHATOKEN,   1,      "%c:",          setplane},
 
 #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),
@@ -187,14 +195,14 @@ STATE     st[] = {
        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);
@@ -202,14 +210,14 @@ pop()
 
        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);
 
@@ -218,16 +226,17 @@ rezero()
        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;
@@ -241,14 +250,14 @@ push(ruleno, ch)
        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();
@@ -285,7 +294,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);
                        }
        }
@@ -303,15 +313,15 @@ getcommand()
        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)
        {
@@ -321,7 +331,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;
@@ -331,7 +341,6 @@ gettoken()
                        {
                                char *shell, *base;
 
-                               setuid(getuid()); /* turn off setuid bit */
                                done_screen();
 
                                                 /* run user's favorite shell */
@@ -342,28 +351,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);
-                       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))
@@ -374,32 +384,31 @@ gettoken()
                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");
@@ -407,9 +416,9 @@ circle(c)
        return (NULL);
 }
 
-char   *
-left(c)
-       char c;
+/* ARGSUSED */
+static const char *
+left(int c __unused)
 {
        dir = D_LEFT;
        p.new_dir = p.dir - 1;
@@ -418,9 +427,9 @@ left(c)
        return (NULL);
 }
 
-char   *
-right(c)
-       char c;
+/* ARGSUSED */
+static const char *
+right(int c __unused)
 {
        dir = D_RIGHT;
        p.new_dir = p.dir + 1;
@@ -429,9 +438,9 @@ right(c)
        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)
@@ -439,9 +448,9 @@ Left(c)
        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)
@@ -449,42 +458,41 @@ Right(c)
        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[(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");
@@ -495,74 +503,83 @@ delayb(c)
        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)
@@ -570,49 +587,48 @@ setrelalt(c)
        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[(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);
 }
 
-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;
 
@@ -630,14 +646,13 @@ rel_dir(c)
                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");
@@ -647,9 +662,9 @@ mark(c)
        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");
@@ -659,9 +674,9 @@ unmark(c)
        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");
@@ -672,24 +687,23 @@ ignore(c)
 }
 
 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);
 }