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 <sys/cdefs.h> -__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 <stdarg.h> #include <string.h> @@ -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", &lab); debug_print_buf("code", &code); debug_print_buf("comment", &com); - 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 <sys/cdefs.h> -__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 <stdio.h> @@ -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; + return newlines >= 1; if (ps.psyms.top >= 2) - return wrote_newlines >= 2; - return wrote_newlines >= 3; + return newlines >= 2; + return newlines >= 3; } static int @@ -311,7 +312,7 @@ output_line_comment(void) } if (out_ind > target_ind) - write_newline(); + buffer_newline(); while (com.s + com.len > p && ch_isspace(com.s[com.len - 1])) com.len--; @@ -328,9 +329,9 @@ 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 + if (want_blank_line() && newlines < 2 && out.line_kind != lk_blank) - write_newline(); + buffer_newline(); /* This kludge aligns function definitions correctly. */ if (ps.ind_level == 0) @@ -353,8 +354,10 @@ output_line_indented(void) output_line_code(); if (com.len > 0) output_line_comment(); + buffer_newline(); + if (out.line_kind != lk_blank) + write_buffered_newlines(); - write_newline(); out.prev_line_kind = out.line_kind; } @@ -407,13 +410,8 @@ output_line(void) 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 { + output_line(); + if (indent_enabled != indent_on) { indent_enabled = indent_last_off_line; output_line(); }