]> diplodocus.org Git - nmh/blob - sbr/getans.c
We're not using the .Bu macro anymore.
[nmh] / sbr / getans.c
1
2 /*
3 * getans.c -- get an answer from the user and return a string array
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/signals.h>
14 #include <setjmp.h>
15 #include <signal.h>
16
17 static char ansbuf[BUFSIZ];
18 static jmp_buf sigenv;
19
20 /*
21 * static prototypes
22 */
23 static RETSIGTYPE intrser (int);
24
25
26 char **
27 getans (char *prompt, struct swit *ansp)
28 {
29 int i;
30 SIGNAL_HANDLER istat;
31 char *cp, **cpp;
32
33 if (!(setjmp (sigenv))) {
34 istat = SIGNAL (SIGINT, intrser);
35 } else {
36 SIGNAL (SIGINT, istat);
37 return NULL;
38 }
39
40 for (;;) {
41 printf ("%s", prompt);
42 fflush (stdout);
43 cp = ansbuf;
44 while ((i = getchar ()) != '\n') {
45 if (i == EOF)
46 longjmp (sigenv, 1);
47 if (cp < &ansbuf[sizeof ansbuf - 1])
48 *cp++ = i;
49 }
50 *cp = '\0';
51 if (ansbuf[0] == '?' || cp == ansbuf) {
52 printf ("Options are:\n");
53 print_sw (ALL, ansp, "");
54 continue;
55 }
56 cpp = brkstring (ansbuf, " ", NULL);
57 switch (smatch (*cpp, ansp)) {
58 case AMBIGSW:
59 ambigsw (*cpp, ansp);
60 continue;
61 case UNKWNSW:
62 printf (" -%s unknown. Hit <CR> for help.\n", *cpp);
63 continue;
64 default:
65 SIGNAL (SIGINT, istat);
66 return cpp;
67 }
68 }
69 }
70
71
72 static RETSIGTYPE
73 intrser (int i)
74 {
75 /*
76 * should this be siglongjmp?
77 */
78 longjmp (sigenv, 1);
79 }