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