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