commit:     9ed12f2dbbd47a621839a4ba3183669f8ec5806c
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri Jun  9 20:49:49 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Jun  9 20:54:07 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9ed12f2d

Have ecma48-cpr flush the terminal's input queue

There is no guarantee that the input queue is empty at the time that
ecma48-cpr is executed. If the terminal has input waiting, not only
will ecma48-cpr potentially fail but it will pollute the input queue.
Work around this by flushing the input queue entirely. I'm not
especially pleased by this approach but I can see no other option,
short of giving up on the idea of using the CPR sequence at all.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>

 ecma48-cpr.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 9aef182..144c4ea 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -95,6 +95,8 @@ main(void) {
         */
        if (tcsetattr(STDIN_FILENO, TCSANOW, &new_tty) != 0) {
                die("failed to modify the terminal settings");
+       } else if (tcflush(STDIN_FILENO, TCIFLUSH) != 0) {
+               die("failed to flush the terminal's input queue");
        } else if (fprintf(tty, "\033[6n") != 4) {
                die("failed to write the CPR sequence to the terminal");
        } else if (fclose(tty) != 0) {

Reply via email to