]> diplodocus.org Git - nmh/blob - sbr/read_switch.c
8cdbfe4ff0234a38a85d45f1ffeba0ff7af4032c
[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 #include "smatch.h"
10
11
12 int
13 read_switch (const char *prompt, const struct swit *ansp)
14 {
15 int i;
16 char *cp;
17 const struct swit *ap;
18 char ansbuf[BUFSIZ];
19
20 for (;;) {
21 fputs(prompt, stdout);
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 i = tolower(i);
29 *cp++ = i;
30 }
31 }
32 *cp = '\0';
33 if (ansbuf[0] == '?' || cp == ansbuf) {
34 puts("Options are:");
35 for (ap = ansp; ap->sw; ap++)
36 printf (" %s\n", ap->sw);
37 continue;
38 }
39 if ((i = smatch (ansbuf, ansp)) < 0) {
40 printf ("%s: %s.\n", ansbuf, i == -1 ? "unknown" : "ambiguous");
41 continue;
42 }
43 return i;
44 }
45 }