Check in Windows icon
[uci2wb.git] / UCI2WB.c
index c6eb949..6b454de 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -3,9 +3,22 @@
 #define VERSION "1.5"\r
 \r
 #include <stdio.h>\r
-#include <windows.h>\r
-#include <io.h>\r
+#include <stdlib.h>\r
+#ifdef WIN32
+#  include <windows.h>
+#  include <io.h>\r
+   HANDLE process;\r
+   DWORD thread_id;\r
+#else
+#  include <pthread.h>
+#  include <signal.h>
+#  define NO_ERROR 0
+#  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
+#endif\r
 #include <fcntl.h>\r
+#include <string.h>\r
 \r
 #ifdef _MSC_VER \r
 #define SLEEP() Sleep(1) \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;\r
 int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; // for analyze mode\r
 \r
 FILE *toE, *fromE;\r
-HANDLE process;\r
 int pid;\r
-DWORD thread_id;\r
 \r
 void\r
 StartSearch(char *ponder)\r
@@ -36,7 +47,7 @@ StartSearch(char *ponder)
        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(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
@@ -76,7 +87,7 @@ char *Convert(char *pv)
     return buf;\r
 }\r
 \r
-void\r
+void *\r
 Engine2GUI()\r
 {\r
     char line[1024], command[256];\r
@@ -98,7 +109,7 @@ printf("# engine said: %s", line);
               strstr(line+9, "0000")) { printf("%s\n", lastScore < -99999 ? "resign" : "1/2-1/2 {stalemate}"); computer = NONE; }\r
            sscanf(line, "bestmove %s", move[moveNr++]);\r
            myTime -= (GetTickCount() - startTime)*1.02 + inc; // update own clock, so we can give correct wtime, btime with ponder\r
-           if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(stime) myTime = stime; // new session or move starts\r
+           if(mps && ((moveNr+1)/2) % mps == 0) myTime += tc; if(sTime) myTime = sTime; // new session or move starts\r
            // first start a new ponder search, if pondering is on and we have a move to ponder on\r
            if(p = strstr(line+9, "ponder")) {\r
              if(computer != NONE && ponder) {\r
@@ -207,7 +218,7 @@ printf("# start search\n");
       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); }\r
+       line[++i] = 0; if(x == EOF) { printf("# EOF\n"); exit(-1); }
        sscanf(line, "%s", command);\r
        while(pause) SLEEP(); // wait for readyok\r
        if(!strcmp(command, "new")) {\r
@@ -251,7 +262,7 @@ printf("# start search\n");
            int sec = 0;\r
            sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 ||\r
            sscanf(line, "level %d %d %d", &mps, &tc, &inc);\r
-           tc = (60*tc + sec)*1000; inc *= 1000; stime = 0;\r
+           tc = (60*tc + sec)*1000; inc *= 1000; sTime = 0;\r
        }\r
        else if(!strcmp(command, "option")) {\r
            char name[80], *p;\r
@@ -300,14 +311,15 @@ 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, inc = 0;\r
        else if(!strcmp(command, "quit"))   fprintf(toE, "quit\n"), fflush(toE), exit(0);\r
     }\r
 }\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
@@ -367,7 +379,31 @@ StartEngine(char *cmdLine, char *dir)
   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
-\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");
+#endif\r
   return NO_ERROR;\r
 }\r
 \r
@@ -377,15 +413,19 @@ main(int argc, char **argv)
 \r
        if(argc == 2 && !strcmp(argv[1], "-v")) { printf("UCI2WB " VERSION " by H.G.Muller\n"); exit(0); }\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>]", sc-32); exit(-1); }\r
+       if(argc < 2) { printf("usage is: U%cI2WB [-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((errno = StartEngine(argv[1], dir)) != NO_ERROR) { perror(argv[1]), exit(-1); }\r
+       if(StartEngine(argv[1], dir) != NO_ERROR) { perror(argv[1]), exit(-1); }\r
 \r
        // create separate thread to handle engine->GUI traffic\r
-       CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Engine2GUI, (LPVOID) NULL, 0, &thread_id);\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); }
+#endif\r
 \r
        // handle GUI->engine traffic in original thread\r
        GUI2Engine();\r