/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: sigretro.c 1.4 89/10/10 $@(#)sh:sigretro.c 1.2"
#

#ifdef SVAE

#include <signal.h>
#include <errno.h>
#include <setjmp.h>
#include "sigretro.h"
#include <stdio.h>

static int cursigmask = 0;	/* present set of blocked signals */

typedef int     (*sigtype)();

/*
 * Signal vector "template" used in sigvec call.
 */

struct	sigvec {
	sigtype	sv_handler;		/* signal handler */
	int	sv_mask;		/* signal mask to apply */
	int	sv_onstack;		/* if non-zero, take on signal stack */
};

sigtype
_signal(s, a)
int	s;
sigtype	a;
{
	struct sigvec osv, sv;

	sv.sv_handler = a;
	sv.sv_mask = sv.sv_onstack = 0;
	if (_sigvec(s, &sv, &osv) < 0)
		return (BADSIG);
	return (osv.sv_handler);
}

sigtype
sigset(sig, func)
int	sig;
sigtype	func;
{
	return(_signal(sig, func));
}

/*
 * sighold(sig);
 * int sig;
 *
 * Simulates the System V sighold system call.
 */

sighold(sig)
int sig;
{
	register int bit;

	bit = 1 << (sig - 1);
	cursigmask |= bit;
	(void) _sigsetmask(cursigmask);
}

sigpause(sig)
int sig;
{
	register int bit;

	bit = 1 << (sig - 1);
	cursigmask &= ~bit;
	(void) _sigpause(cursigmask);
}


gmatch(s, p)
register char	*s, *p;
{
	register char scc;
	char c;

	scc = *s++;
	switch (c = *p++)
	{
	case '[':
		{
			int ok;
			int lc = -1;
			int notflag = 0;

			ok = 0;
			if (*p == '!')
			{
				notflag = 1;
				p++;
			}
			while (c = *p++)
			{
				if (c == ']')
					return(ok ? gmatch(s, p) : 0);
				else if (c == '-' && lc > 0 && *p!= ']')
				{
					if (notflag)
					{
						if ((unsigned char)scc < lc || scc > *(p++))
							ok++;
						else
							return(0);
					}
					else
					{
						if (lc <= (unsigned char)scc && scc <= (*p++))
							ok++;
					}
				}
				else
				{
					if(c == '\\') /* skip to quoted character */
						c = *p++;
					lc = (unsigned char)c;
					if (notflag)
					{
						if (scc && (unsigned char)scc != lc)
							ok++;
						else
							return(0);
					}
					else
					{
						if ((unsigned char)scc == lc)
							ok++;
					}
				}
			}
			return(0);
		}

	case '\\':	
		c = *p++; /* skip to quoted character and see if it matches */
	default:
		if (c != scc)
			return(0);

	case '?':
		return(scc ? gmatch(s, p) : 0);

	case '*':
		while (*p == '*')
			p++;

		if (*p == 0)
			return(1);
		--s;
		while (*s)
		{
			if (gmatch(s++, p))
				return(1);
		}
		return(0);

	case 0:
		return(scc == 0);
	}
}

#endif /* SVAE */
