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