From 82d42178708c6108bb6c1649bd4bb11cbf0507e1 Mon Sep 17 00:00:00 2001 From: dholland Date: Thu, 4 Jun 2009 06:27:47 +0000 Subject: Make a couple of the logging/printing functions printf-alikes. This removes most of the calls to sprintf. --- gomoku/gomoku.h | 8 ++--- gomoku/main.c | 93 ++++++++++++++++++++++++++++++------------------------- gomoku/pickmove.c | 59 ++++++++++++++--------------------- 3 files changed, 78 insertions(+), 82 deletions(-) (limited to 'gomoku') diff --git a/gomoku/gomoku.h b/gomoku/gomoku.h index 4a9b4a1c..1f04ecf8 100644 --- a/gomoku/gomoku.h +++ b/gomoku/gomoku.h @@ -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 bdisp_init(void); 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); diff --git a/gomoku/main.c b/gomoku/main.c index 39cd6272..3f19106a 100644 --- a/gomoku/main.c +++ b/gomoku/main.c @@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1994\ #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 #include #include +#include #include #include #include @@ -176,10 +177,8 @@ again: 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 @@ again: 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 @@ again: } if (curmove != RESIGN && board[curmove].s_occ != EMPTY) { - glog("Illegal move"); + misclog("Illegal move"); goto getinput; } } @@ -278,8 +277,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; @@ -316,7 +314,7 @@ again: 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 @@ top: 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 @@ top: 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 @@ top: 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 @@ 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), + 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 @@ top: 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 @@ syntax: * 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 @@ quitsig(int dummy __unused) * 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(); } diff --git a/gomoku/pickmove.c b/gomoku/pickmove.c index 490edd18..eb65a3e0 100644 --- a/gomoku/pickmove.c +++ b/gomoku/pickmove.c @@ -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 @@ pickmove(int us) 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 @@ pickmove(int us) } 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 @@ pickmove(int us) 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 @@ scanframes(int color) 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 @@ scanframes(int color) #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 @@ makecombo2(struct combostr *ocbp, struct spotstr *osp, int off, int s) 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 @@ makecombo(struct combostr *ocbp, struct spotstr *osp, int off, int s) 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 @@ makecombo(struct combostr *ocbp, struct spotstr *osp, int off, int s) } 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 @@ makeempty(struct combostr *ocbp) 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 @@ makeempty(struct combostr *ocbp) } 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 @@ sortcombo(struct combostr **scbpp, struct combostr **cbpp, 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 @@ inserted: 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 @@ inserted: pdir[(*cpp)->c_dir]); str += strlen(str); } - dlog(fmtbuf); + debuglog("%s", fmtbuf); } #endif /* DEBUG */ return (1); -- cgit v1.2.3-56-ge451