source: trunk/third/nmh/sbr/gans.c @ 12455

Revision 12455, 921 bytes checked in by danw, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12454, which included commits to RCS files with non-trunk default branches.
Line 
1
2/*
3 * gans.c -- get an answer from the user
4 *
5 * $Id: gans.c,v 1.1.1.1 1999-02-07 18:14:08 danw Exp $
6 */
7
8#include <h/mh.h>
9
10
11int
12gans (char *prompt, struct swit *ansp)
13{
14    register int i;
15    register char *cp;
16    register struct swit *ap;
17    char ansbuf[BUFSIZ];
18
19    for (;;) {
20        printf ("%s", prompt);
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#ifdef LOCALE
28                i = (isalpha(i) && isupper(i)) ? tolower(i) : i;
29#else
30                if (i >= 'A' && i <= 'Z')
31                    i += 'a' - 'A';
32#endif
33                *cp++ = i;
34            }
35        }
36        *cp = '\0';
37        if (ansbuf[0] == '?' || cp == ansbuf) {
38            printf ("Options are:\n");
39            for (ap = ansp; ap->sw; ap++)
40                printf ("  %s\n", ap->sw);
41            continue;
42        }
43        if ((i = smatch (ansbuf, ansp)) < 0) {
44            printf ("%s: %s.\n", ansbuf, i == -1 ? "unknown" : "ambiguous");
45            continue;
46        }
47        return i;
48    }
49}
Note: See TracBrowser for help on using the repository browser.