Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / sbr / getans.c
1 /* getans.c - get an answer from the user and return a string array */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: getans.c,v 1.8 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #ifdef  BSD42
8 #include <setjmp.h>
9 #endif  /* BSD42 */
10 #include <signal.h>
11 #include <stdio.h>
12
13
14 static  char ansbuf[BUFSIZ];
15 #ifndef BSD42
16 static  int interrupted;
17 #else   /* BSD42 */
18 static  jmp_buf sigenv;
19 #endif  /* BSD42 */
20 static TYPESIG  intrser ();
21
22 char  **getans (prompt, ansp)
23 char   *prompt;
24 struct swit   *ansp;
25 {
26     int    i;
27     TYPESIG    (*istat) ();
28     char  *cp,
29          **cpp;
30
31 #ifndef BSD42
32     interrupted = 0;
33     istat = signal (SIGINT, intrser);
34 #else   /* BSD42 */
35     switch (setjmp (sigenv)) {
36         case OK: 
37             istat = signal (SIGINT, intrser);
38             break;
39
40         default: 
41             (void) signal (SIGINT, istat);
42             return NULL;
43     }
44 #endif  /* BSD42 */
45     for (;;) {
46         printf ("%s", prompt);
47         (void) fflush (stdout);
48         cp = ansbuf;
49         while ((i = getchar ()) != '\n') {
50 #ifndef BSD42
51             if (i == EOF || interrupted) {
52                 interrupted = 0;
53                 (void) signal (SIGINT, istat);
54                 return NULL;
55             }
56 #else   /* BSD42 */
57             if (i == EOF)
58                 longjmp (sigenv, DONE);
59 #endif  /* BSD42 */
60             if (cp < &ansbuf[sizeof ansbuf - 1])
61                 *cp++ = i;
62         }
63         *cp = 0;
64         if (ansbuf[0] == '?' || cp == ansbuf) {
65             printf ("Options are:\n");
66             printsw (ALL, ansp, "");
67             continue;
68         }
69         cpp = brkstring (ansbuf, " ", NULLCP);
70         switch (smatch (*cpp, ansp)) {
71             case AMBIGSW: 
72                 ambigsw (*cpp, ansp);
73                 continue;
74             case UNKWNSW: 
75                 printf (" -%s unknown. Hit <CR> for help.\n", *cpp);
76                 continue;
77             default: 
78                 (void) signal (SIGINT, istat);
79                 return cpp;
80         }
81     }
82 }
83
84
85 static  TYPESIG intrser (i)
86 int     i;
87 {
88 #ifndef BSD42
89         (void) signal(SIGINT, intrser);
90         interrupted = 1;
91 #else   /* BSD42 */
92         longjmp (sigenv, NOTOK);
93 #endif  /* BSD42 */
94 }