xref: /titanic_52/usr/src/cmd/sh/stak.c (revision 965005c81e0f731867d47892b9fb677030b102df)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * UNIX shell
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* ========	storage allocation	======== */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate unsigned char *
417c478bd9Sstevel@tonic-gate getstak(asize)			/* allocate requested stack */
427c478bd9Sstevel@tonic-gate int	asize;
437c478bd9Sstevel@tonic-gate {
44*965005c8Schin 	unsigned char	*oldstak;
45*965005c8Schin 	int		size;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	size = round(asize, BYTESPERWORD);
487c478bd9Sstevel@tonic-gate 	oldstak = stakbot;
497c478bd9Sstevel@tonic-gate 	staktop = stakbot += size;
507c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
517c478bd9Sstevel@tonic-gate 		growstak(staktop);
527c478bd9Sstevel@tonic-gate 	return(oldstak);
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * set up stack for local use
577c478bd9Sstevel@tonic-gate  * should be followed by `endstak'
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate unsigned char *
607c478bd9Sstevel@tonic-gate locstak()
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if (brkend - stakbot < BRKINCR)
637c478bd9Sstevel@tonic-gate 	{
647c478bd9Sstevel@tonic-gate 		if (setbrk(brkincr) == -1)
657c478bd9Sstevel@tonic-gate 			error(nostack);
667c478bd9Sstevel@tonic-gate 		if (brkincr < BRKMAX)
677c478bd9Sstevel@tonic-gate 			brkincr += 256;
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	return(stakbot);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate void
737c478bd9Sstevel@tonic-gate growstak(newtop)
747c478bd9Sstevel@tonic-gate unsigned char	*newtop;
757c478bd9Sstevel@tonic-gate {
76*965005c8Schin 	unsigned	incr;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	incr = (unsigned)round(newtop - brkend + 1, BYTESPERWORD);
797c478bd9Sstevel@tonic-gate 	if (brkincr > incr)
807c478bd9Sstevel@tonic-gate 		incr = brkincr;
817c478bd9Sstevel@tonic-gate 	if (setbrk(incr) == -1)
827c478bd9Sstevel@tonic-gate 		error(nospace);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate unsigned char *
867c478bd9Sstevel@tonic-gate savstak()
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	assert(staktop == stakbot);
897c478bd9Sstevel@tonic-gate 	return(stakbot);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate unsigned char *
93*965005c8Schin endstak(unsigned char *argp)		/* tidy up after `locstak' */
947c478bd9Sstevel@tonic-gate {
95*965005c8Schin 	unsigned char	*oldstak;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (argp >= brkend)
987c478bd9Sstevel@tonic-gate 		growstak(argp);
997c478bd9Sstevel@tonic-gate 	*argp++ = 0;
1007c478bd9Sstevel@tonic-gate 	oldstak = stakbot;
1017c478bd9Sstevel@tonic-gate 	stakbot = staktop = (unsigned char *)round(argp, BYTESPERWORD);
1027c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
1037c478bd9Sstevel@tonic-gate 		growstak(staktop);
1047c478bd9Sstevel@tonic-gate 	return(oldstak);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
107*965005c8Schin void
108*965005c8Schin tdystak(unsigned char	*x)		/* try to bring stack back to x */
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	while ((unsigned char *)stakbsy > x)
1117c478bd9Sstevel@tonic-gate 	{
1127c478bd9Sstevel@tonic-gate 		free(stakbsy);
1137c478bd9Sstevel@tonic-gate 		stakbsy = stakbsy->word;
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	staktop = stakbot = max(x, stakbas);
1167c478bd9Sstevel@tonic-gate 	rmtemp(x);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
119*965005c8Schin void
120*965005c8Schin stakchk(void)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	if ((brkend - stakbas) > BRKINCR + BRKINCR)
1237c478bd9Sstevel@tonic-gate 		setbrk(-BRKINCR);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate unsigned char *
1277c478bd9Sstevel@tonic-gate cpystak(x)
1287c478bd9Sstevel@tonic-gate unsigned char	*x;
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	return(endstak(movstrstak(x, locstak())));
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate unsigned char *
134*965005c8Schin movstrstak(unsigned char *a, unsigned char *b)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	do
1377c478bd9Sstevel@tonic-gate 	{
1387c478bd9Sstevel@tonic-gate 		if (b >= brkend)
1397c478bd9Sstevel@tonic-gate 			growstak(b);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	while (*b++ = *a++);
1427c478bd9Sstevel@tonic-gate 	return(--b);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Copy s2 to s1, always copy n bytes.
1477c478bd9Sstevel@tonic-gate  * Return s1
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate unsigned char *
150*965005c8Schin memcpystak(unsigned char *s1, unsigned char *s2, int n)
1517c478bd9Sstevel@tonic-gate {
152*965005c8Schin 	unsigned char *os1 = s1;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	while (--n >= 0) {
1557c478bd9Sstevel@tonic-gate 		if (s1 >= brkend)
1567c478bd9Sstevel@tonic-gate 			growstak(s1);
1577c478bd9Sstevel@tonic-gate 		*s1++ = *s2++;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	return (os1);
1607c478bd9Sstevel@tonic-gate }
161