]> diplodocus.org Git - nmh/blob - sbr/read_switch.c
Escape literal leading full stop in man/new.man.
[nmh] / sbr / read_switch.c
1
2 /*
3 * read_switch.c -- prompt the user for an answer from the list
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 read_switch (const char *prompt, const struct swit *ansp)
15 {
16 int i;
17 char *cp;
18 const struct swit *ap;
19 char ansbuf[BUFSIZ];
20
21 for (;;) {
22 fputs(prompt, stdout);
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 = tolower(i);
30 *cp++ = i;
31 }
32 }
33 *cp = '\0';
34 if (ansbuf[0] == '?' || cp == ansbuf) {
35 puts("Options are:");
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 }