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 <afs/param.h>
24 #include <sys/types.h>
35 #define LWP_KEYSTROKE_DELAY 250 /* 250ms. Must be < 1000 */
36 #define LWP_MAXLINELEN 256
39 /* LWP_WaitForKeystroke : Wait until a key has been struck or time (secconds)
40 * runs out and return to caller. The NT version of this function will return
41 * immediately after a key has been pressed (doesn't wait for cr).
43 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
46 * 1: Keyboard input available
47 * 0: seconds elapsed. Timeout.
49 int LWP_WaitForKeystroke(int seconds)
51 time_t startTime, nowTime;
58 twait.tv_usec = LWP_KEYSTROKE_DELAY;
65 /* check if we have a keystroke */
72 /* sleep for LWP_KEYSTROKE_DELAY ms and let other
74 IOMGR_Select(0, 0, 0, 0, &twait);
76 if (seconds > 0) { /* we only worry about elapsed time if
77 * not looping forever (seconds < 0) */
78 /* now check elapsed time */
80 timeleft = seconds - difftime(nowTime, startTime);
88 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
89 * and then returns the line typed.
91 * This is trivial in unix, but requires some processing on NT.
92 * we basically read all chars into a buffer until we hit a newline and
93 * then return it to the user.
95 * n - a whole line has been read.(has n chars)
96 * 0 - buf not big enough.
99 int LWP_GetLine(char *linebuf, int len)
105 /* loop until a new line has been entered */
106 while (ch != '\r' && cnt < len-1)
108 LWP_WaitForKeystroke(-1);
110 if (ch == '\b') {/* print and throw away a backspace */
111 if (!cnt) /* if we are at the start of the line don't bspace */
113 /* print a space to delete char and move cursor back */
123 if (ch == '\r') { /* got a cr. translate to nl */
124 linebuf[cnt-1] = '\n';
129 else { /* buffer too small */
136 /* LWP_WaitForKeystroke(Unix) :Wait until a key has been struck or time (secconds)
137 * runs out and return to caller. The Unix version will actually wait until
138 * a <cr> has been entered before returning.
140 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
143 * 1: Keyboard input available
144 * 0: seconds elapsed. Timeout.
146 int LWP_WaitForKeystroke(int seconds)
150 struct timeval twait;
151 struct timeval *tp = NULL;
153 #ifdef AFS_LINUX20_ENV
154 if (stdin->_IO_read_ptr < stdin->_IO_read_end)
157 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
158 if (stdin->_bf._size > 0)
168 FD_SET(fileno(stdin), &rdfds);
171 twait.tv_sec = seconds;
176 code = IOMGR_Select(1+fileno(stdin), &rdfds, NULL, NULL, tp);
178 return (code == 1) ? 1 : 0;
181 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
182 * and then returns the line typed.
184 * This is trivial in unix, but requires some processing on NT.
185 * we basically read all chars into a buffer until we hit a newline and
186 * then return it to the user.
188 * n - a whole line has been read.(has n chars)
189 * 0 - buf not big enough.
192 int LWP_GetLine(char *linebuf, int len)
196 LWP_WaitForKeystroke(-1);
198 fgets(linebuf, len, stdin);
199 linelen = strlen(linebuf);
200 if (linebuf[linelen-1] != '\n') /* buffer too small */
206 #endif /* else NT40*/
208 /* LWP_GetResponseKey() - Waits for a specified period of time and
209 * returns a char when one has been typed by the user.
211 * seconds - how long to wait for a key press.
212 * *key - char entered by user
214 * 0 - Time ran out before the user typed a key.
215 * 1 - Valid char is being returned.
218 int LWP_GetResponseKey(int seconds, char *key)
223 return 0; /* need space to store char */
226 fflush(stdin); /* flush all existing data and start anew */
229 rc = LWP_WaitForKeystroke(seconds);
230 if (rc == 0) { /* time ran out */
235 /* now read the char. */
237 *key = getche(); /* get char and echo it to screen */