Standardize License information
[openafs.git] / src / lwp / test / test_key.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  * test_key - test waiting for keystrokes.
12  * Use this test as:
13  * 1) test_key - no arguments should wait until stuff followed by <cr>.
14  * 2) test_key -delay n - should wait n seconds and beep then exit. If data
15  *    entered before n seconds, should be printed.
16  * 3) test_key -delay n -iters j - does the above n times.
17  *    
18  */
19
20 #include <afs/param.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #ifdef AFS_NT40_ENV
24 #include <time.h>
25 #else
26 #include <sys/time.h>
27 #include <unistd.h>
28 #endif
29 #include <lwp.h>
30
31 /* prototypes */
32 void interTest();
33 void lineTest();
34
35 void Usage(void)
36 {
37     printf("Usage: test_key [-nobuf] [-delay seconds [-iters n]]\n");
38     printf("test_key waits for keystrokes. Without options it will wait indefinitely.\n");
39     printf("To show that LWP is still running there is a rotating '|' which cycles every quarter second.\n");
40     printf("-nobuf - don't buffer input. Don't need to wait for <cr>.\n");
41     printf("-delay seconds - wait for <seconds> for a keystroke.\n");
42     printf("-iters n - repeat the wait n times.\n");
43     printf("-inter - wait for key and beep every five seconds until pressed(overrides other options).\n");
44     printf("-line - wait for a whole line to be typed.");
45     exit(1);
46 }
47
48
49 int waitingForAnswer = 0;
50
51 static void PrintDots()
52 {
53     static struct timeval constSleepTime = { 1, 0 };
54     struct timeval sleepTime;
55
56     while (waitingForAnswer) {
57         sleepTime = constSleepTime;
58         IOMGR_Select(0, 0, 0, 0, &sleepTime);
59         if (!waitingForAnswer)
60             break;
61         printf("."); fflush(stdout);
62     }
63 }
64
65 static int DotWriter(char *junk)
66 {
67     while (1 ) {
68         LWP_WaitProcess(&waitingForAnswer);
69         PrintDots();
70     }
71     return 0;
72 }
73
74 void main(int ac, char **av)
75 {
76     int delay = 0;
77     int iters = 0;
78     int inter = 0;
79     int line = 0;
80     int i;
81     PROCESS dotpid;
82     int rc;
83
84     for (i=1; i<ac; i++) {
85         if (!strcmp("-delay", av[i])) {
86             if (++i >= ac) {
87                 printf("Missing delay time for -delay option.\n");
88             }
89             delay = atoi(av[i]);
90             if (delay < 0) {
91                 printf("Delay must be at least 0 seconds.\n");
92                 Usage();
93             }
94         }
95         else if(!strcmp("-iters", av[i])) {
96             if (++i >= ac) {
97                 printf("Missing iteration count for -iters option.\n");
98             }
99             iters = atoi(av[i]);
100             if (iters < 0) {
101                 printf("Number of iterations must be at least 0.\n");
102                 Usage();
103             }
104         }
105         else if (!strcmp("-nobuf", av[i])) {
106             rc = setvbuf(stdin, NULL, _IONBF, 0);
107             if (rc<0) {
108                 perror("Setting -nobuf for stdin");
109             }
110         }
111         else if (!strcmp("-inter", av[i])) {
112           inter = 1;
113         }
114         else if (!strcmp("-line", av[i])) {
115           line = 1;
116         }
117         else
118             Usage();
119     }
120
121     IOMGR_Initialize(); 
122
123     LWP_CreateProcess(DotWriter, 32000, LWP_NORMAL_PRIORITY, (char*)0,
124                       "DotWriter", &dotpid);
125
126     if (inter) {
127       interTest();
128       exit(1);
129     }
130     if (line) {
131       lineTest();
132       exit(1);
133     }
134     if (delay == 0) {
135         delay = -1; /* Means wait indefinitely. */
136     }
137     for (; iters >= 0; iters -- ) {
138         waitingForAnswer = 1;
139         LWP_NoYieldSignal(&waitingForAnswer);
140         rc = LWP_WaitForKeystroke(delay);
141         waitingForAnswer = 0;
142         if (rc) {
143             printf("\n'%c'\n", getchar());
144             printf("Flushing remaining input.\n");
145             while(LWP_WaitForKeystroke(0)) {
146                 printf("'%c'\n", getchar());
147             }
148         }
149         else {
150             printf("\nNo data available on this iteration.\n");
151         }
152     }
153         
154
155 }
156
157 /* interTest() : wait for key press and beep to remind user every five 
158  *    seconds.
159 */
160 void interTest()
161 {
162   char ch;
163   int rc;
164
165   printf("Will print dots until you hit a key!\n");
166   /* start the dotwriter thread running */
167   waitingForAnswer = 1;
168   LWP_NoYieldSignal(&waitingForAnswer); 
169   
170   do {
171     rc = LWP_GetResponseKey(5, &ch);
172     if (rc == 0)
173       printf("\a");
174   } while (rc == 0);
175
176   waitingForAnswer = 0; /* turn off dotwriter lwp */
177
178   printf("\nYou typed %c\n", ch);
179
180   return;
181 }
182   
183 /* lineTest() : wait until a whole line has been entered before processing.
184  */
185 void lineTest()
186 {
187   char ch;
188   int rc;
189   char line[256];
190
191   printf("Will print dots until enter a line\n");
192   /* start the dotwriter thread running */
193   waitingForAnswer = 1;
194   LWP_NoYieldSignal(&waitingForAnswer); 
195   
196   rc = LWP_GetLine(line, 256);
197
198   waitingForAnswer = 0;
199
200   printf("You entered : %s\n", line);
201   if (rc)
202     printf("linebuf was too small\n");
203
204   return;
205 }
206
207     
208       
209