1*956e45f6SSimon J. Gerraty /* $NetBSD: buf.c,v 1.42 2020/10/24 20:51:49 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*956e45f6SSimon J. Gerraty /* Automatically-expanding null-terminated buffers. */ 733955d011SMarcel Moolenaar 742c3632d1SSimon J. Gerraty #include <limits.h> 753955d011SMarcel Moolenaar #include "make.h" 763955d011SMarcel Moolenaar 77*956e45f6SSimon J. Gerraty /* "@(#)buf.c 8.1 (Berkeley) 6/6/93" */ 78*956e45f6SSimon J. Gerraty MAKE_RCSID("$NetBSD: buf.c,v 1.42 2020/10/24 20:51:49 rillig Exp $"); 79*956e45f6SSimon J. Gerraty 80*956e45f6SSimon J. Gerraty /* Make space in the buffer for adding a single byte. */ 813955d011SMarcel Moolenaar void 82*956e45f6SSimon J. Gerraty Buf_Expand_1(Buffer *buf) 833955d011SMarcel Moolenaar { 84*956e45f6SSimon J. Gerraty buf->cap += buf->cap > 16 ? buf->cap : 16; 85*956e45f6SSimon J. Gerraty buf->data = bmake_realloc(buf->data, buf->cap); 863955d011SMarcel Moolenaar } 873955d011SMarcel Moolenaar 88*956e45f6SSimon J. Gerraty /* Add the bytes to the buffer. */ 893955d011SMarcel Moolenaar void 90*956e45f6SSimon J. Gerraty Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len) 913955d011SMarcel Moolenaar { 92*956e45f6SSimon J. Gerraty size_t old_len = buf->len; 93*956e45f6SSimon J. Gerraty char *end; 943955d011SMarcel Moolenaar 95*956e45f6SSimon J. Gerraty if (__predict_false(old_len + bytes_len >= buf->cap)) { 96*956e45f6SSimon J. Gerraty buf->cap += buf->cap > bytes_len + 16 ? buf->cap : bytes_len + 16; 97*956e45f6SSimon J. Gerraty buf->data = bmake_realloc(buf->data, buf->cap); 983955d011SMarcel Moolenaar } 993955d011SMarcel Moolenaar 100*956e45f6SSimon J. Gerraty end = buf->data + old_len; 101*956e45f6SSimon J. Gerraty buf->len = old_len + bytes_len; 102*956e45f6SSimon J. Gerraty memcpy(end, bytes, bytes_len); 103*956e45f6SSimon J. Gerraty end[bytes_len] = '\0'; 1043955d011SMarcel Moolenaar } 1053955d011SMarcel Moolenaar 1062c3632d1SSimon J. Gerraty /* Add the bytes between start and end to the buffer. */ 1072c3632d1SSimon J. Gerraty void 108*956e45f6SSimon J. Gerraty Buf_AddBytesBetween(Buffer *buf, const char *start, const char *end) 1093955d011SMarcel Moolenaar { 110*956e45f6SSimon J. Gerraty Buf_AddBytes(buf, start, (size_t)(end - start)); 1112c3632d1SSimon J. Gerraty } 1123955d011SMarcel Moolenaar 113*956e45f6SSimon J. Gerraty /* Add the string to the buffer. */ 1142c3632d1SSimon J. Gerraty void 115*956e45f6SSimon J. Gerraty Buf_AddStr(Buffer *buf, const char *str) 1162c3632d1SSimon J. Gerraty { 117*956e45f6SSimon J. Gerraty Buf_AddBytes(buf, str, strlen(str)); 1182c3632d1SSimon J. Gerraty } 1192c3632d1SSimon J. Gerraty 120*956e45f6SSimon J. Gerraty /* Add the number to the buffer. */ 1212c3632d1SSimon J. Gerraty void 122*956e45f6SSimon J. Gerraty Buf_AddInt(Buffer *buf, int n) 1232c3632d1SSimon J. Gerraty { 1242c3632d1SSimon J. Gerraty enum { 1252c3632d1SSimon J. Gerraty bits = sizeof(int) * CHAR_BIT, 1262c3632d1SSimon J. Gerraty max_octal_digits = (bits + 2) / 3, 1272c3632d1SSimon J. Gerraty max_decimal_digits = /* at most */ max_octal_digits, 1282c3632d1SSimon J. Gerraty max_sign_chars = 1, 129*956e45f6SSimon J. Gerraty str_size = max_sign_chars + max_decimal_digits + 1 1302c3632d1SSimon J. Gerraty }; 131*956e45f6SSimon J. Gerraty char str[str_size]; 1322c3632d1SSimon J. Gerraty 133*956e45f6SSimon J. Gerraty size_t len = (size_t)snprintf(str, sizeof str, "%d", n); 134*956e45f6SSimon J. Gerraty Buf_AddBytes(buf, str, len); 1352c3632d1SSimon J. Gerraty } 1362c3632d1SSimon J. Gerraty 1372c3632d1SSimon J. Gerraty /* Get the data (usually a string) from the buffer. 1382c3632d1SSimon J. Gerraty * The returned data is valid until the next modifying operation 1392c3632d1SSimon J. Gerraty * on the buffer. 1402c3632d1SSimon J. Gerraty * 141*956e45f6SSimon J. Gerraty * Returns the data and optionally the length of the data. */ 1422c3632d1SSimon J. Gerraty char * 143*956e45f6SSimon J. Gerraty Buf_GetAll(Buffer *buf, size_t *out_len) 1442c3632d1SSimon J. Gerraty { 145*956e45f6SSimon J. Gerraty if (out_len != NULL) 146*956e45f6SSimon J. Gerraty *out_len = buf->len; 147*956e45f6SSimon J. Gerraty return buf->data; 1483955d011SMarcel Moolenaar } 1493955d011SMarcel Moolenaar 1502c3632d1SSimon J. Gerraty /* Mark the buffer as empty, so it can be filled with data again. */ 1513955d011SMarcel Moolenaar void 152*956e45f6SSimon J. Gerraty Buf_Empty(Buffer *buf) 1533955d011SMarcel Moolenaar { 154*956e45f6SSimon J. Gerraty buf->len = 0; 155*956e45f6SSimon J. Gerraty buf->data[0] = '\0'; 1563955d011SMarcel Moolenaar } 1573955d011SMarcel Moolenaar 1582c3632d1SSimon J. Gerraty /* Initialize a buffer. 159*956e45f6SSimon J. Gerraty * If the given initial capacity is 0, a reasonable default is used. */ 1603955d011SMarcel Moolenaar void 161*956e45f6SSimon J. Gerraty Buf_Init(Buffer *buf, size_t cap) 1623955d011SMarcel Moolenaar { 163*956e45f6SSimon J. Gerraty if (cap <= 0) 164*956e45f6SSimon J. Gerraty cap = 256; 165*956e45f6SSimon J. Gerraty buf->cap = cap; 166*956e45f6SSimon J. Gerraty buf->len = 0; 167*956e45f6SSimon J. Gerraty buf->data = bmake_malloc(cap); 168*956e45f6SSimon J. Gerraty buf->data[0] = '\0'; 1693955d011SMarcel Moolenaar } 1703955d011SMarcel Moolenaar 1712c3632d1SSimon J. Gerraty /* Reset the buffer. 1722c3632d1SSimon J. Gerraty * If freeData is TRUE, the data from the buffer is freed as well. 1732c3632d1SSimon J. Gerraty * Otherwise it is kept and returned. */ 1742c3632d1SSimon J. Gerraty char * 1753955d011SMarcel Moolenaar Buf_Destroy(Buffer *buf, Boolean freeData) 1763955d011SMarcel Moolenaar { 177*956e45f6SSimon J. Gerraty char *data = buf->data; 1783955d011SMarcel Moolenaar if (freeData) { 1793955d011SMarcel Moolenaar free(data); 1803955d011SMarcel Moolenaar data = NULL; 1813955d011SMarcel Moolenaar } 1823955d011SMarcel Moolenaar 183*956e45f6SSimon J. Gerraty buf->cap = 0; 184*956e45f6SSimon J. Gerraty buf->len = 0; 185*956e45f6SSimon J. Gerraty buf->data = NULL; 1863955d011SMarcel Moolenaar 1873955d011SMarcel Moolenaar return data; 1883955d011SMarcel Moolenaar } 1893955d011SMarcel Moolenaar 1903955d011SMarcel Moolenaar #ifndef BUF_COMPACT_LIMIT 1913955d011SMarcel Moolenaar # define BUF_COMPACT_LIMIT 128 /* worthwhile saving */ 1923955d011SMarcel Moolenaar #endif 1933955d011SMarcel Moolenaar 1942c3632d1SSimon J. Gerraty /* Reset the buffer and return its data. 1952c3632d1SSimon J. Gerraty * 1962c3632d1SSimon J. Gerraty * If the buffer size is much greater than its content, 1972c3632d1SSimon J. Gerraty * a new buffer will be allocated and the old one freed. */ 1982c3632d1SSimon J. Gerraty char * 1993955d011SMarcel Moolenaar Buf_DestroyCompact(Buffer *buf) 2003955d011SMarcel Moolenaar { 2013955d011SMarcel Moolenaar #if BUF_COMPACT_LIMIT > 0 202*956e45f6SSimon J. Gerraty if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) { 2033955d011SMarcel Moolenaar /* We trust realloc to be smart */ 204*956e45f6SSimon J. Gerraty char *data = bmake_realloc(buf->data, buf->len + 1); 205*956e45f6SSimon J. Gerraty data[buf->len] = '\0'; /* XXX: unnecessary */ 2063955d011SMarcel Moolenaar Buf_Destroy(buf, FALSE); 2073955d011SMarcel Moolenaar return data; 2083955d011SMarcel Moolenaar } 2093955d011SMarcel Moolenaar #endif 2103955d011SMarcel Moolenaar return Buf_Destroy(buf, FALSE); 2113955d011SMarcel Moolenaar } 212