xref: /illumos-gate/usr/src/cmd/sh/stak.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1573d361Snakanon  * Common Development and Distribution License (the "License").
6*1573d361Snakanon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*1573d361Snakanon  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * UNIX shell
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	"defs.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* ========	storage allocation	======== */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate unsigned char *
getstak(asize)397c478bd9Sstevel@tonic-gate getstak(asize)			/* allocate requested stack */
407c478bd9Sstevel@tonic-gate int	asize;
417c478bd9Sstevel@tonic-gate {
42965005c8Schin 	unsigned char	*oldstak;
43965005c8Schin 	int		size;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	size = round(asize, BYTESPERWORD);
467c478bd9Sstevel@tonic-gate 	oldstak = stakbot;
477c478bd9Sstevel@tonic-gate 	staktop = stakbot += size;
487c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
497c478bd9Sstevel@tonic-gate 		growstak(staktop);
507c478bd9Sstevel@tonic-gate 	return(oldstak);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * set up stack for local use
557c478bd9Sstevel@tonic-gate  * should be followed by `endstak'
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate unsigned char *
locstak()587c478bd9Sstevel@tonic-gate locstak()
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	if (brkend - stakbot < BRKINCR)
617c478bd9Sstevel@tonic-gate 	{
627c478bd9Sstevel@tonic-gate 		if (setbrk(brkincr) == -1)
637c478bd9Sstevel@tonic-gate 			error(nostack);
647c478bd9Sstevel@tonic-gate 		if (brkincr < BRKMAX)
657c478bd9Sstevel@tonic-gate 			brkincr += 256;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	return(stakbot);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate void
growstak(newtop)717c478bd9Sstevel@tonic-gate growstak(newtop)
727c478bd9Sstevel@tonic-gate unsigned char	*newtop;
737c478bd9Sstevel@tonic-gate {
74965005c8Schin 	unsigned	incr;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	incr = (unsigned)round(newtop - brkend + 1, BYTESPERWORD);
777c478bd9Sstevel@tonic-gate 	if (brkincr > incr)
787c478bd9Sstevel@tonic-gate 		incr = brkincr;
797c478bd9Sstevel@tonic-gate 	if (setbrk(incr) == -1)
807c478bd9Sstevel@tonic-gate 		error(nospace);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate unsigned char *
savstak()847c478bd9Sstevel@tonic-gate savstak()
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	assert(staktop == stakbot);
877c478bd9Sstevel@tonic-gate 	return(stakbot);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate unsigned char *
endstak(unsigned char * argp)91965005c8Schin endstak(unsigned char *argp)		/* tidy up after `locstak' */
927c478bd9Sstevel@tonic-gate {
93965005c8Schin 	unsigned char	*oldstak;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (argp >= brkend)
967c478bd9Sstevel@tonic-gate 		growstak(argp);
977c478bd9Sstevel@tonic-gate 	*argp++ = 0;
987c478bd9Sstevel@tonic-gate 	oldstak = stakbot;
997c478bd9Sstevel@tonic-gate 	stakbot = staktop = (unsigned char *)round(argp, BYTESPERWORD);
1007c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
1017c478bd9Sstevel@tonic-gate 		growstak(staktop);
1027c478bd9Sstevel@tonic-gate 	return(oldstak);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
105965005c8Schin void
tdystak(unsigned char * x)106965005c8Schin tdystak(unsigned char	*x)		/* try to bring stack back to x */
1077c478bd9Sstevel@tonic-gate {
108*1573d361Snakanon 	struct blk *next;
109*1573d361Snakanon 
1107c478bd9Sstevel@tonic-gate 	while ((unsigned char *)stakbsy > x)
1117c478bd9Sstevel@tonic-gate 	{
112*1573d361Snakanon 		next = stakbsy->word;
1137c478bd9Sstevel@tonic-gate 		free(stakbsy);
114*1573d361Snakanon 		stakbsy = next;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	staktop = stakbot = max(x, stakbas);
1177c478bd9Sstevel@tonic-gate 	rmtemp(x);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
120965005c8Schin void
stakchk(void)121965005c8Schin stakchk(void)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	if ((brkend - stakbas) > BRKINCR + BRKINCR)
1247c478bd9Sstevel@tonic-gate 		setbrk(-BRKINCR);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate unsigned char *
cpystak(x)1287c478bd9Sstevel@tonic-gate cpystak(x)
1297c478bd9Sstevel@tonic-gate unsigned char	*x;
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	return(endstak(movstrstak(x, locstak())));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate unsigned char *
movstrstak(unsigned char * a,unsigned char * b)135965005c8Schin movstrstak(unsigned char *a, unsigned char *b)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	do
1387c478bd9Sstevel@tonic-gate 	{
1397c478bd9Sstevel@tonic-gate 		if (b >= brkend)
1407c478bd9Sstevel@tonic-gate 			growstak(b);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	while (*b++ = *a++);
1437c478bd9Sstevel@tonic-gate 	return(--b);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Copy s2 to s1, always copy n bytes.
1487c478bd9Sstevel@tonic-gate  * Return s1
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate unsigned char *
memcpystak(unsigned char * s1,unsigned char * s2,int n)151965005c8Schin memcpystak(unsigned char *s1, unsigned char *s2, int n)
1527c478bd9Sstevel@tonic-gate {
153965005c8Schin 	unsigned char *os1 = s1;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	while (--n >= 0) {
1567c478bd9Sstevel@tonic-gate 		if (s1 >= brkend)
1577c478bd9Sstevel@tonic-gate 			growstak(s1);
1587c478bd9Sstevel@tonic-gate 		*s1++ = *s2++;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return (os1);
1617c478bd9Sstevel@tonic-gate }
162