]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - gomoku/main.c
sprinkle static
[bsdgames-darwin.git] / gomoku / main.c
index 5674fb99a623c06be04fddafdebe43c9b3b2be8c..74fd9d768eea4803e9a532b161bd16f4c7e09c01 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.6 1999/09/08 21:17:49 jsm Exp $     */
+/*     $NetBSD: main.c,v 1.20 2009/08/12 06:19:17 dholland Exp $       */
 
 /*
  * Copyright (c) 1994
  * 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.
  *
 
 #include <sys/cdefs.h>
 #ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1994\n\
      The Regents of the University of California.  All rights reserved.\n");
+__COPYRIGHT("@(#) Copyright (c) 1994\
The Regents of the University of California.  All rights reserved.");
 #endif /* not lint */
 
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)main.c     8.4 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.6 1999/09/08 21:17:49 jsm Exp $");
+__RCSID("$NetBSD: main.c,v 1.20 2009/08/12 06:19:17 dholland Exp $");
 #endif
 #endif /* not lint */
 
 #include <curses.h>
 #include <err.h>
 #include <signal.h>
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -66,13 +63,12 @@ __RCSID("$NetBSD: main.c,v 1.6 1999/09/08 21:17:49 jsm Exp $");
 
 int    interactive = 1;        /* true if interactive */
 int    debug;                  /* true if debugging */
-int    test;                   /* both moves come from 1: input, 2: computer */
-char   *prog;                  /* name of program */
-FILE   *debugfp;               /* file for debug output */
-FILE   *inputfp;               /* file for debug input */
+static int test;               /* both moves come from 1: input, 2: computer */
+static char *prog;             /* name of program */
+static FILE *debugfp;          /* file for debug output */
+static FILE *inputfp;          /* file for debug input */
 
 const char     pdir[4]         = "-\\|/";
-char   fmtbuf[128];
 
 struct spotstr board[BAREA];           /* info for board */
 struct combostr frames[FAREA];         /* storage for all frames */
@@ -83,12 +79,13 @@ int movelog[BSZ * BSZ];             /* log of all the moves */
 int    movenum;                        /* current move number */
 const char     *plyr[2];                       /* who's who */
 
-int    main __P((int, char *[]));
+static int readinput(FILE *);
+static void misclog(const char *, ...) __printflike(1, 2);
+static void quit(void) __dead;
+static void quitsig(int) __dead;
 
 int
-main(argc, argv)
-       int argc;
-       char **argv;
+main(int argc, char **argv)
 {
        char buf[128];
        int color, curmove, i, ch;
@@ -98,6 +95,9 @@ main(argc, argv)
                "%3d        %-6s"
        };
 
+       /* Revoke setgid privileges */
+       setgid(getgid());
+
        color = curmove = 0;
 
        prog = strrchr(argv[0], '/');
@@ -156,7 +156,7 @@ again:
                if (inputfp == NULL && test == 0) {
                        for (;;) {
                                ask("black or white? ");
-                               getline(buf, sizeof(buf));
+                               get_line(buf, sizeof(buf));
                                if (buf[0] == 'b' || buf[0] == 'B') {
                                        color = BLACK;
                                        break;
@@ -173,16 +173,14 @@ again:
                }
        } else {
                setbuf(stdout, 0);
-               getline(buf, sizeof(buf));
+               get_line(buf, sizeof(buf));
                if (strcmp(buf, "black") == 0)
                        color = BLACK;
                else if (strcmp(buf, "white") == 0)
                        color = WHITE;
                else {
-                       sprintf(fmtbuf,
-                           "Huh?  Expected `black' or `white', got `%s'\n",
+                       panic("Huh?  Expected `black' or `white', got `%s'\n",
                            buf);
-                       panic(fmtbuf);
                }
        }
 
@@ -245,7 +243,7 @@ again:
                getinput:
                        if (interactive)
                                ask("move? ");
-                       if (!getline(buf, sizeof(buf))) {
+                       if (!get_line(buf, sizeof(buf))) {
                                curmove = RESIGN;
                                break;
                        }
@@ -257,9 +255,9 @@ again:
                                        FILE *fp;
 
                                        ask("save file name? ");
-                                       (void)getline(buf, sizeof(buf));
+                                       (void)get_line(buf, sizeof(buf));
                                        if ((fp = fopen(buf, "w")) == NULL) {
-                                               log("cannot create save file");
+                                               misclog("cannot create save file");
                                                goto getinput;
                                        }
                                        for (i = 0; i < movenum - 1; i++)
@@ -270,7 +268,7 @@ again:
                                }
                                if (curmove != RESIGN &&
                                    board[curmove].s_occ != EMPTY) {
-                                       log("Illegal move");
+                                       misclog("Illegal move");
                                        goto getinput;
                                }
                        }
@@ -281,8 +279,7 @@ again:
                        break;
                }
                if (interactive) {
-                       sprintf(fmtbuf, fmt[color], movenum, stoc(curmove));
-                       log(fmtbuf);
+                       misclog(fmt[color], movenum, stoc(curmove));
                }
                if ((i = makemove(color, curmove)) != MOVEOK)
                        break;
@@ -310,16 +307,16 @@ again:
                if (i != RESIGN) {
                replay:
                        ask("replay? ");
-                       if (getline(buf, sizeof(buf)) &&
+                       if (get_line(buf, sizeof(buf)) &&
                            (buf[0] == 'y' || buf[0] == 'Y'))
                                goto again;
                        if (strcmp(buf, "save") == 0) {
                                FILE *fp;
 
                                ask("save file name? ");
-                               (void)getline(buf, sizeof(buf));
+                               (void)get_line(buf, sizeof(buf));
                                if ((fp = fopen(buf, "w")) == NULL) {
-                                       log("cannot create save file");
+                                       misclog("cannot create save file");
                                        goto replay;
                                }
                                for (i = 0; i < movenum - 1; i++)
@@ -335,18 +332,18 @@ again:
        return(0);
 }
 
-int
-readinput(fp)
-       FILE *fp;
+static int
+readinput(FILE *fp)
 {
-       char *cp;
        int c;
+       char buf[128];
+       size_t pos;
 
-       cp = fmtbuf;
-       while ((c = getc(fp)) != EOF && c != '\n')
-               *cp++ = c;
-       *cp = '\0';
-       return (ctos(fmtbuf));
+       pos = 0;
+       while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
+               buf[pos++] = c;
+       buf[pos] = '\0';
+       return ctos(buf);
 }
 
 #ifdef DEBUG
@@ -354,31 +351,31 @@ readinput(fp)
  * Handle strange situations.
  */
 void
-whatsup(signum)
-       int signum;
+whatsup(int signum)
 {
-       int i, pnum, n, s1, s2, d1, d2;
+       int i, n, s1, s2, d1, d2;
        struct spotstr *sp;
        FILE *fp;
        char *str;
        struct elist *ep;
        struct combostr *cbp;
+       char input[128];
+       char tmp[128];
 
        if (!interactive)
                quit();
 top:
        ask("cmd? ");
-       if (!getline(fmtbuf, sizeof(fmtbuf)))
+       if (!get_line(input, sizeof(input)))
                quit();
-       switch (*fmtbuf) {
+       switch (*input) {
        case '\0':
                goto top;
        case 'q':               /* conservative quit */
                quit();
        case 'd':               /* set debug level */
-               debug = fmtbuf[1] - '0';
-               sprintf(fmtbuf, "Debug set to %d", debug);
-               dlog(fmtbuf);
+               debug = input[1] - '0';
+               debuglog("Debug set to %d", debug);
                sleep(1);
        case 'c':
                break;
@@ -390,10 +387,9 @@ top:
                }
                goto top;
        case 's':               /* suggest a move */
-               i = fmtbuf[1] == 'b' ? BLACK : WHITE;
-               sprintf(fmtbuf, "suggest %c %s", i == BLACK ? 'B' : 'W',
+               i = input[1] == 'b' ? BLACK : WHITE;
+               debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
                        stoc(pickmove(i)));
-               dlog(fmtbuf);
                goto top;
        case 'f':               /* go forward a move */
                board[movelog[movenum - 1]].s_occ = movenum & 1 ? BLACK : WHITE;
@@ -401,12 +397,12 @@ top:
                bdisp();
                goto top;
        case 'l':               /* print move history */
-               if (fmtbuf[1] == '\0') {
+               if (input[1] == '\0') {
                        for (i = 0; i < movenum - 1; i++)
-                               dlog(stoc(movelog[i]));
+                               debuglog("%s", stoc(movelog[i]));
                        goto top;
                }
-               if ((fp = fopen(fmtbuf + 1, "w")) == NULL)
+               if ((fp = fopen(input + 1, "w")) == NULL)
                        goto top;
                for (i = 0; i < movenum - 1; i++) {
                        fprintf(fp, "%s", stoc(movelog[i]));
@@ -419,14 +415,17 @@ top:
                fclose(fp);
                goto top;
        case 'o':
+               /* avoid use w/o initialization on invalid input */
+               d1 = s1 = 0;
+
                n = 0;
-               for (str = fmtbuf + 1; *str; str++)
+               for (str = input + 1; *str; str++)
                        if (*str == ',') {
                                for (d1 = 0; d1 < 4; d1++)
                                        if (str[-1] == pdir[d1])
                                                break;
                                str[-1] = '\0';
-                               sp = &board[s1 = ctos(fmtbuf + 1)];
+                               sp = &board[s1 = ctos(input + 1)];
                                n = (sp->s_frame[d1] - frames) * FAREA;
                                *str++ = '\0';
                                break;
@@ -438,31 +437,25 @@ top:
                        if (str[-1] == pdir[d2])
                                break;
                n += sp->s_frame[d2] - frames;
-               str = fmtbuf;
-               sprintf(str, "overlap %s%c,", stoc(s1), pdir[d1]);
-               str += strlen(str);
-               sprintf(str, "%s%c = %x", stoc(s2), pdir[d2], overlap[n]);
-               dlog(fmtbuf);
+               debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
+                   stoc(s2), pdir[d2], overlap[n]);
                goto top;
        case 'p':
-               sp = &board[i = ctos(fmtbuf + 1)];
-               sprintf(fmtbuf, "V %s %x/%d %d %x/%d %d %d %x", stoc(i),
+               sp = &board[i = ctos(input + 1)];
+               debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
                        sp->s_combo[BLACK].s, sp->s_level[BLACK],
                        sp->s_nforce[BLACK],
                        sp->s_combo[WHITE].s, sp->s_level[WHITE],
-                       sp->s_nforce[WHITE], sp->s_wval, sp->s_flg);
-               dlog(fmtbuf);
-               sprintf(fmtbuf, "FB %s %x %x %x %x", stoc(i),
+                       sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
+               debuglog("FB %s %x %x %x %x", stoc(i),
                        sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
                        sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
-               dlog(fmtbuf);
-               sprintf(fmtbuf, "FW %s %x %x %x %x", stoc(i),
+               debuglog("FW %s %x %x %x %x", stoc(i),
                        sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
                        sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
-               dlog(fmtbuf);
                goto top;
        case 'e':       /* e {b|w} [0-9] spot */
-               str = fmtbuf + 1;
+               str = input + 1;
                if (*str >= '0' && *str <= '9')
                        n = *str++ - '0';
                else
@@ -476,17 +469,16 @@ top:
                                if (cbp->c_nframes != n)
                                        break;
                        }
-                       printcombo(cbp, fmtbuf);
-                       dlog(fmtbuf);
+                       printcombo(cbp, tmp, sizeof(tmp));
+                       debuglog("%s", tmp);
                }
                goto top;
        default:
-syntax:
-               dlog("Options are:");
-               dlog("q    - quit");
-               dlog("c    - continue");
-               dlog("d#   - set debug level to #");
-               dlog("p#   - print values at #");
+               debuglog("Options are:");
+               debuglog("q    - quit");
+               debuglog("c    - continue");
+               debuglog("d#   - set debug level to #");
+               debuglog("p#   - print values at #");
                goto top;
        }
 }
@@ -496,33 +488,43 @@ syntax:
  * Display debug info.
  */
 void
-dlog(str)
-       const char *str;
+debuglog(const char *fmt, ...)
 {
+       va_list ap;
+       char buf[128];
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       va_end(ap);
 
        if (debugfp)
-               fprintf(debugfp, "%s\n", str);
+               fprintf(debugfp, "%s\n", buf);
        if (interactive)
-               dislog(str);
+               dislog(buf);
        else
-               fprintf(stderr, "%s\n", str);
+               fprintf(stderr, "%s\n", buf);
 }
 
-void
-log(str)
-       const char *str;
+static void
+misclog(const char *fmt, ...)
 {
+       va_list ap;
+       char buf[128];
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       va_end(ap);
 
        if (debugfp)
-               fprintf(debugfp, "%s\n", str);
+               fprintf(debugfp, "%s\n", buf);
        if (interactive)
-               dislog(str);
+               dislog(buf);
        else
-               printf("%s\n", str);
+               printf("%s\n", buf);
 }
 
-void
-quit()
+static void
+quit(void)
 {
        if (interactive) {
                bdisp();                /* show final board */
@@ -531,9 +533,8 @@ quit()
        exit(0);
 }
 
-void
-quitsig(dummy)
-       int dummy;
+static void
+quitsig(int dummy __unused)
 {
        quit();
 }
@@ -542,10 +543,16 @@ quitsig(dummy)
  * Die gracefully.
  */
 void
-panic(str)
-       const char *str;
+panic(const char *fmt, ...)
 {
-       fprintf(stderr, "%s: %s\n", prog, str);
+       va_list ap;
+
+       fprintf(stderr, "%s: ", prog);
+       va_start(ap, fmt);
+       vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       fprintf(stderr, "\n");
+
        fputs("resign\n", stdout);
        quit();
 }