summaryrefslogtreecommitdiff
path: root/pico/osdep/signals.c
blob: 601d96ffdeef390ca4ee913c1e6fd1d1f4c605cd (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
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: signals.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2007 University of Washington
 * Copyright 2013-2016 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include <system.h>
#include <general.h>

#include "../headers.h"

#include "../../pith/charconv/filesys.h"

#include "tty.h"

#include "signals.h"


/* internal prototypes */
#ifndef _WINDOWS
RETSIGTYPE	do_hup_signal(int);
#endif


/*
 * picosigs - Install any handlers for the signals we're interested
 *	      in catching.
 */
void
picosigs(void)
{
#ifndef _WINDOWS
    signal(SIGHUP,  do_hup_signal);	/* deal with SIGHUP */
    signal(SIGTERM, do_hup_signal);	/* deal with SIGTERM */
#ifdef	SIGTSTP
    signal(SIGTSTP, SIG_DFL);
#endif
#if	defined(SIGWINCH) && defined(TIOCGWINSZ)
    signal(SIGWINCH, winch_handler); /* window size changes */
#endif
#endif /* !_WINDOWS */
}




#ifndef _WINDOWS
/*
 * do_hup_signal - jump back in the stack to where we can handle this
 */
RETSIGTYPE
do_hup_signal(int sig)
{
    signal(SIGHUP,  SIG_IGN);			/* ignore further SIGHUP's */
    signal(SIGTERM, SIG_IGN);			/* ignore further SIGTERM's */
    if(Pmaster){
	extern jmp_buf finstate;

	longjmp(finstate, COMP_GOTHUP);
    }
    else{
	/*
	 * if we've been interrupted and the buffer is changed,
	 * save it...
	 */
	if(anycb() == TRUE){			/* time to save */
	    if(curbp->b_fname[0] == '\0'){	/* name it */
		strncpy(curbp->b_fname, "pico.save", sizeof(curbp->b_fname));
		curbp->b_fname[sizeof(curbp->b_fname)-1] = '\0';
	    }
	    else{
		strncat(curbp->b_fname, ".save", sizeof(curbp->b_fname)-strlen(curbp->b_fname)-1);
		curbp->b_fname[sizeof(curbp->b_fname)-1] = '\0';
	    }

	    our_unlink(curbp->b_fname);
	    writeout(curbp->b_fname, TRUE);
	}

	vttidy();
	exit(1);
    }
}
#endif /* !_WINDOWS */


#if	defined(SIGWINCH) && defined(TIOCGWINSZ)
/*
 * winch_handler - handle window change signal
 */
RETSIGTYPE
winch_handler(int sig)
{
    int i;
    signal(SIGWINCH, winch_handler);
    if(wheadp != NULL)
      ttresize();
    else if (Pmaster){
      i = Pmaster->arm_winch_cleanup;
      Pmaster->arm_winch_cleanup = 1;
    }
    if(Pmaster && Pmaster->winch_cleanup && Pmaster->arm_winch_cleanup)
      (*Pmaster->winch_cleanup)();
    if(wheadp == NULL && Pmaster != NULL)
	Pmaster->arm_winch_cleanup = i;
}
#endif	/* SIGWINCH and friends */



#ifdef	POSIX_SIGNALS
/*----------------------------------------------------------------------
   Reset signals after critical imap code
 ----*/
void
(*posix_signal(int sig_num, RETSIGTYPE (*action)(int)))(int)
{
    struct sigaction new_action, old_action;

    memset((void *)&new_action, 0, sizeof(struct sigaction));
    sigemptyset (&new_action.sa_mask);
    new_action.sa_handler = action;
#ifdef	SA_RESTART
    new_action.sa_flags = SA_RESTART;
#else
    new_action.sa_flags = 0;
#endif
    sigaction(sig_num, &new_action, &old_action);
    return(old_action.sa_handler);
}

int
posix_sigunblock(int mask)
{
    sigset_t sig_mask;

    sigemptyset(&sig_mask);
    sigaddset(&sig_mask, mask);
#if	HAVE_PTHREAD
# define sigprocmask	pthread_sigmask
#endif
    return(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL));
}
#endif /* POSIX_SIGNALS */


#if	defined(sv3) || defined(ct)
/* Placed by rll to handle the rename function not found in AT&T */
int
rename(char *oldname, char *newname)
{
    int rtn;
    char b[NLINE];

    strncpy(b, fname_to_locale(oldname), sizeof(b));
    b[sizeof(b)-1] = '\0';

    if ((rtn = link(b, fname_to_locale(newname))) != 0) {
	perror("Was not able to rename file.");
	return(rtn);
    }

    if ((rtn = our_unlink(b)) != 0)
      perror("Was not able to unlink file.");

    return(rtn);
}
#endif