#include #include #include #include #include #include #include #include #include #define TRUE -1 #define FALSE 0 #define FTNLEN short /* ftnlen defined in f2c.h */ struct termios saved_attributes; /* **- SUBROUTINE CLOSE_INKEY() **- disable direct keyboard input */ void close_inkey_ () { tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes); } /* **- SUBROUTINE OPEN_INKEY() **- enable direct keyboard input */ void open_inkey_ () { struct termios tattr; if (!isatty (STDIN_FILENO)) { fprintf (stderr, "Not a terminal. \n"); exit (EXIT_FAILURE); } tcgetattr (STDIN_FILENO, &saved_attributes); atexit (close_inkey_); tcgetattr (STDIN_FILENO, &tattr); tattr.c_iflag &= ~(ISTRIP); tattr.c_lflag &= ~(ICANON|ECHO); tattr.c_cc[VMIN] = 0; tattr.c_cc[VTIME] = 0; tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr); } /* **- SUBROUTINE WAIT_INPUT(INUNIT,WASKEY,TESTKEY) **- waits for the input on the INUNIT or standard input (if TESTKEY=.TRUE.) **- to become available. WASKEY is true if key was pressed. **- input parameters: inunit : c unit number **- testkey : wait for standard input too ? **- output parameters : waskey : was key pressed ? */ void wait_input_ (inunit, waskey, testkey) int *inunit, *waskey, *testkey; { fd_set read_fd_set; *waskey = FALSE; FD_ZERO (&read_fd_set); FD_SET (*inunit, &read_fd_set); if (*testkey) FD_SET (STDIN_FILENO, &read_fd_set); if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) <0) { perror ("select"); exit (EXIT_FAILURE); } if (*testkey) if (FD_ISSET (STDIN_FILENO, &read_fd_set)) *waskey = TRUE; } /* **- SUBROUTINE KEY_READY(WASKEY) **- checks is standar input is available **- output parameters : waskey : was key pressed ? */ void key_ready_ (waskey) int *waskey; { fd_set read_fd_set; struct timeval timeout; *waskey = FALSE; timeout.tv_sec = 0; timeout.tv_usec = 0; FD_ZERO (&read_fd_set); FD_SET (STDIN_FILENO, &read_fd_set); if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, &timeout) <0) { perror ("select"); exit (EXIT_FAILURE); } if (FD_ISSET (STDIN_FILENO, &read_fd_set)) *waskey = TRUE; } /* **- SUBROUTINE WAIT_KEY() **- waits for standar input to become available */ void wait_key_ () { fd_set read_fd_set; FD_ZERO (&read_fd_set); FD_SET (STDIN_FILENO, &read_fd_set); if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) <0) { perror ("select"); exit (EXIT_FAILURE); } } /* **- INTEGER FUNCTION MY_GETC(CH_READ) **- get next character from standar input. function returns **- -1 , CH_READ=CHAR(0) if input empty **- 0 , CH_READ if input not empty **- output parameters : ch_read : input character */ int my_getc_ (ch_read, ch_len) char *ch_read; FTNLEN ch_len; { int status; status = read(STDIN_FILENO,ch_read,1); if (status != 1) { *ch_read = 0; return -1; } else { return 0; } }