Updated copyright notice to 2015
[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  * Enhancement Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
9  *
10  * ------------------------------------------------------------------------
11  *
12  * GNU XBoard is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or (at
15  * your option) any later version.
16  *
17  * GNU XBoard is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see http://www.gnu.org/licenses/.
24  *
25  * ------------------------------------------------------------------------
26  */
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "common.h"
33 #include "backend.h"
34 Boolean GetArgValue(char *a);
35
36 void
37 InitEngineUCI (const char *iniDir, ChessProgramState *cps)
38 {   // replace engine command line by adapter command with expanded meta-symbols
39     if( cps->isUCI ) {
40         char *p, *q;
41         char polyglotCommand[MSG_SIZ];
42
43         if(cps->isUCI == 2) p = appData.ucciAdapter; else
44         p = appData.adapterCommand;
45         q = polyglotCommand;
46         while(*p) {
47           if(*p == '\\') p++; else
48           if(*p == '%') { // substitute marker
49             char argName[MSG_SIZ], buf[MSG_SIZ], *s = buf;
50             if(*++p == '%') { // second %, expand as f or s in option name (e.g. %%cp -> fcp)
51               *s++ = cps == &first ? 'f' : 's';
52               p++;
53             }
54             while(isdigit(*p) || isalpha(*p)) *s++ = *p++; // copy option name
55             *s = NULLCHAR;
56             if(cps == &second) { // change options for first into those for second engine
57               if(strstr(buf, "first") == buf) sprintf(argName, "second%s", buf+5); else
58               if(buf[0] == 'f') sprintf(argName, "s%s", buf+1); else
59                 safeStrCpy(argName, buf, sizeof(argName)/sizeof(argName[0]));
60             } else safeStrCpy(argName, buf, sizeof(argName)/sizeof(argName[0]));
61             if(GetArgValue(argName)) { // look up value of option with this name
62               s = argName;
63               while(*s) *q++ = *s++;
64             } else DisplayFatalError("Bad adapter command", 0, 1);
65             continue;
66           }
67           if(*p) *q++ = *p++;
68         }
69         *q = NULLCHAR;
70         cps->program = StrSave(polyglotCommand);
71         cps->dir = appData.polyglotDir;
72     }
73 }