Convert to the use of siglongjmp()
[mmh] / 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 <h/m_setjmp.h>
13 #include <signal.h>
14
15 static char ansbuf[BUFSIZ];
16 static sigjmp_buf sigenv;
17
18 /*
19  * static prototypes
20  */
21 static void 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 (!(sigsetjmp(sigenv, 1))) {
32         istat = SIGNAL (SIGINT, intrser);
33     } else {
34         SIGNAL (SIGINT, istat);
35         printf("returning NULL\n");
36         return NULL;
37     }
38
39     for (;;) {
40         printf ("%s", prompt);
41         fflush (stdout);
42         cp = ansbuf;
43         while ((i = getchar ()) != '\n') {
44             if (i == EOF) {
45                 printf("Got EOF\n");
46                 siglongjmp (sigenv, 1);
47             }
48             if (cp < &ansbuf[sizeof ansbuf - 1])
49                 *cp++ = i;
50         }
51         *cp = '\0';
52         if (ansbuf[0] == '?' || cp == ansbuf) {
53             printf ("Options are:\n");
54             print_sw (ALL, ansp, "", stdout);
55             continue;
56         }
57         cpp = brkstring (ansbuf, " ", NULL);
58         switch (smatch (*cpp, ansp)) {
59             case AMBIGSW: 
60                 ambigsw (*cpp, ansp);
61                 continue;
62             case UNKWNSW: 
63                 printf (" -%s unknown. Hit <CR> for help.\n", *cpp);
64                 continue;
65             default: 
66                 SIGNAL (SIGINT, istat);
67                 return cpp;
68         }
69     }
70 }
71
72
73 static void
74 intrser (int i)
75 {
76     NMH_UNUSED (i);
77
78     /*
79      * should this be siglongjmp?
80      */
81     siglongjmp (sigenv, 1);
82 }