Module Name:    src
Committed By:   rillig
Date:           Mon Mar  8 20:15:42 UTC 2021

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

Log Message:
indent: convert big macros to functions

Each of these buffers is only modified in a single file.  This makes it
unnecessary to declare the macros in the global header.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.15 -r1.16 src/usr.bin/indent/indent_globs.h
cvs rdiff -u -r1.24 -r1.25 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.13 -r1.14 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.33 src/usr.bin/indent/indent.c:1.34
--- src/usr.bin/indent/indent.c:1.33	Mon Mar  8 19:06:48 2021
+++ src/usr.bin/indent/indent.c	Mon Mar  8 20:15:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.33 2021/03/08 19:06:48 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.34 2021/03/08 20:15:42 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)indent.c	5.1
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.33 2021/03/08 19:06:48 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.34 2021/03/08 20:15:42 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -133,6 +133,36 @@ const char *simple_backup_suffix = ".BAK
 						 * files */
 char        bakfile[MAXPATHLEN] = "";
 
+static void
+check_size_code(size_t desired_size)
+{
+    if (e_code + (desired_size) >= l_code) {
+	int nsize = l_code - s_code + 400 + desired_size;
+	int code_len = e_code - s_code;
+	codebuf = (char *)realloc(codebuf, nsize);
+	if (codebuf == NULL)
+	    err(1, NULL);
+	e_code = codebuf + code_len + 1;
+	l_code = codebuf + nsize - 5;
+	s_code = codebuf + 1;
+    }
+}
+
+static void
+check_size_label(size_t desired_size)
+{
+    if (e_lab + (desired_size) >= l_lab) {
+	int nsize = l_lab - s_lab + 400 + desired_size;
+	int label_len = e_lab - s_lab;
+	labbuf = (char *)realloc(labbuf, nsize);
+	if (labbuf == NULL)
+	    err(1, NULL);
+	e_lab = labbuf + label_len + 1;
+	l_lab = labbuf + nsize - 5;
+	s_lab = labbuf + 1;
+    }
+}
+
 int
 main(int argc, char **argv)
 {
@@ -584,7 +614,7 @@ check_type:
 					 * in a line. fix it */
 		int len = e_com - s_com;
 
-		CHECK_SIZE_CODE(len + 3);
+		check_size_code(len + 3);
 		*e_code++ = ' ';
 		memcpy(e_code, s_com, len);
 		e_code += len;
@@ -603,8 +633,8 @@ check_type:
 	/*-----------------------------------------------------*\
 	|	   do switch on type of token scanned		|
 	\*-----------------------------------------------------*/
-	CHECK_SIZE_CODE(3);	/* maximum number of increments of e_code
-				 * before the next CHECK_SIZE_CODE or
+	check_size_code(3);	/* maximum number of increments of e_code
+				 * before the next check_size_code or
 				 * dump_line() is 2. After that there's the
 				 * final increment for the null character. */
 	switch (type_code) {	/* now, decide what to do with the token */
@@ -720,7 +750,7 @@ check_type:
 	    {
 		int len = e_token - s_token;
 
-		CHECK_SIZE_CODE(len);
+		check_size_code(len);
 		memcpy(e_code, token, len);
 		e_code += len;
 	    }
@@ -731,7 +761,7 @@ check_type:
 	    {
 		int len = e_token - s_token;
 
-		CHECK_SIZE_CODE(len + 1);
+		check_size_code(len + 1);
 		if (ps.want_blank)
 		    *e_code++ = ' ';
 		memcpy(e_code, token, len);
@@ -782,7 +812,7 @@ check_type:
 	    {
 		int len = e_code - s_code;
 
-		CHECK_SIZE_LAB(len + 3);
+		check_size_label(len + 3);
 		memcpy(e_lab, s_code, len);
 		e_lab += len;
 		*e_lab++ = ':';
@@ -1073,7 +1103,7 @@ check_type:
 	    {
 		int len = e_token - s_token;
 
-		CHECK_SIZE_CODE(len + 1);
+		check_size_code(len + 1);
 		if (ps.want_blank)
 		    *e_code++ = ' ';
 		memcpy(e_code, s_token, len);
@@ -1087,7 +1117,7 @@ check_type:
 	    {
 		int len = e_token - s_token;
 
-		CHECK_SIZE_CODE(len + 1);
+		check_size_code(len + 1);
 		if (ps.want_blank)
 		    *e_code++ = ' ';
 		memcpy(e_code, token, len);
@@ -1128,7 +1158,7 @@ check_type:
 		    (s_lab != e_lab) ||
 		    (s_code != e_code))
 		dump_line();
-	    CHECK_SIZE_LAB(1);
+	    check_size_label(1);
 	    *e_lab++ = '#';	/* move whole line to 'label' buffer */
 	    {
 		int         in_comment = 0;
@@ -1142,7 +1172,7 @@ check_type:
 			fill_buffer();
 		}
 		while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
-		    CHECK_SIZE_LAB(2);
+		    check_size_label(2);
 		    *e_lab = *buf_ptr++;
 		    if (buf_ptr >= buf_end)
 			fill_buffer();
@@ -1210,7 +1240,7 @@ check_type:
 		    buf_end = sc_end;
 		    sc_end = NULL;
 		}
-		CHECK_SIZE_LAB(1);
+		check_size_label(1);
 		*e_lab = '\0';	/* null terminate line */
 		ps.pcase = false;
 	    }
@@ -1343,13 +1373,13 @@ indent_declaration(int cur_dec_ind, int 
     if (tabs_to_var) {
 	int tpos;
 
-	CHECK_SIZE_CODE(cur_dec_ind / opt.tabsize);
+	check_size_code(cur_dec_ind / opt.tabsize);
 	while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_dec_ind) {
 	    *e_code++ = '\t';
 	    pos = tpos;
 	}
     }
-    CHECK_SIZE_CODE(cur_dec_ind - pos + 1);
+    check_size_code(cur_dec_ind - pos + 1);
     while (pos < cur_dec_ind) {
 	*e_code++ = ' ';
 	pos++;

Index: src/usr.bin/indent/indent_globs.h
diff -u src/usr.bin/indent/indent_globs.h:1.15 src/usr.bin/indent/indent_globs.h:1.16
--- src/usr.bin/indent/indent_globs.h:1.15	Sun Mar  7 20:40:18 2021
+++ src/usr.bin/indent/indent_globs.h	Mon Mar  8 20:15:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent_globs.h,v 1.15 2021/03/07 20:40:18 rillig Exp $	*/
+/*	$NetBSD: indent_globs.h,v 1.16 2021/03/08 20:15:42 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -54,58 +54,6 @@
 extern FILE       *input;		/* the fid for the input file */
 extern FILE       *output;		/* the output file */
 
-#define CHECK_SIZE_CODE(desired_size) \
-	if (e_code + (desired_size) >= l_code) { \
-	    int nsize = l_code-s_code + 400 + desired_size; \
-	    int code_len = e_code-s_code; \
-	    codebuf = (char *) realloc(codebuf, nsize); \
-	    if (codebuf == NULL) \
-		err(1, NULL); \
-	    e_code = codebuf + code_len + 1; \
-	    l_code = codebuf + nsize - 5; \
-	    s_code = codebuf + 1; \
-	}
-#define CHECK_SIZE_COM(desired_size) \
-	if (e_com + (desired_size) >= l_com) { \
-	    int nsize = l_com-s_com + 400 + desired_size; \
-	    int com_len = e_com - s_com; \
-	    int blank_pos; \
-	    if (last_bl != NULL) \
-		blank_pos = last_bl - combuf; \
-	    else \
-		blank_pos = -1; \
-	    combuf = (char *) realloc(combuf, nsize); \
-	    if (combuf == NULL) \
-		err(1, NULL); \
-	    e_com = combuf + com_len + 1; \
-	    if (blank_pos > 0) \
-		last_bl = combuf + blank_pos; \
-	    l_com = combuf + nsize - 5; \
-	    s_com = combuf + 1; \
-	}
-#define CHECK_SIZE_LAB(desired_size) \
-	if (e_lab + (desired_size) >= l_lab) { \
-	    int nsize = l_lab-s_lab + 400 + desired_size; \
-	    int label_len = e_lab - s_lab; \
-	    labbuf = (char *) realloc(labbuf, nsize); \
-	    if (labbuf == NULL) \
-		err(1, NULL); \
-	    e_lab = labbuf + label_len + 1; \
-	    l_lab = labbuf + nsize - 5; \
-	    s_lab = labbuf + 1; \
-	}
-#define CHECK_SIZE_TOKEN(desired_size) \
-	if (e_token + (desired_size) >= l_token) { \
-	    int nsize = l_token-s_token + 400 + desired_size; \
-	    int token_len = e_token - s_token; \
-	    tokenbuf = (char *) realloc(tokenbuf, nsize); \
-	    if (tokenbuf == NULL) \
-		err(1, NULL); \
-	    e_token = tokenbuf + token_len + 1; \
-	    l_token = tokenbuf + nsize - 5; \
-	    s_token = tokenbuf + 1; \
-	}
-
 extern char       *labbuf;		/* buffer for label */
 extern char       *s_lab;		/* start ... */
 extern char       *e_lab;		/* .. and end of stored label */

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.24 src/usr.bin/indent/lexi.c:1.25
--- src/usr.bin/indent/lexi.c:1.24	Sun Mar  7 22:11:01 2021
+++ src/usr.bin/indent/lexi.c	Mon Mar  8 20:15:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.25 2021/03/08 20:15:42 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.24 2021/03/07 22:11:01 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.25 2021/03/08 20:15:42 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -171,6 +171,21 @@ static char const *table[] = {
     [0]   = "uuiifuufiuuiiuiiiiiuiuuuuu",
 };
 
+static void
+check_size_token(size_t desired_size)
+{
+    if (e_token + (desired_size) >= l_token) {
+	int nsize = l_token - s_token + 400 + desired_size;
+	int token_len = e_token - s_token;
+	tokenbuf = (char *)realloc(tokenbuf, nsize);
+	if (tokenbuf == NULL)
+	    err(1, NULL);
+	e_token = tokenbuf + token_len + 1;
+	l_token = tokenbuf + nsize - 5;
+	s_token = tokenbuf + 1;
+    }
+}
+
 static int
 strcmp_type(const void *e1, const void *e2)
 {
@@ -277,7 +292,7 @@ lexi(struct parser_state *state)
 		    break;
 		}
 		s = table[i][s - 'A'];
-		CHECK_SIZE_TOKEN(1);
+		check_size_token(1);
 		*e_token++ = *buf_ptr++;
 		if (buf_ptr >= buf_end)
 		    fill_buffer();
@@ -297,7 +312,7 @@ lexi(struct parser_state *state)
 			} else
 			    break;
 		}
-		CHECK_SIZE_TOKEN(1);
+		check_size_token(1);
 		/* copy it over */
 		*e_token++ = *buf_ptr++;
 		if (buf_ptr >= buf_end)
@@ -420,7 +435,7 @@ lexi(struct parser_state *state)
 
     /* Scan a non-alphanumeric token */
 
-    CHECK_SIZE_TOKEN(3);		/* things like "<<=" */
+    check_size_token(3);		/* things like "<<=" */
     *e_token++ = *buf_ptr;		/* if it is only a one-character token, it is
 				 * moved here */
     *e_token = '\0';
@@ -448,7 +463,7 @@ lexi(struct parser_state *state)
 		    diag(1, "Unterminated literal");
 		    goto stop_lit;
 		}
-		CHECK_SIZE_TOKEN(2);
+		check_size_token(2);
 		*e_token = *buf_ptr++;
 		if (buf_ptr >= buf_end)
 		    fill_buffer();
@@ -598,7 +613,7 @@ stop_lit:
 	}
 	while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
 	    if (*buf_ptr == '*') {
-		CHECK_SIZE_TOKEN(1);
+		check_size_token(1);
 		*e_token++ = *buf_ptr;
 	    }
 	    if (++buf_ptr >= buf_end)
@@ -634,7 +649,7 @@ stop_lit:
 	    /*
 	     * handle ||, &&, etc, and also things as in int *****i
 	     */
-	    CHECK_SIZE_TOKEN(1);
+	    check_size_token(1);
 	    *e_token++ = *buf_ptr;
 	    if (++buf_ptr >= buf_end)
 		fill_buffer();
@@ -647,7 +662,7 @@ stop_lit:
     if (buf_ptr >= buf_end)	/* check for input buffer empty */
 	fill_buffer();
     state->last_u_d = unary_delim;
-    CHECK_SIZE_TOKEN(1);
+    check_size_token(1);
     *e_token = '\0';		/* null terminate the token */
     return lexi_end(code);
 }

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.13 src/usr.bin/indent/pr_comment.c:1.14
--- src/usr.bin/indent/pr_comment.c:1.13	Sun Mar  7 22:11:01 2021
+++ src/usr.bin/indent/pr_comment.c	Mon Mar  8 20:15:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.13 2021/03/07 22:11:01 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.14 2021/03/08 20:15:42 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)pr_comment.c
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.13 2021/03/07 22:11:01 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.14 2021/03/08 20:15:42 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -59,6 +59,28 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 
 #include "indent.h"
 
+static void
+check_size_comment(size_t desired_size, char **last_bl_ptr)
+{
+    if (e_com + (desired_size) >= l_com) {
+	int nsize = l_com - s_com + 400 + desired_size;
+	int com_len = e_com - s_com;
+	int blank_pos;
+	if (*last_bl_ptr != NULL)
+	    blank_pos = *last_bl_ptr - combuf;
+	else
+	    blank_pos = -1;
+	combuf = (char *)realloc(combuf, nsize);
+	if (combuf == NULL)
+	    err(1, NULL);
+	e_com = combuf + com_len + 1;
+	if (blank_pos > 0)
+	    *last_bl_ptr = combuf + blank_pos;
+	l_com = combuf + nsize - 5;
+	s_com = combuf + 1;
+    }
+}
+
 /*
  * NAME:
  *	pr_comment
@@ -216,7 +238,7 @@ pr_comment(void)
 				 * copied */
 	switch (*buf_ptr) {	/* this checks for various spcl cases */
 	case 014:		/* check for a form feed */
-	    CHECK_SIZE_COM(3);
+	    check_size_comment(3, &last_bl);
 	    if (!ps.box_com) {	/* in a text comment, break the line here */
 		ps.use_ff = true;
 		/* fix so dump_line uses a form feed */
@@ -245,7 +267,7 @@ pr_comment(void)
 		return;
 	    }
 	    last_bl = NULL;
-	    CHECK_SIZE_COM(4);
+	    check_size_comment(4, &last_bl);
 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
 						 * we dont ignore the newline */
 		if (s_com == e_com)
@@ -295,7 +317,7 @@ pr_comment(void)
 				 * of comment */
 	    if (++buf_ptr >= buf_end)	/* get to next char after * */
 		fill_buffer();
-	    CHECK_SIZE_COM(4);
+	    check_size_comment(4, &last_bl);
 	    if (*buf_ptr == '/') {	/* it is the end!!! */
 	end_of_comment:
 		if (++buf_ptr >= buf_end)
@@ -323,7 +345,7 @@ pr_comment(void)
 	default:		/* we have a random char */
 	    now_col = count_spaces_until(ps.com_col, s_com, e_com);
 	    do {
-		CHECK_SIZE_COM(1);
+		check_size_comment(1, &last_bl);
 		*e_com = *buf_ptr++;
 		if (buf_ptr >= buf_end)
 		    fill_buffer();
@@ -356,7 +378,7 @@ pr_comment(void)
 		/*
 		 * t_ptr will be somewhere between e_com (dump_line() reset)
 		 * and l_com. So it's safe to copy byte by byte from t_ptr
-		 * to e_com without any CHECK_SIZE_COM().
+		 * to e_com without any check_size_comment().
 		 */
 		while (*t_ptr != '\0') {
 		    if (*t_ptr == ' ' || *t_ptr == '\t')

Reply via email to