2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * LWP_WaitForKeystroke - wait indefinitely or for a specified number of
12 * seconds for keyboard input.
14 * If seconds < 0, LWP_WaitForKeystroke will wait indefinitely.
15 * If seconds == 0, LWP_WaitForKeystroke will just determine if data is now
17 * Otherwise, wait "seconds" for data.
19 * Return 1 if data available.
22 #include <afsconfig.h>
23 #include <afs/param.h>
29 #include <sys/types.h>
41 #define LWP_KEYSTROKE_DELAY 250 /* 250ms. Must be < 1000 */
42 #define LWP_MAXLINELEN 256
45 /* LWP_WaitForKeystroke : Wait until a key has been struck or time (secconds)
46 * runs out and return to caller. The NT version of this function will return
47 * immediately after a key has been pressed (doesn't wait for cr).
49 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
52 * 1: Keyboard input available
53 * 0: seconds elapsed. Timeout.
56 LWP_WaitForKeystroke(int seconds)
58 time_t startTime, nowTime;
65 twait.tv_usec = LWP_KEYSTROKE_DELAY;
71 /* check if we have a keystroke */
78 /* sleep for LWP_KEYSTROKE_DELAY ms and let other
80 IOMGR_Select(0, 0, 0, 0, &twait);
82 if (seconds > 0) { /* we only worry about elapsed time if
83 * not looping forever (seconds < 0) */
84 /* now check elapsed time */
86 timeleft = seconds - difftime(nowTime, startTime);
94 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
95 * and then returns the line typed.
97 * This is trivial in unix, but requires some processing on NT.
98 * we basically read all chars into a buffer until we hit a newline and
99 * then return it to the user.
101 * n - a whole line has been read.(has n chars)
102 * 0 - buf not big enough.
103 * -1 - line with only EOF
107 LWP_GetLine(char *linebuf, int len)
113 /* loop until a new line has been entered */
114 while (ch != '\r' && cnt < len - 1) {
115 LWP_WaitForKeystroke(-1);
118 if ((ch == EOF) && (cnt == 0))
121 if (ch == '\b') { /* print and throw away a backspace */
122 if (!cnt) /* if we are at the start of the line don't bspace */
124 /* print a space to delete char and move cursor back */
133 if (ch == '\r') { /* got a cr. translate to nl */
134 linebuf[cnt - 1] = '\n';
138 } else { /* buffer too small */
145 /* LWP_WaitForKeystroke(Unix) :Wait until a key has been struck or time (secconds)
146 * runs out and return to caller. The Unix version will actually wait until
147 * a <cr> has been entered before returning.
149 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
152 * 1: Keyboard input available
153 * 0: seconds elapsed. Timeout.
156 LWP_WaitForKeystroke(int seconds)
160 struct timeval twait;
161 struct timeval *tp = NULL;
163 #ifndef AFS_DJGPP_ENV
164 #ifdef AFS_LINUX20_ENV
165 if (stdin->_IO_read_ptr < stdin->_IO_read_end)
168 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
169 if (stdin->_bf._size > 0)
182 FD_SET(fileno(stdin), &rdfds);
185 twait.tv_sec = seconds;
190 code = IOMGR_Select(1 + fileno(stdin), &rdfds, NULL, NULL, tp);
192 return (code == 1) ? 1 : 0;
195 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
196 * and then returns the line typed.
198 * This is trivial in unix, but requires some processing on NT.
199 * we basically read all chars into a buffer until we hit a newline and
200 * then return it to the user.
202 * n - a whole line has been read.(has n chars)
203 * 0 - buf not big enough.
204 * -1 - line with only EOF
208 LWP_GetLine(char *linebuf, int len)
213 LWP_WaitForKeystroke(-1);
215 s = fgets(linebuf, len, stdin);
219 linelen = strlen(linebuf);
220 if (linebuf[linelen - 1] != '\n') /* buffer too small */
226 #endif /* else NT40 */
228 /* LWP_GetResponseKey() - Waits for a specified period of time and
229 * returns a char when one has been typed by the user.
231 * seconds - how long to wait for a key press.
232 * *key - char entered by user
234 * 0 - Time ran out before the user typed a key.
235 * 1 - Valid char is being returned.
239 LWP_GetResponseKey(int seconds, char *key)
244 return 0; /* need space to store char */
247 fflush(stdin); /* flush all existing data and start anew */
250 rc = LWP_WaitForKeystroke(seconds);
251 if (rc == 0) { /* time ran out */
256 /* now read the char. */
258 *key = getche(); /* get char and echo it to screen */
260 *key = (char)getchar();