]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bmake/buf.c
sysctl(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / contrib / bmake / buf.c
1 /*      $NetBSD: buf.c,v 1.44 2020/11/07 14:11:58 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 /* Automatically-expanding null-terminated buffers. */
73
74 #include <limits.h>
75 #include "make.h"
76
77 /*      "@(#)buf.c      8.1 (Berkeley) 6/6/93"  */
78 MAKE_RCSID("$NetBSD: buf.c,v 1.44 2020/11/07 14:11:58 rillig Exp $");
79
80 /* Make space in the buffer for adding a single byte. */
81 void
82 Buf_Expand_1(Buffer *buf)
83 {
84     buf->cap += buf->cap > 16 ? buf->cap : 16;
85     buf->data = bmake_realloc(buf->data, buf->cap);
86 }
87
88 /* Add the bytes to the buffer. */
89 void
90 Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len)
91 {
92     size_t old_len = buf->len;
93     char *end;
94
95     if (__predict_false(old_len + bytes_len >= buf->cap)) {
96         buf->cap += buf->cap > bytes_len + 16 ? buf->cap : bytes_len + 16;
97         buf->data = bmake_realloc(buf->data, buf->cap);
98     }
99
100     end = buf->data + old_len;
101     buf->len = old_len + bytes_len;
102     memcpy(end, bytes, bytes_len);
103     end[bytes_len] = '\0';
104 }
105
106 /* Add the bytes between start and end to the buffer. */
107 void
108 Buf_AddBytesBetween(Buffer *buf, const char *start, const char *end)
109 {
110     Buf_AddBytes(buf, start, (size_t)(end - start));
111 }
112
113 /* Add the string to the buffer. */
114 void
115 Buf_AddStr(Buffer *buf, const char *str)
116 {
117     Buf_AddBytes(buf, str, strlen(str));
118 }
119
120 /* Add the number to the buffer. */
121 void
122 Buf_AddInt(Buffer *buf, int n)
123 {
124     enum {
125         bits = sizeof(int) * CHAR_BIT,
126         max_octal_digits = (bits + 2) / 3,
127         max_decimal_digits = /* at most */ max_octal_digits,
128         max_sign_chars = 1,
129         str_size = max_sign_chars + max_decimal_digits + 1
130     };
131     char str[str_size];
132
133     size_t len = (size_t)snprintf(str, sizeof str, "%d", n);
134     Buf_AddBytes(buf, str, len);
135 }
136
137 /* Get the data (usually a string) from the buffer.
138  * The returned data is valid until the next modifying operation
139  * on the buffer.
140  *
141  * Returns the data and optionally the length of the data. */
142 char *
143 Buf_GetAll(Buffer *buf, size_t *out_len)
144 {
145     if (out_len != NULL)
146         *out_len = buf->len;
147     return buf->data;
148 }
149
150 /* Mark the buffer as empty, so it can be filled with data again. */
151 void
152 Buf_Empty(Buffer *buf)
153 {
154     buf->len = 0;
155     buf->data[0] = '\0';
156 }
157
158 /* Initialize a buffer. */
159 void
160 Buf_InitSize(Buffer *buf, size_t cap)
161 {
162     buf->cap = cap;
163     buf->len = 0;
164     buf->data = bmake_malloc(cap);
165     buf->data[0] = '\0';
166 }
167
168 void
169 Buf_Init(Buffer *buf)
170 {
171     Buf_InitSize(buf, 256);
172 }
173
174 /* Reset the buffer.
175  * If freeData is TRUE, the data from the buffer is freed as well.
176  * Otherwise it is kept and returned. */
177 char *
178 Buf_Destroy(Buffer *buf, Boolean freeData)
179 {
180     char *data = buf->data;
181     if (freeData) {
182         free(data);
183         data = NULL;
184     }
185
186     buf->cap = 0;
187     buf->len = 0;
188     buf->data = NULL;
189
190     return data;
191 }
192
193 #ifndef BUF_COMPACT_LIMIT
194 # define BUF_COMPACT_LIMIT 128          /* worthwhile saving */
195 #endif
196
197 /* Reset the buffer and return its data.
198  *
199  * If the buffer size is much greater than its content,
200  * a new buffer will be allocated and the old one freed. */
201 char *
202 Buf_DestroyCompact(Buffer *buf)
203 {
204 #if BUF_COMPACT_LIMIT > 0
205     if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) {
206         /* We trust realloc to be smart */
207         char *data = bmake_realloc(buf->data, buf->len + 1);
208         data[buf->len] = '\0';  /* XXX: unnecessary */
209         Buf_Destroy(buf, FALSE);
210         return data;
211     }
212 #endif
213     return Buf_Destroy(buf, FALSE);
214 }