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