Module Name:    src
Committed By:   rillig
Date:           Fri Sep 24 18:14:06 UTC 2021

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

Log Message:
indent: group global variables for the comment buffer

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/indent/indent_globs.h
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/indent/io.c
cvs rdiff -u -r1.44 -r1.45 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.35 -r1.36 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.61 src/usr.bin/indent/indent.c:1.62
--- src/usr.bin/indent/indent.c:1.61	Wed Aug 25 22:26:30 2021
+++ src/usr.bin/indent/indent.c	Fri Sep 24 18:14:06 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.61 2021/08/25 22:26:30 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.62 2021/09/24 18:14:06 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.61 2021/08/25 22:26:30 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.62 2021/09/24 18:14:06 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -81,10 +81,7 @@ char       *s_code;
 char       *e_code;
 char       *l_code;
 
-char       *combuf;
-char       *s_com;
-char       *e_com;
-char       *l_com;
+struct comment_buffer com;
 
 char       *tokenbuf;
 char	   *s_token;
@@ -377,8 +374,8 @@ main_init_globals(void)
     ps.last_nl = true;		/* this is true if the last thing scanned was
 				 * a newline */
     ps.last_token = semicolon;
-    combuf = malloc(bufsize);
-    if (combuf == NULL)
+    com.buf = malloc(bufsize);
+    if (com.buf == NULL)
 	err(1, NULL);
     labbuf = malloc(bufsize);
     if (labbuf == NULL)
@@ -391,17 +388,17 @@ main_init_globals(void)
 	err(1, NULL);
     alloc_typenames();
     init_constant_tt();
-    l_com = combuf + bufsize - 5;
+    com.l = com.buf + bufsize - 5;
     l_lab = labbuf + bufsize - 5;
     l_code = codebuf + bufsize - 5;
     l_token = tokenbuf + bufsize - 5;
-    combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
+    com.buf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
 						 * comment buffers */
-    combuf[1] = codebuf[1] = labbuf[1] = tokenbuf[1] = '\0';
+    com.buf[1] = codebuf[1] = labbuf[1] = tokenbuf[1] = '\0';
     opt.else_if = 1;		/* Default else-if special processing to on */
     s_lab = e_lab = labbuf + 1;
     s_code = e_code = codebuf + 1;
-    s_com = e_com = combuf + 1;
+    com.s = com.e = com.buf + 1;
     s_token = e_token = tokenbuf + 1;
 
     in_buffer = malloc(10);
@@ -549,7 +546,7 @@ main_prepare_parsing(void)
 static void __attribute__((__noreturn__))
 process_end_of_file(void)
 {
-    if (s_lab != e_lab || s_code != e_code || s_com != e_com)
+    if (s_lab != e_lab || s_code != e_code || com.s != com.e)
 	dump_line();
 
     if (ps.tos > 1)		/* check for balanced braces */
@@ -584,18 +581,18 @@ process_comment_in_code(token_type type_
     ps.in_stmt = true;		/* turn on flag which causes an extra level of
 				 * indentation. this is turned off by a ; or
 				 * '}' */
-    if (s_com != e_com) {	/* the turkey has embedded a comment
+    if (com.s != com.e) {	/* the turkey has embedded a comment
 				 * in a line. fix it */
-	size_t len = e_com - s_com;
+	size_t len = com.e - com.s;
 
 	check_size_code(len + 3);
 	*e_code++ = ' ';
-	memcpy(e_code, s_com, len);
+	memcpy(e_code, com.s, len);
 	e_code += len;
 	*e_code++ = ' ';
 	*e_code = '\0';		/* null terminate code sect */
 	ps.want_blank = false;
-	e_com = s_com;
+	com.e = com.s;
     }
 }
 
@@ -611,7 +608,7 @@ static void
 process_newline(void)
 {
     if (ps.last_token != comma || ps.p_l_follow > 0
-	|| !opt.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
+	|| !opt.leave_comma || ps.block_init || !break_comma || com.s != com.e) {
 	dump_line();
 	ps.want_blank = false;
     }
@@ -1123,7 +1120,7 @@ process_comma(int dec_ind, int tabs_to_v
 static void
 process_preprocessing(void)
 {
-    if (s_com != e_com || s_lab != e_lab || s_code != e_code)
+    if (com.s != com.e || s_lab != e_lab || s_code != e_code)
 	dump_line();
     check_size_label(1);
     *e_lab++ = '#';	/* move whole line to 'label' buffer */

Index: src/usr.bin/indent/indent_globs.h
diff -u src/usr.bin/indent/indent_globs.h:1.21 src/usr.bin/indent/indent_globs.h:1.22
--- src/usr.bin/indent/indent_globs.h:1.21	Sat Mar 13 23:36:10 2021
+++ src/usr.bin/indent/indent_globs.h	Fri Sep 24 18:14:06 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent_globs.h,v 1.21 2021/03/13 23:36:10 rillig Exp $	*/
+/*	$NetBSD: indent_globs.h,v 1.22 2021/09/24 18:14:06 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -63,10 +63,12 @@ extern char       *s_code;		/* start ...
 extern char       *e_code;		/* .. and end of stored code */
 extern char       *l_code;		/* limit of code section */
 
-extern char       *combuf;		/* buffer for comments */
-extern char       *s_com;		/* start ... */
-extern char       *e_com;		/* ... and end of stored comments */
-extern char       *l_com;		/* limit of comment buffer */
+extern struct comment_buffer {
+    char *buf;				/* buffer for comments */
+    char *s;				/* start ... */
+    char *e;				/* ... and end of stored comments */
+    char *l;				/* limit of comment buffer */
+} com;
 
 #define token s_token
 extern char       *tokenbuf;		/* the last token scanned */

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.51 src/usr.bin/indent/io.c:1.52
--- src/usr.bin/indent/io.c:1.51	Fri Sep 24 18:00:13 2021
+++ src/usr.bin/indent/io.c	Fri Sep 24 18:14:06 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.51 2021/09/24 18:00:13 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.52 2021/09/24 18:14:06 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.51 2021/09/24 18:00:13 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.52 2021/09/24 18:14:06 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -123,7 +123,7 @@ dump_line(void)
 	ps.procname[0] = 0;
     }
 
-    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
+    if (s_code == e_code && s_lab == e_lab && com.s == com.e) {
 	if (suppress_blanklines > 0)
 	    suppress_blanklines--;
 	else {
@@ -217,9 +217,9 @@ dump_line(void)
 	    output_range(s_code, e_code);
 	    cur_col = 1 + indentation_after(cur_col - 1, s_code);
 	}
-	if (s_com != e_com) {		/* print comment, if any */
+	if (com.s != com.e) {		/* print comment, if any */
 	    int target_col = ps.com_col;
-	    char *com_st = s_com;
+	    char *com_st = com.s;
 
 	    target_col += ps.comment_delta;
 	    while (*com_st == '\t')	/* consider original indentation in
@@ -239,10 +239,10 @@ dump_line(void)
 		cur_col = 1;
 		++ps.out_lines;
 	    }
-	    while (e_com > com_st && isspace((unsigned char)e_com[-1]))
-		e_com--;
+	    while (com.e > com_st && isspace((unsigned char)com.e[-1]))
+		com.e--;
 	    (void)output_indent(cur_col - 1, target_col - 1);
-	    output_range(com_st, e_com);
+	    output_range(com_st, com.e);
 	    ps.comment_delta = ps.n_comment_delta;
 	    ++ps.com_lines;	/* count lines with comments */
 	}
@@ -260,7 +260,7 @@ dump_line(void)
     }
 
     /* keep blank lines after '//' comments */
-    if (e_com - s_com > 1 && s_com[1] == '/'
+    if (com.e - com.s > 1 && com.s[1] == '/'
 	&& s_token < e_token && isspace((unsigned char)s_token[0]))
 	output_range(s_token, e_token);
 
@@ -274,7 +274,7 @@ dump_line(void)
     ps.dumped_decl_indent = 0;
     *(e_lab = s_lab) = '\0';	/* reset buffers */
     *(e_code = s_code) = '\0';
-    *(e_com = s_com = combuf + 1) = '\0';
+    *(com.e = com.s = com.buf + 1) = '\0';
     ps.ind_level = ps.i_l_follow;
     ps.paren_level = ps.p_l_follow;
     if (ps.paren_level > 0) {
@@ -406,7 +406,7 @@ fill_buffer(void)
 		    while (*p == ' ' || *p == '\t')
 			p++;
 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && comena) {
-			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
+			if (com.s != com.e || s_lab != e_lab || s_code != e_code)
 			    dump_line();
 			if (!(inhibit_formatting = comena - 1)) {
 			    n_real_blanklines = 0;

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.44 src/usr.bin/indent/lexi.c:1.45
--- src/usr.bin/indent/lexi.c:1.44	Fri Sep 24 06:23:35 2021
+++ src/usr.bin/indent/lexi.c	Fri Sep 24 18:14:06 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.44 2021/09/24 06:23:35 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.45 2021/09/24 18:14:06 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.44 2021/09/24 06:23:35 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.45 2021/09/24 18:14:06 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -275,7 +275,7 @@ lexi_end(token_type code)
     print_buf("token", s_token, e_token);
     print_buf("label", s_lab, e_lab);
     print_buf("code", s_code, e_code);
-    print_buf("comment", s_com, e_com);
+    print_buf("comment", com.s, com.e);
     debug_printf("\n");
 
     return code;

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.35 src/usr.bin/indent/pr_comment.c:1.36
--- src/usr.bin/indent/pr_comment.c:1.35	Sun Mar 14 05:26:42 2021
+++ src/usr.bin/indent/pr_comment.c	Fri Sep 24 18:14:06 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.35 2021/03/14 05:26:42 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.36 2021/09/24 18:14:06 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.35 2021/03/14 05:26:42 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.36 2021/09/24 18:14:06 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -62,17 +62,17 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 static void
 check_size_comment(size_t desired_size)
 {
-    if (e_com + (desired_size) < l_com)
+    if (com.e + (desired_size) < com.l)
         return;
 
-    size_t nsize = l_com - s_com + 400 + desired_size;
-    size_t com_len = e_com - s_com;
-    combuf = realloc(combuf, nsize);
-    if (combuf == NULL)
+    size_t nsize = com.l - com.s + 400 + desired_size;
+    size_t com_len = com.e - com.s;
+    com.buf = realloc(com.buf, nsize);
+    if (com.buf == NULL)
 	err(1, NULL);
-    s_com = combuf + 1;
-    e_com = s_com + com_len;
-    l_com = combuf + nsize - 5;
+    com.s = com.buf + 1;
+    com.e = com.s + com_len;
+    com.l = com.buf + nsize - 5;
 }
 
 /*
@@ -98,7 +98,7 @@ process_comment(void)
 {
     int         adj_max_line_length; /* Adjusted max_line_length for comments
 				 * that spill over the right margin */
-    ssize_t last_blank;		/* index of the last blank in combuf */
+    ssize_t last_blank;		/* index of the last blank in com.buf */
     char       *t_ptr;		/* used for moving string */
     int         break_delim = opt.comment_delimiter_on_blankline;
     int         l_just_saw_decl = ps.just_saw_decl;
@@ -184,10 +184,10 @@ process_comment(void)
 	    buf_ptr++;
     }
     ps.comment_delta = 0;
-    *e_com++ = '/';
-    *e_com++ = e_token[-1];
+    *com.e++ = '/';
+    *com.e++ = e_token[-1];
     if (*buf_ptr != ' ' && !ps.box_com)
-	*e_com++ = ' ';
+	*com.e++ = ' ';
 
     /*
      * Don't put a break delimiter if this is a one-liner that won't wrap.
@@ -211,15 +211,15 @@ process_comment(void)
 	}
 
     if (break_delim) {
-	char       *t = e_com;
-	e_com = s_com + 2;
-	*e_com = 0;
+	char       *t = com.e;
+	com.e = com.s + 2;
+	*com.e = 0;
 	if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
 	    prefix_blankline_requested = 1;
 	dump_line();
-	e_com = s_com = t;
+	com.e = com.s = t;
 	if (!ps.box_com && opt.star_comment_cont)
-	    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+	    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
     }
 
     /* Start to copy the comment */
@@ -235,13 +235,13 @@ process_comment(void)
 		dump_line();
 		last_blank = -1;
 		if (!ps.box_com && opt.star_comment_cont)
-		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+		    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
 		while (*++buf_ptr == ' ' || *buf_ptr == '\t')
 		    ;
 	    } else {
 		if (++buf_ptr >= buf_end)
 		    fill_buffer();
-		*e_com++ = 014;
+		*com.e++ = 014;
 	    }
 	    break;
 
@@ -259,21 +259,21 @@ process_comment(void)
 	    check_size_comment(4);
 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
 						 * we dont ignore the newline */
-		if (s_com == e_com)
-		    *e_com++ = ' ';
-		if (!ps.box_com && e_com - s_com > 3) {
+		if (com.s == com.e)
+		    *com.e++ = ' ';
+		if (!ps.box_com && com.e - com.s > 3) {
 		    dump_line();
 		    if (opt.star_comment_cont)
-			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+			*com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
 		}
 		dump_line();
 		if (!ps.box_com && opt.star_comment_cont)
-		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+		    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
 	    } else {
 		ps.last_nl = 1;
-		if (!(e_com[-1] == ' ' || e_com[-1] == '\t'))
-		    *e_com++ = ' ';
-		last_blank = e_com - 1 - combuf;
+		if (!(com.e[-1] == ' ' || com.e[-1] == '\t'))
+		    *com.e++ = ' ';
+		last_blank = com.e - 1 - com.buf;
 	    }
 	    ++line_no;		/* keep track of input line number */
 	    if (!ps.box_com) {
@@ -303,68 +303,68 @@ process_comment(void)
 		if (++buf_ptr >= buf_end)
 		    fill_buffer();
 		if (break_delim) {
-		    if (e_com > s_com + 3)
+		    if (com.e > com.s + 3)
 			dump_line();
 		    else
-			s_com = e_com;
-		    *e_com++ = ' ';
+			com.s = com.e;
+		    *com.e++ = ' ';
 		}
-		if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
-		    *e_com++ = ' ';	/* ensure blank before end */
+		if (com.e[-1] != ' ' && com.e[-1] != '\t' && !ps.box_com)
+		    *com.e++ = ' ';	/* ensure blank before end */
 		if (e_token[-1] == '/')
-		    *e_com++ = '\n', *e_com = '\0';
+		    *com.e++ = '\n', *com.e = '\0';
 		else
-		    *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
+		    *com.e++ = '*', *com.e++ = '/', *com.e = '\0';
 		ps.just_saw_decl = l_just_saw_decl;
 		return;
 	    } else		/* handle isolated '*' */
-		*e_com++ = '*';
+		*com.e++ = '*';
 	    break;
 	default:		/* we have a random char */
 	    ;
-	    int now_len = indentation_after_range(ps.com_col - 1, s_com, e_com);
+	    int now_len = indentation_after_range(ps.com_col - 1, com.s, com.e);
 	    do {
 		check_size_comment(1);
-		*e_com = *buf_ptr++;
+		*com.e = *buf_ptr++;
 		if (buf_ptr >= buf_end)
 		    fill_buffer();
-		if (*e_com == ' ' || *e_com == '\t')
-		    last_blank = e_com - combuf; /* remember we saw a blank */
-		++e_com;
+		if (*com.e == ' ' || *com.e == '\t')
+		    last_blank = com.e - com.buf; /* remember we saw a blank */
+		++com.e;
 		now_len++;
 	    } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
 		(now_len < adj_max_line_length || last_blank == -1));
 	    ps.last_nl = false;
 	    /* XXX: signed character comparison '>' does not work for UTF-8 */
 	    if (now_len > adj_max_line_length &&
-		    !ps.box_com && e_com[-1] > ' ') {
+		    !ps.box_com && com.e[-1] > ' ') {
 		/*
 		 * the comment is too long, it must be broken up
 		 */
 		if (last_blank == -1) {
 		    dump_line();
 		    if (!ps.box_com && opt.star_comment_cont)
-			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+			*com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
 		    break;
 		}
-		*e_com = '\0';
-		e_com = combuf + last_blank;
+		*com.e = '\0';
+		com.e = com.buf + last_blank;
 		dump_line();
 		if (!ps.box_com && opt.star_comment_cont)
-		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
-		for (t_ptr = combuf + last_blank + 1;
+		    *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
+		for (t_ptr = com.buf + last_blank + 1;
 		     *t_ptr == ' ' || *t_ptr == '\t'; t_ptr++)
 		    continue;
 		last_blank = -1;
 		/*
-		 * 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_comment().
+		 * t_ptr will be somewhere between com.e (dump_line() reset)
+		 * and com.l. So it's safe to copy byte by byte from t_ptr
+		 * to com.e without any check_size_comment().
 		 */
 		while (*t_ptr != '\0') {
 		    if (*t_ptr == ' ' || *t_ptr == '\t')
-			last_blank = e_com - combuf;
-		    *e_com++ = *t_ptr++;
+			last_blank = com.e - com.buf;
+		    *com.e++ = *t_ptr++;
 		}
 	    }
 	    break;

Reply via email to