Pass command to DoCommand through global command queue
[uci2wb.git] / UCI2WB.c
index c0ace0f..0e7f34f 100644 (file)
--- a/UCI2WB.c
+++ b/UCI2WB.c
@@ -250,12 +250,11 @@ ReadLine (FILE *f, char *line)
     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
@@ -412,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
@@ -430,10 +436,14 @@ 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, difficult, think=0;\r
@@ -523,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
@@ -579,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
@@ -610,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