Updated all files to GPL version 3.
[xboard.git] / uci.c
1 /*
2  * UCI support thru Polyglot
3  *
4  * Author: Alessandro Scotti (Jan 2006)
5  *
6  * ------------------------------------------------------------------------
7  *
8  * GNU XBoard is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or (at
11  * your option) any later version.
12  *
13  * GNU XBoard is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see http://www.gnu.org/licenses/. 
20  *
21  * ------------------------------------------------------------------------
22  */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <malloc.h>
26
27 #ifdef WIN32
28 // [HGM] this was probably a Windows-specific constant. Needs to be defined here now I
29 //       threw out the Windows-specific includes (winboard.h etc.). 100 seems enough.
30 #include <windows.h>
31 #define SLASH_CHAR "\\"
32 #else
33 #define MAX_PATH 100
34 #define SLASH_CHAR "/"
35 #endif
36
37 #include "common.h"
38 #include "backend.h"
39
40 #define INIFILE_PREFIX      "polyglot_"
41 #define INIFILE_SUFFIX_1ST  "1st"
42 #define INIFILE_SUFFIX_2ND  "2nd"
43 #define INIFILE_EXT         ".ini"
44
45
46 static const char * GetIniFilename( ChessProgramState * cps )
47 {
48     return cps == &first ? INIFILE_PREFIX INIFILE_SUFFIX_1ST INIFILE_EXT : INIFILE_PREFIX INIFILE_SUFFIX_2ND INIFILE_EXT;
49  }
50
51 void InitEngineUCI( const char * iniDir, ChessProgramState * cps )
52 {
53     if( cps->isUCI ) {
54         const char * iniFileName = GetIniFilename( cps );
55         char polyglotIniFile[ MAX_PATH ];
56         FILE * f;
57
58         /* Build name of initialization file */
59         if( strchr( iniDir, ' ' ) != NULL ) {
60             char iniDirShort[ MAX_PATH ];
61 #ifdef WIN32
62             GetShortPathName( iniDir, iniDirShort, sizeof(iniDirShort) );
63
64             strcpy( polyglotIniFile, iniDirShort );
65 #else
66             // [HGM] UCI: not sure if this works, but GetShortPathName seems Windows pecific
67             // and names with spaces in it do not work in xboard in many places, so ignore
68             strcpy( polyglotIniFile, iniDir );
69 #endif
70         }
71         else {
72             strcpy( polyglotIniFile, iniDir );
73         }
74         
75         strcat( polyglotIniFile, SLASH_CHAR );
76         strcat( polyglotIniFile, iniFileName );
77
78         /* Create initialization file */
79         f = fopen( polyglotIniFile, "w" );
80
81         if( f != NULL ) {
82             fprintf( f, "[Polyglot]\n" );
83
84             if( cps->dir != 0 && strlen(cps->dir) > 0 ) {
85                 fprintf( f, "EngineDir = %s\n", cps->dir );
86             }
87
88             if( cps->program != 0 && strlen(cps->program) > 0 ) {
89                 fprintf( f, "EngineCommand = %s\n", cps->program );
90             }
91
92             fprintf( f, "Book = %s\n", appData.usePolyglotBook ? "true" : "false" );
93             fprintf( f, "BookFile = %s\n", appData.polyglotBook );
94         
95             fprintf( f, "[Engine]\n" );
96             fprintf( f, "Hash = %d\n", appData.defaultHashSize );
97
98             fprintf( f, "NalimovPath = %s\n", appData.defaultPathEGTB );
99             fprintf( f, "NalimovCache = %d\n", appData.defaultCacheSizeEGTB );
100
101             fprintf( f, "OwnBook = %s\n", cps->hasOwnBookUCI ? "true" : "false" );
102
103             fclose( f );
104
105             /* Replace program with properly configured Polyglot */
106             cps->dir = appData.polyglotDir;
107             cps->program = (char *) malloc( strlen(polyglotIniFile) + 32 );
108             strcpy( cps->program, "polyglot " );
109             strcat( cps->program, polyglotIniFile );
110         }
111     }
112 }