Module Name: src Committed By: rillig Date: Sat Oct 30 18:58:05 UTC 2021
Modified Files: src/usr.bin/indent: indent.c Log Message: indent: move buffer functions further up No functional change. To generate a diff of this commit: cvs rdiff -u -r1.192 -r1.193 src/usr.bin/indent/indent.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.192 src/usr.bin/indent/indent.c:1.193 --- src/usr.bin/indent/indent.c:1.192 Sat Oct 30 18:47:36 2021 +++ src/usr.bin/indent/indent.c Sat Oct 30 18:58:04 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.192 2021/10/30 18:47:36 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.193 2021/10/30 18:58:04 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.192 2021/10/30 18:47:36 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.193 2021/10/30 18:58:04 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -142,6 +142,72 @@ init_capsicum(void) } #endif +static void +buf_init(struct buffer *buf) +{ + size_t size = 200; + buf->buf = xmalloc(size); + buf->l = buf->buf + size - 5 /* safety margin */; + buf->s = buf->buf + 1; /* allow accessing buf->e[-1] */ + buf->e = buf->s; + buf->buf[0] = ' '; + buf->buf[1] = '\0'; +} + +static size_t +buf_len(const struct buffer *buf) +{ + return (size_t)(buf->e - buf->s); +} + +void +buf_expand(struct buffer *buf, size_t add_size) +{ + size_t new_size = (size_t)(buf->l - buf->s) + 400 + add_size; + size_t len = buf_len(buf); + buf->buf = xrealloc(buf->buf, new_size); + buf->l = buf->buf + new_size - 5; + buf->s = buf->buf + 1; + buf->e = buf->s + len; + /* At this point, the buffer may not be null-terminated anymore. */ +} + +static void +buf_reserve(struct buffer *buf, size_t n) +{ + if (n >= (size_t)(buf->l - buf->e)) + buf_expand(buf, n); +} + +static void +buf_add_char(struct buffer *buf, char ch) +{ + buf_reserve(buf, 1); + *buf->e++ = ch; +} + +static void +buf_add_buf(struct buffer *buf, const struct buffer *add) +{ + size_t len = buf_len(add); + buf_reserve(buf, len); + memcpy(buf->e, add->s, len); + buf->e += len; +} + +static void +buf_terminate(struct buffer *buf) +{ + buf_reserve(buf, 1); + *buf->e = '\0'; +} + +static void +buf_reset(struct buffer *buf) +{ + buf->e = buf->s; +} + void diag(int level, const char *msg, ...) { @@ -430,72 +496,6 @@ search_stmt(lexer_symbol *lsym, bool *fo } static void -buf_init(struct buffer *buf) -{ - size_t size = 200; - buf->buf = xmalloc(size); - buf->l = buf->buf + size - 5 /* safety margin */; - buf->s = buf->buf + 1; /* allow accessing buf->e[-1] */ - buf->e = buf->s; - buf->buf[0] = ' '; - buf->buf[1] = '\0'; -} - -static size_t -buf_len(const struct buffer *buf) -{ - return (size_t)(buf->e - buf->s); -} - -void -buf_expand(struct buffer *buf, size_t add_size) -{ - size_t new_size = (size_t)(buf->l - buf->s) + 400 + add_size; - size_t len = buf_len(buf); - buf->buf = xrealloc(buf->buf, new_size); - buf->l = buf->buf + new_size - 5; - buf->s = buf->buf + 1; - buf->e = buf->s + len; - /* At this point, the buffer may not be null-terminated anymore. */ -} - -static void -buf_reserve(struct buffer *buf, size_t n) -{ - if (n >= (size_t)(buf->l - buf->e)) - buf_expand(buf, n); -} - -static void -buf_add_char(struct buffer *buf, char ch) -{ - buf_reserve(buf, 1); - *buf->e++ = ch; -} - -static void -buf_add_buf(struct buffer *buf, const struct buffer *add) -{ - size_t len = buf_len(add); - buf_reserve(buf, len); - memcpy(buf->e, add->s, len); - buf->e += len; -} - -static void -buf_terminate(struct buffer *buf) -{ - buf_reserve(buf, 1); - *buf->e = '\0'; -} - -static void -buf_reset(struct buffer *buf) -{ - buf->e = buf->s; -} - -static void main_init_globals(void) { inp.buf = xmalloc(10);