0

I'am developing a Chess engine in windows 10 in C with W64ming that interfaces a standard GUI (eg Arena Chess) using stdin/stdout according to the UCI protocol.

My problem is that with my program (runned in cmd.exe) I cannot insert more than 256 characters (instead of the expected 8192 that should be allowed in Windows 10).

So when I try to write or paste the string

position startpos moves d2d4 g8f6 c2c4 e7e6 g1f3 b7b6 g2g3 c8a6 b2b3 f8b4 c1d2 b4e7 f1g2 c7c6 d2c3 d7d5 f3e5 f6d7 e5d7 b8d7 a2a4 d5c4 g2c6 a8c8 c6b5 a6b5 a4b5 d8c7 e1g1 d7f6 b3c4 c7c4 d1d2 f6e4 d2c1 e4c3 b1c3 c4d4 a1a7 e6e5 f1d1 d4c3 c1b1 e7c5 a7f7 e8f7 g3g4 c8f8 b1f5 f7g8 e2e4 f8f5 g4f5 c5d4 g1g2s c3c2 d1d4 e5d4 g2f3

is blocked/truncated as

position startpos moves d2d4 g8f6 c2c4 e7e6 g1f3 b7b6 g2g3 c8a6 b2b3 f8b4 c1d2 b4e7 f1g2 c7c6 d2c3 d7d5 f3e5 f6d7 e5d7 b8d7 a2a4 d5c4 g2c6 a8c8 c6b5 a6b5 a4b5 d8c7 e1g1 d7f6 b3c4 c7c4 d1d2 f6e4 d2c1 e4c3 b1c3 c4d4 a1a7 e6e5 f1d1 d4c3 c1b1 e7c5 a7f7 e8f7

Here's a simple C program that isolate and shows the above problem reading data from stdin using fgets:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFERSIZE 8192

void UCILoop() {

    static char lineIn[BUFFERSIZE];
    setbuf(stdin, NULL);
    setbuf(stdout, NULL);

    while (1==1) {
        memset(&lineIn[0], 0, BUFFERSIZE);
      
        fflush(stdout);

        if (!fgets(lineIn, BUFFERSIZE, stdin))
            continue;

        if (lineIn[0] == '\n')
            continue;
        
        printf("%s\n", lineIn);
    }
}

int main() {
    UCILoop();
}

The strange things (for me) is that running an another pre compiled chess engine (eg Stockfish) in the same enviroment (cmd.exe) it doesn't show the problem.

How can I solve this? Any help is appreciated!! Tks!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • "the expected 8192" Where do you get that expectation from? What value has `BUFSIZ` in your environment? – Gerhardh Dec 11 '22 at 11:40
  • If you use `setbuf` why don't you set a large buffer instead of switching to unbuffered mode for `stdin`? – Gerhardh Dec 11 '22 at 11:42
  • Hi! I found this in https://stackoverflow.com/questions/3205027/maximum-length-of-command-line-string – Alberto Dec 11 '22 at 16:03
  • That is about command line length, not about buffer of `stdin` as far as I can see. – Gerhardh Dec 12 '22 at 04:57
  • I solved thanks to the hint of Gerhardh changing with: // setbuf(stdin, NULL); // setbuf(stdout, NULL); setvbuf(stdin, NULL, _IOFBF, INPUTBUFFER); setvbuf(stdout, NULL, _IOFBF, INPUTBUFFER); where INPUTBUFFER, for me, is setted to 8192 – Alberto Dec 15 '22 at 08:58

0 Answers0