Module Name:    src
Committed By:   dholland
Date:           Thu Jun  4 06:27:47 UTC 2009

Modified Files:
        src/games/gomoku: gomoku.h main.c pickmove.c

Log Message:
Make a couple of the logging/printing functions printf-alikes. This removes
most of the calls to sprintf.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.15 -r1.16 src/games/gomoku/main.c \
    src/games/gomoku/pickmove.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/gomoku/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.12 src/games/gomoku/gomoku.h:1.13
--- src/games/gomoku/gomoku.h:1.12	Thu Jun  4 05:52:30 2009
+++ src/games/gomoku/gomoku.h	Thu Jun  4 06:27:47 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: gomoku.h,v 1.12 2009/06/04 05:52:30 dholland Exp $	*/
+/*	$NetBSD: gomoku.h,v 1.13 2009/06/04 06:27:47 dholland Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -272,9 +272,9 @@
 void	cursfini(void);
 void	cursinit(void);
 void	bdwho(int);
-void	panic(const char *) __dead;
-void	glog(const char *);
-void	dlog(const char *);
+void	panic(const char *, ...) __printflike(1, 2) __dead;
+void	misclog(const char *, ...) __printflike(1, 2);
+void	debuglog(const char *, ...) __printflike(1, 2);
 void	quit(void) __dead;
 void	quitsig(int) __dead;
 void	whatsup(int);

Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.15 src/games/gomoku/main.c:1.16
--- src/games/gomoku/main.c:1.15	Thu Jun  4 05:27:04 2009
+++ src/games/gomoku/main.c	Thu Jun  4 06:27:47 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.15 2009/06/04 05:27:04 dholland Exp $	*/
+/*	$NetBSD: main.c,v 1.16 2009/06/04 06:27:47 dholland Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -42,13 +42,14 @@
 #if 0
 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.15 2009/06/04 05:27:04 dholland Exp $");
+__RCSID("$NetBSD: main.c,v 1.16 2009/06/04 06:27:47 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>
@@ -176,10 +177,8 @@
 		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);
 		}
 	}
 
@@ -256,7 +255,7 @@
 					ask("save file name? ");
 					(void)getline(buf, sizeof(buf));
 					if ((fp = fopen(buf, "w")) == NULL) {
-						glog("cannot create save file");
+						misclog("cannot create save file");
 						goto getinput;
 					}
 					for (i = 0; i < movenum - 1; i++)
@@ -267,7 +266,7 @@
 				}
 				if (curmove != RESIGN &&
 				    board[curmove].s_occ != EMPTY) {
-					glog("Illegal move");
+					misclog("Illegal move");
 					goto getinput;
 				}
 			}
@@ -278,8 +277,7 @@
 			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;
@@ -316,7 +314,7 @@
 				ask("save file name? ");
 				(void)getline(buf, sizeof(buf));
 				if ((fp = fopen(buf, "w")) == NULL) {
-					glog("cannot create save file");
+					misclog("cannot create save file");
 					goto replay;
 				}
 				for (i = 0; i < movenum - 1; i++)
@@ -372,8 +370,7 @@
 		quit();
 	case 'd':		/* set debug level */
 		debug = fmtbuf[1] - '0';
-		sprintf(fmtbuf, "Debug set to %d", debug);
-		dlog(fmtbuf);
+		debuglog("Debug set to %d", debug);
 		sleep(1);
 	case 'c':
 		break;
@@ -386,9 +383,8 @@
 		goto top;
 	case 's':		/* suggest a move */
 		i = fmtbuf[1] == 'b' ? BLACK : WHITE;
-		sprintf(fmtbuf, "suggest %c %s", i == BLACK ? 'B' : 'W',
+		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;
@@ -398,7 +394,7 @@
 	case 'l':		/* print move history */
 		if (fmtbuf[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)
@@ -433,28 +429,22 @@
 			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),
+		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),
+		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;
@@ -472,16 +462,16 @@
 					break;
 			}
 			printcombo(cbp, fmtbuf);
-			dlog(fmtbuf);
+			debuglog("%s", fmtbuf);
 		}
 		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;
 	}
 }
@@ -491,27 +481,39 @@
  * Display debug info.
  */
 void
-dlog(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(const char *str)
+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
@@ -534,9 +536,16 @@
  * Die gracefully.
  */
 void
-panic(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();
 }
Index: src/games/gomoku/pickmove.c
diff -u src/games/gomoku/pickmove.c:1.15 src/games/gomoku/pickmove.c:1.16
--- src/games/gomoku/pickmove.c:1.15	Thu Jun  4 05:43:29 2009
+++ src/games/gomoku/pickmove.c	Thu Jun  4 06:27:47 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: pickmove.c,v 1.15 2009/06/04 05:43:29 dholland Exp $	*/
+/*	$NetBSD: pickmove.c,v 1.16 2009/06/04 06:27:47 dholland Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)pickmove.c	8.2 (Berkeley) 5/3/95";
 #else
-__RCSID("$NetBSD: pickmove.c,v 1.15 2009/06/04 05:43:29 dholland Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.16 2009/06/04 06:27:47 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -100,13 +100,12 @@
 			continue;
 		if (debug && (sp->s_combo[BLACK].c.a == 1 ||
 		    sp->s_combo[WHITE].c.a == 1)) {
-			sprintf(fmtbuf, "- %s %x/%d %d %x/%d %d %d", stoc(sp - board),
+			debuglog("- %s %x/%d %d %x/%d %d %d", stoc(sp - board),
 				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);
-			dlog(fmtbuf);
 		}
 		/* pick the best black move */
 		if (better(sp, sp1, BLACK))
@@ -117,20 +116,18 @@
 	}
 
 	if (debug) {
-		sprintf(fmtbuf, "B %s %x/%d %d %x/%d %d %d",
+		debuglog("B %s %x/%d %d %x/%d %d %d",
 			stoc(sp1 - board),
 			sp1->s_combo[BLACK].s, sp1->s_level[BLACK],
 			sp1->s_nforce[BLACK],
 			sp1->s_combo[WHITE].s, sp1->s_level[WHITE],
 			sp1->s_nforce[WHITE], sp1->s_wval);
-		dlog(fmtbuf);
-		sprintf(fmtbuf, "W %s %x/%d %d %x/%d %d %d",
+		debuglog("W %s %x/%d %d %x/%d %d %d",
 			stoc(sp2 - board),
 			sp2->s_combo[WHITE].s, sp2->s_level[WHITE],
 			sp2->s_nforce[WHITE],
 			sp2->s_combo[BLACK].s, sp2->s_level[BLACK],
 			sp2->s_nforce[BLACK], sp2->s_wval);
-		dlog(fmtbuf);
 		/*
 		 * Check for more than one force that can't
 		 * all be blocked with one move.
@@ -138,7 +135,7 @@
 		sp = (us == BLACK) ? sp2 : sp1;
 		m = sp - board;
 		if (sp->s_combo[!us].c.a == 1 && !BIT_TEST(forcemap, m))
-			dlog("*** Can't be blocked");
+			debuglog("*** Can't be blocked");
 	}
 	if (us == BLACK) {
 		Ocp = &sp1->s_combo[BLACK];
@@ -329,9 +326,8 @@
 	d = 2;
 	while (d <= ((movenum + 1) >> 1) && combolen > n) {
 		if (debug) {
-			sprintf(fmtbuf, "%cL%d %d %d %d", "BW"[color],
+			debuglog("%cL%d %d %d %d", "BW"[color],
 				d, combolen - n, combocnt, elistcnt);
-			dlog(fmtbuf);
 			refresh();
 		}
 		n = combolen;
@@ -388,15 +384,13 @@
 
 #ifdef DEBUG
 	if (combocnt) {
-		sprintf(fmtbuf, "scanframes: %c combocnt %d", "BW"[color],
+		debuglog("scanframes: %c combocnt %d", "BW"[color],
 			combocnt);
-		dlog(fmtbuf);
 		whatsup(0);
 	}
 	if (elistcnt) {
-		sprintf(fmtbuf, "scanframes: %c elistcnt %d", "BW"[color],
+		debuglog("scanframes: %c elistcnt %d", "BW"[color],
 			elistcnt);
-		dlog(fmtbuf);
 		whatsup(0);
 	}
 #endif
@@ -500,14 +494,13 @@
 		combocnt++;
 
 		if ((c == 1 && debug > 1) || debug > 3) {
-		    sprintf(fmtbuf, "%c c %d %d m %x %x o %d %d",
+		    debuglog(fmtbuf, "%c c %d %d m %x %x o %d %d",
 			"bw"[curcolor],
 			ncbp->c_framecnt[0], ncbp->c_framecnt[1],
 			ncbp->c_emask[0], ncbp->c_emask[1],
 			ncbp->c_voff[0], ncbp->c_voff[1]);
-		    dlog(fmtbuf);
 		    printcombo(ncbp, fmtbuf);
-		    dlog(fmtbuf);
+		    debuglog("%s", fmtbuf);
 		}
 		if (c > 1) {
 		    /* record the empty spots that will complete this combo */
@@ -665,9 +658,8 @@
 		sp = &board[vertices[0].o_intersect];
 #ifdef DEBUG
 		if (sp->s_occ != EMPTY) {
-		    sprintf(fmtbuf, "loop: %c %s", "BW"[curcolor],
+		    debuglog("loop: %c %s", "BW"[curcolor],
 			stoc(sp - board));
-		    dlog(fmtbuf);
 		    whatsup(0);
 		}
 #endif
@@ -747,14 +739,13 @@
 	    }
 
 	    if ((c == 1 && debug > 1) || debug > 3) {
-		sprintf(fmtbuf, "%c v%d i%d d%d c %d %d m %x %x o %d %d",
+		debuglog("%c v%d i%d d%d c %d %d m %x %x o %d %d",
 		    "bw"[curcolor], verts, ncbp->c_frameindex, ncbp->c_dir,
 		    ncbp->c_framecnt[0], ncbp->c_framecnt[1],
 		    ncbp->c_emask[0], ncbp->c_emask[1],
 		    ncbp->c_voff[0], ncbp->c_voff[1]);
-		dlog(fmtbuf);
 		printcombo(ncbp, fmtbuf);
-		dlog(fmtbuf);
+		debuglog("%s", fmtbuf);
 	    }
 	    if (c > 1) {
 		/* record the empty spots that will complete this combo */
@@ -793,9 +784,8 @@
 	int nframes;
 
 	if (debug > 2) {
-		sprintf(fmtbuf, "E%c ", "bw"[curcolor]);
-		printcombo(ocbp, fmtbuf + 3);
-		dlog(fmtbuf);
+		printcombo(ocbp, fmtbuf);
+		debuglog("E%c %s", "bw"[curcolor], fmtbuf);
 	}
 
 	/* should never happen but check anyway */
@@ -917,14 +907,13 @@
 			}
 			nep->e_fval.s = ep->e_fval.s;
 			if (debug > 2) {
-				sprintf(fmtbuf, "e %s o%d i%d c%d m%x %x",
+				debuglog("e %s o%d i%d c%d m%x %x",
 					stoc(sp - board),
 					nep->e_off,
 					nep->e_frameindex,
 					nep->e_framecnt,
 					nep->e_emask,
 					nep->e_fval.s);
-				dlog(fmtbuf);
 			}
 
 			/* sort by the number of frames in the combo */
@@ -1209,16 +1198,15 @@
 	if (debug > 3) {
 		char *str;
 
-		sprintf(fmtbuf, "sortc: %s%c l%d", stoc(fcbp->c_vertex),
+		debuglog("sortc: %s%c l%d", stoc(fcbp->c_vertex),
 			pdir[fcbp->c_dir], curlevel);
-		dlog(fmtbuf);
 		str = fmtbuf;
 		for (cpp = cbpp; cpp < cbpp + curlevel; cpp++) {
 			sprintf(str, " %s%c", stoc((*cpp)->c_vertex),
 				pdir[(*cpp)->c_dir]);
 			str += strlen(str);
 		}
-		dlog(fmtbuf);
+		debuglog("%s", fmtbuf);
 	}
 #endif /* DEBUG */
 
@@ -1268,17 +1256,16 @@
 		if (debug > 3) {
 			char *str;
 
-			sprintf(fmtbuf, "sort1: n%d", n);
-			dlog(fmtbuf);
+			debuglog("sort1: n%d", n);
 			str = fmtbuf;
 			for (cpp = scbpp; cpp < scbpp + n; cpp++) {
 				sprintf(str, " %s%c", stoc((*cpp)->c_vertex),
 					pdir[(*cpp)->c_dir]);
 				str += strlen(str);
 			}
-			dlog(fmtbuf);
+			debuglog("%s", fmtbuf);
 			printcombo(cbp, fmtbuf);
-			dlog(fmtbuf);
+			debuglog("%s", fmtbuf);
 			str = fmtbuf;
 			cbpp--;
 			for (cpp = cbpp; cpp < cbpp + n; cpp++) {
@@ -1286,7 +1273,7 @@
 					pdir[(*cpp)->c_dir]);
 				str += strlen(str);
 			}
-			dlog(fmtbuf);
+			debuglog("%s", fmtbuf);
 		}
 #endif /* DEBUG */
 		return (1);

Reply via email to