*
* Author: Alessandro Scotti (Jan 2006)
*
+ * Copyright 2006 Alessandro Scotti
+ *
+ * Enhancement Copyright 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
+ *
* ------------------------------------------------------------------------
*
* GNU XBoard is free software: you can redistribute it and/or modify
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program. If not, see http://www.gnu.org/licenses/.
+ * along with this program. If not, see http://www.gnu.org/licenses/.
*
* ------------------------------------------------------------------------
*/
#include <stdio.h>
+#include <ctype.h>
#include <stdlib.h>
-#include <malloc.h>
-
-#ifdef WIN32
-// [HGM] this was probably a Windows-specific constant. Needs to be defined here now I
-// threw out the Windows-specific includes (winboard.h etc.). 100 seems enough.
-#include <windows.h>
-#define SLASH_CHAR "\\"
-#else
-#define MAX_PATH 100
-#define SLASH_CHAR "/"
-#endif
+#include <string.h>
#include "common.h"
#include "backend.h"
+Boolean GetArgValue(char *a);
-#define INIFILE_PREFIX "polyglot_"
-#define INIFILE_SUFFIX_1ST "1st"
-#define INIFILE_SUFFIX_2ND "2nd"
-#define INIFILE_EXT ".ini"
-
-
-static const char * GetIniFilename( ChessProgramState * cps )
-{
- return cps == &first ? INIFILE_PREFIX INIFILE_SUFFIX_1ST INIFILE_EXT : INIFILE_PREFIX INIFILE_SUFFIX_2ND INIFILE_EXT;
- }
-
-void InitEngineUCI( const char * iniDir, ChessProgramState * cps )
-{
+void
+InitEngineUCI (const char *iniDir, ChessProgramState *cps)
+{ // replace engine command line by adapter command with expanded meta-symbols
if( cps->isUCI ) {
- const char * iniFileName = GetIniFilename( cps );
- char polyglotIniFile[ MAX_PATH ];
- FILE * f;
-
- /* Build name of initialization file */
- if( strchr( iniDir, ' ' ) != NULL ) {
- char iniDirShort[ MAX_PATH ];
-#ifdef WIN32
- GetShortPathName( iniDir, iniDirShort, sizeof(iniDirShort) );
-
- strcpy( polyglotIniFile, iniDirShort );
-#else
- // [HGM] UCI: not sure if this works, but GetShortPathName seems Windows pecific
- // and names with spaces in it do not work in xboard in many places, so ignore
- strcpy( polyglotIniFile, iniDir );
-#endif
- }
- else {
- strcpy( polyglotIniFile, iniDir );
- }
-
- strcat( polyglotIniFile, SLASH_CHAR );
- strcat( polyglotIniFile, iniFileName );
-
- /* Create initialization file */
- f = fopen( polyglotIniFile, "w" );
-
- if( f != NULL ) {
- fprintf( f, "[Polyglot]\n" );
-
- if( cps->dir != 0 && strlen(cps->dir) > 0 ) {
- fprintf( f, "EngineDir = %s\n", cps->dir );
- }
-
- if( cps->program != 0 && strlen(cps->program) > 0 ) {
- fprintf( f, "EngineCommand = %s\n", cps->program );
+ char *p, *q;
+ char polyglotCommand[MSG_SIZ];
+
+ if(cps->isUCI == 2) p = appData.ucciAdapter; else
+ p = appData.adapterCommand;
+ q = polyglotCommand;
+ while(*p) {
+ if(*p == '\\') p++; else
+ if(*p == '%') { // substitute marker
+ char argName[MSG_SIZ], buf[MSG_SIZ], *s = buf;
+ if(*++p == '%') { // second %, expand as f or s in option name (e.g. %%cp -> fcp)
+ *s++ = cps == &first ? 'f' : 's';
+ p++;
}
-
- fprintf( f, "Book = %s\n", appData.usePolyglotBook ? "true" : "false" );
- fprintf( f, "BookFile = %s\n", appData.polyglotBook );
-
- fprintf( f, "[Engine]\n" );
- fprintf( f, "Hash = %d\n", appData.defaultHashSize );
-
- fprintf( f, "NalimovPath = %s\n", appData.defaultPathEGTB );
- fprintf( f, "NalimovCache = %d\n", appData.defaultCacheSizeEGTB );
-
- fprintf( f, "OwnBook = %s\n", cps->hasOwnBookUCI ? "true" : "false" );
-
- fclose( f );
-
- /* Replace program with properly configured Polyglot */
- cps->dir = appData.polyglotDir;
- cps->program = (char *) malloc( strlen(polyglotIniFile) + 32 );
- strcpy( cps->program, "polyglot " );
- strcat( cps->program, polyglotIniFile );
+ while(isdigit(*p) || isalpha(*p)) *s++ = *p++; // copy option name
+ *s = NULLCHAR;
+ if(cps == &second) { // change options for first into those for second engine
+ if(strstr(buf, "first") == buf) sprintf(argName, "second%s", buf+5); else
+ if(buf[0] == 'f') sprintf(argName, "s%s", buf+1); else
+ safeStrCpy(argName, buf, sizeof(argName)/sizeof(argName[0]));
+ } else safeStrCpy(argName, buf, sizeof(argName)/sizeof(argName[0]));
+ if(GetArgValue(argName)) { // look up value of option with this name
+ s = argName;
+ while(*s) *q++ = *s++;
+ } else DisplayFatalError("Bad adapter command", 0, 1);
+ continue;
+ }
+ if(*p) *q++ = *p++;
}
+ *q = NULLCHAR;
+ cps->program = StrSave(polyglotCommand);
+ cps->dir = appData.polyglotDir;
}
}