Module Name: src Committed By: rillig Date: Fri Jun 9 22:01:26 UTC 2023
Modified Files: src/tests/usr.bin/indent: lsym_newline.c src/usr.bin/indent: indent.c indent.h io.c Log Message: indent: trim trailing blank lines To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/lsym_newline.c cvs rdiff -u -r1.347 -r1.348 src/usr.bin/indent/indent.c cvs rdiff -u -r1.182 -r1.183 src/usr.bin/indent/indent.h cvs rdiff -u -r1.207 -r1.208 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/tests/usr.bin/indent/lsym_newline.c diff -u src/tests/usr.bin/indent/lsym_newline.c:1.4 src/tests/usr.bin/indent/lsym_newline.c:1.5 --- src/tests/usr.bin/indent/lsym_newline.c:1.4 Sun Apr 24 10:36:37 2022 +++ src/tests/usr.bin/indent/lsym_newline.c Fri Jun 9 22:01:26 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_newline.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */ +/* $NetBSD: lsym_newline.c,v 1.5 2023/06/09 22:01:26 rillig Exp $ */ /* * Tests for the token lsym_newline, which represents a forced line break in @@ -32,3 +32,15 @@ int var = + 3 + 4; //indent end + + +// Trim trailing blank lines. +//indent input +int x; + + +//indent end + +//indent run -di0 +int x; +//indent end Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.347 src/usr.bin/indent/indent.c:1.348 --- src/usr.bin/indent/indent.c:1.347 Fri Jun 9 16:23:43 2023 +++ src/usr.bin/indent/indent.c Fri Jun 9 22:01:26 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -83,7 +83,6 @@ struct buffer com; bool found_err; bool had_eof; int line_no = 1; -enum indent_enabled indent_enabled; static int ifdef_level; static struct parser_state state_stack[5]; @@ -380,17 +379,11 @@ is_function_pointer_declaration(void) static int process_eof(void) { - if (lab.len > 0 || code.len > 0 || com.len > 0) - output_line(); - if (indent_enabled != indent_on) { - indent_enabled = indent_last_off_line; - output_line(); - } + output_finish(); if (ps.psyms.top > 1) /* check for balanced braces */ diag(1, "Stuff missing from end of file"); - fflush(output); return found_err ? EXIT_FAILURE : EXIT_SUCCESS; } Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.182 src/usr.bin/indent/indent.h:1.183 --- src/usr.bin/indent/indent.h:1.182 Fri Jun 9 16:23:43 2023 +++ src/usr.bin/indent/indent.h Fri Jun 9 22:01:26 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.182 2023/06/09 16:23:43 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.183 2023/06/09 22:01:26 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -472,6 +472,7 @@ int ind_add(int, const char *, size_t); void inp_skip(void); char inp_next(void); +void output_finish(void); lexer_symbol lexi(void); void diag(int, const char *, ...) __printflike(2, 3); Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.207 src/usr.bin/indent/io.c:1.208 --- src/usr.bin/indent/io.c:1.207 Fri Jun 9 08:10:58 2023 +++ src/usr.bin/indent/io.c Fri Jun 9 22:01:26 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.207 2023/06/09 08:10:58 rillig Exp $ */ +/* $NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: io.c,v 1.207 2023/06/09 08:10:58 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $"); #include <stdio.h> @@ -48,10 +48,12 @@ struct buffer inp; 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 int paren_indent; @@ -108,15 +110,24 @@ inp_next(void) static void output_newline(void) { - fputc('\n', output); - debug_println("output_newline"); + buffered_blank_lines++; wrote_newlines++; out_ind = 0; } static void +write_buffered_blank_lines(void) +{ + for (; buffered_blank_lines > 0; buffered_blank_lines--) { + fputc('\n', output); + debug_println("output_newline"); + } +} + +static void output_range(const char *s, size_t len) { + write_buffered_blank_lines(); fwrite(s, 1, len, output); debug_vis_range("output_range \"", s, len, "\"\n"); for (size_t i = 0; i < len; i++) @@ -127,6 +138,8 @@ output_range(const char *s, size_t len) static void output_indent(int new_ind) { + write_buffered_blank_lines(); + int ind = out_ind; if (opt.use_tabs) { @@ -148,6 +161,22 @@ 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) {