1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: read.c 763 2007-10-23 23:37:34Z 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 */
100 * "15" is the minimum allowed mail check interval.
101 * Anything less, and we're being told to cycle thru
102 * the command loop because some task is pending...
104 return(time_out
< IDLE_TIMEOUT
? NO_OP_COMMAND
: NO_OP_IDLE
);
108 _time_of_last_input
= time((time_t *) 0);
109 return(READY_TO_READ
);
116 #ifdef HAVE_STROPTS_H
117 # include <stropts.h>
120 #ifdef HAVE_SYS_POLL_H
125 * Check whether or not a character is ready to be read, or time out.
126 * This version uses a poll call.
128 * Args: time_out -- number of seconds before it will time out.
130 * Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
131 * NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
132 * READ_INTR: read was interrupted
133 * READY_TO_READ: input is available
134 * BAIL_OUT: reading input failed, time to bail gracefully
135 * PANIC_NOW: system call error, die now
138 input_ready(int time_out
)
140 struct pollfd pollfd
;
146 /* Check to see if there are bytes to read with a timeout */
147 pollfd
.fd
= STDIN_FD
;
148 pollfd
.events
= POLLIN
;
149 res
= poll (&pollfd
, 1, time_out
* 1000);
150 if(res
>= 0){ /* status bits OK? */
151 if(pollfd
.revents
& (POLLERR
| POLLNVAL
))
152 res
= -1; /* bad news, exit below! */
153 else if(pollfd
.revents
& POLLHUP
)
158 if(errno
== EINTR
|| errno
== EAGAIN
)
164 if(res
== 0){ /* the select timed out */
166 /* Parent is init! */
171 * "15" is the minimum allowed mail check interval.
172 * Anything less, and we're being told to cycle thru
173 * the command loop because some task is pending...
175 return(time_out
< IDLE_TIMEOUT
? NO_OP_COMMAND
: NO_OP_IDLE
);
179 _time_of_last_input
= time((time_t *) 0);
180 return(READY_TO_READ
);
183 #endif /* HAVE_POLL */
187 * Read one character from STDIN.
189 * Result: -- the single character read
190 * READ_INTR -- read was interrupted
191 * BAIL_OUT -- read error of some sort
199 res
= read(STDIN_FD
, &c
, 1);
203 * Error reading from terminal!
204 * The only acceptable failure is being interrupted. If so,
205 * return a value indicating such...
207 if(res
< 0 && errno
== EINTR
)
218 set_time_of_last_input(void)
220 _time_of_last_input
= time(0L);
223 #endif /* _WINDOWS */