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
|
/*
* Copyright (C) 1984-2012 Mark Nudelman
* Modified for use with illumos by Garrett D'Amore.
* Copyright 2015 Garrett D'Amore <garrett@damore.org>
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Routines dealing with signals.
*
* A signal usually merely causes a bit to be set in the "signals" word.
* At some convenient time, the mainline code checks to see if any
* signals need processing by calling psignal().
*/
#include <signal.h>
#include "less.h"
/*
* "sigs" contains bits indicating signals which need to be processed.
*/
volatile sig_atomic_t sigs;
extern int sc_width, sc_height;
extern int screen_trashed;
extern int linenums;
extern int wscroll;
extern int quit_on_intr;
extern long jump_sline_fraction;
/*
* Interrupt signal handler.
*/
static void
u_interrupt(int type)
{
sigs |= S_INTERRUPT;
}
/*
* "Stop" (^Z) signal handler.
*/
static void
stop(int type)
{
sigs |= S_STOP;
}
/*
* "Window" change handler
*/
void
sigwinch(int type)
{
sigs |= S_WINCH;
}
/*
* Set up the signal handlers.
*/
void
init_signals(int on)
{
if (on) {
/*
* Set signal handlers.
*/
(void) lsignal(SIGINT, u_interrupt);
(void) lsignal(SIGTSTP, stop);
(void) lsignal(SIGWINCH, sigwinch);
(void) lsignal(SIGQUIT, SIG_IGN);
} else {
/*
* Restore signals to defaults.
*/
(void) lsignal(SIGINT, SIG_DFL);
(void) lsignal(SIGTSTP, SIG_DFL);
(void) lsignal(SIGWINCH, SIG_IGN);
(void) lsignal(SIGQUIT, SIG_DFL);
}
}
/*
* Process any signals we have received.
* A received signal cause a bit to be set in "sigs".
*/
void
psignals(void)
{
int tsignals;
if ((tsignals = sigs) == 0)
return;
sigs = 0;
if (tsignals & S_STOP) {
/*
* Clean up the terminal.
*/
lsignal(SIGTTOU, SIG_IGN);
clear_bot();
deinit();
flush(0);
raw_mode(0);
lsignal(SIGTTOU, SIG_DFL);
lsignal(SIGTSTP, SIG_DFL);
kill(getpid(), SIGTSTP);
/*
* ... Bye bye. ...
* Hopefully we'll be back later and resume here...
* Reset the terminal and arrange to repaint the
* screen when we get back to the main command loop.
*/
lsignal(SIGTSTP, stop);
raw_mode(1);
init();
screen_trashed = 1;
tsignals |= S_WINCH;
}
if (tsignals & S_WINCH) {
int old_width, old_height;
/*
* Re-execute scrsize() to read the new window size.
*/
old_width = sc_width;
old_height = sc_height;
get_term();
if (sc_width != old_width || sc_height != old_height) {
wscroll = (sc_height + 1) / 2;
calc_jump_sline();
calc_shift_count();
screen_trashed = 1;
}
}
if (tsignals & S_INTERRUPT) {
ring_bell();
if (quit_on_intr)
quit(QUIT_INTERRUPT);
}
}
/*
* Custom version of signal() that causes syscalls to be interrupted.
*/
void *
lsignal(int s, void (*a)(int))
{
struct sigaction sa, osa;
sa.sa_handler = a;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0; /* don't restart system calls */
if (sigaction(s, &sa, &osa) != 0)
return (SIG_ERR);
return (osa.sa_handler);
}
|