Make spacing consistent in *dsp.c, kill unused global hidden between funcs.
[gnushogi.git] / gnushogi / sysdeps.c
1 /*
2  * FILE: sysdeps.c
3  *
4  *     System-dependent functions for GNU Shogi.
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  *
10  * GNU SHOGI is based on GNU CHESS
11  *
12  * Copyright (c) 1988, 1989, 1990 John Stanback
13  * Copyright (c) 1992 Free Software Foundation
14  *
15  * This file is part of GNU SHOGI.
16  *
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.
21  *
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.
26  *
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  * ----------------------------------------------------------------------
31  *
32  */
33
34 #include "gnushogi.h"
35
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #if HAVE_SYS_FILIO_H
41 /* Definition of FIONREAD */
42 #include <sys/filio.h>
43 #endif
44
45 #if HAVE_ERRNO_H
46 /* Definition of errno(). */
47 #include <errno.h>
48 #endif
49
50 /* Forward declarations. */
51
52 void ElapsedTime_NOFIONREAD(ElapsedTime_mode iop);
53 void ElapsedTime_FIONREAD(ElapsedTime_mode iop);
54
55
56 /*
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
63  */
64
65 /*
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.
69  */
70
71 void
72 ElapsedTime(ElapsedTime_mode iop)
73 {
74     switch (display_type)
75     {
76     case DISPLAY_RAW:
77         ElapsedTime_NOFIONREAD(iop);
78         break;
79
80     default:
81         ElapsedTime_FIONREAD(iop);
82         break;
83     }
84 }
85
86
87 void
88 ElapsedTime_FIONREAD(ElapsedTime_mode iop)
89 {
90     long current_time;
91     int  i;
92     int  nchar;
93
94 #ifdef HAVE_GETTIMEOFDAY
95     struct timeval tv;
96 #endif
97
98     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
99     {
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);
106     }
107
108     if (nchar)
109     {
110         if (!flag.timeout)
111             flag.back = true;
112
113         flag.bothsides = false;
114     }
115
116 #ifdef HAVE_GETTIMEOFDAY
117     gettimeofday(&tv, NULL);
118     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
119 #else
120     et = ((current_time = time((long *) 0)) - time0) * 100;
121 #endif
122
123 #ifdef INTERRUPT_TEST
124     if (iop == INIT_INTERRUPT_MODE)
125     {
126         itime0 = current_time;
127     }
128     else if (iop == COMPUTE_INTERRUPT_MODE)
129     {
130         it = current_time - itime0;
131     }
132     else
133 #endif
134     {
135 #ifdef HAVE_GETTIMEOFDAY
136         et = current_time - time0;
137 #endif
138         ETnodes = NodeCnt + znodes;
139
140         if (et < 0)
141         {
142 #ifdef INTERRUPT_TEST
143             printf("elapsed time %ld not positive\n", et);
144 #endif
145             et = 0;
146         }
147
148         if (iop == COMPUTE_AND_INIT_MODE)
149         {
150             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
151                 flag.timeout = true;
152
153             time0 = current_time;
154         }
155
156         if (!NOT_CURSES)
157         {
158 #ifdef QUIETBACKGROUND
159             if (!background)
160 #endif
161                 UpdateClocks();
162         }
163     }
164 }
165
166
167 void
168 ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
169 {
170     long current_time;
171 #ifdef HAVE_GETTIMEOFDAY
172     struct timeval tv;
173     gettimeofday(&tv, NULL);
174     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
175 #else
176     et = ((current_time = time((long *) 0)) - time0) * 100;
177 #endif
178
179 #ifdef INTERRUPT_TEST
180     if (iop == INIT_INTERRUPT_MODE)
181     {
182         itime0 = current_time;
183     }
184     else if (iop == COMPUTE_INTERRUPT_MODE)
185     {
186         it = current_time - itime0;
187     }
188     else
189 #endif
190     {
191 #ifdef HAVE_GETTIMEOFDAY
192         et = current_time - time0;
193 #endif
194         ETnodes = NodeCnt + znodes;
195
196         if (et < 0)
197         {
198 #ifdef INTERRUPT_TEST
199             printf("elapsed time %ld not positive\n", et);
200 #endif
201             et = 0;
202         }
203
204         if (iop == COMPUTE_AND_INIT_MODE)
205         {
206             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
207                 flag.timeout = true;
208
209             time0 = current_time;
210         }
211
212         if (!NOT_CURSES)
213         {
214 #ifdef QUIETBACKGROUND
215             if (!background)
216 #endif
217                 UpdateClocks();
218         }
219     }
220 }