### package Signal ###
#
# -*- Perl -*-
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu>,
#                    Jason Lauman <lauman@cs.utah.edu> and the
#                    University of Utah Computer Science Department
#
# $Id: Signal.pm,v 1.1.1.1 2000/05/03 19:55:12 wright Exp $
#
# This is part of rust 1.0 Beta 2
#
# Abstract: Signal package for check-q server
#
# 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 2 of the License, or
# 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 the source distribution of Inquire, in the license
# directory. If you don't have a copy of it, write to: 
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.

@functions = ('Exit','Debug','Error');
require Base;
	
%VALID_SIGNALS= (
	'HUP','',
	'CHLD','',
	'INT','',
	);

%SIGNAL_HANDLERS = ();

sub Signal::Add {
	my($signal,$function) = @_;
	if( exists($VALID_SIGNALS{$signal}) ) {
		$SIG{$signal} = 'SIGNAL_HANDLER';
		push(@{$SIGNAL_HANDLERS{$signal}},$function);	
	}
	else { 
		&$Error("\"$signal\" is not a valid signal"); 
		return 0;
	}
	return 1;
}

sub Signal::Remove {
	my($signal,$function) = @_;
	if( exists($SIG{$signal}) ) {
		@{$SIGNAL_HANDLERS{$signal}} = grep { $function != $_ } @{$SIGNAL_HANDLERS{$signal}};	
		( $#{$SIGNAL_HANDLERS{$signal}} < 0 ) && ( $SIG{$signal} = 'DEFAULT' );
	}
	else { 
		&$Error("\"$signal\" is not a valid signal"); 
		return 0;
	}	
	return 1;
}

sub SIGNAL_HANDLER {
	my($signal) = @_;
	foreach $function (@{$SIGNAL_HANDLERS{$signal}}) {
		&$function;	
	}
}

1;

