A better and more flexible way of invoking Polyglot
[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 #include "common.h"
33 #include "backend.h"
34
35
36 void InitEngineUCI( const char * iniDir, ChessProgramState * cps )
37 {   // replace engine command line by adapter command with expanded meta-symbols
38     if( cps->isUCI ) {
39         char *p, *q;
40         char polyglotCommand[MSG_SIZ];
41
42         p = appData.adapterCommand;
43         q = polyglotCommand;
44         while(*p) {
45           if(*p == '\\') p++; else
46           if(*p == '%') { // substitute marker
47             char argName[MSG_SIZ], *s = argName;
48             if(*++p == '%') { // second %, expand as f or s in option name (e.g. %%cp -> fcp)
49               *s++ = cps == &first ? 'f' : 's';
50               p++;
51             }
52             while(*p != ' ' && *p) *s++ = *p++; // copy option name
53             *s = NULLCHAR;
54             if(GetArgValue(argName)) { // look up value of option with this name
55               s = argName;
56               while(*s) *q++ = *s++;
57             } else DisplayFatalError("Bad adapter command", 0, 1);
58             continue;
59           }
60           if(*p) *q++ = *p++;
61         }
62         *q = NULLCHAR;
63         cps->program = StrSave(polyglotCommand);
64         cps->dir = appData.polyglotDir;
65     }
66 }