11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)memalloc.c	8.3 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
401556Srgrimes
41111422Smarcel#include <sys/param.h>
421556Srgrimes#include "shell.h"
431556Srgrimes#include "output.h"
441556Srgrimes#include "memalloc.h"
451556Srgrimes#include "error.h"
461556Srgrimes#include "mystring.h"
4739049Scracauer#include "expand.h"
4817987Speter#include <stdlib.h>
4917987Speter#include <unistd.h>
501556Srgrimes
511556Srgrimes/*
521556Srgrimes * Like malloc, but returns an error when out of space.
531556Srgrimes */
541556Srgrimes
551556Srgrimespointer
56193221Srseckmalloc(size_t nbytes)
5717987Speter{
5825222Ssteve	pointer p;
591556Srgrimes
60151795Sstefanf	INTOFF;
61151795Sstefanf	p = malloc(nbytes);
62151795Sstefanf	INTON;
63151795Sstefanf	if (p == NULL)
641556Srgrimes		error("Out of space");
651556Srgrimes	return p;
661556Srgrimes}
671556Srgrimes
681556Srgrimes
691556Srgrimes/*
701556Srgrimes * Same for realloc.
711556Srgrimes */
721556Srgrimes
731556Srgrimespointer
7490111Simpckrealloc(pointer p, int nbytes)
7517987Speter{
76151795Sstefanf	INTOFF;
77151795Sstefanf	p = realloc(p, nbytes);
78151795Sstefanf	INTON;
79151795Sstefanf	if (p == NULL)
801556Srgrimes		error("Out of space");
811556Srgrimes	return p;
821556Srgrimes}
831556Srgrimes
84151795Sstefanfvoid
85151795Sstefanfckfree(pointer p)
86151795Sstefanf{
87151795Sstefanf	INTOFF;
88151795Sstefanf	free(p);
89151795Sstefanf	INTON;
90151795Sstefanf}
911556Srgrimes
92151795Sstefanf
931556Srgrimes/*
941556Srgrimes * Make a copy of a string in safe storage.
951556Srgrimes */
961556Srgrimes
971556Srgrimeschar *
98200956Sjillessavestr(const char *s)
9945618Scracauer{
10025222Ssteve	char *p;
101262951Sjmmv	size_t len;
1021556Srgrimes
103262951Sjmmv	len = strlen(s);
104262951Sjmmv	p = ckmalloc(len + 1);
105262951Sjmmv	memcpy(p, s, len + 1);
1061556Srgrimes	return p;
1071556Srgrimes}
1081556Srgrimes
1091556Srgrimes
1101556Srgrimes/*
1111556Srgrimes * Parse trees for commands are allocated in lifo order, so we use a stack
1121556Srgrimes * to make this more efficient, and also to avoid all sorts of exception
1131556Srgrimes * handling code to handle interrupts in the middle of a parse.
1141556Srgrimes *
115111422Smarcel * The size 496 was chosen because with 16-byte alignment the total size
116111422Smarcel * for the allocated block is 512.
1171556Srgrimes */
1181556Srgrimes
119111422Smarcel#define MINSIZE 496		/* minimum size of a block. */
1201556Srgrimes
1211556Srgrimes
1221556Srgrimesstruct stack_block {
1231556Srgrimes	struct stack_block *prev;
124111422Smarcel	/* Data follows */
1251556Srgrimes};
126111422Smarcel#define SPACE(sp)	((char*)(sp) + ALIGN(sizeof(struct stack_block)))
1271556Srgrimes
128213760Sobrienstatic struct stack_block *stackp;
129111422Smarcelchar *stacknxt;
130111422Smarcelint stacknleft;
131216743Sjilleschar *sstrend;
1321556Srgrimes
1331556Srgrimes
134213811Sobrienstatic void
135111422Smarcelstnewblock(int nbytes)
136111422Smarcel{
137111422Smarcel	struct stack_block *sp;
138111422Smarcel	int allocsize;
1391556Srgrimes
140111422Smarcel	if (nbytes < MINSIZE)
141111422Smarcel		nbytes = MINSIZE;
142111422Smarcel
143111422Smarcel	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
144111422Smarcel
145111422Smarcel	INTOFF;
146111422Smarcel	sp = ckmalloc(allocsize);
147111422Smarcel	sp->prev = stackp;
148111422Smarcel	stacknxt = SPACE(sp);
149111422Smarcel	stacknleft = allocsize - (stacknxt - (char*)sp);
150216743Sjilles	sstrend = stacknxt + stacknleft;
151111422Smarcel	stackp = sp;
152111422Smarcel	INTON;
153111422Smarcel}
154111422Smarcel
155111422Smarcel
1561556Srgrimespointer
15790111Simpstalloc(int nbytes)
15817987Speter{
15925222Ssteve	char *p;
1601556Srgrimes
1611556Srgrimes	nbytes = ALIGN(nbytes);
162111422Smarcel	if (nbytes > stacknleft)
163111422Smarcel		stnewblock(nbytes);
1641556Srgrimes	p = stacknxt;
1651556Srgrimes	stacknxt += nbytes;
1661556Srgrimes	stacknleft -= nbytes;
1671556Srgrimes	return p;
1681556Srgrimes}
1691556Srgrimes
1701556Srgrimes
1711556Srgrimesvoid
17290111Simpstunalloc(pointer p)
17345618Scracauer{
1741556Srgrimes	if (p == NULL) {		/*DEBUG */
17580381Ssheldonh		write(STDERR_FILENO, "stunalloc\n", 10);
1761556Srgrimes		abort();
1771556Srgrimes	}
1781556Srgrimes	stacknleft += stacknxt - (char *)p;
1791556Srgrimes	stacknxt = p;
1801556Srgrimes}
1811556Srgrimes
1821556Srgrimes
183297749Sjilleschar *
184297749Sjillesstsavestr(const char *s)
185297749Sjilles{
186297749Sjilles	char *p;
187297749Sjilles	size_t len;
1881556Srgrimes
189297749Sjilles	len = strlen(s);
190297749Sjilles	p = stalloc(len + 1);
191297749Sjilles	memcpy(p, s, len + 1);
192297749Sjilles	return p;
193297749Sjilles}
194297749Sjilles
195297749Sjilles
1961556Srgrimesvoid
19790111Simpsetstackmark(struct stackmark *mark)
19845618Scracauer{
1991556Srgrimes	mark->stackp = stackp;
2001556Srgrimes	mark->stacknxt = stacknxt;
2011556Srgrimes	mark->stacknleft = stacknleft;
202250527Sjilles	/* Ensure this block stays in place. */
203250527Sjilles	if (stackp != NULL && stacknxt == SPACE(stackp))
204250527Sjilles		stalloc(1);
2051556Srgrimes}
2061556Srgrimes
2071556Srgrimes
2081556Srgrimesvoid
20990111Simppopstackmark(struct stackmark *mark)
21045618Scracauer{
2111556Srgrimes	struct stack_block *sp;
2121556Srgrimes
2131556Srgrimes	INTOFF;
2141556Srgrimes	while (stackp != mark->stackp) {
2151556Srgrimes		sp = stackp;
2161556Srgrimes		stackp = sp->prev;
2171556Srgrimes		ckfree(sp);
2181556Srgrimes	}
2191556Srgrimes	stacknxt = mark->stacknxt;
2201556Srgrimes	stacknleft = mark->stacknleft;
221216743Sjilles	sstrend = stacknxt + stacknleft;
2221556Srgrimes	INTON;
2231556Srgrimes}
2241556Srgrimes
2251556Srgrimes
2261556Srgrimes/*
2271556Srgrimes * When the parser reads in a string, it wants to stick the string on the
2281556Srgrimes * stack and only adjust the stack pointer when it knows how big the
2291556Srgrimes * string is.  Stackblock (defined in stack.h) returns a pointer to a block
2301556Srgrimes * of space on top of the stack and stackblocklen returns the length of
2311556Srgrimes * this block.  Growstackblock will grow this space by at least one byte,
2321556Srgrimes * possibly moving it (like realloc).  Grabstackblock actually allocates the
2331556Srgrimes * part of the block that has been used.
2341556Srgrimes */
2351556Srgrimes
236216706Sjillesstatic void
237216706Sjillesgrowstackblock(int min)
23845618Scracauer{
2391556Srgrimes	char *p;
24045618Scracauer	int newlen;
24145618Scracauer	char *oldspace;
24245618Scracauer	int oldlen;
2431556Srgrimes	struct stack_block *sp;
24464702Scracauer	struct stack_block *oldstackp;
2451556Srgrimes
246216706Sjilles	if (min < stacknleft)
247216706Sjilles		min = stacknleft;
248248980Sjilles	if ((unsigned int)min >=
249248980Sjilles	    INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
250216706Sjilles		error("Out of space");
251216706Sjilles	min += stacknleft;
252216706Sjilles	min += ALIGN(sizeof(struct stack_block));
253216706Sjilles	newlen = 512;
254216706Sjilles	while (newlen < min)
255216706Sjilles		newlen <<= 1;
25645618Scracauer	oldspace = stacknxt;
25745618Scracauer	oldlen = stacknleft;
25845618Scracauer
259111422Smarcel	if (stackp != NULL && stacknxt == SPACE(stackp)) {
2601556Srgrimes		INTOFF;
26164702Scracauer		oldstackp = stackp;
262111422Smarcel		stackp = oldstackp->prev;
263111422Smarcel		sp = ckrealloc((pointer)oldstackp, newlen);
2641556Srgrimes		sp->prev = stackp;
2651556Srgrimes		stackp = sp;
266111422Smarcel		stacknxt = SPACE(sp);
267111422Smarcel		stacknleft = newlen - (stacknxt - (char*)sp);
268216743Sjilles		sstrend = stacknxt + stacknleft;
2691556Srgrimes		INTON;
2701556Srgrimes	} else {
271216706Sjilles		newlen -= ALIGN(sizeof(struct stack_block));
2721556Srgrimes		p = stalloc(newlen);
273111422Smarcel		if (oldlen != 0)
274111422Smarcel			memcpy(p, oldspace, oldlen);
275111422Smarcel		stunalloc(p);
2761556Srgrimes	}
2771556Srgrimes}
2781556Srgrimes
2791556Srgrimes
2801556Srgrimes
2811556Srgrimes/*
2821556Srgrimes * The following routines are somewhat easier to use that the above.
2831556Srgrimes * The user declares a variable of type STACKSTR, which may be declared
2841556Srgrimes * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2851556Srgrimes * the user uses the macro STPUTC to add characters to the string.  In
2861556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2871556Srgrimes * grown as necessary.  When the user is done, she can just leave the
2881556Srgrimes * string there and refer to it using stackblock().  Or she can allocate
2891556Srgrimes * the space for it using grabstackstr().  If it is necessary to allow
2901556Srgrimes * someone else to use the stack temporarily and then continue to grow
2911556Srgrimes * the string, the user should use grabstack to allocate the space, and
2921556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation.
2931556Srgrimes *
2941556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow.
2951556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
2961556Srgrimes * is space for at least one character.
2971556Srgrimes */
2981556Srgrimes
299213814Sobrienstatic char *
300216706Sjillesgrowstrstackblock(int n, int min)
301213814Sobrien{
302216706Sjilles	growstackblock(min);
303213814Sobrien	return stackblock() + n;
304213814Sobrien}
3051556Srgrimes
3061556Srgrimeschar *
30790111Simpgrowstackstr(void)
30845618Scracauer{
30945618Scracauer	int len;
31045618Scracauer
31145618Scracauer	len = stackblocksize();
312216706Sjilles	return (growstrstackblock(len, 0));
3131556Srgrimes}
3141556Srgrimes
3151556Srgrimes
3161556Srgrimes/*
3171556Srgrimes * Called from CHECKSTRSPACE.
3181556Srgrimes */
3191556Srgrimes
3201556Srgrimeschar *
321216743Sjillesmakestrspace(int min, char *p)
32245618Scracauer{
32345618Scracauer	int len;
32445618Scracauer
325216743Sjilles	len = p - stackblock();
326216706Sjilles	return (growstrstackblock(len, min));
3271556Srgrimes}
3281556Srgrimes
3291556Srgrimes
330215783Sjilleschar *
331248980Sjillesstputbin(const char *data, size_t len, char *p)
332215783Sjilles{
333216706Sjilles	CHECKSTRSPACE(len, p);
334216706Sjilles	memcpy(p, data, len);
335216706Sjilles	return (p + len);
336215783Sjilles}
337215783Sjilles
338215783Sjilleschar *
339215783Sjillesstputs(const char *data, char *p)
340215783Sjilles{
341215783Sjilles	return (stputbin(data, strlen(data), p));
342215783Sjilles}
343