mirror of
https://github.com/raspberrypi/pico-sdk.git
synced 2025-12-10 07:14:36 +01:00
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:
parent
f396d05f82
commit
d3cb97240f
1 changed files with 14 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue