Merge branch 'ical'
[alpine.git] / pico / osdep / read.c
blob52fe92e58a34cbb7cf0a18d82e228691d20d378e
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: read.c 763 2007-10-23 23:37:34Z hubert@u.washington.edu $";
3 #endif
5 /*
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"
27 #ifndef _WINDOWS
28 #include "raw.h"
29 #endif /* !_WINDOWS */
31 #include "read.h"
34 static time_t _time_of_last_input;
36 time_t
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);
45 #ifndef _WINDOWS
46 #if HAVE_SELECT
48 #ifdef HAVE_SYS_SELECT_H
49 # include <sys/select.h>
50 #endif
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
66 UCS
67 input_ready(int time_out)
69 struct timeval tmo;
70 fd_set readfds, errfds;
71 int res;
73 fflush(stdout);
75 if(time_out > 0){
76 /* Check to see if there are bytes to read with a timeout */
77 FD_ZERO(&readfds);
78 FD_ZERO(&errfds);
79 FD_SET(STDIN_FD, &readfds);
80 FD_SET(STDIN_FD, &errfds);
81 tmo.tv_sec = time_out;
82 tmo.tv_usec = 0;
83 res = select(STDIN_FD+1, &readfds, 0, &errfds, &tmo);
84 if(res < 0){
85 if(errno == EINTR || errno == EAGAIN)
86 return(READ_INTR);
88 return(BAIL_OUT);
91 if(res == 0){ /* the select timed out */
92 #ifndef __CYGWIN__
93 if(getppid() == 1){
94 /* Parent is init! */
95 return(BAIL_OUT);
97 #endif
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);
113 #elif HAVE_POLL
116 #ifdef HAVE_STROPTS_H
117 # include <stropts.h>
118 #endif
120 #ifdef HAVE_SYS_POLL_H
121 # include <poll.h>
122 #endif
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;
141 int res;
143 fflush(stdout);
145 if(time_out > 0){
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)
154 return(BAIL_OUT);
157 if(res < 0){
158 if(errno == EINTR || errno == EAGAIN)
159 return(READ_INTR);
161 return(PANIC_NOW);
164 if(res == 0){ /* the select timed out */
165 if(getppid() == 1){
166 /* Parent is init! */
167 return(BAIL_OUT);
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
194 read_one_char(void)
196 int res;
197 unsigned char c;
199 res = read(STDIN_FD, &c, 1);
201 if(res <= 0){
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)
208 return(READ_INTR);
209 else
210 return(BAIL_OUT);
213 return((int)c);
216 #else /* _WINDOWS */
218 set_time_of_last_input(void)
220 _time_of_last_input = time(0L);
221 return(0);
223 #endif /* _WINDOWS */