afsconfig-and-rcsid-all-around-20010705
[openafs.git] / src / lwp / waitkey.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * LWP_WaitForKeystroke - wait indefinitely or for a specified number of
12  * seconds for keyboard input.
13  *
14  * If seconds < 0, LWP_WaitForKeystroke will wait indefinitely. 
15  * If seconds == 0, LWP_WaitForKeystroke will just determine if data is now
16  *      present.
17  * Otherwise, wait "seconds" for data.
18  *
19  * Return 1 if data available.
20  */
21
22 #include <afs/param.h>
23 #include <afsconfig.h>
24
25 RCSID("$Header$");
26
27 #include <stdio.h>
28 #include <sys/types.h>
29 #ifdef AFS_NT40_ENV
30 #include <time.h>
31 #include <conio.h>
32 #include <assert.h>
33 #else
34 #include <sys/time.h>
35 #include <unistd.h>
36 #endif
37 #include "lwp.h"
38
39 #define LWP_KEYSTROKE_DELAY   250 /* 250ms. Must be < 1000 */
40 #define LWP_MAXLINELEN  256
41
42 #ifdef AFS_NT40_ENV
43 /* LWP_WaitForKeystroke : Wait until a key has been struck or time (secconds)
44  * runs out and return to caller. The NT version of this function will return
45  * immediately after a key has been pressed (doesn't wait for cr).
46  * Input:
47  *   seconds: wait for <seconds> seconds before returning. If seconds < 0,
48  *            wait infinitely.
49  * Return Value:
50  *    1:  Keyboard input available
51  *    0:  seconds elapsed. Timeout.
52  */
53 int LWP_WaitForKeystroke(int seconds)
54 {
55   time_t startTime, nowTime;
56   double timeleft = 1;
57   struct timeval twait;
58
59   time(&startTime); 
60
61   twait.tv_sec = 0;
62   twait.tv_usec = LWP_KEYSTROKE_DELAY;
63
64   if (seconds >= 0)
65       timeleft = seconds; 
66
67   do
68   {
69       /* check if we have a keystroke */
70       if (_kbhit()) 
71           return 1;
72       
73       if (timeleft == 0)
74           break;
75
76       /* sleep for  LWP_KEYSTROKE_DELAY ms and let other
77        * process run some*/
78       IOMGR_Select(0, 0, 0, 0, &twait);
79
80       if (seconds > 0) { /* we only worry about elapsed time if 
81                            * not looping forever (seconds < 0) */
82           /* now check elapsed time */
83           time(&nowTime);
84           timeleft = seconds - difftime(nowTime, startTime);
85       }
86   }
87   while(timeleft > 0);
88
89   return 0;
90 }
91
92 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
93  * and then returns the line typed.
94  * 
95  * This is trivial in unix, but requires some processing on NT.
96  *   we basically read all chars into a buffer until we hit a newline and
97  *   then return it to the user.
98  * Return Value:
99  *   n - a whole line has been read.(has n chars)
100  *   0 - buf not big enough.
101  */
102
103 int LWP_GetLine(char *linebuf, int len)
104 {
105   int cnt = 0;
106   char ch = '\0';
107
108   fflush(stdin);
109   /* loop until a new line has been entered */
110   while (ch != '\r' && cnt < len-1) 
111     { 
112       LWP_WaitForKeystroke(-1);
113       ch = getch(); 
114       if (ch == '\b') {/* print and throw away a backspace */
115         if (!cnt) /* if we are at the start of the line don't bspace */
116           continue;
117         /* print a space to delete char and move cursor back */
118         printf("\b \b");
119         cnt--; 
120       }
121       else {
122         putchar(ch);
123         linebuf[cnt++] = ch;
124       }
125     }
126
127   if (ch == '\r') { /* got a cr. translate to nl */
128     linebuf[cnt-1] = '\n'; 
129     linebuf[cnt]='\0';
130     putchar('\n');
131     return cnt;
132   }
133   else { /* buffer too small */
134     linebuf[cnt] = '\0';
135     return 0;
136   }
137     
138 }
139 #else
140 /* LWP_WaitForKeystroke(Unix) :Wait until a key has been struck or time (secconds)
141  * runs out and return to caller. The Unix version will actually wait until
142  * a <cr> has been entered before returning. 
143  * Input:
144  *   seconds: wait for <seconds> seconds before returning. If seconds < 0,
145  *            wait infinitely.
146  * Return Value:
147  *    1:  Keyboard input available
148  *    0:  seconds elapsed. Timeout.
149  */
150 int LWP_WaitForKeystroke(int seconds)
151 {
152     fd_set rdfds;
153     int code;
154     struct timeval twait;
155     struct timeval *tp = NULL;
156
157 #ifndef AFS_DJGPP_ENV
158 #ifdef AFS_LINUX20_ENV
159     if (stdin->_IO_read_ptr < stdin->_IO_read_end)
160         return 1;
161 #else
162 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
163     if (stdin->_bf._size > 0)
164         return 1;
165 #else
166     if (stdin->_cnt > 0)
167         return 1;
168 #endif
169 #endif
170 #else /* DJGPP */
171     if (stdin->_cnt > 0)
172         return 1;
173 #endif /* DJGPP */
174
175     FD_ZERO(&rdfds);
176     FD_SET(fileno(stdin), &rdfds);
177
178     if (seconds>=0) {
179         twait.tv_sec = seconds;
180         twait.tv_usec = 0;
181         tp = &twait;
182     }
183
184     code = IOMGR_Select(1+fileno(stdin), &rdfds, NULL, NULL, tp);
185
186     return (code == 1) ? 1 : 0;
187 }
188
189 /* LWP_GetLine() - Waits indefinitely until a newline has been typed
190  * and then returns the line typed.
191  * 
192  * This is trivial in unix, but requires some processing on NT.
193  *   we basically read all chars into a buffer until we hit a newline and
194  *   then return it to the user.
195  * Return Value:
196  *   n - a whole line has been read.(has n chars)
197  *   0 - buf not big enough.
198  */
199
200 int LWP_GetLine(char *linebuf, int len)
201 {
202   int linelen;
203
204   LWP_WaitForKeystroke(-1);
205
206   fgets(linebuf, len, stdin);
207   linelen = strlen(linebuf);
208   if (linebuf[linelen-1] != '\n') /* buffer too small */
209     return 0;
210   else
211     return linelen;
212 }
213   
214 #endif /* else NT40*/
215
216 /* LWP_GetResponseKey() - Waits for a specified period of time and
217  * returns a char when one has been typed by the user.
218  * Input:
219  *    seconds - how long to wait for a key press.
220  *    *key    - char entered by user
221  * Return Values: 
222  *    0 - Time ran out before the user typed a key.
223  *    1 - Valid char is being returned.
224  */
225
226 int LWP_GetResponseKey(int seconds, char *key)
227 {
228   int rc;
229
230   if (key == NULL)
231     return 0;     /* need space to store char */
232
233   
234   fflush(stdin); /* flush all existing data and start anew */
235
236   
237   rc = LWP_WaitForKeystroke(seconds);
238   if (rc == 0) { /* time ran out */
239     *key = 0;
240     return rc;
241   }
242   
243   /* now read the char. */
244 #ifdef AFS_NT40_ENV
245   *key = getche(); /* get char and echo it to screen */
246 #else
247   *key = getchar();
248 #endif
249
250   return rc;
251 }