xref: /freebsd/contrib/bmake/buf.c (revision 2ccf8a827cce7e34e2063a3d33e5cce4b79799cc)
1 /*	$NetBSD: buf.c,v 1.37 2020/08/23 08:21:50 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.37 2020/08/23 08:21:50 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.37 2020/08/23 08:21:50 rillig Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
84 
85 /* Functions for automatically-expanded null-terminated buffers. */
86 
87 #include <limits.h>
88 #include "make.h"
89 
90 /* Extend the buffer for adding a single byte. */
91 void
92 Buf_Expand_1(Buffer *bp)
93 {
94     bp->size += MAX(bp->size, 16);
95     bp->buffer = bmake_realloc(bp->buffer, bp->size);
96 }
97 
98 /* Add the given bytes to the buffer. */
99 void
100 Buf_AddBytes(Buffer *bp, const char *bytesPtr, size_t numBytes)
101 {
102     size_t count = bp->count;
103     char *ptr;
104 
105     if (__predict_false(count + numBytes >= bp->size)) {
106 	bp->size += MAX(bp->size, numBytes + 16);
107 	bp->buffer = bmake_realloc(bp->buffer, bp->size);
108     }
109 
110     ptr = bp->buffer + count;
111     bp->count = count + numBytes;
112     memcpy(ptr, bytesPtr, numBytes);
113     ptr[numBytes] = '\0';
114 }
115 
116 /* Add the bytes between start and end to the buffer. */
117 void
118 Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
119 {
120     Buf_AddBytes(bp, start, (size_t)(end - start));
121 }
122 
123 /* Add the given string to the buffer. */
124 void
125 Buf_AddStr(Buffer *bp, const char *str)
126 {
127     Buf_AddBytes(bp, str, strlen(str));
128 }
129 
130 /* Add the given number to the buffer. */
131 void
132 Buf_AddInt(Buffer *bp, int n)
133 {
134     enum {
135 	bits = sizeof(int) * CHAR_BIT,
136 	max_octal_digits = (bits + 2) / 3,
137 	max_decimal_digits = /* at most */ max_octal_digits,
138 	max_sign_chars = 1,
139 	buf_size = max_sign_chars + max_decimal_digits + 1
140     };
141     char buf[buf_size];
142 
143     size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
144     Buf_AddBytes(bp, buf, len);
145 }
146 
147 /* Get the data (usually a string) from the buffer.
148  * The returned data is valid until the next modifying operation
149  * on the buffer.
150  *
151  * Returns the pointer to the data and optionally the length of the
152  * data in the buffer. */
153 char *
154 Buf_GetAll(Buffer *bp, size_t *numBytesPtr)
155 {
156     if (numBytesPtr != NULL)
157 	*numBytesPtr = bp->count;
158     return bp->buffer;
159 }
160 
161 /* Mark the buffer as empty, so it can be filled with data again. */
162 void
163 Buf_Empty(Buffer *bp)
164 {
165     bp->count = 0;
166     bp->buffer[0] = '\0';
167 }
168 
169 /* Initialize a buffer.
170  * If the given initial size is 0, a reasonable default is used. */
171 void
172 Buf_Init(Buffer *bp, size_t size)
173 {
174     if (size <= 0) {
175 	size = 256;
176     }
177     bp->size = size;
178     bp->count = 0;
179     bp->buffer = bmake_malloc(size);
180     bp->buffer[0] = '\0';
181 }
182 
183 /* Reset the buffer.
184  * If freeData is TRUE, the data from the buffer is freed as well.
185  * Otherwise it is kept and returned. */
186 char *
187 Buf_Destroy(Buffer *buf, Boolean freeData)
188 {
189     char *data = buf->buffer;
190     if (freeData) {
191 	free(data);
192 	data = NULL;
193     }
194 
195     buf->size = 0;
196     buf->count = 0;
197     buf->buffer = NULL;
198 
199     return data;
200 }
201 
202 #ifndef BUF_COMPACT_LIMIT
203 # define BUF_COMPACT_LIMIT 128		/* worthwhile saving */
204 #endif
205 
206 /* Reset the buffer and return its data.
207  *
208  * If the buffer size is much greater than its content,
209  * a new buffer will be allocated and the old one freed. */
210 char *
211 Buf_DestroyCompact(Buffer *buf)
212 {
213 #if BUF_COMPACT_LIMIT > 0
214     if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
215 	/* We trust realloc to be smart */
216 	char *data = bmake_realloc(buf->buffer, buf->count + 1);
217 	data[buf->count] = '\0';
218 	Buf_Destroy(buf, FALSE);
219 	return data;
220     }
221 #endif
222     return Buf_Destroy(buf, FALSE);
223 }
224