Convert more stuff from #ifdef to dspwrappers: GetString.
[gnushogi.git] / gnushogi / dspwrappers.c
1 /*
2  * FILE: dspwrappers.c
3  *
4  *     Wrapper functions which call analogous functions in rawdsp.c
5  *     or cursesdsp.c depending on the interface.
6  *
7  * ----------------------------------------------------------------------
8  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
9  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
10  *
11  * GNU SHOGI is based on GNU CHESS
12  *
13  * Copyright (c) 1988, 1989, 1990 John Stanback
14  * Copyright (c) 1992 Free Software Foundation
15  *
16  * This file is part of GNU SHOGI.
17  *
18  * GNU Shogi is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 3 of the License,
21  * or (at your option) any later version.
22  *
23  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with GNU Shogi; see the file COPYING. If not, see
30  * <http://www.gnu.org/licenses/>.
31  * ----------------------------------------------------------------------
32  *
33  */
34
35 #include "gnushogi.h"
36 #include "dspwrappers.h"
37 #include "rawdsp.h"
38 #include "cursesdsp.h"
39
40 #include <stdarg.h>
41 #include <stdio.h>
42
43 #define CASE_DSP_RAW(func,args) \
44   case DISPLAY_RAW:             \
45   case DISPLAY_X:               \
46     func args;                  \
47     break
48
49 #ifdef HAVE_LIBCURSES
50 #define CASE_DSP_CURSES(func,args) \
51   case DISPLAY_CURSES:             \
52     func args;                     \
53     break;
54 #else
55 #define CASE_DSP_CURSES(func,args)
56 #endif
57
58 #define DISPLAY_FUNC(func,argsdecl,args)        \
59   void func argsdecl                            \
60   {                                             \
61     switch (display_type) {                     \
62       CASE_DSP_RAW(Raw_ ## func, args);         \
63       CASE_DSP_CURSES(Curses_ ## func, args);   \
64     }                                           \
65   }
66 #define DISPLAY_VOIDFUNC(func)                  \
67   DISPLAY_FUNC(func,(void),())
68
69 #define DISPLAY_STDARGFUNC(func,argsdecl,last,args)     \
70   void func argsdecl                                    \
71   {                                                     \
72     va_list ap;                                         \
73     va_start(ap, last);                                 \
74     switch (display_type) {                             \
75       CASE_DSP_RAW(Raw_ ## func, args);                 \
76       CASE_DSP_CURSES(Curses_ ## func, args);           \
77     }                                                   \
78     va_end(ap);                                         \
79   }
80
81 DISPLAY_VOIDFUNC(ChangeAlphaWindow)
82 DISPLAY_VOIDFUNC(ChangeBetaWindow)
83 DISPLAY_VOIDFUNC(ChangeHashDepth)
84 DISPLAY_VOIDFUNC(ChangeSearchDepth)
85 DISPLAY_VOIDFUNC(ChangeXwindow)
86 DISPLAY_VOIDFUNC(ClearScreen)
87 DISPLAY_VOIDFUNC(DoDebug)
88 DISPLAY_FUNC(DoTable, (short table[NO_SQUARES]), (table))
89 DISPLAY_VOIDFUNC(EditBoard)
90 DISPLAY_VOIDFUNC(ExitShogi)
91 DISPLAY_VOIDFUNC(GiveHint)
92 DISPLAY_VOIDFUNC(Initialize)
93 DISPLAY_FUNC(ShowNodeCnt, (long NodeCnt), (NodeCnt))
94 DISPLAY_VOIDFUNC(OutputMove)
95 DISPLAY_VOIDFUNC(SetContempt)
96 DISPLAY_FUNC(SearchStartStuff, (short side), (side))
97 DISPLAY_FUNC(SelectLevel, (char *sx), (sx))
98 DISPLAY_FUNC(ShowCurrentMove, (short pnt, short f, short t), (pnt, f, t))
99 DISPLAY_FUNC(ShowDepth, (char ch), (ch))
100 DISPLAY_VOIDFUNC(ShowGameType)
101 DISPLAY_FUNC(ShowLine, (unsigned short *bstline), (bstline))
102 DISPLAY_FUNC(ShowMessage, (char *s), (s))
103 DISPLAY_STDARGFUNC(AlwaysShowMessage, (const char *format, ...), format, (format, ap))
104 DISPLAY_STDARGFUNC(Printf, (const char *format, ...), format, (format, ap))
105 DISPLAY_FUNC(ShowPatternCount, (short side, short n), (side, n))
106 DISPLAY_FUNC(ShowPostnValue, (short sq), (sq))
107 DISPLAY_VOIDFUNC(ShowPostnValues)
108 DISPLAY_VOIDFUNC(ShowResponseTime)
109 DISPLAY_FUNC(ShowResults, (short score, unsigned short *bstline, char ch), (score, bstline, ch))
110 DISPLAY_VOIDFUNC(ShowSidetoMove)
111 DISPLAY_VOIDFUNC(ShowStage)
112 DISPLAY_FUNC(TerminateSearch, (int sig), (sig))
113 DISPLAY_FUNC(UpdateDisplay, (short f, short t, short redraw, short isspec), (f, t, redraw, isspec))
114 DISPLAY_VOIDFUNC(help)
115
116 DISPLAY_FUNC(doRequestInputString, (const char* fmt, char* buffer), (fmt, buffer))
117 void RequestInputString(char* buffer, unsigned bufsize)
118 {
119     static char fmt[10];
120     int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
121     if (ret >= sizeof(fmt)) {
122         fprintf(stderr,
123                 "Insufficient format-buffer size in %s for bufsize=%u\n",
124                 __FUNCTION__, bufsize);
125         exit(1);
126     }
127     doRequestInputString(fmt, buffer);
128 }
129
130 /*********/
131
132 #define CASE_DSPFUNC_RAW(func,args) \
133   case DISPLAY_RAW:                 \
134   case DISPLAY_X:                   \
135     return (func args);             \
136     break
137
138 #ifdef HAVE_LIBCURSES
139 #define CASE_DSPFUNC_CURSES(func,args) \
140   case DISPLAY_CURSES:                 \
141     return (func args);                \
142     break;
143 #else
144 #define CASE_DSPFUNC_CURSES(func,args)
145 #endif
146
147 #define DISPLAY_INTFUNC(func,argsdecl,args)         \
148   int func argsdecl                                 \
149   {                                                 \
150     switch (display_type) {                         \
151       CASE_DSPFUNC_RAW(Raw_ ## func, args);         \
152       CASE_DSPFUNC_CURSES(Curses_ ## func, args);   \
153     }                                               \
154     assert(0);                                      \
155   }
156
157 DISPLAY_INTFUNC(GetString, (char* sx), (sx))