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