X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=4cbf5a87819667a4b3e66be87e2e24f96c6a9be0;hb=f9b3d49480bbbf3200dde21ca58dcd5ae41bcfa1;hp=178cdcb97e7c71b8cdd38df66a18b8a1cfcb4966;hpb=8e699739bef4730a34efb65939800a3a3b6f13b0;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index 178cdcb..4cbf5a8 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -243,29 +243,24 @@ Move4GUI(char *m) } int -GetChar() +ReadLine (FILE *f, char *line) { - int c; - if(fromF) { - if((c = fgetc(fromF)) != EOF) return c; - fclose(fromF); fromF = 0; printf("# end fake\n"); - } - return fgetc(fromE); + int x, i = 0; + while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0; + return (x != EOF); } -void * -Engine2GUI() +void +HandleEngineOutput() { char line[1024], command[256]; static char egts[999]; - if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); while(1) { int i=0, x; char *p, dummy, len; fflush(stdout); fflush(toE); - while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++; - line[++i] = 0; - if(x == EOF) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0); + if(fromF && !ReadLine(fromF, line)) fromF = 0, printf("# end fake\n"); + if(!fromF && !ReadLine(fromE, line)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0); DPRINT("# engine said: %s", line), fflush(stdout); if(sscanf(line, "%s", command) != 1) continue; if(!strcmp(command, "bestmove")) { @@ -416,6 +411,13 @@ Engine2GUI() } } +void * +Engine2GUI() +{ + if(fromF = fopen("DefectiveEngineOptions.ini", "r")) printf("# fake engine input\n"); + HandleEngineOutput(); +} + void Move4Engine(char *m) { @@ -434,13 +436,16 @@ Move4Engine(char *m) } } +int DoCommand (char *line); +char mySide; + void GUI2Engine() { - char line[256], command[256], *p, *q, *r, mySide, type[99]; + char line[256], command[256], *p; while(1) { - int i, x, difficult, think=0; + int i, difficult, think=0; if((computer == stm || computer == ANALYZE && !searching) && !suspended) { DPRINT("# start search\n"); @@ -459,8 +464,7 @@ GUI2Engine() nomove: for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking fflush(toE); fflush(stdout); - i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++; - line[++i] = 0; if(x == EOF) { printf("# EOF\n"); sprintf(line, "quit -1\n"); } + if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n"); if(think && !pause) Sync(PAUSE), think = 0, Release(); // if no longer thinking, take dummy pause sscanf(line, "%s", command); DPRINT("# '%s' think=%d pause=%d log=%d sent=%d\n", command, think, pause, logLen, sentLen); if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first @@ -528,6 +532,18 @@ GUI2Engine() !strcmp(command, "force") || !strcmp(command, "result")) { EPRINT((f, "# stop\n")); fflush(toE); } Sync(PAUSE); Release(); // block processing of difficult commands during thinking; send backlog left because of race } + if(DoCommand(line)) goto nomove; + } +} + +int +DoCommand (char *line) +{ + char command[256], *p, *q, *r, type[99]; + int i; + + sscanf(line, "%s", command); + if(!strcmp(command, "new")) { computer = BLACK; moveNr = 0; depth = -1; move[0][0] = 0; stm = WHITE; strcpy(iniPos, "position startpos"); frc &= ~1; @@ -584,13 +600,13 @@ GUI2Engine() } else if(!strcmp(command, ".")) { printf("stat01: %d %d %d %d 100 %s\n", statTime, statNodes, statDepth, 100-currNr, currMove); - goto nomove; + return 1; } else if(!strcmp(command+2, "clude") && collect > 2) { // include or exclude int all = !strcmp(line+8, "all"), in = command[1] == 'n'; inex = 1; line[strlen(line)-1] = sm = 0; // strip LF and clear sm flag for(i=1; i