/*******************************************************************************

Copyright (c) 1997-2004, Perforce Software, Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTR
IBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Name		: signaler.cpp
 *
 * Author	: Tony Smith <tony@perforce.com> or <tony@smee.org>
 *
 * Description	: Filthy, ugly hack (I'm so ashamed!) to make MacOSX's
 * 		  dynamic loaded behave itself. This is a re-implementation
 *		  of Perforce's Signaller class that doesn't call signal()
 *		  on construction and therefore works with OSX's broken
 *		  dyld implemenation.
 *
 *                NOTE THAT THIS IMPLEMENTATION IS IN EFFECT BROKEN. THE
 *		  PERFORCE VERSION CALLS Catch() ON CONSTRUCTION OF THE
 *		  ONE GLOBAL INSTANCE OF THIS CLASS. THIS VERSION DOES NOT
 *		  BECAUSE MAC OSX CAN'T HANDLE IT.
 *
 *                THIS HACK WILL BE REMOVED AS SOON AS POSSIBLE!
 *
 ******************************************************************************/

#ifdef OS_DARWIN
//
// This sucker's only required on Darwin, so let's not let it get a wider
// audience
//

#define NEED_SIGNAL
#include <stdhdrs.h>
#include <signaler.h>

typedef void(*sighandler_t)(int);

// 
// The one and only signaler object
//
Signaler signaler;


// C linkage for signal
extern "C" {

void CatchInt( int sig )
{
    signaler.Intr();
    exit( -1 );
}

} // extern "C"


//
// Record of an action to be carried out when a signal is received.
//
struct SignalMan
{
    SignalFunc	func;
    void	*ptr;
    SignalMan	*next;
};


Signaler::Signaler()
{
    list = 0;
}

void
Signaler::Catch()
{
    sighandler_t  f = signal( SIGINT, CatchInt );
    if( f != SIG_DFL && f != CatchInt )
    {
	signal( SIGINT, f );
	return;
    }
}

void
Signaler::Block()
{
    signal( SIGINT, SIG_IGN );
}

void
Signaler::Intr()
{
    signal( SIGINT, SIG_IGN );	// Ignore from now on since we'll exit soon
    for( SignalMan *s = list; s; s = s->next )
	s->func( s->ptr );
}

void
Signaler::OnIntr( SignalFunc callback, void *ptr )
{
    SignalMan  *s = new SignalMan;
    s->func = callback;
    s->ptr  = ptr;

    // Insert at front of list - order unimportant
    s->next = list;
    list = s;
}

void 
Signaler::DeleteOnIntr( void *ptr )
{
    SignalMan *l, *p;

    for( l = p = list; p; l = p, p = l->next )
    {
	if( p->ptr == ptr )
	{
	    if( l != p ) l->next = p->next;
	    else	 list = p->next;
	    delete p;
	    break;
	}
    }
}

#endif
