]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - gomoku/main.c
Allow saved game filenames up to PATH_MAX. From OpenBSD.
[bsdgames-darwin.git] / gomoku / main.c
index e707e47ebfa0fcf7eac52a13d1fa488a65608557..0f9b18f99d251b85fc2c3b0ae20f5aa45774d73b 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.13 2007/12/15 19:44:40 perry Exp $  */
+/*     $NetBSD: main.c,v 1.21 2010/03/29 02:46:05 dholland Exp $       */
 
 /*
  * Copyright (c) 1994
 
 #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.13 2007/12/15 19:44:40 perry Exp $");
+__RCSID("$NetBSD: main.c,v 1.21 2010/03/29 02:46:05 dholland Exp $");
 #endif
 #endif /* not lint */
 
 #include <curses.h>
 #include <err.h>
+#include <limits.h>
 #include <signal.h>
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -62,13 +64,12 @@ __RCSID("$NetBSD: main.c,v 1.13 2007/12/15 19:44:40 perry 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 */
@@ -79,14 +80,16 @@ int movelog[BSZ * BSZ];             /* log of all the moves */
 int    movenum;                        /* current move number */
 const char     *plyr[2];                       /* who's who */
 
-int    main(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];
+       char fname[PATH_MAX];
        int color, curmove, i, ch;
        int input[2];
        static const char *const fmt[2] = {
@@ -155,7 +158,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;
@@ -172,16 +175,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);
                }
        }
 
@@ -244,7 +245,7 @@ again:
                getinput:
                        if (interactive)
                                ask("move? ");
-                       if (!getline(buf, sizeof(buf))) {
+                       if (!get_line(buf, sizeof(buf))) {
                                curmove = RESIGN;
                                break;
                        }
@@ -256,9 +257,9 @@ again:
                                        FILE *fp;
 
                                        ask("save file name? ");
-                                       (void)getline(buf, sizeof(buf));
-                                       if ((fp = fopen(buf, "w")) == NULL) {
-                                               glog("cannot create save file");
+                                       (void)get_line(fname, sizeof(fname));
+                                       if ((fp = fopen(fname, "w")) == NULL) {
+                                               misclog("cannot create save file");
                                                goto getinput;
                                        }
                                        for (i = 0; i < movenum - 1; i++)
@@ -269,7 +270,7 @@ again:
                                }
                                if (curmove != RESIGN &&
                                    board[curmove].s_occ != EMPTY) {
-                                       glog("Illegal move");
+                                       misclog("Illegal move");
                                        goto getinput;
                                }
                        }
@@ -280,8 +281,7 @@ again:
                        break;
                }
                if (interactive) {
-                       sprintf(fmtbuf, fmt[color], movenum, stoc(curmove));
-                       glog(fmtbuf);
+                       misclog(fmt[color], movenum, stoc(curmove));
                }
                if ((i = makemove(color, curmove)) != MOVEOK)
                        break;
@@ -309,16 +309,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));
-                               if ((fp = fopen(buf, "w")) == NULL) {
-                                       glog("cannot create save file");
+                               (void)get_line(fname, sizeof(fname));
+                               if ((fp = fopen(fname, "w")) == NULL) {
+                                       misclog("cannot create save file");
                                        goto replay;
                                }
                                for (i = 0; i < movenum - 1; i++)
@@ -334,18 +334,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
@@ -353,31 +353,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;
@@ -389,10 +389,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;
@@ -400,12 +399,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]));
@@ -418,14 +417,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;
@@ -437,31 +439,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
@@ -475,17 +471,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;
        }
 }
@@ -495,33 +490,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
-glog(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 */
@@ -530,9 +535,8 @@ quit()
        exit(0);
 }
 
-void
-quitsig(dummy)
-       int dummy __unused;
+static void
+quitsig(int dummy __unused)
 {
        quit();
 }
@@ -541,10 +545,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();
 }