[pacman-dev] [RFC] Proposed fix for FS#20538

Dave Reisner d at falconindy.com
Wed Mar 23 11:41:52 EDT 2011


https://bugs.archlinux.org/task/20538

The attached patch flushes the terminal input buffer shortly before
prompting users for input to answer a question. On the one hand, I can
see how this might be nice when you accidentally hit <enter> twice, or
fat finger a key during a download and press <enter> at the prompt only
to be oddly returned to your shell. On the other hand, I've occasionally
abused this exact behavior to walk away from my computer in the middle
of a large -Syu.

Thoughts? Any issues with the code approach?

dave
-------------- next part --------------
>From cac59bcc85ee168b56365655f9f27a95a9d56e0e Mon Sep 17 00:00:00 2001
From: Dave Reisner <d at falconindy.com>
Date: Fri, 18 Mar 2011 11:03:28 -0400
Subject: [PATCH] pacman/util: flush terminal input before reading response

Addresses FS#20538

Signed-off-by: Dave Reisner <d at falconindy.com>
---
 src/pacman/util.c |   17 +++++++++++++++++
 src/pacman/util.h |    1 +
 2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index 558d6f8..7fb4562 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <limits.h>
 #include <wchar.h>
+#include <termios.h> /* tcflush */
 
 #include <alpm.h>
 #include <alpm_list.h>
@@ -100,6 +101,16 @@ int needs_root(void)
 	}
 }
 
+/* discard unhandled input on the terminal's input buffer */
+int flush_term_input(void) {
+	if(isatty(fileno(stdin))) {
+		return(tcflush(fileno(stdin), TCIFLUSH));
+	}
+
+	/* fail silently */
+	return(0);
+}
+
 /* gets the current screen column width */
 int getcols(void)
 {
@@ -812,6 +823,8 @@ int multiselect_question(char *array, int count)
 			break;
 		}
 
+		flush_term_input();
+
 		if(fgets(response, sizeof(response), stdin)) {
 			strtrim(response);
 			if(strlen(response) > 0) {
@@ -849,6 +862,8 @@ int select_question(int count)
 			break;
 		}
 
+		flush_term_input();
+
 		if(fgets(response, sizeof(response), stdin)) {
 			strtrim(response);
 			if(strlen(response) > 0) {
@@ -891,6 +906,8 @@ static int question(short preset, char *fmt, va_list args)
 		return preset;
 	}
 
+	flush_term_input();
+
 	if(fgets(response, sizeof(response), stdin)) {
 		strtrim(response);
 		if(strlen(response) == 0) {
diff --git a/src/pacman/util.h b/src/pacman/util.h
index 234a631..27cbf46 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -59,6 +59,7 @@ int str_cmp(const void *s1, const void *s2);
 void display_new_optdepends(pmpkg_t *oldpkg, pmpkg_t *newpkg);
 void display_optdepends(pmpkg_t *pkg);
 void print_packages(const alpm_list_t *packages);
+int flush_term_input(void);
 void select_display(const alpm_list_t *pkglist);
 int select_question(int count);
 int multiselect_question(char *array, int count);
-- 
1.7.4.1



More information about the pacman-dev mailing list