1*548bfc56SSimon J. Gerraty /* $NetBSD: buf.c,v 1.58 2024/04/28 15:10:19 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
72d5e0a182SSimon J. Gerraty /* Automatically growing null-terminated buffers of characters. */
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*548bfc56SSimon J. Gerraty MAKE_RCSID("$NetBSD: buf.c,v 1.58 2024/04/28 15:10:19 rillig Exp $");
79956e45f6SSimon J. Gerraty
8006b9b3e0SSimon J. Gerraty /* Make space in the buffer for adding at least 16 more bytes. */
813955d011SMarcel Moolenaar void
Buf_Expand(Buffer * buf)8206b9b3e0SSimon 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
Buf_AddBytes(Buffer * buf,const char * bytes,size_t bytes_len)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
95dba7b0efSSimon J. Gerraty if (old_len + bytes_len >= buf->cap) {
9606b9b3e0SSimon J. Gerraty size_t minIncr = bytes_len + 16;
9706b9b3e0SSimon 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
Buf_AddRange(Buffer * buf,const char * start,const char * end)109148ee845SSimon J. Gerraty Buf_AddRange(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
Buf_AddStr(Buffer * buf,const char * str)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
Buf_AddInt(Buffer * buf,int n)123956e45f6SSimon J. Gerraty Buf_AddInt(Buffer *buf, int n)
1242c3632d1SSimon J. Gerraty {
12512904384SSimon J. Gerraty char str[sizeof(int) * CHAR_BIT + 1];
1262c3632d1SSimon J. Gerraty
127956e45f6SSimon J. Gerraty size_t len = (size_t)snprintf(str, sizeof str, "%d", n);
128956e45f6SSimon J. Gerraty Buf_AddBytes(buf, str, len);
1292c3632d1SSimon J. Gerraty }
1302c3632d1SSimon J. Gerraty
13112904384SSimon J. Gerraty void
Buf_AddFlag(Buffer * buf,bool flag,const char * name)13212904384SSimon J. Gerraty Buf_AddFlag(Buffer *buf, bool flag, const char *name)
13312904384SSimon J. Gerraty {
13412904384SSimon J. Gerraty if (flag) {
13512904384SSimon J. Gerraty if (buf->len > 0)
13612904384SSimon J. Gerraty Buf_AddByte(buf, '|');
13712904384SSimon J. Gerraty Buf_AddBytes(buf, name, strlen(name));
13812904384SSimon J. Gerraty }
13912904384SSimon J. Gerraty }
14012904384SSimon J. Gerraty
141e2eeea75SSimon J. Gerraty /* Initialize a buffer. */
1423955d011SMarcel Moolenaar void
Buf_InitSize(Buffer * buf,size_t cap)143e2eeea75SSimon J. Gerraty Buf_InitSize(Buffer *buf, size_t cap)
1443955d011SMarcel Moolenaar {
145956e45f6SSimon J. Gerraty buf->cap = cap;
146956e45f6SSimon J. Gerraty buf->len = 0;
147956e45f6SSimon J. Gerraty buf->data = bmake_malloc(cap);
148956e45f6SSimon J. Gerraty buf->data[0] = '\0';
1493955d011SMarcel Moolenaar }
1503955d011SMarcel Moolenaar
151e2eeea75SSimon J. Gerraty void
Buf_Init(Buffer * buf)152e2eeea75SSimon J. Gerraty Buf_Init(Buffer *buf)
153e2eeea75SSimon J. Gerraty {
154e2eeea75SSimon J. Gerraty Buf_InitSize(buf, 256);
155e2eeea75SSimon J. Gerraty }
156e2eeea75SSimon J. Gerraty
15706b9b3e0SSimon J. Gerraty /*
158dba7b0efSSimon J. Gerraty * Free the data from the buffer.
159dba7b0efSSimon J. Gerraty * Leave the buffer itself in an indeterminate state.
16006b9b3e0SSimon J. Gerraty */
161dba7b0efSSimon J. Gerraty void
Buf_Done(Buffer * buf)162dba7b0efSSimon J. Gerraty Buf_Done(Buffer *buf)
1633955d011SMarcel Moolenaar {
164dba7b0efSSimon J. Gerraty free(buf->data);
1653955d011SMarcel Moolenaar
166dba7b0efSSimon J. Gerraty #ifdef CLEANUP
167956e45f6SSimon J. Gerraty buf->cap = 0;
168956e45f6SSimon J. Gerraty buf->len = 0;
169956e45f6SSimon J. Gerraty buf->data = NULL;
170dba7b0efSSimon J. Gerraty #endif
171dba7b0efSSimon J. Gerraty }
172dba7b0efSSimon J. Gerraty
173dba7b0efSSimon J. Gerraty /*
174dba7b0efSSimon J. Gerraty * Return the data from the buffer.
175dba7b0efSSimon J. Gerraty * Leave the buffer itself in an indeterminate state.
176dba7b0efSSimon J. Gerraty */
177dba7b0efSSimon J. Gerraty char *
Buf_DoneData(Buffer * buf)178dba7b0efSSimon J. Gerraty Buf_DoneData(Buffer *buf)
179dba7b0efSSimon J. Gerraty {
180dba7b0efSSimon J. Gerraty char *data = buf->data;
181dba7b0efSSimon J. Gerraty
182dba7b0efSSimon J. Gerraty #ifdef CLEANUP
183dba7b0efSSimon J. Gerraty buf->cap = 0;
184dba7b0efSSimon J. Gerraty buf->len = 0;
185dba7b0efSSimon J. Gerraty buf->data = NULL;
186dba7b0efSSimon J. Gerraty #endif
1873955d011SMarcel Moolenaar
1883955d011SMarcel Moolenaar return data;
1893955d011SMarcel Moolenaar }
190