1 /* 2 ** gans.c -- get an answer from the user 3 ** 4 ** This code is Copyright (c) 2002, by the authors of nmh. See the 5 ** COPYRIGHT file in the root directory of the nmh distribution for 6 ** complete copyright information. 7 */ 8 9 #include <h/mh.h> 10 #include <ctype.h> 11 12 13 int gans(char * prompt,struct swit * ansp)14gans(char *prompt, struct swit *ansp) 15 { 16 int i; 17 char *cp; 18 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 *cp++ = tolower(i); 30 } 31 } 32 *cp = '\0'; 33 if (ansbuf[0] == '?' || cp == ansbuf) { 34 printf("Options are:\n"); 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 } 46