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