Pass command to DoCommand through global command queue
[uci2wb.git] / UCI2WB.c
index 178cdcb..0e7f34f 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -243,29 +243,24 @@ Move4GUI(char *m)
 }\r
 \r
 int\r
-GetChar()\r
+ReadLine (FILE *f, char *line)\r
 {\r
-    int c;\r
-    if(fromF) {\r
-       if((c = fgetc(fromF)) != EOF) return c;\r
-       fclose(fromF); fromF = 0; printf("# end fake\n");\r
-    }\r
-    return fgetc(fromE);\r
+    int x, i = 0;\r
+    while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0;\r
+    return (x != EOF);\r
 }\r
 \r
-void *\r
-Engine2GUI()\r
+void\r
+HandleEngineOutput()\r
 {\r
     char line[1024], command[256]; static char egts[999];\r
 \r
-    if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
     while(1) {\r
        int i=0, x; char *p, dummy, len;\r
 \r
        fflush(stdout); fflush(toE);\r
-       while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++;\r
-       line[++i] = 0;\r
-       if(x == EOF) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0);\r
+       if(fromF && !ReadLine(fromF, line))  fromF = 0, printf("# end fake\n");\r
+       if(!fromF && !ReadLine(fromE, line)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0);\r
        DPRINT("# engine said: %s", line), fflush(stdout);\r
        if(sscanf(line, "%s", command) != 1) continue;\r
        if(!strcmp(command, "bestmove")) {\r
@@ -416,6 +411,13 @@ Engine2GUI()
     }\r
 }\r
 \r
+void *\r
+Engine2GUI()\r
+{\r
+    if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n");\r
+    HandleEngineOutput();\r
+}\r
+\r
 void\r
 Move4Engine(char *m)\r
 {\r
@@ -434,13 +436,17 @@ Move4Engine(char *m)
     }\r
 }\r
 \r
+int DoCommand ();\r
+char mySide;\r
+char queue[10000], *qStart, *qEnd;\r
+\r
 void\r
 GUI2Engine()\r
 {\r
-    char line[256], command[256], *p, *q, *r, mySide, type[99];\r
+    char line[256], command[256], *p;\r
 \r
     while(1) {\r
-       int i, x, difficult, think=0;\r
+       int i, difficult, think=0;\r
 \r
        if((computer == stm || computer == ANALYZE && !searching) && !suspended) {\r
            DPRINT("# start search\n");\r
@@ -459,8 +465,7 @@ GUI2Engine()
       nomove:\r
        for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking\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"); sprintf(line, "quit -1\n"); }\r
+       if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n");\r
        if(think && !pause) Sync(PAUSE), think = 0, Release(); // if no longer thinking, take dummy pause\r
        sscanf(line, "%s", command); DPRINT("# '%s' think=%d pause=%d log=%d sent=%d\n", command, think, pause, logLen, sentLen);\r
        if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first\r
@@ -528,6 +533,21 @@ GUI2Engine()
               !strcmp(command, "force") || !strcmp(command, "result")) { EPRINT((f, "# stop\n")); fflush(toE); }\r
            Sync(PAUSE); Release(); // block processing of difficult commands during thinking; send backlog left because of race\r
        }\r
+       if(qStart == qEnd) qStart = qEnd = queue;\r
+       p = line; while(qEnd < queue+10000 && (*qEnd++ = *p++) != '\n') {}\r
+       if(DoCommand()) goto nomove;\r
+    }\r
+}\r
+\r
+int\r
+DoCommand ()\r
+{\r
+    char line[1024], command[256], *p, *q, *r, type[99];\r
+    int i;\r
+\r
+    p=line; while(qStart < qEnd && (*p++ = *qStart++) != '\n') {} *p = 0;\r
+    sscanf(line, "%s", command);\r
+\r
        if(!strcmp(command, "new")) {\r
            computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0;\r
            stm = WHITE; strcpy(iniPos, "position startpos"); frc &= ~1;\r
@@ -584,13 +604,13 @@ GUI2Engine()
        }\r
        else if(!strcmp(command, ".")) {\r
            printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove);\r
-           goto nomove;\r
+           return 1;\r
        }\r
        else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude\r
            int all = !strcmp(line+8, "all"), in = command[1] == 'n';\r
            inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag\r
            for(i=1; i<nr; i++) { if(!strcmp(line+8, moveMap[i]) || all) on[i] = in; sm |= on[i]+1; } // sm: 2 = enabled, 1 = disabled\r
-           if(!(sm & 2)) goto nomove; // no moves enabled; continue current search\r
+           if(!(sm & 2)) return 1; // no moves enabled; continue current search\r
            if(computer == ANALYZE) StopPonder(1), searching = 0; // abort old analysis\r
        }\r
        else if(!strcmp(command, "xboard")) ;\r
@@ -615,7 +635,8 @@ GUI2Engine()
            computer = NONE;\r
        }\r
        else if(!strcmp(command, "quit"))   { EPRINT((f, "# quit\n")) fflush(toE), exit(atoi(line+4)); }\r
-    }\r
+\r
+    return 0;\r
 }\r
 \r
 int\r