xref: /titanic_50/usr/src/cmd/sh/stak.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * UNIX shell
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include	"defs.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /* ========	storage allocation	======== */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate unsigned char *
41*7c478bd9Sstevel@tonic-gate getstak(asize)			/* allocate requested stack */
42*7c478bd9Sstevel@tonic-gate int	asize;
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	register unsigned char	*oldstak;
45*7c478bd9Sstevel@tonic-gate 	register int	size;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	size = round(asize, BYTESPERWORD);
48*7c478bd9Sstevel@tonic-gate 	oldstak = stakbot;
49*7c478bd9Sstevel@tonic-gate 	staktop = stakbot += size;
50*7c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
51*7c478bd9Sstevel@tonic-gate 		growstak(staktop);
52*7c478bd9Sstevel@tonic-gate 	return(oldstak);
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  * set up stack for local use
57*7c478bd9Sstevel@tonic-gate  * should be followed by `endstak'
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate unsigned char *
60*7c478bd9Sstevel@tonic-gate locstak()
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	if (brkend - stakbot < BRKINCR)
63*7c478bd9Sstevel@tonic-gate 	{
64*7c478bd9Sstevel@tonic-gate 		if (setbrk(brkincr) == -1)
65*7c478bd9Sstevel@tonic-gate 			error(nostack);
66*7c478bd9Sstevel@tonic-gate 		if (brkincr < BRKMAX)
67*7c478bd9Sstevel@tonic-gate 			brkincr += 256;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	return(stakbot);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate void
73*7c478bd9Sstevel@tonic-gate growstak(newtop)
74*7c478bd9Sstevel@tonic-gate unsigned char	*newtop;
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	register unsigned	incr;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	incr = (unsigned)round(newtop - brkend + 1, BYTESPERWORD);
79*7c478bd9Sstevel@tonic-gate 	if (brkincr > incr)
80*7c478bd9Sstevel@tonic-gate 		incr = brkincr;
81*7c478bd9Sstevel@tonic-gate 	if (setbrk(incr) == -1)
82*7c478bd9Sstevel@tonic-gate 		error(nospace);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate unsigned char *
86*7c478bd9Sstevel@tonic-gate savstak()
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	assert(staktop == stakbot);
89*7c478bd9Sstevel@tonic-gate 	return(stakbot);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate unsigned char *
93*7c478bd9Sstevel@tonic-gate endstak(argp)		/* tidy up after `locstak' */
94*7c478bd9Sstevel@tonic-gate register unsigned char	*argp;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	register unsigned char	*oldstak;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (argp >= brkend)
99*7c478bd9Sstevel@tonic-gate 		growstak(argp);
100*7c478bd9Sstevel@tonic-gate 	*argp++ = 0;
101*7c478bd9Sstevel@tonic-gate 	oldstak = stakbot;
102*7c478bd9Sstevel@tonic-gate 	stakbot = staktop = (unsigned char *)round(argp, BYTESPERWORD);
103*7c478bd9Sstevel@tonic-gate 	if (staktop >= brkend)
104*7c478bd9Sstevel@tonic-gate 		growstak(staktop);
105*7c478bd9Sstevel@tonic-gate 	return(oldstak);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate tdystak(x)		/* try to bring stack back to x */
109*7c478bd9Sstevel@tonic-gate register unsigned char	*x;
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	while ((unsigned char *)stakbsy > x)
112*7c478bd9Sstevel@tonic-gate 	{
113*7c478bd9Sstevel@tonic-gate 		free(stakbsy);
114*7c478bd9Sstevel@tonic-gate 		stakbsy = stakbsy->word;
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	staktop = stakbot = max(x, stakbas);
117*7c478bd9Sstevel@tonic-gate 	rmtemp(x);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate stakchk()
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	if ((brkend - stakbas) > BRKINCR + BRKINCR)
123*7c478bd9Sstevel@tonic-gate 		setbrk(-BRKINCR);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate unsigned char *
127*7c478bd9Sstevel@tonic-gate cpystak(x)
128*7c478bd9Sstevel@tonic-gate unsigned char	*x;
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	return(endstak(movstrstak(x, locstak())));
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate unsigned char *
134*7c478bd9Sstevel@tonic-gate movstrstak(a, b)
135*7c478bd9Sstevel@tonic-gate register unsigned char	*a, *b;
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	do
138*7c478bd9Sstevel@tonic-gate 	{
139*7c478bd9Sstevel@tonic-gate 		if (b >= brkend)
140*7c478bd9Sstevel@tonic-gate 			growstak(b);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	while (*b++ = *a++);
143*7c478bd9Sstevel@tonic-gate 	return(--b);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate /*
147*7c478bd9Sstevel@tonic-gate  * Copy s2 to s1, always copy n bytes.
148*7c478bd9Sstevel@tonic-gate  * Return s1
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate unsigned char *
151*7c478bd9Sstevel@tonic-gate memcpystak(s1, s2, n)
152*7c478bd9Sstevel@tonic-gate register unsigned char *s1, *s2;
153*7c478bd9Sstevel@tonic-gate register int n;
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	register unsigned char *os1 = s1;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	while (--n >= 0) {
158*7c478bd9Sstevel@tonic-gate 		if (s1 >= brkend)
159*7c478bd9Sstevel@tonic-gate 			growstak(s1);
160*7c478bd9Sstevel@tonic-gate 		*s1++ = *s2++;
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 	return (os1);
163*7c478bd9Sstevel@tonic-gate }
164