main.c revision 1.38 1 1.38 rillig /* $NetBSD: main.c,v 1.38 2022/05/16 21:53:41 rillig Exp $ */
2 1.3 cgd
3 1.1 tls /*
4 1.1 tls * Copyright (c) 1994
5 1.1 tls * The Regents of the University of California. All rights reserved.
6 1.1 tls *
7 1.1 tls * This code is derived from software contributed to Berkeley by
8 1.1 tls * Ralph Campbell.
9 1.1 tls *
10 1.1 tls * Redistribution and use in source and binary forms, with or without
11 1.1 tls * modification, are permitted provided that the following conditions
12 1.1 tls * are met:
13 1.1 tls * 1. Redistributions of source code must retain the above copyright
14 1.1 tls * notice, this list of conditions and the following disclaimer.
15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tls * notice, this list of conditions and the following disclaimer in the
17 1.1 tls * documentation and/or other materials provided with the distribution.
18 1.11 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 tls * may be used to endorse or promote products derived from this software
20 1.1 tls * without specific prior written permission.
21 1.1 tls *
22 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 tls * SUCH DAMAGE.
33 1.1 tls */
34 1.1 tls
35 1.4 lukem #include <sys/cdefs.h>
36 1.1 tls #ifndef lint
37 1.14 lukem __COPYRIGHT("@(#) Copyright (c) 1994\
38 1.14 lukem The Regents of the University of California. All rights reserved.");
39 1.1 tls #endif /* not lint */
40 1.1 tls
41 1.1 tls #ifndef lint
42 1.2 tls #if 0
43 1.1 tls static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
44 1.2 tls #else
45 1.38 rillig __RCSID("$NetBSD: main.c,v 1.38 2022/05/16 21:53:41 rillig Exp $");
46 1.2 tls #endif
47 1.1 tls #endif /* not lint */
48 1.1 tls
49 1.1 tls #include <curses.h>
50 1.1 tls #include <err.h>
51 1.21 dholland #include <limits.h>
52 1.1 tls #include <signal.h>
53 1.16 dholland #include <stdarg.h>
54 1.1 tls #include <stdlib.h>
55 1.1 tls #include <string.h>
56 1.5 perry #include <time.h>
57 1.1 tls #include <unistd.h>
58 1.1 tls
59 1.1 tls #include "gomoku.h"
60 1.1 tls
61 1.1 tls #define USER 0 /* get input from standard input */
62 1.1 tls #define PROGRAM 1 /* get input from program */
63 1.1 tls #define INPUTF 2 /* get input from a file */
64 1.1 tls
65 1.37 rillig bool interactive = true; /* true if interactive */
66 1.33 rillig int debug; /* > 0 if debugging */
67 1.20 dholland static int test; /* both moves come from 1: input, 2: computer */
68 1.20 dholland static char *prog; /* name of program */
69 1.23 dholland static char user[LOGIN_NAME_MAX]; /* name of player */
70 1.20 dholland static FILE *debugfp; /* file for debug output */
71 1.20 dholland static FILE *inputfp; /* file for debug input */
72 1.1 tls
73 1.6 jsm const char pdir[4] = "-\\|/";
74 1.1 tls
75 1.1 tls struct spotstr board[BAREA]; /* info for board */
76 1.1 tls struct combostr frames[FAREA]; /* storage for all frames */
77 1.1 tls struct combostr *sortframes[2]; /* sorted list of non-empty frames */
78 1.1 tls u_char overlap[FAREA * FAREA]; /* true if frame [a][b] overlap */
79 1.1 tls short intersect[FAREA * FAREA]; /* frame [a][b] intersection */
80 1.1 tls int movelog[BSZ * BSZ]; /* log of all the moves */
81 1.1 tls int movenum; /* current move number */
82 1.29 rillig const char *plyr[2]; /* who's who */
83 1.1 tls
84 1.20 dholland static int readinput(FILE *);
85 1.20 dholland static void misclog(const char *, ...) __printflike(1, 2);
86 1.20 dholland static void quit(void) __dead;
87 1.35 rillig #if !defined(DEBUG)
88 1.35 rillig static void quitsig(int) __dead;
89 1.35 rillig #endif
90 1.1 tls
91 1.4 lukem int
92 1.15 dholland main(int argc, char **argv)
93 1.1 tls {
94 1.1 tls char buf[128];
95 1.21 dholland char fname[PATH_MAX];
96 1.23 dholland char *tmp;
97 1.1 tls int color, curmove, i, ch;
98 1.1 tls int input[2];
99 1.8 jsm
100 1.8 jsm /* Revoke setgid privileges */
101 1.10 mycroft setgid(getgid());
102 1.1 tls
103 1.23 dholland tmp = getlogin();
104 1.33 rillig if (tmp != NULL) {
105 1.23 dholland strlcpy(user, tmp, sizeof(user));
106 1.23 dholland } else {
107 1.23 dholland strcpy(user, "you");
108 1.23 dholland }
109 1.23 dholland
110 1.4 lukem color = curmove = 0;
111 1.4 lukem
112 1.1 tls prog = strrchr(argv[0], '/');
113 1.33 rillig if (prog != NULL)
114 1.1 tls prog++;
115 1.1 tls else
116 1.1 tls prog = argv[0];
117 1.1 tls
118 1.4 lukem while ((ch = getopt(argc, argv, "bcdD:u")) != -1) {
119 1.1 tls switch (ch) {
120 1.1 tls case 'b': /* background */
121 1.33 rillig interactive = false;
122 1.1 tls break;
123 1.1 tls case 'd': /* debugging */
124 1.1 tls debug++;
125 1.1 tls break;
126 1.1 tls case 'D': /* log debug output to file */
127 1.1 tls if ((debugfp = fopen(optarg, "w")) == NULL)
128 1.1 tls err(1, "%s", optarg);
129 1.1 tls break;
130 1.22 dholland case 'u': /* testing: user versus user */
131 1.1 tls test = 1;
132 1.1 tls break;
133 1.22 dholland case 'c': /* testing: computer versus computer */
134 1.1 tls test = 2;
135 1.1 tls break;
136 1.38 rillig default:
137 1.38 rillig fprintf(stderr, "usage: %s [-bcdu] [-Dfile] [file]\n",
138 1.38 rillig getprogname());
139 1.38 rillig return EXIT_FAILURE;
140 1.1 tls }
141 1.1 tls }
142 1.1 tls argc -= optind;
143 1.1 tls argv += optind;
144 1.33 rillig if (argc != 0) {
145 1.1 tls if ((inputfp = fopen(*argv, "r")) == NULL)
146 1.1 tls err(1, "%s", *argv);
147 1.1 tls }
148 1.1 tls
149 1.33 rillig if (debug == 0)
150 1.32 rillig srandom((unsigned int)time(0));
151 1.1 tls if (interactive)
152 1.1 tls cursinit(); /* initialize curses */
153 1.1 tls again:
154 1.1 tls bdinit(board); /* initialize board contents */
155 1.1 tls
156 1.1 tls if (interactive) {
157 1.1 tls plyr[BLACK] = plyr[WHITE] = "???";
158 1.1 tls bdisp_init(); /* initialize display of board */
159 1.1 tls #ifdef DEBUG
160 1.1 tls signal(SIGINT, whatsup);
161 1.1 tls #else
162 1.4 lukem signal(SIGINT, quitsig);
163 1.1 tls #endif
164 1.1 tls
165 1.1 tls if (inputfp == NULL && test == 0) {
166 1.31 rillig move(BSZ + 3, 0);
167 1.24 dholland printw("Black moves first. ");
168 1.24 dholland ask("(B)lack or (W)hite? ");
169 1.1 tls for (;;) {
170 1.24 dholland ch = get_key(NULL);
171 1.23 dholland if (ch == 'b' || ch == 'B') {
172 1.1 tls color = BLACK;
173 1.1 tls break;
174 1.1 tls }
175 1.23 dholland if (ch == 'w' || ch == 'W') {
176 1.1 tls color = WHITE;
177 1.1 tls break;
178 1.1 tls }
179 1.24 dholland if (ch == 'q' || ch == 'Q') {
180 1.24 dholland quit();
181 1.24 dholland }
182 1.24 dholland beep();
183 1.24 dholland ask("Please choose (B)lack or (W)hite: ");
184 1.1 tls }
185 1.31 rillig move(BSZ + 3, 0);
186 1.1 tls clrtoeol();
187 1.1 tls }
188 1.1 tls } else {
189 1.1 tls setbuf(stdout, 0);
190 1.19 roy get_line(buf, sizeof(buf));
191 1.1 tls if (strcmp(buf, "black") == 0)
192 1.1 tls color = BLACK;
193 1.1 tls else if (strcmp(buf, "white") == 0)
194 1.1 tls color = WHITE;
195 1.1 tls else {
196 1.16 dholland panic("Huh? Expected `black' or `white', got `%s'\n",
197 1.1 tls buf);
198 1.1 tls }
199 1.1 tls }
200 1.1 tls
201 1.33 rillig if (inputfp != NULL) {
202 1.1 tls input[BLACK] = INPUTF;
203 1.1 tls input[WHITE] = INPUTF;
204 1.1 tls } else {
205 1.1 tls switch (test) {
206 1.22 dholland case 0: /* user versus program */
207 1.1 tls input[color] = USER;
208 1.33 rillig input[color != BLACK ? BLACK : WHITE] = PROGRAM;
209 1.1 tls break;
210 1.1 tls
211 1.22 dholland case 1: /* user versus user */
212 1.1 tls input[BLACK] = USER;
213 1.1 tls input[WHITE] = USER;
214 1.1 tls break;
215 1.1 tls
216 1.22 dholland case 2: /* program versus program */
217 1.1 tls input[BLACK] = PROGRAM;
218 1.1 tls input[WHITE] = PROGRAM;
219 1.1 tls break;
220 1.1 tls }
221 1.1 tls }
222 1.1 tls if (interactive) {
223 1.23 dholland plyr[BLACK] = input[BLACK] == USER ? user : prog;
224 1.23 dholland plyr[WHITE] = input[WHITE] == USER ? user : prog;
225 1.33 rillig bdwho(true);
226 1.1 tls }
227 1.1 tls
228 1.33 rillig for (color = BLACK; ; color = color != BLACK ? BLACK : WHITE) {
229 1.1 tls top:
230 1.1 tls switch (input[color]) {
231 1.1 tls case INPUTF: /* input comes from a file */
232 1.1 tls curmove = readinput(inputfp);
233 1.1 tls if (curmove != ILLEGAL)
234 1.1 tls break;
235 1.1 tls switch (test) {
236 1.22 dholland case 0: /* user versus program */
237 1.1 tls input[color] = USER;
238 1.33 rillig input[color != BLACK ? BLACK : WHITE] =
239 1.33 rillig PROGRAM;
240 1.1 tls break;
241 1.1 tls
242 1.22 dholland case 1: /* user versus user */
243 1.1 tls input[BLACK] = USER;
244 1.1 tls input[WHITE] = USER;
245 1.1 tls break;
246 1.1 tls
247 1.22 dholland case 2: /* program versus program */
248 1.1 tls input[BLACK] = PROGRAM;
249 1.1 tls input[WHITE] = PROGRAM;
250 1.1 tls break;
251 1.1 tls }
252 1.23 dholland plyr[BLACK] = input[BLACK] == USER ? user : prog;
253 1.23 dholland plyr[WHITE] = input[WHITE] == USER ? user : prog;
254 1.33 rillig bdwho(true);
255 1.1 tls goto top;
256 1.1 tls
257 1.1 tls case USER: /* input comes from standard input */
258 1.1 tls getinput:
259 1.23 dholland if (interactive) {
260 1.24 dholland ask("Select move, (S)ave or (Q)uit.");
261 1.23 dholland curmove = get_coord();
262 1.1 tls if (curmove == SAVE) {
263 1.1 tls FILE *fp;
264 1.1 tls
265 1.24 dholland ask("Save file name? ");
266 1.21 dholland (void)get_line(fname, sizeof(fname));
267 1.21 dholland if ((fp = fopen(fname, "w")) == NULL) {
268 1.16 dholland misclog("cannot create save file");
269 1.1 tls goto getinput;
270 1.1 tls }
271 1.1 tls for (i = 0; i < movenum - 1; i++)
272 1.1 tls fprintf(fp, "%s\n",
273 1.29 rillig stoc(movelog[i]));
274 1.1 tls fclose(fp);
275 1.1 tls goto getinput;
276 1.1 tls }
277 1.1 tls if (curmove != RESIGN &&
278 1.1 tls board[curmove].s_occ != EMPTY) {
279 1.23 dholland /*misclog("Illegal move");*/
280 1.23 dholland beep();
281 1.1 tls goto getinput;
282 1.1 tls }
283 1.23 dholland } else {
284 1.23 dholland if (!get_line(buf, sizeof(buf))) {
285 1.23 dholland curmove = RESIGN;
286 1.23 dholland break;
287 1.23 dholland }
288 1.23 dholland if (buf[0] == '\0')
289 1.23 dholland goto getinput;
290 1.23 dholland curmove = ctos(buf);
291 1.1 tls }
292 1.1 tls break;
293 1.1 tls
294 1.1 tls case PROGRAM: /* input comes from the program */
295 1.23 dholland if (interactive)
296 1.23 dholland ask("Thinking...");
297 1.1 tls curmove = pickmove(color);
298 1.1 tls break;
299 1.1 tls }
300 1.1 tls if (interactive) {
301 1.33 rillig misclog("%3d%s%-6s", movenum,
302 1.33 rillig color != BLACK ? " " : " ",
303 1.26 christos stoc(curmove));
304 1.1 tls }
305 1.1 tls if ((i = makemove(color, curmove)) != MOVEOK)
306 1.1 tls break;
307 1.1 tls if (interactive)
308 1.1 tls bdisp();
309 1.1 tls }
310 1.1 tls if (interactive) {
311 1.31 rillig move(BSZ + 3, 0);
312 1.1 tls switch (i) {
313 1.1 tls case WIN:
314 1.1 tls if (input[color] == PROGRAM)
315 1.1 tls addstr("Ha ha, I won");
316 1.23 dholland else if (input[0] == USER && input[1] == USER)
317 1.23 dholland addstr("Well, you won (and lost)");
318 1.1 tls else
319 1.1 tls addstr("Rats! you won");
320 1.1 tls break;
321 1.1 tls case TIE:
322 1.24 dholland addstr("Wow! It's a tie");
323 1.1 tls break;
324 1.1 tls case ILLEGAL:
325 1.1 tls addstr("Illegal move");
326 1.1 tls break;
327 1.1 tls }
328 1.1 tls clrtoeol();
329 1.1 tls bdisp();
330 1.1 tls if (i != RESIGN) {
331 1.1 tls replay:
332 1.24 dholland ask("Play again? ");
333 1.28 rillig ch = get_key("YyNnQqSs");
334 1.24 dholland if (ch == 'Y' || ch == 'y')
335 1.1 tls goto again;
336 1.24 dholland if (ch == 'S') {
337 1.1 tls FILE *fp;
338 1.1 tls
339 1.24 dholland ask("Save file name? ");
340 1.21 dholland (void)get_line(fname, sizeof(fname));
341 1.21 dholland if ((fp = fopen(fname, "w")) == NULL) {
342 1.16 dholland misclog("cannot create save file");
343 1.1 tls goto replay;
344 1.1 tls }
345 1.1 tls for (i = 0; i < movenum - 1; i++)
346 1.1 tls fprintf(fp, "%s\n",
347 1.29 rillig stoc(movelog[i]));
348 1.1 tls fclose(fp);
349 1.1 tls goto replay;
350 1.1 tls }
351 1.1 tls }
352 1.1 tls }
353 1.1 tls quit();
354 1.4 lukem /* NOTREACHED */
355 1.30 rillig return 0;
356 1.1 tls }
357 1.1 tls
358 1.20 dholland static int
359 1.15 dholland readinput(FILE *fp)
360 1.1 tls {
361 1.1 tls int c;
362 1.17 dholland char buf[128];
363 1.17 dholland size_t pos;
364 1.1 tls
365 1.17 dholland pos = 0;
366 1.17 dholland while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
367 1.17 dholland buf[pos++] = c;
368 1.17 dholland buf[pos] = '\0';
369 1.17 dholland return ctos(buf);
370 1.1 tls }
371 1.1 tls
372 1.1 tls #ifdef DEBUG
373 1.1 tls /*
374 1.1 tls * Handle strange situations.
375 1.1 tls */
376 1.35 rillig /* ARGSUSED */
377 1.1 tls void
378 1.15 dholland whatsup(int signum)
379 1.1 tls {
380 1.18 dholland int i, n, s1, s2, d1, d2;
381 1.1 tls struct spotstr *sp;
382 1.1 tls FILE *fp;
383 1.1 tls char *str;
384 1.1 tls struct elist *ep;
385 1.1 tls struct combostr *cbp;
386 1.18 dholland char input[128];
387 1.18 dholland char tmp[128];
388 1.1 tls
389 1.1 tls if (!interactive)
390 1.1 tls quit();
391 1.1 tls top:
392 1.24 dholland ask("debug command: ");
393 1.19 roy if (!get_line(input, sizeof(input)))
394 1.1 tls quit();
395 1.18 dholland switch (*input) {
396 1.1 tls case '\0':
397 1.1 tls goto top;
398 1.1 tls case 'q': /* conservative quit */
399 1.1 tls quit();
400 1.35 rillig /* NOTREACHED */
401 1.1 tls case 'd': /* set debug level */
402 1.18 dholland debug = input[1] - '0';
403 1.16 dholland debuglog("Debug set to %d", debug);
404 1.1 tls sleep(1);
405 1.35 rillig break;
406 1.1 tls case 'c':
407 1.1 tls break;
408 1.1 tls case 'b': /* back up a move */
409 1.1 tls if (movenum > 1) {
410 1.1 tls movenum--;
411 1.1 tls board[movelog[movenum - 1]].s_occ = EMPTY;
412 1.1 tls bdisp();
413 1.1 tls }
414 1.1 tls goto top;
415 1.1 tls case 's': /* suggest a move */
416 1.18 dholland i = input[1] == 'b' ? BLACK : WHITE;
417 1.16 dholland debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
418 1.1 tls stoc(pickmove(i)));
419 1.1 tls goto top;
420 1.1 tls case 'f': /* go forward a move */
421 1.35 rillig board[movelog[movenum - 1]].s_occ =
422 1.35 rillig (movenum & 1) != 0 ? BLACK : WHITE;
423 1.1 tls movenum++;
424 1.1 tls bdisp();
425 1.1 tls goto top;
426 1.1 tls case 'l': /* print move history */
427 1.18 dholland if (input[1] == '\0') {
428 1.1 tls for (i = 0; i < movenum - 1; i++)
429 1.16 dholland debuglog("%s", stoc(movelog[i]));
430 1.1 tls goto top;
431 1.1 tls }
432 1.18 dholland if ((fp = fopen(input + 1, "w")) == NULL)
433 1.1 tls goto top;
434 1.1 tls for (i = 0; i < movenum - 1; i++) {
435 1.1 tls fprintf(fp, "%s", stoc(movelog[i]));
436 1.1 tls if (++i < movenum - 1)
437 1.1 tls fprintf(fp, " %s\n", stoc(movelog[i]));
438 1.1 tls else
439 1.1 tls fputc('\n', fp);
440 1.1 tls }
441 1.1 tls bdump(fp);
442 1.1 tls fclose(fp);
443 1.1 tls goto top;
444 1.1 tls case 'o':
445 1.18 dholland /* avoid use w/o initialization on invalid input */
446 1.18 dholland d1 = s1 = 0;
447 1.18 dholland
448 1.1 tls n = 0;
449 1.35 rillig for (str = input + 1; *str != '\0'; str++)
450 1.1 tls if (*str == ',') {
451 1.1 tls for (d1 = 0; d1 < 4; d1++)
452 1.1 tls if (str[-1] == pdir[d1])
453 1.1 tls break;
454 1.1 tls str[-1] = '\0';
455 1.18 dholland sp = &board[s1 = ctos(input + 1)];
456 1.36 rillig n = (int)(sp->s_frame[d1] - frames) * FAREA;
457 1.1 tls *str++ = '\0';
458 1.1 tls break;
459 1.1 tls }
460 1.1 tls sp = &board[s2 = ctos(str)];
461 1.35 rillig while (*str != '\0')
462 1.1 tls str++;
463 1.1 tls for (d2 = 0; d2 < 4; d2++)
464 1.1 tls if (str[-1] == pdir[d2])
465 1.1 tls break;
466 1.36 rillig n += (int)(sp->s_frame[d2] - frames);
467 1.16 dholland debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
468 1.16 dholland stoc(s2), pdir[d2], overlap[n]);
469 1.1 tls goto top;
470 1.1 tls case 'p':
471 1.18 dholland sp = &board[i = ctos(input + 1)];
472 1.16 dholland debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
473 1.1 tls sp->s_combo[BLACK].s, sp->s_level[BLACK],
474 1.1 tls sp->s_nforce[BLACK],
475 1.1 tls sp->s_combo[WHITE].s, sp->s_level[WHITE],
476 1.18 dholland sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
477 1.16 dholland debuglog("FB %s %x %x %x %x", stoc(i),
478 1.1 tls sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
479 1.1 tls sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
480 1.16 dholland debuglog("FW %s %x %x %x %x", stoc(i),
481 1.1 tls sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
482 1.1 tls sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
483 1.1 tls goto top;
484 1.1 tls case 'e': /* e {b|w} [0-9] spot */
485 1.18 dholland str = input + 1;
486 1.1 tls if (*str >= '0' && *str <= '9')
487 1.1 tls n = *str++ - '0';
488 1.1 tls else
489 1.1 tls n = 0;
490 1.1 tls sp = &board[i = ctos(str)];
491 1.35 rillig for (ep = sp->s_empty; ep != NULL; ep = ep->e_next) {
492 1.1 tls cbp = ep->e_combo;
493 1.35 rillig if (n != 0) {
494 1.1 tls if (cbp->c_nframes > n)
495 1.1 tls continue;
496 1.1 tls if (cbp->c_nframes != n)
497 1.1 tls break;
498 1.1 tls }
499 1.18 dholland printcombo(cbp, tmp, sizeof(tmp));
500 1.18 dholland debuglog("%s", tmp);
501 1.1 tls }
502 1.1 tls goto top;
503 1.1 tls default:
504 1.16 dholland debuglog("Options are:");
505 1.16 dholland debuglog("q - quit");
506 1.16 dholland debuglog("c - continue");
507 1.16 dholland debuglog("d# - set debug level to #");
508 1.16 dholland debuglog("p# - print values at #");
509 1.1 tls goto top;
510 1.1 tls }
511 1.1 tls }
512 1.1 tls #endif /* DEBUG */
513 1.1 tls
514 1.1 tls /*
515 1.1 tls * Display debug info.
516 1.1 tls */
517 1.4 lukem void
518 1.16 dholland debuglog(const char *fmt, ...)
519 1.1 tls {
520 1.16 dholland va_list ap;
521 1.16 dholland char buf[128];
522 1.16 dholland
523 1.16 dholland va_start(ap, fmt);
524 1.16 dholland vsnprintf(buf, sizeof(buf), fmt, ap);
525 1.16 dholland va_end(ap);
526 1.1 tls
527 1.33 rillig if (debugfp != NULL)
528 1.16 dholland fprintf(debugfp, "%s\n", buf);
529 1.1 tls if (interactive)
530 1.16 dholland dislog(buf);
531 1.1 tls else
532 1.16 dholland fprintf(stderr, "%s\n", buf);
533 1.1 tls }
534 1.1 tls
535 1.20 dholland static void
536 1.16 dholland misclog(const char *fmt, ...)
537 1.1 tls {
538 1.16 dholland va_list ap;
539 1.16 dholland char buf[128];
540 1.16 dholland
541 1.16 dholland va_start(ap, fmt);
542 1.16 dholland vsnprintf(buf, sizeof(buf), fmt, ap);
543 1.16 dholland va_end(ap);
544 1.1 tls
545 1.33 rillig if (debugfp != NULL)
546 1.16 dholland fprintf(debugfp, "%s\n", buf);
547 1.1 tls if (interactive)
548 1.16 dholland dislog(buf);
549 1.1 tls else
550 1.16 dholland printf("%s\n", buf);
551 1.1 tls }
552 1.1 tls
553 1.20 dholland static void
554 1.15 dholland quit(void)
555 1.1 tls {
556 1.1 tls if (interactive) {
557 1.1 tls bdisp(); /* show final board */
558 1.1 tls cursfini();
559 1.1 tls }
560 1.1 tls exit(0);
561 1.1 tls }
562 1.1 tls
563 1.35 rillig #if !defined(DEBUG)
564 1.20 dholland static void
565 1.15 dholland quitsig(int dummy __unused)
566 1.4 lukem {
567 1.4 lukem quit();
568 1.4 lukem }
569 1.35 rillig #endif
570 1.4 lukem
571 1.1 tls /*
572 1.1 tls * Die gracefully.
573 1.1 tls */
574 1.4 lukem void
575 1.16 dholland panic(const char *fmt, ...)
576 1.1 tls {
577 1.16 dholland va_list ap;
578 1.16 dholland
579 1.27 dholland if (interactive) {
580 1.27 dholland bdisp();
581 1.27 dholland cursfini();
582 1.27 dholland }
583 1.27 dholland
584 1.16 dholland fprintf(stderr, "%s: ", prog);
585 1.16 dholland va_start(ap, fmt);
586 1.16 dholland vfprintf(stderr, fmt, ap);
587 1.16 dholland va_end(ap);
588 1.16 dholland fprintf(stderr, "\n");
589 1.16 dholland
590 1.27 dholland fputs("I resign\n", stdout);
591 1.27 dholland exit(1);
592 1.1 tls }
593