#include #include #include #include #include #include #include static struct termios term_attributes; int tty_cbreak (int fd, struct termios *orig) { struct termios buf; if (tcgetattr (fd, &buf) < 0) return -1; /* Save the original state, for resetting later */ *orig = buf; buf.c_lflag &= ~(ECHO | ICANON); buf.c_iflag &= ~(ICRNL | INLCR); buf.c_cc[VMIN] = 1; buf.c_cc[VTIME] = 0; #if defined (VLNEXT) && defined (_POSIX_VDISABLE) buf.c_cc[VLNEXT] = _POSIX_VDISABLE; #endif #if defined (VDSUSP) && defined (_POSIX_VDISABLE) buf.c_cc[VDSUSP] = _POSIX_VDISABLE; #endif if (tcsetattr (fd, TCSAFLUSH, &buf) < 0) return -1; return 0; } int tty_set_attributes (int fd, struct termios *buf) { if (tcsetattr (fd, TCSAFLUSH, buf) < 0) return -1; return 0; } int user_input_loop () { fprintf (stderr, "In user_input_loop\r\n"); return 0; } static int main_loop (void) { fd_set rset; int max = STDIN_FILENO; int val; for (;;) { /* Reset the fd_set, and watch for input from GDB or stdin */ FD_ZERO (&rset); FD_SET (STDIN_FILENO, &rset); /* Wait for input */ val =select (max + 1, &rset, NULL, NULL, NULL); fprintf (stderr, "Select return value:%d\r\n", val); if (val == -1) { if (errno == EINTR) continue; else { fprintf (stderr, __FILE__, __LINE__, "select failed: %s", strerror (errno)); return -1; } } /* Input received: Handle it */ if (FD_ISSET (STDIN_FILENO, &rset)) { if (user_input_loop () == -1) return -1; return 0; } } return 0; } int main (int argc, char *argv[]) { if (tty_cbreak (STDIN_FILENO, &term_attributes) == -1) { fprintf (stderr, __FILE__, __LINE__, "tty_cbreak error"); exit (-1); } /* Enter main loop */ main_loop (); if (tty_set_attributes (STDIN_FILENO, &term_attributes) == -1) fprintf (stderr, __FILE__, __LINE__, "tty_reset error"); return 0; }