xref: /onnv-gate/usr/src/cmd/sh/stak.c (revision 5976:b177556d43c5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5976Snakanon  * Common Development and Distribution License (the "License").
6*5976Snakanon  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5976Snakanon  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * UNIX shell
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include	"defs.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /* ========	storage allocation	======== */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate unsigned char *
getstak(asize)400Sstevel@tonic-gate getstak(asize)			/* allocate requested stack */
410Sstevel@tonic-gate int	asize;
420Sstevel@tonic-gate {
43527Schin 	unsigned char	*oldstak;
44527Schin 	int		size;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	size = round(asize, BYTESPERWORD);
470Sstevel@tonic-gate 	oldstak = stakbot;
480Sstevel@tonic-gate 	staktop = stakbot += size;
490Sstevel@tonic-gate 	if (staktop >= brkend)
500Sstevel@tonic-gate 		growstak(staktop);
510Sstevel@tonic-gate 	return(oldstak);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * set up stack for local use
560Sstevel@tonic-gate  * should be followed by `endstak'
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate unsigned char *
locstak()590Sstevel@tonic-gate locstak()
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	if (brkend - stakbot < BRKINCR)
620Sstevel@tonic-gate 	{
630Sstevel@tonic-gate 		if (setbrk(brkincr) == -1)
640Sstevel@tonic-gate 			error(nostack);
650Sstevel@tonic-gate 		if (brkincr < BRKMAX)
660Sstevel@tonic-gate 			brkincr += 256;
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 	return(stakbot);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate void
growstak(newtop)720Sstevel@tonic-gate growstak(newtop)
730Sstevel@tonic-gate unsigned char	*newtop;
740Sstevel@tonic-gate {
75527Schin 	unsigned	incr;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	incr = (unsigned)round(newtop - brkend + 1, BYTESPERWORD);
780Sstevel@tonic-gate 	if (brkincr > incr)
790Sstevel@tonic-gate 		incr = brkincr;
800Sstevel@tonic-gate 	if (setbrk(incr) == -1)
810Sstevel@tonic-gate 		error(nospace);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate unsigned char *
savstak()850Sstevel@tonic-gate savstak()
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	assert(staktop == stakbot);
880Sstevel@tonic-gate 	return(stakbot);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate unsigned char *
endstak(unsigned char * argp)92527Schin endstak(unsigned char *argp)		/* tidy up after `locstak' */
930Sstevel@tonic-gate {
94527Schin 	unsigned char	*oldstak;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (argp >= brkend)
970Sstevel@tonic-gate 		growstak(argp);
980Sstevel@tonic-gate 	*argp++ = 0;
990Sstevel@tonic-gate 	oldstak = stakbot;
1000Sstevel@tonic-gate 	stakbot = staktop = (unsigned char *)round(argp, BYTESPERWORD);
1010Sstevel@tonic-gate 	if (staktop >= brkend)
1020Sstevel@tonic-gate 		growstak(staktop);
1030Sstevel@tonic-gate 	return(oldstak);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
106527Schin void
tdystak(unsigned char * x)107527Schin tdystak(unsigned char	*x)		/* try to bring stack back to x */
1080Sstevel@tonic-gate {
109*5976Snakanon 	struct blk *next;
110*5976Snakanon 
1110Sstevel@tonic-gate 	while ((unsigned char *)stakbsy > x)
1120Sstevel@tonic-gate 	{
113*5976Snakanon 		next = stakbsy->word;
1140Sstevel@tonic-gate 		free(stakbsy);
115*5976Snakanon 		stakbsy = next;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 	staktop = stakbot = max(x, stakbas);
1180Sstevel@tonic-gate 	rmtemp(x);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
121527Schin void
stakchk(void)122527Schin stakchk(void)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	if ((brkend - stakbas) > BRKINCR + BRKINCR)
1250Sstevel@tonic-gate 		setbrk(-BRKINCR);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate unsigned char *
cpystak(x)1290Sstevel@tonic-gate cpystak(x)
1300Sstevel@tonic-gate unsigned char	*x;
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	return(endstak(movstrstak(x, locstak())));
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate unsigned char *
movstrstak(unsigned char * a,unsigned char * b)136527Schin movstrstak(unsigned char *a, unsigned char *b)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	do
1390Sstevel@tonic-gate 	{
1400Sstevel@tonic-gate 		if (b >= brkend)
1410Sstevel@tonic-gate 			growstak(b);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	while (*b++ = *a++);
1440Sstevel@tonic-gate 	return(--b);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * Copy s2 to s1, always copy n bytes.
1490Sstevel@tonic-gate  * Return s1
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate unsigned char *
memcpystak(unsigned char * s1,unsigned char * s2,int n)152527Schin memcpystak(unsigned char *s1, unsigned char *s2, int n)
1530Sstevel@tonic-gate {
154527Schin 	unsigned char *os1 = s1;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	while (--n >= 0) {
1570Sstevel@tonic-gate 		if (s1 >= brkend)
1580Sstevel@tonic-gate 			growstak(s1);
1590Sstevel@tonic-gate 		*s1++ = *s2++;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 	return (os1);
1620Sstevel@tonic-gate }
163