Make supported variants run-time configurable
[uci2wb.git] / UCI2WB.c
index 764f921..7f90ba6 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -1,6 +1,6 @@
 /************************* UCI2WB by H.G.Muller ****************************/\r
 \r
-#define VERSION "1.5"\r
+#define VERSION "1.7"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\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
+char move[2000][10], checkOptions[8192], iniPos[256], hashOpt[20], pause, pondering, ponder, post, hasHash, c, sc='c', *suffix, *variants;\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
@@ -49,7 +60,12 @@ void
 Sync (int action)\r
 {\r
 #ifdef WIN32\r
-       if(action == PAUSE) while(pause) SLEEP(); // in Windows we still rely on polling. :-(\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
@@ -65,14 +81,14 @@ StartSearch(char *ponder)
 {      // 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
-       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
-       if(depth > 0) fprintf(toE, " depth %d", depth),printf(" depth %d", depth);\r
-        if(suffix) fprintf(toE, suffix, inc),printf(suffix, inc);\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); DPRINT(" movetime %d", sTime); } else\r
+       if(mps) { fprintf(toE, " movestogo %d", mps*(nr/(2*mps)+1)-nr/2); DPRINT(" movestogo %d", mps*(nr/(2*mps)+1)-nr/2); }\r
+       if(inc && !suffix) { fprintf(toE, " winc %d binc %d", inc, inc); DPRINT(" winc %d binc %d", inc, inc); }\r
+       if(depth > 0) { fprintf(toE, " depth %d", depth); DPRINT(" depth %d", depth); }\r
+        if(suffix) { fprintf(toE, suffix, inc); DPRINT(suffix, inc); }\r
        fprintf(toE, "%s\n", ponder);\r
-       printf("%s\n", ponder);\r
+       DPRINT("%s\n", ponder);\r
 }\r
 \r
 void\r
@@ -80,7 +96,7 @@ StopPonder(int pondering)
 {\r
        if(!pondering) return;\r
        pause = 1;\r
-       fprintf(toE, "stop\n"); fflush(toE); printf("# stop\n"); // note: 'pondering' remains set until engine acknowledges '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
@@ -89,8 +105,8 @@ LoadPos(int moveNr)
 {\r
        int j;\r
        fprintf(toE, "%s moves", iniPos);\r
-       printf(    "# %s moves", iniPos);\r
-       for(j=0; j<moveNr; j++) fprintf(toE, " %s", move[j]),printf(" %s", move[j]);\r
+       DPRINT(    "# %s moves", iniPos);\r
+       for(j=0; j<moveNr; j++) { fprintf(toE, " %s", move[j]); DPRINT(" %s", move[j]); }\r
 }\r
 \r
 char *Convert(char *pv)\r
@@ -112,13 +128,13 @@ Engine2GUI()
     char line[1024], command[256];\r
 \r
     while(1) {\r
-       int i=0, x; char *p;\r
+       int i=0, x; char *p, dummy;\r
 \r
        fflush(stdout); fflush(toE);\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); fflush(stdout);\r
+       DPRINT("# engine said: %s", line), fflush(stdout);\r
        sscanf(line, "%s", command);\r
        if(!strcmp(command, "bestmove")) {\r
            if(pause) { pondering = pause = 0; Sync(WAKEUP); continue; } // bestmove was reply to ponder miss or analysis result; ignore.\r
@@ -133,7 +149,7 @@ printf("# engine said: %s", line); fflush(stdout);
            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
@@ -162,8 +178,9 @@ printf("# ponder on %s\n", move[moveNr]);
        else if(!strcmp(command, "info")) {\r
            int d=0, s=0, t=0, n=0;\r
            char *pv;\r
+           if(sscanf(line+5, "string times @ %c", &dummy) == 1) { printf("# %s", line+12); continue; }\r
            if(!post) continue;\r
-           if(strstr(line, "info string ") == line) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
+           if(sscanf(line+5, "string %c", &dummy) == 1) printf("%d 0 0 0 %s", lastDepth, line+12); else {\r
                if(p = strstr(line+4, " depth "))      sscanf(p+7, "%d", &d), statDepth = d;\r
                if(p = strstr(line+4, " score cp "))   sscanf(p+10, "%d", &s), statScore = s; else\r
                if(p = strstr(line+4, " score mate ")) sscanf(p+12, "%d", &s), s += s>0 ? 100000 : -100000, statScore = s; else\r
@@ -211,7 +228,7 @@ printf("# ponder on %s\n", move[moveNr]);
        }\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; 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
@@ -227,7 +244,7 @@ GUI2Engine()
        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
@@ -270,7 +287,7 @@ printf("# start search\n");
            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
@@ -285,14 +302,17 @@ printf("# start search\n");
        }\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 ping=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS);\r
+           if(!variants) variants = 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", 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
@@ -331,7 +351,7 @@ printf("# start search\n");
        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
-       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime *= 1000, inc = 0;\r
+       else if(!strcmp(command, "st"))     sscanf(line, "st %d", &sTime), sTime = 1000*sTime - 30, inc = 0;\r
        else if(!strcmp(command, "quit"))   fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
     }\r
 }\r
@@ -342,22 +362,16 @@ StartEngine(char *cmdLine, char *dir)
 #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
@@ -432,8 +446,10 @@ main(int argc, char **argv)
        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 && !strcmp(argv[1], "-var")) { variants = argv[2]; argc-=2; argv+=2; }\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