X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=fa37c4232684d8bf77f28a0119f0c4f8307e350e;hb=bb98ac73064aee6dcae34c236e466abf4b5dd6a3;hp=c6eb94960bce870a672ea9a578fed0c769bc1d0c;hpb=d16786b3fc9f28a8d3a0f38a2b8158ab7381e232;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index c6eb949..fa37c42 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -1,48 +1,94 @@ /************************* UCI2WB by H.G.Muller ****************************/ -#define VERSION "1.5" +#define VERSION "1.7" #include -#include -#include +#include +#ifdef WIN32 +# include +# include + HANDLE process; + DWORD thread_id; +#else +# include +# include +# define NO_ERROR 0 +# include + int GetTickCount() // with thanks to Tord + { struct timeval t; gettimeofday(&t, NULL); return t.tv_sec*1000 + t.tv_usec/1000; } +#endif #include - -#ifdef _MSC_VER -#define SLEEP() Sleep(1) -#else -#define SLEEP() usleep(10) -#endif +#include // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".) # define VARIANTS "normal,xiangqi" +#define DPRINT if(debug) printf + #define WHITE 0 #define BLACK 1 #define NONE 2 #define ANALYZE 3 -char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix; -int mps, tc, inc, stime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime; +char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix, *variants; +int mps, tc, inc, sTime, depth, myTime, hisTime, stm, computer = NONE, memory, oldMem=0, cores, moveNr, lastDepth, lastScore, startTime, debug; int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode FILE *toE, *fromE; -HANDLE process; int pid; -DWORD thread_id; + +#ifdef WIN32 +WinPipe(HANDLE *hRd, HANDLE *hWr) +{ + SECURITY_ATTRIBUTES saAttr; + + /* Set the bInheritHandle flag so pipe handles are inherited. */ + saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); + saAttr.bInheritHandle = TRUE; + saAttr.lpSecurityDescriptor = NULL; + + /* Create a pipe */ + return CreatePipe(hRd, hWr, &saAttr, 0); +} +#endif + +#define INIT 0 +#define WAKEUP 1 +#define PAUSE 2 + +void +Sync (int action) +{ +#ifdef WIN32 + static HANDLE hWr, hRd; DWORD d; char c; + switch(action) { + case INIT: WinPipe(&hRd, &hWr); break; + case WAKEUP: WriteFile(hWr, "\n", 1, &d, NULL); break; + case PAUSE: ReadFile(hRd, &c, 1, &d, NULL); + } +#else + static int syncPipe[2]; char c; + switch(action) { + case INIT: pipe(syncPipe); break; + case WAKEUP: write(syncPipe[1], "\n", 1); break; + case PAUSE: read(syncPipe[0], &c, 1); + } +#endif +} void StartSearch(char *ponder) { // send the 'go' command to engine. Suffix by ponder. int nr = moveNr + (ponder[0] != 0); // we ponder for one move ahead! fprintf(toE, "\ngo btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); - printf( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); - if(stime > 0) fprintf(toE, " movetime %d", stime),printf(" movetime %d", stime); else - if(mps) fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2),printf(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); - if(inc && !suffix) fprintf(toE, " winc %d binc %d", inc, inc),printf(" winc %d binc %d", inc, inc); - if(depth > 0) fprintf(toE, " depth %d", depth),printf(" depth %d", depth); - if(suffix) fprintf(toE, suffix, inc),printf(suffix, inc); + DPRINT( "\n# go btime %d wtime %d", stm == BLACK ^ sc=='s' ? myTime : hisTime, stm == WHITE ^ sc=='s' ? myTime : hisTime); + if(sTime > 0) { fprintf(toE, " movetime %d", sTime); DPRINT(" movetime %d", sTime); } else + if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); } + if(inc && !suffix) { fprintf(toE, " winc %d binc %d", inc, inc); DPRINT(" winc %d binc %d", inc, inc); } + if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); } + if(suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); } fprintf(toE, "%s\n", ponder); - printf("%s\n", ponder); + DPRINT("%s\n", ponder); } void @@ -50,8 +96,8 @@ StopPonder(int pondering) { if(!pondering) return; pause = 1; - fprintf(toE, "stop\n");printf("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' - while(pause) SLEEP(); // wait for engine to acknowledge 'stop' with 'bestmove'. + fprintf(toE, "stop\n"); fflush(toE); DPRINT("# stop\n"); // note: 'pondering' remains set until engine acknowledges 'stop' with 'bestmove' + Sync(PAUSE); // wait for engine to acknowledge 'stop' with 'bestmove'. } void @@ -59,8 +105,8 @@ LoadPos(int moveNr) { int j; fprintf(toE, "%s moves", iniPos); - printf( "# %s moves", iniPos); - for(j=0; j'0'+size-size/3 || m[3]>'0'+size-size/3) + : (m[1] <= '0'+size/3 || m[3] <= '0'+size/3)) && m[4] != '+') + m[4] = '=', m[5] = 0; + } + } + if(m[0] == 'e' && (m[5] == 'a' || m[5] == 'h')) // seirawan castling + gating at Rook + m[0] = m[5], m[2] = 'e'; // convert to RxK notation +} + +void * Engine2GUI() { char line[1024], command[256]; while(1) { - int i=0, x; char *p; + int i=0, x; char *p, dummy; fflush(stdout); fflush(toE); while((line[i] = x = fgetc(fromE)) != EOF && line[i] != '\n') i++; line[++i] = 0; if(x == EOF) exit(0); -printf("# engine said: %s", line); + DPRINT("# engine said: %s", line), fflush(stdout); sscanf(line, "%s", command); if(!strcmp(command, "bestmove")) { - if(pause) { pondering = pause = 0; continue; } // bestmove was reply to ponder miss or analysis result; ignore. + if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore. // move was a move to be played if(strstr(line+9, "resign")) { printf("resign\n"); computer = NONE; } if(strstr(line+9, "(none)") || strstr(line+9, "null") || strstr(line+9, "0000")) { printf("%s\n", lastScore < -99999 ? "resign" : "1/2-1/2 {stalemate}"); computer = NONE; } sscanf(line, "bestmove %s", move[moveNr++]); myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder - if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(stime) myTime = stime; // new session or move starts + if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(sTime) myTime = sTime; // new session or move starts // first start a new ponder search, if pondering is on and we have a move to ponder on if(p = strstr(line+9, "ponder")) { if(computer != NONE && ponder) { sscanf(p+7, "%s", move[moveNr]); -printf("# ponder on %s\n", move[moveNr]); + DPRINT("# ponder on %s\n", move[moveNr]); LoadPos(moveNr+1); // load position // and set engine pondering pondering = 1; lastDepth = 1; @@ -111,20 +178,7 @@ printf("# ponder on %s\n", move[moveNr]); } p[-1] = '\n'; *p = 0; // strip off ponder move } - if(sc == 's') { - // convert USI move to WB format - line[11] = 'a'+'0'+size - line[11]; - line[12] = 'a'+'0'+size - line[12]; - if(line[10] == '*') { // drop - line[10] = '@'; - } else { - line[9] = 'a'+'0'+size - line[9]; - line[10] = 'a'+'0'+size - line[10]; - if((stm == WHITE ? (line[10]>'0'+size-size/3 || line[12]>'0'+size-size/3) - : (line[10] <= '0'+size/3 || line[12] <= '0'+size/3)) && line[13] != '+') - line[13] = '=', line[14] = 0; - } - } + Move4GUI(line+9); printf("move %s\n", line+9); // send move to GUI if(lastScore == 100001 && iniPos[0] != 'f') { printf("%s {mate}\n", stm == WHITE ? "1-0" : "0-1"); computer = NONE; } stm = WHITE+BLACK - stm; @@ -132,8 +186,9 @@ printf("# ponder on %s\n", move[moveNr]); else if(!strcmp(command, "info")) { int d=0, s=0, t=0, n=0; char *pv; + if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; } if(!post) continue; - if(strstr(line, "info string ") == line) printf("%d 0 0 0 %s", lastDepth, line+12); else { + if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else { if(p = strstr(line+4, " depth ")) sscanf(p+7, "%d", &d), statDepth = d; if(p = strstr(line+4, " score cp ")) sscanf(p+10, "%d", &s), statScore = s; else if(p = strstr(line+4, " score mate ")) sscanf(p+12, "%d", &s), s += s>0 ? 100000 : -100000, statScore = s; else @@ -181,23 +236,46 @@ printf("# ponder on %s\n", move[moveNr]); } else if(!strcmp(command, "id")) { char name[256]; - if(sscanf(line, "id name %s", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32); + if(sscanf(line, "id name %[^\n]", name) == 1) printf("feature myname=\"%s (U%cI2WB)\"\n", name, sc-32); } - else if(!strcmp(command, "readyok")) pause = 0; // resume processing of GUI commands - else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash); // done with options + else if(!strcmp(command, "readyok")) { pause = 0; Sync(WAKEUP); } // resume processing of GUI commands + else if(sscanf(command, "u%ciok", &c)==1 && c==sc) printf("feature smp=1 memory=%d done=1\n", hasHash), Sync(WAKEUP); // done with options + } +} + +void +Move4Engine(char *m) +{ + if(sc == 's') { + // convert input move to USI format + if(m[1] == '@') { // drop + m[1] = '*'; + } else { + m[0] = 'a'+'0'+size - m[0]; + m[1] = 'a'+'0'+size - m[1]; + } + m[2] = 'a'+'0'+size - m[2]; + m[3] = 'a'+'0'+size - m[3]; + if(m[4] == '=') m[4] = 0; // no '=' in USI format! + else if(m[4] != '\n') m[4] = '+'; // cater to WB 4.4 bug :-( + } + if(m[4] && (m[1] == '1' || m[1] == '8')) { // seirawan gating + m[5] = m[0], m[6] = m[1], m[7] = '\0'; // copy from-square behind it, as gating square + if(m[2] == 'e' && (m[0] == 'a' || m[0] == 'h')) + m[2] = (m[0]+m[2]+1)>>1, m[0] = 'e'; // gating at Rook } } void GUI2Engine() { - char line[256], command[256], *p, *q; + char line[256], command[256], *p, *q, *r; while(1) { int i, x; if(computer == stm || computer == ANALYZE) { -printf("# start search\n"); + DPRINT("# start search\n"); LoadPos(moveNr); // load position // and set engine thinking (note USI swaps colors!) startTime = GetTickCount(); @@ -209,7 +287,6 @@ printf("# start search\n"); i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); } sscanf(line, "%s", command); - while(pause) SLEEP(); // wait for readyok if(!strcmp(command, "new")) { computer = BLACK; moveNr = 0; depth = -1; stm = WHITE; strcpy(iniPos, "position startpos"); @@ -218,59 +295,55 @@ printf("# start search\n"); // we can set other options here pause = 1; // wait for option settings to take effect fprintf(toE, "isready\n"); - fprintf(toE, "u%cinewgame\n", sc); + fprintf(toE, "u%cinewgame\n", sc); fflush(toE); + Sync(PAUSE); // wait for readyok } else if(!strcmp(command, "usermove")) { - if(sc == 's') { - // convert input move to USI format - if(line[10] == '@') { // drop - line[10] = '*'; - } else { - line[9] = 'a'+'0'+size - line[9]; - line[10] = 'a'+'0'+size - line[10]; - } - line[11] = 'a'+'0'+size - line[11]; - line[12] = 'a'+'0'+size - line[12]; - if(line[13] == '=') line[13] = 0; // no '=' in USI format! - else if(line[13] != '\n') line[13] = '+'; // cater to WB 4.4 bug :-( - } sscanf(line, "usermove %s", command); // strips off linefeed + Move4Engine(command); stm = WHITE+BLACK - stm; // when pondering we either continue the ponder search as normal search, or abort it if(pondering || computer == ANALYZE) { if(pondering && !strcmp(command, move[moveNr])) { // ponder hit pondering = 0; moveNr++; startTime = GetTickCount(); // clock starts running now - fprintf(toE, "ponderhit\n"); printf("# ponderhit\n"); + fprintf(toE, "ponderhit\n"); DPRINT("# ponderhit\n"); goto nomove; } StopPonder(1); } - sscanf(line, "usermove %s", move[moveNr++]); // possibly overwrites ponder move + strcpy(move[moveNr++], command); // possibly overwrites ponder move } else if(!strcmp(command, "level")) { int sec = 0; sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 || sscanf(line, "level %d %d %d", &mps, &tc, &inc); - tc = (60*tc + sec)*1000; inc *= 1000; stime = 0; + tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0; } else if(!strcmp(command, "option")) { char name[80], *p; + if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else if(p = strchr(line, '=')) { *p++ = 0; if(strstr(checkOptions, line+7)) sprintf(p, "%s\n", atoi(p) ? "true" : "false"); - fprintf(toE, "setoption name %s value %s", line+7, p), printf("# setoption name %s value %s", line+7, p); - } else fprintf(toE, "setoption name %s\n", line+7), printf("# setoption name %s\n", line+7); + fprintf(toE, "setoption name %s value %s", line+7, p); DPRINT("# setoption name %s value %s", line+7, p); + } else { fprintf(toE, "setoption name %s\n", line+7); DPRINT("# setoption name %s\n", line+7); } } else if(!strcmp(command, "protover")) { - printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS); - fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options + if(!variants) variants = sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS; + printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 done=0\n", variants); + printf("feature option=\"UCI2WB debug output -check %d\"\n", debug); + fprintf(toE, "u%ci\n", sc); fflush(toE); // this prompts UCI engine for options + Sync(PAUSE); // wait for uciok } else if(!strcmp(command, "setboard")) { if(strstr(line+9, " b ")) stm = BLACK; - if(sc == 's' && (p = strchr(line+9, '['))) { char c; - *p++ = ' '; q = strchr(p, ']'); c = 'w' + 'b' - q[2]; strcpy(q+2, " 1\n"); while(*--q != ' ') q[2] = *q; *p = c; p[1] = ' '; - } - sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", line+9); + if(p = strchr(line+9, '[')) { char c; + *p++ = 0; q = strchr(p, ']'); *q = 0; r = q + 4; + if(sc == 's') q[2] = 'w' + 'b' - q[2], strcpy(r=q+3, " 1\n"); // Shogi: reverse color + else r = strchr(strchr(q+4, ' ') + 1, ' '); // skip to second space (after e.p. square) + *r = 0; sprintf(command, "%s%s %s %s", line+9, q+1, p, r+1); + } else strcpy(command, line+9); + sprintf(iniPos, "%s%sfen %s", iniPos[0]=='p' ? "position " : "", sc=='s' ? "s" : "", command); iniPos[strlen(iniPos)-1] = 0; } else if(!strcmp(command, "variant")) { @@ -297,10 +370,11 @@ printf("# start search\n"); else if(!strcmp(command, "nopost")) post = 0; else if(!strcmp(command, "easy")) ponder = 0; else if(!strcmp(command, "hard")) ponder = 1; + else if(!strcmp(command, "ping")) pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5); else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory); else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores); else if(!strcmp(command, "sd")) sscanf(line, "sd %d", &depth); - else if(!strcmp(command, "st")) sscanf(line, "st %d", &stime), stime *= 1000, inc = 0; + else if(!strcmp(command, "st")) sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0; else if(!strcmp(command, "quit")) fprintf(toE, "quit\n"), fflush(toE), exit(0); } } @@ -308,24 +382,19 @@ printf("# start search\n"); int StartEngine(char *cmdLine, char *dir) { +#ifdef WIN32 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr; - SECURITY_ATTRIBUTES saAttr; BOOL fSuccess; PROCESS_INFORMATION piProcInfo; STARTUPINFO siStartInfo; DWORD err; - /* Set the bInheritHandle flag so pipe handles are inherited. */ - saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); - saAttr.bInheritHandle = TRUE; - saAttr.lpSecurityDescriptor = NULL; - /* Create a pipe for the child's STDOUT. */ - if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) return GetLastError(); + if (! WinPipe(&hChildStdoutRd, &hChildStdoutWr)) return GetLastError(); /* Create a pipe for the child's STDIN. */ - if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) return GetLastError(); + if (! WinPipe(&hChildStdinRd, &hChildStdinWr)) return GetLastError(); SetCurrentDirectory(dir); // go to engine directory @@ -367,7 +436,31 @@ StartEngine(char *cmdLine, char *dir) pid = piProcInfo.dwProcessId; fromE = (FILE*) _fdopen( _open_osfhandle((long)hChildStdoutRd, _O_TEXT|_O_RDONLY), "r"); toE = (FILE*) _fdopen( _open_osfhandle((long)hChildStdinWr, _O_WRONLY), "w"); - +#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"); +#endif return NO_ERROR; } @@ -376,16 +469,24 @@ main(int argc, char **argv) char *dir = NULL, *p, *q; int e; if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); } + if(argc > 1 && !strcmp(argv[1], "debug")) { debug = 1; argc--; argv++; } + if(argc > 1 && !strcmp(argv[1], "-var")) { variants = argv[2]; argc-=2; argv+=2; } if(argc > 1 && argv[1][0] == '-') { sc = argv[1][1]; argc--; argv++; } - if(argc < 2) { printf("usage is: U%cI2WB [-s] []", sc-32); exit(-1); } + if(argc < 2) { printf("usage is: U%cI2WB [debug] [-s] []\n", sc-32); exit(-1); } if(argc > 2) dir = argv[2]; if(argc > 3) suffix = argv[3]; // spawn engine proc - if((errno = StartEngine(argv[1], dir)) != NO_ERROR) { perror(argv[1]), exit(-1); } + if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); } + + Sync(INIT); // create separate thread to handle engine->GUI traffic +#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); } +#endif // handle GUI->engine traffic in original thread GUI2Engine();