Sat Jun 10 12:59:31 2023 UTC ()
indent: in debug mode, null-terminate buffers


(rillig)
diff -r1.352 -r1.353 src/usr.bin/indent/indent.c
diff -r1.185 -r1.186 src/usr.bin/indent/indent.h
diff -r1.214 -r1.215 src/usr.bin/indent/io.c
diff -r1.222 -r1.223 src/usr.bin/indent/lexi.c
diff -r1.159 -r1.160 src/usr.bin/indent/pr_comment.c

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

--- src/usr.bin/indent/indent.c 2023/06/10 08:17:04 1.352
+++ src/usr.bin/indent/indent.c 2023/06/10 12:59:31 1.353
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.352 2023/06/10 08:17:04 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 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.352 2023/06/10 08:17:04 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 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 = {
@@ -100,43 +100,55 @@ nonnull(void *p) @@ -100,43 +100,55 @@ nonnull(void *p)
100{ 100{
101 if (p == NULL) 101 if (p == NULL)
102 err(EXIT_FAILURE, NULL); 102 err(EXIT_FAILURE, NULL);
103 return p; 103 return p;
104} 104}
105 105
106static void 106static void
107buf_expand(struct buffer *buf, size_t add_size) 107buf_expand(struct buffer *buf, size_t add_size)
108{ 108{
109 buf->cap = buf->cap + add_size + 400; 109 buf->cap = buf->cap + add_size + 400;
110 buf->s = nonnull(realloc(buf->s, buf->cap)); 110 buf->s = nonnull(realloc(buf->s, buf->cap));
111} 111}
112 112
 113#ifdef debug
 114void
 115buf_terminate(struct buffer *buf)
 116{
 117 if (buf->len == buf->cap)
 118 buf_expand(buf, 1);
 119 buf->s[buf->len] = '\0';
 120}
 121#endif
 122
113void 123void
114buf_add_char(struct buffer *buf, char ch) 124buf_add_char(struct buffer *buf, char ch)
115{ 125{
116 if (buf->len == buf->cap) 126 if (buf->len == buf->cap)
117 buf_expand(buf, 1); 127 buf_expand(buf, 1);
118 buf->s[buf->len++] = ch; 128 buf->s[buf->len++] = ch;
 129 buf_terminate(buf);
119} 130}
120 131
121void 132void
122buf_add_chars(struct buffer *buf, const char *s, size_t len) 133buf_add_chars(struct buffer *buf, const char *s, size_t len)
123{ 134{
124 if (len == 0) 135 if (len == 0)
125 return; 136 return;
126 if (len > buf->cap - buf->len) 137 if (len > buf->cap - buf->len)
127 buf_expand(buf, len); 138 buf_expand(buf, len);
128 memcpy(buf->s + buf->len, s, len); 139 memcpy(buf->s + buf->len, s, len);
129 buf->len += len; 140 buf->len += len;
 141 buf_terminate(buf);
130} 142}
131 143
132static void 144static void
133buf_add_buf(struct buffer *buf, const struct buffer *add) 145buf_add_buf(struct buffer *buf, const struct buffer *add)
134{ 146{
135 buf_add_chars(buf, add->s, add->len); 147 buf_add_chars(buf, add->s, add->len);
136} 148}
137 149
138void 150void
139diag(int level, const char *msg, ...) 151diag(int level, const char *msg, ...)
140{ 152{
141 va_list ap; 153 va_list ap;
142 154
@@ -317,27 +329,27 @@ maybe_break_line(lexer_symbol lsym) @@ -317,27 +329,27 @@ maybe_break_line(lexer_symbol lsym)
317 && ps.prev_lsym != lsym_lbrace) 329 && ps.prev_lsym != lsym_lbrace)
318 return; 330 return;
319 331
320 output_line(); 332 output_line();
321 ps.force_nl = false; 333 ps.force_nl = false;
322} 334}
323 335
324static void 336static void
325move_com_to_code(lexer_symbol lsym) 337move_com_to_code(lexer_symbol lsym)
326{ 338{
327 if (ps.want_blank) 339 if (ps.want_blank)
328 buf_add_char(&code, ' '); 340 buf_add_char(&code, ' ');
329 buf_add_buf(&code, &com); 341 buf_add_buf(&code, &com);
330 com.len = 0; 342 buf_clear(&com);
331 ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket; 343 ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
332} 344}
333 345
334static void 346static void
335update_ps_lbrace_kind(lexer_symbol lsym) 347update_ps_lbrace_kind(lexer_symbol lsym)
336{ 348{
337 if (lsym == lsym_tag) { 349 if (lsym == lsym_tag) {
338 ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct : 350 ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
339 token.s[0] == 'u' ? psym_lbrace_union : 351 token.s[0] == 'u' ? psym_lbrace_union :
340 psym_lbrace_enum; 352 psym_lbrace_enum;
341 } else if (lsym != lsym_type_outside_parentheses 353 } else if (lsym != lsym_type_outside_parentheses
342 && lsym != lsym_word 354 && lsym != lsym_word
343 && lsym != lsym_lbrace) 355 && lsym != lsym_lbrace)
@@ -423,26 +435,27 @@ read_preprocessing_line(void) @@ -423,26 +435,27 @@ read_preprocessing_line(void)
423 state = CHR; 435 state = CHR;
424 break; 436 break;
425 case '*': 437 case '*':
426 if (inp_p[0] == '/' && state == COMM) { 438 if (inp_p[0] == '/' && state == COMM) {
427 state = PLAIN; 439 state = PLAIN;
428 buf_add_char(&lab, *inp_p++); 440 buf_add_char(&lab, *inp_p++);
429 } 441 }
430 break; 442 break;
431 } 443 }
432 } 444 }
433 445
434 while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1])) 446 while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
435 lab.len--; 447 lab.len--;
 448 buf_terminate(&lab);
436} 449}
437 450
438static void 451static void
439process_preprocessing(void) 452process_preprocessing(void)
440{ 453{
441 if (lab.len > 0 || code.len > 0 || com.len > 0) 454 if (lab.len > 0 || code.len > 0 || com.len > 0)
442 output_line(); 455 output_line();
443 456
444 read_preprocessing_line(); 457 read_preprocessing_line();
445 458
446 const char *dir = lab.s + 1, *line_end = lab.s + lab.len; 459 const char *dir = lab.s + 1, *line_end = lab.s + lab.len;
447 while (dir < line_end && ch_isblank(*dir)) 460 while (dir < line_end && ch_isblank(*dir))
448 dir++; 461 dir++;
@@ -795,27 +808,27 @@ process_comma(void) @@ -795,27 +808,27 @@ process_comma(void)
795 int typical_varname_length = 8; 808 int typical_varname_length = 8;
796 if (ps.break_after_comma && (opt.break_after_comma || 809 if (ps.break_after_comma && (opt.break_after_comma ||
797 ind_add(compute_code_indent(), code.s, code.len) 810 ind_add(compute_code_indent(), code.s, code.len)
798 >= opt.max_line_length - typical_varname_length)) 811 >= opt.max_line_length - typical_varname_length))
799 ps.force_nl = true; 812 ps.force_nl = true;
800 } 813 }
801} 814}
802 815
803static void 816static void
804process_colon_label(void) 817process_colon_label(void)
805{ 818{
806 buf_add_buf(&lab, &code); 819 buf_add_buf(&lab, &code);
807 buf_add_char(&lab, ':'); 820 buf_add_char(&lab, ':');
808 code.len = 0; 821 buf_clear(&code);
809 822
810 if (ps.seen_case) 823 if (ps.seen_case)
811 out.line_kind = lk_case_or_default; 824 out.line_kind = lk_case_or_default;
812 ps.in_stmt_or_decl = false; 825 ps.in_stmt_or_decl = false;
813 ps.force_nl = ps.seen_case; 826 ps.force_nl = ps.seen_case;
814 ps.seen_case = false; 827 ps.seen_case = false;
815 ps.want_blank = false; 828 ps.want_blank = false;
816} 829}
817 830
818static void 831static void
819process_colon_other(void) 832process_colon_other(void)
820{ 833{
821 buf_add_char(&code, ':'); 834 buf_add_char(&code, ':');

cvs diff -r1.185 -r1.186 src/usr.bin/indent/indent.h (expand / switch to unified diff)

--- src/usr.bin/indent/indent.h 2023/06/10 07:42:41 1.185
+++ src/usr.bin/indent/indent.h 2023/06/10 12:59:31 1.186
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.h,v 1.185 2023/06/10 07:42:41 rillig Exp $ */ 1/* $NetBSD: indent.h,v 1.186 2023/06/10 12:59:31 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * 5 *
6 * Copyright (c) 2001 Jens Schweikhardt 6 * Copyright (c) 2001 Jens Schweikhardt
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -131,27 +131,27 @@ typedef enum parser_symbol { @@ -131,27 +131,27 @@ typedef enum parser_symbol {
131 psym_stmt, 131 psym_stmt,
132 psym_stmt_list, 132 psym_stmt_list,
133 psym_for_exprs, /* 'for' '(' ... ')' */ 133 psym_for_exprs, /* 'for' '(' ... ')' */
134 psym_if_expr, /* 'if' '(' expr ')' */ 134 psym_if_expr, /* 'if' '(' expr ')' */
135 psym_if_expr_stmt, /* 'if' '(' expr ')' stmt */ 135 psym_if_expr_stmt, /* 'if' '(' expr ')' stmt */
136 psym_if_expr_stmt_else, /* 'if' '(' expr ')' stmt 'else' */ 136 psym_if_expr_stmt_else, /* 'if' '(' expr ')' stmt 'else' */
137 psym_else, /* 'else'; not stored on the stack */ 137 psym_else, /* 'else'; not stored on the stack */
138 psym_switch_expr, /* 'switch' '(' expr ')' */ 138 psym_switch_expr, /* 'switch' '(' expr ')' */
139 psym_do, /* 'do' */ 139 psym_do, /* 'do' */
140 psym_do_stmt, /* 'do' stmt */ 140 psym_do_stmt, /* 'do' stmt */
141 psym_while_expr, /* 'while' '(' expr ')' */ 141 psym_while_expr, /* 'while' '(' expr ')' */
142} parser_symbol; 142} parser_symbol;
143 143
144/* A range of characters, not null-terminated. */ 144/* A range of characters, only null-terminated in debug mode. */
145struct buffer { 145struct buffer {
146 char *s; 146 char *s;
147 size_t len; 147 size_t len;
148 size_t cap; 148 size_t cap;
149}; 149};
150 150
151extern FILE *input; 151extern FILE *input;
152extern FILE *output; 152extern FILE *output;
153 153
154/* 154/*
155 * The current line from the input file, used by the lexer to generate tokens. 155 * The current line from the input file, used by the lexer to generate tokens.
156 * To read from the line, start at inp_p and continue up to and including the 156 * To read from the line, start at inp_p and continue up to and including the
157 * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will 157 * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will
@@ -512,13 +512,26 @@ ch_isdigit(char ch) @@ -512,13 +512,26 @@ ch_isdigit(char ch)
512} 512}
513 513
514static inline bool 514static inline bool
515ch_isspace(char ch) 515ch_isspace(char ch)
516{ 516{
517 return isspace((unsigned char)ch) != 0; 517 return isspace((unsigned char)ch) != 0;
518} 518}
519 519
520static inline int 520static inline int
521next_tab(int ind) 521next_tab(int ind)
522{ 522{
523 return ind - ind % opt.tabsize + opt.tabsize; 523 return ind - ind % opt.tabsize + opt.tabsize;
524} 524}
 525
 526#ifdef debug
 527void buf_terminate(struct buffer *);
 528#else
 529#define buf_terminate(buf) debug_noop()
 530#endif
 531
 532static inline void
 533buf_clear(struct buffer *buf)
 534{
 535 buf->len = 0;
 536 buf_terminate(buf);
 537}

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

--- src/usr.bin/indent/io.c 2023/06/10 11:01:58 1.214
+++ src/usr.bin/indent/io.c 2023/06/10 12:59:31 1.215
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.214 2023/06/10 11:01:58 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.215 2023/06/10 12:59:31 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,75 +28,76 @@ @@ -28,75 +28,76 @@
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.214 2023/06/10 11:01:58 rillig Exp $"); 41__RCSID("$NetBSD: io.c,v 1.215 2023/06/10 12:59:31 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;
51enum indent_enabled indent_enabled; 51enum indent_enabled indent_enabled;
52static int out_ind; /* width of the line that is being written */ 52static int out_ind; /* width of the line that is being written */
53static unsigned newlines = 2; /* the total of written and buffered newlines; 53static unsigned newlines = 2; /* the total of written and buffered newlines;
54 * 0 in the middle of a line, 1 after a single 54 * 0 in the middle of a line, 1 after a single
55 * finished line, anything > 1 are trailing 55 * finished line, anything > 1 are trailing
56 * blank lines */ 56 * blank lines */
57static unsigned buffered_newlines; /* not yet written */ 57static unsigned buffered_newlines; /* not yet written */
58static int paren_indent; 58static int paren_indent;
59 59
60 60
61static void 61static void
62inp_read_next_line(FILE *f) 62inp_read_next_line(FILE *f)
63{ 63{
64 inp.len = 0; 64 buf_clear(&inp);
65 65
66 for (;;) { 66 for (;;) {
67 int ch = getc(f); 67 int ch = getc(f);
68 if (ch == EOF) { 68 if (ch == EOF) {
69 if (indent_enabled == indent_on) { 69 if (indent_enabled == indent_on) {
70 buf_add_char(&inp, ' '); 70 buf_add_char(&inp, ' ');
71 buf_add_char(&inp, '\n'); 71 buf_add_char(&inp, '\n');
72 } 72 }
73 had_eof = true; 73 had_eof = true;
74 break; 74 break;
75 } 75 }
76 76
77 if (ch != '\0') 77 if (ch != '\0')
78 buf_add_char(&inp, (char)ch); 78 buf_add_char(&inp, (char)ch);
79 if (ch == '\n') 79 if (ch == '\n')
80 break; 80 break;
81 } 81 }
 82 buf_terminate(&inp);
82 inp_p = inp.s; 83 inp_p = inp.s;
83} 84}
84 85
85void 86void
86inp_read_line(void) 87inp_read_line(void)
87{ 88{
88 if (indent_enabled == indent_on) 89 if (indent_enabled == indent_on)
89 out.indent_off_text.len = 0; 90 buf_clear(&out.indent_off_text);
90 buf_add_chars(&out.indent_off_text, inp.s, inp.len); 91 buf_add_chars(&out.indent_off_text, inp.s, inp.len);
91 inp_read_next_line(input); 92 inp_read_next_line(input);
92} 93}
93 94
94void 95void
95inp_skip(void) 96inp_skip(void)
96{ 97{
97 inp_p++; 98 inp_p++;
98 if ((size_t)(inp_p - inp.s) >= inp.len) 99 if ((size_t)(inp_p - inp.s) >= inp.len)
99 inp_read_line(); 100 inp_read_line();
100} 101}
101 102
102char 103char
@@ -306,26 +307,27 @@ output_line_comment(void) @@ -306,26 +307,27 @@ output_line_comment(void)
306 else if (*p == '\t') 307 else if (*p == '\t')
307 target_ind = next_tab(target_ind); 308 target_ind = next_tab(target_ind);
308 else { 309 else {
309 target_ind = 0; 310 target_ind = 0;
310 break; 311 break;
311 } 312 }
312 } 313 }
313 314
314 if (out_ind > target_ind) 315 if (out_ind > target_ind)
315 buffer_newline(); 316 buffer_newline();
316 317
317 while (com.s + com.len > p && ch_isspace(com.s[com.len - 1])) 318 while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
318 com.len--; 319 com.len--;
 320 buf_terminate(&com);
319 321
320 write_indent(target_ind); 322 write_indent(target_ind);
321 write_range(p, com.len - (size_t)(p - com.s)); 323 write_range(p, com.len - (size_t)(p - com.s));
322 324
323 ps.comment_delta = ps.n_comment_delta; 325 ps.comment_delta = ps.n_comment_delta;
324} 326}
325 327
326static void 328static void
327output_line_indented(void) 329output_line_indented(void)
328{ 330{
329 if (lab.len == 0 && code.len == 0 && com.len == 0) 331 if (lab.len == 0 && code.len == 0 && com.len == 0)
330 out.line_kind = lk_blank; 332 out.line_kind = lk_blank;
331 333
@@ -367,32 +369,32 @@ output_line_indented(void) @@ -367,32 +369,32 @@ output_line_indented(void)
367 */ 369 */
368void 370void
369output_line(void) 371output_line(void)
370{ 372{
371 debug_blank_line(); 373 debug_blank_line();
372 debug_printf("%s", __func__); 374 debug_printf("%s", __func__);
373 debug_buffers(); 375 debug_buffers();
374 376
375 if (indent_enabled == indent_on) 377 if (indent_enabled == indent_on)
376 output_line_indented(); 378 output_line_indented();
377 else if (indent_enabled == indent_last_off_line) { 379 else if (indent_enabled == indent_last_off_line) {
378 indent_enabled = indent_on; 380 indent_enabled = indent_on;
379 write_range(out.indent_off_text.s, out.indent_off_text.len); 381 write_range(out.indent_off_text.s, out.indent_off_text.len);
380 out.indent_off_text.len = 0; 382 buf_clear(&out.indent_off_text);
381 } 383 }
382 384
383 lab.len = 0; 385 buf_clear(&lab);
384 code.len = 0; 386 buf_clear(&code);
385 com.len = 0; 387 buf_clear(&com);
386 388
387 ps.line_has_decl = ps.in_decl; 389 ps.line_has_decl = ps.in_decl;
388 ps.line_has_func_def = false; 390 ps.line_has_func_def = false;
389 ps.in_stmt_cont = ps.in_stmt_or_decl 391 ps.in_stmt_cont = ps.in_stmt_or_decl
390 && (!ps.in_decl || ps.in_init) 392 && (!ps.in_decl || ps.in_init)
391 && ps.init_level == 0; 393 && ps.init_level == 0;
392 ps.decl_indent_done = false; 394 ps.decl_indent_done = false;
393 if (ps.extra_expr_indent == eei_last) 395 if (ps.extra_expr_indent == eei_last)
394 ps.extra_expr_indent = eei_no; 396 ps.extra_expr_indent = eei_no;
395 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else 397 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else
396 && ps.nparen > 0)) 398 && ps.nparen > 0))
397 ps.ind_level = ps.ind_level_follow; 399 ps.ind_level = ps.ind_level_follow;
398 ps.line_start_nparen = ps.nparen; 400 ps.line_start_nparen = ps.nparen;

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

--- src/usr.bin/indent/lexi.c 2023/06/10 07:42:41 1.222
+++ src/usr.bin/indent/lexi.c 2023/06/10 12:59:31 1.223
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lexi.c,v 1.222 2023/06/10 07:42:41 rillig Exp $ */ 1/* $NetBSD: lexi.c,v 1.223 2023/06/10 12:59:31 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.222 2023/06/10 07:42:41 rillig Exp $"); 41__RCSID("$NetBSD: lexi.c,v 1.223 2023/06/10 12:59:31 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;
@@ -519,27 +519,27 @@ lex_indent_comment(void) @@ -519,27 +519,27 @@ lex_indent_comment(void)
519 if (!skip_string(&p, "*/\n")) 519 if (!skip_string(&p, "*/\n"))
520 return; 520 return;
521 521
522 if (lab.len > 0 || code.len > 0 || com.len > 0) 522 if (lab.len > 0 || code.len > 0 || com.len > 0)
523 output_line(); 523 output_line();
524 524
525 indent_enabled = enabled; 525 indent_enabled = enabled;
526} 526}
527 527
528/* Reads the next token, placing it in the global variable "token". */ 528/* Reads the next token, placing it in the global variable "token". */
529lexer_symbol 529lexer_symbol
530lexi(void) 530lexi(void)
531{ 531{
532 token.len = 0; 532 buf_clear(&token);
533 ps.curr_col_1 = ps.next_col_1; 533 ps.curr_col_1 = ps.next_col_1;
534 ps.next_col_1 = false; 534 ps.next_col_1 = false;
535 535
536 for (;;) { 536 for (;;) {
537 if (ch_isblank(inp_p[0])) { 537 if (ch_isblank(inp_p[0])) {
538 ps.curr_col_1 = false; 538 ps.curr_col_1 = false;
539 inp_p++; 539 inp_p++;
540 } else if (inp_p[0] == '\\' && inp_p[1] == '\n') { 540 } else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
541 inp_p++; 541 inp_p++;
542 inp_skip(); 542 inp_skip();
543 line_no++; 543 line_no++;
544 } else 544 } else
545 break; 545 break;
@@ -654,27 +654,27 @@ lexi(void) @@ -654,27 +654,27 @@ lexi(void)
654 case '\'': 654 case '\'':
655 case '"': 655 case '"':
656 lex_char_or_string(); 656 lex_char_or_string();
657 lsym = lsym_word; 657 lsym = lsym_word;
658 next_unary = false; 658 next_unary = false;
659 break; 659 break;
660 660
661 default: 661 default:
662 if (token.s[token.len - 1] == '/' 662 if (token.s[token.len - 1] == '/'
663 && (inp_p[0] == '*' || inp_p[0] == '/')) { 663 && (inp_p[0] == '*' || inp_p[0] == '/')) {
664 enum indent_enabled prev = indent_enabled; 664 enum indent_enabled prev = indent_enabled;
665 lex_indent_comment(); 665 lex_indent_comment();
666 if (prev == indent_on && indent_enabled == indent_off) 666 if (prev == indent_on && indent_enabled == indent_off)
667 out.indent_off_text.len = 0; 667 buf_clear(&out.indent_off_text);
668 token_add_char(*inp_p++); 668 token_add_char(*inp_p++);
669 lsym = lsym_comment; 669 lsym = lsym_comment;
670 next_unary = ps.next_unary; 670 next_unary = ps.next_unary;
671 break; 671 break;
672 } 672 }
673 673
674 /* things like '||', '&&', '<<=' */ 674 /* things like '||', '&&', '<<=' */
675 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op; 675 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
676 if (inp_p[0] == token.s[token.len - 1]) 676 if (inp_p[0] == token.s[token.len - 1])
677 token_add_char(*inp_p++), lsym = lsym_binary_op; 677 token_add_char(*inp_p++), lsym = lsym_binary_op;
678 if (inp_p[0] == '=') 678 if (inp_p[0] == '=')
679 token_add_char(*inp_p++), lsym = lsym_binary_op; 679 token_add_char(*inp_p++), lsym = lsym_binary_op;
680 680

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

--- src/usr.bin/indent/pr_comment.c 2023/06/10 06:38:21 1.159
+++ src/usr.bin/indent/pr_comment.c 2023/06/10 12:59:31 1.160
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pr_comment.c,v 1.159 2023/06/10 06:38:21 rillig Exp $ */ 1/* $NetBSD: pr_comment.c,v 1.160 2023/06/10 12:59:31 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.159 2023/06/10 06:38:21 rillig Exp $"); 41__RCSID("$NetBSD: pr_comment.c,v 1.160 2023/06/10 12:59:31 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)
@@ -238,41 +238,42 @@ copy_comment_wrap_newline(ssize_t *last_ @@ -238,41 +238,42 @@ copy_comment_wrap_newline(ssize_t *last_
238 inp_p++; 238 inp_p++;
239 } 239 }
240 240
241 return true; 241 return true;
242} 242}
243 243
244static void 244static void
245copy_comment_wrap_finish(int line_length, bool delim) 245copy_comment_wrap_finish(int line_length, bool delim)
246{ 246{
247 if (delim) { 247 if (delim) {
248 if (com.len > 3) 248 if (com.len > 3)
249 output_line(); 249 output_line();
250 else 250 else
251 com.len = 0; 251 buf_clear(&com);
252 com_add_char(' '); 252 com_add_char(' ');
253 } else { 253 } else {
254 size_t len = com.len; 254 size_t len = com.len;
255 while (ch_isblank(com.s[len - 1])) 255 while (ch_isblank(com.s[len - 1]))
256 len--; 256 len--;
257 int end_ind = ind_add(ps.com_ind, com.s, len); 257 int end_ind = ind_add(ps.com_ind, com.s, len);
258 if (end_ind + 3 > line_length) 258 if (end_ind + 3 > line_length)
259 output_line(); 259 output_line();
260 } 260 }
261 261
262 while (com.len >= 2 262 while (com.len >= 2
263 && ch_isblank(com.s[com.len - 1]) 263 && ch_isblank(com.s[com.len - 1])
264 && ch_isblank(com.s[com.len - 2])) 264 && ch_isblank(com.s[com.len - 2]))
265 com.len--; 265 com.len--;
 266 buf_terminate(&com);
266 267
267 inp_p += 2; 268 inp_p += 2;
268 if (com.len > 0 && ch_isblank(com.s[com.len - 1])) 269 if (com.len > 0 && ch_isblank(com.s[com.len - 1]))
269 buf_add_chars(&com, "*/", 2); 270 buf_add_chars(&com, "*/", 2);
270 else 271 else
271 buf_add_chars(&com, " */", 3); 272 buf_add_chars(&com, " */", 3);
272} 273}
273 274
274/* 275/*
275 * Copy characters from 'inp' to 'com'. Try to keep comments from going over 276 * Copy characters from 'inp' to 'com'. Try to keep comments from going over
276 * the maximum line length. To do that, remember where the last blank, tab, or 277 * the maximum line length. To do that, remember where the last blank, tab, or
277 * newline was. When a line is filled, print up to the last blank and continue 278 * newline was. When a line is filled, print up to the last blank and continue
278 * copying. 279 * copying.