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
struct stackmark *markp;
110
char *stacknxt = stackbase.space;
111
size_t stacknleft = MINSIZE;
112
char *sstrend = stackbase.space + MINSIZE;
116
stalloc(size_t nbytes)
121
aligned = SHELL_ALIGN(nbytes);
122
if (aligned > stacknleft) {
125
struct stack_block *sp;
128
if (blocksize < MINSIZE)
130
len = sizeof(struct stack_block) - MINSIZE + blocksize;
132
sh_error("Out of space");
136
stacknxt = sp->space;
137
stacknleft = blocksize;
138
sstrend = stacknxt + blocksize;
144
stacknleft -= aligned;
153
if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) {
154
write(2, "stunalloc\n", 10);
158
stacknleft += stacknxt - (char *)p;
165
setstackmark(struct stackmark *mark)
167
mark->stackp = stackp;
168
mark->stacknxt = stacknxt;
169
mark->stacknleft = stacknleft;
170
mark->marknext = markp;
176
popstackmark(struct stackmark *mark)
178
struct stack_block *sp;
181
markp = mark->marknext;
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 *oldstackp;
217
struct stackmark *xmark;
218
struct stack_block *sp;
219
struct stack_block *prevstackp;
225
prevstackp = sp->prev;
226
grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
227
sp = ckrealloc((pointer)sp, grosslen);
228
sp->prev = prevstackp;
230
stacknxt = sp->space;
232
sstrend = sp->space + newlen;
235
* Stack marks pointing to the start of the old block
236
* must be relocated to point to the new block
239
while (xmark != NULL && xmark->stackp == oldstackp) {
240
xmark->stackp = stackp;
241
xmark->stacknxt = stacknxt;
242
xmark->stacknleft = stacknleft;
243
xmark = xmark->marknext;
247
char *oldspace = stacknxt;
248
int oldlen = stacknleft;
249
char *p = stalloc(newlen);
251
/* free the space we just allocated */
252
stacknxt = memcpy(p, oldspace, oldlen);
253
stacknleft += newlen;
258
grabstackblock(size_t len)
260
len = SHELL_ALIGN(len);
266
* The following routines are somewhat easier to use than the above.
267
* The user declares a variable of type STACKSTR, which may be declared
268
* to be a register. The macro STARTSTACKSTR initializes things. Then
269
* the user uses the macro STPUTC to add characters to the string. In
270
* effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
271
* grown as necessary. When the user is done, she can just leave the
272
* string there and refer to it using stackblock(). Or she can allocate
273
* the space for it using grabstackstr(). If it is necessary to allow
274
* someone else to use the stack temporarily and then continue to grow
275
* the string, the user should use grabstack to allocate the space, and
276
* then call ungrabstr(p) to return to the previous mode of operation.
278
* USTPUTC is like STPUTC except that it doesn't check for overflow.
279
* CHECKSTACKSPACE can be called before USTPUTC to ensure that there
280
* is space for at least one character.
286
size_t len = stackblocksize();
287
if (herefd >= 0 && len >= 1024) {
288
xwrite(herefd, stackblock(), len);
292
return stackblock() + len;
296
* Called from CHECKSTRSPACE.
300
makestrspace(size_t newlen, char *p)
302
size_t len = p - stacknxt;
303
size_t size = stackblocksize();
308
size = stackblocksize();
314
return stackblock() + len;
318
stnputs(const char *s, size_t n, char *p)
320
p = makestrspace(n, p);
321
p = mempcpy(p, s, n);
326
stputs(const char *s, char *p)
328
return stnputs(s, strlen(s), p);