Remove hack previously used to insert diags into generated HTML.
[gnushogi.git] / gnushogi / sysdeps.c
blobc837930a521f67edf02a83605e9d3002596f1094
1 /*
2 * FILE: sysdeps.c
4 * System-dependent functions for GNU Shogi.
6 * ----------------------------------------------------------------------
7 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
10 * GNU SHOGI is based on GNU CHESS
12 * Copyright (c) 1988, 1989, 1990 John Stanback
13 * Copyright (c) 1992 Free Software Foundation
15 * This file is part of GNU SHOGI.
17 * GNU Shogi is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 3 of the License,
20 * or (at your option) any later version.
22 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with GNU Shogi; see the file COPYING. If not, see
29 * <http://www.gnu.org/licenses/>.
30 * ----------------------------------------------------------------------
34 #include "gnushogi.h"
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #if HAVE_SYS_FILIO_H
41 /* Definition of FIONREAD */
42 #include <sys/filio.h>
43 #endif
45 #if HAVE_ERRNO_H
46 /* Definition of errno(). */
47 #include <errno.h>
48 #endif
50 /* Forward declarations. */
52 void ElapsedTime_NOFIONREAD(ElapsedTime_mode iop);
53 void ElapsedTime_FIONREAD(ElapsedTime_mode iop);
57 * Determine the time that has passed since the search was started. If the
58 * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
59 * to true which will terminate the search.
60 * iop = COMPUTE_MODE calculate et, bump ETnodes
61 * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
62 * set reference time
66 * ElapsedTime() is actually a wrapper function around the different
67 * versions of ElapsedTime_XXX(). This allows us to compile all the
68 * different ways of measuring time in one executable.
71 void
72 ElapsedTime(ElapsedTime_mode iop)
74 switch (display_type)
76 case DISPLAY_RAW:
77 ElapsedTime_NOFIONREAD(iop);
78 break;
80 default:
81 ElapsedTime_FIONREAD(iop);
82 break;
88 #ifdef HAVE_GETTIMEOFDAY
89 void
90 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
92 long current_time;
93 int i;
94 int nchar;
96 struct timeval tv;
98 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
100 perror("FIONREAD");
101 fprintf(stderr,
102 "You probably have a non-ANSI <ioctl.h>; "
103 "see README. %d %d %x\n",
104 i, errno, FIONREAD);
105 exit(1);
108 if (nchar)
110 if (!flag.timeout)
111 flag.back = true;
113 flag.bothsides = false;
116 gettimeofday(&tv, NULL);
117 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
119 # ifdef INTERRUPT_TEST
120 if (iop == INIT_INTERRUPT_MODE)
122 itime0 = current_time;
124 else if (iop == COMPUTE_INTERRUPT_MODE)
126 it = current_time - itime0;
128 else
129 # endif
131 et = current_time - time0;
132 ETnodes = NodeCnt + znodes;
134 if (et < 0)
136 # ifdef INTERRUPT_TEST
137 printf("elapsed time %ld not positive\n", et);
138 # endif
139 et = 0;
142 if (iop == COMPUTE_AND_INIT_MODE)
144 if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
145 flag.timeout = true;
147 time0 = current_time;
150 if (!NOT_CURSES)
152 # ifdef QUIETBACKGROUND
153 if (!background)
154 # endif /* QUIETBACKGROUND */
155 UpdateClocks();
161 void
162 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
164 struct timeval tv;
165 long current_time;
167 gettimeofday(&tv, NULL);
168 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
170 # ifdef INTERRUPT_TEST
171 if (iop == INIT_INTERRUPT_MODE)
173 itime0 = current_time;
175 else if (iop == COMPUTE_INTERRUPT_MODE)
177 it = current_time - itime0;
179 else
180 # endif
182 et = current_time - time0;
183 ETnodes = NodeCnt + znodes;
185 if (et < 0)
187 # ifdef INTERRUPT_TEST
188 printf("elapsed time %ld not positive\n", et);
189 # endif
190 et = 0;
193 if (iop == COMPUTE_AND_INIT_MODE)
195 if ((et > ResponseTime + ExtraTime) && (Sdepth > MINDEPTH))
196 flag.timeout = true;
198 time0 = current_time;
201 if (!NOT_CURSES)
203 # ifdef QUIETBACKGROUND
204 if (!background)
205 # endif /* QUIETBACKGROUND */
206 UpdateClocks();
212 #else /* !HAVE_GETTIMEOFDAY */
216 * Determine the time that has passed since the search was started. If the
217 * elapsed time exceeds the target (ResponseTime + ExtraTime) then set
218 * timeout to true which will terminate the search.
220 * iop = 0 calculate et, bump ETnodes
221 * iop = 1 calculate et, set timeout if time exceeded, calculate et
225 void
226 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
228 long current_time;
229 int nchar;
230 int i;
232 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
234 perror("FIONREAD");
235 fprintf(stderr,
236 "You probably have a non-ANSI <ioctl.h>; "
237 "see README. %d %d %x\n",
238 i, errno, FIONREAD);
239 exit(1);
242 if (nchar)
244 if (!flag.timeout)
245 flag.back = true;
246 flag.bothsides = false;
249 et = ((current_time = time((long *) 0)) - time0) * 100;
251 #ifdef INTERRUPT_TEST
252 if (iop == INIT_INTERRUPT_MODE)
254 itime0 = current_time;
256 else if (iop == COMPUTE_INTERRUPT_MODE)
258 it = current_time - itime0;
260 else
261 #endif
263 ETnodes = NodeCnt + znodes;
265 if (et < 0)
267 #ifdef INTERRUPT_TEST
268 printf("elapsed time %ld not positive\n", et);
269 #endif
270 et = 0;
273 if (iop == COMPUTE_AND_INIT_MODE)
275 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
276 flag.timeout = true;
278 time0 = current_time;
281 if (!NOT_CURSES)
283 #ifdef QUIETBACKGROUND
284 if (!background)
285 #endif /* QUIETBACKGROUND */
286 UpdateClocks();
293 void
294 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
296 long current_time;
298 et = ((current_time = time((long *) 0)) - time0) * 100;
300 #ifdef INTERRUPT_TEST
301 if (iop == INIT_INTERRUPT_MODE)
303 itime0 = current_time;
305 else if (iop == COMPUTE_INTERRUPT_MODE)
307 it = current_time - itime0;
309 else
310 #endif
312 ETnodes = NodeCnt + znodes;
314 if (et < 0)
316 #ifdef INTERRUPT_TEST
317 printf("elapsed time %ld not positive\n", et);
318 #endif
319 et = 0;
322 if (iop == COMPUTE_AND_INIT_MODE)
324 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
325 flag.timeout = true;
327 time0 = current_time;
330 if (!NOT_CURSES)
332 #ifdef QUIETBACKGROUND
333 if (!background)
334 #endif /* QUIETBACKGROUND */
335 UpdateClocks();
341 #endif /* HAVE_GETTIMEOFDAY */