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