9 #include "kvm/read-write.h"
13 #include "kvm/kvm-cpu.h"
15 extern struct kvm
*kvm
;
16 static struct termios orig_term
;
18 int term_escape_char
= 0x01; /* ctrl-a is used for escape */
19 bool term_got_escape
= false;
23 int term_getc(int who
)
27 if (who
!= active_console
)
30 if (read_in_full(STDIN_FILENO
, &c
, 1) < 0)
35 if (term_got_escape
) {
36 term_got_escape
= false;
39 if (c
== term_escape_char
)
43 if (c
== term_escape_char
) {
44 term_got_escape
= true;
51 int term_putc(int who
, char *addr
, int cnt
)
53 if (who
!= active_console
)
57 fprintf(stdout
, "%c", *addr
++);
63 int term_getc_iov(int who
, struct iovec
*iov
, int iovcnt
)
67 if (who
!= active_console
)
75 *((int *)iov
[0].iov_base
) = c
;
80 int term_putc_iov(int who
, struct iovec
*iov
, int iovcnt
)
82 if (who
!= active_console
)
85 return writev(STDOUT_FILENO
, iov
, iovcnt
);
88 bool term_readable(int who
)
90 struct pollfd pollfd
= (struct pollfd
) {
96 if (who
!= active_console
)
99 return poll(&pollfd
, 1, 0) > 0;
102 static void term_cleanup(void)
104 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_term
);
107 static void term_sig_cleanup(int sig
)
110 signal(sig
, SIG_DFL
);
118 if (tcgetattr(STDIN_FILENO
, &orig_term
) < 0)
119 die("unable to save initial standard input settings");
122 term
.c_lflag
&= ~(ICANON
| ECHO
| ISIG
);
123 tcsetattr(STDIN_FILENO
, TCSANOW
, &term
);
125 signal(SIGTERM
, term_sig_cleanup
);
126 atexit(term_cleanup
);