+int ptyfd = -1;
+
+void openpty()
+{
+ int fd = posix_openpt(O_RDWR);
+ char b[128];
+ struct termios kbdios;
+
+ grantpt(fd);
+ fcntl(fd, F_SETFD, 0); /* clear close-on-exec */
+ tcgetattr(fd, &kbdios);
+ kbdios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
+ tcsetattr(fd, TCSANOW, &kbdios);
+ sprintf(b, "rxvt -pty-fd %d -bg black -fg white -title \"Output terminal\" &", fd);
+ system(b);
+ unlockpt(fd);
+ ptyfd = open(ptsname(fd), O_RDWR | O_NONBLOCK);
+ close(fd);
+}
+
+unsigned int term_input()
+{
+ int rv;
+ unsigned char c;
+ if (ptyfd == -1)
+ openpty();
+ rv = read(ptyfd, &c, 1);
+ if (rv < 0)
+ return 0;
+ return 0x100 | c;
+}
+