Move input and procesing time, otim to separate routine
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index 2b89d21..2f4a767 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -2244,6 +2244,25 @@ printf("# setup done");fflush(stdout);
       else printf("0-1%s\n", tail);\r
     }\r
 \r
+    void GetLine(int root)\r
+    {\r
+      int i, c;\r
+      while(1) {\r
+        // wait for input, and read it until we have collected a complete line\r
+        for(i = 0; (inBuf[i] = c = getchar()) != '\n'; i++) if(c == EOF || i>7997) exit(0);\r
+        inBuf[i+1] = 0;\r
+\r
+        // extract the first word\r
+        sscanf(inBuf, "%s", command);\r
+        if(!strcmp(command, "otim"))    { continue; } // do not start pondering after receiving time commands, as move will follow immediately\r
+        if(!strcmp(command, "time"))    { sscanf(inBuf, "time %d", &timeLeft); continue; }\r
+        if(!strcmp(command, "put"))     { ReadSquare(inBuf+4, &lastPut); continue; }  // ditto\r
+        if(!strcmp(command, "."))       { inBuf[0] = 0; return; } // ignore for now\r
+        if(!strcmp(command, "lift"))    { inBuf[0] = 0; Highlight(inBuf+5); return; } // treat here\r
+        return;\r
+      }\r
+    }\r
+\r
     main()\r
     {\r
       int engineSide=NONE;                     // side played by engine\r
@@ -2307,22 +2326,14 @@ pboard(board);
           }\r
         }\r
 \r
-      noPonder:\r
-        // wait for input, and read it until we have collected a complete line\r
-        for(i = 0; (inBuf[i] = getchar()) != '\n'; i++);\r
-        inBuf[i+1] = 0;\r
-\r
-        // extract the first word\r
-        sscanf(inBuf, "%s", command);\r
-printf("in: %s\n", command);\r
+        fflush(stdout);         // make sure everything is printed before we do something that might take time\r
+        if(!*inBuf) GetLine(1); // takes care of time and otim commands\r
 \r
         // recognize the command,and execute it\r
         if(!strcmp(command, "quit"))    { break; } // breaks out of infinite loop\r
         if(!strcmp(command, "force"))   { engineSide = NONE;    continue; }\r
         if(!strcmp(command, "analyze")) { engineSide = ANALYZE; continue; }\r
         if(!strcmp(command, "exit"))    { engineSide = NONE;    continue; }\r
-        if(!strcmp(command, "otim"))    { goto noPonder; } // do not start pondering after receiving time commands, as move will follow immediately\r
-        if(!strcmp(command, "time"))    { sscanf(inBuf, "time %d", &timeLeft); goto noPonder; }\r
         if(!strcmp(command, "level"))   {\r
           int min, sec=0;\r
           sscanf(inBuf, "level %d %d %d", &mps, &min, &inc) == 3 ||  // if this does not work, it must be min:sec format\r
@@ -2356,8 +2367,6 @@ printf("in: %s\n", command);
         if(!strcmp(command, "nopost"))  { postThinking = OFF;continue; }\r
         if(!strcmp(command, "random"))  { randomize = ON;    continue; }\r
         if(!strcmp(command, "hint"))    { if(ponderMove != INVALID) printf("Hint: %s\n", MoveToText(ponderMove, 0)); continue; }\r
-        if(!strcmp(command, "lift"))    { Highlight(inBuf+5); continue; }\r
-        if(!strcmp(command, "put"))     { ReadSquare(inBuf+4, &lastPut); continue; }\r
         if(!strcmp(command, "book"))    {  continue; }\r
        // non-standard commands\r
         if(!strcmp(command, "p"))       { pboard(board); continue; }\r