Do not always set stdin to FNONBLOCK when PICO_PLATFORM=host

Because the UART simulation was setting FNONBLOCK flag on stdin, all
stdio functions like scanf() or fgets() were non blocking.
The workaround is to use a non_blocking_getchar() function to do the UART
simulation, which will temporarily set stdin to non blocking.
This will make stdio functions blocking like they normally do.
This commit is contained in:
Przemyslaw Czarnota 2023-03-28 17:54:50 +02:00 committed by Przemyslaw Czarnota
parent f396d05f82
commit d3cb97240f

View file

@ -48,12 +48,24 @@ void _inittty(void) {
//_tty.c_oflag &= ~ONLCR;
tcsetattr(STDIN_FILENO, TCSANOW, &_tty);
fcntl(STDIN_FILENO, F_SETFL, FNONBLOCK);
atexit(_resettty);
}
static int non_blocking_getchar(void) {
fcntl(STDIN_FILENO, F_SETFL, FNONBLOCK);
int c = getchar();
int old = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, old & ~FNONBLOCK);
return c;
}
#else
void _inittty() {}
static int non_blocking_getchar(void) { return getchar(); }
#endif
typedef struct {
@ -67,7 +79,7 @@ static int _nextchar = EOF;
static bool _peekchar() {
if (_nextchar == EOF) {
_nextchar = getchar();
_nextchar = non_blocking_getchar();
}
return _nextchar != EOF;
}