Fix engine names with spaces
[uci2wb.git] / UCI2WB.c
index 764f921..bb6527d 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
 #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
@@ -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,7 +81,7 @@ 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
+       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
@@ -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,7 +105,7 @@ LoadPos(int moveNr)
 {\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
@@ -118,7 +134,7 @@ Engine2GUI()
        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
@@ -211,7 +227,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 +243,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 +286,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 +301,16 @@ 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
+           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
@@ -342,22 +360,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 +444,9 @@ 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 && 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