removed check and #includes for malloc.h, since it's not needed
[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 #include <string.h>
28
29 #include "common.h"
30 #include "backend.h"
31 Boolean GetArgValue(char *a);                           
32
33 void InitEngineUCI( const char * iniDir, ChessProgramState * cps )
34 {   // replace engine command line by adapter command with expanded meta-symbols
35     if( cps->isUCI ) {
36         char *p, *q;
37         char polyglotCommand[MSG_SIZ];
38
39         p = appData.adapterCommand;
40         q = polyglotCommand;
41         while(*p) {
42           if(*p == '\\') p++; else
43           if(*p == '%') { // substitute marker
44             char argName[MSG_SIZ], buf[MSG_SIZ], *s = buf;
45             if(*++p == '%') { // second %, expand as f or s in option name (e.g. %%cp -> fcp)
46               *s++ = cps == &first ? 'f' : 's';
47               p++;
48             }
49             while(*p >= '0' && *p) *s++ = *p++; // copy option name
50             *s = NULLCHAR;
51             if(cps == &second) { // change options for first into those for second engine
52               if(strstr(buf, "first") == buf) sprintf(argName, "second%s", buf+5); else
53               if(buf[0] == 'f') sprintf(argName, "s%s", buf+1); else
54                 safeStrCpy(argName, buf, sizeof(argName)/sizeof(argName[0]));
55             } else safeStrCpy(argName, buf, sizeof(argName)/sizeof(argName[0]));
56             if(GetArgValue(argName)) { // look up value of option with this name
57               s = argName;
58               while(*s) *q++ = *s++;
59             } else DisplayFatalError("Bad adapter command", 0, 1);
60             continue;
61           }
62           if(*p) *q++ = *p++;
63         }
64         *q = NULLCHAR;
65         cps->program = StrSave(polyglotCommand);
66         cps->dir = appData.polyglotDir;
67     }
68 }