From 960d6994630018f187838b82521c720ce6ec5aee Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 18 Jan 2011 23:05:06 +0200 Subject: [PATCH] kvm tools: Cleanup termios handling This patch cleans up the confused termios handling to do what Lguest does. Signed-off-by: Pekka Enberg --- tools/kvm/main.c | 50 +++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/tools/kvm/main.c b/tools/kvm/main.c index 42857cdf018b..643f6bde0505 100644 --- a/tools/kvm/main.c +++ b/tools/kvm/main.c @@ -29,46 +29,34 @@ static void usage(char *argv[]) static struct kvm *kvm; -static struct termios tty_origin_stdin; -static struct termios tty_origin_stdout; -static struct termios tty_origin_stderr; +static struct termios orig_term; -static void tty_save_origins(void) +static void setup_console(void) { - tcgetattr(fileno(stdin), &tty_origin_stdin); - tcgetattr(fileno(stdout), &tty_origin_stdout); - tcgetattr(fileno(stderr), &tty_origin_stderr); -} + struct termios term; -static void tty_restore_origins(void) -{ - tcsetattr(fileno(stdin), TCSAFLUSH, &tty_origin_stdin); - tcsetattr(fileno(stdout), TCSAFLUSH, &tty_origin_stdout); - tcsetattr(fileno(stderr), TCSAFLUSH, &tty_origin_stderr); + if (tcgetattr(STDIN_FILENO, &orig_term) < 0) + die("unable to save initial standard input settings"); + + term = orig_term; + + term.c_lflag &= ~(ICANON|ECHO); + + tcsetattr(STDIN_FILENO, TCSANOW, &term); } -static void tty_set_canon_flag(int fd, int on) +static void cleanup_console(void) { - struct termios tty; - int mask = ISTRIP | INLCR | ICRNL | IGNCR | IXON | IXOFF | ICANON | ECHO; - - tcgetattr(fd, &tty); - if (on) - tty.c_lflag |= mask; - else - tty.c_lflag &= ~mask; - tcsetattr(fd, TCSAFLUSH, &tty); + tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); } static void shutdown(void) { - tty_set_canon_flag(fileno(stdin), 0); - tty_restore_origins(); + cleanup_console(); } static void handle_sigint(int sig) { - shutdown(); exit(1); } @@ -101,13 +89,13 @@ int main(int argc, char *argv[]) bool single_step = false; int i; - tty_save_origins(); - - atexit(shutdown); - signal(SIGQUIT, handle_sigquit); signal(SIGINT, handle_sigint); + setup_console(); + + atexit(shutdown); + for (i = 1; i < argc; i++) { if (option_matches(argv[i], "--kernel=")) { kernel_filename = &argv[i][9]; @@ -183,8 +171,6 @@ int main(int argc, char *argv[]) kvm__start_timer(kvm); - tty_set_canon_flag(fileno(stdin), 1); - for (;;) { kvm__run(kvm); -- 2.39.5