Code cosmetics
[mmh] / sbr / gans.c
1 /*
2 ** gans.c -- get an answer from the user
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11
12 int
13 gans(char *prompt, struct swit *ansp)
14 {
15         register int i;
16         register char *cp;
17         register struct swit *ap;
18         char ansbuf[BUFSIZ];
19
20         for (;;) {
21                 printf("%s", prompt);
22                 fflush(stdout);
23                 cp = ansbuf;
24                 while ((i = getchar()) != '\n') {
25                         if (i == EOF)
26                                 return 0;
27                         if (cp < &ansbuf[sizeof ansbuf - 1]) {
28 #ifdef LOCALE
29                                 i = (isalpha(i) && isupper(i)) ? tolower(i) : i;
30 #else
31                                 if (i >= 'A' && i <= 'Z')
32                                         i += 'a' - 'A';
33 #endif
34                                 *cp++ = i;
35                         }
36                 }
37                 *cp = '\0';
38                 if (ansbuf[0] == '?' || cp == ansbuf) {
39                         printf("Options are:\n");
40                         for (ap = ansp; ap->sw; ap++)
41                                 printf("  %s\n", ap->sw);
42                         continue;
43                 }
44                 if ((i = smatch(ansbuf, ansp)) < 0) {
45                         printf("%s: %s.\n", ansbuf, i == -1 ? "unknown" : "ambiguous");
46                         continue;
47                 }
48                 return i;
49         }
50 }