X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=e87504ae4a616bb3fadee27287c5ccfd53e4735d;hb=0eb214919078bb63985e66a39b392a7d125e3218;hp=178cdcb97e7c71b8cdd38df66a18b8a1cfcb4966;hpb=8e699739bef4730a34efb65939800a3a3b6f13b0;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index 178cdcb..e87504a 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) { @@ -440,7 +442,7 @@ GUI2Engine() char line[256], command[256], *p, *q, *r, mySide, type[99]; 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 +461,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