console: fix linenoiseProbe never timing out

Fixes a regression from 753a92952: if cb was negative, read_bytes
overflowed, because the type was changed from int to size_t.

Also fixes incorrect timeout calculation: timeout_ms was 200, but
each iteration delayed for 10ms, and reduced timeout_ms by 1. This
made the effective timeout to be 2000ms.
This commit is contained in:
Ivan Grokhotkov
2021-01-15 09:22:04 +01:00
parent bba7826ac0
commit 9b2b86b7d9

View File

@@ -935,13 +935,17 @@ int linenoiseProbe(void) {
/* Try to read response */
int timeout_ms = 200;
const int retry_ms = 10;
size_t read_bytes = 0;
while (timeout_ms > 0 && read_bytes < 4) { // response is ESC[0n or ESC[3n
usleep(10000);
usleep(retry_ms * 1000);
timeout_ms -= retry_ms;
char c;
int cb = read(stdin_fileno, &c, 1);
if (cb < 0) {
continue;
}
read_bytes += cb;
timeout_ms--;
}
/* Restore old mode */
flags &= ~O_NONBLOCK;