xref: /netbsd-src/external/bsd/less/dist/signal.c (revision 838f5788460f0f133b15d706e644d692a9d4d6ec)
1*838f5788Ssimonb /*	$NetBSD: signal.c,v 1.5 2023/10/06 05:49:49 simonb Exp $	*/
220006a0bStron 
320006a0bStron /*
4*838f5788Ssimonb  * Copyright (C) 1984-2023  Mark Nudelman
520006a0bStron  *
620006a0bStron  * You may distribute under the terms of either the GNU General Public
720006a0bStron  * License or the Less License, as specified in the README file.
820006a0bStron  *
9ec18bca0Stron  * For more information, see the README file.
1020006a0bStron  */
1120006a0bStron 
1220006a0bStron 
1320006a0bStron /*
1420006a0bStron  * Routines dealing with signals.
1520006a0bStron  *
1620006a0bStron  * A signal usually merely causes a bit to be set in the "signals" word.
1720006a0bStron  * At some convenient time, the mainline code checks to see if any
1820006a0bStron  * signals need processing by calling psignal().
1920006a0bStron  * If we happen to be reading from a file [in iread()] at the time
2020006a0bStron  * the signal is received, we call intread to interrupt the iread.
2120006a0bStron  */
2220006a0bStron 
2320006a0bStron #include "less.h"
2420006a0bStron #include <signal.h>
2520006a0bStron 
2620006a0bStron /*
2720006a0bStron  * "sigs" contains bits indicating signals which need to be processed.
2820006a0bStron  */
2920006a0bStron public int sigs;
3020006a0bStron 
31824a88bbStron static RETSIGTYPE u_interrupt __P((int));
32824a88bbStron static RETSIGTYPE stop __P((int));
33824a88bbStron #if MSDOS_COMPILER==WIN32C
34824a88bbStron static BOOL WINAPI wbreak_handler __P((DWORD));
35824a88bbStron #endif
36824a88bbStron 
3720006a0bStron extern int sc_width, sc_height;
3820006a0bStron extern int screen_trashed;
3920006a0bStron extern int lnloop;
4020006a0bStron extern int linenums;
4120006a0bStron extern int wscroll;
4220006a0bStron extern int reading;
4320006a0bStron extern int quit_on_intr;
44*838f5788Ssimonb extern int secure;
4520006a0bStron extern long jump_sline_fraction;
4620006a0bStron 
4720006a0bStron /*
4820006a0bStron  * Interrupt signal handler.
4920006a0bStron  */
50*838f5788Ssimonb #if MSDOS_COMPILER!=WIN32C
5120006a0bStron 	/* ARGSUSED*/
u_interrupt(int type)52*838f5788Ssimonb static RETSIGTYPE u_interrupt(int type)
5320006a0bStron {
5420006a0bStron 	bell();
5520006a0bStron #if OS2
5620006a0bStron 	LSIGNAL(SIGINT, SIG_ACK);
5720006a0bStron #endif
5820006a0bStron 	LSIGNAL(SIGINT, u_interrupt);
5920006a0bStron 	sigs |= S_INTERRUPT;
6020006a0bStron #if MSDOS_COMPILER==DJGPPC
6120006a0bStron 	/*
6220006a0bStron 	 * If a keyboard has been hit, it must be Ctrl-C
6320006a0bStron 	 * (as opposed to Ctrl-Break), so consume it.
6420006a0bStron 	 * (Otherwise, Less will beep when it sees Ctrl-C from keyboard.)
6520006a0bStron 	 */
6620006a0bStron 	if (kbhit())
6720006a0bStron 		getkey();
6820006a0bStron #endif
69*838f5788Ssimonb #if HILITE_SEARCH
70*838f5788Ssimonb 	set_filter_pattern(NULL, 0);
71*838f5788Ssimonb #endif
7220006a0bStron 	if (reading)
7320006a0bStron 		intread(); /* May longjmp */
7420006a0bStron }
75*838f5788Ssimonb #endif
7620006a0bStron 
7720006a0bStron #ifdef SIGTSTP
7820006a0bStron /*
7920006a0bStron  * "Stop" (^Z) signal handler.
8020006a0bStron  */
8120006a0bStron 	/* ARGSUSED*/
stop(int type)82*838f5788Ssimonb static RETSIGTYPE stop(int type)
8320006a0bStron {
8420006a0bStron 	LSIGNAL(SIGTSTP, stop);
8520006a0bStron 	sigs |= S_STOP;
8620006a0bStron 	if (reading)
8720006a0bStron 		intread();
8820006a0bStron }
8920006a0bStron #endif
9020006a0bStron 
91*838f5788Ssimonb #undef SIG_LESSWINDOW
9220006a0bStron #ifdef SIGWINCH
93*838f5788Ssimonb #define SIG_LESSWINDOW SIGWINCH
9420006a0bStron #else
9520006a0bStron #ifdef SIGWIND
96*838f5788Ssimonb #define SIG_LESSWINDOW SIGWIND
97*838f5788Ssimonb #endif
98*838f5788Ssimonb #endif
99*838f5788Ssimonb 
100*838f5788Ssimonb #ifdef SIG_LESSWINDOW
10120006a0bStron /*
10220006a0bStron  * "Window" change handler
10320006a0bStron  */
10420006a0bStron 	/* ARGSUSED*/
winch(int type)105*838f5788Ssimonb public RETSIGTYPE winch(int type)
10620006a0bStron {
107*838f5788Ssimonb 	LSIGNAL(SIG_LESSWINDOW, winch);
10820006a0bStron 	sigs |= S_WINCH;
10920006a0bStron 	if (reading)
11020006a0bStron 		intread();
11120006a0bStron }
11220006a0bStron #endif
11320006a0bStron 
11420006a0bStron #if MSDOS_COMPILER==WIN32C
11520006a0bStron /*
11620006a0bStron  * Handle CTRL-C and CTRL-BREAK keys.
11720006a0bStron  */
118*838f5788Ssimonb #define WIN32_LEAN_AND_MEAN
119*838f5788Ssimonb #include <windows.h>
12020006a0bStron 
wbreak_handler(DWORD dwCtrlType)121*838f5788Ssimonb static BOOL WINAPI wbreak_handler(DWORD dwCtrlType)
12220006a0bStron {
12320006a0bStron 	switch (dwCtrlType)
12420006a0bStron 	{
12520006a0bStron 	case CTRL_C_EVENT:
12620006a0bStron 	case CTRL_BREAK_EVENT:
12720006a0bStron 		sigs |= S_INTERRUPT;
128*838f5788Ssimonb #if HILITE_SEARCH
129*838f5788Ssimonb 		set_filter_pattern(NULL, 0);
130*838f5788Ssimonb #endif
13120006a0bStron 		return (TRUE);
13220006a0bStron 	default:
13320006a0bStron 		break;
13420006a0bStron 	}
13520006a0bStron 	return (FALSE);
13620006a0bStron }
13720006a0bStron #endif
13820006a0bStron 
terminate(int type)139*838f5788Ssimonb static RETSIGTYPE terminate(int type)
140*838f5788Ssimonb {
141*838f5788Ssimonb 	quit(15);
142*838f5788Ssimonb }
143*838f5788Ssimonb 
14420006a0bStron /*
14520006a0bStron  * Set up the signal handlers.
14620006a0bStron  */
init_signals(int on)147*838f5788Ssimonb public void init_signals(int on)
14820006a0bStron {
14920006a0bStron 	if (on)
15020006a0bStron 	{
15120006a0bStron 		/*
15220006a0bStron 		 * Set signal handlers.
15320006a0bStron 		 */
15420006a0bStron #if MSDOS_COMPILER==WIN32C
15520006a0bStron 		SetConsoleCtrlHandler(wbreak_handler, TRUE);
156*838f5788Ssimonb #else
157*838f5788Ssimonb 		(void) LSIGNAL(SIGINT, u_interrupt);
15820006a0bStron #endif
15920006a0bStron #ifdef SIGTSTP
160*838f5788Ssimonb 		(void) LSIGNAL(SIGTSTP, secure ? SIG_IGN : stop);
16120006a0bStron #endif
16220006a0bStron #ifdef SIGWINCH
16320006a0bStron 		(void) LSIGNAL(SIGWINCH, winch);
16420006a0bStron #endif
16520006a0bStron #ifdef SIGWIND
16620006a0bStron 		(void) LSIGNAL(SIGWIND, winch);
16720006a0bStron #endif
16820006a0bStron #ifdef SIGQUIT
16920006a0bStron 		(void) LSIGNAL(SIGQUIT, SIG_IGN);
17020006a0bStron #endif
171*838f5788Ssimonb #ifdef SIGTERM
172*838f5788Ssimonb 		(void) LSIGNAL(SIGTERM, terminate);
173*838f5788Ssimonb #endif
17420006a0bStron 	} else
17520006a0bStron 	{
17620006a0bStron 		/*
17720006a0bStron 		 * Restore signals to defaults.
17820006a0bStron 		 */
17920006a0bStron #if MSDOS_COMPILER==WIN32C
18020006a0bStron 		SetConsoleCtrlHandler(wbreak_handler, FALSE);
181*838f5788Ssimonb #else
182*838f5788Ssimonb 		(void) LSIGNAL(SIGINT, SIG_DFL);
18320006a0bStron #endif
18420006a0bStron #ifdef SIGTSTP
18520006a0bStron 		(void) LSIGNAL(SIGTSTP, SIG_DFL);
18620006a0bStron #endif
18720006a0bStron #ifdef SIGWINCH
18820006a0bStron 		(void) LSIGNAL(SIGWINCH, SIG_IGN);
18920006a0bStron #endif
19020006a0bStron #ifdef SIGWIND
19120006a0bStron 		(void) LSIGNAL(SIGWIND, SIG_IGN);
19220006a0bStron #endif
19320006a0bStron #ifdef SIGQUIT
19420006a0bStron 		(void) LSIGNAL(SIGQUIT, SIG_DFL);
19520006a0bStron #endif
196*838f5788Ssimonb #ifdef SIGTERM
197*838f5788Ssimonb 		(void) LSIGNAL(SIGTERM, SIG_DFL);
198*838f5788Ssimonb #endif
19920006a0bStron 	}
20020006a0bStron }
20120006a0bStron 
20220006a0bStron /*
20320006a0bStron  * Process any signals we have received.
20420006a0bStron  * A received signal cause a bit to be set in "sigs".
20520006a0bStron  */
psignals(void)206*838f5788Ssimonb public void psignals(void)
20720006a0bStron {
208*838f5788Ssimonb 	int tsignals;
20920006a0bStron 
21020006a0bStron 	if ((tsignals = sigs) == 0)
21120006a0bStron 		return;
21220006a0bStron 	sigs = 0;
21320006a0bStron 
21420006a0bStron #ifdef SIGTSTP
21520006a0bStron 	if (tsignals & S_STOP)
21620006a0bStron 	{
21720006a0bStron 		/*
21820006a0bStron 		 * Clean up the terminal.
21920006a0bStron 		 */
22020006a0bStron #ifdef SIGTTOU
22120006a0bStron 		LSIGNAL(SIGTTOU, SIG_IGN);
22220006a0bStron #endif
22320006a0bStron 		clear_bot();
22420006a0bStron 		deinit();
22520006a0bStron 		flush();
22620006a0bStron 		raw_mode(0);
22720006a0bStron #ifdef SIGTTOU
22820006a0bStron 		LSIGNAL(SIGTTOU, SIG_DFL);
22920006a0bStron #endif
23020006a0bStron 		LSIGNAL(SIGTSTP, SIG_DFL);
23120006a0bStron 		kill(getpid(), SIGTSTP);
23220006a0bStron 		/*
23320006a0bStron 		 * ... Bye bye. ...
23420006a0bStron 		 * Hopefully we'll be back later and resume here...
23520006a0bStron 		 * Reset the terminal and arrange to repaint the
23620006a0bStron 		 * screen when we get back to the main command loop.
23720006a0bStron 		 */
23820006a0bStron 		LSIGNAL(SIGTSTP, stop);
23920006a0bStron 		raw_mode(1);
24020006a0bStron 		init();
24120006a0bStron 		screen_trashed = 1;
24220006a0bStron 		tsignals |= S_WINCH;
24320006a0bStron 	}
24420006a0bStron #endif
24520006a0bStron #ifdef S_WINCH
24620006a0bStron 	if (tsignals & S_WINCH)
24720006a0bStron 	{
24820006a0bStron 		int old_width, old_height;
24920006a0bStron 		/*
25020006a0bStron 		 * Re-execute scrsize() to read the new window size.
25120006a0bStron 		 */
25220006a0bStron 		old_width = sc_width;
25320006a0bStron 		old_height = sc_height;
25420006a0bStron 		get_term();
25520006a0bStron 		if (sc_width != old_width || sc_height != old_height)
25620006a0bStron 		{
25720006a0bStron 			wscroll = (sc_height + 1) / 2;
25820006a0bStron 			calc_jump_sline();
25920006a0bStron 			calc_shift_count();
26020006a0bStron 		}
261*838f5788Ssimonb 		screen_trashed = 1;
26220006a0bStron 	}
26320006a0bStron #endif
26420006a0bStron 	if (tsignals & S_INTERRUPT)
26520006a0bStron 	{
26620006a0bStron 		if (quit_on_intr)
26720006a0bStron 			quit(QUIT_INTERRUPT);
26820006a0bStron 	}
26920006a0bStron }
270