1 /* $NetBSD: input.c,v 1.11 2009/05/25 04:33:53 dholland Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)input.c 8.1 (Berkeley) 5/31/93
41 #include <sys/types.h>
51 /* return true iff the given timeval is positive */
53 ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
55 /* subtract timeval `sub' from `res' */
56 #define TV_SUB(res, sub) \
57 (res)->tv_sec -= (sub)->tv_sec; \
58 (res)->tv_usec -= (sub)->tv_usec; \
59 if ((res)->tv_usec < 0) { \
60 (res)->tv_usec += 1000000; \
65 * Do a `read wait': poll for reading from stdin, with timeout *tvp.
66 * On return, modify *tvp to reflect the amount of time spent waiting.
67 * It will be positive only if input appeared before the time ran out;
68 * otherwise it will be zero or perhaps negative.
70 * If tvp is nil, wait forever, but return if poll is interrupted.
72 * Return 0 => no input, 1 => can read() from stdin
75 rwait(struct timeval
*tvp
)
78 struct timeval starttv
, endtv
;
80 #define NILTZ ((struct timezone *)0)
83 (void) gettimeofday(&starttv
, NILTZ
);
85 timeout
= tvp
->tv_sec
* 1000 + tvp
->tv_usec
/ 1000;
89 set
[0].fd
= STDIN_FILENO
;
90 set
[0].events
= POLLIN
;
91 switch (poll(set
, 1, timeout
)) {
98 stop("poll failed, help");
101 case 0: /* timed out */
109 /* since there is input, we may not have timed out */
110 (void) gettimeofday(&endtv
, NILTZ
);
111 TV_SUB(&endtv
, &starttv
);
112 TV_SUB(tvp
, &endtv
); /* adjust *tvp by elapsed time */
118 * `sleep' for the current turn time.
119 * Eat any input that might be available.
128 tv
.tv_usec
= fallrate
;
130 if (rwait(&tv
) && read(0, &c
, 1) != 1)
135 * getchar with timeout.
140 static struct timeval timeleft
;
144 * Reset timeleft to fallrate whenever it is not positive.
145 * In any case, wait to see if there is any input. If so,
146 * take it, and update timeleft so that the next call to
147 * tgetchar() will not wait as long. If there is no input,
148 * make timeleft zero or negative, and return -1.
150 * Most of the hard work is done by rwait().
152 if (!TV_POS(&timeleft
)) {
153 faster(); /* go faster */
155 timeleft
.tv_usec
= fallrate
;
157 if (!rwait(&timeleft
))
159 if (read(0, &c
, 1) != 1)
160 stop("end of file, help");
161 return ((int)(unsigned char)c
);