CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 21:44:43 UTC 2023

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

Log Message:
indent: inline input-related macros

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.85 -r1.86 src/usr.bin/indent/args.c
cvs rdiff -u -r1.389 -r1.390 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.206 -r1.207 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.234 -r1.235 src/usr.bin/indent/io.c
cvs rdiff -u -r1.241 -r1.242 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.172 -r1.173 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.



CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 21:44:43 UTC 2023

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

Log Message:
indent: inline input-related macros

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.85 -r1.86 src/usr.bin/indent/args.c
cvs rdiff -u -r1.389 -r1.390 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.206 -r1.207 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.234 -r1.235 src/usr.bin/indent/io.c
cvs rdiff -u -r1.241 -r1.242 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.172 -r1.173 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/args.c
diff -u src/usr.bin/indent/args.c:1.85 src/usr.bin/indent/args.c:1.86
--- src/usr.bin/indent/args.c:1.85	Thu Jun 15 09:19:06 2023
+++ src/usr.bin/indent/args.c	Sun Dec  3 21:44:42 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: args.c,v 1.85 2023/06/15 09:19:06 rillig Exp $	*/
+/*	$NetBSD: args.c,v 1.86 2023/12/03 21:44:42 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: args.c,v 1.85 2023/06/15 09:19:06 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.86 2023/12/03 21:44:42 rillig Exp $");
 
 /* Read options from profile files and from the command line. */
 
@@ -168,8 +168,8 @@ set_special_option(const char *arg, cons
 	}
 
 	if (strcmp(arg, "st") == 0) {
-		if (input == NULL)
-			input = stdin;
+		if (in.f == NULL)
+			in.f = stdin;
 		if (output == NULL)
 			output = stdout;
 		return true;

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.389 src/usr.bin/indent/indent.c:1.390
--- src/usr.bin/indent/indent.c:1.389	Sun Dec  3 21:40:44 2023
+++ src/usr.bin/indent/indent.c	Sun Dec  3 21:44:42 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.389 2023/12/03 21:40:44 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.390 2023/12/03 21:44:42 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.389 2023/12/03 21:40:44 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.390 2023/12/03 21:44:42 rillig Exp $");
 
 #include 
 #include 
@@ -156,7 +156,7 @@ diag(int level, const char *msg, ...)
 
 	va_start(ap, msg);
 	fprintf(stderr, "%s: %s:%d: ",
-	level == 0 ? "warning" : "error", in_name, token_start_line_no);
+	level == 0 ? "warning" : "error", in_name, in.token_start_line);
 	vfprintf(stderr, msg, ap);
 	fprintf(stderr, "\n");
 	va_end(ap);
@@ -228,17 +228,17 @@ copy_to_bak_file(void)
 	if (bak == NULL)
 		err(1, "%s", backup_name);
 
-	while ((n = fread(buff, 1, sizeof(buff), input)) > 0)
+	while ((n = fread(buff, 1, sizeof(buff), in.f)) > 0)
 		if (fwrite(buff, 1, n, bak) != n)
 			err(1, "%s", backup_name);
-	if (fclose(input) != 0)
+	if (fclose(in.f) != 0)
 		err(1, "%s", in_name);
 	if (fclose(bak) != 0)
 		err(1, "%s", backup_name);
 
 	/* re-open the backup file as the input file */
-	input = fopen(backup_name, "r");
-	if (input == NULL)
+	in.f = fopen(backup_name, "r");
+	if (in.f == NULL)
 		err(1, "%s", backup_name);
 	/* now the original input file will be the output */
 	output = fopen(in_name, "w");
@@ -257,9 +257,9 @@ parse_command_line(int argc, char **argv
 		if (arg[0] == '-') {
 			set_option(arg, "Command line");
 
-		} else if (input == NULL) {
+		} else if (in.f == NULL) {
 			in_name = arg;
-			if ((input = fopen(in_name, "r")) == NULL)
+			if ((in.f = fopen(in_name, "r")) == NULL)
 err(1, "%s", in_name);
 
 		} else if (output == NULL) {
@@ -273,8 +273,8 @@ parse_command_line(int argc, char **argv
 			errx(1, "too many arguments: %s", arg);
 	}
 
-	if (input == NULL) {
-		input = stdin;
+	if (in.f == NULL) {
+		in.f = stdin;
 		output = stdout;
 	} else if (output == NULL)
 		copy_to_bak_file();
@@ -300,7 +300,7 @@ set_initial_indentation(void)
 	inp_read_line();
 
 	int ind = 0;
-	for (const char *p = inp_p;; p++) {
+	for (const char *p = in.p;; p++) {
 		if (*p == ' ')
 			ind++;
 		else if (*p == '\t')
@@ -417,7 +417,7 @@ read_preprocessing_line(void)
 
 	buf_add_char(, '#');
 
-	while (inp_p[0] != '\n' || (state == COMM && !had_eof)) {
+	while (in.p[0] != '\n' || (state == COMM && !had_eof)) {
 		buf_add_char(, inp_next());
 		switch (lab.s[lab.len - 1]) {
 		case '\\':
@@ -425,9 +425,9 @@ read_preprocessing_line(void)
 buf_add_char(, inp_next());
 			break;
 		case '/':
-			if (inp_p[0] == '*' && state == PLAIN) {
+			if (in.p[0] == '*' && state == PLAIN) {
 state = COMM;
-buf_add_char(, *inp_p++);
+buf_add_char(, *in.p++);
 			}
 			break;
 		case '"':
@@ -443,9 +443,9 @@ read_preprocessing_line(void)
 state = CHR;
 			break;
 		case '*':
-			if (inp_p[0] == '/' && state == COMM) {
+			if (in.p[0] == '/' && state == COMM) {
 state = PLAIN;
-buf_add_char(, *inp_p++);
+buf_add_char(, *in.p++);
 			}
 			break;
 		}
@@ -578,7 

CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 21:40:44 UTC 2023

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

Log Message:
indent: group input-related variables into a struct

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.388 -r1.389 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.205 -r1.206 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.233 -r1.234 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 21:40:44 UTC 2023

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

Log Message:
indent: group input-related variables into a struct

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.388 -r1.389 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.205 -r1.206 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.233 -r1.234 src/usr.bin/indent/io.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.388 src/usr.bin/indent/indent.c:1.389
--- src/usr.bin/indent/indent.c:1.388	Sun Dec  3 21:03:58 2023
+++ src/usr.bin/indent/indent.c	Sun Dec  3 21:40:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.388 2023/12/03 21:03:58 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.389 2023/12/03 21:40:44 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.388 2023/12/03 21:03:58 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.389 2023/12/03 21:40:44 rillig Exp $");
 
 #include 
 #include 
@@ -87,7 +87,6 @@ static struct {
 	size_t cap;
 } ifdef;
 
-FILE *input;
 FILE *output;
 
 static const char *in_name = "Standard Input";

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.205 src/usr.bin/indent/indent.h:1.206
--- src/usr.bin/indent/indent.h:1.205	Sun Dec  3 21:03:58 2023
+++ src/usr.bin/indent/indent.h	Sun Dec  3 21:40:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.205 2023/12/03 21:03:58 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.206 2023/12/03 21:40:44 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -146,18 +146,28 @@ struct buffer {
 	size_t cap;
 };
 
-extern FILE *input;
-extern FILE *output;
-
 /*
- * The current line from the input file, used by the lexer to generate tokens.
- * To read from the line, start at inp_p and continue up to and including the
+ * The current input file, used by the lexer to generate tokens.
+ * To read from the input, start at p and continue up to and including the
  * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will
  * make the next line available, invalidating any pointers into the previous
  * line.
  */
-extern struct buffer inp;
-extern const char *inp_p;
+extern struct input_state {
+	FILE *f;
+	struct buffer line;
+	const char *p;
+	int token_start_line;
+	int token_end_line;
+} in;
+
+#define input in.f
+#define inp in.line
+#define inp_p in.p
+#define token_start_line_no in.token_start_line
+#define token_end_line_no in.token_end_line
+
+extern FILE *output;
 
 extern struct buffer token;	/* the current token to be processed, is
  * typically copied to the buffer 'code', or in
@@ -249,8 +259,6 @@ extern struct options {
 
 extern bool found_err;
 extern bool had_eof;		/* whether input is exhausted */
-extern int token_start_line_no;
-extern int token_end_line_no;
 extern enum indent_enabled {
 	indent_on,
 	indent_off,

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.233 src/usr.bin/indent/io.c:1.234
--- src/usr.bin/indent/io.c:1.233	Sun Dec  3 21:03:58 2023
+++ src/usr.bin/indent/io.c	Sun Dec  3 21:40:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.233 2023/12/03 21:03:58 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.234 2023/12/03 21:40:44 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,16 +38,15 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.233 2023/12/03 21:03:58 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.234 2023/12/03 21:40:44 rillig Exp $");
 
 #include 
 
 #include "indent.h"
 
-struct buffer inp;
-const char *inp_p;
-int token_start_line_no;
-int token_end_line_no = 1;
+struct input_state in = {
+	.token_end_line = 1,
+};
 
 struct output_state out;
 enum indent_enabled indent_enabled;



CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 21:03:58 UTC 2023

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

Log Message:
indent: use line number of the token start in diagnostics

Previously, the line number of the end of the token was used, which was
confusing in debug mode.


To generate a diff of this commit:
cvs rdiff -u -r1.387 -r1.388 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.204 -r1.205 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.232 -r1.233 src/usr.bin/indent/io.c
cvs rdiff -u -r1.240 -r1.241 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.171 -r1.172 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.387 src/usr.bin/indent/indent.c:1.388
--- src/usr.bin/indent/indent.c:1.387	Tue Jun 27 04:41:23 2023
+++ src/usr.bin/indent/indent.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.387 2023/06/27 04:41:23 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.388 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.387 2023/06/27 04:41:23 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.388 2023/12/03 21:03:58 rillig Exp $");
 
 #include 
 #include 
@@ -80,7 +80,6 @@ struct buffer com;
 
 bool found_err;
 bool had_eof;
-int line_no = 1;
 
 static struct {
 	struct parser_state *item;
@@ -158,7 +157,7 @@ diag(int level, const char *msg, ...)
 
 	va_start(ap, msg);
 	fprintf(stderr, "%s: %s:%d: ",
-	level == 0 ? "warning" : "error", in_name, line_no);
+	level == 0 ? "warning" : "error", in_name, token_start_line_no);
 	vfprintf(stderr, msg, ap);
 	fprintf(stderr, "\n");
 	va_end(ap);
@@ -580,7 +579,7 @@ process_newline(void)
 	output_line();
 
 stay_in_line:
-	line_no++;
+	token_end_line_no++;
 }
 
 static bool
@@ -1109,7 +1108,8 @@ indent(void)
 		lexer_symbol lsym = lexi();
 
 		debug_blank_line();
-		debug_printf("line %d: %s", line_no, lsym_name[lsym]);
+		debug_printf("line %d: %s",
+		token_start_line_no, lsym_name[lsym]);
 		debug_print_buf("token", );
 		debug_buffers();
 		debug_blank_line();

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.204 src/usr.bin/indent/indent.h:1.205
--- src/usr.bin/indent/indent.h:1.204	Mon Jun 26 20:03:09 2023
+++ src/usr.bin/indent/indent.h	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.204 2023/06/26 20:03:09 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.205 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -249,7 +249,8 @@ extern struct options {
 
 extern bool found_err;
 extern bool had_eof;		/* whether input is exhausted */
-extern int line_no;		/* the current input line number */
+extern int token_start_line_no;
+extern int token_end_line_no;
 extern enum indent_enabled {
 	indent_on,
 	indent_off,

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.232 src/usr.bin/indent/io.c:1.233
--- src/usr.bin/indent/io.c:1.232	Tue Jun 27 04:41:23 2023
+++ src/usr.bin/indent/io.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.232 2023/06/27 04:41:23 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.233 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.232 2023/06/27 04:41:23 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.233 2023/12/03 21:03:58 rillig Exp $");
 
 #include 
 
@@ -46,6 +46,8 @@ __RCSID("$NetBSD: io.c,v 1.232 2023/06/2
 
 struct buffer inp;
 const char *inp_p;
+int token_start_line_no;
+int token_end_line_no = 1;
 
 struct output_state out;
 enum indent_enabled indent_enabled;

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.240 src/usr.bin/indent/lexi.c:1.241
--- src/usr.bin/indent/lexi.c:1.240	Sun Dec  3 20:42:31 2023
+++ src/usr.bin/indent/lexi.c	Sun Dec  3 21:03:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.240 2023/12/03 20:42:31 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.241 2023/12/03 21:03:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.240 2023/12/03 20:42:31 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.241 2023/12/03 21:03:58 rillig Exp $");
 
 #include 
 #include 
@@ -188,7 +188,7 @@ skip_line_continuation(void)
 	if (inp_p[0] == '\\' && inp_p[1] == '\n') {
 		inp_p++;
 		inp_skip();
-		line_no++;
+		token_end_line_no++;
 		return true;
 	}
 	return false;
@@ -245,7 +245,7 @@ lex_char_or_string(void)
 
 		if (token.s[token.len - 1] == '\\') {
 			if (*inp_p == '\n')
-line_no++;
+token_end_line_no++;
 			token_add_char(inp_next());
 		}
 	}
@@ -524,7 +524,7 @@ lex_asterisk_unary(void)
 		if (*inp_p == '*')
 			token_add_char('*');
 		if (*inp_p == '\n')
-			line_no++;
+			

CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 21:03:58 UTC 2023

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

Log Message:
indent: use line number of the token start in diagnostics

Previously, the line number of the end of the token was used, which was
confusing in debug mode.


To generate a diff of this commit:
cvs rdiff -u -r1.387 -r1.388 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.204 -r1.205 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.232 -r1.233 src/usr.bin/indent/io.c
cvs rdiff -u -r1.240 -r1.241 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.171 -r1.172 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.



CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 20:42:32 UTC 2023

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

Log Message:
indent: fix line number counting in function definition

In a function definition that is split on two lines, if the first line
ends with a '*', the following line break didn't include the line
number.


To generate a diff of this commit:
cvs rdiff -u -r1.239 -r1.240 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-12-03 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Dec  3 20:42:32 UTC 2023

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

Log Message:
indent: fix line number counting in function definition

In a function definition that is split on two lines, if the first line
ends with a '*', the following line break didn't include the line
number.


To generate a diff of this commit:
cvs rdiff -u -r1.239 -r1.240 src/usr.bin/indent/lexi.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/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.239 src/usr.bin/indent/lexi.c:1.240
--- src/usr.bin/indent/lexi.c:1.239	Mon Jun 26 20:23:40 2023
+++ src/usr.bin/indent/lexi.c	Sun Dec  3 20:42:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.239 2023/06/26 20:23:40 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.240 2023/12/03 20:42:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.239 2023/06/26 20:23:40 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.240 2023/12/03 20:42:31 rillig Exp $");
 
 #include 
 #include 
@@ -523,6 +523,8 @@ lex_asterisk_unary(void)
 	while (*inp_p == '*' || ch_isspace(*inp_p)) {
 		if (*inp_p == '*')
 			token_add_char('*');
+		if (*inp_p == '\n')
+			line_no++;
 		inp_skip();
 	}
 



CVS commit: src/usr.bin/indent

2023-06-26 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun 26 10:23:59 UTC 2023

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

Log Message:
indent: clean up indentation


To generate a diff of this commit:
cvs rdiff -u -r1.236 -r1.237 src/usr.bin/indent/lexi.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/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.236 src/usr.bin/indent/lexi.c:1.237
--- src/usr.bin/indent/lexi.c:1.236	Sun Jun 25 19:35:45 2023
+++ src/usr.bin/indent/lexi.c	Mon Jun 26 10:23:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.237 2023/06/26 10:23:59 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.237 2023/06/26 10:23:59 rillig Exp $");
 
 #include 
 #include 
@@ -207,8 +207,8 @@ lex_number(void)
 
 		unsigned char row = lex_number_row[ch];
 		if (lex_number_state[row][s - 'A'] == ' ') {
-		// lex_number_state[0][s - 'A'] now indicates the type:
-		// f = floating, i = integer, u = unknown
+			// lex_number_state[0][s - 'A'] now indicates the type:
+			// f = floating, i = integer, u = unknown
 			return;
 		}
 
@@ -406,7 +406,7 @@ lexi_alnum(void)
 	if (ps.prev_lsym == lsym_tag && ps.paren.len == 0)
 		return lsym_type;
 
-	token_add_char('\0');		// Terminate in non-debug mode as well.
+	token_add_char('\0');	// Terminate in non-debug mode as well.
 	token.len--;
 	const struct keyword *kw = bsearch(token.s, keywords,
 	array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);



CVS commit: src/usr.bin/indent

2023-06-26 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun 26 10:23:59 UTC 2023

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

Log Message:
indent: clean up indentation


To generate a diff of this commit:
cvs rdiff -u -r1.236 -r1.237 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-25 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 25 19:35:45 UTC 2023

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

Log Message:
indent: move cast detection from the lexer to the main processor

It is not the job of the lexer to modify the parser state.


To generate a diff of this commit:
cvs rdiff -u -r1.383 -r1.384 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.235 -r1.236 src/usr.bin/indent/lexi.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.383 src/usr.bin/indent/indent.c:1.384
--- src/usr.bin/indent/indent.c:1.383	Sun Jun 25 19:19:42 2023
+++ src/usr.bin/indent/indent.c	Sun Jun 25 19:35:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $");
 
 #include 
 #include 
@@ -958,6 +958,10 @@ process_type_outside_parentheses(void)
 static void
 process_word(lexer_symbol lsym)
 {
+	if (lsym == lsym_type	/* in parentheses */
+	&& ps.paren.item[ps.paren.len - 1].cast == cast_unknown)
+		ps.paren.item[ps.paren.len - 1].cast = cast_maybe;
+
 	if (ps.in_decl) {
 		if (lsym == lsym_funcname) {
 			ps.in_decl = false;

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.235 src/usr.bin/indent/lexi.c:1.236
--- src/usr.bin/indent/lexi.c:1.235	Sun Jun 25 19:29:57 2023
+++ src/usr.bin/indent/lexi.c	Sun Jun 25 19:35:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.235 2023/06/25 19:29:57 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.235 2023/06/25 19:29:57 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $");
 
 #include 
 #include 
@@ -296,6 +296,8 @@ bsearch_typenames(const char *key)
 static bool
 is_typename(void)
 {
+	if (ps.prev_lsym == lsym_tag)
+		return true;
 	if (opt.auto_typedefs &&
 	token.len >= 2 && memcmp(token.s + token.len - 2, "_t", 2) == 0)
 		return true;
@@ -335,6 +337,7 @@ cmp_keyword_by_name(const void *key, con
 static bool
 probably_function_definition(const char *p)
 {
+	// TODO: Don't look at characters in comments, see lsym_funcname.c.
 	int paren_level = 0;
 	for (; *p != '\n'; p++) {
 		if (*p == '(')
@@ -409,29 +412,20 @@ lexi_alnum(void)
 	array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
 	lexer_symbol lsym = lsym_word;
 	if (kw != NULL) {
-		if (kw->lsym == lsym_type)
-			lsym = lsym_type;
+		lsym = kw->lsym;
 		ps.next_unary = true;
-		if (kw->lsym == lsym_tag || kw->lsym == lsym_type)
+		if (lsym == lsym_tag || lsym == lsym_type)
 			goto found_typename;
-		return kw->lsym;
+		return lsym;
 	}
 
 	if (is_typename()) {
 		lsym = lsym_type;
 		ps.next_unary = true;
 found_typename:
-		if (ps.paren.len > 0) {
-			/* inside parentheses: cast, param list, offsetof or
-			 * sizeof */
-			struct paren_level *paren_level =
-			ps.paren.item + ps.paren.len - 1;
-			if (paren_level->cast == cast_unknown)
-paren_level->cast = cast_maybe;
-		}
 		if (ps.prev_lsym != lsym_period
 		&& ps.prev_lsym != lsym_unary_op) {
-			if (kw != NULL && kw->lsym == lsym_tag)
+			if (lsym == lsym_tag)
 return lsym_tag;
 			if (ps.paren.len == 0)
 return lsym_type;



CVS commit: src/usr.bin/indent

2023-06-25 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 25 19:35:45 UTC 2023

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

Log Message:
indent: move cast detection from the lexer to the main processor

It is not the job of the lexer to modify the parser state.


To generate a diff of this commit:
cvs rdiff -u -r1.383 -r1.384 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.235 -r1.236 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-25 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 25 18:41:03 UTC 2023

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

Log Message:
indent: don't use strspn on inp_p, as it is not null-terminated

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.232 -r1.233 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-25 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 25 18:41:03 UTC 2023

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

Log Message:
indent: don't use strspn on inp_p, as it is not null-terminated

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.232 -r1.233 src/usr.bin/indent/lexi.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/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.232 src/usr.bin/indent/lexi.c:1.233
--- src/usr.bin/indent/lexi.c:1.232	Sat Jun 17 23:03:20 2023
+++ src/usr.bin/indent/lexi.c	Sun Jun 25 18:41:03 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.232 2023/06/17 23:03:20 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.233 2023/06/25 18:41:03 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.232 2023/06/17 23:03:20 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.233 2023/06/25 18:41:03 rillig Exp $");
 
 #include 
 #include 
@@ -459,7 +459,10 @@ found_typename:
 static bool
 is_asterisk_pointer(void)
 {
-	if (inp_p[strspn(inp_p, "* \t")] == ')')
+	const char *p = inp_p;
+	while (*p == '*' || ch_isblank(*p))
+		p++;
+	if (*p == ')')
 		return true;
 	if (ps.next_unary || ps.in_func_def_params)
 		return true;



CVS commit: src/usr.bin/indent

2023-06-23 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun 23 20:43:21 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h

Log Message:
indent: properly store parser state in debug mode

The stacks in the parser state are allocated now and need to be copied
individually.

The test whether two paren stacks are equal was broken since 2023-06-14
14:11:28.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.381 -r1.382 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.202 -r1.203 src/usr.bin/indent/indent.h

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/debug.c
diff -u src/usr.bin/indent/debug.c:1.67 src/usr.bin/indent/debug.c:1.68
--- src/usr.bin/indent/debug.c:1.67	Sat Jun 17 22:28:49 2023
+++ src/usr.bin/indent/debug.c	Fri Jun 23 20:43:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.67 2023/06/17 22:28:49 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.67 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $");
 
 #include 
 #include 
@@ -263,8 +263,8 @@ paren_stack_equal(const struct paren_sta
 	for (size_t i = 0, n = a->len; i < n; i++)
 		if (a->item[i].indent != b->item[i].indent
 		|| a->item[i].cast != b->item[i].cast)
-			return true;
-	return false;
+			return false;
+	return true;
 }
 
 static void
@@ -375,7 +375,8 @@ debug_parser_state(void)
 	state.heading = NULL;
 	debug_blank_line();
 
-	state.prev_ps = ps;
+	parser_state_free(_ps);
+	parser_state_back_up(_ps);
 	state.ps_first = false;
 }
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.381 src/usr.bin/indent/indent.c:1.382
--- src/usr.bin/indent/indent.c:1.381	Sun Jun 18 07:10:24 2023
+++ src/usr.bin/indent/indent.c	Fri Jun 23 20:43:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.381 2023/06/18 07:10:24 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.382 2023/06/23 20:43:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.381 2023/06/18 07:10:24 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.382 2023/06/23 20:43:21 rillig Exp $");
 
 #include 
 #include 
@@ -464,7 +464,7 @@ dup_mem(const void *src, size_t size)
 #define copy_array(dst, src, len) \
 memcpy((dst), (src), sizeof((dst)[0]) * (len))
 
-static void
+static_unless_debug void
 parser_state_back_up(struct parser_state *dst)
 {
 	*dst = ps;
@@ -496,7 +496,7 @@ parser_state_restore(const struct parser
 	copy_array(ps.psyms.ind_level, src->psyms.ind_level, src->psyms.len);
 }
 
-static void
+static_unless_debug void
 parser_state_free(struct parser_state *pst)
 {
 	free(pst->paren.item);

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.202 src/usr.bin/indent/indent.h:1.203
--- src/usr.bin/indent/indent.h:1.202	Fri Jun 16 23:51:32 2023
+++ src/usr.bin/indent/indent.h	Fri Jun 23 20:43:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.202 2023/06/16 23:51:32 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.203 2023/06/23 20:43:21 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -442,10 +442,13 @@ void debug_parser_state(void);
 void debug_psyms_stack(const char *);
 void debug_print_buf(const char *, const struct buffer *);
 void debug_buffers(void);
+void parser_state_back_up(struct parser_state *);
+void parser_state_free(struct parser_state *);
 extern const char *const lsym_name[];
 extern const char *const psym_name[];
 extern const char *const paren_level_cast_name[];
 extern const char *const line_kind_name[];
+#define static_unless_debug /* nothing */
 #else
 #define debug_noop() do { } while (false)
 #define	debug_printf(fmt, ...) debug_noop()
@@ -456,6 +459,7 @@ extern const char *const line_kind_name[
 #define	debug_psyms_stack(situation) debug_noop()
 #define debug_print_buf(name, buf) debug_noop()
 #define	debug_buffers() debug_noop()
+#define static_unless_debug static
 #endif
 
 void register_typename(const char *);



CVS commit: src/usr.bin/indent

2023-06-23 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun 23 20:43:21 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h

Log Message:
indent: properly store parser state in debug mode

The stacks in the parser state are allocated now and need to be copied
individually.

The test whether two paren stacks are equal was broken since 2023-06-14
14:11:28.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.381 -r1.382 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.202 -r1.203 src/usr.bin/indent/indent.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-18 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 18 06:56:33 UTC 2023

Modified Files:
src/usr.bin/indent: parse.c

Log Message:
indent: untangle code for handling the statement indentation

The expression 'psyms.level-- - 2' did too much in a single line, so
extract the '--' to a separate statement, to highlight the symmetry
between the 'sym' and 'ind_level' code.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/usr.bin/indent/parse.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/parse.c
diff -u src/usr.bin/indent/parse.c:1.78 src/usr.bin/indent/parse.c:1.79
--- src/usr.bin/indent/parse.c:1.78	Sat Jun 17 22:28:49 2023
+++ src/usr.bin/indent/parse.c	Sun Jun 18 06:56:32 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.78 2023/06/17 22:28:49 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.79 2023/06/18 06:56:32 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: parse.c,v 1.78 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.79 2023/06/18 06:56:32 rillig Exp $");
 
 #include 
 
@@ -51,28 +51,30 @@ __RCSID("$NetBSD: parse.c,v 1.78 2023/06
 static bool
 psyms_reduce_stmt(void)
 {
-	struct psym_stack *psyms = 
-	switch (psyms->sym[psyms->len - 2]) {
+	switch (ps.psyms.sym[ps.psyms.len - 2]) {
 
 	case psym_stmt:
-		psyms->sym[psyms->len-- - 2] = psym_stmt;
+		ps.psyms.len--;
+		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
 		return true;
 
 	case psym_do:
-		psyms->sym[psyms->len-- - 2] = psym_do_stmt;
-		ps.ind_level_follow = psyms->ind_level[psyms->len - 1];
+		ps.psyms.len--;
+		ps.psyms.sym[ps.psyms.len - 1] = psym_do_stmt;
+		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
 		return true;
 
 	case psym_if_expr:
-		psyms->sym[psyms->len-- - 2] = psym_if_expr_stmt;
-		size_t i = psyms->len - 2;
-		while (psyms->sym[i] != psym_stmt &&
-		psyms->sym[i] != psym_lbrace_block)
-			i--;
-		ps.ind_level_follow = psyms->ind_level[i];
+		ps.psyms.len--;
+		ps.psyms.sym[ps.psyms.len - 1] = psym_if_expr_stmt;
 		/* For the time being, assume that there is no 'else' on this
 		 * 'if', and set the indentation level accordingly. If an
 		 * 'else' is scanned, it will be fixed up later. */
+		size_t i = ps.psyms.len - 2;
+		while (ps.psyms.sym[i] != psym_stmt
+		&& ps.psyms.sym[i] != psym_lbrace_block)
+			i--;
+		ps.ind_level_follow = ps.psyms.ind_level[i];
 		return true;
 
 	case psym_switch_expr:
@@ -80,8 +82,9 @@ psyms_reduce_stmt(void)
 	case psym_if_expr_stmt_else:
 	case psym_for_exprs:
 	case psym_while_expr:
-		psyms->sym[psyms->len-- - 2] = psym_stmt;
-		ps.ind_level_follow = psyms->ind_level[psyms->len - 1];
+		ps.psyms.len--;
+		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
+		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
 		return true;
 
 	default:
@@ -90,7 +93,7 @@ psyms_reduce_stmt(void)
 }
 
 static int
-decl_level(void)
+left_justify_decl_level(void)
 {
 	int level = 0;
 	for (size_t i = ps.psyms.len - 2; i > 0; i--)
@@ -122,14 +125,13 @@ ps_push(parser_symbol psym, bool follow)
 static void
 psyms_reduce(void)
 {
-	struct psym_stack *psyms = 
 again:
-	if (psyms->len >= 2 && psyms->sym[psyms->len - 1] == psym_stmt
+	if (ps.psyms.len >= 2 && ps.psyms.sym[ps.psyms.len - 1] == psym_stmt
 	&& psyms_reduce_stmt())
 		goto again;
-	if (psyms->sym[psyms->len - 1] == psym_while_expr &&
-	psyms->sym[psyms->len - 2] == psym_do_stmt) {
-		psyms->len -= 2;
+	if (ps.psyms.sym[ps.psyms.len - 1] == psym_while_expr &&
+	ps.psyms.sym[ps.psyms.len - 2] == psym_do_stmt) {
+		ps.psyms.len -= 2;
 		goto again;
 	}
 }
@@ -153,10 +155,9 @@ parse(parser_symbol psym)
 	debug_blank_line();
 	debug_println("parse token: %s", psym_name[psym]);
 
-	struct psym_stack *psyms = 
 	if (psym != psym_else) {
-		while (psyms->sym[psyms->len - 1] == psym_if_expr_stmt) {
-			psyms->sym[psyms->len - 1] = psym_stmt;
+		while (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt) {
+			ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
 			psyms_reduce();
 		}
 	}
@@ -168,8 +169,8 @@ parse(parser_symbol psym)
 	case psym_lbrace_union:
 	case psym_lbrace_enum:
 		ps.break_after_comma = false;
-		if (psyms->sym[psyms->len - 1] == psym_decl
-		|| psyms->sym[psyms->len - 1] == psym_stmt)
+		if (ps.psyms.sym[ps.psyms.len - 1] == psym_decl
+		|| ps.psyms.sym[ps.psyms.len - 1] == psym_stmt)
 			ps.ind_level_follow++;
 		else if (code.len == 0) {
 			/* It is part of a while, for, etc. */
@@ -177,7 +178,7 @@ parse(parser_symbol psym)
 
 			/* for a switch, brace should be two levels out from
 			 * the code */
-			if (psyms->sym[psyms->len - 1] == psym_switch_expr
+			if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr
 			&& opt.case_indent >= 1.0F)
 ps.ind_level--;
 		}
@@ -188,25 +189,28 @@ parse(parser_symbol psym)
 
 	case psym_rbrace:
 		/* 

CVS commit: src/usr.bin/indent

2023-06-18 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 18 06:56:33 UTC 2023

Modified Files:
src/usr.bin/indent: parse.c

Log Message:
indent: untangle code for handling the statement indentation

The expression 'psyms.level-- - 2' did too much in a single line, so
extract the '--' to a separate statement, to highlight the symmetry
between the 'sym' and 'ind_level' code.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/usr.bin/indent/parse.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-17 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 17 23:03:20 UTC 2023

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

Log Message:
indent: clean up

Extract duplicate code for handling line continuations.

Prevent theoretic undefined behavior in strspn, as inp.s is not
null-terminated.

Remove adding extra space characters when processing comments, as these
are not necessary to force a line of output.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.228 -r1.229 src/usr.bin/indent/io.c
cvs rdiff -u -r1.231 -r1.232 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.167 -r1.168 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/io.c
diff -u src/usr.bin/indent/io.c:1.228 src/usr.bin/indent/io.c:1.229
--- src/usr.bin/indent/io.c:1.228	Sat Jun 17 22:28:49 2023
+++ src/usr.bin/indent/io.c	Sat Jun 17 23:03:20 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.229 2023/06/17 23:03:20 rillig Exp $");
 
 #include 
 
@@ -341,8 +341,7 @@ output_indented_line(void)
 	if (lab.len == 0 && code.len == 0 && com.len == 0)
 		out.line_kind = lk_blank;
 
-	if (want_blank_line() && newlines < 2
-	&& out.line_kind != lk_blank)
+	if (want_blank_line() && newlines < 2 && out.line_kind != lk_blank)
 		add_buffered_newline();
 
 	/* This kludge aligns function definitions correctly. */

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.231 src/usr.bin/indent/lexi.c:1.232
--- src/usr.bin/indent/lexi.c:1.231	Sat Jun 17 22:28:49 2023
+++ src/usr.bin/indent/lexi.c	Sat Jun 17 23:03:20 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.231 2023/06/17 22:28:49 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.232 2023/06/17 23:03:20 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.231 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.232 2023/06/17 23:03:20 rillig Exp $");
 
 #include 
 #include 
@@ -182,17 +182,25 @@ token_add_char(char ch)
 	buf_add_char(, ch);
 }
 
+static bool
+skip_line_continuation(void)
+{
+	if (inp_p[0] == '\\' && inp_p[1] == '\n') {
+		inp_p++;
+		inp_skip();
+		line_no++;
+		return true;
+	}
+	return false;
+}
+
 static void
 lex_number(void)
 {
 	for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
 		unsigned char ch = (unsigned char)*inp_p;
-		if (ch == '\\' && inp_p[1] == '\n') {
-			inp_p++;
-			inp_skip();
-			line_no++;
+		if (skip_line_continuation())
 			continue;
-		}
 		if (ch >= array_length(lex_number_row)
 		|| lex_number_row[ch] == 0)
 			break;
@@ -213,13 +221,11 @@ static void
 lex_word(void)
 {
 	for (;;) {
-		if (is_identifier_part(inp_p[0]))
+		if (is_identifier_part(*inp_p))
 			token_add_char(*inp_p++);
-		else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
-			inp_p++;
-			inp_skip();
-			line_no++;
-		} else
+		else if (skip_line_continuation())
+			continue;
+		else
 			return;
 	}
 }
@@ -533,13 +539,11 @@ lexi(void)
 	buf_clear();
 
 	for (;;) {
-		if (ch_isblank(inp_p[0]))
+		if (ch_isblank(*inp_p))
 			inp_p++;
-		else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
-			inp_p++;
-			inp_skip();
-			line_no++;
-		} else
+		else if (skip_line_continuation())
+			continue;
+		else
 			break;
 	}
 

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.167 src/usr.bin/indent/pr_comment.c:1.168
--- src/usr.bin/indent/pr_comment.c:1.167	Sat Jun 17 22:28:49 2023
+++ src/usr.bin/indent/pr_comment.c	Sat Jun 17 23:03:20 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.167 2023/06/17 22:28:49 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.168 2023/06/17 23:03:20 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.167 2023/06/17 22:28:49 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.168 2023/06/17 23:03:20 rillig Exp $");
 
 #include 
 
@@ -75,6 +75,15 @@ fits_in_one_line(int max_line_length)
 	return false;
 }
 
+static bool
+is_block_comment(void)
+{
+	const char *p = inp_p;
+	while (*p == '*')
+		p++;
+	return *p == '\n';
+}
+
 static void
 analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
 {
@@ -91,7 +100,7 @@ analyze_comment(bool *p_may_wrap, bool *
 		token.s[token.len - 1] == '/' ||
 		(inp_p[0] == '\n' && !opt.format_block_comments))
 			may_wrap = false;
-		if (code.len == 0 && inp_p[strspn(inp_p, "*")] == '\n')
+		if (is_block_comment())
 			out.line_kind = lk_block_comment;
 
 		if (com.len > 0)
@@ -143,8 +152,7 @@ static void
 copy_comment_start(bool may_wrap, bool 

CVS commit: src/usr.bin/indent

2023-06-17 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 17 23:03:20 UTC 2023

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

Log Message:
indent: clean up

Extract duplicate code for handling line continuations.

Prevent theoretic undefined behavior in strspn, as inp.s is not
null-terminated.

Remove adding extra space characters when processing comments, as these
are not necessary to force a line of output.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.228 -r1.229 src/usr.bin/indent/io.c
cvs rdiff -u -r1.231 -r1.232 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.167 -r1.168 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.



CVS commit: src/usr.bin/indent

2023-06-17 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 17 22:28:49 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c io.c lexi.c parse.c pr_comment.c

Log Message:
indent: miscellaneous cleanups

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.379 -r1.380 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.227 -r1.228 src/usr.bin/indent/io.c
cvs rdiff -u -r1.230 -r1.231 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.77 -r1.78 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.166 -r1.167 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/debug.c
diff -u src/usr.bin/indent/debug.c:1.66 src/usr.bin/indent/debug.c:1.67
--- src/usr.bin/indent/debug.c:1.66	Fri Jun 16 23:51:32 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 17 22:28:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.66 2023/06/16 23:51:32 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.67 2023/06/17 22:28:49 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.66 2023/06/16 23:51:32 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.67 2023/06/17 22:28:49 rillig Exp $");
 
 #include 
 #include 
@@ -384,7 +384,7 @@ debug_psyms_stack(const char *situation)
 {
 	debug_printf("parse stack %s:", situation);
 	const struct psym_stack *psyms = 
-	for (size_t i = 0; i < psyms->len; ++i)
+	for (size_t i = 0; i < psyms->len; i++)
 		debug_printf(" %d %s",
 		psyms->ind_level[i], psym_name[psyms->sym[i]]);
 	debug_println("");

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.379 src/usr.bin/indent/indent.c:1.380
--- src/usr.bin/indent/indent.c:1.379	Fri Jun 16 23:51:32 2023
+++ src/usr.bin/indent/indent.c	Sat Jun 17 22:28:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.379 2023/06/16 23:51:32 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.380 2023/06/17 22:28:49 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.379 2023/06/16 23:51:32 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.380 2023/06/17 22:28:49 rillig Exp $");
 
 #include 
 #include 
@@ -177,9 +177,9 @@ ind_add(int ind, const char *s, size_t l
 		else if (*p == '\t')
 			ind = next_tab(ind);
 		else if (*p == '\b')
-			--ind;
+			ind--;
 		else
-			++ind;
+			ind++;
 	}
 	return ind;
 }
@@ -201,7 +201,7 @@ load_profiles(int argc, char **argv)
 {
 	const char *profile_name = NULL;
 
-	for (int i = 1; i < argc; ++i) {
+	for (int i = 1; i < argc; i++) {
 		const char *arg = argv[i];
 
 		if (strcmp(arg, "-npro") == 0)
@@ -255,7 +255,7 @@ copy_to_bak_file(void)
 static void
 parse_command_line(int argc, char **argv)
 {
-	for (int i = 1; i < argc; ++i) {
+	for (int i = 1; i < argc; i++) {
 		const char *arg = argv[i];
 
 		if (arg[0] == '-') {
@@ -566,7 +566,7 @@ process_newline(void)
 	output_line();
 
 stay_in_line:
-	++line_no;
+	line_no++;
 }
 
 static bool

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.227 src/usr.bin/indent/io.c:1.228
--- src/usr.bin/indent/io.c:1.227	Fri Jun 16 11:48:32 2023
+++ src/usr.bin/indent/io.c	Sat Jun 17 22:28:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.227 2023/06/16 11:48:32 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.227 2023/06/16 11:48:32 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.228 2023/06/17 22:28:49 rillig Exp $");
 
 #include 
 
@@ -331,6 +331,10 @@ output_comment(void)
 	write_range(p, com.len - (size_t)(p - com.s));
 }
 
+/*
+ * Write a line of formatted source to the output file. The line consists of
+ * the label, the code and the comment.
+ */
 static void
 output_indented_line(void)
 {
@@ -382,29 +386,9 @@ is_stmt_cont(void)
 	&& ps.init_level == 0;
 }
 
-/*
- * Write a line of formatted source to the output file. The line consists of
- * the label, the code and the comment.
- */
-void
-output_line(void)
+static void
+prepare_next_line(void)
 {
-	debug_blank_line();
-	debug_printf("%s", __func__);
-	debug_buffers();
-
-	if (indent_enabled == indent_on)
-		output_indented_line();
-	else if (indent_enabled == indent_last_off_line) {
-		indent_enabled = indent_on;
-		write_range(out.indent_off_text.s, out.indent_off_text.len);
-		buf_clear(_off_text);
-	}
-
-	buf_clear();
-	buf_clear();
-	buf_clear();
-
 	ps.line_has_decl = ps.in_decl;
 	ps.line_has_func_def = false;
 	ps.line_is_stmt_cont = is_stmt_cont();
@@ -427,6 +411,28 @@ output_line(void)
 }
 
 void
+output_line(void)
+{
+	debug_blank_line();
+	debug_printf("%s", __func__);
+	debug_buffers();
+
+	if (indent_enabled == indent_on)
+		output_indented_line();
+	else if (indent_enabled == indent_last_off_line) {
+		indent_enabled 

CVS commit: src/usr.bin/indent

2023-06-17 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 17 22:28:49 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c io.c lexi.c parse.c pr_comment.c

Log Message:
indent: miscellaneous cleanups

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.379 -r1.380 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.227 -r1.228 src/usr.bin/indent/io.c
cvs rdiff -u -r1.230 -r1.231 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.77 -r1.78 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.166 -r1.167 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.



CVS commit: src/usr.bin/indent

2023-06-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun 16 23:17:22 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: add debug output for typedef declarations


To generate a diff of this commit:
cvs rdiff -u -r1.64 -r1.65 src/usr.bin/indent/debug.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun 16 23:17:22 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: add debug output for typedef declarations


To generate a diff of this commit:
cvs rdiff -u -r1.64 -r1.65 src/usr.bin/indent/debug.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.64 src/usr.bin/indent/debug.c:1.65
--- src/usr.bin/indent/debug.c:1.64	Fri Jun 16 11:48:32 2023
+++ src/usr.bin/indent/debug.c	Fri Jun 16 23:17:22 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.64 2023/06/16 11:48:32 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.65 2023/06/16 23:17:22 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.64 2023/06/16 11:48:32 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.65 2023/06/16 23:17:22 rillig Exp $");
 
 #include 
 #include 
@@ -330,6 +330,7 @@ debug_parser_state(void)
 	debug_ps_enum(prev_lsym, lsym_name);
 	debug_ps_bool(in_stmt_or_decl);
 	debug_ps_bool(in_decl);
+	debug_ps_bool(in_typedef_decl);
 	debug_ps_bool(in_var_decl);
 	debug_ps_bool(in_init);
 	debug_ps_int(init_level);



CVS commit: src/usr.bin/indent

2023-06-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun 16 11:27:49 UTC 2023

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

Log Message:
indent: rename a field of the parser state

The previous name 'comment_in_first_line' was misleading, as it could
mean that there was a comment in the first line of the file.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.198 -r1.199 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.225 -r1.226 src/usr.bin/indent/io.c
cvs rdiff -u -r1.165 -r1.166 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/debug.c
diff -u src/usr.bin/indent/debug.c:1.62 src/usr.bin/indent/debug.c:1.63
--- src/usr.bin/indent/debug.c:1.62	Thu Jun 15 10:59:06 2023
+++ src/usr.bin/indent/debug.c	Fri Jun 16 11:27:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.62 2023/06/15 10:59:06 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.63 2023/06/16 11:27:49 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.62 2023/06/15 10:59:06 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.63 2023/06/16 11:27:49 rillig Exp $");
 
 #include 
 #include 
@@ -363,7 +363,7 @@ debug_parser_state(void)
 	state.heading = "indentation of comments";
 	debug_ps_int(comment_ind);
 	debug_ps_int(comment_shift);
-	debug_ps_bool(comment_in_first_line);
+	debug_ps_bool(comment_cont);
 
 	state.heading = "vertical spacing";
 	debug_ps_bool(break_after_comma);

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.198 src/usr.bin/indent/indent.h:1.199
--- src/usr.bin/indent/indent.h:1.198	Thu Jun 15 10:59:06 2023
+++ src/usr.bin/indent/indent.h	Fri Jun 16 11:27:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.198 2023/06/15 10:59:06 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.199 2023/06/16 11:27:49 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -383,10 +383,10 @@ extern struct parser_state {
 
 	/* Indentation of comments */
 
-	int comment_ind;	/* indentation of the current comment */
+	int comment_ind;	/* total indentation of the current comment */
 	int comment_shift;	/* all but the first line of a boxed comment
  * are shifted this much to the right */
-	bool comment_in_first_line;
+	bool comment_cont;	/* after the first line of a comment */
 
 	/* Vertical spacing */
 

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.225 src/usr.bin/indent/io.c:1.226
--- src/usr.bin/indent/io.c:1.225	Thu Jun 15 11:27:36 2023
+++ src/usr.bin/indent/io.c	Fri Jun 16 11:27:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.225 2023/06/15 11:27:36 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.226 2023/06/16 11:27:49 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.225 2023/06/15 11:27:36 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.226 2023/06/16 11:27:49 rillig Exp $");
 
 #include 
 
@@ -300,9 +300,9 @@ output_comment(void)
 	int target_ind = ps.comment_ind;
 	const char *p;
 
-	if (!ps.comment_in_first_line)
+	if (ps.comment_cont)
 		target_ind += ps.comment_shift;
-	ps.comment_in_first_line = false;
+	ps.comment_cont = true;
 
 	/* consider the original indentation in case this is a box comment */
 	for (p = com.s; *p == '\t'; p++)

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.165 src/usr.bin/indent/pr_comment.c:1.166
--- src/usr.bin/indent/pr_comment.c:1.165	Wed Jun 14 14:11:28 2023
+++ src/usr.bin/indent/pr_comment.c	Fri Jun 16 11:27:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.165 2023/06/14 14:11:28 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.166 2023/06/16 11:27:49 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.165 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.166 2023/06/16 11:27:49 rillig Exp $");
 
 #include 
 
@@ -142,7 +142,7 @@ analyze_comment(bool *p_may_wrap, bool *
 static void
 copy_comment_start(bool may_wrap, bool *delim, int line_length)
 {
-	ps.comment_in_first_line = true;
+	ps.comment_cont = false;
 	com_add_char('/');
 	com_add_char(token.s[token.len - 1]);	/* either '*' or '/' */
 



CVS commit: src/usr.bin/indent

2023-06-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun 16 11:27:49 UTC 2023

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

Log Message:
indent: rename a field of the parser state

The previous name 'comment_in_first_line' was misleading, as it could
mean that there was a comment in the first line of the file.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.198 -r1.199 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.225 -r1.226 src/usr.bin/indent/io.c
cvs rdiff -u -r1.165 -r1.166 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.



CVS commit: src/usr.bin/indent

2023-06-15 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Thu Jun 15 11:27:37 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c io.c

Log Message:
indent: consolidate handling of statement continuations


To generate a diff of this commit:
cvs rdiff -u -r1.371 -r1.372 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.224 -r1.225 src/usr.bin/indent/io.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.371 src/usr.bin/indent/indent.c:1.372
--- src/usr.bin/indent/indent.c:1.371	Thu Jun 15 10:59:06 2023
+++ src/usr.bin/indent/indent.c	Thu Jun 15 11:27:36 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.371 2023/06/15 10:59:06 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.372 2023/06/15 11:27:36 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.371 2023/06/15 10:59:06 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.372 2023/06/15 11:27:36 rillig Exp $");
 
 #include 
 #include 
@@ -562,12 +562,6 @@ process_newline(void)
 
 	output_line();
 
-	if (ps.psyms.len >= 2
-	&& ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum
-	&& ps.paren.len == 0
-	&& ps.prev_lsym == lsym_comma)
-		ps.line_is_stmt_cont = false;
-
 stay_in_line:
 	++line_no;
 }

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.224 src/usr.bin/indent/io.c:1.225
--- src/usr.bin/indent/io.c:1.224	Thu Jun 15 10:59:06 2023
+++ src/usr.bin/indent/io.c	Thu Jun 15 11:27:36 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.224 2023/06/15 10:59:06 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.225 2023/06/15 11:27:36 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.224 2023/06/15 10:59:06 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.225 2023/06/15 11:27:36 rillig Exp $");
 
 #include 
 
@@ -55,7 +55,7 @@ static unsigned newlines = 2;	/* the tot
  * finished line, anything > 1 are trailing
  * blank lines */
 static unsigned buffered_newlines;	/* not yet written */
-static int paren_indent;
+static int paren_indent;	/* total indentation when parenthesized */
 
 
 static void
@@ -368,6 +368,19 @@ output_indented_line(void)
 	out.prev_line_kind = out.line_kind;
 }
 
+static bool
+is_stmt_cont(void)
+{
+	if (ps.psyms.len >= 2
+	&& ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum
+	&& ps.prev_lsym == lsym_comma
+	&& ps.paren.len == 0)
+		return false;
+	return ps.in_stmt_or_decl
+	&& (!ps.in_decl || ps.in_init)
+	&& ps.init_level == 0;
+}
+
 /*
  * Write a line of formatted source to the output file. The line consists of
  * the label, the code and the comment.
@@ -393,9 +406,7 @@ output_line(void)
 
 	ps.line_has_decl = ps.in_decl;
 	ps.line_has_func_def = false;
-	ps.line_is_stmt_cont = ps.in_stmt_or_decl
-	&& (!ps.in_decl || ps.in_init)
-	&& ps.init_level == 0;
+	ps.line_is_stmt_cont = is_stmt_cont();
 	ps.decl_indent_done = false;
 	if (ps.extra_expr_indent == eei_last)
 		ps.extra_expr_indent = eei_no;



CVS commit: src/usr.bin/indent

2023-06-15 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Thu Jun 15 11:27:37 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c io.c

Log Message:
indent: consolidate handling of statement continuations


To generate a diff of this commit:
cvs rdiff -u -r1.371 -r1.372 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.224 -r1.225 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-15 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Thu Jun 15 10:59:06 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: rename state variable to be more accurate

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.370 -r1.371 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.197 -r1.198 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.223 -r1.224 src/usr.bin/indent/io.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.61 src/usr.bin/indent/debug.c:1.62
--- src/usr.bin/indent/debug.c:1.61	Wed Jun 14 20:46:08 2023
+++ src/usr.bin/indent/debug.c	Thu Jun 15 10:59:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.61 2023/06/14 20:46:08 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.62 2023/06/15 10:59:06 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.61 2023/06/14 20:46:08 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.62 2023/06/15 10:59:06 rillig Exp $");
 
 #include 
 #include 
@@ -344,7 +344,7 @@ debug_parser_state(void)
 	state.heading = "indentation of statements and declarations";
 	debug_ps_int(ind_level);
 	debug_ps_int(ind_level_follow);
-	debug_ps_bool(in_stmt_cont);
+	debug_ps_bool(line_is_stmt_cont);
 	debug_ps_int(decl_level);
 	debug_ps_di_stack();
 	debug_ps_bool(decl_indent_done);

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.370 src/usr.bin/indent/indent.c:1.371
--- src/usr.bin/indent/indent.c:1.370	Thu Jun 15 10:34:12 2023
+++ src/usr.bin/indent/indent.c	Thu Jun 15 10:59:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.370 2023/06/15 10:34:12 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.371 2023/06/15 10:59:06 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.370 2023/06/15 10:34:12 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.371 2023/06/15 10:59:06 rillig Exp $");
 
 #include 
 #include 
@@ -566,7 +566,7 @@ process_newline(void)
 	&& ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum
 	&& ps.paren.len == 0
 	&& ps.prev_lsym == lsym_comma)
-		ps.in_stmt_cont = false;
+		ps.line_is_stmt_cont = false;
 
 stay_in_line:
 	++line_no;
@@ -724,8 +724,7 @@ process_lbrace(void)
 	}
 
 	if (code.len == 0)
-		ps.in_stmt_cont = false;	/* don't indent the '{' itself
-		 */
+		ps.line_is_stmt_cont = false;
 	if (ps.in_decl && ps.in_var_decl) {
 		ps.di_stack[ps.decl_level] = ps.decl_ind;
 		if (++ps.decl_level == (int)array_length(ps.di_stack)) {
@@ -768,7 +767,7 @@ process_rbrace(void)
 	buf_add_char(, '}');
 	ps.want_blank = true;
 	ps.in_stmt_or_decl = false;	// XXX: Initializers don't end a stmt
-	ps.in_stmt_cont = false;
+	ps.line_is_stmt_cont = false;
 
 	if (ps.decl_level > 0) {	/* multi-level structure declaration */
 		ps.decl_ind = ps.di_stack[--ps.decl_level];
@@ -925,7 +924,7 @@ process_type_outside_parentheses(void)
 	if (ps.in_func_def_params && opt.indent_parameters &&
 	ps.decl_level == 0) {
 		ps.ind_level = ps.ind_level_follow = 1;
-		ps.in_stmt_cont = false;
+		ps.line_is_stmt_cont = false;
 	}
 
 	ps.in_var_decl = /* maybe */ true;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.197 src/usr.bin/indent/indent.h:1.198
--- src/usr.bin/indent/indent.h:1.197	Wed Jun 14 20:46:08 2023
+++ src/usr.bin/indent/indent.h	Thu Jun 15 10:59:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.197 2023/06/14 20:46:08 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.198 2023/06/15 10:59:06 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -342,7 +342,7 @@ extern struct parser_state {
  * currently prepared for output */
 	int ind_level_follow;	/* the level to which ind_level should be set
  * after the current line is printed */
-	bool in_stmt_cont;	/* whether the current line should have an
+	bool line_is_stmt_cont;	/* whether the current line should have an
  * extra indentation level because we are in
  * the middle of a statement */
 	int decl_level;		/* current nesting level for a structure

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.223 src/usr.bin/indent/io.c:1.224
--- src/usr.bin/indent/io.c:1.223	Thu Jun 15 10:34:12 2023
+++ src/usr.bin/indent/io.c	Thu Jun 15 10:59:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.223 2023/06/15 10:34:12 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.224 2023/06/15 10:59:06 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.223 2023/06/15 10:34:12 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.224 2023/06/15 10:59:06 rillig Exp $");
 
 #include 
 
@@ -254,7 +254,7 @@ compute_code_indent(void)
 	int base_ind = ps.ind_level * 

CVS commit: src/usr.bin/indent

2023-06-15 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Thu Jun 15 10:59:06 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: rename state variable to be more accurate

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.370 -r1.371 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.197 -r1.198 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.223 -r1.224 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 21:35:01 UTC 2023

Modified Files:
src/usr.bin/indent: args.c

Log Message:
indent: reduce number of relocations

Since all command line options modify a member of struct options, there
is no need to encode that relocation 38 times.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/usr.bin/indent/args.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/args.c
diff -u src/usr.bin/indent/args.c:1.83 src/usr.bin/indent/args.c:1.84
--- src/usr.bin/indent/args.c:1.83	Sat Jun 10 16:43:55 2023
+++ src/usr.bin/indent/args.c	Wed Jun 14 21:35:01 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: args.c,v 1.83 2023/06/10 16:43:55 rillig Exp $	*/
+/*	$NetBSD: args.c,v 1.84 2023/06/14 21:35:01 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,12 +38,13 @@
  */
 
 #include 
-__RCSID("$NetBSD: args.c,v 1.83 2023/06/10 16:43:55 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.84 2023/06/14 21:35:01 rillig Exp $");
 
 /* Read options from profile files and from the command line. */
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -51,17 +52,18 @@ __RCSID("$NetBSD: args.c,v 1.83 2023/06/
 #include "indent.h"
 
 #if __STDC_VERSION__ >= 201112L
-#define assert_type(expr, type) _Generic((expr), type : (expr))
+#define get_offset(name, type) \
+	_Generic((), type *: offsetof(struct options, name))
 #else
-#define assert_type(expr, type) (expr)
+#define get_offset(name, type) (offsetof(struct options, name))
 #endif
 
 #define bool_option(name, value, var) \
-	{name, true, false, value, 0, 0, assert_type(&(opt.var), bool *)}
+	{name, true, false, value, 0, 0, get_offset(var, bool)}
 #define bool_options(name, var) \
-	{name, true, true, false, 0, 0, assert_type(&(opt.var), bool *)}
+	{name, true, true, false, 0, 0, get_offset(var, bool)}
 #define int_option(name, var, min, max) \
-	{name, false, false, false, min, max, assert_type(&(opt.var), int *)}
+	{name, false, false, false, min, max, get_offset(var, int)}
 
 /* See set_special_option for special options. */
 static const struct pro {
@@ -71,7 +73,7 @@ static const struct pro {
 	bool p_bool_value;	/* only relevant if !p_may_negate */
 	short i_min;
 	short i_max;
-	void *p_var;		/* the associated variable */
+	unsigned short opt_offset;	/* the associated variable */
 } pro[] = {
 	bool_options("bacc", blank_line_around_conditional_compilation),
 	bool_options("bad", blank_line_after_decl),
@@ -232,7 +234,7 @@ found:
 			errx(1, "%s: unknown option \"-%s\"",
 			option_source, arg);
 
-		*(bool *)p->p_var =
+		*(bool *)((unsigned char *)(void *) + p->opt_offset) =
 		p->p_may_negate ? arg[0] != 'n' : p->p_bool_value;
 		return;
 	}
@@ -250,7 +252,7 @@ found:
 		"must be between %d and %d",
 		option_source, arg_arg, p->p_name, p->i_min, p->i_max);
 
-	*(int *)p->p_var = (int)num;
+	*(int *)((unsigned char *)(void *) + p->opt_offset) = (int)num;
 }
 
 static void



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 21:35:01 UTC 2023

Modified Files:
src/usr.bin/indent: args.c

Log Message:
indent: reduce number of relocations

Since all command line options modify a member of struct options, there
is no need to encode that relocation 38 times.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/usr.bin/indent/args.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 17:52:46 UTC 2023

Modified Files:
src/usr.bin/indent: parse.c

Log Message:
indent: fix out-of-bounds read when reducing a statement

Since parse.c 1.73 from today. The parser symbol psym_stmt_list that was
removed in that commit acted as a stop symbol, so that psyms_reduce_stmt
would save a memory access.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.75 src/usr.bin/indent/parse.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/parse.c
diff -u src/usr.bin/indent/parse.c:1.74 src/usr.bin/indent/parse.c:1.75
--- src/usr.bin/indent/parse.c:1.74	Wed Jun 14 16:14:30 2023
+++ src/usr.bin/indent/parse.c	Wed Jun 14 17:52:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.74 2023/06/14 16:14:30 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.75 2023/06/14 17:52:45 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: parse.c,v 1.74 2023/06/14 16:14:30 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.75 2023/06/14 17:52:45 rillig Exp $");
 
 #include 
 
@@ -116,7 +116,7 @@ static void
 psyms_reduce(struct psym_stack *psyms)
 {
 again:
-	if (psyms->sym[psyms->len - 1] == psym_stmt
+	if (psyms->len >= 2 && psyms->sym[psyms->len - 1] == psym_stmt
 	&& psyms_reduce_stmt(psyms))
 		goto again;
 	if (psyms->sym[psyms->len - 1] == psym_while_expr &&



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 17:52:46 UTC 2023

Modified Files:
src/usr.bin/indent: parse.c

Log Message:
indent: fix out-of-bounds read when reducing a statement

Since parse.c 1.73 from today. The parser symbol psym_stmt_list that was
removed in that commit acted as a stop symbol, so that psyms_reduce_stmt
would save a memory access.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.75 src/usr.bin/indent/parse.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 16:14:30 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c parse.c

Log Message:
indent: clean up array indexing for parser symbols

With 'top' pointing to the actual top element, the array was indexed in
the closed range from 0 to top. All other arrays are indexed by the
usual half-open interval from 0 to len.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.364 -r1.365 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.194 -r1.195 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.220 -r1.221 src/usr.bin/indent/io.c
cvs rdiff -u -r1.228 -r1.229 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.73 -r1.74 src/usr.bin/indent/parse.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.59 src/usr.bin/indent/debug.c:1.60
--- src/usr.bin/indent/debug.c:1.59	Wed Jun 14 14:11:28 2023
+++ src/usr.bin/indent/debug.c	Wed Jun 14 16:14:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.59 2023/06/14 14:11:28 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.60 2023/06/14 16:14:30 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.59 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.60 2023/06/14 16:14:30 rillig Exp $");
 
 #include 
 #include 
@@ -383,7 +383,7 @@ debug_psyms_stack(const char *situation)
 {
 	debug_printf("parse stack %s:", situation);
 	const struct psym_stack *psyms = 
-	for (int i = 0; i <= psyms->top; ++i)
+	for (size_t i = 0; i < psyms->len; ++i)
 		debug_printf(" %d %s",
 		psyms->ind_level[i], psym_name[psyms->sym[i]]);
 	debug_println("");

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.364 src/usr.bin/indent/indent.c:1.365
--- src/usr.bin/indent/indent.c:1.364	Wed Jun 14 14:11:28 2023
+++ src/usr.bin/indent/indent.c	Wed Jun 14 16:14:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.364 2023/06/14 14:11:28 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.365 2023/06/14 16:14:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.364 2023/06/14 14:11:28 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.365 2023/06/14 16:14:30 rillig Exp $");
 
 #include 
 #include 
@@ -187,7 +187,7 @@ ind_add(int ind, const char *s, size_t l
 static void
 init_globals(void)
 {
-	ps.psyms.sym[0] = psym_stmt;
+	ps.psyms.sym[ps.psyms.len++] = psym_stmt;
 	ps.prev_lsym = lsym_semicolon;
 	ps.lbrace_kind = psym_lbrace_block;
 
@@ -388,7 +388,7 @@ process_eof(void)
 {
 	finish_output();
 
-	if (ps.psyms.top > 1)	/* check for balanced braces */
+	if (ps.psyms.len > 2)	/* check for balanced braces */
 		diag(1, "Stuff missing from end of file");
 
 	return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
@@ -534,7 +534,7 @@ process_newline(void)
 	&& lab.len == 0	/* for preprocessing lines */
 	&& com.len == 0)
 		goto stay_in_line;
-	if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr
+	if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr
 	&& opt.brace_same_line
 	&& com.len == 0) {
 		ps.force_nl = true;
@@ -577,7 +577,7 @@ process_lparen(void)
 	if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
 		ps.extra_expr_indent = eei_maybe;
 
-	if (ps.in_var_decl && ps.psyms.top <= 2 && !ps.in_init) {
+	if (ps.in_var_decl && ps.psyms.len <= 3 && !ps.in_init) {
 		parse(psym_stmt);	/* prepare for function definition */
 		ps.in_var_decl = false;
 	}
@@ -762,14 +762,14 @@ process_rbrace(void)
 		ps.in_decl = true;
 	}
 
-	if (ps.psyms.top == 2)
+	if (ps.psyms.len == 3)
 		out.line_kind = lk_func_end;
 
 	parse(psym_rbrace);
 
 	if (!ps.in_var_decl
-	&& ps.psyms.sym[ps.psyms.top] != psym_do_stmt
-	&& ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt)
+	&& ps.psyms.sym[ps.psyms.len - 1] != psym_do_stmt
+	&& ps.psyms.sym[ps.psyms.len - 1] != psym_if_expr_stmt)
 		ps.force_nl = true;
 }
 
@@ -905,7 +905,7 @@ process_type_outside_parentheses(void)
 {
 	parse(psym_decl);	/* let the parser worry about indentation */
 
-	if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0)
+	if (ps.prev_lsym == lsym_rparen && ps.psyms.len <= 2 && code.len > 0)
 		output_line();
 
 	if (ps.in_func_def_params && opt.indent_parameters &&

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.194 src/usr.bin/indent/indent.h:1.195
--- src/usr.bin/indent/indent.h:1.194	Wed Jun 14 14:11:28 2023
+++ src/usr.bin/indent/indent.h	Wed Jun 14 16:14:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.194 2023/06/14 14:11:28 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.195 2023/06/14 16:14:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -272,7 +272,10 @@ struct 

CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 16:14:30 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c parse.c

Log Message:
indent: clean up array indexing for parser symbols

With 'top' pointing to the actual top element, the array was indexed in
the closed range from 0 to top. All other arrays are indexed by the
usual half-open interval from 0 to len.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.364 -r1.365 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.194 -r1.195 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.220 -r1.221 src/usr.bin/indent/io.c
cvs rdiff -u -r1.228 -r1.229 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.73 -r1.74 src/usr.bin/indent/parse.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 13:15:30 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: merge duplicate code


To generate a diff of this commit:
cvs rdiff -u -r1.362 -r1.363 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.



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 13:15:30 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: merge duplicate code


To generate a diff of this commit:
cvs rdiff -u -r1.362 -r1.363 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.362 src/usr.bin/indent/indent.c:1.363
--- src/usr.bin/indent/indent.c:1.362	Wed Jun 14 11:18:09 2023
+++ src/usr.bin/indent/indent.c	Wed Jun 14 13:15:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.362 2023/06/14 11:18:09 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.363 2023/06/14 13:15:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.362 2023/06/14 11:18:09 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.363 2023/06/14 13:15:30 rillig Exp $");
 
 #include 
 #include 
@@ -523,13 +523,20 @@ want_blank_before_lparen(void)
 }
 
 static void
-process_lparen(void)
+ps_paren_push(int indent, enum paren_level_cast cast)
 {
 	if (++ps.nparen == array_length(ps.paren)) {
 		diag(0, "Reached internal limit of %zu unclosed parentheses",
-		array_length(ps.paren));
+		 array_length(ps.paren));
 		ps.nparen--;
 	}
+	ps.paren[ps.nparen - 1].indent = indent;
+	ps.paren[ps.nparen - 1].cast = cast;
+}
+
+static void
+process_lparen(void)
+{
 
 	if (is_function_pointer_declaration())
 		indent_declarator(ps.decl_ind, ps.tabs_to_var);
@@ -558,8 +565,7 @@ process_lparen(void)
 	|| ps.line_has_func_def)
 		cast = cast_no;
 
-	ps.paren[ps.nparen - 1].indent = indent;
-	ps.paren[ps.nparen - 1].cast = cast;
+	ps_paren_push(indent, cast);
 	debug_println("paren_indents[%d] is now %s%d",
 	ps.nparen - 1, paren_level_cast_name[cast], indent);
 }
@@ -605,12 +611,6 @@ unbalanced:
 static void
 process_lbracket(void)
 {
-	if (++ps.nparen == array_length(ps.paren)) {
-		diag(0, "Reached internal limit of %zu unclosed parentheses",
-		array_length(ps.paren));
-		ps.nparen--;
-	}
-
 	if (code.len > 0
 	&& (ps.prev_lsym == lsym_comma || ps.prev_lsym == lsym_binary_op))
 		buf_add_char(, ' ');
@@ -618,9 +618,7 @@ process_lbracket(void)
 	buf_add_char(, token.s[0]);
 
 	int indent = ind_add(0, code.s, code.len);
-
-	ps.paren[ps.nparen - 1].indent = indent;
-	ps.paren[ps.nparen - 1].cast = cast_no;
+	ps_paren_push(indent, cast_no);
 	debug_println("paren_indents[%d] is now %d", ps.nparen - 1, indent);
 }
 



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 09:57:02 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.h io.c

Log Message:
indent: clean up debugging code


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.192 -r1.193 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.218 -r1.219 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 09:57:02 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.h io.c

Log Message:
indent: clean up debugging code


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.192 -r1.193 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.218 -r1.219 src/usr.bin/indent/io.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.57 src/usr.bin/indent/debug.c:1.58
--- src/usr.bin/indent/debug.c:1.57	Wed Jun 14 09:31:05 2023
+++ src/usr.bin/indent/debug.c	Wed Jun 14 09:57:02 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.58 2023/06/14 09:57:02 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.58 2023/06/14 09:57:02 rillig Exp $");
 
 #include 
 #include 
@@ -40,10 +40,8 @@ __RCSID("$NetBSD: debug.c,v 1.57 2023/06
 #ifdef debug
 
 static struct {
-	/*-
-	 * false	show only the changes to the parser state
-	 * true		show unchanged parts of the parser state as well
-	 */
+	// false	show only the changes to the parser state
+	// true		show unchanged parts of the parser state as well
 	bool full_parser_state;
 } config = {
 	.full_parser_state = false,
@@ -191,10 +189,9 @@ debug_blank_line(void)
 }
 
 void
-debug_vis_range(const char *prefix, const char *s, size_t len,
-const char *suffix)
+debug_vis_range(const char *s, size_t len)
 {
-	debug_printf("%s", prefix);
+	debug_printf("\"");
 	for (size_t i = 0; i < len; i++) {
 		const char *p = s + i;
 		if (*p == '\\' || *p == '"')
@@ -208,7 +205,7 @@ debug_vis_range(const char *prefix, cons
 		else
 			debug_printf("\\x%02x", (unsigned char)*p);
 	}
-	debug_printf("%s", suffix);
+	debug_printf("\"");
 }
 
 void
@@ -216,7 +213,7 @@ debug_print_buf(const char *name, const 
 {
 	if (buf->len > 0) {
 		debug_printf(" %s ", name);
-		debug_vis_range("\"", buf->s, buf->len, "\"");
+		debug_vis_range(buf->s, buf->len);
 	}
 }
 
@@ -230,7 +227,7 @@ debug_buffers(void)
 }
 
 static void
-write_ps_bool(const char *name, bool prev, bool curr)
+debug_ps_bool_member(const char *name, bool prev, bool curr)
 {
 	if (!state.ps_first && curr != prev) {
 		char diff = " -+x"[(prev ? 1 : 0) + (curr ? 2 : 0)];
@@ -240,7 +237,7 @@ write_ps_bool(const char *name, bool pre
 }
 
 static void
-write_ps_int(const char *name, int prev, int curr)
+debug_ps_int_member(const char *name, int prev, int curr)
 {
 	if (!state.ps_first && curr != prev)
 		debug_println(" %3d -> %3d  ps.%s", prev, curr, name);
@@ -249,7 +246,7 @@ write_ps_int(const char *name, int prev,
 }
 
 static void
-write_ps_enum(const char *name, const char *prev, const char *curr)
+debug_ps_enum_member(const char *name, const char *prev, const char *curr)
 {
 	if (!state.ps_first && strcmp(prev, curr) != 0)
 		debug_println(" %3s -> %3s  ps.%s", prev, curr, name);
@@ -316,11 +313,12 @@ debug_ps_di_stack(void)
 }
 
 #define debug_ps_bool(name) \
-	write_ps_bool(#name, state.prev_ps.name, ps.name)
+	debug_ps_bool_member(#name, state.prev_ps.name, ps.name)
 #define debug_ps_int(name) \
-	write_ps_int(#name, state.prev_ps.name, ps.name)
+	debug_ps_int_member(#name, state.prev_ps.name, ps.name)
 #define debug_ps_enum(name, names) \
-write_ps_enum(#name, (names)[state.prev_ps.name], (names)[ps.name])
+debug_ps_enum_member(#name, (names)[state.prev_ps.name], \
+	(names)[ps.name])
 
 void
 debug_parser_state(void)

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.192 src/usr.bin/indent/indent.h:1.193
--- src/usr.bin/indent/indent.h:1.192	Wed Jun 14 09:31:05 2023
+++ src/usr.bin/indent/indent.h	Wed Jun 14 09:57:02 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.192 2023/06/14 09:31:05 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.193 2023/06/14 09:57:02 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -431,7 +431,7 @@ extern struct output_state {
 void debug_printf(const char *, ...) __printflike(1, 2);
 void debug_println(const char *, ...) __printflike(1, 2);
 void debug_blank_line(void);
-void debug_vis_range(const char *, const char *, size_t, const char *);
+void debug_vis_range(const char *, size_t);
 void debug_parser_state(void);
 void debug_psyms_stack(const char *);
 void debug_print_buf(const char *, const struct buffer *);
@@ -445,7 +445,7 @@ extern const char *const line_kind_name[
 #define	debug_printf(fmt, ...) debug_noop()
 #define	debug_println(fmt, ...) debug_noop()
 #define debug_blank_line() debug_noop()
-#define	debug_vis_range(prefix, s, e, suffix) debug_noop()
+#define	debug_vis_range(s, len) debug_noop()
 #define	debug_parser_state() debug_noop()
 

CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 08:36:51 UTC 2023

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

Log Message:
indent: remove another flag from parser state

When processing a comment, the flag ps.next_col_1 was not used for the
next token, but for a line within a comment.  As its scope was limited
to a single comment, there is no need to store it any longer than that

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.358 -r1.359 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.190 -r1.191 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.226 -r1.227 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.162 -r1.163 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/debug.c
diff -u src/usr.bin/indent/debug.c:1.55 src/usr.bin/indent/debug.c:1.56
--- src/usr.bin/indent/debug.c:1.55	Wed Jun 14 08:25:15 2023
+++ src/usr.bin/indent/debug.c	Wed Jun 14 08:36:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.55 2023/06/14 08:25:15 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.55 2023/06/14 08:25:15 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $");
 
 #include 
 #include 
@@ -374,9 +374,6 @@ debug_parser_state(void)
 	debug_ps_enum(declaration, declaration_name);
 	debug_ps_bool(blank_line_after_decl);
 
-	state.heading = "comments";
-	debug_ps_bool(next_col_1);
-
 	state.heading = NULL;
 	debug_blank_line();
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.358 src/usr.bin/indent/indent.c:1.359
--- src/usr.bin/indent/indent.c:1.358	Wed Jun 14 07:20:55 2023
+++ src/usr.bin/indent/indent.c	Wed Jun 14 08:36:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.358 2023/06/14 07:20:55 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.359 2023/06/14 08:36:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.358 2023/06/14 07:20:55 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.359 2023/06/14 08:36:51 rillig Exp $");
 
 #include 
 #include 
@@ -186,7 +186,6 @@ init_globals(void)
 {
 	ps.psyms.sym[0] = psym_stmt;
 	ps.prev_lsym = lsym_semicolon;
-	ps.next_col_1 = true;
 	ps.lbrace_kind = psym_lbrace_block;
 
 	const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.190 src/usr.bin/indent/indent.h:1.191
--- src/usr.bin/indent/indent.h:1.190	Wed Jun 14 08:25:15 2023
+++ src/usr.bin/indent/indent.h	Wed Jun 14 08:36:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.190 2023/06/14 08:25:15 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.191 2023/06/14 08:36:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -402,11 +402,6 @@ extern struct parser_state {
 		decl_end,	/* finished a declaration */
 	} declaration;
 	bool blank_line_after_decl;
-
-	/* Comments */
-
-	bool next_col_1;	/* whether the next token starts in column 1 of
- * the original input */
 } ps;
 
 extern struct output_state {

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.226 src/usr.bin/indent/lexi.c:1.227
--- src/usr.bin/indent/lexi.c:1.226	Wed Jun 14 08:25:15 2023
+++ src/usr.bin/indent/lexi.c	Wed Jun 14 08:36:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.226 2023/06/14 08:25:15 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.227 2023/06/14 08:36:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.226 2023/06/14 08:25:15 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.227 2023/06/14 08:36:51 rillig Exp $");
 
 #include 
 #include 
@@ -532,7 +532,6 @@ lexer_symbol
 lexi(void)
 {
 	buf_clear();
-	ps.next_col_1 = false;
 
 	for (;;) {
 		if (ch_isblank(inp_p[0]))
@@ -567,7 +566,6 @@ lexi(void)
 		/* if data has been exhausted, the '\n' is a dummy. */
 		lsym = had_eof ? lsym_eof : lsym_newline;
 		next_unary = ps.next_unary;
-		ps.next_col_1 = true;
 		break;
 
 	/* INDENT OFF */

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.162 src/usr.bin/indent/pr_comment.c:1.163
--- src/usr.bin/indent/pr_comment.c:1.162	Wed Jun 14 08:25:15 2023
+++ src/usr.bin/indent/pr_comment.c	Wed Jun 14 08:36:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.162 2023/06/14 08:25:15 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.162 2023/06/14 08:25:15 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp 

CVS commit: src/usr.bin/indent

2023-06-14 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun 14 08:36:51 UTC 2023

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

Log Message:
indent: remove another flag from parser state

When processing a comment, the flag ps.next_col_1 was not used for the
next token, but for a line within a comment.  As its scope was limited
to a single comment, there is no need to store it any longer than that

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.358 -r1.359 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.190 -r1.191 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.226 -r1.227 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.162 -r1.163 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.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 21:36:38 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: rename misleading variable

The name started with 'line_start', but the value is not always the
value from the beginning of the line.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.356 -r1.357 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.187 -r1.188 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.216 -r1.217 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 21:36:38 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: rename misleading variable

The name started with 'line_start', but the value is not always the
value from the beginning of the line.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.356 -r1.357 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.187 -r1.188 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.216 -r1.217 src/usr.bin/indent/io.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.52 src/usr.bin/indent/debug.c:1.53
--- src/usr.bin/indent/debug.c:1.52	Sat Jun 10 20:37:12 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 10 21:36:38 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.52 2023/06/10 20:37:12 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.53 2023/06/10 21:36:38 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.52 2023/06/10 20:37:12 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.53 2023/06/10 21:36:38 rillig Exp $");
 
 #include 
 #include 
@@ -360,7 +360,7 @@ debug_parser_state(void)
 	state.heading = "spacing inside a statement or declaration";
 	debug_ps_bool(next_unary);
 	debug_ps_bool(want_blank);
-	debug_ps_int(line_start_nparen);
+	debug_ps_int(ind_paren_level);
 	debug_ps_int(nparen);
 	debug_ps_paren();
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.356 src/usr.bin/indent/indent.c:1.357
--- src/usr.bin/indent/indent.c:1.356	Sat Jun 10 20:37:12 2023
+++ src/usr.bin/indent/indent.c	Sat Jun 10 21:36:38 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.356 2023/06/10 20:37:12 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.357 2023/06/10 21:36:38 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.356 2023/06/10 20:37:12 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.357 2023/06/10 21:36:38 rillig Exp $");
 
 #include 
 #include 
@@ -378,7 +378,7 @@ is_function_pointer_declaration(void)
 	&& !ps.in_init
 	&& !ps.decl_indent_done
 	&& !ps.line_has_func_def
-	&& ps.line_start_nparen == 0;
+	&& ps.ind_paren_level == 0;
 }
 
 static int
@@ -580,7 +580,7 @@ process_rparen(void)
 		ps.want_blank = true;
 
 	if (code.len == 0)
-		ps.line_start_nparen = ps.nparen;
+		ps.ind_paren_level = ps.nparen;
 
 unbalanced:
 	buf_add_char(, token.s[0]);
@@ -631,7 +631,7 @@ process_rbracket(void)
 
 	ps.want_blank = true;
 	if (code.len == 0)
-		ps.line_start_nparen = ps.nparen;
+		ps.ind_paren_level = ps.nparen;
 
 unbalanced:
 	buf_add_char(, token.s[0]);
@@ -788,7 +788,7 @@ process_comma(void)
 	 * does not start the line */
 
 	if (ps.in_decl && !ps.line_has_func_def && !ps.in_init &&
-	!ps.decl_indent_done && ps.line_start_nparen == 0) {
+	!ps.decl_indent_done && ps.ind_paren_level == 0) {
 		/* indent leading commas and not the actual identifiers */
 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
 	}
@@ -844,7 +844,7 @@ process_semicolon(void)
 	ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no;
 
 	if (ps.in_decl && code.len == 0 && !ps.in_init &&
-	!ps.decl_indent_done && ps.line_start_nparen == 0) {
+	!ps.decl_indent_done && ps.ind_paren_level == 0) {
 		/* indent stray semicolons in declarations */
 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
 	}
@@ -915,7 +915,7 @@ process_word(lexer_symbol lsym)
 			ps.want_blank = false;
 
 		} else if (!ps.in_init && !ps.decl_indent_done &&
-		ps.line_start_nparen == 0) {
+		ps.ind_paren_level == 0) {
 			if (opt.decl_indent == 0
 			&& code.len > 0 && code.s[code.len - 1] == '}')
 ps.decl_ind = ind_add(0, code.s, code.len) + 1;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.187 src/usr.bin/indent/indent.h:1.188
--- src/usr.bin/indent/indent.h:1.187	Sat Jun 10 16:43:56 2023
+++ src/usr.bin/indent/indent.h	Sat Jun 10 21:36:38 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.187 2023/06/10 16:43:56 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.188 2023/06/10 21:36:38 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -368,10 +368,9 @@ extern struct parser_state {
 	bool want_blank;	/* whether the following token should be
  * prefixed by a blank. (Said prefixing is
  * ignored in some cases.) */
-	int line_start_nparen;	/* the number of parentheses or brackets that
- * were open at the beginning of the current
- * line; used to indent within statements,
- * initializers and declarations */
+	int ind_paren_level;	/* the number of parentheses or brackets that
+ * is used for indenting a continuation line of
+ * a 

CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 20:37:13 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c

Log Message:
indent: fix debug output

When the parser state was first printed, there were unintended diff
markers.  Treat the previous lexer symbol like the other parts of the
parser state, as omitting it from the diff output is confusing.


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.355 -r1.356 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.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 20:37:13 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c

Log Message:
indent: fix debug output

When the parser state was first printed, there were unintended diff
markers.  Treat the previous lexer symbol like the other parts of the
parser state, as omitting it from the diff output is confusing.


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.355 -r1.356 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/debug.c
diff -u src/usr.bin/indent/debug.c:1.51 src/usr.bin/indent/debug.c:1.52
--- src/usr.bin/indent/debug.c:1.51	Sat Jun 10 18:46:42 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 10 20:37:12 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.51 2023/06/10 18:46:42 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.52 2023/06/10 20:37:12 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.51 2023/06/10 18:46:42 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.52 2023/06/10 20:37:12 rillig Exp $");
 
 #include 
 #include 
@@ -233,7 +233,7 @@ debug_buffers(void)
 static void
 write_ps_bool(const char *name, bool prev, bool curr)
 {
-	if (curr != prev) {
+	if (!state.ps_first && curr != prev) {
 		char diff = " -+x"[(prev ? 1 : 0) + (curr ? 2 : 0)];
 		debug_println("[%c]  ps.%s", diff, name);
 	} else if (config.full_parser_state || state.ps_first)
@@ -243,7 +243,7 @@ write_ps_bool(const char *name, bool pre
 static void
 write_ps_int(const char *name, int prev, int curr)
 {
-	if (curr != prev)
+	if (!state.ps_first && curr != prev)
 		debug_println(" %3d -> %3d  ps.%s", prev, curr, name);
 	else if (config.full_parser_state || state.ps_first)
 		debug_println("%3d  ps.%s", curr, name);
@@ -252,7 +252,7 @@ write_ps_int(const char *name, int prev,
 static void
 write_ps_enum(const char *name, const char *prev, const char *curr)
 {
-	if (strcmp(prev, curr) != 0)
+	if (!state.ps_first && strcmp(prev, curr) != 0)
 		debug_println(" %3s -> %3s  ps.%s", prev, curr, name);
 	else if (config.full_parser_state || state.ps_first)
 		debug_println(" %10s  ps.%s", curr, name);
@@ -327,10 +327,9 @@ void
 debug_parser_state(void)
 {
 	debug_blank_line();
-	debug_println(" ps.prev_lsym = %s",
-	lsym_name[ps.prev_lsym]);
 
 	state.heading = "token classification";
+	debug_ps_enum(prev_lsym, lsym_name);
 	debug_ps_bool(in_stmt_or_decl);
 	debug_ps_bool(in_decl);
 	debug_ps_bool(in_var_decl);

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.355 src/usr.bin/indent/indent.c:1.356
--- src/usr.bin/indent/indent.c:1.355	Sat Jun 10 18:46:42 2023
+++ src/usr.bin/indent/indent.c	Sat Jun 10 20:37:12 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.355 2023/06/10 18:46:42 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.356 2023/06/10 20:37:12 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.355 2023/06/10 18:46:42 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.356 2023/06/10 20:37:12 rillig Exp $");
 
 #include 
 #include 
@@ -1060,12 +1060,12 @@ indent(void)
 
 		process_lsym(lsym);
 
-		debug_parser_state();
-
 		if (lsym != lsym_preprocessing
 		&& lsym != lsym_newline
 		&& lsym != lsym_comment)
 			ps.prev_lsym = lsym;
+
+		debug_parser_state();
 	}
 }
 



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 13:03:17 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c lexi.c

Log Message:
indent: clean up function names, fix blank lines in debug output


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.223 -r1.224 src/usr.bin/indent/lexi.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.48 src/usr.bin/indent/debug.c:1.49
--- src/usr.bin/indent/debug.c:1.48	Sat Jun 10 11:01:58 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 10 13:03:17 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.48 2023/06/10 11:01:58 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.49 2023/06/10 13:03:17 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.48 2023/06/10 11:01:58 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.49 2023/06/10 13:03:17 rillig Exp $");
 
 #include 
 #include 
@@ -287,7 +287,7 @@ debug_ps_paren(void)
 	}
 	if (ps.nparen == 0)
 		debug_printf(" none");
-	debug_blank_line();
+	debug_println("");
 }
 
 static bool
@@ -313,7 +313,7 @@ debug_ps_di_stack(void)
 		debug_printf(" %d", ps.di_stack[i]);
 	if (ps.decl_level == 0)
 		debug_printf(" none");
-	debug_blank_line();
+	debug_println("");
 }
 
 #define debug_ps_bool(name) \

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.223 src/usr.bin/indent/lexi.c:1.224
--- src/usr.bin/indent/lexi.c:1.223	Sat Jun 10 12:59:31 2023
+++ src/usr.bin/indent/lexi.c	Sat Jun 10 13:03:17 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.223 2023/06/10 12:59:31 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.224 2023/06/10 13:03:17 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.223 2023/06/10 12:59:31 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.224 2023/06/10 13:03:17 rillig Exp $");
 
 #include 
 #include 
@@ -313,7 +313,7 @@ cmp_keyword_by_name(const void *key, con
  * this starts a function definition or a declaration.
  */
 static bool
-probably_looking_at_definition(void)
+probably_function_definition(void)
 {
 	int paren_level = 0;
 	for (const char *p = inp_p; *p != '\n'; p++) {
@@ -418,7 +418,7 @@ found_typename:
 	if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 &&
 	!ps.in_func_def_params && !ps.in_init) {
 
-		if (ps.nparen == 0 && probably_looking_at_definition()) {
+		if (ps.nparen == 0 && probably_function_definition()) {
 			ps.line_has_func_def = true;
 			if (ps.in_decl)
 ps.in_func_def_params = true;
@@ -434,7 +434,7 @@ found_typename:
 }
 
 static bool
-is_asterisk_unary(void)
+is_asterisk_pointer(void)
 {
 	if (inp_p[strspn(inp_p, "* \t")] == ')')
 		return true;
@@ -464,7 +464,7 @@ probably_in_function_definition(void)
 }
 
 static void
-lex_asterisk_unary(void)
+lex_asterisk_pointer(void)
 {
 	while (inp_p[0] == '*' || ch_isspace(inp_p[0])) {
 		if (inp_p[0] == '*')
@@ -623,8 +623,8 @@ lexi(void)
 		if (inp_p[0] == '=') {
 			token_add_char(*inp_p++);
 			lsym = lsym_binary_op;
-		} else if (is_asterisk_unary()) {
-			lex_asterisk_unary();
+		} else if (is_asterisk_pointer()) {
+			lex_asterisk_pointer();
 			lsym = lsym_unary_op;
 		} else
 			lsym = lsym_binary_op;



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 13:03:17 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c lexi.c

Log Message:
indent: clean up function names, fix blank lines in debug output


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.223 -r1.224 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 12:59:31 UTC 2023

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

Log Message:
indent: in debug mode, null-terminate buffers


To generate a diff of this commit:
cvs rdiff -u -r1.352 -r1.353 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.185 -r1.186 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.214 -r1.215 src/usr.bin/indent/io.c
cvs rdiff -u -r1.222 -r1.223 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.159 -r1.160 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.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 12:59:31 UTC 2023

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

Log Message:
indent: in debug mode, null-terminate buffers


To generate a diff of this commit:
cvs rdiff -u -r1.352 -r1.353 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.185 -r1.186 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.214 -r1.215 src/usr.bin/indent/io.c
cvs rdiff -u -r1.222 -r1.223 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.159 -r1.160 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.352 src/usr.bin/indent/indent.c:1.353
--- src/usr.bin/indent/indent.c:1.352	Sat Jun 10 08:17:04 2023
+++ src/usr.bin/indent/indent.c	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.352 2023/06/10 08:17:04 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.352 2023/06/10 08:17:04 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $");
 
 #include 
 #include 
@@ -110,12 +110,23 @@ buf_expand(struct buffer *buf, size_t ad
 	buf->s = nonnull(realloc(buf->s, buf->cap));
 }
 
+#ifdef debug
+void
+buf_terminate(struct buffer *buf)
+{
+	if (buf->len == buf->cap)
+		buf_expand(buf, 1);
+	buf->s[buf->len] = '\0';
+}
+#endif
+
 void
 buf_add_char(struct buffer *buf, char ch)
 {
 	if (buf->len == buf->cap)
 		buf_expand(buf, 1);
 	buf->s[buf->len++] = ch;
+	buf_terminate(buf);
 }
 
 void
@@ -127,6 +138,7 @@ buf_add_chars(struct buffer *buf, const 
 		buf_expand(buf, len);
 	memcpy(buf->s + buf->len, s, len);
 	buf->len += len;
+	buf_terminate(buf);
 }
 
 static void
@@ -327,7 +339,7 @@ move_com_to_code(lexer_symbol lsym)
 	if (ps.want_blank)
 		buf_add_char(, ' ');
 	buf_add_buf(, );
-	com.len = 0;
+	buf_clear();
 	ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
 }
 
@@ -433,6 +445,7 @@ read_preprocessing_line(void)
 
 	while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
 		lab.len--;
+	buf_terminate();
 }
 
 static void
@@ -805,7 +818,7 @@ process_colon_label(void)
 {
 	buf_add_buf(, );
 	buf_add_char(, ':');
-	code.len = 0;
+	buf_clear();
 
 	if (ps.seen_case)
 		out.line_kind = lk_case_or_default;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.185 src/usr.bin/indent/indent.h:1.186
--- src/usr.bin/indent/indent.h:1.185	Sat Jun 10 07:42:41 2023
+++ src/usr.bin/indent/indent.h	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.185 2023/06/10 07:42:41 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.186 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -141,7 +141,7 @@ typedef enum parser_symbol {
 	psym_while_expr,	/* 'while' '(' expr ')' */
 } parser_symbol;
 
-/* A range of characters, not null-terminated. */
+/* A range of characters, only null-terminated in debug mode. */
 struct buffer {
 	char *s;
 	size_t len;
@@ -522,3 +522,16 @@ next_tab(int ind)
 {
 	return ind - ind % opt.tabsize + opt.tabsize;
 }
+
+#ifdef debug
+void buf_terminate(struct buffer *);
+#else
+#define buf_terminate(buf) debug_noop()
+#endif
+
+static inline void
+buf_clear(struct buffer *buf)
+{
+	buf->len = 0;
+	buf_terminate(buf);
+}

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.214 src/usr.bin/indent/io.c:1.215
--- src/usr.bin/indent/io.c:1.214	Sat Jun 10 11:01:58 2023
+++ src/usr.bin/indent/io.c	Sat Jun 10 12:59:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.215 2023/06/10 12:59:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.215 2023/06/10 12:59:31 rillig Exp $");
 
 #include 
 
@@ -61,7 +61,7 @@ static int paren_indent;
 static void
 inp_read_next_line(FILE *f)
 {
-	inp.len = 0;
+	buf_clear();
 
 	for (;;) {
 		int ch = getc(f);
@@ -79,6 +79,7 @@ inp_read_next_line(FILE *f)
 		if (ch == '\n')
 			break;
 	}
+	buf_terminate();
 	inp_p = inp.s;
 }
 
@@ -86,7 +87,7 @@ void
 inp_read_line(void)
 {
 	if (indent_enabled == indent_on)
-		out.indent_off_text.len = 0;
+		buf_clear(_off_text);
 	buf_add_chars(_off_text, inp.s, inp.len);
 	inp_read_next_line(input);
 }
@@ -316,6 +317,7 @@ output_line_comment(void)
 
 	while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
 		com.len--;
+	buf_terminate();
 
 	write_indent(target_ind);
 	write_range(p, com.len - (size_t)(p - com.s));
@@ -377,12 +379,12 @@ output_line(void)
 	else if (indent_enabled == indent_last_off_line) {
 		indent_enabled = indent_on;
 		write_range(out.indent_off_text.s, out.indent_off_text.len);
-		

CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 11:01:58 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c io.c

Log Message:
indent: distinguish blank lines from newline characters


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.213 -r1.214 src/usr.bin/indent/io.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.47 src/usr.bin/indent/debug.c:1.48
--- src/usr.bin/indent/debug.c:1.47	Sat Jun 10 09:31:41 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 10 11:01:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.47 2023/06/10 09:31:41 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.48 2023/06/10 11:01:58 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.47 2023/06/10 09:31:41 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.48 2023/06/10 11:01:58 rillig Exp $");
 
 #include 
 #include 
@@ -144,7 +144,7 @@ static struct {
 	struct parser_state prev_ps;
 	bool ps_first;
 	const char *heading;
-	unsigned wrote_newlines;
+	unsigned wrote_newlines;
 } state = {
 	.ps_first = true,
 	.wrote_newlines = 1,
@@ -227,7 +227,7 @@ debug_buffers(void)
 	debug_print_buf("label", );
 	debug_print_buf("code", );
 	debug_print_buf("comment", );
-	debug_println("");
+	debug_blank_line();
 }
 
 static void
@@ -287,7 +287,7 @@ debug_ps_paren(void)
 	}
 	if (ps.nparen == 0)
 		debug_printf(" none");
-	debug_println("");
+	debug_blank_line();
 }
 
 static bool
@@ -313,7 +313,7 @@ debug_ps_di_stack(void)
 		debug_printf(" %d", ps.di_stack[i]);
 	if (ps.decl_level == 0)
 		debug_printf(" none");
-	debug_println("");
+	debug_blank_line();
 }
 
 #define debug_ps_bool(name) \
@@ -395,6 +395,6 @@ debug_parse_stack(const char *situation)
 	for (int i = 0; i <= psyms->top; ++i)
 		debug_printf(" %d %s",
 		psyms->ind_level[i], psym_name[psyms->sym[i]]);
-	debug_println("");
+	debug_blank_line();
 }
 #endif

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.213 src/usr.bin/indent/io.c:1.214
--- src/usr.bin/indent/io.c:1.213	Sat Jun 10 08:17:04 2023
+++ src/usr.bin/indent/io.c	Sat Jun 10 11:01:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.213 2023/06/10 08:17:04 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.213 2023/06/10 08:17:04 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $");
 
 #include 
 
@@ -50,10 +50,11 @@ const char *inp_p;
 struct output_state out;
 enum indent_enabled indent_enabled;
 static int out_ind;		/* width of the line that is being written */
-static unsigned wrote_newlines = 2;	/* 0 in the middle of a line, 1 after a
-	 * single '\n', > 1 means there were (n
-	 * - 1) blank lines above */
-static unsigned buffered_blank_lines;
+static unsigned newlines = 2;	/* the total of written and buffered newlines;
+ * 0 in the middle of a line, 1 after a single
+ * finished line, anything > 1 are trailing
+ * blank lines */
+static unsigned buffered_newlines;	/* not yet written */
 static int paren_indent;
 
 
@@ -108,37 +109,37 @@ inp_next(void)
 
 
 static void
-write_newline(void)
+buffer_newline(void)
 {
-	buffered_blank_lines++;
-	wrote_newlines++;
+	buffered_newlines++;
+	newlines++;
 	out_ind = 0;
 }
 
 static void
-write_buffered_blank_lines(void)
+write_buffered_newlines(void)
 {
-	for (; buffered_blank_lines > 0; buffered_blank_lines--) {
+	for (; buffered_newlines > 0; buffered_newlines--) {
 		fputc('\n', output);
-		debug_println("output_newline");
+		debug_println("write_newline");
 	}
 }
 
 static void
 write_range(const char *s, size_t len)
 {
-	write_buffered_blank_lines();
+	write_buffered_newlines();
 	fwrite(s, 1, len, output);
-	debug_vis_range("output_range \"", s, len, "\"\n");
+	debug_vis_range("write_range \"", s, len, "\"\n");
 	for (size_t i = 0; i < len; i++)
-		wrote_newlines = s[i] == '\n' ? wrote_newlines + 1 : 0;
+		newlines = s[i] == '\n' ? newlines + 1 : 0;
 	out_ind = ind_add(out_ind, s, len);
 }
 
 static void
 write_indent(int new_ind)
 {
-	write_buffered_blank_lines();
+	write_buffered_newlines();
 
 	int ind = out_ind;
 
@@ -148,16 +149,16 @@ write_indent(int new_ind)
 			ind = ind - ind % opt.tabsize + n * opt.tabsize;
 			while (n-- > 0)
 fputc('\t', output);
-			wrote_newlines = 0;
+			newlines = 0;
 		}
 	}
 
 	for (; ind < new_ind; ind++) {
 		fputc(' ', output);
-		wrote_newlines = 0;
+		newlines = 0;
 	}
 
-	debug_println("output_indent %d", ind);
+	debug_println("write_indent %d", ind);
 	out_ind = ind;
 }
 
@@ -191,10 +192,10 @@ static bool
 is_blank_line_optional(void)
 {
 	if (out.prev_line_kind == lk_stmt_head)
-		return wrote_newlines >= 1;
+		

CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 11:01:58 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c io.c

Log Message:
indent: distinguish blank lines from newline characters


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.213 -r1.214 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 09:31:42 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: clean up debug output

In diff mode, don't print a diff of the very first parser state, instead
print its full state.

Don't print headings for empty sections of the parser state.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/usr.bin/indent/debug.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.46 src/usr.bin/indent/debug.c:1.47
--- src/usr.bin/indent/debug.c:1.46	Sat Jun 10 07:42:41 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 10 09:31:41 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.46 2023/06/10 07:42:41 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.47 2023/06/10 09:31:41 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,19 +30,24 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.46 2023/06/10 07:42:41 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.47 2023/06/10 09:31:41 rillig Exp $");
 
 #include 
+#include 
 
 #include "indent.h"
 
 #ifdef debug
 
-/*-
- * false	show only the changes to the parser state
- * true		show unchanged parts of the parser state as well
- */
-static bool debug_full_parser_state = true;
+static struct {
+	/*-
+	 * false	show only the changes to the parser state
+	 * true		show unchanged parts of the parser state as well
+	 */
+	bool full_parser_state;
+} config = {
+	.full_parser_state = false,
+};
 
 const char *const lsym_name[] = {
 	"eof",
@@ -135,8 +140,15 @@ static const char *const extra_expr_inde
 	"last",
 };
 
-static unsigned wrote_newlines = 1;
-
+static struct {
+	struct parser_state prev_ps;
+	bool ps_first;
+	const char *heading;
+	unsigned wrote_newlines;
+} state = {
+	.ps_first = true,
+	.wrote_newlines = 1,
+};
 
 void
 debug_printf(const char *fmt, ...)
@@ -144,10 +156,14 @@ debug_printf(const char *fmt, ...)
 	FILE *f = output == stdout ? stderr : stdout;
 	va_list ap;
 
+	if (state.heading != NULL) {
+		fprintf(f, "%s\n", state.heading);
+		state.heading = NULL;
+	}
 	va_start(ap, fmt);
 	vfprintf(f, fmt, ap);
 	va_end(ap);
-	wrote_newlines = 0;
+	state.wrote_newlines = 0;
 }
 
 void
@@ -156,17 +172,22 @@ debug_println(const char *fmt, ...)
 	FILE *f = output == stdout ? stderr : stdout;
 	va_list ap;
 
+	if (state.heading != NULL) {
+		fprintf(f, "%s\n", state.heading);
+		state.heading = NULL;
+		state.wrote_newlines = 1;
+	}
 	va_start(ap, fmt);
 	vfprintf(f, fmt, ap);
 	va_end(ap);
 	fprintf(f, "\n");
-	wrote_newlines = fmt[0] == '\0' ? wrote_newlines + 1 : 1;
+	state.wrote_newlines = fmt[0] == '\0' ? state.wrote_newlines + 1 : 1;
 }
 
 void
 debug_blank_line(void)
 {
-	while (wrote_newlines < 2)
+	while (state.wrote_newlines < 2)
 		debug_println("");
 }
 
@@ -209,31 +230,41 @@ debug_buffers(void)
 	debug_println("");
 }
 
-#define debug_ps_bool(name) \
-	if (ps.name != prev_ps.name) \
-	debug_println("[%c]  ps." #name, \
-		" -+x"[(prev_ps.name ? 1 : 0) + (ps.name ? 2 : 0)]); \
-	else if (debug_full_parser_state) \
-	debug_println("[%c]  ps." #name, ps.name ? 'x' : ' ')
-#define debug_ps_int(name) \
-	if (ps.name != prev_ps.name) \
-	debug_println(" %3d -> %3d  ps." #name, prev_ps.name, ps.name); \
-	else if (debug_full_parser_state) \
-	debug_println("%3d  ps." #name, ps.name)
-#define debug_ps_enum(name, names) \
-	if (ps.name != prev_ps.name) \
-	debug_println(" %3s -> %3s  ps." #name, \
-		(names)[prev_ps.name], (names)[ps.name]); \
-	else if (debug_full_parser_state) \
-	debug_println(" %10s  ps." #name, (names)[ps.name])
+static void
+write_ps_bool(const char *name, bool prev, bool curr)
+{
+	if (curr != prev) {
+		char diff = " -+x"[(prev ? 1 : 0) + (curr ? 2 : 0)];
+		debug_println("[%c]  ps.%s", diff, name);
+	} else if (config.full_parser_state || state.ps_first)
+		debug_println("[%c]  ps.%s", curr ? 'x' : ' ', name);
+}
+
+static void
+write_ps_int(const char *name, int prev, int curr)
+{
+	if (curr != prev)
+		debug_println(" %3d -> %3d  ps.%s", prev, curr, name);
+	else if (config.full_parser_state || state.ps_first)
+		debug_println("%3d  ps.%s", curr, name);
+}
+
+static void
+write_ps_enum(const char *name, const char *prev, const char *curr)
+{
+	if (strcmp(prev, curr) != 0)
+		debug_println(" %3s -> %3s  ps.%s", prev, curr, name);
+	else if (config.full_parser_state || state.ps_first)
+		debug_println(" %10s  ps.%s", curr, name);
+}
 
 static bool
-ps_paren_has_changed(const struct parser_state *prev_ps)
+ps_paren_has_changed(void)
 {
-	if (prev_ps->nparen != ps.nparen)
+	if (state.prev_ps.nparen != ps.nparen)
 		return true;
 
-	const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
+	const paren_level_props *prev = state.prev_ps.paren, *curr = ps.paren;
 	for (int i = 0; i < 

CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 09:31:42 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: clean up debug output

In diff mode, don't print a diff of the very first parser state, instead
print its full state.

Don't print headings for empty sections of the parser state.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/usr.bin/indent/debug.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 07:53:00 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: extract output of an indented line to separate function


To generate a diff of this commit:
cvs rdiff -u -r1.211 -r1.212 src/usr.bin/indent/io.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/io.c
diff -u src/usr.bin/indent/io.c:1.211 src/usr.bin/indent/io.c:1.212
--- src/usr.bin/indent/io.c:1.211	Sat Jun 10 07:48:55 2023
+++ src/usr.bin/indent/io.c	Sat Jun 10 07:53:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.211 2023/06/10 07:48:55 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.212 2023/06/10 07:53:00 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.211 2023/06/10 07:48:55 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.212 2023/06/10 07:53:00 rillig Exp $");
 
 #include 
 
@@ -322,6 +322,42 @@ output_line_comment(void)
 	ps.comment_delta = ps.n_comment_delta;
 }
 
+static void
+output_line_indented(void)
+{
+	if (lab.len == 0 && code.len == 0 && com.len == 0)
+		out.line_kind = lk_blank;
+
+	if (want_blank_line() && wrote_newlines < 2
+	&& out.line_kind != lk_blank)
+		write_newline();
+
+	/* This kludge aligns function definitions correctly. */
+	if (ps.ind_level == 0)
+		ps.in_stmt_cont = false;
+
+	if (opt.blank_line_after_decl && ps.declaration == decl_end
+	&& ps.psyms.top > 1) {
+		ps.declaration = decl_no;
+		ps.blank_line_after_decl = true;
+	}
+
+	if (opt.swallow_optional_blanklines
+	&& out.line_kind == lk_blank
+	&& is_blank_line_optional())
+		return;
+
+	if (lab.len > 0)
+		output_line_label();
+	if (code.len > 0)
+		output_line_code();
+	if (com.len > 0)
+		output_line_comment();
+
+	write_newline();
+	out.prev_line_kind = out.line_kind;
+}
+
 /*
  * Write a line of formatted source to the output file. The line consists of
  * the label, the code and the comment.
@@ -333,47 +369,14 @@ output_line(void)
 	debug_printf("%s", __func__);
 	debug_buffers();
 
-	if (indent_enabled == indent_on) {
-		if (lab.len == 0 && code.len == 0 && com.len == 0)
-			out.line_kind = lk_blank;
-
-		if (want_blank_line() && wrote_newlines < 2
-		&& out.line_kind != lk_blank)
-			write_newline();
-
-		/* This kludge aligns function definitions correctly. */
-		if (ps.ind_level == 0)
-			ps.in_stmt_cont = false;
-
-		if (opt.blank_line_after_decl && ps.declaration == decl_end
-		&& ps.psyms.top > 1) {
-			ps.declaration = decl_no;
-			ps.blank_line_after_decl = true;
-		}
-
-		if (opt.swallow_optional_blanklines
-		&& out.line_kind == lk_blank
-		&& is_blank_line_optional())
-			goto prepare_next_line;
-
-		if (lab.len > 0)
-			output_line_label();
-		if (code.len > 0)
-			output_line_code();
-		if (com.len > 0)
-			output_line_comment();
-
-		write_newline();
-		out.prev_line_kind = out.line_kind;
-	}
-
-	if (indent_enabled == indent_last_off_line) {
+	if (indent_enabled == indent_on)
+		output_line_indented();
+	else if (indent_enabled == indent_last_off_line) {
 		indent_enabled = indent_on;
 		write_range(out.indent_off_text.s, out.indent_off_text.len);
 		out.indent_off_text.len = 0;
 	}
 
-prepare_next_line:
 	lab.len = 0;
 	code.len = 0;
 	com.len = 0;



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 07:53:00 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: extract output of an indented line to separate function


To generate a diff of this commit:
cvs rdiff -u -r1.211 -r1.212 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 07:48:55 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: clean up function names and order in output


To generate a diff of this commit:
cvs rdiff -u -r1.210 -r1.211 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 07:48:55 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: clean up function names and order in output


To generate a diff of this commit:
cvs rdiff -u -r1.210 -r1.211 src/usr.bin/indent/io.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/io.c
diff -u src/usr.bin/indent/io.c:1.210 src/usr.bin/indent/io.c:1.211
--- src/usr.bin/indent/io.c:1.210	Sat Jun 10 07:42:41 2023
+++ src/usr.bin/indent/io.c	Sat Jun 10 07:48:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.210 2023/06/10 07:42:41 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.211 2023/06/10 07:48:55 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.210 2023/06/10 07:42:41 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.211 2023/06/10 07:48:55 rillig Exp $");
 
 #include 
 
@@ -108,7 +108,7 @@ inp_next(void)
 
 
 static void
-output_newline(void)
+write_newline(void)
 {
 	buffered_blank_lines++;
 	wrote_newlines++;
@@ -125,7 +125,7 @@ write_buffered_blank_lines(void)
 }
 
 static void
-output_range(const char *s, size_t len)
+write_range(const char *s, size_t len)
 {
 	write_buffered_blank_lines();
 	fwrite(s, 1, len, output);
@@ -136,7 +136,7 @@ output_range(const char *s, size_t len)
 }
 
 static void
-output_indent(int new_ind)
+write_indent(int new_ind)
 {
 	write_buffered_blank_lines();
 
@@ -161,22 +161,6 @@ output_indent(int new_ind)
 	out_ind = ind;
 }
 
-void
-output_finish(void)
-{
-	if (lab.len > 0 || code.len > 0 || com.len > 0)
-		output_line();
-	if (indent_enabled == indent_on) {
-		if (buffered_blank_lines > 1)
-			buffered_blank_lines = 1;
-		write_buffered_blank_lines();
-	} else {
-		indent_enabled = indent_last_off_line;
-		output_line();
-	}
-	fflush(output);
-}
-
 static bool
 want_blank_line(void)
 {
@@ -236,8 +220,8 @@ compute_label_indent(void)
 static void
 output_line_label(void)
 {
-	output_indent(compute_label_indent());
-	output_range(lab.s, lab.len);
+	write_indent(compute_label_indent());
+	write_range(lab.s, lab.len);
 }
 
 static int
@@ -300,9 +284,9 @@ output_line_code(void)
 	}
 
 	if (lab.len > 0 && target_ind <= out_ind)
-		output_range(" ", 1);
-	output_indent(target_ind);
-	output_range(code.s, code.len);
+		write_range(" ", 1);
+	write_indent(target_ind);
+	write_range(code.s, code.len);
 }
 
 static void
@@ -327,13 +311,13 @@ output_line_comment(void)
 	}
 
 	if (out_ind > target_ind)
-		output_newline();
+		write_newline();
 
 	while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
 		com.len--;
 
-	output_indent(target_ind);
-	output_range(p, com.len - (size_t)(p - com.s));
+	write_indent(target_ind);
+	write_range(p, com.len - (size_t)(p - com.s));
 
 	ps.comment_delta = ps.n_comment_delta;
 }
@@ -355,7 +339,7 @@ output_line(void)
 
 		if (want_blank_line() && wrote_newlines < 2
 		&& out.line_kind != lk_blank)
-			output_newline();
+			write_newline();
 
 		/* This kludge aligns function definitions correctly. */
 		if (ps.ind_level == 0)
@@ -379,13 +363,13 @@ output_line(void)
 		if (com.len > 0)
 			output_line_comment();
 
-		output_newline();
+		write_newline();
 		out.prev_line_kind = out.line_kind;
 	}
 
 	if (indent_enabled == indent_last_off_line) {
 		indent_enabled = indent_on;
-		output_range(out.indent_off_text.s, out.indent_off_text.len);
+		write_range(out.indent_off_text.s, out.indent_off_text.len);
 		out.indent_off_text.len = 0;
 	}
 
@@ -416,3 +400,19 @@ prepare_next_line:
 
 	out.line_kind = lk_other;
 }
+
+void
+output_finish(void)
+{
+	if (lab.len > 0 || code.len > 0 || com.len > 0)
+		output_line();
+	if (indent_enabled == indent_on) {
+		if (buffered_blank_lines > 1)
+			buffered_blank_lines = 1;
+		write_buffered_blank_lines();
+	} else {
+		indent_enabled = indent_last_off_line;
+		output_line();
+	}
+	fflush(output);
+}



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 07:42:41 UTC 2023

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

Log Message:
indent: clean up function and variable names


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.350 -r1.351 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.184 -r1.185 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.209 -r1.210 src/usr.bin/indent/io.c
cvs rdiff -u -r1.221 -r1.222 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-10 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Jun 10 07:42:41 UTC 2023

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

Log Message:
indent: clean up function and variable names


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.350 -r1.351 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.184 -r1.185 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.209 -r1.210 src/usr.bin/indent/io.c
cvs rdiff -u -r1.221 -r1.222 src/usr.bin/indent/lexi.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.45 src/usr.bin/indent/debug.c:1.46
--- src/usr.bin/indent/debug.c:1.45	Sat Jun 10 07:05:18 2023
+++ src/usr.bin/indent/debug.c	Sat Jun 10 07:42:41 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.45 2023/06/10 07:05:18 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.46 2023/06/10 07:42:41 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.45 2023/06/10 07:05:18 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.46 2023/06/10 07:42:41 rillig Exp $");
 
 #include 
 
@@ -299,7 +299,7 @@ debug_parser_state(void)
 	debug_ps_bool(in_var_decl);
 	debug_ps_bool(in_init);
 	debug_ps_int(init_level);
-	debug_ps_bool(in_func_def_line);
+	debug_ps_bool(line_has_func_def);
 	debug_ps_bool(in_func_def_params);
 	debug_ps_bool(line_has_decl);
 	debug_ps_enum(lbrace_kind, psym_name);

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.350 src/usr.bin/indent/indent.c:1.351
--- src/usr.bin/indent/indent.c:1.350	Sat Jun 10 06:52:35 2023
+++ src/usr.bin/indent/indent.c	Sat Jun 10 07:42:41 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.350 2023/06/10 06:52:35 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.351 2023/06/10 07:42:41 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.350 2023/06/10 06:52:35 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.351 2023/06/10 07:42:41 rillig Exp $");
 
 #include 
 #include 
@@ -372,7 +372,7 @@ is_function_pointer_declaration(void)
 	return ps.in_decl
 	&& !ps.in_init
 	&& !ps.decl_indent_done
-	&& !ps.in_func_def_line
+	&& !ps.line_has_func_def
 	&& ps.line_start_nparen == 0;
 }
 
@@ -545,7 +545,7 @@ process_lparen(void)
 	|| ps.prev_lsym == lsym_if
 	|| ps.prev_lsym == lsym_switch
 	|| ps.prev_lsym == lsym_while
-	|| ps.in_func_def_line)
+	|| ps.line_has_func_def)
 		cast = cast_no;
 
 	ps.paren[ps.nparen - 1].indent = indent;
@@ -779,21 +779,15 @@ static void
 process_question(void)
 {
 	ps.quest_level++;
-	if (code.len == 0) {
-		ps.in_stmt_cont = true;
-		ps.in_stmt_or_decl = true;
-		ps.in_decl = false;
-	}
+	if (code.len == 0)
+		ps.in_stmt_cont = true;	// XXX: should be unnecessary.
 }
 
 static void
 process_colon_question(void)
 {
-	if (code.len == 0) {
-		ps.in_stmt_cont = true;
-		ps.in_stmt_or_decl = true;
-		ps.in_decl = false;
-	}
+	if (code.len == 0)
+		ps.in_stmt_cont = true;	// XXX: should be unnecessary.
 }
 
 static void
@@ -802,7 +796,7 @@ process_comma(void)
 	ps.want_blank = code.len > 0;	/* only put blank after comma if comma
 	 * does not start the line */
 
-	if (ps.in_decl && !ps.in_func_def_line && !ps.in_init &&
+	if (ps.in_decl && !ps.line_has_func_def && !ps.in_init &&
 	!ps.decl_indent_done && ps.line_start_nparen == 0) {
 		/* indent leading commas and not the actual identifiers */
 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
@@ -891,7 +885,7 @@ process_semicolon(void)
 }
 
 static void
-process_type(void)
+process_type_outside_parentheses(void)
 {
 	parse(psym_decl);	/* let the parser worry about indentation */
 
@@ -918,7 +912,7 @@ process_type(void)
 }
 
 static void
-process_ident(lexer_symbol lsym)
+process_word(lexer_symbol lsym)
 {
 	if (ps.in_decl) {
 		if (lsym == lsym_funcname) {
@@ -940,8 +934,8 @@ process_ident(lexer_symbol lsym)
 
 	} else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
 		ps.force_nl = true;
-		ps.next_unary = true;
 		ps.in_stmt_or_decl = false;
+		ps.next_unary = true;
 		parse(ps.spaced_expr_psym);
 		ps.spaced_expr_psym = psym_0;
 	}
@@ -1014,7 +1008,7 @@ process_lsym(lexer_symbol lsym)
 			goto copy_token;
 		/* FALLTHROUGH */
 	case lsym_type_outside_parentheses:
-		process_type();
+		process_type_outside_parentheses();
 		goto copy_token;
 
 	case lsym_type_in_parentheses:
@@ -1023,7 +1017,7 @@ process_lsym(lexer_symbol lsym)
 	case lsym_word:
 	case lsym_funcname:
 	case lsym_return:
-		process_ident(lsym);
+		process_word(lsym);
 copy_token:
 		if (ps.want_blank)
 			buf_add_char(, ' ');

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.184 src/usr.bin/indent/indent.h:1.185
--- src/usr.bin/indent/indent.h:1.184	Sat Jun 10 

CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 19:50:51 UTC 2023

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

Log Message:
indent: clean up lexer

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.219 -r1.220 src/usr.bin/indent/lexi.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/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.219 src/usr.bin/indent/lexi.c:1.220
--- src/usr.bin/indent/lexi.c:1.219	Fri Jun  9 09:49:07 2023
+++ src/usr.bin/indent/lexi.c	Fri Jun  9 19:50:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.219 2023/06/09 09:49:07 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.220 2023/06/09 19:50:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.219 2023/06/09 09:49:07 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.220 2023/06/09 19:50:51 rillig Exp $");
 
 #include 
 #include 
@@ -260,16 +260,16 @@ probably_typename(void)
 		return false;
 	if (ps.in_stmt_or_decl)	/* XXX: this condition looks incorrect */
 		return false;
-	if (inp_p[0] == '*' && inp_p[1] != '=')
-		goto maybe;
-	/* XXX: is_identifier_start */
-	if (ch_isalpha(inp_p[0]))
-		goto maybe;
+	if (ps.prev_lsym == lsym_semicolon
+	|| ps.prev_lsym == lsym_lbrace
+	|| ps.prev_lsym == lsym_rbrace) {
+		if (inp_p[0] == '*' && inp_p[1] != '=')
+			return true;
+		/* XXX: is_identifier_start */
+		if (ch_isalpha(inp_p[0]))
+			return true;
+	}
 	return false;
-maybe:
-	return ps.prev_lsym == lsym_semicolon ||
-	ps.prev_lsym == lsym_lbrace ||
-	ps.prev_lsym == lsym_rbrace;
 }
 
 static int
@@ -558,26 +558,9 @@ lexi(void)
 
 	switch (token.s[token.len - 1]) {
 
-	/* INDENT OFF */
-	case '(':	lsym = lsym_lparen;	next_unary = true;	break;
-	case '[':	lsym = lsym_lbracket;	next_unary = true;	break;
-	case ')':	lsym = lsym_rparen;	next_unary = false;	break;
-	case ']':	lsym = lsym_rbracket;	next_unary = false;	break;
-	case '?':	lsym = lsym_question;	next_unary = true;	break;
-	case ';':	lsym = lsym_semicolon;	next_unary = true;	break;
-	case '{':	lsym = lsym_lbrace;	next_unary = true;	break;
-	case '}':	lsym = lsym_rbrace;	next_unary = true;	break;
-	case ',':	lsym = lsym_comma;	next_unary = true;	break;
-	case '.':	lsym = lsym_period;	next_unary = false;	break;
-	/* INDENT ON */
-
-	case ':':
-		lsym = ps.quest_level > 0
-		? (ps.quest_level--, lsym_colon_question)
-		: ps.init_or_struct
-		? lsym_colon_other
-		: lsym_colon_label;
-		next_unary = true;
+	case '#':
+		lsym = lsym_preprocessing;
+		next_unary = ps.next_unary;
 		break;
 
 	case '\n':
@@ -587,17 +570,18 @@ lexi(void)
 		ps.next_col_1 = true;
 		break;
 
-	case '#':
-		lsym = lsym_preprocessing;
-		next_unary = ps.next_unary;
-		break;
-
-	case '\'':
-	case '"':
-		lex_char_or_string();
-		lsym = lsym_word;
-		next_unary = false;
-		break;
+	/* INDENT OFF */
+	case '(':	lsym = lsym_lparen;	next_unary = true;	break;
+	case ')':	lsym = lsym_rparen;	next_unary = false;	break;
+	case '[':	lsym = lsym_lbracket;	next_unary = true;	break;
+	case ']':	lsym = lsym_rbracket;	next_unary = false;	break;
+	case '{':	lsym = lsym_lbrace;	next_unary = true;	break;
+	case '}':	lsym = lsym_rbrace;	next_unary = true;	break;
+	case '.':	lsym = lsym_period;	next_unary = false;	break;
+	case '?':	lsym = lsym_question;	next_unary = true;	break;
+	case ',':	lsym = lsym_comma;	next_unary = true;	break;
+	case ';':	lsym = lsym_semicolon;	next_unary = true;	break;
+	/* INDENT ON */
 
 	case '-':
 	case '+':
@@ -626,6 +610,27 @@ lexi(void)
 		}
 		break;
 
+	case ':':
+		lsym = ps.quest_level > 0
+		? (ps.quest_level--, lsym_colon_question)
+		: ps.init_or_struct
+		? lsym_colon_other
+		: lsym_colon_label;
+		next_unary = true;
+		break;
+
+	case '*':
+		if (inp_p[0] == '=') {
+			token_add_char(*inp_p++);
+			lsym = lsym_binary_op;
+		} else if (is_asterisk_unary()) {
+			lex_asterisk_unary();
+			lsym = lsym_unary_op;
+		} else
+			lsym = lsym_binary_op;
+		next_unary = true;
+		break;
+
 	case '=':
 		if (ps.init_or_struct)
 			ps.block_init = true;
@@ -646,16 +651,11 @@ lexi(void)
 		next_unary = true;
 		break;
 
-	case '*':
-		if (inp_p[0] == '=') {
-			token_add_char(*inp_p++);
-			lsym = lsym_binary_op;
-		} else if (is_asterisk_unary()) {
-			lex_asterisk_unary();
-			lsym = lsym_unary_op;
-		} else
-			lsym = lsym_binary_op;
-		next_unary = true;
+	case '\'':
+	case '"':
+		lex_char_or_string();
+		lsym = lsym_word;
+		next_unary = false;
 		break;
 
 	default:



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 19:50:51 UTC 2023

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

Log Message:
indent: clean up lexer

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.219 -r1.220 src/usr.bin/indent/lexi.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 18:09:30 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: sync debug information for lexer symbols


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/usr.bin/indent/debug.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.42 src/usr.bin/indent/debug.c:1.43
--- src/usr.bin/indent/debug.c:1.42	Fri Jun  9 10:24:55 2023
+++ src/usr.bin/indent/debug.c	Fri Jun  9 18:09:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.42 2023/06/09 10:24:55 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.42 2023/06/09 10:24:55 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.43 2023/06/09 18:09:30 rillig Exp $");
 
 #include 
 
@@ -50,32 +50,32 @@ const char *const lsym_name[] = {
 	"newline",
 	"comment",
 	"lparen",
-	"lbracket",
 	"rparen",
+	"lbracket",
 	"rbracket",
 	"lbrace",
 	"rbrace",
 	"period",
 	"unary_op",
-	"binary_op",
+	"sizeof",
+	"offsetof",
 	"postfix_op",
+	"binary_op",
 	"question",
 	"'?:' colon",
-	"label colon",
-	"other colon",
 	"comma",
-	"semicolon",
 	"typedef",
 	"modifier",
+	"tag",
 	"type_outside_parentheses",
 	"type_in_parentheses",
-	"tag",
-	"case",
-	"default",
-	"sizeof",
-	"offsetof",
 	"word",
 	"funcname",
+	"label colon",
+	"other colon",
+	"semicolon",
+	"case",
+	"default",
 	"do",
 	"else",
 	"for",



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 18:09:30 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: sync debug information for lexer symbols


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/usr.bin/indent/debug.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 16:23:43 UTC 2023

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

Log Message:
indent: group lexer symbols by topic, sort processing functions

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.346 -r1.347 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.181 -r1.182 src/usr.bin/indent/indent.h

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.346 src/usr.bin/indent/indent.c:1.347
--- src/usr.bin/indent/indent.c:1.346	Fri Jun  9 11:22:31 2023
+++ src/usr.bin/indent/indent.c	Fri Jun  9 16:23:43 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.346 2023/06/09 11:22:31 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.346 2023/06/09 11:22:31 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $");
 
 #include 
 #include 
@@ -185,6 +185,23 @@ init_globals(void)
 		backup_suffix = suffix;
 }
 
+static void
+load_profiles(int argc, char **argv)
+{
+	const char *profile_name = NULL;
+
+	for (int i = 1; i < argc; ++i) {
+		const char *arg = argv[i];
+
+		if (strcmp(arg, "-npro") == 0)
+			return;
+		if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
+			profile_name = arg + 2;
+	}
+
+	load_profile_files(profile_name);
+}
+
 /*
  * Copy the input file to the backup file, then make the backup file the input
  * and the original input file the output.
@@ -227,23 +244,6 @@ bakcopy(void)
 }
 
 static void
-load_profiles(int argc, char **argv)
-{
-	const char *profile_name = NULL;
-
-	for (int i = 1; i < argc; ++i) {
-		const char *arg = argv[i];
-
-		if (strcmp(arg, "-npro") == 0)
-			return;
-		if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
-			profile_name = arg + 2;
-	}
-
-	load_profile_files(profile_name);
-}
-
-static void
 parse_command_line(int argc, char **argv)
 {
 	for (int i = 1; i < argc; ++i) {
@@ -308,6 +308,44 @@ set_initial_indentation(void)
 }
 
 static void
+maybe_break_line(lexer_symbol lsym)
+{
+	if (!ps.force_nl)
+		return;
+	if (lsym == lsym_semicolon)
+		return;
+	if (lsym == lsym_lbrace && opt.brace_same_line
+	&& ps.prev_lsym != lsym_lbrace)
+		return;
+
+	output_line();
+	ps.force_nl = false;
+}
+
+static void
+move_com_to_code(lexer_symbol lsym)
+{
+	if (ps.want_blank)
+		buf_add_char(, ' ');
+	buf_add_buf(, );
+	com.len = 0;
+	ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
+}
+
+static void
+update_ps_lbrace_kind(lexer_symbol lsym)
+{
+	if (lsym == lsym_tag) {
+		ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
+		token.s[0] == 'u' ? psym_lbrace_union :
+		psym_lbrace_enum;
+	} else if (lsym != lsym_type_outside_parentheses
+	&& lsym != lsym_word
+	&& lsym != lsym_lbrace)
+		ps.lbrace_kind = psym_lbrace_block;
+}
+
+static void
 indent_declarator(int decl_ind, bool tabs_to_var)
 {
 	int base = ps.ind_level * opt.indent_size;
@@ -329,17 +367,14 @@ indent_declarator(int decl_ind, bool tab
 	ps.decl_indent_done = true;
 }
 
-static void
-update_ps_lbrace_kind(lexer_symbol lsym)
+static bool
+is_function_pointer_declaration(void)
 {
-	if (lsym == lsym_tag) {
-		ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
-		token.s[0] == 'u' ? psym_lbrace_union :
-		psym_lbrace_enum;
-	} else if (lsym != lsym_type_outside_parentheses
-	&& lsym != lsym_word
-	&& lsym != lsym_lbrace)
-		ps.lbrace_kind = psym_lbrace_block;
+	return ps.in_decl
+	&& !ps.block_init
+	&& !ps.decl_indent_done
+	&& !ps.is_function_definition
+	&& ps.line_start_nparen == 0;
 }
 
 static int
@@ -359,29 +394,90 @@ process_eof(void)
 	return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 
+/* move the whole line to the 'label' buffer */
 static void
-maybe_break_line(lexer_symbol lsym)
+read_preprocessing_line(void)
 {
-	if (!ps.force_nl)
-		return;
-	if (lsym == lsym_semicolon)
-		return;
-	if (lsym == lsym_lbrace && opt.brace_same_line
-	&& ps.prev_lsym != lsym_lbrace)
-		return;
+	enum {
+		PLAIN, STR, CHR, COMM
+	} state = PLAIN;
 
-	output_line();
-	ps.force_nl = false;
+	buf_add_char(, '#');
+
+	while (inp_p[0] != '\n' || (state == COMM && !had_eof)) {
+		buf_add_char(, inp_next());
+		switch (lab.s[lab.len - 1]) {
+		case '\\':
+			if (state != COMM)
+buf_add_char(, inp_next());
+			break;
+		case '/':
+			if (inp_p[0] == '*' && state == PLAIN) {
+state = COMM;
+buf_add_char(, *inp_p++);
+			}
+			break;
+		case '"':
+			if (state == STR)
+state = PLAIN;
+			else if (state == PLAIN)
+state = STR;
+			break;
+		case '\'':
+			if (state == CHR)
+state = PLAIN;
+			else if (state == PLAIN)
+state = CHR;
+			break;
+		case '*':
+			if (inp_p[0] == '/' && state == COMM) {
+state 

CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 16:23:43 UTC 2023

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

Log Message:
indent: group lexer symbols by topic, sort processing functions

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.346 -r1.347 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.181 -r1.182 src/usr.bin/indent/indent.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 08:16:06 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: eliminate unused variable

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.343 -r1.344 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.



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 08:16:06 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: eliminate unused variable

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.343 -r1.344 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.343 src/usr.bin/indent/indent.c:1.344
--- src/usr.bin/indent/indent.c:1.343	Fri Jun  9 08:10:58 2023
+++ src/usr.bin/indent/indent.c	Fri Jun  9 08:16:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.343 2023/06/09 08:10:58 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.344 2023/06/09 08:16:06 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.343 2023/06/09 08:10:58 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.344 2023/06/09 08:16:06 rillig Exp $");
 
 #include 
 #include 
@@ -92,7 +92,6 @@ FILE *input;
 FILE *output;
 
 static const char *in_name = "Standard Input";
-static const char *out_name = "Standard Output";
 static const char *backup_suffix = ".BAK";
 static char bakfile[MAXPATHLEN] = "";
 
@@ -259,12 +258,11 @@ parse_command_line(int argc, char **argv
 err(1, "%s", in_name);
 
 		} else if (output == NULL) {
-			out_name = arg;
-			if (strcmp(in_name, out_name) == 0)
+			if (strcmp(arg, in_name) == 0)
 errx(1, "input and output files "
 "must be different");
-			if ((output = fopen(out_name, "w")) == NULL)
-err(1, "%s", out_name);
+			if ((output = fopen(arg, "w")) == NULL)
+err(1, "%s", arg);
 
 		} else
 			errx(1, "too many arguments: %s", arg);
@@ -273,10 +271,8 @@ parse_command_line(int argc, char **argv
 	if (input == NULL) {
 		input = stdin;
 		output = stdout;
-	} else if (output == NULL) {
-		out_name = in_name;
+	} else if (output == NULL)
 		bakcopy();
-	}
 
 	if (opt.comment_column <= 1)
 		opt.comment_column = 2;	/* don't put normal comments in column



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 07:20:31 UTC 2023

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

Log Message:
indent: format its own code


To generate a diff of this commit:
cvs rdiff -u -r1.341 -r1.342 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.205 -r1.206 src/usr.bin/indent/io.c
cvs rdiff -u -r1.217 -r1.218 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.69 -r1.70 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.157 -r1.158 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.341 src/usr.bin/indent/indent.c:1.342
--- src/usr.bin/indent/indent.c:1.341	Thu Jun  8 21:18:54 2023
+++ src/usr.bin/indent/indent.c	Fri Jun  9 07:20:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.341 2023/06/08 21:18:54 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.342 2023/06/09 07:20:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.341 2023/06/08 21:18:54 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.342 2023/06/09 07:20:30 rillig Exp $");
 
 #include 
 #include 
@@ -569,7 +569,7 @@ process_unary_op(void)
 		indent_declarator(ind, ps.tabs_to_var);
 		ps.want_blank = false;
 	} else if ((token.s[0] == '+' || token.s[0] == '-')
-	   && code.len > 0 && code.s[code.len - 1] == token.s[0])
+	&& code.len > 0 && code.s[code.len - 1] == token.s[0])
 		ps.want_blank = true;
 
 	if (ps.want_blank)
@@ -682,7 +682,7 @@ process_lbrace(void)
 	if (ps.prev_lsym == lsym_rparen
 	&& ps.psyms.top >= 2
 	&& !(psym == psym_for_exprs || psym == psym_if_expr
-		|| psym == psym_switch_expr || psym == psym_while_expr)) {
+		|| psym == psym_switch_expr || psym == psym_while_expr)) {
 		ps.block_init = true;
 		ps.init_or_struct = true;
 	}
@@ -901,8 +901,8 @@ process_comma(void)
 			ps.block_init = false;
 		int typical_varname_length = 8;
 		if (ps.break_after_comma && (opt.break_after_comma ||
-		ind_add(compute_code_indent(), code.s, code.len)
-		>= opt.max_line_length - typical_varname_length))
+			ind_add(compute_code_indent(), code.s, code.len)
+			>= opt.max_line_length - typical_varname_length))
 			ps.force_nl = true;
 	}
 }

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.205 src/usr.bin/indent/io.c:1.206
--- src/usr.bin/indent/io.c:1.205	Fri Jun  9 06:36:57 2023
+++ src/usr.bin/indent/io.c	Fri Jun  9 07:20:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.205 2023/06/09 06:36:57 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.205 2023/06/09 06:36:57 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $");
 
 #include 
 
@@ -375,7 +375,7 @@ prepare_next_line:
 	if (ps.extra_expr_indent == eei_last)
 		ps.extra_expr_indent = eei_no;
 	if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else
-	&& ps.nparen > 0))
+		&& ps.nparen > 0))
 		ps.ind_level = ps.ind_level_follow;
 	ps.line_start_nparen = ps.nparen;
 	ps.want_blank = false;

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.217 src/usr.bin/indent/lexi.c:1.218
--- src/usr.bin/indent/lexi.c:1.217	Thu Jun  8 21:18:54 2023
+++ src/usr.bin/indent/lexi.c	Fri Jun  9 07:20:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.217 2023/06/08 21:18:54 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.218 2023/06/09 07:20:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: lexi.c,v 1.217 2023/06/08 21:18:54 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.218 2023/06/09 07:20:30 rillig Exp $");
 
 #include 
 #include 
@@ -689,7 +689,7 @@ register_typename(const char *name)
 	if (typenames.len >= typenames.cap) {
 		typenames.cap = 16 + 2 * typenames.cap;
 		typenames.items = nonnull(realloc(typenames.items,
-		sizeof(typenames.items[0]) * typenames.cap));
+			sizeof(typenames.items[0]) * typenames.cap));
 	}
 
 	int pos = bsearch_typenames(name);

Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.69 src/usr.bin/indent/parse.c:1.70
--- src/usr.bin/indent/parse.c:1.69	Wed Jun  7 15:46:12 2023
+++ src/usr.bin/indent/parse.c	Fri Jun  9 07:20:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.69 2023/06/07 15:46:12 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.70 2023/06/09 07:20:30 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: parse.c,v 1.69 2023/06/07 15:46:12 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.70 2023/06/09 07:20:30 rillig Exp $");
 
 #include 
 
@@ -236,7 +236,7 @@ parse(parser_symbol psym)
 	case psym_rbrace:
 		/* stack should have   or   */
 		if (!(psyms->top > 0
-		&& 

CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 07:20:31 UTC 2023

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

Log Message:
indent: format its own code


To generate a diff of this commit:
cvs rdiff -u -r1.341 -r1.342 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.205 -r1.206 src/usr.bin/indent/io.c
cvs rdiff -u -r1.217 -r1.218 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.69 -r1.70 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.157 -r1.158 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.



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 07:04:51 UTC 2023

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

Log Message:
indent: express more clearly how delimited and no-wrap comments relate

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.155 -r1.156 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/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.155 src/usr.bin/indent/pr_comment.c:1.156
--- src/usr.bin/indent/pr_comment.c:1.155	Tue Jun  6 07:51:35 2023
+++ src/usr.bin/indent/pr_comment.c	Fri Jun  9 07:04:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.155 2023/06/06 07:51:35 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.156 2023/06/09 07:04:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.155 2023/06/06 07:51:35 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.156 2023/06/09 07:04:51 rillig Exp $");
 
 #include 
 
@@ -80,22 +80,18 @@ analyze_comment(bool *p_may_wrap, bool *
 int *p_ind, int *p_line_length)
 {
 	bool may_wrap = true;
-	bool delim = opt.comment_delimiter_on_blankline;
+	bool delim = false;
 	int ind;
 	int line_length = opt.max_line_length;
 
 	if (ps.curr_col_1 && !opt.format_col1_comments) {
 		may_wrap = false;
-		delim = false;
 		ind = 0;
-
 	} else {
 		if (inp_p[0] == '-' || inp_p[0] == '*' ||
 		token.s[token.len - 1] == '/' ||
-		(inp_p[0] == '\n' && !opt.format_block_comments)) {
+		(inp_p[0] == '\n' && !opt.format_block_comments))
 			may_wrap = false;
-			delim = false;
-		}
 		if (code.len == 0 && inp_p[strspn(inp_p, "*")] == '\n')
 			out.line_kind = lk_block_comment;
 
@@ -107,9 +103,8 @@ analyze_comment(bool *p_may_wrap, bool *
 			if (ind <= 0)
 ind = opt.format_col1_comments ? 0 : 1;
 			line_length = opt.block_comment_max_line_length;
+			delim = opt.comment_delimiter_on_blankline;
 		} else {
-			delim = false;
-
 			int target_ind = code.len > 0
 			? ind_add(compute_code_indent(), code.s, code.len)
 			: ind_add(compute_label_indent(), lab.s, lab.len);
@@ -151,15 +146,16 @@ copy_comment_start(bool may_wrap, bool *
 	com_add_char('/');
 	com_add_char(token.s[token.len - 1]);	/* either '*' or '/' */
 
-	if (may_wrap && !ch_isblank(inp_p[0]))
-		com_add_char(' ');
-
-	if (*delim && fits_in_one_line(ind, line_length))
-		*delim = false;
+	if (may_wrap) {
+		if (!ch_isblank(inp_p[0]))
+			com_add_char(' ');
 
-	if (*delim) {
-		output_line();
-		com_add_delim();
+		if (*delim && fits_in_one_line(ind, line_length))
+			*delim = false;
+		if (*delim) {
+			output_line();
+			com_add_delim();
+		}
 	}
 }
 



CVS commit: src/usr.bin/indent

2023-06-09 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Jun  9 07:04:51 UTC 2023

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

Log Message:
indent: express more clearly how delimited and no-wrap comments relate

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.155 -r1.156 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.



CVS commit: src/usr.bin/indent

2023-06-07 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun  7 15:46:12 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c parse.c

Log Message:
indent: extract the stack of parser symbols to a separate struct

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.337 -r1.338 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.176 -r1.177 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.201 -r1.202 src/usr.bin/indent/io.c
cvs rdiff -u -r1.215 -r1.216 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.68 -r1.69 src/usr.bin/indent/parse.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.38 src/usr.bin/indent/debug.c:1.39
--- src/usr.bin/indent/debug.c:1.38	Wed Jun  7 15:25:08 2023
+++ src/usr.bin/indent/debug.c	Wed Jun  7 15:46:11 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.38 2023/06/07 15:25:08 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.39 2023/06/07 15:46:11 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.38 2023/06/07 15:25:08 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.39 2023/06/07 15:46:11 rillig Exp $");
 
 #include 
 
@@ -359,8 +359,10 @@ void
 debug_parse_stack(const char *situation)
 {
 	debug_printf("parse stack %s:", situation);
-	for (int i = 0; i <= ps.tos; ++i)
-		debug_printf(" %d %s", ps.s_ind_level[i], psym_name[ps.s_sym[i]]);
+	const struct psym_stack *psyms = 
+	for (int i = 0; i <= psyms->top; ++i)
+		debug_printf(" %d %s",
+		psyms->ind_level[i], psym_name[psyms->sym[i]]);
 	debug_println("");
 }
 #endif

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.337 src/usr.bin/indent/indent.c:1.338
--- src/usr.bin/indent/indent.c:1.337	Tue Jun  6 04:37:26 2023
+++ src/usr.bin/indent/indent.c	Wed Jun  7 15:46:11 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.338 2023/06/07 15:46:11 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.338 2023/06/07 15:46:11 rillig Exp $");
 
 #include 
 #include 
@@ -176,7 +176,7 @@ ind_add(int ind, const char *s, size_t l
 static void
 init_globals(void)
 {
-	ps.s_sym[0] = psym_stmt_list;
+	ps.psyms.sym[0] = psym_stmt_list;
 	ps.prev_lsym = lsym_semicolon;
 	ps.next_col_1 = true;
 	ps.lbrace_kind = psym_lbrace_block;
@@ -374,7 +374,7 @@ process_eof(void)
 		output_line();
 	}
 
-	if (ps.tos > 1)		/* check for balanced braces */
+	if (ps.psyms.top > 1)	/* check for balanced braces */
 		diag(1, "Stuff missing from end of file");
 
 	fflush(output);
@@ -415,7 +415,8 @@ process_newline(void)
 	&& lab.len == 0	/* for preprocessing lines */
 	&& com.len == 0)
 		goto stay_in_line;
-	if (ps.s_sym[ps.tos] == psym_switch_expr && opt.brace_same_line) {
+	if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr
+	&& opt.brace_same_line) {
 		ps.force_nl = true;
 		goto stay_in_line;
 	}
@@ -476,7 +477,7 @@ process_lparen(void)
 	&& opt.continuation_indent == opt.indent_size)
 		ps.extra_expr_indent = eei_yes;
 
-	if (ps.init_or_struct && ps.tos <= 2) {
+	if (ps.init_or_struct && ps.psyms.top <= 2) {
 		/* A kludge to correctly align function definitions. */
 		parse(psym_stmt);
 		ps.init_or_struct = false;
@@ -734,9 +735,9 @@ process_semicolon(void)
 static void
 process_lbrace(void)
 {
-	parser_symbol psym = ps.s_sym[ps.tos];
+	parser_symbol psym = ps.psyms.sym[ps.psyms.top];
 	if (ps.prev_lsym == lsym_rparen
-	&& ps.tos >= 2
+	&& ps.psyms.top >= 2
 	&& !(psym == psym_for_exprs || psym == psym_if_expr
 		|| psym == psym_switch_expr || psym == psym_while_expr)) {
 		ps.block_init = true;
@@ -835,14 +836,14 @@ process_rbrace(void)
 		ps.in_decl = true;
 	}
 
-	if (ps.tos == 2)
+	if (ps.psyms.top == 2)
 		out.line_kind = lk_func_end;
 
 	parse(psym_rbrace);
 
 	if (!ps.init_or_struct
-	&& ps.s_sym[ps.tos] != psym_do_stmt
-	&& ps.s_sym[ps.tos] != psym_if_expr_stmt)
+	&& ps.psyms.sym[ps.psyms.top] != psym_do_stmt
+	&& ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt)
 		ps.force_nl = true;
 }
 
@@ -877,7 +878,7 @@ process_type(void)
 {
 	parse(psym_decl);	/* let the parser worry about indentation */
 
-	if (ps.prev_lsym == lsym_rparen && ps.tos <= 1) {
+	if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1) {
 		if (code.len > 0)
 			output_line();
 	}

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.176 src/usr.bin/indent/indent.h:1.177
--- src/usr.bin/indent/indent.h:1.176	Tue Jun  6 04:37:26 2023
+++ src/usr.bin/indent/indent.h	Wed Jun  7 15:46:12 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.176 2023/06/06 04:37:26 

CVS commit: src/usr.bin/indent

2023-06-07 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun  7 15:46:12 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c parse.c

Log Message:
indent: extract the stack of parser symbols to a separate struct

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.337 -r1.338 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.176 -r1.177 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.201 -r1.202 src/usr.bin/indent/io.c
cvs rdiff -u -r1.215 -r1.216 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.68 -r1.69 src/usr.bin/indent/parse.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-07 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun  7 15:25:09 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: send all debug output to the same stream


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/indent/debug.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.37 src/usr.bin/indent/debug.c:1.38
--- src/usr.bin/indent/debug.c:1.37	Tue Jun  6 05:11:11 2023
+++ src/usr.bin/indent/debug.c	Wed Jun  7 15:25:08 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.37 2023/06/06 05:11:11 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.38 2023/06/07 15:25:08 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.37 2023/06/06 05:11:11 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.38 2023/06/07 15:25:08 rillig Exp $");
 
 #include 
 
@@ -358,9 +358,9 @@ debug_parser_state(void)
 void
 debug_parse_stack(const char *situation)
 {
-	printf("parse stack %s:", situation);
+	debug_printf("parse stack %s:", situation);
 	for (int i = 0; i <= ps.tos; ++i)
-		printf(" %d %s", ps.s_ind_level[i], psym_name[ps.s_sym[i]]);
-	printf("\n");
+		debug_printf(" %d %s", ps.s_ind_level[i], psym_name[ps.s_sym[i]]);
+	debug_println("");
 }
 #endif



CVS commit: src/usr.bin/indent

2023-06-07 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Wed Jun  7 15:25:09 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: send all debug output to the same stream


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/indent/debug.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-06 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 07:14:20 UTC 2023

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

Log Message:
indent: clean up formatting of comments

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.153 -r1.154 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.



CVS commit: src/usr.bin/indent

2023-06-06 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 07:14:20 UTC 2023

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

Log Message:
indent: clean up formatting of comments

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.153 -r1.154 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/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.153 src/usr.bin/indent/pr_comment.c:1.154
--- src/usr.bin/indent/pr_comment.c:1.153	Tue Jun  6 06:59:39 2023
+++ src/usr.bin/indent/pr_comment.c	Tue Jun  6 07:14:20 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.153 2023/06/06 06:59:39 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.154 2023/06/06 07:14:20 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.153 2023/06/06 06:59:39 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.154 2023/06/06 07:14:20 rillig Exp $");
 
 #include 
 
@@ -53,9 +53,8 @@ com_add_char(char ch)
 static void
 com_add_delim(void)
 {
-	if (!opt.star_comment_cont)
-		return;
-	buf_add_chars(, " * ", 3);
+	if (opt.star_comment_cont)
+		buf_add_chars(, " * ", 3);
 }
 
 static bool
@@ -253,16 +252,16 @@ copy_comment_wrap_finish(int line_length
 		size_t len = com.len;
 		while (ch_isblank(com.s[len - 1]))
 			len--;
-		int now_len = ind_add(ps.com_ind, com.s, len);
-		if (now_len + 3 > line_length)
+		int end_ind = ind_add(ps.com_ind, com.s, len);
+		if (end_ind + 3 > line_length)
 			output_line();
 	}
 
 	inp_p += 2;
-	if (!(com.len > 0 && ch_isblank(com.s[com.len - 1])))
-		com_add_char(' ');
-	com_add_char('*');
-	com_add_char('/');
+	if (com.len > 0 && ch_isblank(com.s[com.len - 1]))
+		buf_add_chars(, "*/", 2);
+	else
+		buf_add_chars(, " */", 3);
 }
 
 /*



CVS commit: src/usr.bin/indent

2023-06-06 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 06:59:39 UTC 2023

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

Log Message:
indent: simplify handling of comments

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.152 -r1.153 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/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.152 src/usr.bin/indent/pr_comment.c:1.153
--- src/usr.bin/indent/pr_comment.c:1.152	Tue Jun  6 06:51:44 2023
+++ src/usr.bin/indent/pr_comment.c	Tue Jun  6 06:59:39 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.152 2023/06/06 06:51:44 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.153 2023/06/06 06:59:39 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.152 2023/06/06 06:51:44 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.153 2023/06/06 06:59:39 rillig Exp $");
 
 #include 
 
@@ -226,16 +226,16 @@ copy_comment_wrap_newline(ssize_t *last_
 	++line_no;
 
 	/* flush any blanks and/or tabs at start of next line */
-	bool skip_asterisk = true;
-	do {
-		inp_skip();
-		if (inp_p[0] == '*' && skip_asterisk) {
-			skip_asterisk = false;
+	inp_skip();		/* '\n' */
+	while (ch_isblank(inp_p[0]))
+		inp_p++;
+	if (inp_p[0] == '*' && inp_p[1] == '/')
+		return false;
+	if (inp_p[0] == '*') {
+		inp_p++;
+		while (ch_isblank(inp_p[0]))
 			inp_p++;
-			if (inp_p[0] == '/')
-return false;
-		}
-	} while (ch_isblank(inp_p[0]));
+	}
 
 	return true;
 }
@@ -243,8 +243,6 @@ copy_comment_wrap_newline(ssize_t *last_
 static void
 copy_comment_wrap_finish(int line_length, bool delim)
 {
-	inp_p++;
-
 	if (delim) {
 		if (com.len > 3)
 			output_line();
@@ -260,6 +258,7 @@ copy_comment_wrap_finish(int line_length
 			output_line();
 	}
 
+	inp_p += 2;
 	if (!(com.len > 0 && ch_isblank(com.s[com.len - 1])))
 		com_add_char(' ');
 	com_add_char('*');
@@ -283,10 +282,9 @@ copy_comment_wrap(int line_length, bool 
 goto unterminated_comment;
 			if (!copy_comment_wrap_newline(_blank))
 goto end_of_comment;
-		} else if (inp_p[0] == '*' && inp_p[1] == '/') {
-			inp_p++;
+		} else if (inp_p[0] == '*' && inp_p[1] == '/')
 			goto end_of_comment;
-		} else
+		else
 			copy_comment_wrap_text(line_length, _blank);
 	}
 



CVS commit: src/usr.bin/indent

2023-06-06 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 06:59:39 UTC 2023

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

Log Message:
indent: simplify handling of comments

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.152 -r1.153 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.



CVS commit: src/usr.bin/indent

2023-06-06 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 06:51:44 UTC 2023

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

Log Message:
indent: split printing of comments into smaller functions

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.151 -r1.152 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/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.151 src/usr.bin/indent/pr_comment.c:1.152
--- src/usr.bin/indent/pr_comment.c:1.151	Sun Jun  4 20:51:19 2023
+++ src/usr.bin/indent/pr_comment.c	Tue Jun  6 06:51:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.151 2023/06/04 20:51:19 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.152 2023/06/06 06:51:44 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pr_comment.c,v 1.151 2023/06/04 20:51:19 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.152 2023/06/06 06:51:44 rillig Exp $");
 
 #include 
 
@@ -73,7 +73,8 @@ fits_in_one_line(int com_ind, int max_li
 }
 
 static void
-analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
+analyze_comment(bool *p_may_wrap, bool *p_delim,
+int *p_ind, int *p_line_length)
 {
 	bool may_wrap = true;
 	bool delim = opt.comment_delimiter_on_blankline;
@@ -134,6 +135,15 @@ analyze_comment(bool *p_may_wrap, bool *
 inp_p++;
 	}
 
+	*p_may_wrap = may_wrap;
+	*p_delim = delim;
+	*p_ind = ind;
+	*p_line_length = line_length;
+}
+
+static void
+copy_comment_start(bool may_wrap, bool *delim, int ind, int line_length)
+{
 	ps.comment_delta = 0;
 	com_add_char('/');
 	com_add_char(token.s[token.len - 1]);	/* either '*' or '/' */
@@ -141,152 +151,152 @@ analyze_comment(bool *p_may_wrap, bool *
 	if (may_wrap && !ch_isblank(inp_p[0]))
 		com_add_char(' ');
 
-	if (delim && fits_in_one_line(ind, line_length))
-		delim = false;
+	if (*delim && fits_in_one_line(ind, line_length))
+		*delim = false;
 
-	if (delim) {
+	if (*delim) {
 		output_line();
 		com_add_delim();
 	}
-
-	*p_line_length = line_length;
-	*p_delim = delim;
-	*p_may_wrap = may_wrap;
 }
 
-/*
- * Copy characters from 'inp' to 'com'. Try to keep comments from going over
- * the maximum line length. To do that, remember where the last blank, tab, or
- * newline was. When a line is filled, print up to the last blank and continue
- * copying.
- */
 static void
-copy_comment_wrap(int line_length, bool delim)
+copy_comment_wrap_text(int line_length, ssize_t *last_blank)
 {
-	ssize_t last_blank = -1;	/* index of the last blank in 'com' */
-
+	int now_len = ind_add(ps.com_ind, com.s, com.len);
 	for (;;) {
-		switch (inp_p[0]) {
-		case '\n':
-			if (had_eof) {
-diag(1, "Unterminated comment");
-output_line();
-return;
-			}
-
-			last_blank = -1;
-			if (ps.next_col_1) {
-if (com.len == 0) {
-	/* force empty line of output */
-	com_add_char(' ');
-}
-if (com.len > 3) {
-	output_line();
-	com_add_delim();
-}
-output_line();
-com_add_delim();
+		char ch = inp_next();
+		if (ch_isblank(ch))
+			*last_blank = (ssize_t)com.len;
+		com_add_char(ch);
+		now_len++;
+		if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL)
+			break;
+		if (now_len >= line_length && *last_blank != -1)
+			break;
+	}
 
-			} else {
-ps.next_col_1 = true;
-if (!(com.len > 0
-&& ch_isblank(com.s[com.len - 1])))
-	com_add_char(' ');
-last_blank = (int)com.len - 1;
-			}
-			++line_no;
+	ps.next_col_1 = false;
 
-			bool skip_asterisk = true;
-			do {	/* flush any blanks and/or tabs at start of
- * next line */
-inp_skip();
-if (inp_p[0] == '*' && skip_asterisk) {
-	skip_asterisk = false;
-	inp_p++;
-	if (inp_p[0] == '/')
-		goto end_of_comment;
-}
-			} while (ch_isblank(inp_p[0]));
+	if (now_len <= line_length)
+		return;
+	if (ch_isspace(com.s[com.len - 1]))
+		return;
 
-			break;	/* end of case for newline */
+	if (*last_blank == -1) {
+		/* only a single word in this line */
+		output_line();
+		com_add_delim();
+		return;
+	}
 
-		case '*':
-			inp_p++;
-			if (inp_p[0] == '/') {
-		end_of_comment:
-inp_p++;
+	const char *last_word_s = com.s + *last_blank + 1;
+	size_t last_word_len = com.len - (size_t)(*last_blank + 1);
+	com.len = (size_t)*last_blank;
+	output_line();
+	com_add_delim();
+
+	/* Assume that output_line and com_add_delim don't
+	 * invalidate the "unused" part of the buffer beyond
+	 * com.s + com.len. */
+	memmove(com.s + com.len, last_word_s, last_word_len);
+	com.len += last_word_len;
+	*last_blank = -1;
+}
 
-if (delim) {
-	if (com.len > 3)
-		output_line();
-	else
-		com.len = 0;
-	com_add_char(' ');
-} else {
-	size_t len = com.len;
-	while (ch_isblank(com.s[len - 1]))
-		len--;
-	int now_len = ind_add(
-	ps.com_ind, com.s, len);
-	if 

CVS commit: src/usr.bin/indent

2023-06-06 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 06:51:44 UTC 2023

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

Log Message:
indent: split printing of comments into smaller functions

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.151 -r1.152 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.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 05:27:56 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: condense code for writing tabs

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.200 -r1.201 src/usr.bin/indent/io.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/io.c
diff -u src/usr.bin/indent/io.c:1.200 src/usr.bin/indent/io.c:1.201
--- src/usr.bin/indent/io.c:1.200	Tue Jun  6 05:11:11 2023
+++ src/usr.bin/indent/io.c	Tue Jun  6 05:27:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.200 2023/06/06 05:11:11 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.201 2023/06/06 05:27:56 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.200 2023/06/06 05:11:11 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.201 2023/06/06 05:27:56 rillig Exp $");
 
 #include 
 
@@ -130,13 +130,11 @@ output_indent(int new_ind)
 	int ind = out_ind;
 
 	if (opt.use_tabs) {
-		int tabsize = opt.tabsize;
-		int n = new_ind / tabsize - ind / tabsize;
-		if (n > 0)
-			ind -= ind % tabsize;
-		for (int i = 0; i < n; i++) {
-			fputc('\t', output);
-			ind += tabsize;
+		int n = new_ind / opt.tabsize - ind / opt.tabsize;
+		if (n > 0) {
+			ind = ind - ind % opt.tabsize + n * opt.tabsize;
+			while (n-- > 0)
+fputc('\t', output);
 			wrote_newlines = 0;
 		}
 	}



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 05:27:56 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: condense code for writing tabs

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.200 -r1.201 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 05:11:11 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c io.c lexi.c parse.c

Log Message:
indent: sort functions in call order

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.199 -r1.200 src/usr.bin/indent/io.c
cvs rdiff -u -r1.214 -r1.215 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/indent/parse.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Jun  6 05:11:11 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c io.c lexi.c parse.c

Log Message:
indent: sort functions in call order

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.199 -r1.200 src/usr.bin/indent/io.c
cvs rdiff -u -r1.214 -r1.215 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/indent/parse.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.36 src/usr.bin/indent/debug.c:1.37
--- src/usr.bin/indent/debug.c:1.36	Mon Jun  5 14:40:13 2023
+++ src/usr.bin/indent/debug.c	Tue Jun  6 05:11:11 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.36 2023/06/05 14:40:13 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.37 2023/06/06 05:11:11 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.36 2023/06/05 14:40:13 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.37 2023/06/06 05:11:11 rillig Exp $");
 
 #include 
 
@@ -144,6 +144,7 @@ static const char *const decl_ptr_name[]
 
 static unsigned wrote_newlines = 1;
 
+
 void
 debug_printf(const char *fmt, ...)
 {

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.199 src/usr.bin/indent/io.c:1.200
--- src/usr.bin/indent/io.c:1.199	Tue Jun  6 04:37:26 2023
+++ src/usr.bin/indent/io.c	Tue Jun  6 05:11:11 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.200 2023/06/06 05:11:11 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.200 2023/06/06 05:11:11 rillig Exp $");
 
 #include 
 
@@ -55,22 +55,6 @@ static unsigned wrote_newlines = 2;	/* 0
 static int paren_indent;
 
 
-void
-inp_skip(void)
-{
-	inp_p++;
-	if ((size_t)(inp_p - inp.s) >= inp.len)
-		inp_read_line();
-}
-
-char
-inp_next(void)
-{
-	char ch = inp_p[0];
-	inp_skip();
-	return ch;
-}
-
 static void
 inp_read_next_line(FILE *f)
 {
@@ -95,6 +79,32 @@ inp_read_next_line(FILE *f)
 	inp_p = inp.s;
 }
 
+void
+inp_read_line(void)
+{
+	if (indent_enabled == indent_on)
+		out.indent_off_text.len = 0;
+	buf_add_chars(_off_text, inp.s, inp.len);
+	inp_read_next_line(input);
+}
+
+void
+inp_skip(void)
+{
+	inp_p++;
+	if ((size_t)(inp_p - inp.s) >= inp.len)
+		inp_read_line();
+}
+
+char
+inp_next(void)
+{
+	char ch = inp_p[0];
+	inp_skip();
+	return ch;
+}
+
+
 static void
 output_newline(void)
 {
@@ -176,6 +186,26 @@ is_blank_line_optional(void)
 	return wrote_newlines >= 3;
 }
 
+static int
+compute_case_label_indent(void)
+{
+	int i = ps.tos;
+	while (i > 0 && ps.s_sym[i] != psym_switch_expr)
+		i--;
+	float case_ind = (float)ps.s_ind_level[i] + opt.case_indent;
+	return (int)(case_ind * (float)opt.indent_size);
+}
+
+int
+compute_label_indent(void)
+{
+	if (out.line_kind == lk_case_or_default)
+		return compute_case_label_indent();
+	if (lab.s[0] == '#')
+		return 0;
+	return opt.indent_size * (ps.ind_level - 2);
+}
+
 static void
 output_line_label(void)
 {
@@ -183,6 +213,53 @@ output_line_label(void)
 	output_range(lab.s, lab.len);
 }
 
+static int
+compute_code_indent_lineup(int base_ind)
+{
+	int ind = paren_indent;
+	int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
+	if (overflow < 0)
+		return ind;
+
+	if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
+		ind -= overflow + 2;
+		if (ind > base_ind)
+			return ind;
+		return base_ind;
+	}
+
+	return ind;
+}
+
+int
+compute_code_indent(void)
+{
+	int base_ind = ps.ind_level * opt.indent_size;
+
+	if (ps.line_start_nparen == 0) {
+		if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum)
+			return base_ind;
+		if (ps.in_stmt_cont)
+			return base_ind + opt.continuation_indent;
+		return base_ind;
+	}
+
+	if (opt.lineup_to_parens) {
+		if (opt.lineup_to_parens_always)
+			return paren_indent;
+		return compute_code_indent_lineup(base_ind);
+	}
+
+	if (ps.extra_expr_indent != eei_no)
+		return base_ind + 2 * opt.continuation_indent;
+
+	if (2 * opt.continuation_indent == opt.indent_size)
+		return base_ind + opt.continuation_indent;
+	else
+		return base_ind +
+		opt.continuation_indent * ps.line_start_nparen;
+}
+
 static void
 output_line_code(void)
 {
@@ -314,79 +391,3 @@ dont_write_line:
 	ps.want_blank = false;
 	out.line_kind = lk_other;
 }
-
-static int
-compute_code_indent_lineup(int base_ind)
-{
-	int ind = paren_indent;
-	int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
-	if (overflow < 0)
-		return ind;
-
-	if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
-		ind -= overflow + 2;
-		if (ind > base_ind)
-			return ind;
-		return base_ind;
-	}
-
-	return 

CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 20:56:18 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: in 'if (expr)', the parentheses do not form a cast expression

No functional change.  When stepping through the code in debug mode, it
was just too confusing that indent would log an 'unknown cast' in this
situation.


To generate a diff of this commit:
cvs rdiff -u -r1.335 -r1.336 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.335 src/usr.bin/indent/indent.c:1.336
--- src/usr.bin/indent/indent.c:1.335	Mon Jun  5 12:05:01 2023
+++ src/usr.bin/indent/indent.c	Mon Jun  5 20:56:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.335 2023/06/05 12:05:01 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.336 2023/06/05 20:56:18 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.335 2023/06/05 12:05:01 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.336 2023/06/05 20:56:18 rillig Exp $");
 
 #include 
 #include 
@@ -489,7 +489,12 @@ process_lparen(void)
 		indent = 2 * opt.indent_size;
 
 	enum paren_level_cast cast = cast_unknown;
-	if (ps.prev_lsym == lsym_offsetof || ps.prev_lsym == lsym_sizeof
+	if (ps.prev_lsym == lsym_offsetof
+	|| ps.prev_lsym == lsym_sizeof
+	|| ps.prev_lsym == lsym_for
+	|| ps.prev_lsym == lsym_if
+	|| ps.prev_lsym == lsym_switch
+	|| ps.prev_lsym == lsym_while
 	|| ps.is_function_definition)
 		cast = cast_no;
 



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 20:56:18 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: in 'if (expr)', the parentheses do not form a cast expression

No functional change.  When stepping through the code in debug mode, it
was just too confusing that indent would log an 'unknown cast' in this
situation.


To generate a diff of this commit:
cvs rdiff -u -r1.335 -r1.336 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.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 14:40:14 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: improve layout of debug output


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/usr.bin/indent/debug.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 14:40:14 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c

Log Message:
indent: improve layout of debug output


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/usr.bin/indent/debug.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/debug.c
diff -u src/usr.bin/indent/debug.c:1.35 src/usr.bin/indent/debug.c:1.36
--- src/usr.bin/indent/debug.c:1.35	Mon Jun  5 14:22:26 2023
+++ src/usr.bin/indent/debug.c	Mon Jun  5 14:40:13 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.35 2023/06/05 14:22:26 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.36 2023/06/05 14:40:13 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.35 2023/06/05 14:22:26 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.36 2023/06/05 14:40:13 rillig Exp $");
 
 #include 
 
@@ -87,11 +87,11 @@ const char *const lsym_name[] = {
 
 const char *const psym_name[] = {
 	"-",
-	"lbrace_block",
-	"lbrace_struct",
-	"lbrace_union",
-	"lbrace_enum",
-	"rbrace",
+	"{block",
+	"{struct",
+	"{union",
+	"{enum",
+	"}",
 	"decl",
 	"stmt",
 	"stmt_list",
@@ -217,21 +217,21 @@ debug_buffers(void)
 
 #define debug_ps_bool(name) \
 	if (ps.name != prev_ps.name) \
-	debug_println("[%c] -> [%c] ps." #name, \
-		prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
+	debug_println("[%c]  ps." #name, \
+		" -+x"[(prev_ps.name ? 1 : 0) + (ps.name ? 2 : 0)]); \
 	else if (debug_full_parser_state) \
-	debug_println("   [%c] ps." #name, ps.name ? 'x' : ' ')
+	debug_println("[%c]  ps." #name, ps.name ? 'x' : ' ')
 #define debug_ps_int(name) \
 	if (ps.name != prev_ps.name) \
-	debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
+	debug_println(" %3d -> %3d  ps." #name, prev_ps.name, ps.name); \
 	else if (debug_full_parser_state) \
-	debug_println("   %3d ps." #name, ps.name)
+	debug_println("%3d  ps." #name, ps.name)
 #define debug_ps_enum(name, names) \
 	if (ps.name != prev_ps.name) \
-	debug_println("%3s -> %3s ps." #name, \
+	debug_println(" %3s -> %3s  ps." #name, \
 		(names)[prev_ps.name], (names)[ps.name]); \
 	else if (debug_full_parser_state) \
-	debug_println("%10s ps." #name, (names)[ps.name])
+	debug_println(" %10s  ps." #name, (names)[ps.name])
 
 static bool
 ps_paren_has_changed(const struct parser_state *prev_ps)
@@ -253,7 +253,7 @@ debug_ps_paren(const struct parser_state
 	if (!debug_full_parser_state && !ps_paren_has_changed(prev_ps))
 		return;
 
-	debug_printf("   ps.paren:");
+	debug_printf(" ps.paren:");
 	for (int i = 0; i < ps.nparen; i++) {
 		debug_printf(" %s%d",
 		paren_level_cast_name[ps.paren[i].cast],
@@ -282,7 +282,7 @@ debug_ps_di_stack(const struct parser_st
 	if (!debug_full_parser_state && !changed)
 		return;
 
-	debug_printf("%s ps.di_stack:", changed ? "->" : "  ");
+	debug_printf(" %s  ps.di_stack:", changed ? "->" : "  ");
 	for (int i = 0; i < ps.decl_level; i++)
 		debug_printf(" %d", ps.di_stack[i]);
 	if (ps.decl_level == 0)
@@ -296,7 +296,8 @@ debug_parser_state(void)
 	static struct parser_state prev_ps;
 
 	debug_blank_line();
-	debug_println("   ps.prev_lsym = %s", lsym_name[ps.prev_lsym]);
+	debug_println(" ps.prev_lsym = %s",
+	lsym_name[ps.prev_lsym]);
 
 	debug_println("token classification");
 	debug_ps_int(quest_level);



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 14:22:26 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.h

Log Message:
indent: sync debug output with parser state


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.174 -r1.175 src/usr.bin/indent/indent.h

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/debug.c
diff -u src/usr.bin/indent/debug.c:1.34 src/usr.bin/indent/debug.c:1.35
--- src/usr.bin/indent/debug.c:1.34	Sun Jun  4 20:51:19 2023
+++ src/usr.bin/indent/debug.c	Mon Jun  5 14:22:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.34 2023/06/04 20:51:19 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.35 2023/06/05 14:22:26 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: debug.c,v 1.34 2023/06/04 20:51:19 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.35 2023/06/05 14:22:26 rillig Exp $");
 
 #include 
 
@@ -129,6 +129,12 @@ const char *const line_kind_name[] = {
 	"case/default",
 };
 
+static const char *const extra_expr_indent_name[] = {
+	"no",
+	"yes",
+	"last",
+};
+
 static const char *const decl_ptr_name[] = {
 	"start",
 	"word",
@@ -290,51 +296,58 @@ debug_parser_state(void)
 	static struct parser_state prev_ps;
 
 	debug_blank_line();
-	debug_println("   ps.prev_lsym = %s",
-	lsym_name[ps.prev_lsym]);
-	debug_ps_bool(curr_col_1);
-	debug_ps_bool(next_col_1);
-	debug_ps_bool(next_unary);
-	debug_ps_bool(is_function_definition);
-	debug_ps_bool(want_blank);
-	debug_ps_bool(break_after_comma);
-	debug_ps_bool(force_nl);
-	debug_ps_int(line_start_nparen);
-	debug_ps_int(nparen);
-	debug_ps_paren(_ps);
-
-	debug_ps_int(comment_delta);
-	debug_ps_int(n_comment_delta);
-	debug_ps_int(com_ind);
+	debug_println("   ps.prev_lsym = %s", lsym_name[ps.prev_lsym]);
 
+	debug_println("token classification");
+	debug_ps_int(quest_level);
+	debug_ps_bool(is_function_definition);
 	debug_ps_bool(block_init);
 	debug_ps_int(block_init_level);
 	debug_ps_bool(init_or_struct);
+	debug_ps_bool(decl_on_line);
+	debug_ps_bool(in_stmt_or_decl);
+	debug_ps_bool(in_decl);
+	debug_ps_bool(in_func_def_params);
+	debug_ps_bool(seen_case);
+	debug_ps_enum(spaced_expr_psym, psym_name);
+	debug_ps_enum(lbrace_kind, psym_name);
 
+	debug_println("indentation of statements and declarations");
 	debug_ps_int(ind_level);
 	debug_ps_int(ind_level_follow);
-
+	debug_ps_bool(in_stmt_cont);
 	debug_ps_int(decl_level);
 	debug_ps_di_stack(_ps);
-	debug_ps_bool(decl_on_line);
-	debug_ps_bool(in_decl);
-	debug_ps_enum(declaration, declaration_name);
-	debug_ps_bool(blank_line_after_decl);
-	debug_ps_bool(in_func_def_params);
-	debug_ps_enum(lbrace_kind, psym_name);
-	debug_ps_enum(decl_ptr, decl_ptr_name);
 	debug_ps_bool(decl_indent_done);
 	debug_ps_int(decl_ind);
 	debug_ps_bool(tabs_to_var);
+	debug_ps_enum(extra_expr_indent, extra_expr_indent_name);
 
-	debug_ps_bool(in_stmt_or_decl);
-	debug_ps_bool(in_stmt_cont);
-	debug_ps_bool(seen_case);
+	// The parser symbol stack is printed in debug_parse_stack instead.
 
-	// The debug output for the parser symbols is done in 'parse' instead.
+	debug_println("spacing inside a statement or declaration");
+	debug_ps_bool(next_unary);
+	debug_ps_bool(want_blank);
+	debug_ps_int(line_start_nparen);
+	debug_ps_int(nparen);
+	debug_ps_paren(_ps);
+	debug_ps_enum(decl_ptr, decl_ptr_name);
+
+	debug_println("horizontal spacing for comments");
+	debug_ps_int(comment_delta);
+	debug_ps_int(n_comment_delta);
+	debug_ps_int(com_ind);
+
+	debug_println("vertical spacing");
+	debug_ps_bool(break_after_comma);
+	debug_ps_bool(force_nl);
+	debug_ps_enum(declaration, declaration_name);
+	debug_ps_bool(blank_line_after_decl);
+
+	debug_println("comments");
+	debug_ps_bool(curr_col_1);
+	debug_ps_bool(next_col_1);
 
-	debug_ps_enum(spaced_expr_psym, psym_name);
-	debug_ps_int(quest_level);
 	debug_blank_line();
 
 	prev_ps = ps;
@@ -345,7 +358,7 @@ debug_parse_stack(const char *situation)
 {
 	printf("parse stack %s:", situation);
 	for (int i = 0; i <= ps.tos; ++i)
-		printf(" %s %d", psym_name[ps.s_sym[i]], ps.s_ind_level[i]);
+		printf(" %d %s", ps.s_ind_level[i], psym_name[ps.s_sym[i]]);
 	printf("\n");
 }
 #endif

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.174 src/usr.bin/indent/indent.h:1.175
--- src/usr.bin/indent/indent.h:1.174	Mon Jun  5 12:05:01 2023
+++ src/usr.bin/indent/indent.h	Mon Jun  5 14:22:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.174 2023/06/05 12:05:01 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.175 2023/06/05 14:22:26 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -373,6 +373,7 @@ extern struct parser_state {
  * are currently open; used to indent the
  * remaining lines of the statement,
  * initializer or 

CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 14:22:26 UTC 2023

Modified Files:
src/usr.bin/indent: debug.c indent.h

Log Message:
indent: sync debug output with parser state


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.174 -r1.175 src/usr.bin/indent/indent.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 12:06:51 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: clean up comments


To generate a diff of this commit:
cvs rdiff -u -r1.197 -r1.198 src/usr.bin/indent/io.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 12:06:51 UTC 2023

Modified Files:
src/usr.bin/indent: io.c

Log Message:
indent: clean up comments


To generate a diff of this commit:
cvs rdiff -u -r1.197 -r1.198 src/usr.bin/indent/io.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/io.c
diff -u src/usr.bin/indent/io.c:1.197 src/usr.bin/indent/io.c:1.198
--- src/usr.bin/indent/io.c:1.197	Mon Jun  5 12:01:33 2023
+++ src/usr.bin/indent/io.c	Mon Jun  5 12:06:51 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.197 2023/06/05 12:01:33 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.198 2023/06/05 12:06:51 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: io.c,v 1.197 2023/06/05 12:01:33 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.198 2023/06/05 12:06:51 rillig Exp $");
 
 #include 
 
@@ -240,8 +240,6 @@ output_line_comment(void)
 /*
  * Write a line of formatted source to the output file. The line consists of
  * the label, the code and the comment.
- *
- * Comments are written directly, bypassing this function.
  */
 void
 output_line(void)
@@ -260,9 +258,9 @@ output_line(void)
 		&& out.line_kind != lk_blank)
 			output_newline();
 
+		/* This kludge aligns function definitions correctly. */
 		if (ps.ind_level == 0)
-			ps.in_stmt_cont = false;	/* this is a class A
-			 * kludge */
+			ps.in_stmt_cont = false;
 
 		if (opt.blank_line_after_decl && ps.declaration == decl_end
 		&& ps.tos > 1) {



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 12:05:01 UTC 2023

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

Log Message:
indent: format own source code


To generate a diff of this commit:
cvs rdiff -u -r1.334 -r1.335 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.173 -r1.174 src/usr.bin/indent/indent.h

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.334 src/usr.bin/indent/indent.c:1.335
--- src/usr.bin/indent/indent.c:1.334	Mon Jun  5 12:01:33 2023
+++ src/usr.bin/indent/indent.c	Mon Jun  5 12:05:01 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.334 2023/06/05 12:01:33 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.335 2023/06/05 12:05:01 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.334 2023/06/05 12:01:33 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.335 2023/06/05 12:05:01 rillig Exp $");
 
 #include 
 #include 
@@ -413,7 +413,7 @@ process_newline(void)
 	if (ps.prev_lsym == lsym_comma
 	&& ps.nparen == 0 && !ps.block_init
 	&& !opt.break_after_comma && ps.break_after_comma
-	&& lab.len == 0 /* for preprocessing lines */
+	&& lab.len == 0	/* for preprocessing lines */
 	&& com.len == 0)
 		goto stay_in_line;
 	if (ps.s_sym[ps.tos] == psym_switch_expr && opt.brace_same_line) {
@@ -1167,7 +1167,7 @@ process_lsym(lexer_symbol lsym)
 	case lsym_funcname:
 	case lsym_return:
 		process_ident(lsym);
-	copy_token:
+copy_token:
 		if (ps.want_blank)
 			buf_add_char(, ' ');
 		buf_add_buf(, );

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.173 src/usr.bin/indent/indent.h:1.174
--- src/usr.bin/indent/indent.h:1.173	Mon Jun  5 10:12:21 2023
+++ src/usr.bin/indent/indent.h	Mon Jun  5 12:05:01 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.173 2023/06/05 10:12:21 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.174 2023/06/05 12:05:01 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -89,7 +89,7 @@ typedef enum lexer_symbol {
 	lsym_colon_other,	/* bit-fields, generic-association (C11),
  * enum-type-specifier (C23),
  * attribute-prefixed-token (C23),
- * pp-prefixed-parameter (C23 6.10)*/
+ * pp-prefixed-parameter (C23 6.10) */
 	lsym_comma,
 	lsym_semicolon,
 	lsym_typedef,
@@ -320,8 +320,8 @@ extern struct parser_state {
  * processing of braces is then slightly
  * different */
 	bool in_func_def_params;
-	bool seen_case;		/* whether there was a 'case' or 'default',
- * to properly space the following ':' */
+	bool seen_case;		/* whether there was a 'case' or 'default', to
+ * properly space the following ':' */
 	parser_symbol spaced_expr_psym;	/* the parser symbol to be shifted
 	 * after the parenthesized expression
 	 * from a 'for', 'if', 'switch' or
@@ -374,13 +374,13 @@ extern struct parser_state {
  * remaining lines of the statement,
  * initializer or declaration */
 	enum {
-	dp_start,		/* the beginning of a declaration */
-	dp_word,		/* seen a type name */
-	dp_word_asterisk,	/* seen a type name and some '*' */
-	dp_other,
-	} decl_ptr;		/* detects declarations like 'typename *x',
- * to prevent the '*' from being interpreted as
- * a binary operator */
+		dp_start,	/* the beginning of a declaration */
+		dp_word,	/* seen a type name */
+		dp_word_asterisk,	/* seen a type name and some '*' */
+		dp_other,
+	} decl_ptr;		/* detects declarations like 'typename *x', to
+ * prevent the '*' from being interpreted as a
+ * binary operator */
 	paren_level_props paren[20];
 
 	/* Horizontal spacing for comments */



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 12:05:01 UTC 2023

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

Log Message:
indent: format own source code


To generate a diff of this commit:
cvs rdiff -u -r1.334 -r1.335 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.173 -r1.174 src/usr.bin/indent/indent.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 09:10:31 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: make heuristics for '*' pointer types simpler

Previously, a '}' token did not reset the state machine, but it should.


To generate a diff of this commit:
cvs rdiff -u -r1.330 -r1.331 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.



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 09:10:31 UTC 2023

Modified Files:
src/usr.bin/indent: indent.c

Log Message:
indent: make heuristics for '*' pointer types simpler

Previously, a '}' token did not reset the state machine, but it should.


To generate a diff of this commit:
cvs rdiff -u -r1.330 -r1.331 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.330 src/usr.bin/indent/indent.c:1.331
--- src/usr.bin/indent/indent.c:1.330	Mon Jun  5 08:22:00 2023
+++ src/usr.bin/indent/indent.c	Mon Jun  5 09:10:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.330 2023/06/05 08:22:00 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.331 2023/06/05 09:10:31 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.330 2023/06/05 08:22:00 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.331 2023/06/05 09:10:31 rillig Exp $");
 
 #include 
 #include 
@@ -336,38 +336,20 @@ code_add_decl_indent(int decl_ind, bool 
 static void
 update_ps_decl_ptr(lexer_symbol lsym)
 {
-	switch (ps.decl_ptr) {
-	case dp_start:
-		if (lsym == lsym_modifier)
-			ps.decl_ptr = dp_start;
-		else if (lsym == lsym_type_outside_parentheses)
-			ps.decl_ptr = dp_word;
-		else if (lsym == lsym_word)
-			ps.decl_ptr = dp_word;
-		else
-			ps.decl_ptr = dp_other;
-		break;
-	case dp_word:
-		if (lsym == lsym_unary_op && token.s[0] == '*')
-			ps.decl_ptr = dp_word_asterisk;
-		else
-			ps.decl_ptr = dp_other;
-		break;
-	case dp_word_asterisk:
-		if (lsym == lsym_unary_op && token.s[0] == '*')
-			ps.decl_ptr = dp_word_asterisk;
-		else
-			ps.decl_ptr = dp_other;
-		break;
-	case dp_other:
-		if (lsym == lsym_semicolon || lsym == lsym_rbrace)
-			ps.decl_ptr = dp_start;
-		if (lsym == lsym_lparen && ps.prev_lsym != lsym_sizeof)
-			ps.decl_ptr = dp_start;
-		if (lsym == lsym_comma && ps.in_decl)
-			ps.decl_ptr = dp_start;
-		break;
-	}
+	if (lsym == lsym_semicolon
+	|| lsym == lsym_lbrace
+	|| lsym == lsym_rbrace
+	|| (lsym == lsym_lparen && ps.prev_lsym != lsym_sizeof)
+	|| (lsym == lsym_comma && ps.in_decl)
+	|| lsym == lsym_modifier)
+		ps.decl_ptr = dp_start;
+	else if (ps.decl_ptr == dp_start && lsym == lsym_word)
+		ps.decl_ptr = dp_word;
+	else if ((ps.decl_ptr == dp_word || ps.decl_ptr == dp_word_asterisk)
+	&& (lsym == lsym_unary_op && token.s[0] == '*'))
+		ps.decl_ptr = dp_word_asterisk;
+	else
+		ps.decl_ptr = dp_other;
 }
 
 static void



CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 07:35:05 UTC 2023

Modified Files:
src/usr.bin/indent: args.c indent.c indent.h parse.c

Log Message:
indent: rename variables, clean up comments

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/usr.bin/indent/args.c
cvs rdiff -u -r1.328 -r1.329 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.171 -r1.172 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.65 -r1.66 src/usr.bin/indent/parse.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/args.c
diff -u src/usr.bin/indent/args.c:1.80 src/usr.bin/indent/args.c:1.81
--- src/usr.bin/indent/args.c:1.80	Thu May 18 06:01:39 2023
+++ src/usr.bin/indent/args.c	Mon Jun  5 07:35:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: args.c,v 1.80 2023/05/18 06:01:39 rillig Exp $	*/
+/*	$NetBSD: args.c,v 1.81 2023/06/05 07:35:05 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: args.c,v 1.80 2023/05/18 06:01:39 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.81 2023/06/05 07:35:05 rillig Exp $");
 
 /* Read options from profile files and from the command line. */
 
@@ -91,9 +91,9 @@ static const struct pro {
 	bool_options("cs", space_after_cast),
 	int_option("d", unindent_displace, -999, 999),
 	int_option("di", decl_indent, 0, 999),
-	bool_options("dj", ljust_decl),
+	bool_options("dj", left_justify_decl),
 	bool_options("eei", extra_expr_indent),
-	bool_options("ei", else_if),
+	bool_options("ei", else_if_in_same_line),
 	bool_options("fbs", function_brace_split),
 	bool_options("fc1", format_col1_comments),
 	bool_options("fcb", format_block_comments),

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.328 src/usr.bin/indent/indent.c:1.329
--- src/usr.bin/indent/indent.c:1.328	Mon Jun  5 07:23:03 2023
+++ src/usr.bin/indent/indent.c	Mon Jun  5 07:35:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.328 2023/06/05 07:23:03 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.329 2023/06/05 07:35:05 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: indent.c,v 1.328 2023/06/05 07:23:03 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.329 2023/06/05 07:35:05 rillig Exp $");
 
 #include 
 #include 
@@ -57,7 +57,7 @@ struct options opt = {
 	.cuddle_else = true,
 	.comment_column = 33,
 	.decl_indent = 16,
-	.else_if = true,
+	.else_if_in_same_line = true,
 	.function_brace_split = true,
 	.format_col1_comments = true,
 	.format_block_comments = true,
@@ -287,7 +287,7 @@ parse_command_line(int argc, char **argv
 	if (opt.local_decl_indent < 0)
 		opt.local_decl_indent = opt.decl_indent;
 	if (opt.decl_comment_column <= 0)
-		opt.decl_comment_column = opt.ljust_decl
+		opt.decl_comment_column = opt.left_justify_decl
 		? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
 		: opt.comment_column;
 	if (opt.continuation_indent == 0)
@@ -500,9 +500,7 @@ process_lparen(void)
 		ps.extra_expr_indent = eei_yes;
 
 	if (ps.init_or_struct && ps.tos <= 2) {
-		/* this is a kluge to make sure that declarations will be
-		 * aligned right if proc decl has an explicit type on it, i.e.
-		 * "int a(x) {..." */
+		/* A kludge to correctly align function definitions. */
 		parse(psym_stmt);
 		ps.init_or_struct = false;
 	}
@@ -1241,7 +1239,7 @@ indent(void)
 			return process_eof();
 
 		if (lsym == lsym_if && ps.prev_lsym == lsym_else
-		&& opt.else_if)
+		&& opt.else_if_in_same_line)
 			ps.force_nl = false;
 
 		if (lsym == lsym_newline || lsym == lsym_preprocessing)

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.171 src/usr.bin/indent/indent.h:1.172
--- src/usr.bin/indent/indent.h:1.171	Sun Jun  4 20:51:19 2023
+++ src/usr.bin/indent/indent.h	Mon Jun  5 07:35:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.171 2023/06/04 20:51:19 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.172 2023/06/05 07:35:05 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -201,17 +201,16 @@ extern struct options {
 	int comment_column;	/* the column in which comments to the right of
  * code should start */
 	int decl_indent;	/* indentation of identifier in declaration */
-	bool ljust_decl;	/* true if declarations should be left
- * justified */
+	bool left_justify_decl;
 	int unindent_displace;	/* comments not to the right of code will be
  * placed this many indentation levels to the
  * left of code */
 	bool extra_expr_indent;	/* whether continuation lines from the
  * expression part of "if (e)", "while (e)",
  * "for (e; e; e)" should be indented an extra
- * tab stop so that they don't conflict with
+ * tab stop so that they are not confused with
  * the code that follows */
-	bool else_if;		/* whether else-if pairs use the same line */
+	bool 

CVS commit: src/usr.bin/indent

2023-06-05 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Jun  5 07:35:05 UTC 2023

Modified Files:
src/usr.bin/indent: args.c indent.c indent.h parse.c

Log Message:
indent: rename variables, clean up comments

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/usr.bin/indent/args.c
cvs rdiff -u -r1.328 -r1.329 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.171 -r1.172 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.65 -r1.66 src/usr.bin/indent/parse.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



  1   2   3   4   5   6   7   8   9   >