xref: /netbsd-src/usr.bin/make/buf.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*	$NetBSD: buf.c,v 1.35 2020/08/13 04:12:13 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 #ifndef MAKE_NATIVE
73 static char rcsid[] = "$NetBSD: buf.c,v 1.35 2020/08/13 04:12:13 rillig Exp $";
74 #else
75 #include <sys/cdefs.h>
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)buf.c	8.1 (Berkeley) 6/6/93";
79 #else
80 __RCSID("$NetBSD: buf.c,v 1.35 2020/08/13 04:12:13 rillig Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
84 
85 /* Functions for automatically-expanded NUL-terminated buffers. */
86 
87 #include <limits.h>
88 #include "make.h"
89 #include "buf.h"
90 
91 #ifndef max
92 #define max(a, b)  ((a) > (b) ? (a) : (b))
93 #endif
94 
95 #define BUF_DEF_SIZE	256 	/* Default buffer size */
96 
97 /* Extend the buffer for adding a single byte. */
98 void
99 Buf_Expand_1(Buffer *bp)
100 {
101     bp->size += max(bp->size, 16);
102     bp->buffer = bmake_realloc(bp->buffer, bp->size);
103 }
104 
105 /* Add the given bytes to the buffer. */
106 void
107 Buf_AddBytes(Buffer *bp, const char *bytesPtr, size_t numBytes)
108 {
109     size_t count = bp->count;
110     char *ptr;
111 
112     if (__predict_false(count + numBytes >= bp->size)) {
113 	bp->size += max(bp->size, numBytes + 16);
114 	bp->buffer = bmake_realloc(bp->buffer, bp->size);
115     }
116 
117     ptr = bp->buffer + count;
118     bp->count = count + numBytes;
119     memcpy(ptr, bytesPtr, numBytes);
120     ptr[numBytes] = '\0';
121 }
122 
123 /* Add the bytes between start and end to the buffer. */
124 void
125 Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
126 {
127     Buf_AddBytes(bp, start, (size_t)(end - start));
128 }
129 
130 /* Add the given string to the buffer. */
131 void
132 Buf_AddStr(Buffer *bp, const char *str)
133 {
134     Buf_AddBytes(bp, str, strlen(str));
135 }
136 
137 /* Add the given number to the buffer. */
138 void
139 Buf_AddInt(Buffer *bp, int n)
140 {
141     /*
142      * We need enough space for the decimal representation of an int.
143      * We calculate the space needed for the octal representation, and
144      * add enough slop to cope with a '-' sign and a trailing '\0'.
145      */
146     enum {
147 	bits = sizeof(int) * CHAR_BIT,
148 	buf_size = 1 + (bits + 2) / 3 + 1
149     };
150     char buf[buf_size];
151 
152     size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
153     Buf_AddBytes(bp, buf, len);
154 }
155 
156 /* Get the data (usually a string) from the buffer.
157  * The returned data is valid until the next modifying operation
158  * on the buffer.
159  *
160  * Returns the pointer to the data and optionally the length of the
161  * data in the buffer. */
162 char *
163 Buf_GetAll(Buffer *bp, size_t *numBytesPtr)
164 {
165     if (numBytesPtr != NULL)
166 	*numBytesPtr = bp->count;
167     return bp->buffer;
168 }
169 
170 /* Mark the buffer as empty, so it can be filled with data again. */
171 void
172 Buf_Empty(Buffer *bp)
173 {
174     bp->count = 0;
175     bp->buffer[0] = '\0';
176 }
177 
178 /* Initialize a buffer.
179  * If the given initial size is 0, a reasonable default is used. */
180 void
181 Buf_Init(Buffer *bp, size_t size)
182 {
183     if (size <= 0) {
184 	size = BUF_DEF_SIZE;
185     }
186     bp->size = size;
187     bp->count = 0;
188     bp->buffer = bmake_malloc(size);
189     *bp->buffer = 0;
190 }
191 
192 /* Reset the buffer.
193  * If freeData is TRUE, the data from the buffer is freed as well.
194  * Otherwise it is kept and returned. */
195 char *
196 Buf_Destroy(Buffer *buf, Boolean freeData)
197 {
198     char *data = buf->buffer;
199     if (freeData) {
200 	free(data);
201 	data = NULL;
202     }
203 
204     buf->size = 0;
205     buf->count = 0;
206     buf->buffer = NULL;
207 
208     return data;
209 }
210 
211 #ifndef BUF_COMPACT_LIMIT
212 # define BUF_COMPACT_LIMIT 128		/* worthwhile saving */
213 #endif
214 
215 /* Reset the buffer and return its data.
216  *
217  * If the buffer size is much greater than its content,
218  * a new buffer will be allocated and the old one freed. */
219 char *
220 Buf_DestroyCompact(Buffer *buf)
221 {
222 #if BUF_COMPACT_LIMIT > 0
223     if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
224 	/* We trust realloc to be smart */
225 	char *data = bmake_realloc(buf->buffer, buf->count + 1);
226 	data[buf->count] = 0;
227 	Buf_Destroy(buf, FALSE);
228 	return data;
229     }
230 #endif
231     return Buf_Destroy(buf, FALSE);
232 }
233