xref: /freebsd/contrib/bmake/buf.c (revision 06b9b3e0ad0dc3f0166b3e8f26ced68c271cf527)
1*06b9b3e0SSimon J. Gerraty /*	$NetBSD: buf.c,v 1.47 2020/12/30 10:03:16 rillig Exp $	*/
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
53955d011SMarcel Moolenaar  * All rights reserved.
63955d011SMarcel Moolenaar  *
73955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
83955d011SMarcel Moolenaar  * Adam de Boor.
93955d011SMarcel Moolenaar  *
103955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
113955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
123955d011SMarcel Moolenaar  * are met:
133955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
143955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
153955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
163955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
173955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
183955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
193955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
203955d011SMarcel Moolenaar  *    without specific prior written permission.
213955d011SMarcel Moolenaar  *
223955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323955d011SMarcel Moolenaar  * SUCH DAMAGE.
333955d011SMarcel Moolenaar  */
343955d011SMarcel Moolenaar 
353955d011SMarcel Moolenaar /*
363955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989 by Adam de Boor
373955d011SMarcel Moolenaar  * Copyright (c) 1989 by Berkeley Softworks
383955d011SMarcel Moolenaar  * All rights reserved.
393955d011SMarcel Moolenaar  *
403955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
413955d011SMarcel Moolenaar  * Adam de Boor.
423955d011SMarcel Moolenaar  *
433955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
443955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
453955d011SMarcel Moolenaar  * are met:
463955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
473955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
483955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
493955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
503955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
513955d011SMarcel Moolenaar  * 3. All advertising materials mentioning features or use of this software
523955d011SMarcel Moolenaar  *    must display the following acknowledgement:
533955d011SMarcel Moolenaar  *	This product includes software developed by the University of
543955d011SMarcel Moolenaar  *	California, Berkeley and its contributors.
553955d011SMarcel Moolenaar  * 4. Neither the name of the University nor the names of its contributors
563955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
573955d011SMarcel Moolenaar  *    without specific prior written permission.
583955d011SMarcel Moolenaar  *
593955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
603955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
613955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
623955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
633955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
643955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
653955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
663955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
673955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
683955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
693955d011SMarcel Moolenaar  * SUCH DAMAGE.
703955d011SMarcel Moolenaar  */
713955d011SMarcel Moolenaar 
72*06b9b3e0SSimon J. Gerraty /* Automatically-expanding null-terminated character buffers. */
733955d011SMarcel Moolenaar 
742c3632d1SSimon J. Gerraty #include <limits.h>
753955d011SMarcel Moolenaar #include "make.h"
763955d011SMarcel Moolenaar 
77956e45f6SSimon J. Gerraty /*	"@(#)buf.c	8.1 (Berkeley) 6/6/93"	*/
78*06b9b3e0SSimon J. Gerraty MAKE_RCSID("$NetBSD: buf.c,v 1.47 2020/12/30 10:03:16 rillig Exp $");
79956e45f6SSimon J. Gerraty 
80*06b9b3e0SSimon J. Gerraty /* Make space in the buffer for adding at least 16 more bytes. */
813955d011SMarcel Moolenaar void
82*06b9b3e0SSimon J. Gerraty Buf_Expand(Buffer *buf)
833955d011SMarcel Moolenaar {
84956e45f6SSimon J. Gerraty 	buf->cap += buf->cap > 16 ? buf->cap : 16;
85956e45f6SSimon J. Gerraty 	buf->data = bmake_realloc(buf->data, buf->cap);
863955d011SMarcel Moolenaar }
873955d011SMarcel Moolenaar 
88956e45f6SSimon J. Gerraty /* Add the bytes to the buffer. */
893955d011SMarcel Moolenaar void
90956e45f6SSimon J. Gerraty Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len)
913955d011SMarcel Moolenaar {
92956e45f6SSimon J. Gerraty 	size_t old_len = buf->len;
93956e45f6SSimon J. Gerraty 	char *end;
943955d011SMarcel Moolenaar 
95956e45f6SSimon J. Gerraty 	if (__predict_false(old_len + bytes_len >= buf->cap)) {
96*06b9b3e0SSimon J. Gerraty 		size_t minIncr = bytes_len + 16;
97*06b9b3e0SSimon J. Gerraty 		buf->cap += buf->cap > minIncr ? buf->cap : minIncr;
98956e45f6SSimon J. Gerraty 		buf->data = bmake_realloc(buf->data, buf->cap);
993955d011SMarcel Moolenaar 	}
1003955d011SMarcel Moolenaar 
101956e45f6SSimon J. Gerraty 	end = buf->data + old_len;
102956e45f6SSimon J. Gerraty 	buf->len = old_len + bytes_len;
103956e45f6SSimon J. Gerraty 	memcpy(end, bytes, bytes_len);
104956e45f6SSimon J. Gerraty 	end[bytes_len] = '\0';
1053955d011SMarcel Moolenaar }
1063955d011SMarcel Moolenaar 
1072c3632d1SSimon J. Gerraty /* Add the bytes between start and end to the buffer. */
1082c3632d1SSimon J. Gerraty void
109956e45f6SSimon J. Gerraty Buf_AddBytesBetween(Buffer *buf, const char *start, const char *end)
1103955d011SMarcel Moolenaar {
111956e45f6SSimon J. Gerraty 	Buf_AddBytes(buf, start, (size_t)(end - start));
1122c3632d1SSimon J. Gerraty }
1133955d011SMarcel Moolenaar 
114956e45f6SSimon J. Gerraty /* Add the string to the buffer. */
1152c3632d1SSimon J. Gerraty void
116956e45f6SSimon J. Gerraty Buf_AddStr(Buffer *buf, const char *str)
1172c3632d1SSimon J. Gerraty {
118956e45f6SSimon J. Gerraty 	Buf_AddBytes(buf, str, strlen(str));
1192c3632d1SSimon J. Gerraty }
1202c3632d1SSimon J. Gerraty 
121956e45f6SSimon J. Gerraty /* Add the number to the buffer. */
1222c3632d1SSimon J. Gerraty void
123956e45f6SSimon J. Gerraty Buf_AddInt(Buffer *buf, int n)
1242c3632d1SSimon J. Gerraty {
1252c3632d1SSimon J. Gerraty 	enum {
1262c3632d1SSimon J. Gerraty 		bits = sizeof(int) * CHAR_BIT,
1272c3632d1SSimon J. Gerraty 		max_octal_digits = (bits + 2) / 3,
1282c3632d1SSimon J. Gerraty 		max_decimal_digits = /* at most */ max_octal_digits,
1292c3632d1SSimon J. Gerraty 		max_sign_chars = 1,
130956e45f6SSimon J. Gerraty 		str_size = max_sign_chars + max_decimal_digits + 1
1312c3632d1SSimon J. Gerraty 	};
132956e45f6SSimon J. Gerraty 	char str[str_size];
1332c3632d1SSimon J. Gerraty 
134956e45f6SSimon J. Gerraty 	size_t len = (size_t)snprintf(str, sizeof str, "%d", n);
135956e45f6SSimon J. Gerraty 	Buf_AddBytes(buf, str, len);
1362c3632d1SSimon J. Gerraty }
1372c3632d1SSimon J. Gerraty 
138*06b9b3e0SSimon J. Gerraty /*
139*06b9b3e0SSimon J. Gerraty  * Get the data (usually a string) from the buffer.
1402c3632d1SSimon J. Gerraty  * The returned data is valid until the next modifying operation
1412c3632d1SSimon J. Gerraty  * on the buffer.
1422c3632d1SSimon J. Gerraty  *
143*06b9b3e0SSimon J. Gerraty  * Returns the data and optionally the length of the data.
144*06b9b3e0SSimon J. Gerraty  */
1452c3632d1SSimon J. Gerraty char *
146956e45f6SSimon J. Gerraty Buf_GetAll(Buffer *buf, size_t *out_len)
1472c3632d1SSimon J. Gerraty {
148956e45f6SSimon J. Gerraty 	if (out_len != NULL)
149956e45f6SSimon J. Gerraty 		*out_len = buf->len;
150956e45f6SSimon J. Gerraty 	return buf->data;
1513955d011SMarcel Moolenaar }
1523955d011SMarcel Moolenaar 
1532c3632d1SSimon J. Gerraty /* Mark the buffer as empty, so it can be filled with data again. */
1543955d011SMarcel Moolenaar void
155956e45f6SSimon J. Gerraty Buf_Empty(Buffer *buf)
1563955d011SMarcel Moolenaar {
157956e45f6SSimon J. Gerraty 	buf->len = 0;
158956e45f6SSimon J. Gerraty 	buf->data[0] = '\0';
1593955d011SMarcel Moolenaar }
1603955d011SMarcel Moolenaar 
161e2eeea75SSimon J. Gerraty /* Initialize a buffer. */
1623955d011SMarcel Moolenaar void
163e2eeea75SSimon J. Gerraty Buf_InitSize(Buffer *buf, size_t cap)
1643955d011SMarcel Moolenaar {
165956e45f6SSimon J. Gerraty 	buf->cap = cap;
166956e45f6SSimon J. Gerraty 	buf->len = 0;
167956e45f6SSimon J. Gerraty 	buf->data = bmake_malloc(cap);
168956e45f6SSimon J. Gerraty 	buf->data[0] = '\0';
1693955d011SMarcel Moolenaar }
1703955d011SMarcel Moolenaar 
171e2eeea75SSimon J. Gerraty void
172e2eeea75SSimon J. Gerraty Buf_Init(Buffer *buf)
173e2eeea75SSimon J. Gerraty {
174e2eeea75SSimon J. Gerraty 	Buf_InitSize(buf, 256);
175e2eeea75SSimon J. Gerraty }
176e2eeea75SSimon J. Gerraty 
177*06b9b3e0SSimon J. Gerraty /*
178*06b9b3e0SSimon J. Gerraty  * Reset the buffer.
1792c3632d1SSimon J. Gerraty  * If freeData is TRUE, the data from the buffer is freed as well.
180*06b9b3e0SSimon J. Gerraty  * Otherwise it is kept and returned.
181*06b9b3e0SSimon J. Gerraty  */
1822c3632d1SSimon J. Gerraty char *
1833955d011SMarcel Moolenaar Buf_Destroy(Buffer *buf, Boolean freeData)
1843955d011SMarcel Moolenaar {
185956e45f6SSimon J. Gerraty 	char *data = buf->data;
1863955d011SMarcel Moolenaar 	if (freeData) {
1873955d011SMarcel Moolenaar 		free(data);
1883955d011SMarcel Moolenaar 		data = NULL;
1893955d011SMarcel Moolenaar 	}
1903955d011SMarcel Moolenaar 
191956e45f6SSimon J. Gerraty 	buf->cap = 0;
192956e45f6SSimon J. Gerraty 	buf->len = 0;
193956e45f6SSimon J. Gerraty 	buf->data = NULL;
1943955d011SMarcel Moolenaar 
1953955d011SMarcel Moolenaar 	return data;
1963955d011SMarcel Moolenaar }
1973955d011SMarcel Moolenaar 
1983955d011SMarcel Moolenaar #ifndef BUF_COMPACT_LIMIT
1993955d011SMarcel Moolenaar # define BUF_COMPACT_LIMIT 128	/* worthwhile saving */
2003955d011SMarcel Moolenaar #endif
2013955d011SMarcel Moolenaar 
202*06b9b3e0SSimon J. Gerraty /*
203*06b9b3e0SSimon J. Gerraty  * Reset the buffer and return its data.
2042c3632d1SSimon J. Gerraty  *
2052c3632d1SSimon J. Gerraty  * If the buffer size is much greater than its content,
206*06b9b3e0SSimon J. Gerraty  * a new buffer will be allocated and the old one freed.
207*06b9b3e0SSimon J. Gerraty  */
2082c3632d1SSimon J. Gerraty char *
2093955d011SMarcel Moolenaar Buf_DestroyCompact(Buffer *buf)
2103955d011SMarcel Moolenaar {
2113955d011SMarcel Moolenaar #if BUF_COMPACT_LIMIT > 0
212956e45f6SSimon J. Gerraty 	if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) {
2133955d011SMarcel Moolenaar 		/* We trust realloc to be smart */
214956e45f6SSimon J. Gerraty 		char *data = bmake_realloc(buf->data, buf->len + 1);
215956e45f6SSimon J. Gerraty 		data[buf->len] = '\0';	/* XXX: unnecessary */
2163955d011SMarcel Moolenaar 		Buf_Destroy(buf, FALSE);
2173955d011SMarcel Moolenaar 		return data;
2183955d011SMarcel Moolenaar 	}
2193955d011SMarcel Moolenaar #endif
2203955d011SMarcel Moolenaar 	return Buf_Destroy(buf, FALSE);
2213955d011SMarcel Moolenaar }
222