1 /* signal.c: signal and miscellaneous routines for the ed line editor. */
2 /* GNU ed - The GNU line editor.
3 Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
4 Copyright (C) 2006-2015 Antonio Diaz Diaz.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/ioctl.h>
35 static int mutex = 0; /* If > 0, signals stay pending */
36 static int window_lines_ = 22; /* scroll lines set by sigwinch_handler */
37 static int window_columns_ = 72;
38 static bool sighup_pending = false;
39 static bool sigint_pending = false;
42 static void sighup_handler( int signum )
44 if( signum ) {} /* keep compiler happy */
45 if( mutex ) sighup_pending = true;
48 const char hb[] = "ed.hup";
49 sighup_pending = false;
50 if( last_addr() && modified() &&
51 write_file( hb, "w", 1, last_addr() ) < 0 )
53 char * const s = getenv( "HOME" );
54 const int len = ( s ? strlen( s ) : 0 );
55 const int need_slash = ( ( !len || s[len-1] != '/' ) ? 1 : 0 );
56 char * const hup = ( ( len + need_slash + (int)sizeof hb < path_max( 0 ) ) ?
57 (char *) malloc( len + need_slash + sizeof hb ) : 0 );
58 if( len && hup ) /* hup filename */
60 memcpy( hup, s, len );
61 if( need_slash ) hup[len] = '/';
62 memcpy( hup + len + need_slash, hb, sizeof hb );
63 if( write_file( hup, "w", 1, last_addr() ) >= 0 ) exit( 0 );
65 exit( 1 ); /* hup file write failed */
72 static void sigint_handler( int signum )
74 if( mutex ) sigint_pending = true;
78 sigint_pending = false;
80 sigaddset( &set, signum );
81 sigprocmask( SIG_UNBLOCK, &set, 0 );
82 longjmp( jmp_state, -1 );
87 static void sigwinch_handler( int signum )
90 struct winsize ws; /* window size structure */
92 if( ioctl( 0, TIOCGWINSZ, (char *) &ws ) >= 0 )
94 /* Sanity check values of environment vars */
95 if( ws.ws_row > 2 && ws.ws_row < 600 ) window_lines_ = ws.ws_row - 2;
96 if( ws.ws_col > 8 && ws.ws_col < 1800 ) window_columns_ = ws.ws_col - 8;
99 if( signum ) {} /* keep compiler happy */
103 static int set_signal( const int signum, void (*handler)( int ) )
105 struct sigaction new_action;
107 new_action.sa_handler = handler;
108 sigemptyset( &new_action.sa_mask );
110 new_action.sa_flags = SA_RESTART;
112 new_action.sa_flags = 0;
114 return sigaction( signum, &new_action, 0 );
118 void enable_interrupts( void )
123 if( sighup_pending ) sighup_handler( SIGHUP );
124 if( sigint_pending ) sigint_handler( SIGINT );
129 void disable_interrupts( void ) { ++mutex; }
132 void set_signals( void )
135 sigwinch_handler( SIGWINCH );
136 if( isatty( 0 ) ) set_signal( SIGWINCH, sigwinch_handler );
138 set_signal( SIGHUP, sighup_handler );
139 set_signal( SIGQUIT, SIG_IGN );
140 set_signal( SIGINT, sigint_handler );
144 void set_window_lines( const int lines ) { window_lines_ = lines; }
145 int window_columns( void ) { return window_columns_; }
146 int window_lines( void ) { return window_lines_; }
149 /* convert a string to int with out_of_range detection */
150 bool parse_int( int * const i, const char * const str, const char ** const tail )
156 *i = li = strtol( str, &tmp, 10 );
157 if( tail ) *tail = tmp;
160 set_error_msg( "Bad numerical result" );
164 if( errno == ERANGE || li > INT_MAX || li < INT_MIN )
166 set_error_msg( "Numerical result out of range" );
174 /* assure at least a minimum size for buffer 'buf' */
175 bool resize_buffer( char ** const buf, int * const size, const int min_size )
177 if( *size < min_size )
179 const int new_size = ( min_size < 512 ? 512 : ( min_size / 512 ) * 1024 );
181 disable_interrupts();
182 if( *buf ) new_buf = realloc( *buf, new_size );
183 else new_buf = malloc( new_size );
186 show_strerror( 0, errno );
187 set_error_msg( "Memory exhausted" );
192 *buf = (char *)new_buf;
199 /* assure at least a minimum size for buffer 'buf' */
200 bool resize_line_buffer( const line_t *** const buf, int * const size,
203 if( *size < min_size )
205 const int new_size = ( min_size < 512 ? 512 : ( min_size / 512 ) * 1024 );
207 disable_interrupts();
208 if( *buf ) new_buf = realloc( *buf, new_size );
209 else new_buf = malloc( new_size );
212 show_strerror( 0, errno );
213 set_error_msg( "Memory exhausted" );
218 *buf = (const line_t **)new_buf;
225 /* assure at least a minimum size for buffer 'buf' */
226 bool resize_undo_buffer( undo_t ** const buf, int * const size,
229 if( *size < min_size )
231 const int new_size = ( min_size < 512 ? 512 : ( min_size / 512 ) * 1024 );
233 disable_interrupts();
234 if( *buf ) new_buf = realloc( *buf, new_size );
235 else new_buf = malloc( new_size );
238 show_strerror( 0, errno );
239 set_error_msg( "Memory exhausted" );
244 *buf = (undo_t *)new_buf;
251 /* return unescaped copy of escaped string */
252 const char * strip_escapes( const char * p )
254 static char * buf = 0;
255 static int bufsz = 0;
256 const int len = strlen( p );
259 if( !resize_buffer( &buf, &bufsz, len + 1 ) ) return 0;
260 /* assert: no trailing escape */
261 while( ( buf[i++] = ( (*p == '\\' ) ? *++p : *p ) ) )