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>
27 #ifdef HAVE_STDIO_EXT_H
28 #include <stdio_ext.h>
37 #define LWP_KEYSTROKE_DELAY 250 /* 250ms. Must be < 1000 */
38 #define LWP_MAXLINELEN 256
41 /* LWP_WaitForKeystroke : Wait until a key has been struck or time (secconds)
42 * runs out and return to caller. The NT version of this function will return
43 * immediately after a key has been pressed (doesn't wait for cr).
45 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
48 * 1: Keyboard input available
49 * 0: seconds elapsed. Timeout.
52 LWP_WaitForKeystroke(int seconds)
54 time_t startTime, nowTime;
56 #ifndef AFS_PTHREAD_ENV
60 twait.tv_usec = LWP_KEYSTROKE_DELAY;
69 /* check if we have a keystroke */
76 /* sleep for LWP_KEYSTROKE_DELAY ms and let other
78 #ifdef AFS_PTHREAD_ENV
79 Sleep(LWP_KEYSTROKE_DELAY);
81 IOMGR_Select(0, 0, 0, 0, &twait);
83 if (seconds > 0) { /* we only worry about elapsed time if
84 * not looping forever (seconds < 0) */
85 /* now check elapsed time */
87 timeleft = seconds - difftime(nowTime, startTime);
95 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
96 * and then returns the line typed.
98 * This is trivial in unix, but requires some processing on NT.
99 * we basically read all chars into a buffer until we hit a newline and
100 * then return it to the user.
102 * n - a whole line has been read.(has n chars)
103 * 0 - buf not big enough.
104 * -1 - line with only EOF
108 LWP_GetLine(char *linebuf, int len)
114 /* loop until a new line has been entered */
115 while (ch != '\r' && cnt < len - 1) {
116 LWP_WaitForKeystroke(-1);
119 if ((ch == EOF) && (cnt == 0))
122 if (ch == '\b') { /* print and throw away a backspace */
123 if (!cnt) /* if we are at the start of the line don't bspace */
125 /* print a space to delete char and move cursor back */
134 if (ch == '\r') { /* got a cr. translate to nl */
135 linebuf[cnt - 1] = '\n';
139 } else { /* buffer too small */
146 /* LWP_WaitForKeystroke(Unix) :Wait until a key has been struck or time (secconds)
147 * runs out and return to caller. The Unix version will actually wait until
148 * a <cr> has been entered before returning.
150 * seconds: wait for <seconds> seconds before returning. If seconds < 0,
153 * 1: Keyboard input available
154 * 0: seconds elapsed. Timeout.
157 LWP_WaitForKeystroke(int seconds)
161 struct timeval twait;
162 struct timeval *tp = NULL;
164 #if defined(HAVE_STDIO_EXT_H)
165 if (__fbufsize(stdin) > 0)
167 #elif defined(AFS_LINUX20_ENV)
168 if (stdin->_IO_read_ptr < stdin->_IO_read_end)
170 #elif (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(AFS_DFBSD_ENV)
172 unsigned char *_base;
177 struct __FILE_public pub;
178 struct appx_sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
180 struct APPX_FILE *appx_stdin = (struct APPX_FILE *) stdin;
181 if (appx_stdin->_bf._size > 0)
183 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
184 if (stdin->_bf._size > 0)
192 FD_SET(fileno(stdin), &rdfds);
195 twait.tv_sec = seconds;
200 #ifdef AFS_PTHREAD_ENV
201 code = select(1 + fileno(stdin), &rdfds, NULL, NULL, tp);
203 code = IOMGR_Select(1 + fileno(stdin), &rdfds, NULL, NULL, tp);
206 return (code == 1) ? 1 : 0;
209 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
210 * and then returns the line typed.
212 * This is trivial in unix, but requires some processing on NT.
213 * we basically read all chars into a buffer until we hit a newline and
214 * then return it to the user.
216 * n - a whole line has been read.(has n chars)
217 * 0 - buf not big enough.
218 * -1 - line with only EOF
222 LWP_GetLine(char *linebuf, int len)
227 LWP_WaitForKeystroke(-1);
229 s = fgets(linebuf, len, stdin);
233 linelen = strlen(linebuf);
234 if (linebuf[linelen - 1] != '\n') /* buffer too small */
240 #endif /* else NT40 */
242 /* LWP_GetResponseKey() - Waits for a specified period of time and
243 * returns a char when one has been typed by the user.
245 * seconds - how long to wait for a key press.
246 * *key - char entered by user
248 * 0 - Time ran out before the user typed a key.
249 * 1 - Valid char is being returned.
253 LWP_GetResponseKey(int seconds, char *key)
258 return 0; /* need space to store char */
261 fflush(stdin); /* flush all existing data and start anew */
264 rc = LWP_WaitForKeystroke(seconds);
265 if (rc == 0) { /* time ran out */
270 /* now read the char. */
272 *key = getche(); /* get char and echo it to screen */
274 *key = (char)getchar();