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 * test_key - test waiting for keystrokes.
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.
20 #include <afs/param.h>
22 #include <sys/types.h>
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.");
49 int waitingForAnswer = 0;
51 static void PrintDots()
53 static struct timeval constSleepTime = { 1, 0 };
54 struct timeval sleepTime;
56 while (waitingForAnswer) {
57 sleepTime = constSleepTime;
58 IOMGR_Select(0, 0, 0, 0, &sleepTime);
59 if (!waitingForAnswer)
61 printf("."); fflush(stdout);
65 static int DotWriter(char *junk)
68 LWP_WaitProcess(&waitingForAnswer);
74 void main(int ac, char **av)
84 for (i=1; i<ac; i++) {
85 if (!strcmp("-delay", av[i])) {
87 printf("Missing delay time for -delay option.\n");
91 printf("Delay must be at least 0 seconds.\n");
95 else if(!strcmp("-iters", av[i])) {
97 printf("Missing iteration count for -iters option.\n");
101 printf("Number of iterations must be at least 0.\n");
105 else if (!strcmp("-nobuf", av[i])) {
106 rc = setvbuf(stdin, NULL, _IONBF, 0);
108 perror("Setting -nobuf for stdin");
111 else if (!strcmp("-inter", av[i])) {
114 else if (!strcmp("-line", av[i])) {
123 LWP_CreateProcess(DotWriter, 32000, LWP_NORMAL_PRIORITY, (char*)0,
124 "DotWriter", &dotpid);
135 delay = -1; /* Means wait indefinitely. */
137 for (; iters >= 0; iters -- ) {
138 waitingForAnswer = 1;
139 LWP_NoYieldSignal(&waitingForAnswer);
140 rc = LWP_WaitForKeystroke(delay);
141 waitingForAnswer = 0;
143 printf("\n'%c'\n", getchar());
144 printf("Flushing remaining input.\n");
145 while(LWP_WaitForKeystroke(0)) {
146 printf("'%c'\n", getchar());
150 printf("\nNo data available on this iteration.\n");
157 /* interTest() : wait for key press and beep to remind user every five
165 printf("Will print dots until you hit a key!\n");
166 /* start the dotwriter thread running */
167 waitingForAnswer = 1;
168 LWP_NoYieldSignal(&waitingForAnswer);
171 rc = LWP_GetResponseKey(5, &ch);
176 waitingForAnswer = 0; /* turn off dotwriter lwp */
178 printf("\nYou typed %c\n", ch);
183 /* lineTest() : wait until a whole line has been entered before processing.
191 printf("Will print dots until enter a line\n");
192 /* start the dotwriter thread running */
193 waitingForAnswer = 1;
194 LWP_NoYieldSignal(&waitingForAnswer);
196 rc = LWP_GetLine(line, 256);
198 waitingForAnswer = 0;
200 printf("You entered : %s\n", line);
202 printf("linebuf was too small\n");