Fri Jun 9 07:20:31 2023 UTC ()
indent: format its own code


(rillig)
diff -r1.341 -r1.342 src/usr.bin/indent/indent.c
diff -r1.205 -r1.206 src/usr.bin/indent/io.c
diff -r1.217 -r1.218 src/usr.bin/indent/lexi.c
diff -r1.69 -r1.70 src/usr.bin/indent/parse.c
diff -r1.157 -r1.158 src/usr.bin/indent/pr_comment.c

cvs diff -r1.341 -r1.342 src/usr.bin/indent/indent.c (expand / switch to unified diff)

--- src/usr.bin/indent/indent.c 2023/06/08 21:18:54 1.341
+++ src/usr.bin/indent/indent.c 2023/06/09 07:20:30 1.342
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.341 2023/06/08 21:18:54 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.342 2023/06/09 07:20:30 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993 8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved. 9 * The Regents of the University of California. All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: indent.c,v 1.341 2023/06/08 21:18:54 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.342 2023/06/09 07:20:30 rillig Exp $");
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <err.h> 44#include <err.h>
45#include <fcntl.h> 45#include <fcntl.h>
46#include <stdarg.h> 46#include <stdarg.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <stdlib.h> 48#include <stdlib.h>
49#include <string.h> 49#include <string.h>
50#include <unistd.h> 50#include <unistd.h>
51 51
52#include "indent.h" 52#include "indent.h"
53 53
54struct options opt = { 54struct options opt = {
@@ -559,27 +559,27 @@ process_rbracket(void) @@ -559,27 +559,27 @@ process_rbracket(void)
559 559
560unbalanced: 560unbalanced:
561 buf_add_char(&code, token.s[0]); 561 buf_add_char(&code, token.s[0]);
562} 562}
563 563
564static void 564static void
565process_unary_op(void) 565process_unary_op(void)
566{ 566{
567 if (is_function_pointer_declaration()) { 567 if (is_function_pointer_declaration()) {
568 int ind = ps.decl_ind - (int)token.len; 568 int ind = ps.decl_ind - (int)token.len;
569 indent_declarator(ind, ps.tabs_to_var); 569 indent_declarator(ind, ps.tabs_to_var);
570 ps.want_blank = false; 570 ps.want_blank = false;
571 } else if ((token.s[0] == '+' || token.s[0] == '-') 571 } else if ((token.s[0] == '+' || token.s[0] == '-')
572 && code.len > 0 && code.s[code.len - 1] == token.s[0]) 572 && code.len > 0 && code.s[code.len - 1] == token.s[0])
573 ps.want_blank = true; 573 ps.want_blank = true;
574 574
575 if (ps.want_blank) 575 if (ps.want_blank)
576 buf_add_char(&code, ' '); 576 buf_add_char(&code, ' ');
577 buf_add_buf(&code, &token); 577 buf_add_buf(&code, &token);
578 ps.want_blank = false; 578 ps.want_blank = false;
579} 579}
580 580
581static void 581static void
582process_postfix_op(void) 582process_postfix_op(void)
583{ 583{
584 buf_add_buf(&code, &token); 584 buf_add_buf(&code, &token);
585 ps.want_blank = true; 585 ps.want_blank = true;
@@ -672,27 +672,27 @@ process_semicolon(void) @@ -672,27 +672,27 @@ process_semicolon(void)
672 if (ps.spaced_expr_psym == psym_0) { 672 if (ps.spaced_expr_psym == psym_0) {
673 parse(psym_stmt); 673 parse(psym_stmt);
674 ps.force_nl = true; 674 ps.force_nl = true;
675 } 675 }
676} 676}
677 677
678static void 678static void
679process_lbrace(void) 679process_lbrace(void)
680{ 680{
681 parser_symbol psym = ps.psyms.sym[ps.psyms.top]; 681 parser_symbol psym = ps.psyms.sym[ps.psyms.top];
682 if (ps.prev_lsym == lsym_rparen 682 if (ps.prev_lsym == lsym_rparen
683 && ps.psyms.top >= 2 683 && ps.psyms.top >= 2
684 && !(psym == psym_for_exprs || psym == psym_if_expr 684 && !(psym == psym_for_exprs || psym == psym_if_expr
685 || psym == psym_switch_expr || psym == psym_while_expr)) { 685 || psym == psym_switch_expr || psym == psym_while_expr)) {
686 ps.block_init = true; 686 ps.block_init = true;
687 ps.init_or_struct = true; 687 ps.init_or_struct = true;
688 } 688 }
689 689
690 if (out.line_kind == lk_stmt_head) 690 if (out.line_kind == lk_stmt_head)
691 out.line_kind = lk_other; 691 out.line_kind = lk_other;
692 692
693 ps.in_stmt_or_decl = false; /* don't indent the {} */ 693 ps.in_stmt_or_decl = false; /* don't indent the {} */
694 694
695 if (!ps.block_init) 695 if (!ps.block_init)
696 ps.force_nl = true; 696 ps.force_nl = true;
697 else 697 else
698 ps.block_init_level++; 698 ps.block_init_level++;
@@ -891,28 +891,28 @@ process_comma(void) @@ -891,28 +891,28 @@ process_comma(void)
891 if (ps.in_decl && !ps.is_function_definition && !ps.block_init && 891 if (ps.in_decl && !ps.is_function_definition && !ps.block_init &&
892 !ps.decl_indent_done && ps.line_start_nparen == 0) { 892 !ps.decl_indent_done && ps.line_start_nparen == 0) {
893 /* indent leading commas and not the actual identifiers */ 893 /* indent leading commas and not the actual identifiers */
894 indent_declarator(ps.decl_ind - 1, ps.tabs_to_var); 894 indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
895 } 895 }
896 896
897 buf_add_char(&code, ','); 897 buf_add_char(&code, ',');
898 898
899 if (ps.nparen == 0) { 899 if (ps.nparen == 0) {
900 if (ps.block_init_level == 0) 900 if (ps.block_init_level == 0)
901 ps.block_init = false; 901 ps.block_init = false;
902 int typical_varname_length = 8; 902 int typical_varname_length = 8;
903 if (ps.break_after_comma && (opt.break_after_comma || 903 if (ps.break_after_comma && (opt.break_after_comma ||
904 ind_add(compute_code_indent(), code.s, code.len) 904 ind_add(compute_code_indent(), code.s, code.len)
905 >= opt.max_line_length - typical_varname_length)) 905 >= opt.max_line_length - typical_varname_length))
906 ps.force_nl = true; 906 ps.force_nl = true;
907 } 907 }
908} 908}
909 909
910/* move the whole line to the 'label' buffer */ 910/* move the whole line to the 'label' buffer */
911static void 911static void
912read_preprocessing_line(void) 912read_preprocessing_line(void)
913{ 913{
914 enum { 914 enum {
915 PLAIN, STR, CHR, COMM 915 PLAIN, STR, CHR, COMM
916 } state = PLAIN; 916 } state = PLAIN;
917 917
918 buf_add_char(&lab, '#'); 918 buf_add_char(&lab, '#');

cvs diff -r1.205 -r1.206 src/usr.bin/indent/io.c (expand / switch to unified diff)

--- src/usr.bin/indent/io.c 2023/06/09 06:36:57 1.205
+++ src/usr.bin/indent/io.c 2023/06/09 07:20:30 1.206
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.205 2023/06/09 06:36:57 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: io.c,v 1.205 2023/06/09 06:36:57 rillig Exp $"); 41__RCSID("$NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $");
42 42
43#include <stdio.h> 43#include <stdio.h>
44 44
45#include "indent.h" 45#include "indent.h"
46 46
47struct buffer inp; 47struct buffer inp;
48const char *inp_p; 48const char *inp_p;
49 49
50struct output_state out; 50struct output_state out;
51static int out_ind; /* width of the line that is being written */ 51static int out_ind; /* width of the line that is being written */
52static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a 52static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a
53 * single '\n', > 1 means there were (n 53 * single '\n', > 1 means there were (n
54 * - 1) blank lines above */ 54 * - 1) blank lines above */
@@ -365,26 +365,26 @@ output_line(void) @@ -365,26 +365,26 @@ output_line(void)
365prepare_next_line: 365prepare_next_line:
366 lab.len = 0; 366 lab.len = 0;
367 code.len = 0; 367 code.len = 0;
368 com.len = 0; 368 com.len = 0;
369 369
370 ps.decl_on_line = ps.in_decl; 370 ps.decl_on_line = ps.in_decl;
371 // XXX: don't reset in_stmt_cont here; see process_colon_question. 371 // XXX: don't reset in_stmt_cont here; see process_colon_question.
372 ps.in_stmt_cont = ps.in_stmt_or_decl 372 ps.in_stmt_cont = ps.in_stmt_or_decl
373 && !ps.in_decl && ps.block_init_level == 0; 373 && !ps.in_decl && ps.block_init_level == 0;
374 ps.decl_indent_done = false; 374 ps.decl_indent_done = false;
375 if (ps.extra_expr_indent == eei_last) 375 if (ps.extra_expr_indent == eei_last)
376 ps.extra_expr_indent = eei_no; 376 ps.extra_expr_indent = eei_no;
377 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else 377 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else
378 && ps.nparen > 0)) 378 && ps.nparen > 0))
379 ps.ind_level = ps.ind_level_follow; 379 ps.ind_level = ps.ind_level_follow;
380 ps.line_start_nparen = ps.nparen; 380 ps.line_start_nparen = ps.nparen;
381 ps.want_blank = false; 381 ps.want_blank = false;
382 382
383 if (ps.nparen > 0) { 383 if (ps.nparen > 0) {
384 /* TODO: explain what negative indentation means */ 384 /* TODO: explain what negative indentation means */
385 paren_indent = -1 - ps.paren[ps.nparen - 1].indent; 385 paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
386 debug_println("paren_indent is now %d", paren_indent); 386 debug_println("paren_indent is now %d", paren_indent);
387 } 387 }
388 388
389 out.line_kind = lk_other; 389 out.line_kind = lk_other;
390} 390}

cvs diff -r1.217 -r1.218 src/usr.bin/indent/lexi.c (expand / switch to unified diff)

--- src/usr.bin/indent/lexi.c 2023/06/08 21:18:54 1.217
+++ src/usr.bin/indent/lexi.c 2023/06/09 07:20:30 1.218
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lexi.c,v 1.217 2023/06/08 21:18:54 rillig Exp $ */ 1/* $NetBSD: lexi.c,v 1.218 2023/06/09 07:20:30 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: lexi.c,v 1.217 2023/06/08 21:18:54 rillig Exp $"); 41__RCSID("$NetBSD: lexi.c,v 1.218 2023/06/09 07:20:30 rillig Exp $");
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45 45
46#include "indent.h" 46#include "indent.h"
47 47
48/* In lexi_alnum, this constant marks a type, independent of parentheses. */ 48/* In lexi_alnum, this constant marks a type, independent of parentheses. */
49#define lsym_type lsym_type_outside_parentheses 49#define lsym_type lsym_type_outside_parentheses
50 50
51/* must be sorted alphabetically, is used in binary search */ 51/* must be sorted alphabetically, is used in binary search */
52static const struct keyword { 52static const struct keyword {
53 const char name[12]; 53 const char name[12];
54 lexer_symbol lsym; 54 lexer_symbol lsym;
@@ -679,25 +679,25 @@ lexi(void) @@ -679,25 +679,25 @@ lexi(void)
679 } 679 }
680 680
681 ps.next_unary = next_unary; 681 ps.next_unary = next_unary;
682 682
683 return lsym; 683 return lsym;
684} 684}
685 685
686void 686void
687register_typename(const char *name) 687register_typename(const char *name)
688{ 688{
689 if (typenames.len >= typenames.cap) { 689 if (typenames.len >= typenames.cap) {
690 typenames.cap = 16 + 2 * typenames.cap; 690 typenames.cap = 16 + 2 * typenames.cap;
691 typenames.items = nonnull(realloc(typenames.items, 691 typenames.items = nonnull(realloc(typenames.items,
692 sizeof(typenames.items[0]) * typenames.cap)); 692 sizeof(typenames.items[0]) * typenames.cap));
693 } 693 }
694 694
695 int pos = bsearch_typenames(name); 695 int pos = bsearch_typenames(name);
696 if (pos >= 0) 696 if (pos >= 0)
697 return; /* already in the list */ 697 return; /* already in the list */
698 698
699 pos = -(pos + 1); 699 pos = -(pos + 1);
700 memmove(typenames.items + pos + 1, typenames.items + pos, 700 memmove(typenames.items + pos + 1, typenames.items + pos,
701 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos)); 701 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
702 typenames.items[pos] = nonnull(strdup(name)); 702 typenames.items[pos] = nonnull(strdup(name));
703} 703}

cvs diff -r1.69 -r1.70 src/usr.bin/indent/parse.c (expand / switch to unified diff)

--- src/usr.bin/indent/parse.c 2023/06/07 15:46:12 1.69
+++ src/usr.bin/indent/parse.c 2023/06/09 07:20:30 1.70
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: parse.c,v 1.69 2023/06/07 15:46:12 rillig Exp $ */ 1/* $NetBSD: parse.c,v 1.70 2023/06/09 07:20:30 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: parse.c,v 1.69 2023/06/07 15:46:12 rillig Exp $"); 41__RCSID("$NetBSD: parse.c,v 1.70 2023/06/09 07:20:30 rillig Exp $");
42 42
43#include <err.h> 43#include <err.h>
44 44
45#include "indent.h" 45#include "indent.h"
46 46
47/* 47/*
48 * Try to combine the statement on the top of the parse stack with the symbol 48 * Try to combine the statement on the top of the parse stack with the symbol
49 * directly below it, replacing these two symbols with a single symbol. 49 * directly below it, replacing these two symbols with a single symbol.
50 */ 50 */
51static bool 51static bool
52psyms_reduce_stmt(struct psym_stack *psyms) 52psyms_reduce_stmt(struct psym_stack *psyms)
53{ 53{
54 switch (psyms->sym[psyms->top - 1]) { 54 switch (psyms->sym[psyms->top - 1]) {
@@ -226,27 +226,27 @@ parse(parser_symbol psym) @@ -226,27 +226,27 @@ parse(parser_symbol psym)
226 case psym_else: 226 case psym_else:
227 if (psyms->sym[psyms->top] != psym_if_expr_stmt) { 227 if (psyms->sym[psyms->top] != psym_if_expr_stmt) {
228 diag(1, "Unmatched 'else'"); 228 diag(1, "Unmatched 'else'");
229 break; 229 break;
230 } 230 }
231 ps.ind_level = psyms->ind_level[psyms->top]; 231 ps.ind_level = psyms->ind_level[psyms->top];
232 ps.ind_level_follow = ps.ind_level + 1; 232 ps.ind_level_follow = ps.ind_level + 1;
233 psyms->sym[psyms->top] = psym_if_expr_stmt_else; 233 psyms->sym[psyms->top] = psym_if_expr_stmt_else;
234 break; 234 break;
235 235
236 case psym_rbrace: 236 case psym_rbrace:
237 /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ 237 /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
238 if (!(psyms->top > 0 238 if (!(psyms->top > 0
239 && is_lbrace(psyms->sym[psyms->top - 1]))) { 239 && is_lbrace(psyms->sym[psyms->top - 1]))) {
240 diag(1, "Statement nesting error"); 240 diag(1, "Statement nesting error");
241 break; 241 break;
242 } 242 }
243 ps.ind_level = ps.ind_level_follow = 243 ps.ind_level = ps.ind_level_follow =
244 psyms->ind_level[--psyms->top]; 244 psyms->ind_level[--psyms->top];
245 psyms->sym[psyms->top] = psym_stmt; 245 psyms->sym[psyms->top] = psym_stmt;
246 break; 246 break;
247 247
248 case psym_switch_expr: 248 case psym_switch_expr:
249 ps_push_follow(psym_switch_expr); 249 ps_push_follow(psym_switch_expr);
250 ps.ind_level_follow += (int)opt.case_indent + 1; 250 ps.ind_level_follow += (int)opt.case_indent + 1;
251 break; 251 break;
252 252

cvs diff -r1.157 -r1.158 src/usr.bin/indent/pr_comment.c (expand / switch to unified diff)

--- src/usr.bin/indent/pr_comment.c 2023/06/09 07:18:52 1.157
+++ src/usr.bin/indent/pr_comment.c 2023/06/09 07:20:30 1.158
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pr_comment.c,v 1.157 2023/06/09 07:18:52 rillig Exp $ */ 1/* $NetBSD: pr_comment.c,v 1.158 2023/06/09 07:20:30 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: pr_comment.c,v 1.157 2023/06/09 07:18:52 rillig Exp $"); 41__RCSID("$NetBSD: pr_comment.c,v 1.158 2023/06/09 07:20:30 rillig Exp $");
42 42
43#include <string.h> 43#include <string.h>
44 44
45#include "indent.h" 45#include "indent.h"
46 46
47static void 47static void
48com_add_char(char ch) 48com_add_char(char ch)
49{ 49{
50 buf_add_char(&com, ch); 50 buf_add_char(&com, ch);
51} 51}
52 52
53static void 53static void
54com_add_delim(void) 54com_add_delim(void)
@@ -188,29 +188,28 @@ copy_comment_wrap_text(int line_length,  @@ -188,29 +188,28 @@ copy_comment_wrap_text(int line_length,
188 if (*last_blank == -1) { 188 if (*last_blank == -1) {
189 /* only a single word in this line */ 189 /* only a single word in this line */
190 output_line(); 190 output_line();
191 com_add_delim(); 191 com_add_delim();
192 return; 192 return;
193 } 193 }
194 194
195 const char *last_word_s = com.s + *last_blank + 1; 195 const char *last_word_s = com.s + *last_blank + 1;
196 size_t last_word_len = com.len - (size_t)(*last_blank + 1); 196 size_t last_word_len = com.len - (size_t)(*last_blank + 1);
197 com.len = (size_t)*last_blank; 197 com.len = (size_t)*last_blank;
198 output_line(); 198 output_line();
199 com_add_delim(); 199 com_add_delim();
200 200
201 /* Assume that output_line and com_add_delim don't 201 /* Assume that output_line and com_add_delim don't invalidate the
202 * invalidate the "unused" part of the buffer beyond 202 * "unused" part of the buffer beyond com.s + com.len. */
203 * com.s + com.len. */ 
204 memmove(com.s + com.len, last_word_s, last_word_len); 203 memmove(com.s + com.len, last_word_s, last_word_len);
205 com.len += last_word_len; 204 com.len += last_word_len;
206 *last_blank = -1; 205 *last_blank = -1;
207} 206}
208 207
209static bool 208static bool
210copy_comment_wrap_newline(ssize_t *last_blank) 209copy_comment_wrap_newline(ssize_t *last_blank)
211{ 210{
212 *last_blank = -1; 211 *last_blank = -1;
213 if (ps.next_col_1) { 212 if (ps.next_col_1) {
214 if (com.len == 0) 213 if (com.len == 0)
215 com_add_char(' '); /* force empty output line */ 214 com_add_char(' '); /* force empty output line */
216 if (com.len > 3) { 215 if (com.len > 3) {