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