2 * $Id: ui_getc.c,v 1.66 2012/11/30 01:56:14 tom Exp $
4 * ui_getc.c - user interface glue for getc()
6 * Copyright 2001-2011,2012 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)
93 if (p->input == dialog_state.pipe_input)
94 dialog_state.pipe_input = 0;
99 dlg_del_window(p->win);
100 if ((q = dialog_state.getc_callbacks) == p) {
101 dialog_state.getc_callbacks = p->next;
112 /* handle dlg_add_callback_ref cleanup */
113 if (p->freeback != 0)
122 * A select() might find more than one input ready for service. Handle them
126 handle_inputs(WINDOW *win)
134 getyx(win, cur_y, cur_x);
135 for (p = dialog_state.getc_callbacks, q = 0; p != 0; p = q) {
137 if ((p->handle_input != 0) && p->input_ready) {
138 p->input_ready = FALSE;
142 if (p->handle_input(p)) {
148 (void) wmove(win, cur_y, cur_x); /* Restore cursor position */
156 may_handle_inputs(void)
162 for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
173 * Check any any inputs registered via callbacks, to see if there is any input
174 * available. If there is, return a file-descriptor which should be read.
175 * Otherwise, return -1.
188 if ((p = dialog_state.getc_callbacks) != 0) {
192 p->input_ready = FALSE;
193 if (p->input != 0 && (fd = fileno(p->input)) >= 0) {
194 FD_SET(fd, &read_fds);
202 test.tv_usec = WTIMEOUT_VAL * 1000;
203 found = select(last_fd + 1, &read_fds,
209 for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
211 && (fd = fileno(p->input)) >= 0
212 && FD_ISSET(fd, &read_fds)) {
213 p->input_ready = TRUE;
224 dlg_getc_callbacks(int ch, int fkey, int *result)
227 DIALOG_CALLBACK *p, *q;
229 if ((p = dialog_state.getc_callbacks) != 0) {
230 if (check_inputs() >= 0) {
233 if (p->input_ready) {
234 if (!(p->handle_getc(p, ch, fkey, result))) {
235 dlg_remove_callback(p);
238 } while ((p = q) != 0);
240 code = (dialog_state.getc_callbacks != 0);
246 dlg_raise_window(WINDOW *win)
249 wmove(win, getcury(win), getcurx(win));
255 * This is a work-around for the case where we actually need the wide-character
256 * code versus a byte stream.
258 static int last_getc = ERR;
260 #ifdef USE_WIDE_CURSES
261 static char last_getc_bytes[80];
262 static int have_last_getc;
263 static int used_last_getc;
269 #ifdef USE_WIDE_CURSES
270 if (used_last_getc != 1)
271 return ERR; /* not really an error... */
280 #ifdef USE_WIDE_CURSES
287 * Check if the stream has been unexpectedly closed, returning false in that
297 if (fcntl(fd, F_GETFL, 0) >= 0) {
305 really_getch(WINDOW *win, int *fkey)
308 #ifdef USE_WIDE_CURSES
315 * We get a wide character, translate it to multibyte form to avoid
316 * having to change the rest of the code to use wide-characters.
318 if (used_last_getc >= have_last_getc) {
323 code = wget_wch(win, &my_wint);
324 my_wchar = (wchar_t) my_wint;
327 ch = *fkey = my_wchar;
328 last_getc = my_wchar;
331 memset(&state, 0, sizeof(state));
332 have_last_getc = (int) wcrtomb(last_getc_bytes, my_wchar, &state);
333 if (have_last_getc < 0) {
334 have_last_getc = used_last_getc = 0;
335 last_getc_bytes[0] = (char) my_wchar;
337 ch = (int) CharOf(last_getc_bytes[used_last_getc++]);
338 last_getc = my_wchar;
348 ch = (int) CharOf(last_getc_bytes[used_last_getc++]);
353 *fkey = (ch > KEY_MIN && ch < KEY_MAX);
358 static DIALOG_CALLBACK *
359 next_callback(DIALOG_CALLBACK * p)
361 if ((p = dialog_state.getc_redirect) != 0) {
364 p = dialog_state.getc_callbacks;
369 static DIALOG_CALLBACK *
370 prev_callback(DIALOG_CALLBACK * p)
374 if ((p = dialog_state.getc_redirect) != 0) {
375 if (p == dialog_state.getc_callbacks) {
376 for (p = dialog_state.getc_callbacks; p->next != 0; p = p->next) ;
378 for (q = dialog_state.getc_callbacks; q->next != p; q = q->next) ;
382 p = dialog_state.getc_callbacks;
387 #define isBeforeChr(chr) ((chr) == before_chr && !before_fkey)
388 #define isBeforeFkey(chr) ((chr) == before_chr && before_fkey)
391 * Read a character from the given window. Handle repainting here (to simplify
392 * things in the calling application). Also, if input-callback(s) are set up,
393 * poll the corresponding files and handle the updates, e.g., for displaying a
397 dlg_getc(WINDOW *win, int *fkey)
399 WINDOW *save_win = win;
405 bool literal = FALSE;
406 DIALOG_CALLBACK *p = 0;
407 int interval = (dialog_vars.timeout_secs * 1000);
408 time_t expired = time((time_t *) 0) + dialog_vars.timeout_secs;
411 if (may_handle_inputs())
412 wtimeout(win, WTIMEOUT_VAL);
413 else if (interval > 0)
414 wtimeout(win, interval);
417 bool handle_others = FALSE;
420 * If there was no pending file-input, check the keyboard.
422 ch = really_getch(win, fkey);
431 ch = dlg_lookup_key(win, ch, fkey);
432 dlg_trace_chr(ch, *fkey);
434 current = time((time_t *) 0);
437 * If we acquired a fkey value, then it is one of dialog's builtin
438 * codes such as DLGK_HELPFILE.
440 if (!*fkey || *fkey != before_fkey) {
447 (void) touchwin(win);
448 (void) wrefresh(curscr);
450 case ERR: /* wtimeout() in effect; check for file I/O */
452 && current >= expired) {
453 dlg_exiterr("timeout");
455 if (!valid_file(stdin)
456 || !valid_file(dialog_state.screen_output)) {
459 } else if (check_inputs()) {
460 if (handle_inputs(win))
461 dlg_raise_window(win);
465 done = (interval <= 0);
469 if (dialog_vars.help_file) {
471 getyx(win, yold, xold);
472 dialog_helpfile("HELP", dialog_vars.help_file, 0, 0);
473 dlg_raise_window(win);
474 wmove(win, yold, xold);
477 case DLGK_FIELD_PREV:
481 case DLGK_FIELD_NEXT:
484 /* Handle tab/backtab as a special case for traversing between
485 * the nominal "current" window, and other windows having
486 * callbacks. If the nominal (control) window closes, we'll
487 * close the windows with callbacks.
489 if (dialog_state.getc_callbacks != 0 &&
491 isBeforeFkey(KEY_BTAB))) {
492 p = (isBeforeChr(TAB)
495 if ((dialog_state.getc_redirect = p) != 0) {
500 dlg_raise_window(win);
506 if (isBeforeChr(DLG_CTRL('P'))) {
507 /* for testing, ^P closes the connection */
514 handle_others = TRUE;
516 #ifdef HAVE_DLG_TRACE
523 handle_others = TRUE;
527 if ((p = dialog_state.getc_redirect) != 0) {
528 if (!(p->handle_getc(p, ch, *fkey, &result))) {
529 done = (p->win == save_win) && (!p->keep_win);
530 dlg_remove_callback(p);
531 dialog_state.getc_redirect = 0;
545 finish_bg(int sig GCC_UNUSED)
548 dlg_exit(DLG_EXIT_ERROR);
552 * If we have callbacks active, purge the list of all that are not marked
553 * to keep in the background. If any remain, run those in a background
557 dlg_killall_bg(int *retval)
561 #ifdef HAVE_TYPE_UNIONWAIT
567 if ((cb = dialog_state.getc_callbacks) != 0) {
572 dlg_remove_callback(cb);
573 cb = dialog_state.getc_callbacks;
576 if (dialog_state.getc_callbacks != 0) {
582 if ((pid = fork()) != 0) {
583 _exit(pid > 0 ? DLG_EXIT_OK : DLG_EXIT_ERROR);
584 } else if (pid == 0) { /* child */
585 if ((pid = fork()) != 0) {
587 * Echo the process-id of the grandchild so a shell script
588 * can read that, and kill that process. We'll wait around
589 * until then. Our parent has already left, leaving us
590 * temporarily orphaned.
592 if (pid > 0) { /* parent */
593 fprintf(stderr, "%d\n", pid);
598 while (-1 == waitpid(pid, &wstatus, 0)) {
604 if (errno == ERESTARTSYS)
606 #endif /* ERESTARTSYS */
610 while (wait(&wstatus) != pid) /* do nothing */
613 _exit(WEXITSTATUS(wstatus));
614 } else if (pid == 0) {
615 if (!dialog_vars.cant_kill)
616 (void) signal(SIGHUP, finish_bg);
617 (void) signal(SIGINT, finish_bg);
618 (void) signal(SIGQUIT, finish_bg);
619 (void) signal(SIGSEGV, finish_bg);
620 while (dialog_state.getc_callbacks != 0) {
622 dlg_getc_callbacks(ERR, fkey, retval);