Code cosmetics
[mmh] / sbr / getans.c
1 /*
2 ** getans.c -- get an answer from the user and return a string array
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 <h/signals.h>
11 #include <setjmp.h>
12 #include <signal.h>
13
14 static char ansbuf[BUFSIZ];
15 static jmp_buf sigenv;
16
17 /*
18 ** static prototypes
19 */
20 static RETSIGTYPE intrser(int);
21
22
23 char **
24 getans(char *prompt, struct swit *ansp)
25 {
26         int i;
27         SIGNAL_HANDLER istat = NULL;
28         char *cp, **cpp;
29
30         if (!(setjmp(sigenv))) {
31                 istat = SIGNAL(SIGINT, intrser);
32         } else {
33                 SIGNAL(SIGINT, istat);
34                 return NULL;
35         }
36
37         for (;;) {
38                 printf("%s", prompt);
39                 fflush(stdout);
40                 cp = ansbuf;
41                 while ((i = getchar()) != '\n') {
42                         if (i == EOF)
43                                 longjmp(sigenv, 1);
44                         if (cp < &ansbuf[sizeof ansbuf - 1])
45                                 *cp++ = i;
46                 }
47                 *cp = '\0';
48                 if (ansbuf[0] == '?' || cp == ansbuf) {
49                         printf("Options are:\n");
50                         print_sw(ALL, ansp, "", stdout);
51                         continue;
52                 }
53                 cpp = brkstring(ansbuf, " ", NULL);
54                 switch (smatch(*cpp, ansp)) {
55                         case AMBIGSW:
56                                 ambigsw(*cpp, ansp);
57                                 continue;
58                         case UNKWNSW:
59                                 printf(" -%s unknown. Hit <CR> for help.\n", *cpp);
60                                 continue;
61                         default:
62                                 SIGNAL(SIGINT, istat);
63                                 return cpp;
64                 }
65         }
66 }
67
68
69 static RETSIGTYPE
70 intrser(int i)
71 {
72         /*
73         ** should this be siglongjmp?
74         */
75         longjmp(sigenv, 1);
76 }