/************************* UCI2WB by H.G.Muller ****************************/\r
\r
-#define VERSION "1.5"\r
+#define VERSION "1.6"\r
\r
#include <stdio.h>\r
#include <stdlib.h>\r
-#ifdef WIN32
-# include <windows.h>
+#ifdef WIN32\r
+# include <windows.h>\r
# include <io.h>\r
HANDLE process;\r
DWORD thread_id;\r
-#else
-# include <pthread.h>
-# include <signal.h>
-# define NO_ERROR 0
+#else\r
+# include <pthread.h>\r
+# include <signal.h>\r
+# define NO_ERROR 0\r
# include <sys/time.h>\r
int GetTickCount() // with thanks to Tord\r
{ struct timeval t; gettimeofday(&t, NULL); return t.tv_sec*1000 + t.tv_usec/1000; }\r
#include <fcntl.h>\r
#include <string.h>\r
\r
-#ifdef _MSC_VER \r
-#define SLEEP() Sleep(1) \r
-#else \r
-#define SLEEP() usleep(10) \r
-#endif \r
-\r
// Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".)\r
# define VARIANTS "normal,xiangqi"\r
\r
+#define DPRINT if(debug) printf\r
+\r
#define WHITE 0\r
#define BLACK 1\r
#define NONE 2\r
#define ANALYZE 3\r
\r
char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix;\r
-int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime;\r
+int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug;\r
int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode\r
\r
FILE *toE, *fromE;\r
int pid;\r
\r
+#ifdef WIN32\r
+WinPipe(HANDLE *hRd, HANDLE *hWr)\r
+{\r
+ SECURITY_ATTRIBUTES saAttr;\r
+\r
+ /* Set the bInheritHandle flag so pipe handles are inherited. */\r
+ saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);\r
+ saAttr.bInheritHandle = TRUE;\r
+ saAttr.lpSecurityDescriptor = NULL;\r
+\r
+ /* Create a pipe */\r
+ return CreatePipe(hRd, hWr, &saAttr, 0);\r
+}\r
+#endif\r
+\r
+#define INIT 0\r
+#define WAKEUP 1\r
+#define PAUSE 2\r
+\r
+void\r
+Sync (int action)\r
+{\r
+#ifdef WIN32\r
+ static HANDLE hWr, hRd; DWORD d; char c;\r
+ switch(action) {\r
+ case INIT: WinPipe(&hRd, &hWr); break;\r
+ case WAKEUP: WriteFile(hWr, "\n", 1, &d, NULL); break;\r
+ case PAUSE: ReadFile(hRd, &c, 1, &d, NULL);\r
+ }\r
+#else\r
+ static int syncPipe[2]; char c;\r
+ switch(action) {\r
+ case INIT: pipe(syncPipe); break;\r
+ case WAKEUP: write(syncPipe[1], "\n", 1); break;\r
+ case PAUSE: read(syncPipe[0], &c, 1);\r
+ }\r
+#endif\r
+}\r
+\r
void\r
StartSearch(char *ponder)\r
{ // send the 'go' command to engine. Suffix by ponder.\r
int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead!\r
fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
- printf( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
+ DPRINT( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime);\r
if(sTime > 0) fprintf(toE, " movetime %d", sTime),printf(" movetime %d", sTime); else\r
if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2);\r
if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc);\r
{\r
if(!pondering) return;\r
pause = 1;\r
- fprintf(toE, "stop\n");printf("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove'\r
- while(pause) SLEEP(); // wait for engine to acknowledge 'stop' with 'bestmove'.\r
+ fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove'\r
+ Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'.\r
}\r
\r
void\r
{\r
int j;\r
fprintf(toE, "%s moves", iniPos);\r
- printf( "# %s moves", iniPos);\r
+ DPRINT( "# %s moves", iniPos);\r
for(j=0; j<moveNr; j++) fprintf(toE, " %s", move[j]),printf(" %s", move[j]);\r
}\r
\r
while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++;\r
line[++i] = 0;\r
if(x == EOF) exit(0);\r
-printf("# engine said: %s", line);\r
+ DPRINT("# engine said: %s", line); fflush(stdout);\r
sscanf(line, "%s", command);\r
if(!strcmp(command, "bestmove")) {\r
- if(pause) { pondering = pause = 0; continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
+ if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
// move was a move to be played\r
if(strstr(line+9, "resign")) { printf("resign\n"); computer = NONE; }\r
if(strstr(line+9, "(none)") || strstr(line+9, "null") ||\r
if(p = strstr(line+9, "ponder")) {\r
if(computer != NONE && ponder) {\r
sscanf(p+7, "%s", move[moveNr]);\r
-printf("# ponder on %s\n", move[moveNr]);\r
+ DPRINT("# ponder on %s\n", move[moveNr]);\r
LoadPos(moveNr+1); // load position\r
// and set engine pondering\r
pondering = 1; lastDepth = 1;\r
}\r
else if(!strcmp(command, "id")) {\r
char name[256];\r
- if(sscanf(line, "id name %s", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32);\r
+ if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32);\r
}\r
- else if(!strcmp(command, "readyok")) pause = 0; // resume processing of GUI commands\r
+ else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands\r
else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash); // done with options\r
}\r
}\r
int i, x;\r
\r
if(computer == stm || computer == ANALYZE) {\r
-printf("# start search\n");\r
+ DPRINT("# start search\n");\r
LoadPos(moveNr); // load position\r
// and set engine thinking (note USI swaps colors!)\r
startTime = GetTickCount();\r
nomove:\r
fflush(toE); fflush(stdout);\r
i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++;\r
- line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); }
+ line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); }\r
sscanf(line, "%s", command);\r
- while(pause) SLEEP(); // wait for readyok\r
if(!strcmp(command, "new")) {\r
computer = BLACK; moveNr = 0; depth = -1;\r
stm = WHITE; strcpy(iniPos, "position startpos");\r
// we can set other options here\r
pause = 1; // wait for option settings to take effect\r
fprintf(toE, "isready\n");\r
- fprintf(toE, "u%cinewgame\n", sc);\r
+ fprintf(toE, "u%cinewgame\n", sc); fflush(toE);\r
+ Sync(PAUSE); // wait for readyok\r
}\r
else if(!strcmp(command, "usermove")) {\r
if(sc == 's') {\r
if(pondering || computer == ANALYZE) {\r
if(pondering && !strcmp(command, move[moveNr])) { // ponder hit\r
pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now\r
- fprintf(toE, "ponderhit\n"); printf("# ponderhit\n");\r
+ fprintf(toE, "ponderhit\n"); DPRINT("# ponderhit\n");\r
goto nomove;\r
}\r
StopPonder(1);\r
}\r
else if(!strcmp(command, "option")) {\r
char name[80], *p;\r
+ if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else\r
if(p = strchr(line, '=')) {\r
*p++ = 0;\r
if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false");\r
- fprintf(toE, "setoption name %s value %s", line+7, p), printf("# setoption name %s value %s", line+7, p);\r
- } else fprintf(toE, "setoption name %s\n", line+7), printf("# setoption name %s\n", line+7);\r
+ fprintf(toE, "setoption name %s value %s", line+7, p); DPRINT("# setoption name %s value %s", line+7, p);\r
+ } else { fprintf(toE, "setoption name %s\n", line+7); DPRINT("# setoption name %s\n", line+7); }\r
}\r
else if(!strcmp(command, "protover")) {\r
- printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS);\r
+ printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS);\r
+ printf("feature option=\"UCI2WB debug output -check %d\"\n", debug);\r
fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options\r
}\r
else if(!strcmp(command, "setboard")) {\r
else if(!strcmp(command, "nopost")) post = 0;\r
else if(!strcmp(command, "easy")) ponder = 0;\r
else if(!strcmp(command, "hard")) ponder = 1;\r
+ else if(!strcmp(command, "ping")) pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5);\r
else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory);\r
else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores);\r
else if(!strcmp(command, "sd")) sscanf(line, "sd %d", &depth);\r
\r
int\r
StartEngine(char *cmdLine, char *dir)\r
-{
+{\r
#ifdef WIN32\r
HANDLE hChildStdinRd, hChildStdinWr,\r
hChildStdoutRd, hChildStdoutWr;\r
- SECURITY_ATTRIBUTES saAttr;\r
BOOL fSuccess;\r
PROCESS_INFORMATION piProcInfo;\r
STARTUPINFO siStartInfo;\r
DWORD err;\r
\r
- /* Set the bInheritHandle flag so pipe handles are inherited. */\r
- saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);\r
- saAttr.bInheritHandle = TRUE;\r
- saAttr.lpSecurityDescriptor = NULL;\r
-\r
/* Create a pipe for the child's STDOUT. */\r
- if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) return GetLastError();\r
+ if (! WinPipe(&hChildStdoutRd, &hChildStdoutWr)) return GetLastError();\r
\r
/* Create a pipe for the child's STDIN. */\r
- if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) return GetLastError();\r
+ if (! WinPipe(&hChildStdinRd, &hChildStdinWr)) return GetLastError();\r
\r
SetCurrentDirectory(dir); // go to engine directory\r
\r
pid = piProcInfo.dwProcessId;\r
fromE = (FILE*) _fdopen( _open_osfhandle((long)hChildStdoutRd, _O_TEXT|_O_RDONLY), "r");\r
toE = (FILE*) _fdopen( _open_osfhandle((long)hChildStdinWr, _O_WRONLY), "w");\r
-#else
- char *argv[10], *p, buf[200];
- int i, toEngine[2], fromEngine[2];
-
- if (dir && dir[0] && chdir(dir)) { perror(dir); exit(1); }
- pipe(toEngine); pipe(fromEngine); // create two pipes
-
- if ((pid = fork()) == 0) { // Child
- dup2(toEngine[0], 0); close(toEngine[0]); close(toEngine[1]); // stdin from toE pipe
- dup2(fromEngine[1], 1); close(fromEngine[0]); close(fromEngine[1]); // stdout into fromE pipe
- dup2(1, fileno(stderr)); // stderr into frome pipe
-
- strcpy(buf, cmdLine); p = buf;
- for (i=0;;) { argv[i++] = p; p = strchr(p, ' '); if (p == NULL) break; *p++ = 0; }
- argv[i] = NULL;
- execvp(argv[0], argv); // startup engine
-
- perror(argv[0]); exit(1); // could not start engine; quit.
- }
- signal(SIGPIPE, SIG_IGN);
- close(toEngine[0]); close(fromEngine[1]); // close engine ends of pipes in adapter
-
- fromE = (FILE*) fdopen(fromEngine[0], "r"); // make into high-level I/O
- toE = (FILE*) fdopen(toEngine[1], "w");
+#else\r
+ char *argv[10], *p, buf[200];\r
+ int i, toEngine[2], fromEngine[2];\r
+\r
+ if (dir && dir[0] && chdir(dir)) { perror(dir); exit(1); }\r
+ pipe(toEngine); pipe(fromEngine); // create two pipes\r
+\r
+ if ((pid = fork()) == 0) { // Child\r
+ dup2(toEngine[0], 0); close(toEngine[0]); close(toEngine[1]); // stdin from toE pipe\r
+ dup2(fromEngine[1], 1); close(fromEngine[0]); close(fromEngine[1]); // stdout into fromE pipe\r
+ dup2(1, fileno(stderr)); // stderr into frome pipe\r
+\r
+ strcpy(buf, cmdLine); p = buf;\r
+ for (i=0;;) { argv[i++] = p; p = strchr(p, ' '); if (p == NULL) break; *p++ = 0; }\r
+ argv[i] = NULL;\r
+ execvp(argv[0], argv); // startup engine\r
+ \r
+ perror(argv[0]); exit(1); // could not start engine; quit.\r
+ }\r
+ signal(SIGPIPE, SIG_IGN);\r
+ close(toEngine[0]); close(fromEngine[1]); // close engine ends of pipes in adapter\r
+ \r
+ fromE = (FILE*) fdopen(fromEngine[0], "r"); // make into high-level I/O\r
+ toE = (FILE*) fdopen(toEngine[1], "w");\r
#endif\r
return NO_ERROR;\r
}\r
char *dir = NULL, *p, *q; int e;\r
\r
if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); }\r
+ if(argc > 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; }\r
if(argc > 1 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; }\r
- if(argc < 2) { printf("usage is: U%cI2WB [-s] <engine.exe> [<engine directory>]\n", sc-32); exit(-1); }\r
+ if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] <engine.exe> [<engine directory>]\n", sc-32); exit(-1); }\r
if(argc > 2) dir = argv[2];\r
if(argc > 3) suffix = argv[3];\r
\r
// spawn engine proc\r
if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); }\r
\r
+ Sync(INIT);\r
+\r
// create separate thread to handle engine->GUI traffic\r
-#ifdef WIN32
- CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id);
-#else
- { pthread_t t; signal(SIGINT, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); }
+#ifdef WIN32\r
+ CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id);\r
+#else\r
+ { pthread_t t; signal(SIGINT, SIG_IGN); pthread_create(&t, NULL, Engine2GUI, NULL); }\r
#endif\r
\r
// handle GUI->engine traffic in original thread\r