]> diplodocus.org Git - nmh/blob - sbr/read_switch_multiword.c
new.c: Order two return statements to match comment.
[nmh] / sbr / read_switch_multiword.c
1 /* read_switch_multiword.c -- get an answer from the user and return a string array
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/signals.h>
10 #include <setjmp.h>
11
12 static char ansbuf[BUFSIZ];
13 static sigjmp_buf sigenv;
14
15 /*
16 * static prototypes
17 */
18 static void intrser (int);
19
20
21 char **
22 read_switch_multiword (const char *prompt, const struct swit *ansp)
23 {
24 int i;
25 SIGNAL_HANDLER istat = NULL;
26 char *cp, **cpp;
27
28 if (sigsetjmp(sigenv, 1)) {
29 SIGNAL (SIGINT, istat);
30 return NULL;
31 }
32 istat = SIGNAL (SIGINT, intrser);
33
34 for (;;) {
35 fputs(prompt, stdout);
36 fflush (stdout);
37 cp = ansbuf;
38 while ((i = getchar ()) != '\n') {
39 if (i == EOF) {
40 /*
41 * If we get an EOF, return
42 */
43 if (feof(stdin))
44 siglongjmp (sigenv, 1);
45
46 /*
47 * For errors, if we get an EINTR that means that we got
48 * a signal and we should retry. If we get another error,
49 * then just return.
50 */
51
52 if (ferror(stdin)) {
53 if (errno == EINTR) {
54 clearerr(stdin);
55 continue;
56 }
57 fprintf(stderr, "\nError %s during read\n",
58 strerror(errno));
59 siglongjmp (sigenv, 1);
60 }
61
62 /* Just for completeness's sake... */
63 fprintf(stderr, "\nUnknown problem in getchar()\n");
64 siglongjmp(sigenv, 1);
65 }
66 if (cp < &ansbuf[sizeof ansbuf - 1])
67 *cp++ = i;
68 }
69 *cp = '\0';
70 if (ansbuf[0] == '?' || cp == ansbuf) {
71 puts("Options are:");
72 print_sw (ALL, ansp, "", stdout);
73 continue;
74 }
75 cpp = brkstring (ansbuf, " ", NULL);
76 switch (smatch (*cpp, ansp)) {
77 case AMBIGSW:
78 ambigsw (*cpp, ansp);
79 continue;
80 case UNKWNSW:
81 printf (" -%s unknown. Hit <CR> for help.\n", *cpp);
82 continue;
83 default:
84 SIGNAL (SIGINT, istat);
85 return cpp;
86 }
87 }
88 }
89
90
91 static void
92 intrser (int i)
93 {
94 NMH_UNUSED (i);
95
96 siglongjmp (sigenv, 1);
97 }