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
1831556Srgrimes
1841556Srgrimesvoid
18590111Simpsetstackmark(struct stackmark *mark)
18645618Scracauer{
1871556Srgrimes	mark->stackp = stackp;
1881556Srgrimes	mark->stacknxt = stacknxt;
1891556Srgrimes	mark->stacknleft = stacknleft;
190250527Sjilles	/* Ensure this block stays in place. */
191250527Sjilles	if (stackp != NULL && stacknxt == SPACE(stackp))
192250527Sjilles		stalloc(1);
1931556Srgrimes}
1941556Srgrimes
1951556Srgrimes
1961556Srgrimesvoid
19790111Simppopstackmark(struct stackmark *mark)
19845618Scracauer{
1991556Srgrimes	struct stack_block *sp;
2001556Srgrimes
2011556Srgrimes	INTOFF;
2021556Srgrimes	while (stackp != mark->stackp) {
2031556Srgrimes		sp = stackp;
2041556Srgrimes		stackp = sp->prev;
2051556Srgrimes		ckfree(sp);
2061556Srgrimes	}
2071556Srgrimes	stacknxt = mark->stacknxt;
2081556Srgrimes	stacknleft = mark->stacknleft;
209216743Sjilles	sstrend = stacknxt + stacknleft;
2101556Srgrimes	INTON;
2111556Srgrimes}
2121556Srgrimes
2131556Srgrimes
2141556Srgrimes/*
2151556Srgrimes * When the parser reads in a string, it wants to stick the string on the
2161556Srgrimes * stack and only adjust the stack pointer when it knows how big the
2171556Srgrimes * string is.  Stackblock (defined in stack.h) returns a pointer to a block
2181556Srgrimes * of space on top of the stack and stackblocklen returns the length of
2191556Srgrimes * this block.  Growstackblock will grow this space by at least one byte,
2201556Srgrimes * possibly moving it (like realloc).  Grabstackblock actually allocates the
2211556Srgrimes * part of the block that has been used.
2221556Srgrimes */
2231556Srgrimes
224216706Sjillesstatic void
225216706Sjillesgrowstackblock(int min)
22645618Scracauer{
2271556Srgrimes	char *p;
22845618Scracauer	int newlen;
22945618Scracauer	char *oldspace;
23045618Scracauer	int oldlen;
2311556Srgrimes	struct stack_block *sp;
23264702Scracauer	struct stack_block *oldstackp;
2331556Srgrimes
234216706Sjilles	if (min < stacknleft)
235216706Sjilles		min = stacknleft;
236248980Sjilles	if ((unsigned int)min >=
237248980Sjilles	    INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
238216706Sjilles		error("Out of space");
239216706Sjilles	min += stacknleft;
240216706Sjilles	min += ALIGN(sizeof(struct stack_block));
241216706Sjilles	newlen = 512;
242216706Sjilles	while (newlen < min)
243216706Sjilles		newlen <<= 1;
24445618Scracauer	oldspace = stacknxt;
24545618Scracauer	oldlen = stacknleft;
24645618Scracauer
247111422Smarcel	if (stackp != NULL && stacknxt == SPACE(stackp)) {
2481556Srgrimes		INTOFF;
24964702Scracauer		oldstackp = stackp;
250111422Smarcel		stackp = oldstackp->prev;
251111422Smarcel		sp = ckrealloc((pointer)oldstackp, newlen);
2521556Srgrimes		sp->prev = stackp;
2531556Srgrimes		stackp = sp;
254111422Smarcel		stacknxt = SPACE(sp);
255111422Smarcel		stacknleft = newlen - (stacknxt - (char*)sp);
256216743Sjilles		sstrend = stacknxt + stacknleft;
2571556Srgrimes		INTON;
2581556Srgrimes	} else {
259216706Sjilles		newlen -= ALIGN(sizeof(struct stack_block));
2601556Srgrimes		p = stalloc(newlen);
261111422Smarcel		if (oldlen != 0)
262111422Smarcel			memcpy(p, oldspace, oldlen);
263111422Smarcel		stunalloc(p);
2641556Srgrimes	}
2651556Srgrimes}
2661556Srgrimes
2671556Srgrimes
2681556Srgrimes
2691556Srgrimes/*
2701556Srgrimes * The following routines are somewhat easier to use that the above.
2711556Srgrimes * The user declares a variable of type STACKSTR, which may be declared
2721556Srgrimes * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2731556Srgrimes * the user uses the macro STPUTC to add characters to the string.  In
2741556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2751556Srgrimes * grown as necessary.  When the user is done, she can just leave the
2761556Srgrimes * string there and refer to it using stackblock().  Or she can allocate
2771556Srgrimes * the space for it using grabstackstr().  If it is necessary to allow
2781556Srgrimes * someone else to use the stack temporarily and then continue to grow
2791556Srgrimes * the string, the user should use grabstack to allocate the space, and
2801556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation.
2811556Srgrimes *
2821556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow.
2831556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
2841556Srgrimes * is space for at least one character.
2851556Srgrimes */
2861556Srgrimes
287213814Sobrienstatic char *
288216706Sjillesgrowstrstackblock(int n, int min)
289213814Sobrien{
290216706Sjilles	growstackblock(min);
291213814Sobrien	return stackblock() + n;
292213814Sobrien}
2931556Srgrimes
2941556Srgrimeschar *
29590111Simpgrowstackstr(void)
29645618Scracauer{
29745618Scracauer	int len;
29845618Scracauer
29945618Scracauer	len = stackblocksize();
300216706Sjilles	return (growstrstackblock(len, 0));
3011556Srgrimes}
3021556Srgrimes
3031556Srgrimes
3041556Srgrimes/*
3051556Srgrimes * Called from CHECKSTRSPACE.
3061556Srgrimes */
3071556Srgrimes
3081556Srgrimeschar *
309216743Sjillesmakestrspace(int min, char *p)
31045618Scracauer{
31145618Scracauer	int len;
31245618Scracauer
313216743Sjilles	len = p - stackblock();
314216706Sjilles	return (growstrstackblock(len, min));
3151556Srgrimes}
3161556Srgrimes
3171556Srgrimes
318215783Sjilleschar *
319248980Sjillesstputbin(const char *data, size_t len, char *p)
320215783Sjilles{
321216706Sjilles	CHECKSTRSPACE(len, p);
322216706Sjilles	memcpy(p, data, len);
323216706Sjilles	return (p + len);
324215783Sjilles}
325215783Sjilles
326215783Sjilleschar *
327215783Sjillesstputs(const char *data, char *p)
328215783Sjilles{
329215783Sjilles	return (stputbin(data, strlen(data), p));
330215783Sjilles}
331