1
#if !defined(lint) && !defined(DOS)
2
static char rcsid[] = "$Id: read.c 308 2006-12-08 20:14:42Z hubert@u.washington.edu $";
6
* ========================================================================
7
* Copyright 2006 University of Washington
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* ========================================================================
19
* Keyboard input test and read functions
22
#include <system.h> /* os-dep defs/includes */
23
#include <general.h> /* generally useful definitions */
25
#include "../keydefs.h"
29
#endif /* !_WINDOWS */
34
static time_t _time_of_last_input;
37
time_of_last_input(void)
39
if(_time_of_last_input == 0)
40
_time_of_last_input = time((time_t *) 0);
42
return(_time_of_last_input);
48
#ifdef HAVE_SYS_SELECT_H
49
# include <sys/select.h>
54
* Check whether or not a character is ready to be read, or time out.
55
* This version uses a select call.
57
* Args: time_out -- number of seconds before it will time out.
59
* Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
60
* NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
61
* READ_INTR: read was interrupted
62
* READY_TO_READ: input is available
63
* BAIL_OUT: reading input failed, time to bail gracefully
64
* PANIC_NOW: system call error, die now
67
input_ready(int time_out)
70
fd_set readfds, errfds;
76
/* Check to see if there are bytes to read with a timeout */
79
FD_SET(STDIN_FD, &readfds);
80
FD_SET(STDIN_FD, &errfds);
81
tmo.tv_sec = time_out;
83
res = select(STDIN_FD+1, &readfds, 0, &errfds, &tmo);
85
if(errno == EINTR || errno == EAGAIN)
91
if(res == 0){ /* the select timed out */
98
* "15" is the minimum allowed mail check interval.
99
* Anything less, and we're being told to cycle thru
100
* the command loop because some task is pending...
102
return(time_out < IDLE_TIMEOUT ? NO_OP_COMMAND : NO_OP_IDLE);
106
_time_of_last_input = time((time_t *) 0);
107
return(READY_TO_READ);
114
#ifdef HAVE_STROPTS_H
115
# include <stropts.h>
118
#ifdef HAVE_SYS_POLL_H
123
* Check whether or not a character is ready to be read, or time out.
124
* This version uses a poll call.
126
* Args: time_out -- number of seconds before it will time out.
128
* Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
129
* NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
130
* READ_INTR: read was interrupted
131
* READY_TO_READ: input is available
132
* BAIL_OUT: reading input failed, time to bail gracefully
133
* PANIC_NOW: system call error, die now
136
input_ready(int time_out)
138
struct pollfd pollfd;
144
/* Check to see if there are bytes to read with a timeout */
145
pollfd.fd = STDIN_FD;
146
pollfd.events = POLLIN;
147
res = poll (&pollfd, 1, time_out * 1000);
148
if(res >= 0){ /* status bits OK? */
149
if(pollfd.revents & (POLLERR | POLLNVAL))
150
res = -1; /* bad news, exit below! */
151
else if(pollfd.revents & POLLHUP)
156
if(errno == EINTR || errno == EAGAIN)
162
if(res == 0){ /* the select timed out */
164
/* Parent is init! */
169
* "15" is the minimum allowed mail check interval.
170
* Anything less, and we're being told to cycle thru
171
* the command loop because some task is pending...
173
return(time_out < IDLE_TIMEOUT ? NO_OP_COMMAND : NO_OP_IDLE);
177
_time_of_last_input = time((time_t *) 0);
178
return(READY_TO_READ);
181
#endif /* HAVE_POLL */
185
* Read one character from STDIN.
187
* Result: -- the single character read
188
* READ_INTR -- read was interrupted
189
* BAIL_OUT -- read error of some sort
197
res = read(STDIN_FD, &c, 1);
201
* Error reading from terminal!
202
* The only acceptable failure is being interrupted. If so,
203
* return a value indicating such...
205
if(res < 0 && errno == EINTR)
216
set_time_of_last_input(void)
218
_time_of_last_input = time(0L);
221
#endif /* _WINDOWS */