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 207 208 209 210 211 212 213 214
|
/* signal.c: signal and miscellaneous routines for the ed line editor. */
/* GNU ed - The GNU line editor.
Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
Copyright (C) 2006, 2007 Antonio Diaz Diaz.
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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "ed.h"
jmp_buf jmp_state;
static int mutex = 0; /* If set, signals stay pending */
static int _window_lines = 22; /* scroll length: ws_row - 2 */
static int _window_columns = 72;
static char sighup_pending = 0;
static char sigint_pending = 0;
void sighup_handler( int signum )
{
signum = 0; /* keep compiler happy */
if( mutex ) sighup_pending = 1;
else
{
char hb[] = "ed.hup";
char *hup = 0; /* hup filename */
char *s;
int n, m = 0;
sighup_pending = 0;
if( last_addr() && write_file( "ed.hup", "w", 1, last_addr(), 1 ) < 0 &&
( s = getenv( "HOME" ) ) != 0 &&
( !( n = strlen( s ) ) ||
( int )( n + ( m = *( s + n - 1 ) != '/' ) + sizeof( hb ) ) < path_max( 0 ) ) &&
( hup = ( char *) malloc( n + m + sizeof( hb ) ) ) != 0 )
{
memcpy( hup, s, n );
memcpy( hup + n, m ? "/" : "", 1 );
memcpy( hup + n + m, hb, sizeof( hb ) );
write_file( hup, "w", 1, last_addr(), 1 );
}
exit( 2 );
}
}
void sigint_handler( int signum )
{
if( mutex ) sigint_pending = 1;
else
{
sigset_t set;
sigint_pending = 0;
sigemptyset( &set );
sigaddset( &set, signum );
sigprocmask( SIG_UNBLOCK, &set, 0 );
longjmp( jmp_state, -1 );
}
}
void sigwinch_handler( int signum )
{
#ifdef TIOCGWINSZ
struct winsize ws; /* window size structure */
if( ioctl( 0, TIOCGWINSZ, (char *) &ws ) >= 0 )
{
/* Sanity check values of environment vars */
if( ws.ws_row > 2 && ws.ws_row < 600 ) _window_lines = ws.ws_row - 2;
if( ws.ws_col > 8 && ws.ws_col < 1800 ) _window_columns = ws.ws_col - 8;
}
#endif
signum = 0; /* keep compiler happy */
}
int set_signal( int signum, void (*handler )( int ) )
{
struct sigaction new_action;
new_action.sa_handler = handler;
sigemptyset( &new_action.sa_mask );
#ifdef SA_RESTART
new_action.sa_flags = SA_RESTART;
#else
new_action.sa_flags = 0;
#endif
return sigaction( signum, &new_action, 0 );
}
void enable_interrupts( void )
{
if( --mutex <= 0 )
{
mutex = 0;
if( sighup_pending ) sighup_handler( SIGHUP );
if( sigint_pending ) sigint_handler( SIGINT );
}
}
void disable_interrupts( void ) { ++mutex; }
void set_signals( void )
{
#ifdef SIGWINCH
sigwinch_handler( SIGWINCH );
if( isatty( 0 ) ) set_signal( SIGWINCH, sigwinch_handler );
#endif
set_signal( SIGHUP, sighup_handler );
set_signal( SIGQUIT, SIG_IGN );
set_signal( SIGINT, sigint_handler );
}
void set_window_lines( const int lines ) { _window_lines = lines; }
int window_columns( void ) { return _window_columns; }
int window_lines( void ) { return _window_lines; }
/* convert a string to int with out_of_range detection */
char parse_int( int *i, const char *str, const char **tail )
{
char *tmp;
errno = 0;
*i = strtol( str, &tmp, 10 );
if( tail ) *tail = tmp;
if( tmp == str )
{
set_error_msg( "Bad numerical result" );
*i = 0;
return 0;
}
if( errno == ERANGE )
{
set_error_msg( "Numerical result out of range" );
*i = 0;
return 0;
}
return 1;
}
/* assure at least a minimum size for buffer `buf' */
char resize_buffer( char **buf, int *size, int min_size )
{
if( *size < min_size )
{
const int new_size = ( min_size < 512 ? 512 : ( min_size / 512 ) * 1024 );
char *new_buf = 0;
disable_interrupts();
if( *buf ) new_buf = realloc( *buf, new_size );
else new_buf = malloc( new_size );
if( !new_buf )
{
show_strerror( 0, errno );
set_error_msg( "Memory exhausted" );
enable_interrupts();
return 0;
}
*size = new_size;
*buf = new_buf;
enable_interrupts();
}
return 1;
}
/* scan command buffer for next non-space char */
const char *skip_blanks( const char *s )
{
while( isspace( (unsigned char)*s ) && *s != '\n' ) ++s;
return s;
}
/* return copy of escaped string of at most length PATH_MAX */
const char *strip_escapes( const char *s )
{
static char *file = 0;
static int filesz = 0;
int i = 0;
if( !resize_buffer( &file, &filesz, path_max( 0 ) + 1 ) ) return 0;
/* assert: no trailing escape */
while( ( file[i++] = ( (*s == '\\' ) ? *++s : *s ) ) )
s++;
return file;
}
|