| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: io.c,v 1.211 2023/06/10 07:48:55 rillig Exp $ */ | | 1 | /* $NetBSD: io.c,v 1.212 2023/06/10 07:53:00 rillig Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * SPDX-License-Identifier: BSD-4-Clause | | 4 | * SPDX-License-Identifier: BSD-4-Clause |
5 | * | | 5 | * |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. |
7 | * Copyright (c) 1980, 1993 | | 7 | * Copyright (c) 1980, 1993 |
8 | * The Regents of the University of California. All rights reserved. | | 8 | * The Regents of the University of California. All rights reserved. |
9 | * All rights reserved. | | 9 | * All rights reserved. |
10 | * | | 10 | * |
11 | * Redistribution and use in source and binary forms, with or without | | 11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | | 12 | * modification, are permitted provided that the following conditions |
13 | * are met: | | 13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright | | 14 | * 1. Redistributions of source code must retain the above copyright |
| @@ -28,27 +28,27 @@ | | | @@ -28,27 +28,27 @@ |
28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
37 | * SUCH DAMAGE. | | 37 | * SUCH DAMAGE. |
38 | */ | | 38 | */ |
39 | | | 39 | |
40 | #include <sys/cdefs.h> | | 40 | #include <sys/cdefs.h> |
41 | __RCSID("$NetBSD: io.c,v 1.211 2023/06/10 07:48:55 rillig Exp $"); | | 41 | __RCSID("$NetBSD: io.c,v 1.212 2023/06/10 07:53:00 rillig Exp $"); |
42 | | | 42 | |
43 | #include <stdio.h> | | 43 | #include <stdio.h> |
44 | | | 44 | |
45 | #include "indent.h" | | 45 | #include "indent.h" |
46 | | | 46 | |
47 | struct buffer inp; | | 47 | struct buffer inp; |
48 | const char *inp_p; | | 48 | const char *inp_p; |
49 | | | 49 | |
50 | struct output_state out; | | 50 | struct output_state out; |
51 | enum indent_enabled indent_enabled; | | 51 | enum indent_enabled indent_enabled; |
52 | static int out_ind; /* width of the line that is being written */ | | 52 | static int out_ind; /* width of the line that is being written */ |
53 | static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a | | 53 | static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a |
54 | * single '\n', > 1 means there were (n | | 54 | * single '\n', > 1 means there were (n |
| @@ -312,78 +312,81 @@ output_line_comment(void) | | | @@ -312,78 +312,81 @@ output_line_comment(void) |
312 | | | 312 | |
313 | if (out_ind > target_ind) | | 313 | if (out_ind > target_ind) |
314 | write_newline(); | | 314 | write_newline(); |
315 | | | 315 | |
316 | while (com.s + com.len > p && ch_isspace(com.s[com.len - 1])) | | 316 | while (com.s + com.len > p && ch_isspace(com.s[com.len - 1])) |
317 | com.len--; | | 317 | com.len--; |
318 | | | 318 | |
319 | write_indent(target_ind); | | 319 | write_indent(target_ind); |
320 | write_range(p, com.len - (size_t)(p - com.s)); | | 320 | write_range(p, com.len - (size_t)(p - com.s)); |
321 | | | 321 | |
322 | ps.comment_delta = ps.n_comment_delta; | | 322 | ps.comment_delta = ps.n_comment_delta; |
323 | } | | 323 | } |
324 | | | 324 | |
| | | 325 | static void |
| | | 326 | output_line_indented(void) |
| | | 327 | { |
| | | 328 | if (lab.len == 0 && code.len == 0 && com.len == 0) |
| | | 329 | out.line_kind = lk_blank; |
| | | 330 | |
| | | 331 | if (want_blank_line() && wrote_newlines < 2 |
| | | 332 | && out.line_kind != lk_blank) |
| | | 333 | write_newline(); |
| | | 334 | |
| | | 335 | /* This kludge aligns function definitions correctly. */ |
| | | 336 | if (ps.ind_level == 0) |
| | | 337 | ps.in_stmt_cont = false; |
| | | 338 | |
| | | 339 | if (opt.blank_line_after_decl && ps.declaration == decl_end |
| | | 340 | && ps.psyms.top > 1) { |
| | | 341 | ps.declaration = decl_no; |
| | | 342 | ps.blank_line_after_decl = true; |
| | | 343 | } |
| | | 344 | |
| | | 345 | if (opt.swallow_optional_blanklines |
| | | 346 | && out.line_kind == lk_blank |
| | | 347 | && is_blank_line_optional()) |
| | | 348 | return; |
| | | 349 | |
| | | 350 | if (lab.len > 0) |
| | | 351 | output_line_label(); |
| | | 352 | if (code.len > 0) |
| | | 353 | output_line_code(); |
| | | 354 | if (com.len > 0) |
| | | 355 | output_line_comment(); |
| | | 356 | |
| | | 357 | write_newline(); |
| | | 358 | out.prev_line_kind = out.line_kind; |
| | | 359 | } |
| | | 360 | |
325 | /* | | 361 | /* |
326 | * Write a line of formatted source to the output file. The line consists of | | 362 | * Write a line of formatted source to the output file. The line consists of |
327 | * the label, the code and the comment. | | 363 | * the label, the code and the comment. |
328 | */ | | 364 | */ |
329 | void | | 365 | void |
330 | output_line(void) | | 366 | output_line(void) |
331 | { | | 367 | { |
332 | debug_blank_line(); | | 368 | debug_blank_line(); |
333 | debug_printf("%s", __func__); | | 369 | debug_printf("%s", __func__); |
334 | debug_buffers(); | | 370 | debug_buffers(); |
335 | | | 371 | |
336 | if (indent_enabled == indent_on) { | | 372 | if (indent_enabled == indent_on) |
337 | if (lab.len == 0 && code.len == 0 && com.len == 0) | | 373 | output_line_indented(); |
338 | out.line_kind = lk_blank; | | 374 | else if (indent_enabled == indent_last_off_line) { |
339 | | | | |
340 | if (want_blank_line() && wrote_newlines < 2 | | | |
341 | && out.line_kind != lk_blank) | | | |
342 | write_newline(); | | | |
343 | | | | |
344 | /* This kludge aligns function definitions correctly. */ | | | |
345 | if (ps.ind_level == 0) | | | |
346 | ps.in_stmt_cont = false; | | | |
347 | | | | |
348 | if (opt.blank_line_after_decl && ps.declaration == decl_end | | | |
349 | && ps.psyms.top > 1) { | | | |
350 | ps.declaration = decl_no; | | | |
351 | ps.blank_line_after_decl = true; | | | |
352 | } | | | |
353 | | | | |
354 | if (opt.swallow_optional_blanklines | | | |
355 | && out.line_kind == lk_blank | | | |
356 | && is_blank_line_optional()) | | | |
357 | goto prepare_next_line; | | | |
358 | | | | |
359 | if (lab.len > 0) | | | |
360 | output_line_label(); | | | |
361 | if (code.len > 0) | | | |
362 | output_line_code(); | | | |
363 | if (com.len > 0) | | | |
364 | output_line_comment(); | | | |
365 | | | | |
366 | write_newline(); | | | |
367 | out.prev_line_kind = out.line_kind; | | | |
368 | } | | | |
369 | | | | |
370 | if (indent_enabled == indent_last_off_line) { | | | |
371 | indent_enabled = indent_on; | | 375 | indent_enabled = indent_on; |
372 | write_range(out.indent_off_text.s, out.indent_off_text.len); | | 376 | write_range(out.indent_off_text.s, out.indent_off_text.len); |
373 | out.indent_off_text.len = 0; | | 377 | out.indent_off_text.len = 0; |
374 | } | | 378 | } |
375 | | | 379 | |
376 | prepare_next_line: | | | |
377 | lab.len = 0; | | 380 | lab.len = 0; |
378 | code.len = 0; | | 381 | code.len = 0; |
379 | com.len = 0; | | 382 | com.len = 0; |
380 | | | 383 | |
381 | ps.line_has_decl = ps.in_decl; | | 384 | ps.line_has_decl = ps.in_decl; |
382 | ps.line_has_func_def = false; | | 385 | ps.line_has_func_def = false; |
383 | // XXX: don't reset in_stmt_cont here; see process_colon_question. | | 386 | // XXX: don't reset in_stmt_cont here; see process_colon_question. |
384 | ps.in_stmt_cont = ps.in_stmt_or_decl | | 387 | ps.in_stmt_cont = ps.in_stmt_or_decl |
385 | && !ps.in_decl && ps.init_level == 0; | | 388 | && !ps.in_decl && ps.init_level == 0; |
386 | ps.decl_indent_done = false; | | 389 | ps.decl_indent_done = false; |
387 | if (ps.extra_expr_indent == eei_last) | | 390 | if (ps.extra_expr_indent == eei_last) |
388 | ps.extra_expr_indent = eei_no; | | 391 | ps.extra_expr_indent = eei_no; |
389 | if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else | | 392 | if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else |