]>
diplodocus.org Git - nmh/blob - sbr/getans.c
3 * getans.c -- get an answer from the user and return a string array
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.
11 #include <h/signals.h>
16 static char ansbuf
[BUFSIZ
];
17 static sigjmp_buf sigenv
;
22 static void intrser (int);
26 getans (char *prompt
, struct swit
*ansp
)
29 SIGNAL_HANDLER istat
= NULL
;
32 if (!(sigsetjmp(sigenv
, 1))) {
33 istat
= SIGNAL (SIGINT
, intrser
);
35 SIGNAL (SIGINT
, istat
);
40 printf ("%s", prompt
);
43 while ((i
= getchar ()) != '\n') {
46 * If we get an EOF, return
49 siglongjmp (sigenv
, 1);
52 * For errors, if we get an EINTR that means that we got
53 * a signal and we should retry. If we get another error,
57 else if (ferror(stdin
)) {
62 fprintf(stderr
, "\nError %s during read\n",
64 siglongjmp (sigenv
, 1);
67 * Just for completeness's sake ...
70 fprintf(stderr
, "\nUnknown problem in getchar()\n");
71 siglongjmp (sigenv
, 1);
74 if (cp
< &ansbuf
[sizeof ansbuf
- 1])
78 if (ansbuf
[0] == '?' || cp
== ansbuf
) {
79 printf ("Options are:\n");
80 print_sw (ALL
, ansp
, "", stdout
);
83 cpp
= brkstring (ansbuf
, " ", NULL
);
84 switch (smatch (*cpp
, ansp
)) {
89 printf (" -%s unknown. Hit <CR> for help.\n", *cpp
);
92 SIGNAL (SIGINT
, istat
);
105 * should this be siglongjmp?
107 siglongjmp (sigenv
, 1);