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