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;
1011556Srgrimes
1021556Srgrimes	p = ckmalloc(strlen(s) + 1);
1031556Srgrimes	scopy(s, p);
1041556Srgrimes	return p;
1051556Srgrimes}
1061556Srgrimes
1071556Srgrimes
1081556Srgrimes/*
1091556Srgrimes * Parse trees for commands are allocated in lifo order, so we use a stack
1101556Srgrimes * to make this more efficient, and also to avoid all sorts of exception
1111556Srgrimes * handling code to handle interrupts in the middle of a parse.
1121556Srgrimes *
113111422Smarcel * The size 496 was chosen because with 16-byte alignment the total size
114111422Smarcel * for the allocated block is 512.
1151556Srgrimes */
1161556Srgrimes
117111422Smarcel#define MINSIZE 496		/* minimum size of a block. */
1181556Srgrimes
1191556Srgrimes
1201556Srgrimesstruct stack_block {
1211556Srgrimes	struct stack_block *prev;
122111422Smarcel	/* Data follows */
1231556Srgrimes};
124111422Smarcel#define SPACE(sp)	((char*)(sp) + ALIGN(sizeof(struct stack_block)))
1251556Srgrimes
126213760Sobrienstatic struct stack_block *stackp;
127213760Sobrienstatic struct stackmark *markp;
128111422Smarcelchar *stacknxt;
129111422Smarcelint stacknleft;
130216743Sjilleschar *sstrend;
1311556Srgrimes
1321556Srgrimes
133213811Sobrienstatic void
134111422Smarcelstnewblock(int nbytes)
135111422Smarcel{
136111422Smarcel	struct stack_block *sp;
137111422Smarcel	int allocsize;
1381556Srgrimes
139111422Smarcel	if (nbytes < MINSIZE)
140111422Smarcel		nbytes = MINSIZE;
141111422Smarcel
142111422Smarcel	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
143111422Smarcel
144111422Smarcel	INTOFF;
145111422Smarcel	sp = ckmalloc(allocsize);
146111422Smarcel	sp->prev = stackp;
147111422Smarcel	stacknxt = SPACE(sp);
148111422Smarcel	stacknleft = allocsize - (stacknxt - (char*)sp);
149216743Sjilles	sstrend = stacknxt + stacknleft;
150111422Smarcel	stackp = sp;
151111422Smarcel	INTON;
152111422Smarcel}
153111422Smarcel
154111422Smarcel
1551556Srgrimespointer
15690111Simpstalloc(int nbytes)
15717987Speter{
15825222Ssteve	char *p;
1591556Srgrimes
1601556Srgrimes	nbytes = ALIGN(nbytes);
161111422Smarcel	if (nbytes > stacknleft)
162111422Smarcel		stnewblock(nbytes);
1631556Srgrimes	p = stacknxt;
1641556Srgrimes	stacknxt += nbytes;
1651556Srgrimes	stacknleft -= nbytes;
1661556Srgrimes	return p;
1671556Srgrimes}
1681556Srgrimes
1691556Srgrimes
1701556Srgrimesvoid
17190111Simpstunalloc(pointer p)
17245618Scracauer{
1731556Srgrimes	if (p == NULL) {		/*DEBUG */
17480381Ssheldonh		write(STDERR_FILENO, "stunalloc\n", 10);
1751556Srgrimes		abort();
1761556Srgrimes	}
1771556Srgrimes	stacknleft += stacknxt - (char *)p;
1781556Srgrimes	stacknxt = p;
1791556Srgrimes}
1801556Srgrimes
1811556Srgrimes
1821556Srgrimes
1831556Srgrimesvoid
18490111Simpsetstackmark(struct stackmark *mark)
18545618Scracauer{
1861556Srgrimes	mark->stackp = stackp;
1871556Srgrimes	mark->stacknxt = stacknxt;
1881556Srgrimes	mark->stacknleft = stacknleft;
18964702Scracauer	mark->marknext = markp;
19064702Scracauer	markp = mark;
1911556Srgrimes}
1921556Srgrimes
1931556Srgrimes
1941556Srgrimesvoid
19590111Simppopstackmark(struct stackmark *mark)
19645618Scracauer{
1971556Srgrimes	struct stack_block *sp;
1981556Srgrimes
1991556Srgrimes	INTOFF;
20064702Scracauer	markp = mark->marknext;
2011556Srgrimes	while (stackp != mark->stackp) {
2021556Srgrimes		sp = stackp;
2031556Srgrimes		stackp = sp->prev;
2041556Srgrimes		ckfree(sp);
2051556Srgrimes	}
2061556Srgrimes	stacknxt = mark->stacknxt;
2071556Srgrimes	stacknleft = mark->stacknleft;
208216743Sjilles	sstrend = stacknxt + stacknleft;
2091556Srgrimes	INTON;
2101556Srgrimes}
2111556Srgrimes
2121556Srgrimes
2131556Srgrimes/*
2141556Srgrimes * When the parser reads in a string, it wants to stick the string on the
2151556Srgrimes * stack and only adjust the stack pointer when it knows how big the
2161556Srgrimes * string is.  Stackblock (defined in stack.h) returns a pointer to a block
2171556Srgrimes * of space on top of the stack and stackblocklen returns the length of
2181556Srgrimes * this block.  Growstackblock will grow this space by at least one byte,
2191556Srgrimes * possibly moving it (like realloc).  Grabstackblock actually allocates the
2201556Srgrimes * part of the block that has been used.
2211556Srgrimes */
2221556Srgrimes
223216706Sjillesstatic void
224216706Sjillesgrowstackblock(int min)
22545618Scracauer{
2261556Srgrimes	char *p;
22745618Scracauer	int newlen;
22845618Scracauer	char *oldspace;
22945618Scracauer	int oldlen;
2301556Srgrimes	struct stack_block *sp;
23164702Scracauer	struct stack_block *oldstackp;
232111422Smarcel	struct stackmark *xmark;
2331556Srgrimes
234216706Sjilles	if (min < stacknleft)
235216706Sjilles		min = stacknleft;
236216707Sjilles	if (min >= INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
237216706Sjilles		error("Out of space");
238216706Sjilles	min += stacknleft;
239216706Sjilles	min += ALIGN(sizeof(struct stack_block));
240216706Sjilles	newlen = 512;
241216706Sjilles	while (newlen < min)
242216706Sjilles		newlen <<= 1;
24345618Scracauer	oldspace = stacknxt;
24445618Scracauer	oldlen = stacknleft;
24545618Scracauer
246111422Smarcel	if (stackp != NULL && stacknxt == SPACE(stackp)) {
2471556Srgrimes		INTOFF;
24864702Scracauer		oldstackp = stackp;
249111422Smarcel		stackp = oldstackp->prev;
250111422Smarcel		sp = ckrealloc((pointer)oldstackp, newlen);
2511556Srgrimes		sp->prev = stackp;
2521556Srgrimes		stackp = sp;
253111422Smarcel		stacknxt = SPACE(sp);
254111422Smarcel		stacknleft = newlen - (stacknxt - (char*)sp);
255216743Sjilles		sstrend = stacknxt + stacknleft;
256111422Smarcel
257111422Smarcel		/*
258111422Smarcel		 * Stack marks pointing to the start of the old block
259111422Smarcel		 * must be relocated to point to the new block
260111422Smarcel		 */
261111422Smarcel		xmark = markp;
262111422Smarcel		while (xmark != NULL && xmark->stackp == oldstackp) {
263111422Smarcel			xmark->stackp = stackp;
264111422Smarcel			xmark->stacknxt = stacknxt;
265111422Smarcel			xmark->stacknleft = stacknleft;
266111422Smarcel			xmark = xmark->marknext;
26764702Scracauer		}
2681556Srgrimes		INTON;
2691556Srgrimes	} else {
270216706Sjilles		newlen -= ALIGN(sizeof(struct stack_block));
2711556Srgrimes		p = stalloc(newlen);
272111422Smarcel		if (oldlen != 0)
273111422Smarcel			memcpy(p, oldspace, oldlen);
274111422Smarcel		stunalloc(p);
2751556Srgrimes	}
2761556Srgrimes}
2771556Srgrimes
2781556Srgrimes
2791556Srgrimes
2801556Srgrimes/*
2811556Srgrimes * The following routines are somewhat easier to use that the above.
2821556Srgrimes * The user declares a variable of type STACKSTR, which may be declared
2831556Srgrimes * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2841556Srgrimes * the user uses the macro STPUTC to add characters to the string.  In
2851556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2861556Srgrimes * grown as necessary.  When the user is done, she can just leave the
2871556Srgrimes * string there and refer to it using stackblock().  Or she can allocate
2881556Srgrimes * the space for it using grabstackstr().  If it is necessary to allow
2891556Srgrimes * someone else to use the stack temporarily and then continue to grow
2901556Srgrimes * the string, the user should use grabstack to allocate the space, and
2911556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation.
2921556Srgrimes *
2931556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow.
2941556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
2951556Srgrimes * is space for at least one character.
2961556Srgrimes */
2971556Srgrimes
298213814Sobrienstatic char *
299216706Sjillesgrowstrstackblock(int n, int min)
300213814Sobrien{
301216706Sjilles	growstackblock(min);
302213814Sobrien	return stackblock() + n;
303213814Sobrien}
3041556Srgrimes
3051556Srgrimeschar *
30690111Simpgrowstackstr(void)
30745618Scracauer{
30845618Scracauer	int len;
30945618Scracauer
31045618Scracauer	len = stackblocksize();
311216706Sjilles	return (growstrstackblock(len, 0));
3121556Srgrimes}
3131556Srgrimes
3141556Srgrimes
3151556Srgrimes/*
3161556Srgrimes * Called from CHECKSTRSPACE.
3171556Srgrimes */
3181556Srgrimes
3191556Srgrimeschar *
320216743Sjillesmakestrspace(int min, char *p)
32145618Scracauer{
32245618Scracauer	int len;
32345618Scracauer
324216743Sjilles	len = p - stackblock();
325216706Sjilles	return (growstrstackblock(len, min));
3261556Srgrimes}
3271556Srgrimes
3281556Srgrimes
329215783Sjilleschar *
330215783Sjillesstputbin(const char *data, int len, char *p)
331215783Sjilles{
332216706Sjilles	CHECKSTRSPACE(len, p);
333216706Sjilles	memcpy(p, data, len);
334216706Sjilles	return (p + len);
335215783Sjilles}
336215783Sjilles
337215783Sjilleschar *
338215783Sjillesstputs(const char *data, char *p)
339215783Sjilles{
340215783Sjilles	return (stputbin(data, strlen(data), p));
341215783Sjilles}
342