Making a purge in cbreak mode also makes the code compatible with
canonical mode. This can be useful in the case a shell, like bash, does
not restore the tty state of stopped jobs before restarting them. An
alternative fix to this minor shortcoming would be to retest the tty
state each time inside ask().
* in cases involving special characters. Here a C version.
*/
#include <stdio.h>
+#include <stdio_ext.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
}
else {
buf[0] = c;
- if (c != '\n' && tty_cbreak) /* no purge necessary */
+ if (c != '\n' && tty_cbreak) {
+ /* Possibly purge a multi-byte character; or do a
+ required purge of the rest of the line (including
+ the newline) if the tty has been put back in
+ canonical mode (for example by a shell after a
+ SIGTSTP signal). */
+ __fpurge(stdin);
printf("\n");
+ }
else if (c != '\n')
while ((c = fgetc(stdin)) != '\n' && c != EOF);
}