2 * $Id: ui_getc.c,v 1.70 2018/06/14 00:05:05 tom Exp $
4 * ui_getc.c - user interface glue for getc()
6 * Copyright 2001-2013,2018 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
42 #ifdef HAVE_SYS_WAIT_H
47 #include <sys/select.h>
51 # ifdef HAVE_TYPE_UNIONWAIT
52 # define WEXITSTATUS(status) (status.w_retcode)
54 # define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
59 # ifdef HAVE_TYPE_UNIONWAIT
60 # define WTERMSIG(status) (status.w_termsig)
62 # define WTERMSIG(status) ((status) & 0x7f)
67 dlg_add_callback(DIALOG_CALLBACK * p)
69 p->next = dialog_state.getc_callbacks;
70 dialog_state.getc_callbacks = p;
71 wtimeout(p->win, WTIMEOUT_VAL);
75 * Like dlg_add_callback(), but providing for cleanup of caller's associated
79 dlg_add_callback_ref(DIALOG_CALLBACK ** p, DIALOG_FREEBACK freeback)
82 (*p)->freeback = freeback;
87 dlg_remove_callback(DIALOG_CALLBACK * p)
92 FILE *input = p->input;
94 if (p->input == dialog_state.pipe_input)
95 dialog_state.pipe_input = 0;
96 /* more than one callback can have the same input */
97 for (q = dialog_state.getc_callbacks; q != 0; q = q->next) {
98 if (q->input == input) {
105 dlg_del_window(p->win);
106 if ((q = dialog_state.getc_callbacks) == p) {
107 dialog_state.getc_callbacks = p->next;
118 /* handle dlg_add_callback_ref cleanup */
119 if (p->freeback != 0)
128 * A select() might find more than one input ready for service. Handle them
132 handle_inputs(WINDOW *win)
140 getyx(win, cur_y, cur_x);
141 for (p = dialog_state.getc_callbacks, q = 0; p != 0; p = q) {
143 if ((p->handle_input != 0) && p->input_ready) {
144 p->input_ready = FALSE;
148 if (p->handle_input(p)) {
154 (void) wmove(win, cur_y, cur_x); /* Restore cursor position */
163 may_handle_inputs(void)
169 for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
180 * Check any any inputs registered via callbacks, to see if there is any input
181 * available. If there is, return a file-descriptor which should be read.
182 * Otherwise, return -1.
195 if ((p = dialog_state.getc_callbacks) != 0) {
199 p->input_ready = FALSE;
200 if (p->input != 0 && (fd = fileno(p->input)) >= 0) {
201 FD_SET(fd, &read_fds);
209 test.tv_usec = WTIMEOUT_VAL * 1000;
210 found = select(last_fd + 1, &read_fds,
216 for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
218 && (fd = fileno(p->input)) >= 0
219 && FD_ISSET(fd, &read_fds)) {
220 p->input_ready = TRUE;
231 dlg_getc_callbacks(int ch, int fkey, int *result)
234 DIALOG_CALLBACK *p, *q;
236 if ((p = dialog_state.getc_callbacks) != 0) {
237 if (check_inputs() >= 0) {
240 if (p->input_ready) {
241 if (!(p->handle_getc(p, ch, fkey, result))) {
242 dlg_remove_callback(p);
245 } while ((p = q) != 0);
247 code = (dialog_state.getc_callbacks != 0);
253 dlg_raise_window(WINDOW *win)
256 wmove(win, getcury(win), getcurx(win));
262 * This is a work-around for the case where we actually need the wide-character
263 * code versus a byte stream.
265 static int last_getc = ERR;
267 #ifdef USE_WIDE_CURSES
268 static char last_getc_bytes[80];
269 static int have_last_getc;
270 static int used_last_getc;
276 #ifdef USE_WIDE_CURSES
277 if (used_last_getc != 1)
278 return ERR; /* not really an error... */
287 #ifdef USE_WIDE_CURSES
294 * Report the last key entered by the user. The 'mode' parameter controls
295 * the way it is separated from other results:
297 * -1 (separator after the key name)
298 * 0 (separator is optionally before the key name)
302 dlg_add_last_key(int mode)
304 if (dialog_vars.last_key) {
307 dlg_add_last_key(-1);
309 if (dlg_need_separator())
311 dlg_add_last_key(-2);
315 sprintf(temp, "%d", last_getc);
316 dlg_add_string(temp);
324 * Check if the stream has been unexpectedly closed, returning false in that
334 if (fcntl(fd, F_GETFL, 0) >= 0) {
342 really_getch(WINDOW *win, int *fkey)
345 #ifdef USE_WIDE_CURSES
352 * We get a wide character, translate it to multibyte form to avoid
353 * having to change the rest of the code to use wide-characters.
355 if (used_last_getc >= have_last_getc) {
360 code = wget_wch(win, &my_wint);
361 my_wchar = (wchar_t) my_wint;
364 ch = *fkey = my_wchar;
365 last_getc = my_wchar;
368 memset(&state, 0, sizeof(state));
369 have_last_getc = (int) wcrtomb(last_getc_bytes, my_wchar, &state);
370 if (have_last_getc < 0) {
371 have_last_getc = used_last_getc = 0;
372 last_getc_bytes[0] = (char) my_wchar;
374 ch = (int) CharOf(last_getc_bytes[used_last_getc++]);
375 last_getc = my_wchar;
385 ch = (int) CharOf(last_getc_bytes[used_last_getc++]);
390 *fkey = (ch > KEY_MIN && ch < KEY_MAX);
395 static DIALOG_CALLBACK *
396 next_callback(DIALOG_CALLBACK * p)
398 if ((p = dialog_state.getc_redirect) != 0) {
401 p = dialog_state.getc_callbacks;
406 static DIALOG_CALLBACK *
407 prev_callback(DIALOG_CALLBACK * p)
411 if ((p = dialog_state.getc_redirect) != 0) {
412 if (p == dialog_state.getc_callbacks) {
413 for (p = dialog_state.getc_callbacks; p->next != 0; p = p->next) ;
415 for (q = dialog_state.getc_callbacks; q->next != p; q = q->next) ;
419 p = dialog_state.getc_callbacks;
424 #define isBeforeChr(chr) ((chr) == before_chr && !before_fkey)
425 #define isBeforeFkey(chr) ((chr) == before_chr && before_fkey)
428 * Read a character from the given window. Handle repainting here (to simplify
429 * things in the calling application). Also, if input-callback(s) are set up,
430 * poll the corresponding files and handle the updates, e.g., for displaying a
434 dlg_getc(WINDOW *win, int *fkey)
436 WINDOW *save_win = win;
442 bool literal = FALSE;
443 DIALOG_CALLBACK *p = 0;
444 int interval = (dialog_vars.timeout_secs * 1000);
445 time_t expired = time((time_t *) 0) + dialog_vars.timeout_secs;
448 if (may_handle_inputs())
449 wtimeout(win, WTIMEOUT_VAL);
450 else if (interval > 0)
451 wtimeout(win, interval);
454 bool handle_others = FALSE;
457 * If there was no pending file-input, check the keyboard.
459 ch = really_getch(win, fkey);
468 ch = dlg_lookup_key(win, ch, fkey);
469 dlg_trace_chr(ch, *fkey);
471 current = time((time_t *) 0);
474 * If we acquired a fkey value, then it is one of dialog's builtin
475 * codes such as DLGK_HELPFILE.
477 if (!*fkey || *fkey != before_fkey) {
484 (void) touchwin(win);
485 (void) wrefresh(curscr);
487 case ERR: /* wtimeout() in effect; check for file I/O */
489 && current >= expired) {
490 DLG_TRACE(("# dlg_getc: timeout expired\n"));
493 } else if (!valid_file(stdin)
494 || !valid_file(dialog_state.screen_output)) {
495 DLG_TRACE(("# dlg_getc: input or output is invalid\n"));
498 } else if (check_inputs()) {
499 if (handle_inputs(win))
500 dlg_raise_window(win);
504 done = (interval <= 0);
508 if (dialog_vars.help_file) {
510 getyx(win, yold, xold);
511 dialog_helpfile("HELP", dialog_vars.help_file, 0, 0);
512 dlg_raise_window(win);
513 wmove(win, yold, xold);
516 case DLGK_FIELD_PREV:
520 case DLGK_FIELD_NEXT:
523 /* Handle tab/backtab as a special case for traversing between
524 * the nominal "current" window, and other windows having
525 * callbacks. If the nominal (control) window closes, we'll
526 * close the windows with callbacks.
528 if (dialog_state.getc_callbacks != 0 &&
530 isBeforeFkey(KEY_BTAB))) {
531 p = (isBeforeChr(TAB)
534 if ((dialog_state.getc_redirect = p) != 0) {
539 dlg_raise_window(win);
545 if (isBeforeChr(DLG_CTRL('P'))) {
546 /* for testing, ^P closes the connection */
553 handle_others = TRUE;
555 #ifdef HAVE_DLG_TRACE
562 handle_others = TRUE;
566 if ((p = dialog_state.getc_redirect) != 0) {
567 if (!(p->handle_getc(p, ch, *fkey, &result))) {
568 done = (p->win == save_win) && (!p->keep_win);
569 dlg_remove_callback(p);
570 dialog_state.getc_redirect = 0;
584 finish_bg(int sig GCC_UNUSED)
587 dlg_exit(DLG_EXIT_ERROR);
591 * If we have callbacks active, purge the list of all that are not marked
592 * to keep in the background. If any remain, run those in a background
596 dlg_killall_bg(int *retval)
600 #ifdef HAVE_TYPE_UNIONWAIT
606 if ((cb = dialog_state.getc_callbacks) != 0) {
611 dlg_remove_callback(cb);
612 cb = dialog_state.getc_callbacks;
615 if (dialog_state.getc_callbacks != 0) {
621 if ((pid = fork()) != 0) {
622 _exit(pid > 0 ? DLG_EXIT_OK : DLG_EXIT_ERROR);
623 } else if (pid == 0) { /* child */
624 if ((pid = fork()) != 0) {
626 * Echo the process-id of the grandchild so a shell script
627 * can read that, and kill that process. We'll wait around
628 * until then. Our parent has already left, leaving us
629 * temporarily orphaned.
631 if (pid > 0) { /* parent */
632 fprintf(stderr, "%d\n", pid);
637 while (-1 == waitpid(pid, &wstatus, 0)) {
643 if (errno == ERESTARTSYS)
645 #endif /* ERESTARTSYS */
649 while (wait(&wstatus) != pid) /* do nothing */
652 _exit(WEXITSTATUS(wstatus));
653 } else if (pid == 0) {
654 if (!dialog_vars.cant_kill)
655 (void) signal(SIGHUP, finish_bg);
656 (void) signal(SIGINT, finish_bg);
657 (void) signal(SIGQUIT, finish_bg);
658 (void) signal(SIGSEGV, finish_bg);
659 while (dialog_state.getc_callbacks != 0) {
661 dlg_getc_callbacks(ERR, fkey, retval);