summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/texinfo/info/signals.c
blob: 0fd9fc0c63af7a42234673cf719cd4bbe92fb41e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* signals.c -- install and maintain Info signal handlers.
   $Id: signals.c,v 1.1.1.3 2000/02/09 01:25:00 espie Exp $

   Copyright (C) 1993, 94, 95, 98 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Written by Brian Fox (bfox@ai.mit.edu). */

#include "info.h"
#include "signals.h"

/* **************************************************************** */
/*                                                                  */
/*              Pretending That We Have POSIX Signals               */
/*                                                                  */
/* **************************************************************** */

/* Non-zero when our signal handler has been called to handle SIGWINCH. */
static int in_sigwinch = 0;

#if !defined (HAVE_SIGPROCMASK) && defined (HAVE_SIGSETMASK)
/* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
static void
sigprocmask (operation, newset, oldset)
     int operation, *newset, *oldset;
{
  switch (operation)
    {
    case SIG_UNBLOCK:
      sigsetmask (sigblock (0) & ~(*newset));
      break;

    case SIG_BLOCK:
      *oldset = sigblock (*newset);
      break;

    case SIG_SETMASK:
      sigsetmask (*newset);
      break;

    default:
      abort ();
    }
}
#endif /* !HAVE_SIGPROCMASK && HAVE_SIGSETMASK */

/* **************************************************************** */
/*                                                                  */
/*                  Signal Handling for Info                        */
/*                                                                  */
/* **************************************************************** */

typedef RETSIGTYPE signal_handler ();

static RETSIGTYPE info_signal_handler ();
static signal_handler *old_TSTP, *old_TTOU, *old_TTIN;
static signal_handler *old_WINCH, *old_INT, *old_USR1;

void
initialize_info_signal_handler ()
{
#if defined (SIGTSTP)
  old_TSTP = (signal_handler *) signal (SIGTSTP, info_signal_handler);
  old_TTOU = (signal_handler *) signal (SIGTTOU, info_signal_handler);
  old_TTIN = (signal_handler *) signal (SIGTTIN, info_signal_handler);
#endif /* SIGTSTP */

#if defined (SIGWINCH)
  old_WINCH = (signal_handler *) signal (SIGWINCH, info_signal_handler);
#endif

#if defined (SIGINT)
  old_INT = (signal_handler *) signal (SIGINT, info_signal_handler);
#endif

#if defined (SIGUSR1)
  /* Used by DJGPP to simulate SIGTSTP on Ctrl-Z.  */
  old_USR1 = (signal_handler *) signal (SIGUSR1, info_signal_handler);
#endif
}

static void
redisplay_after_signal ()
{
  terminal_clear_screen ();
  display_clear_display (the_display);
  window_mark_chain (windows, W_UpdateWindow);
  display_update_display (windows);
  display_cursor_at_point (active_window);
  fflush (stdout);
}

static void
reset_info_window_sizes ()
{
  terminal_goto_xy (0, 0);
  fflush (stdout);
  terminal_unprep_terminal ();
  terminal_get_screen_size ();
  terminal_prep_terminal ();
  display_initialize_display (screenwidth, screenheight);
  window_new_screen_size (screenwidth, screenheight, NULL);
  redisplay_after_signal ();
}

static RETSIGTYPE
info_signal_handler (sig)
     int sig;
{
  signal_handler **old_signal_handler;

  switch (sig)
    {
#if defined (SIGTSTP)
    case SIGTSTP:
    case SIGTTOU:
    case SIGTTIN:
#endif
#if defined (SIGINT)
    case SIGINT:
#endif
      {
#if defined (SIGTSTP)
        if (sig == SIGTSTP)
          old_signal_handler = &old_TSTP;
        if (sig == SIGTTOU)
          old_signal_handler = &old_TTOU;
        if (sig == SIGTTIN)
          old_signal_handler = &old_TTIN;
#endif /* SIGTSTP */
        if (sig == SIGINT)
          old_signal_handler = &old_INT;

        /* For stop signals, restore the terminal IO, leave the cursor
           at the bottom of the window, and stop us. */
        terminal_goto_xy (0, screenheight - 1);
        terminal_clear_to_eol ();
        fflush (stdout);
        terminal_unprep_terminal ();
        signal (sig, *old_signal_handler);
        UNBLOCK_SIGNAL (sig);
        kill (getpid (), sig);

        /* The program is returning now.  Restore our signal handler,
           turn on terminal handling, redraw the screen, and place the
           cursor where it belongs. */
        terminal_prep_terminal ();
        *old_signal_handler = (signal_handler *) signal (sig, info_signal_handler);
        redisplay_after_signal ();
        fflush (stdout);
      }
      break;

#if defined (SIGWINCH) || defined (SIGUSR1)
#ifdef SIGWINCH
    case SIGWINCH:
#endif
#ifdef SIGUSR1
    case SIGUSR1:
#endif
      {
        if (!in_sigwinch) {
          in_sigwinch++;
          
          /* Turn off terminal IO, tell our parent that the window has changed,
             then reinitialize the terminal and rebuild our windows. */
#ifdef SIGWINCH
          if (sig == SIGWINCH)
            old_signal_handler = &old_WINCH;
#endif
#ifdef SIGUSR1
          if (sig == SIGUSR1)
            old_signal_handler = &old_USR1;
#endif
          terminal_goto_xy (0, 0);
          fflush (stdout);
          terminal_unprep_terminal ();
          signal (sig, *old_signal_handler);
          UNBLOCK_SIGNAL (sig);
          kill (getpid (), sig);

          /* After our old signal handler returns... */
          *old_signal_handler
            = (signal_handler *) signal (sig, info_signal_handler);
          terminal_prep_terminal ();
          reset_info_window_sizes ();
          in_sigwinch--;
        }
      }
      break;
#endif /* SIGWINCH || SIGUSR1 */
    }
}