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