2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * Like malloc, but returns an error when out of space.
51 ckmalloc(size_t nbytes)
57 sh_error("Out of space");
67 ckrealloc(pointer p, size_t nbytes)
69 p = realloc(p, nbytes);
71 sh_error("Out of space");
77 * Make a copy of a string in safe storage.
81 savestr(const char *s)
85 sh_error("Out of space");
91 * Parse trees for commands are allocated in lifo order, so we use a stack
92 * to make this more efficient, and also to avoid all sorts of exception
93 * handling code to handle interrupts in the middle of a parse.
95 * The size 504 was chosen because the Ultrix malloc handles that size
99 /* minimum size of a block */
100 #define MINSIZE SHELL_ALIGN(504)
103 struct stack_block *prev;
107 struct stack_block stackbase;
108 struct stack_block *stackp = &stackbase;
109 char *stacknxt = stackbase.space;
110 size_t stacknleft = MINSIZE;
111 char *sstrend = stackbase.space + MINSIZE;
114 stalloc(size_t nbytes)
119 aligned = SHELL_ALIGN(nbytes);
120 if (aligned > stacknleft) {
123 struct stack_block *sp;
126 if (blocksize < MINSIZE)
128 len = sizeof(struct stack_block) - MINSIZE + blocksize;
130 sh_error("Out of space");
134 stacknxt = sp->space;
135 stacknleft = blocksize;
136 sstrend = stacknxt + blocksize;
142 stacknleft -= aligned;
151 if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) {
152 write(2, "stunalloc\n", 10);
156 stacknleft += stacknxt - (char *)p;
162 void pushstackmark(struct stackmark *mark, size_t len)
164 mark->stackp = stackp;
165 mark->stacknxt = stacknxt;
166 mark->stacknleft = stacknleft;
170 void setstackmark(struct stackmark *mark)
172 pushstackmark(mark, stacknxt == stackp->space && stackp != &stackbase);
177 popstackmark(struct stackmark *mark)
179 struct stack_block *sp;
182 while (stackp != mark->stackp) {
187 stacknxt = mark->stacknxt;
188 stacknleft = mark->stacknleft;
189 sstrend = mark->stacknxt + mark->stacknleft;
195 * When the parser reads in a string, it wants to stick the string on the
196 * stack and only adjust the stack pointer when it knows how big the
197 * string is. Stackblock (defined in stack.h) returns a pointer to a block
198 * of space on top of the stack and stackblocklen returns the length of
199 * this block. Growstackblock will grow this space by at least one byte,
200 * possibly moving it (like realloc). Grabstackblock actually allocates the
201 * part of the block that has been used.
209 newlen = stacknleft * 2;
210 if (newlen < stacknleft)
211 sh_error("Out of space");
215 if (stacknxt == stackp->space && stackp != &stackbase) {
216 struct stack_block *sp;
217 struct stack_block *prevstackp;
222 prevstackp = sp->prev;
223 grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
224 sp = ckrealloc((pointer)sp, grosslen);
225 sp->prev = prevstackp;
227 stacknxt = sp->space;
229 sstrend = sp->space + newlen;
232 char *oldspace = stacknxt;
233 int oldlen = stacknleft;
234 char *p = stalloc(newlen);
236 /* free the space we just allocated */
237 stacknxt = memcpy(p, oldspace, oldlen);
238 stacknleft += newlen;
243 * The following routines are somewhat easier to use than the above.
244 * The user declares a variable of type STACKSTR, which may be declared
245 * to be a register. The macro STARTSTACKSTR initializes things. Then
246 * the user uses the macro STPUTC to add characters to the string. In
247 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
248 * grown as necessary. When the user is done, she can just leave the
249 * string there and refer to it using stackblock(). Or she can allocate
250 * the space for it using grabstackstr(). If it is necessary to allow
251 * someone else to use the stack temporarily and then continue to grow
252 * the string, the user should use grabstack to allocate the space, and
253 * then call ungrabstr(p) to return to the previous mode of operation.
255 * USTPUTC is like STPUTC except that it doesn't check for overflow.
256 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
257 * is space for at least one character.
263 size_t len = stackblocksize();
265 return stackblock() + len;
269 * Called from CHECKSTRSPACE.
273 makestrspace(size_t newlen, char *p)
275 size_t len = p - stacknxt;
281 size = stackblocksize();
287 return stackblock() + len;
291 stnputs(const char *s, size_t n, char *p)
293 p = makestrspace(n, p);
294 p = mempcpy(p, s, n);
299 stputs(const char *s, char *p)
301 return stnputs(s, strlen(s), p);