Module Name:    src
Committed By:   rillig
Date:           Sat Sep 25 22:14:21 UTC 2021

Modified Files:
        src/usr.bin/indent: indent.c indent.h lexi.c pr_comment.c

Log Message:
indent: merge duplicate code for token buffers

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.24 -r1.25 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.57 -r1.58 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.45 -r1.46 src/usr.bin/indent/pr_comment.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.80 src/usr.bin/indent/indent.c:1.81
--- src/usr.bin/indent/indent.c:1.80	Sat Sep 25 21:42:43 2021
+++ src/usr.bin/indent/indent.c	Sat Sep 25 22:14:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.80 2021/09/25 21:42:43 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.81 2021/09/25 22:14:21 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.80 2021/09/25 21:42:43 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.81 2021/09/25 22:14:21 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -137,29 +137,15 @@ char        bakfile[MAXPATHLEN] = "";
 static void
 check_size_code(size_t desired_size)
 {
-    if (code.e + desired_size < code.l)
-        return;
-
-    size_t nsize = code.l - code.s + 400 + desired_size;
-    size_t code_len = code.e - code.s;
-    code.buf = xrealloc(code.buf, nsize);
-    code.e = code.buf + code_len + 1;
-    code.l = code.buf + nsize - 5;
-    code.s = code.buf + 1;
+    if (code.e + desired_size >= code.l)
+	buf_expand(&code, desired_size);
 }
 
 static void
 check_size_label(size_t desired_size)
 {
-    if (lab.e + (desired_size) < lab.l)
-        return;
-
-    size_t nsize = lab.l - lab.s + 400 + desired_size;
-    size_t label_len = lab.e - lab.s;
-    lab.buf = xrealloc(lab.buf, nsize);
-    lab.e = lab.buf + label_len + 1;
-    lab.l = lab.buf + nsize - 5;
-    lab.s = lab.buf + 1;
+    if (lab.e + desired_size >= lab.l)
+	buf_expand(&lab, desired_size);
 }
 
 #if HAVE_CAPSICUM
@@ -376,6 +362,17 @@ buf_init(struct buffer *buf)
     buf->l = buf->buf + bufsize - 5;	/* safety margin, though unreliable */
 }
 
+void
+buf_expand(struct buffer *buf, size_t desired_size)
+{
+    size_t nsize = buf->l - buf->s + 400 + desired_size;
+    size_t code_len = buf->e - buf->s;
+    buf->buf = xrealloc(buf->buf, nsize);
+    buf->e = buf->buf + code_len + 1;
+    buf->l = buf->buf + nsize - 5;
+    buf->s = buf->buf + 1;
+}
+
 static void
 main_init_globals(void)
 {

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.24 src/usr.bin/indent/indent.h:1.25
--- src/usr.bin/indent/indent.h:1.24	Sat Sep 25 21:42:43 2021
+++ src/usr.bin/indent/indent.h	Sat Sep 25 22:14:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.24 2021/09/25 21:42:43 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.25 2021/09/25 22:14:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -70,3 +70,5 @@ void		set_profile(const char *);
 void		*xmalloc(size_t);
 void		*xrealloc(void *, size_t);
 char		*xstrdup(const char *);
+
+void		buf_expand(struct buffer *, size_t);

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.57 src/usr.bin/indent/lexi.c:1.58
--- src/usr.bin/indent/lexi.c:1.57	Sat Sep 25 20:05:55 2021
+++ src/usr.bin/indent/lexi.c	Sat Sep 25 22:14:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.57 2021/09/25 20:05:55 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.58 2021/09/25 22:14:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.57 2021/09/25 20:05:55 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.58 2021/09/25 22:14:21 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -205,15 +205,8 @@ inbuf_next(void)
 static void
 check_size_token(size_t desired_size)
 {
-    if (token.e + (desired_size) < token.l)
-        return;
-
-    size_t nsize = token.l - token.s + 400 + desired_size;
-    size_t token_len = token.e - token.s;
-    token.buf = xrealloc(token.buf, nsize);
-    token.e = token.buf + token_len + 1;
-    token.l = token.buf + nsize - 5;
-    token.s = token.buf + 1;
+    if (token.e + desired_size >= token.l)
+	buf_expand(&token, desired_size);
 }
 
 static int

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.45 src/usr.bin/indent/pr_comment.c:1.46
--- src/usr.bin/indent/pr_comment.c:1.45	Sat Sep 25 20:23:42 2021
+++ src/usr.bin/indent/pr_comment.c	Sat Sep 25 22:14:21 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.45 2021/09/25 20:23:42 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.46 2021/09/25 22:14:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,14 +43,12 @@ static char sccsid[] = "@(#)pr_comment.c
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.45 2021/09/25 20:23:42 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.46 2021/09/25 22:14:21 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
 
-#include <err.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include "indent.h"
@@ -58,15 +56,8 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 static void
 check_size_comment(size_t desired_size)
 {
-    if (com.e + (desired_size) < com.l)
-        return;
-
-    size_t nsize = com.l - com.s + 400 + desired_size;
-    size_t com_len = com.e - com.s;
-    com.buf = xrealloc(com.buf, nsize);
-    com.s = com.buf + 1;
-    com.e = com.s + com_len;
-    com.l = com.buf + nsize - 5;
+    if (com.e + desired_size >= com.l)
+	buf_expand(&com, desired_size);
 }
 
 /*

Reply via email to