Fri Mar 12 23:10:18 2021 UTC ()
indent: use consistent indentation for 'else'

Half of the code used -ce, the other half the opposite -nce.

No functional change.


(rillig)
diff -r1.18 -r1.19 src/usr.bin/indent/args.c
diff -r1.18 -r1.19 src/usr.bin/indent/pr_comment.c
diff -r1.44 -r1.45 src/usr.bin/indent/indent.c
diff -r1.29 -r1.30 src/usr.bin/indent/io.c
diff -r1.37 -r1.38 src/usr.bin/indent/lexi.c
diff -r1.17 -r1.18 src/usr.bin/indent/parse.c

cvs diff -r1.18 -r1.19 src/usr.bin/indent/args.c (expand / switch to unified diff)

--- src/usr.bin/indent/args.c 2021/03/09 16:48:28 1.18
+++ src/usr.bin/indent/args.c 2021/03/12 23:10:18 1.19
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: args.c,v 1.18 2021/03/09 16:48:28 rillig Exp $ */ 1/* $NetBSD: args.c,v 1.19 2021/03/12 23:10:18 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
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
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#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93"; 42static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: args.c,v 1.18 2021/03/09 16:48:28 rillig Exp $"); 49__RCSID("$NetBSD: args.c,v 1.19 2021/03/12 23:10:18 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
52#endif 52#endif
53#endif 53#endif
54 54
55/* 55/*
56 * Argument scanning and profile reading code. Default parameters are set 56 * Argument scanning and profile reading code. Default parameters are set
57 * here as well. 57 * here as well.
58 */ 58 */
59 59
60#include <ctype.h> 60#include <ctype.h>
61#include <err.h> 61#include <err.h>
62#include <limits.h> 62#include <limits.h>
@@ -224,28 +224,27 @@ scan_profile(FILE *f) @@ -224,28 +224,27 @@ scan_profile(FILE *f)
224 comment_index = 0; 224 comment_index = 0;
225 } else if (isspace((unsigned char)i)) { 225 } else if (isspace((unsigned char)i)) {
226 if (p > buf && !comment_index) 226 if (p > buf && !comment_index)
227 break; 227 break;
228 } else { 228 } else {
229 *p++ = i; 229 *p++ = i;
230 } 230 }
231 } 231 }
232 if (p != buf) { 232 if (p != buf) {
233 *p++ = 0; 233 *p++ = 0;
234 if (opt.verbose) 234 if (opt.verbose)
235 printf("profile: %s\n", buf); 235 printf("profile: %s\n", buf);
236 set_option(buf); 236 set_option(buf);
237 } 237 } else if (i == EOF)
238 else if (i == EOF) 
239 return; 238 return;
240 } 239 }
241} 240}
242 241
243static const char * 242static const char *
244eqin(const char *s1, const char *s2) 243eqin(const char *s1, const char *s2)
245{ 244{
246 while (*s1) { 245 while (*s1) {
247 if (*s1++ != *s2++) 246 if (*s1++ != *s2++)
248 return NULL; 247 return NULL;
249 } 248 }
250 return s2; 249 return s2;
251} 250}

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

--- src/usr.bin/indent/pr_comment.c 2021/03/11 22:32:06 1.18
+++ src/usr.bin/indent/pr_comment.c 2021/03/12 23:10:18 1.19
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pr_comment.c,v 1.18 2021/03/11 22:32:06 rillig Exp $ */ 1/* $NetBSD: pr_comment.c,v 1.19 2021/03/12 23:10:18 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
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
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#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93"; 42static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: pr_comment.c,v 1.18 2021/03/11 22:32:06 rillig Exp $"); 49__RCSID("$NetBSD: pr_comment.c,v 1.19 2021/03/12 23:10:18 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
52#endif 52#endif
53#endif 53#endif
54 54
55#include <err.h> 55#include <err.h>
56#include <stdio.h> 56#include <stdio.h>
57#include <stdlib.h> 57#include <stdlib.h>
58#include <string.h> 58#include <string.h>
59 59
60#include "indent.h" 60#include "indent.h"
61 61
62static void 62static void
@@ -126,51 +126,49 @@ pr_comment(void) @@ -126,51 +126,49 @@ pr_comment(void)
126 last_bl = NULL; /* no blanks found so far */ 126 last_bl = NULL; /* no blanks found so far */
127 ps.box_com = false; /* at first, assume that we are not in 127 ps.box_com = false; /* at first, assume that we are not in
128 * a boxed comment or some other 128 * a boxed comment or some other
129 * comment that should not be touched */ 129 * comment that should not be touched */
130 ++ps.out_coms; /* keep track of number of comments */ 130 ++ps.out_coms; /* keep track of number of comments */
131 131
132 /* Figure where to align and how to treat the comment */ 132 /* Figure where to align and how to treat the comment */
133 133
134 if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in column 134 if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in column
135 * 1 it should not be touched */ 135 * 1 it should not be touched */
136 ps.box_com = true; 136 ps.box_com = true;
137 break_delim = false; 137 break_delim = false;
138 ps.com_col = 1; 138 ps.com_col = 1;
139 } 139 } else {
140 else { 
141 if (*buf_ptr == '-' || *buf_ptr == '*' || e_token[-1] == '/' || 140 if (*buf_ptr == '-' || *buf_ptr == '*' || e_token[-1] == '/' ||
142 (*buf_ptr == '\n' && !opt.format_block_comments)) { 141 (*buf_ptr == '\n' && !opt.format_block_comments)) {
143 ps.box_com = true; /* A comment with a '-' or '*' immediately 142 ps.box_com = true; /* A comment with a '-' or '*' immediately
144 * after the /+* is assumed to be a boxed 143 * after the /+* is assumed to be a boxed
145 * comment. A comment with a newline 144 * comment. A comment with a newline
146 * immediately after the /+* is assumed to 145 * immediately after the /+* is assumed to
147 * be a block comment and is treated as a 146 * be a block comment and is treated as a
148 * box comment unless format_block_comments 147 * box comment unless format_block_comments
149 * is nonzero (the default). */ 148 * is nonzero (the default). */
150 break_delim = false; 149 break_delim = false;
151 } 150 }
152 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 151 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
153 /* klg: check only if this line is blank */ 152 /* klg: check only if this line is blank */
154 /* 153 /*
155 * If this (*and previous lines are*) blank, dont put comment way 154 * If this (*and previous lines are*) blank, dont put comment way
156 * out at left 155 * out at left
157 */ 156 */
158 ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1; 157 ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
159 adj_max_col = opt.block_comment_max_col; 158 adj_max_col = opt.block_comment_max_col;
160 if (ps.com_col <= 1) 159 if (ps.com_col <= 1)
161 ps.com_col = 1 + !opt.format_col1_comments; 160 ps.com_col = 1 + !opt.format_col1_comments;
162 } 161 } else {
163 else { 
164 int target_col; 162 int target_col;
165 break_delim = false; 163 break_delim = false;
166 if (s_code != e_code) 164 if (s_code != e_code)
167 target_col = count_spaces(compute_code_target(), s_code); 165 target_col = count_spaces(compute_code_target(), s_code);
168 else { 166 else {
169 target_col = 1; 167 target_col = 1;
170 if (s_lab != e_lab) 168 if (s_lab != e_lab)
171 target_col = count_spaces(compute_label_target(), s_lab); 169 target_col = count_spaces(compute_label_target(), s_lab);
172 } 170 }
173 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind; 171 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
174 if (ps.com_col <= target_col) 172 if (ps.com_col <= target_col)
175 ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1; 173 ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
176 if (ps.com_col + 24 > adj_max_col) 174 if (ps.com_col + 24 > adj_max_col)
@@ -181,28 +179,27 @@ pr_comment(void) @@ -181,28 +179,27 @@ pr_comment(void)
181 /* 179 /*
182 * Find out how much indentation there was originally, because that 180 * Find out how much indentation there was originally, because that
183 * much will have to be ignored by pad_output() in dump_line(). This 181 * much will have to be ignored by pad_output() in dump_line(). This
184 * is a box comment, so nothing changes -- not even indentation. 182 * is a box comment, so nothing changes -- not even indentation.
185 * 183 *
186 * The comment we're about to read usually comes from in_buffer, 184 * The comment we're about to read usually comes from in_buffer,
187 * unless it has been copied into save_com. 185 * unless it has been copied into save_com.
188 */ 186 */
189 char *start; 187 char *start;
190 188
191 start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? 189 start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
192 sc_buf : in_buffer; 190 sc_buf : in_buffer;
193 ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2); 191 ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
194 } 192 } else {
195 else { 
196 ps.n_comment_delta = 0; 193 ps.n_comment_delta = 0;
197 while (*buf_ptr == ' ' || *buf_ptr == '\t') 194 while (*buf_ptr == ' ' || *buf_ptr == '\t')
198 buf_ptr++; 195 buf_ptr++;
199 } 196 }
200 ps.comment_delta = 0; 197 ps.comment_delta = 0;
201 *e_com++ = '/'; 198 *e_com++ = '/';
202 *e_com++ = e_token[-1]; 199 *e_com++ = e_token[-1];
203 if (*buf_ptr != ' ' && !ps.box_com) 200 if (*buf_ptr != ' ' && !ps.box_com)
204 *e_com++ = ' '; 201 *e_com++ = ' ';
205 202
206 /* 203 /*
207 * Don't put a break delimiter if this is a one-liner that won't wrap. 204 * Don't put a break delimiter if this is a one-liner that won't wrap.
208 */ 205 */
@@ -235,28 +232,27 @@ pr_comment(void) @@ -235,28 +232,27 @@ pr_comment(void)
235 * copied */ 232 * copied */
236 switch (*buf_ptr) { /* this checks for various spcl cases */ 233 switch (*buf_ptr) { /* this checks for various spcl cases */
237 case 014: /* check for a form feed */ 234 case 014: /* check for a form feed */
238 check_size_comment(3, &last_bl); 235 check_size_comment(3, &last_bl);
239 if (!ps.box_com) { /* in a text comment, break the line here */ 236 if (!ps.box_com) { /* in a text comment, break the line here */
240 ps.use_ff = true; 237 ps.use_ff = true;
241 /* fix so dump_line uses a form feed */ 238 /* fix so dump_line uses a form feed */
242 dump_line(); 239 dump_line();
243 last_bl = NULL; 240 last_bl = NULL;
244 if (!ps.box_com && opt.star_comment_cont) 241 if (!ps.box_com && opt.star_comment_cont)
245 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 242 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
246 while (*++buf_ptr == ' ' || *buf_ptr == '\t') 243 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
247 ; 244 ;
248 } 245 } else {
249 else { 
250 if (++buf_ptr >= buf_end) 246 if (++buf_ptr >= buf_end)
251 fill_buffer(); 247 fill_buffer();
252 *e_com++ = 014; 248 *e_com++ = 014;
253 } 249 }
254 break; 250 break;
255 251
256 case '\n': 252 case '\n':
257 if (e_token[-1] == '/') { 253 if (e_token[-1] == '/') {
258 ++line_no; 254 ++line_no;
259 goto end_of_comment; 255 goto end_of_comment;
260 } 256 }
261 if (had_eof) { /* check for unexpected eof */ 257 if (had_eof) { /* check for unexpected eof */
262 printf("Unterminated comment\n"); 258 printf("Unterminated comment\n");
@@ -267,86 +263,82 @@ pr_comment(void) @@ -267,86 +263,82 @@ pr_comment(void)
267 check_size_comment(4, &last_bl); 263 check_size_comment(4, &last_bl);
268 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, 264 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
269 * we dont ignore the newline */ 265 * we dont ignore the newline */
270 if (s_com == e_com) 266 if (s_com == e_com)
271 *e_com++ = ' '; 267 *e_com++ = ' ';
272 if (!ps.box_com && e_com - s_com > 3) { 268 if (!ps.box_com && e_com - s_com > 3) {
273 dump_line(); 269 dump_line();
274 if (opt.star_comment_cont) 270 if (opt.star_comment_cont)
275 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 271 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
276 } 272 }
277 dump_line(); 273 dump_line();
278 if (!ps.box_com && opt.star_comment_cont) 274 if (!ps.box_com && opt.star_comment_cont)
279 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 275 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
280 } 276 } else {
281 else { 
282 ps.last_nl = 1; 277 ps.last_nl = 1;
283 if (e_com[-1] == ' ' || e_com[-1] == '\t') 278 if (e_com[-1] == ' ' || e_com[-1] == '\t')
284 last_bl = e_com - 1; 279 last_bl = e_com - 1;
285 /* 280 /*
286 * if there was a space at the end of the last line, remember 281 * if there was a space at the end of the last line, remember
287 * where it was 282 * where it was
288 */ 283 */
289 else { /* otherwise, insert one */ 284 else { /* otherwise, insert one */
290 last_bl = e_com; 285 last_bl = e_com;
291 *e_com++ = ' '; 286 *e_com++ = ' ';
292 } 287 }
293 } 288 }
294 ++line_no; /* keep track of input line number */ 289 ++line_no; /* keep track of input line number */
295 if (!ps.box_com) { 290 if (!ps.box_com) {
296 int nstar = 1; 291 int nstar = 1;
297 do { /* flush any blanks and/or tabs at start of 292 do { /* flush any blanks and/or tabs at start of
298 * next line */ 293 * next line */
299 if (++buf_ptr >= buf_end) 294 if (++buf_ptr >= buf_end)
300 fill_buffer(); 295 fill_buffer();
301 if (*buf_ptr == '*' && --nstar >= 0) { 296 if (*buf_ptr == '*' && --nstar >= 0) {
302 if (++buf_ptr >= buf_end) 297 if (++buf_ptr >= buf_end)
303 fill_buffer(); 298 fill_buffer();
304 if (*buf_ptr == '/') 299 if (*buf_ptr == '/')
305 goto end_of_comment; 300 goto end_of_comment;
306 } 301 }
307 } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 302 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
308 } 303 } else if (++buf_ptr >= buf_end)
309 else if (++buf_ptr >= buf_end) 
310 fill_buffer(); 304 fill_buffer();
311 break; /* end of case for newline */ 305 break; /* end of case for newline */
312 306
313 case '*': /* must check for possibility of being at end 307 case '*': /* must check for possibility of being at end
314 * of comment */ 308 * of comment */
315 if (++buf_ptr >= buf_end) /* get to next char after * */ 309 if (++buf_ptr >= buf_end) /* get to next char after * */
316 fill_buffer(); 310 fill_buffer();
317 check_size_comment(4, &last_bl); 311 check_size_comment(4, &last_bl);
318 if (*buf_ptr == '/') { /* it is the end!!! */ 312 if (*buf_ptr == '/') { /* it is the end!!! */
319 end_of_comment: 313 end_of_comment:
320 if (++buf_ptr >= buf_end) 314 if (++buf_ptr >= buf_end)
321 fill_buffer(); 315 fill_buffer();
322 if (break_delim) { 316 if (break_delim) {
323 if (e_com > s_com + 3) { 317 if (e_com > s_com + 3)
324 dump_line(); 318 dump_line();
325 } 
326 else 319 else
327 s_com = e_com; 320 s_com = e_com;
328 *e_com++ = ' '; 321 *e_com++ = ' ';
329 } 322 }
330 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com) 323 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
331 *e_com++ = ' '; /* ensure blank before end */ 324 *e_com++ = ' '; /* ensure blank before end */
332 if (e_token[-1] == '/') 325 if (e_token[-1] == '/')
333 *e_com++ = '\n', *e_com = '\0'; 326 *e_com++ = '\n', *e_com = '\0';
334 else 327 else
335 *e_com++ = '*', *e_com++ = '/', *e_com = '\0'; 328 *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
336 ps.just_saw_decl = l_just_saw_decl; 329 ps.just_saw_decl = l_just_saw_decl;
337 return; 330 return;
338 } 331 } else /* handle isolated '*' */
339 else /* handle isolated '*' */ 
340 *e_com++ = '*'; 332 *e_com++ = '*';
341 break; 333 break;
342 default: /* we have a random char */ 334 default: /* we have a random char */
343 now_col = count_spaces_until(ps.com_col, s_com, e_com); 335 now_col = count_spaces_until(ps.com_col, s_com, e_com);
344 do { 336 do {
345 check_size_comment(1, &last_bl); 337 check_size_comment(1, &last_bl);
346 *e_com = *buf_ptr++; 338 *e_com = *buf_ptr++;
347 if (buf_ptr >= buf_end) 339 if (buf_ptr >= buf_end)
348 fill_buffer(); 340 fill_buffer();
349 if (*e_com == ' ' || *e_com == '\t') 341 if (*e_com == ' ' || *e_com == '\t')
350 last_bl = e_com; /* remember we saw a blank */ 342 last_bl = e_com; /* remember we saw a blank */
351 ++e_com; 343 ++e_com;
352 now_col++; 344 now_col++;

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

--- src/usr.bin/indent/indent.c 2021/03/12 00:15:34 1.44
+++ src/usr.bin/indent/indent.c 2021/03/12 23:10:18 1.45
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.44 2021/03/12 00:15:34 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.45 2021/03/12 23:10:18 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
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
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#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; 42static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: indent.c,v 1.44 2021/03/12 00:15:34 rillig Exp $"); 49__RCSID("$NetBSD: indent.c,v 1.45 2021/03/12 23:10:18 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
52#endif 52#endif
53#endif 53#endif
54 54
55#include <sys/param.h> 55#include <sys/param.h>
56#if HAVE_CAPSICUM 56#if HAVE_CAPSICUM
57#include <sys/capsicum.h> 57#include <sys/capsicum.h>
58#include <capsicum_helpers.h> 58#include <capsicum_helpers.h>
59#endif 59#endif
60#include <err.h> 60#include <err.h>
61#include <errno.h> 61#include <errno.h>
62#include <fcntl.h> 62#include <fcntl.h>
@@ -497,41 +497,39 @@ main(int argc, char **argv) @@ -497,41 +497,39 @@ main(int argc, char **argv)
497 497
498 for (i = 1; i < argc; ++i) { 498 for (i = 1; i < argc; ++i) {
499 499
500 /* 500 /*
501 * look thru args (if any) for changes to defaults 501 * look thru args (if any) for changes to defaults
502 */ 502 */
503 if (argv[i][0] != '-') {/* no flag on parameter */ 503 if (argv[i][0] != '-') {/* no flag on parameter */
504 if (input == NULL) { /* we must have the input file */ 504 if (input == NULL) { /* we must have the input file */
505 in_name = argv[i]; /* remember name of input file */ 505 in_name = argv[i]; /* remember name of input file */
506 input = fopen(in_name, "r"); 506 input = fopen(in_name, "r");
507 if (input == NULL) /* check for open error */ 507 if (input == NULL) /* check for open error */
508 err(1, "%s", in_name); 508 err(1, "%s", in_name);
509 continue; 509 continue;
510 } 510 } else if (output == NULL) { /* we have the output file */
511 else if (output == NULL) { /* we have the output file */ 
512 out_name = argv[i]; /* remember name of output file */ 511 out_name = argv[i]; /* remember name of output file */
513 if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite 512 if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
514 * the file */ 513 * the file */
515 errx(1, "input and output files must be different"); 514 errx(1, "input and output files must be different");
516 } 515 }
517 output = fopen(out_name, "w"); 516 output = fopen(out_name, "w");
518 if (output == NULL) /* check for create error */ 517 if (output == NULL) /* check for create error */
519 err(1, "%s", out_name); 518 err(1, "%s", out_name);
520 continue; 519 continue;
521 } 520 }
522 errx(1, "unknown parameter: %s", argv[i]); 521 errx(1, "unknown parameter: %s", argv[i]);
523 } 522 } else
524 else 
525 set_option(argv[i]); 523 set_option(argv[i]);
526 } /* end of for */ 524 } /* end of for */
527 if (input == NULL) 525 if (input == NULL)
528 input = stdin; 526 input = stdin;
529 if (output == NULL) { 527 if (output == NULL) {
530 if (input == stdin) 528 if (input == stdin)
531 output = stdout; 529 output = stdout;
532 else { 530 else {
533 out_name = in_name; 531 out_name = in_name;
534 bakcopy(); 532 bakcopy();
535 } 533 }
536 } 534 }
537 535
@@ -626,28 +624,27 @@ main(int argc, char **argv) @@ -626,28 +624,27 @@ main(int argc, char **argv)
626 if (s_com != e_com) { /* the turkey has embedded a comment 624 if (s_com != e_com) { /* the turkey has embedded a comment
627 * in a line. fix it */ 625 * in a line. fix it */
628 int len = e_com - s_com; 626 int len = e_com - s_com;
629 627
630 check_size_code(len + 3); 628 check_size_code(len + 3);
631 *e_code++ = ' '; 629 *e_code++ = ' ';
632 memcpy(e_code, s_com, len); 630 memcpy(e_code, s_com, len);
633 e_code += len; 631 e_code += len;
634 *e_code++ = ' '; 632 *e_code++ = ' ';
635 *e_code = '\0'; /* null terminate code sect */ 633 *e_code = '\0'; /* null terminate code sect */
636 ps.want_blank = false; 634 ps.want_blank = false;
637 e_com = s_com; 635 e_com = s_com;
638 } 636 }
639 } 637 } else if (type_code != comment) /* preserve force_nl thru a comment */
640 else if (type_code != comment) /* preserve force_nl thru a comment */ 
641 force_nl = false; /* cancel forced newline after newline, form 638 force_nl = false; /* cancel forced newline after newline, form
642 * feed, etc */ 639 * feed, etc */
643 640
644 641
645 642
646 /*-----------------------------------------------------*\ 643 /*-----------------------------------------------------*\
647 | do switch on type of token scanned | 644 | do switch on type of token scanned |
648 \*-----------------------------------------------------*/ 645 \*-----------------------------------------------------*/
649 check_size_code(3); /* maximum number of increments of e_code 646 check_size_code(3); /* maximum number of increments of e_code
650 * before the next check_size_code or 647 * before the next check_size_code or
651 * dump_line() is 2. After that there's the 648 * dump_line() is 2. After that there's the
652 * final increment for the null character. */ 649 * final increment for the null character. */
653 switch (type_code) { /* now, decide what to do with the token */ 650 switch (type_code) { /* now, decide what to do with the token */
@@ -671,28 +668,27 @@ main(int argc, char **argv) @@ -671,28 +668,27 @@ main(int argc, char **argv)
671 /* count parens to make Healy happy */ 668 /* count parens to make Healy happy */
672 if (++ps.p_l_follow == nitems(ps.paren_indents)) { 669 if (++ps.p_l_follow == nitems(ps.paren_indents)) {
673 diag(0, "Reached internal limit of %zu unclosed parens", 670 diag(0, "Reached internal limit of %zu unclosed parens",
674 nitems(ps.paren_indents)); 671 nitems(ps.paren_indents));
675 ps.p_l_follow--; 672 ps.p_l_follow--;
676 } 673 }
677 if (*token == '[') 674 if (*token == '[')
678 /* not a function pointer declaration or a function call */; 675 /* not a function pointer declaration or a function call */;
679 else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent && 676 else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
680 ps.procname[0] == '\0' && ps.paren_level == 0) { 677 ps.procname[0] == '\0' && ps.paren_level == 0) {
681 /* function pointer declarations */ 678 /* function pointer declarations */
682 indent_declaration(dec_ind, tabs_to_var); 679 indent_declaration(dec_ind, tabs_to_var);
683 ps.dumped_decl_indent = true; 680 ps.dumped_decl_indent = true;
684 } 681 } else if (ps.want_blank &&
685 else if (ps.want_blank && 
686 ((ps.last_token != ident && ps.last_token != funcname) || 682 ((ps.last_token != ident && ps.last_token != funcname) ||
687 opt.proc_calls_space || 683 opt.proc_calls_space ||
688 (ps.keyword == rw_sizeof ? opt.Bill_Shannon : 684 (ps.keyword == rw_sizeof ? opt.Bill_Shannon :
689 ps.keyword != rw_0 && ps.keyword != rw_offsetof))) 685 ps.keyword != rw_0 && ps.keyword != rw_offsetof)))
690 *e_code++ = ' '; 686 *e_code++ = ' ';
691 ps.want_blank = false; 687 ps.want_blank = false;
692 *e_code++ = token[0]; 688 *e_code++ = token[0];
693 ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1; 689 ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
694 if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent 690 if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
695 && ps.paren_indents[0] < 2 * opt.ind_size) 691 && ps.paren_indents[0] < 2 * opt.ind_size)
696 ps.paren_indents[0] = 2 * opt.ind_size; 692 ps.paren_indents[0] = 2 * opt.ind_size;
697 if (ps.in_or_st && *token == '(' && ps.tos <= 2) { 693 if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
698 /* 694 /*
@@ -746,28 +742,27 @@ main(int argc, char **argv) @@ -746,28 +742,27 @@ main(int argc, char **argv)
746 case unary_op: /* this could be any unary operation */ 742 case unary_op: /* this could be any unary operation */
747 if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init && 743 if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
748 ps.procname[0] == '\0' && ps.paren_level == 0) { 744 ps.procname[0] == '\0' && ps.paren_level == 0) {
749 /* pointer declarations */ 745 /* pointer declarations */
750 746
751 /* 747 /*
752 * if this is a unary op in a declaration, we should indent 748 * if this is a unary op in a declaration, we should indent
753 * this token 749 * this token
754 */ 750 */
755 for (i = 0; token[i]; ++i) 751 for (i = 0; token[i]; ++i)
756 /* find length of token */; 752 /* find length of token */;
757 indent_declaration(dec_ind - i, tabs_to_var); 753 indent_declaration(dec_ind - i, tabs_to_var);
758 ps.dumped_decl_indent = true; 754 ps.dumped_decl_indent = true;
759 } 755 } else if (ps.want_blank)
760 else if (ps.want_blank) 
761 *e_code++ = ' '; 756 *e_code++ = ' ';
762 757
763 { 758 {
764 int len = e_token - s_token; 759 int len = e_token - s_token;
765 760
766 check_size_code(len); 761 check_size_code(len);
767 memcpy(e_code, token, len); 762 memcpy(e_code, token, len);
768 e_code += len; 763 e_code += len;
769 } 764 }
770 ps.want_blank = false; 765 ps.want_blank = false;
771 break; 766 break;
772 767
773 case binary_op: /* any binary operation */ 768 case binary_op: /* any binary operation */
@@ -896,28 +891,27 @@ main(int argc, char **argv) @@ -896,28 +891,27 @@ main(int argc, char **argv)
896 ps.in_stmt = false; /* dont indent the {} */ 891 ps.in_stmt = false; /* dont indent the {} */
897 if (!ps.block_init) 892 if (!ps.block_init)
898 force_nl = true;/* force other stuff on same line as '{' onto 893 force_nl = true;/* force other stuff on same line as '{' onto
899 * new line */ 894 * new line */
900 else if (ps.block_init_level <= 0) 895 else if (ps.block_init_level <= 0)
901 ps.block_init_level = 1; 896 ps.block_init_level = 1;
902 else 897 else
903 ps.block_init_level++; 898 ps.block_init_level++;
904 899
905 if (s_code != e_code && !ps.block_init) { 900 if (s_code != e_code && !ps.block_init) {
906 if (!opt.btype_2) { 901 if (!opt.btype_2) {
907 dump_line(); 902 dump_line();
908 ps.want_blank = false; 903 ps.want_blank = false;
909 } 904 } else if (ps.in_parameter_declaration && !ps.in_or_st) {
910 else if (ps.in_parameter_declaration && !ps.in_or_st) { 
911 ps.i_l_follow = 0; 905 ps.i_l_follow = 0;
912 if (opt.function_brace_split) { /* dump the line prior 906 if (opt.function_brace_split) { /* dump the line prior
913 * to the brace ... */ 907 * to the brace ... */
914 dump_line(); 908 dump_line();
915 ps.want_blank = false; 909 ps.want_blank = false;
916 } else /* add a space between the decl and brace */ 910 } else /* add a space between the decl and brace */
917 ps.want_blank = true; 911 ps.want_blank = true;
918 } 912 }
919 } 913 }
920 if (ps.in_parameter_declaration) 914 if (ps.in_parameter_declaration)
921 prefix_blankline_requested = 0; 915 prefix_blankline_requested = 0;
922 916
923 if (ps.p_l_follow > 0) { /* check for preceding unbalanced 917 if (ps.p_l_follow > 0) { /* check for preceding unbalanced
@@ -932,28 +926,27 @@ main(int argc, char **argv) @@ -932,28 +926,27 @@ main(int argc, char **argv)
932 } 926 }
933 if (s_code == e_code) 927 if (s_code == e_code)
934 ps.ind_stmt = false; /* dont put extra indentation on line 928 ps.ind_stmt = false; /* dont put extra indentation on line
935 * with '{' */ 929 * with '{' */
936 if (ps.in_decl && ps.in_or_st) { /* this is either a structure 930 if (ps.in_decl && ps.in_or_st) { /* this is either a structure
937 * declaration or an init */ 931 * declaration or an init */
938 di_stack[ps.dec_nest] = dec_ind; 932 di_stack[ps.dec_nest] = dec_ind;
939 if (++ps.dec_nest == nitems(di_stack)) { 933 if (++ps.dec_nest == nitems(di_stack)) {
940 diag(0, "Reached internal limit of %zu struct levels", 934 diag(0, "Reached internal limit of %zu struct levels",
941 nitems(di_stack)); 935 nitems(di_stack));
942 ps.dec_nest--; 936 ps.dec_nest--;
943 } 937 }
944 /* ? dec_ind = 0; */ 938 /* ? dec_ind = 0; */
945 } 939 } else {
946 else { 
947 ps.decl_on_line = false; /* we can't be in the middle of 940 ps.decl_on_line = false; /* we can't be in the middle of
948 * a declaration, so don't do 941 * a declaration, so don't do
949 * special indentation of 942 * special indentation of
950 * comments */ 943 * comments */
951 if (opt.blanklines_after_declarations_at_proctop 944 if (opt.blanklines_after_declarations_at_proctop
952 && ps.in_parameter_declaration) 945 && ps.in_parameter_declaration)
953 postfix_blankline_requested = 1; 946 postfix_blankline_requested = 1;
954 ps.in_parameter_declaration = 0; 947 ps.in_parameter_declaration = 0;
955 ps.in_decl = false; 948 ps.in_decl = false;
956 } 949 }
957 dec_ind = 0; 950 dec_ind = 0;
958 parse(lbrace); /* let parser know about this */ 951 parse(lbrace); /* let parser know about this */
959 if (ps.want_blank) /* put a blank before '{' if '{' is not at 952 if (ps.want_blank) /* put a blank before '{' if '{' is not at
@@ -1020,28 +1013,27 @@ main(int argc, char **argv) @@ -1020,28 +1013,27 @@ main(int argc, char **argv)
1020 1013
1021 case keyword_do_else: 1014 case keyword_do_else:
1022 ps.in_stmt = false; 1015 ps.in_stmt = false;
1023 if (*token == 'e') { 1016 if (*token == 'e') {
1024 if (e_code != s_code && (!opt.cuddle_else || e_code[-1] != '}')) { 1017 if (e_code != s_code && (!opt.cuddle_else || e_code[-1] != '}')) {
1025 if (opt.verbose) 1018 if (opt.verbose)
1026 diag(0, "Line broken"); 1019 diag(0, "Line broken");
1027 dump_line();/* make sure this starts a line */ 1020 dump_line();/* make sure this starts a line */
1028 ps.want_blank = false; 1021 ps.want_blank = false;
1029 } 1022 }
1030 force_nl = true;/* also, following stuff must go onto new line */ 1023 force_nl = true;/* also, following stuff must go onto new line */
1031 last_else = 1; 1024 last_else = 1;
1032 parse(keyword_else); 1025 parse(keyword_else);
1033 } 1026 } else {
1034 else { 
1035 if (e_code != s_code) { /* make sure this starts a line */ 1027 if (e_code != s_code) { /* make sure this starts a line */
1036 if (opt.verbose) 1028 if (opt.verbose)
1037 diag(0, "Line broken"); 1029 diag(0, "Line broken");
1038 dump_line(); 1030 dump_line();
1039 ps.want_blank = false; 1031 ps.want_blank = false;
1040 } 1032 }
1041 force_nl = true;/* also, following stuff must go onto new line */ 1033 force_nl = true;/* also, following stuff must go onto new line */
1042 last_else = 0; 1034 last_else = 0;
1043 parse(keyword_do); 1035 parse(keyword_do);
1044 } 1036 }
1045 goto copy_id; /* move the token into line */ 1037 goto copy_id; /* move the token into line */
1046 1038
1047 case type_def: 1039 case type_def:
@@ -1082,41 +1074,38 @@ main(int argc, char **argv) @@ -1082,41 +1074,38 @@ main(int argc, char **argv)
1082 dec_ind = opt.local_decl_indent > 0 ? opt.local_decl_indent : i; 1074 dec_ind = opt.local_decl_indent > 0 ? opt.local_decl_indent : i;
1083 tabs_to_var = (opt.use_tabs ? opt.local_decl_indent > 0 : 0); 1075 tabs_to_var = (opt.use_tabs ? opt.local_decl_indent > 0 : 0);
1084 } 1076 }
1085 goto copy_id; 1077 goto copy_id;
1086 1078
1087 case funcname: 1079 case funcname:
1088 case ident: /* got an identifier or constant */ 1080 case ident: /* got an identifier or constant */
1089 if (ps.in_decl) { 1081 if (ps.in_decl) {
1090 if (type_code == funcname) { 1082 if (type_code == funcname) {
1091 ps.in_decl = false; 1083 ps.in_decl = false;
1092 if (opt.procnames_start_line && s_code != e_code) { 1084 if (opt.procnames_start_line && s_code != e_code) {
1093 *e_code = '\0'; 1085 *e_code = '\0';
1094 dump_line(); 1086 dump_line();
1095 } 1087 } else if (ps.want_blank) {
1096 else if (ps.want_blank) { 
1097 *e_code++ = ' '; 1088 *e_code++ = ' ';
1098 } 1089 }
1099 ps.want_blank = false; 1090 ps.want_blank = false;
1100 } 1091 } else if (!ps.block_init && !ps.dumped_decl_indent &&
1101 else if (!ps.block_init && !ps.dumped_decl_indent && 
1102 ps.paren_level == 0) { /* if we are in a declaration, we 1092 ps.paren_level == 0) { /* if we are in a declaration, we
1103 * must indent identifier */ 1093 * must indent identifier */
1104 indent_declaration(dec_ind, tabs_to_var); 1094 indent_declaration(dec_ind, tabs_to_var);
1105 ps.dumped_decl_indent = true; 1095 ps.dumped_decl_indent = true;
1106 ps.want_blank = false; 1096 ps.want_blank = false;
1107 } 1097 }
1108 } 1098 } else if (sp_sw && ps.p_l_follow == 0) {
1109 else if (sp_sw && ps.p_l_follow == 0) { 
1110 sp_sw = false; 1099 sp_sw = false;
1111 force_nl = true; 1100 force_nl = true;
1112 ps.last_u_d = true; 1101 ps.last_u_d = true;
1113 ps.in_stmt = false; 1102 ps.in_stmt = false;
1114 parse(hd_type); 1103 parse(hd_type);
1115 } 1104 }
1116 copy_id: 1105 copy_id:
1117 { 1106 {
1118 int len = e_token - s_token; 1107 int len = e_token - s_token;
1119 1108
1120 check_size_code(len + 1); 1109 check_size_code(len + 1);
1121 if (ps.want_blank) 1110 if (ps.want_blank)
1122 *e_code++ = ' '; 1111 *e_code++ = ' ';
@@ -1221,28 +1210,27 @@ main(int argc, char **argv) @@ -1221,28 +1210,27 @@ main(int argc, char **argv)
1221 } 1210 }
1222 break; 1211 break;
1223 } 1212 }
1224 } 1213 }
1225 1214
1226 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 1215 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1227 e_lab--; 1216 e_lab--;
1228 if (e_lab - s_lab == com_end && bp_save == NULL) { 1217 if (e_lab - s_lab == com_end && bp_save == NULL) {
1229 /* comment on preprocessor line */ 1218 /* comment on preprocessor line */
1230 if (sc_end == NULL) { /* if this is the first comment, 1219 if (sc_end == NULL) { /* if this is the first comment,
1231 * we must set up the buffer */ 1220 * we must set up the buffer */
1232 save_com = sc_buf; 1221 save_com = sc_buf;
1233 sc_end = &save_com[0]; 1222 sc_end = &save_com[0];
1234 } 1223 } else {
1235 else { 
1236 *sc_end++ = '\n'; /* add newline between 1224 *sc_end++ = '\n'; /* add newline between
1237 * comments */ 1225 * comments */
1238 *sc_end++ = ' '; 1226 *sc_end++ = ' ';
1239 --line_no; 1227 --line_no;
1240 } 1228 }
1241 if (sc_end - save_com + com_end - com_start > sc_size) 1229 if (sc_end - save_com + com_end - com_start > sc_size)
1242 errx(1, "input too long"); 1230 errx(1, "input too long");
1243 memmove(sc_end, s_lab + com_start, com_end - com_start); 1231 memmove(sc_end, s_lab + com_start, com_end - com_start);
1244 sc_end += com_end - com_start; 1232 sc_end += com_end - com_start;
1245 e_lab = s_lab + com_start; 1233 e_lab = s_lab + com_start;
1246 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 1234 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1247 e_lab--; 1235 e_lab--;
1248 bp_save = buf_ptr; /* save current input buffer */ 1236 bp_save = buf_ptr; /* save current input buffer */
@@ -1253,39 +1241,36 @@ main(int argc, char **argv) @@ -1253,39 +1241,36 @@ main(int argc, char **argv)
1253 *sc_end++ = ' '; /* add trailing blank, just in case */ 1241 *sc_end++ = ' '; /* add trailing blank, just in case */
1254 buf_end = sc_end; 1242 buf_end = sc_end;
1255 sc_end = NULL; 1243 sc_end = NULL;
1256 } 1244 }
1257 check_size_label(1); 1245 check_size_label(1);
1258 *e_lab = '\0'; /* null terminate line */ 1246 *e_lab = '\0'; /* null terminate line */
1259 ps.pcase = false; 1247 ps.pcase = false;
1260 } 1248 }
1261 1249
1262 if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */ 1250 if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
1263 if ((size_t)ifdef_level < nitems(state_stack)) { 1251 if ((size_t)ifdef_level < nitems(state_stack)) {
1264 match_state[ifdef_level].tos = -1; 1252 match_state[ifdef_level].tos = -1;
1265 state_stack[ifdef_level++] = ps; 1253 state_stack[ifdef_level++] = ps;
1266 } 1254 } else
1267 else 
1268 diag(1, "#if stack overflow"); 1255 diag(1, "#if stack overflow");
1269 } 1256 } else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
1270 else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */ 
1271 if (ifdef_level <= 0) 1257 if (ifdef_level <= 0)
1272 diag(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else"); 1258 diag(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1273 else { 1259 else {
1274 match_state[ifdef_level - 1] = ps; 1260 match_state[ifdef_level - 1] = ps;
1275 ps = state_stack[ifdef_level - 1]; 1261 ps = state_stack[ifdef_level - 1];
1276 } 1262 }
1277 } 1263 } else if (strncmp(s_lab, "#endif", 6) == 0) {
1278 else if (strncmp(s_lab, "#endif", 6) == 0) { 
1279 if (ifdef_level <= 0) 1264 if (ifdef_level <= 0)
1280 diag(1, "Unmatched #endif"); 1265 diag(1, "Unmatched #endif");
1281 else 1266 else
1282 ifdef_level--; 1267 ifdef_level--;
1283 } else { 1268 } else {
1284 static const struct directives { 1269 static const struct directives {
1285 int size; 1270 int size;
1286 const char *string; 1271 const char *string;
1287 } recognized[] = { 1272 } recognized[] = {
1288 {7, "include"}, 1273 {7, "include"},
1289 {6, "define"}, 1274 {6, "define"},
1290 {5, "undef"}, 1275 {5, "undef"},
1291 {4, "line"}, 1276 {4, "line"},
@@ -1294,28 +1279,27 @@ main(int argc, char **argv) @@ -1294,28 +1279,27 @@ main(int argc, char **argv)
1294 }; 1279 };
1295 int d = nitems(recognized); 1280 int d = nitems(recognized);
1296 while (--d >= 0) 1281 while (--d >= 0)
1297 if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0) 1282 if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
1298 break; 1283 break;
1299 if (d < 0) { 1284 if (d < 0) {
1300 diag(1, "Unrecognized cpp directive"); 1285 diag(1, "Unrecognized cpp directive");
1301 break; 1286 break;
1302 } 1287 }
1303 } 1288 }
1304 if (opt.blanklines_around_conditional_compilation) { 1289 if (opt.blanklines_around_conditional_compilation) {
1305 postfix_blankline_requested++; 1290 postfix_blankline_requested++;
1306 n_real_blanklines = 0; 1291 n_real_blanklines = 0;
1307 } 1292 } else {
1308 else { 
1309 postfix_blankline_requested = 0; 1293 postfix_blankline_requested = 0;
1310 prefix_blankline_requested = 0; 1294 prefix_blankline_requested = 0;
1311 } 1295 }
1312 break; /* subsequent processing of the newline 1296 break; /* subsequent processing of the newline
1313 * character will cause the line to be printed */ 1297 * character will cause the line to be printed */
1314 1298
1315 case comment: /* we have gotten a / followed by * this is a biggie */ 1299 case comment: /* we have gotten a / followed by * this is a biggie */
1316 pr_comment(); 1300 pr_comment();
1317 break; 1301 break;
1318 1302
1319 default: 1303 default:
1320 break; 1304 break;
1321 } /* end of big switch stmt */ 1305 } /* end of big switch stmt */

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

--- src/usr.bin/indent/io.c 2021/03/12 19:14:18 1.29
+++ src/usr.bin/indent/io.c 2021/03/12 23:10:18 1.30
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.29 2021/03/12 19:14:18 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.30 2021/03/12 23:10:18 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
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
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#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 42static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: io.c,v 1.29 2021/03/12 19:14:18 rillig Exp $"); 49__RCSID("$NetBSD: io.c,v 1.30 2021/03/12 23:10:18 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
52#endif 52#endif
53#endif 53#endif
54 54
55#include <ctype.h> 55#include <ctype.h>
56#include <err.h> 56#include <err.h>
57#include <stdio.h> 57#include <stdio.h>
58#include <stdlib.h> 58#include <stdlib.h>
59#include <string.h> 59#include <string.h>
60#include <stdarg.h> 60#include <stdarg.h>
61 61
62#include "indent.h" 62#include "indent.h"
@@ -94,43 +94,42 @@ output_int(int i) @@ -94,43 +94,42 @@ output_int(int i)
94 * source. It prints the label section, followed by the code section with 94 * source. It prints the label section, followed by the code section with
95 * the appropriate nesting level, followed by any comments. 95 * the appropriate nesting level, followed by any comments.
96 */ 96 */
97void 97void
98dump_line(void) 98dump_line(void)
99{ 99{
100 int cur_col, target_col; 100 int cur_col, target_col;
101 static int not_first_line; 101 static int not_first_line;
102 102
103 if (ps.procname[0]) { 103 if (ps.procname[0]) {
104 ps.ind_level = 0; 104 ps.ind_level = 0;
105 ps.procname[0] = 0; 105 ps.procname[0] = 0;
106 } 106 }
 107
107 if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 108 if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
108 if (suppress_blanklines > 0) 109 if (suppress_blanklines > 0)
109 suppress_blanklines--; 110 suppress_blanklines--;
110 else { 111 else {
111 ps.bl_line = true; 112 ps.bl_line = true;
112 n_real_blanklines++; 113 n_real_blanklines++;
113 } 114 }
114 } 115 } else if (!inhibit_formatting) {
115 else if (!inhibit_formatting) { 
116 suppress_blanklines = 0; 116 suppress_blanklines = 0;
117 ps.bl_line = false; 117 ps.bl_line = false;
118 if (prefix_blankline_requested && not_first_line) { 118 if (prefix_blankline_requested && not_first_line) {
119 if (opt.swallow_optional_blanklines) { 119 if (opt.swallow_optional_blanklines) {
120 if (n_real_blanklines == 1) 120 if (n_real_blanklines == 1)
121 n_real_blanklines = 0; 121 n_real_blanklines = 0;
122 } 122 } else {
123 else { 
124 if (n_real_blanklines == 0) 123 if (n_real_blanklines == 0)
125 n_real_blanklines = 1; 124 n_real_blanklines = 1;
126 } 125 }
127 } 126 }
128 while (--n_real_blanklines >= 0) 127 while (--n_real_blanklines >= 0)
129 output_char('\n'); 128 output_char('\n');
130 n_real_blanklines = 0; 129 n_real_blanklines = 0;
131 if (ps.ind_level == 0) 130 if (ps.ind_level == 0)
132 ps.ind_stmt = 0; /* this is a class A kludge. dont do 131 ps.ind_stmt = 0; /* this is a class A kludge. dont do
133 * additional statement indentation if we are 132 * additional statement indentation if we are
134 * at bracket level 0 */ 133 * at bracket level 0 */
135 134
136 if (e_lab != s_lab || e_code != s_code) 135 if (e_lab != s_lab || e_code != s_code)
@@ -158,28 +157,27 @@ dump_line(void) @@ -158,28 +157,27 @@ dump_line(void)
158 if (s < e_lab) { 157 if (s < e_lab) {
159 if (s[0] == '/' && s[1] == '*') { 158 if (s[0] == '/' && s[1] == '*') {
160 output_char('\t'); 159 output_char('\t');
161 output_range(s, e_lab); 160 output_range(s, e_lab);
162 } else { 161 } else {
163 output_string("\t/* "); 162 output_string("\t/* ");
164 output_range(s, e_lab); 163 output_range(s, e_lab);
165 output_string(" */"); 164 output_string(" */");
166 } 165 }
167 } 166 }
168 } else 167 } else
169 output_range(s_lab, e_lab); 168 output_range(s_lab, e_lab);
170 cur_col = count_spaces(cur_col, s_lab); 169 cur_col = count_spaces(cur_col, s_lab);
171 } 170 } else
172 else 
173 cur_col = 1; /* there is no label section */ 171 cur_col = 1; /* there is no label section */
174 172
175 ps.pcase = false; 173 ps.pcase = false;
176 174
177 if (s_code != e_code) { /* print code section, if any */ 175 if (s_code != e_code) { /* print code section, if any */
178 char *p; 176 char *p;
179 177
180 if (comment_open) { 178 if (comment_open) {
181 comment_open = 0; 179 comment_open = 0;
182 output_string(".*/\n"); 180 output_string(".*/\n");
183 } 181 }
184 target_col = compute_code_target(); 182 target_col = compute_code_target();
185 { 183 {
@@ -201,52 +199,50 @@ dump_line(void) @@ -201,52 +199,50 @@ dump_line(void)
201 int target = ps.com_col; 199 int target = ps.com_col;
202 char *com_st = s_com; 200 char *com_st = s_com;
203 201
204 target += ps.comment_delta; 202 target += ps.comment_delta;
205 while (*com_st == '\t') /* consider original indentation in 203 while (*com_st == '\t') /* consider original indentation in
206 * case this is a box comment */ 204 * case this is a box comment */
207 com_st++, target += opt.tabsize; 205 com_st++, target += opt.tabsize;
208 while (target <= 0) 206 while (target <= 0)
209 if (*com_st == ' ') 207 if (*com_st == ' ')
210 target++, com_st++; 208 target++, com_st++;
211 else if (*com_st == '\t') { 209 else if (*com_st == '\t') {
212 target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1; 210 target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
213 com_st++; 211 com_st++;
214 } 212 } else
215 else 
216 target = 1; 213 target = 1;
217 if (cur_col > target) { /* if comment can't fit on this line, 214 if (cur_col > target) { /* if comment can't fit on this line,
218 * put it on next line */ 215 * put it on next line */
219 output_char('\n'); 216 output_char('\n');
220 cur_col = 1; 217 cur_col = 1;
221 ++ps.out_lines; 218 ++ps.out_lines;
222 } 219 }
223 while (e_com > com_st && isspace((unsigned char)e_com[-1])) 220 while (e_com > com_st && isspace((unsigned char)e_com[-1]))
224 e_com--; 221 e_com--;
225 (void)pad_output(cur_col, target); 222 (void)pad_output(cur_col, target);
226 output_range(com_st, e_com); 223 output_range(com_st, e_com);
227 ps.comment_delta = ps.n_comment_delta; 224 ps.comment_delta = ps.n_comment_delta;
228 ++ps.com_lines; /* count lines with comments */ 225 ++ps.com_lines; /* count lines with comments */
229 } 226 }
230 if (ps.use_ff) 227 if (ps.use_ff)
231 output_char('\014'); 228 output_char('\014');
232 else 229 else
233 output_char('\n'); 230 output_char('\n');
234 ++ps.out_lines; 231 ++ps.out_lines;
235 if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) { 232 if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
236 prefix_blankline_requested = 1; 233 prefix_blankline_requested = 1;
237 ps.just_saw_decl = 0; 234 ps.just_saw_decl = 0;
238 } 235 } else
239 else 
240 prefix_blankline_requested = postfix_blankline_requested; 236 prefix_blankline_requested = postfix_blankline_requested;
241 postfix_blankline_requested = 0; 237 postfix_blankline_requested = 0;
242 } 238 }
243 239
244 /* keep blank lines after '//' comments */ 240 /* keep blank lines after '//' comments */
245 if (e_com - s_com > 1 && s_com[1] == '/') 241 if (e_com - s_com > 1 && s_com[1] == '/')
246 output_range(s_token, e_token); 242 output_range(s_token, e_token);
247 243
248 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 244 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
249 * declaration, remember that fact for 245 * declaration, remember that fact for
250 * proper comment indentation */ 246 * proper comment indentation */
251 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 247 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
252 * indented if we have not 248 * indented if we have not
@@ -260,46 +256,45 @@ dump_line(void) @@ -260,46 +256,45 @@ dump_line(void)
260 *(e_com = s_com = combuf + 1) = '\0'; 256 *(e_com = s_com = combuf + 1) = '\0';
261 ps.ind_level = ps.i_l_follow; 257 ps.ind_level = ps.i_l_follow;
262 ps.paren_level = ps.p_l_follow; 258 ps.paren_level = ps.p_l_follow;
263 if (ps.paren_level > 0) 259 if (ps.paren_level > 0)
264 paren_target = -ps.paren_indents[ps.paren_level - 1]; 260 paren_target = -ps.paren_indents[ps.paren_level - 1];
265 not_first_line = 1; 261 not_first_line = 1;
266} 262}
267 263
268int 264int
269compute_code_target(void) 265compute_code_target(void)
270{ 266{
271 int target_col = opt.ind_size * ps.ind_level + 1; 267 int target_col = opt.ind_size * ps.ind_level + 1;
272 268
273 if (ps.paren_level) 269 if (ps.paren_level) {
274 if (!opt.lineup_to_parens) 270 if (!opt.lineup_to_parens)
275 target_col += opt.continuation_indent * 271 target_col += opt.continuation_indent *
276 (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level); 272 (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
277 else if (opt.lineup_to_parens_always) 273 else if (opt.lineup_to_parens_always)
278 target_col = paren_target; 274 target_col = paren_target;
279 else { 275 else {
280 int w; 276 int w;
281 int t = paren_target; 277 int t = paren_target;
282 278
283 if ((w = count_spaces(t, s_code) - opt.max_col) > 0 279 if ((w = count_spaces(t, s_code) - opt.max_col) > 0
284 && count_spaces(target_col, s_code) <= opt.max_col) { 280 && count_spaces(target_col, s_code) <= opt.max_col) {
285 t -= w + 1; 281 t -= w + 1;
286 if (t > target_col) 282 if (t > target_col)
287 target_col = t; 283 target_col = t;
288 } 284 } else
289 else 
290 target_col = t; 285 target_col = t;
291 } 286 }
292 else if (ps.ind_stmt) 287 } else if (ps.ind_stmt)
293 target_col += opt.continuation_indent; 288 target_col += opt.continuation_indent;
294 return target_col; 289 return target_col;
295} 290}
296 291
297int 292int
298compute_label_target(void) 293compute_label_target(void)
299{ 294{
300 return 295 return
301 ps.pcase ? (int) (case_ind * opt.ind_size) + 1 296 ps.pcase ? (int) (case_ind * opt.ind_size) + 1
302 : *s_lab == '#' ? 1 297 : *s_lab == '#' ? 1
303 : opt.ind_size * (ps.ind_level - label_offset) + 1; 298 : opt.ind_size * (ps.ind_level - label_offset) + 1;
304} 299}
305 300

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

--- src/usr.bin/indent/lexi.c 2021/03/12 18:11:50 1.37
+++ src/usr.bin/indent/lexi.c 2021/03/12 23:10:18 1.38
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lexi.c,v 1.37 2021/03/12 18:11:50 rillig Exp $ */ 1/* $NetBSD: lexi.c,v 1.38 2021/03/12 23:10:18 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
@@ -36,27 +36,27 @@ @@ -36,27 +36,27 @@
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#if 0 40#if 0
41#ifndef lint 41#ifndef lint
42static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93"; 42static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
43#endif /* not lint */ 43#endif /* not lint */
44#endif 44#endif
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47#ifndef lint 47#ifndef lint
48#if defined(__NetBSD__) 48#if defined(__NetBSD__)
49__RCSID("$NetBSD: lexi.c,v 1.37 2021/03/12 18:11:50 rillig Exp $"); 49__RCSID("$NetBSD: lexi.c,v 1.38 2021/03/12 23:10:18 rillig Exp $");
50#elif defined(__FreeBSD__) 50#elif defined(__FreeBSD__)
51__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); 51__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
52#endif 52#endif
53#endif 53#endif
54 54
55/* 55/*
56 * Here we have the token scanner for indent. It scans off one token and puts 56 * Here we have the token scanner for indent. It scans off one token and puts
57 * it in the global variable "token". It returns a code, indicating the type 57 * it in the global variable "token". It returns a code, indicating the type
58 * of token scanned. 58 * of token scanned.
59 */ 59 */
60 60
61#include <assert.h> 61#include <assert.h>
62#include <err.h> 62#include <err.h>
@@ -331,43 +331,43 @@ lexi(struct parser_state *state) @@ -331,43 +331,43 @@ lexi(struct parser_state *state)
331 331
332 for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) { 332 for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
333 i = (unsigned char)*buf_ptr; 333 i = (unsigned char)*buf_ptr;
334 if (i >= nitems(table) || table[i] == NULL || 334 if (i >= nitems(table) || table[i] == NULL ||
335 table[i][s - 'A'] == ' ') { 335 table[i][s - 'A'] == ' ') {
336 s = table[0][s - 'A']; 336 s = table[0][s - 'A'];
337 break; 337 break;
338 } 338 }
339 s = table[i][s - 'A']; 339 s = table[i][s - 'A'];
340 check_size_token(1); 340 check_size_token(1);
341 *e_token++ = inbuf_next(); 341 *e_token++ = inbuf_next();
342 } 342 }
343 /* s now indicates the type: f(loating), i(integer), u(nknown) */ 343 /* s now indicates the type: f(loating), i(integer), u(nknown) */
344 } 344 } else {
345 else 
346 while (isalnum((unsigned char)*buf_ptr) || 345 while (isalnum((unsigned char)*buf_ptr) ||
347 *buf_ptr == '\\' || 346 *buf_ptr == '\\' ||
348 *buf_ptr == '_' || *buf_ptr == '$') { 347 *buf_ptr == '_' || *buf_ptr == '$') {
349 /* fill_buffer() terminates buffer with newline */ 348 /* fill_buffer() terminates buffer with newline */
350 if (*buf_ptr == '\\') { 349 if (*buf_ptr == '\\') {
351 if (buf_ptr[1] == '\n') { 350 if (buf_ptr[1] == '\n') {
352 buf_ptr += 2; 351 buf_ptr += 2;
353 if (buf_ptr >= buf_end) 352 if (buf_ptr >= buf_end)
354 fill_buffer(); 353 fill_buffer();
355 } else 354 } else
356 break; 355 break;
357 } 356 }
358 check_size_token(1); 357 check_size_token(1);
359 *e_token++ = inbuf_next(); 358 *e_token++ = inbuf_next();
360 } 359 }
 360 }
361 *e_token = '\0'; 361 *e_token = '\0';
362 362
363 if (s_token[0] == 'L' && s_token[1] == '\0' && 363 if (s_token[0] == 'L' && s_token[1] == '\0' &&
364 (*buf_ptr == '"' || *buf_ptr == '\'')) 364 (*buf_ptr == '"' || *buf_ptr == '\''))
365 return lexi_end(string_prefix); 365 return lexi_end(string_prefix);
366 366
367 while (*buf_ptr == ' ' || *buf_ptr == '\t') /* get rid of blanks */ 367 while (*buf_ptr == ' ' || *buf_ptr == '\t') /* get rid of blanks */
368 inbuf_next(); 368 inbuf_next();
369 state->keyword = rw_0; 369 state->keyword = rw_0;
370 if (state->last_token == keyword_struct_union_enum && 370 if (state->last_token == keyword_struct_union_enum &&
371 !state->p_l_follow) { 371 !state->p_l_follow) {
372 /* if last token was 'struct' and we're not in parentheses, then 372 /* if last token was 'struct' and we're not in parentheses, then
373 * this token should be treated as a declaration */ 373 * this token should be treated as a declaration */
@@ -494,28 +494,27 @@ lexi(struct parser_state *state) @@ -494,28 +494,27 @@ lexi(struct parser_state *state)
494 while (1) { /* move one character or [/<char>]<char> */ 494 while (1) { /* move one character or [/<char>]<char> */
495 if (*buf_ptr == '\n') { 495 if (*buf_ptr == '\n') {
496 diag(1, "Unterminated literal"); 496 diag(1, "Unterminated literal");
497 goto stop_lit; 497 goto stop_lit;
498 } 498 }
499 check_size_token(2); 499 check_size_token(2);
500 *e_token = inbuf_next(); 500 *e_token = inbuf_next();
501 if (*e_token == '\\') { /* if escape, copy extra char */ 501 if (*e_token == '\\') { /* if escape, copy extra char */
502 if (*buf_ptr == '\n') /* check for escaped newline */ 502 if (*buf_ptr == '\n') /* check for escaped newline */
503 ++line_no; 503 ++line_no;
504 *++e_token = inbuf_next(); 504 *++e_token = inbuf_next();
505 ++e_token; /* we must increment this again because we 505 ++e_token; /* we must increment this again because we
506 * copied two chars */ 506 * copied two chars */
507 } 507 } else
508 else 
509 break; /* we copied one character */ 508 break; /* we copied one character */
510 } /* end of while (1) */ 509 } /* end of while (1) */
511 } while (*e_token++ != qchar); 510 } while (*e_token++ != qchar);
512stop_lit: 511stop_lit:
513 code = ident; 512 code = ident;
514 break; 513 break;
515 514
516 case ('('): 515 case ('('):
517 case ('['): 516 case ('['):
518 unary_delim = true; 517 unary_delim = true;
519 code = lparen; 518 code = lparen;
520 break; 519 break;
521 520
@@ -581,28 +580,27 @@ stop_lit: @@ -581,28 +580,27 @@ stop_lit:
581 case '+': /* check for -, +, --, ++ */ 580 case '+': /* check for -, +, --, ++ */
582 code = (state->last_u_d ? unary_op : binary_op); 581 code = (state->last_u_d ? unary_op : binary_op);
583 unary_delim = true; 582 unary_delim = true;
584 583
585 if (*buf_ptr == token[0]) { 584 if (*buf_ptr == token[0]) {
586 /* check for doubled character */ 585 /* check for doubled character */
587 *e_token++ = *buf_ptr++; 586 *e_token++ = *buf_ptr++;
588 /* buffer overflow will be checked at end of loop */ 587 /* buffer overflow will be checked at end of loop */
589 if (state->last_token == ident || state->last_token == rparen) { 588 if (state->last_token == ident || state->last_token == rparen) {
590 code = (state->last_u_d ? unary_op : postfix_op); 589 code = (state->last_u_d ? unary_op : postfix_op);
591 /* check for following ++ or -- */ 590 /* check for following ++ or -- */
592 unary_delim = false; 591 unary_delim = false;
593 } 592 }
594 } 593 } else if (*buf_ptr == '=')
595 else if (*buf_ptr == '=') 
596 /* check for operator += */ 594 /* check for operator += */
597 *e_token++ = *buf_ptr++; 595 *e_token++ = *buf_ptr++;
598 else if (*buf_ptr == '>') { 596 else if (*buf_ptr == '>') {
599 /* check for operator -> */ 597 /* check for operator -> */
600 *e_token++ = *buf_ptr++; 598 *e_token++ = *buf_ptr++;
601 unary_delim = false; 599 unary_delim = false;
602 code = unary_op; 600 code = unary_op;
603 state->want_blank = false; 601 state->want_blank = false;
604 } 602 }
605 break; /* buffer overflow will be checked at end of 603 break; /* buffer overflow will be checked at end of
606 * switch */ 604 * switch */
607 605
608 case '=': 606 case '=':
@@ -705,28 +703,27 @@ add_typename(const char *key) @@ -705,28 +703,27 @@ add_typename(const char *key)
705 if (typename_top + 1 >= typename_count) { 703 if (typename_top + 1 >= typename_count) {
706 typenames = realloc((void *)typenames, 704 typenames = realloc((void *)typenames,
707 sizeof(typenames[0]) * (typename_count *= 2)); 705 sizeof(typenames[0]) * (typename_count *= 2));
708 if (typenames == NULL) 706 if (typenames == NULL)
709 err(1, NULL); 707 err(1, NULL);
710 } 708 }
711 if (typename_top == -1) 709 if (typename_top == -1)
712 typenames[++typename_top] = copy = strdup(key); 710 typenames[++typename_top] = copy = strdup(key);
713 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) { 711 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
714 /* take advantage of sorted input */ 712 /* take advantage of sorted input */
715 if (comparison == 0) /* remove duplicates */ 713 if (comparison == 0) /* remove duplicates */
716 return; 714 return;
717 typenames[++typename_top] = copy = strdup(key); 715 typenames[++typename_top] = copy = strdup(key);
718 } 716 } else {
719 else { 
720 int p; 717 int p;
721 718
722 for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++) 719 for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
723 /* find place for the new key */; 720 /* find place for the new key */;
724 if (comparison == 0) /* remove duplicates */ 721 if (comparison == 0) /* remove duplicates */
725 return; 722 return;
726 memmove(&typenames[p + 1], &typenames[p], 723 memmove(&typenames[p + 1], &typenames[p],
727 sizeof(typenames[0]) * (++typename_top - p)); 724 sizeof(typenames[0]) * (++typename_top - p));
728 typenames[p] = copy = strdup(key); 725 typenames[p] = copy = strdup(key);
729 } 726 }
730 727
731 if (copy == NULL) 728 if (copy == NULL)
732 err(1, NULL); 729 err(1, NULL);

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

--- src/usr.bin/indent/parse.c 2021/03/09 19:14:39 1.17
+++ src/usr.bin/indent/parse.c 2021/03/12 23:10:18 1.18
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: parse.c,v 1.17 2021/03/09 19:14:39 rillig Exp $ */ 1/* $NetBSD: parse.c,v 1.18 2021/03/12 23:10:18 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
@@ -146,28 +146,27 @@ parse(token_type tk) /* tk: the code fo @@ -146,28 +146,27 @@ parse(token_type tk) /* tk: the code fo
146 ps.p_stack[++ps.tos] = lbrace; 146 ps.p_stack[++ps.tos] = lbrace;
147 ps.il[ps.tos] = ps.ind_level; 147 ps.il[ps.tos] = ps.ind_level;
148 ps.p_stack[++ps.tos] = stmt; 148 ps.p_stack[++ps.tos] = stmt;
149 /* allow null stmt between braces */ 149 /* allow null stmt between braces */
150 ps.il[ps.tos] = ps.i_l_follow; 150 ps.il[ps.tos] = ps.i_l_follow;
151 break; 151 break;
152 152
153 case while_expr: /* 'while' '(' <expr> ')' */ 153 case while_expr: /* 'while' '(' <expr> ')' */
154 if (ps.p_stack[ps.tos] == do_stmt) { 154 if (ps.p_stack[ps.tos] == do_stmt) {
155 /* it is matched with do stmt */ 155 /* it is matched with do stmt */
156 ps.ind_level = ps.i_l_follow = ps.il[ps.tos]; 156 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
157 ps.p_stack[++ps.tos] = while_expr; 157 ps.p_stack[++ps.tos] = while_expr;
158 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow; 158 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
159 } 159 } else { /* it is a while loop */
160 else { /* it is a while loop */ 
161 ps.p_stack[++ps.tos] = while_expr; 160 ps.p_stack[++ps.tos] = while_expr;
162 ps.il[ps.tos] = ps.i_l_follow; 161 ps.il[ps.tos] = ps.i_l_follow;
163 ++ps.i_l_follow; 162 ++ps.i_l_follow;
164 ps.search_brace = opt.btype_2; 163 ps.search_brace = opt.btype_2;
165 } 164 }
166 165
167 break; 166 break;
168 167
169 case keyword_else: 168 case keyword_else:
170 if (ps.p_stack[ps.tos] != if_expr_stmt) 169 if (ps.p_stack[ps.tos] != if_expr_stmt)
171 diag(1, "Unmatched 'else'"); 170 diag(1, "Unmatched 'else'");
172 else { 171 else {
173 ps.ind_level = ps.il[ps.tos]; /* indentation for else should 172 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
@@ -175,28 +174,27 @@ parse(token_type tk) /* tk: the code fo @@ -175,28 +174,27 @@ parse(token_type tk) /* tk: the code fo
175 ps.i_l_follow = ps.ind_level + 1; /* everything following should 174 ps.i_l_follow = ps.ind_level + 1; /* everything following should
176 * be in 1 level */ 175 * be in 1 level */
177 ps.p_stack[ps.tos] = if_expr_stmt_else; 176 ps.p_stack[ps.tos] = if_expr_stmt_else;
178 /* remember if with else */ 177 /* remember if with else */
179 ps.search_brace = opt.btype_2 | opt.else_if; 178 ps.search_brace = opt.btype_2 | opt.else_if;
180 } 179 }
181 break; 180 break;
182 181
183 case rbrace: /* scanned a } */ 182 case rbrace: /* scanned a } */
184 /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ 183 /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
185 if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) { 184 if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
186 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos]; 185 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
187 ps.p_stack[ps.tos] = stmt; 186 ps.p_stack[ps.tos] = stmt;
188 } 187 } else
189 else 
190 diag(1, "Statement nesting error"); 188 diag(1, "Statement nesting error");
191 break; 189 break;
192 190
193 case switch_expr: /* had switch (...) */ 191 case switch_expr: /* had switch (...) */
194 ps.p_stack[++ps.tos] = switch_expr; 192 ps.p_stack[++ps.tos] = switch_expr;
195 ps.cstk[ps.tos] = case_ind; 193 ps.cstk[ps.tos] = case_ind;
196 /* save current case indent level */ 194 /* save current case indent level */
197 ps.il[ps.tos] = ps.i_l_follow; 195 ps.il[ps.tos] = ps.i_l_follow;
198 case_ind = ps.i_l_follow + opt.case_indent; /* cases should be one 196 case_ind = ps.i_l_follow + opt.case_indent; /* cases should be one
199 * level down from 197 * level down from
200 * switch */ 198 * switch */
201 ps.i_l_follow += opt.case_indent + 1; /* statements should be two 199 ps.i_l_follow += opt.case_indent + 1; /* statements should be two
202 * levels in */ 200 * levels in */