/* C K I S I G  --  Kermit signal handling for Amiga systems */

/*
  Modified from CKUSIG.C by Stephen Walton
  (stephen.walton@csun.edu), California State University Northridge

  Original Author: Jeffrey Altman (jaltman@columbia.edu),
  Columbia University Academic Information Systems, New York City

  Copyright (C) 1985, 1995, Trustees of Columbia University in the City of New
  York.  The C-Kermit software may not be, in whole or in part, licensed or
  sold for profit as a software product itself, nor may it be included in or
  distributed with commercial products or otherwise distributed by commercial
  concerns to their clients or customers without written permission of the
  Office of Kermit Development and Distribution, Columbia University.  This
  copyright notice must not be removed, altered, or obscured.
*/
#include "ckcsym.h"
#include "ckcasc.h"			/* ASCII character symbols */
#include "ckcdeb.h"			/* Debug & other symbols */
#include "ckcker.h"			/* Kermit symbols */
#include "ckcnet.h"			/* Network symbols */
#ifndef NOSPL
#include "ckuusr.h"
#endif /* NOSPL */

#include <signal.h>
#include <setjmp.h>
#include "ckcsig.h"

#ifdef NOCCTRAP
extern ckjmpbuf cmjbuf;
#endif /* NOCCTRAP */

#ifndef NOCCTRAP
int
#ifdef CK_ANSIC
cc_execute( ckjptr(sj_buf), ck_sigfunc dofunc, ck_sigfunc failfunc ) 
#else
cc_execute( sj_buf, dofunc, failfunc)
    ckjptr(sj_buf);
    ck_sigfunc dofunc;
    ck_sigfunc failfunc;
#endif /* CK_ANSIC */
/* cc_execute */ {
    int rc = 0 ;
    if (cksetjmp(ckjdref(sj_buf))) {	/* Control-C trap returns to here. */
	(*failfunc)(NULL) ;
	rc = -1 ;
    } else {			
	(*dofunc)(NULL) ;
    }
    return rc ;
}
#endif /* NOCCTRAP */

int
#ifdef CK_ANSIC				/* ANSIC C declaration... */
alrm_execute(ckjptr(sj_buf),
	     int timo,
	     ck_sighand handler,
	     ck_sigfunc dofunc,
	     ck_sigfunc failfunc
	     ) 

#else /* Not ANSIC C ... */

alrm_execute(sj_buf,
	     timo,
	     handler,
	     dofunc,
	     failfunc
	     )
    ckjptr(sj_buf);
    int timo;
    ck_sighand handler;
    ck_sigfunc dofunc;
    ck_sigfunc failfunc;
#endif /* CK_ANSIC */

/* alrm_execute */ {

    int rc = 0;
    int savalrm = 0;
_PROTOTYP( SIGTYP (*savhandler), (int) );

    savalrm = alarm(timo);
    savhandler = signal( SIGALRM, handler );
    if (cksetjmp(ckjdref(sj_buf))) {	/* Alarm trap returns to here. */
	(*failfunc)(NULL) ;
	rc = -1 ;
    } else {			
	(*dofunc)(NULL) ;
    }
    alarm(savalrm) ;
    if ( savhandler )
        signal( SIGALRM, savhandler ) ;
    return rc ;
}

int
#ifdef CK_ANSIC				/* ANSIC C declaration... */
cc_alrm_execute(ckjptr(sj_buf),
		int timo,
		ck_sighand handler,
		ck_sigfunc dofunc,
		ck_sigfunc failfunc
		) 

#else /* Not ANSIC C ... */

cc_alrm_execute(sj_buf,
	     timo,
	     handler,
	     dofunc,
	     failfunc
	     )
    ckjptr(sj_buf);
    int timo;
    ck_sighand handler;
    ck_sigfunc dofunc;
    ck_sigfunc failfunc;
#endif /* CK_ANSIC */

/* cc_alrm_execute */ {

    int rc = 0;
    int savalrm = 0;
_PROTOTYP( SIGTYP (*savhandler), (int) );

    savalrm = alarm(timo);
    savhandler = signal( SIGALRM, handler );
    if (cksetjmp(ckjdref(sj_buf))) {	/* ^C or alarm trap returns here. */
	(*failfunc)(NULL) ;
	rc = -1 ;
    } else {			
	(*dofunc)(NULL) ;
    }
    alarm(savalrm) ;
    if ( savhandler )
      signal( SIGALRM, savhandler ) ;
    return rc ;
}
