The discovered bit in PGCCSR register indicates if the device has been
[linux-2.6/next.git] / tools / kvm / term.c
blobfa4382dde7642b9f14213555b5d37a5cccb8d92c
1 #include <poll.h>
2 #include <stdbool.h>
3 #include <termios.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <sys/uio.h>
7 #include <signal.h>
9 #include "kvm/read-write.h"
10 #include "kvm/term.h"
11 #include "kvm/util.h"
12 #include "kvm/kvm.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;
21 int active_console;
23 int term_getc(int who)
25 int c;
27 if (who != active_console)
28 return -1;
30 if (read_in_full(STDIN_FILENO, &c, 1) < 0)
31 return -1;
33 c &= 0xff;
35 if (term_got_escape) {
36 term_got_escape = false;
37 if (c == 'x')
38 kvm_cpu__reboot();
39 if (c == term_escape_char)
40 return c;
43 if (c == term_escape_char) {
44 term_got_escape = true;
45 return -1;
48 return c;
51 int term_putc(int who, char *addr, int cnt)
53 if (who != active_console)
54 return -1;
56 while (cnt--)
57 fprintf(stdout, "%c", *addr++);
59 fflush(stdout);
60 return cnt;
63 int term_getc_iov(int who, struct iovec *iov, int iovcnt)
65 int c;
67 if (who != active_console)
68 return 0;
70 c = term_getc(who);
72 if (c < 0)
73 return 0;
75 *((int *)iov[0].iov_base) = c;
77 return sizeof(char);
80 int term_putc_iov(int who, struct iovec *iov, int iovcnt)
82 if (who != active_console)
83 return 0;
85 return writev(STDOUT_FILENO, iov, iovcnt);
88 bool term_readable(int who)
90 struct pollfd pollfd = (struct pollfd) {
91 .fd = STDIN_FILENO,
92 .events = POLLIN,
93 .revents = 0,
96 if (who != active_console)
97 return false;
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)
109 term_cleanup();
110 signal(sig, SIG_DFL);
111 raise(sig);
114 void term_init(void)
116 struct termios term;
118 if (tcgetattr(STDIN_FILENO, &orig_term) < 0)
119 die("unable to save initial standard input settings");
121 term = orig_term;
122 term.c_lflag &= ~(ICANON | ECHO | ISIG);
123 tcsetattr(STDIN_FILENO, TCSANOW, &term);
125 signal(SIGTERM, term_sig_cleanup);
126 atexit(term_cleanup);