Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.236
      1 /*	$NetBSD: io.c,v 1.236 2024/12/12 05:51:50 rillig Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-4-Clause
      5  *
      6  * Copyright (c) 1985 Sun Microsystems, Inc.
      7  * Copyright (c) 1980, 1993
      8  *	The Regents of the University of California.  All rights reserved.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     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
     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
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __RCSID("$NetBSD: io.c,v 1.236 2024/12/12 05:51:50 rillig Exp $");
     42 
     43 #include <err.h>
     44 #include <stdio.h>
     45 
     46 #include "indent.h"
     47 
     48 struct input_state in = {
     49 	.token_end_line = 1,
     50 };
     51 
     52 struct output_state out;
     53 enum indent_enabled indent_enabled;
     54 static int out_ind;		/* width of the line that is being written */
     55 static unsigned newlines = 2;	/* the total of written and buffered newlines;
     56 				 * 0 in the middle of a line, 1 after a single
     57 				 * finished line, anything > 1 are trailing
     58 				 * blank lines */
     59 static unsigned buffered_newlines;	/* not yet written */
     60 static int paren_indent;	/* total indentation when parenthesized */
     61 
     62 
     63 static void
     64 inp_read_next_line(void)
     65 {
     66 	buf_clear(&in.line);
     67 
     68 	for (;;) {
     69 		int ch = getc(in.f);
     70 		if (ch == EOF) {
     71 			if (indent_enabled == indent_on) {
     72 				buf_add_char(&in.line, ' ');
     73 				buf_add_char(&in.line, '\n');
     74 			}
     75 			had_eof = true;
     76 			break;
     77 		}
     78 
     79 		if (ch != '\0')
     80 			buf_add_char(&in.line, (char)ch);
     81 		if (ch == '\n')
     82 			break;
     83 	}
     84 	buf_terminate(&in.line);
     85 	in.p = in.line.s;
     86 }
     87 
     88 void
     89 inp_read_line(void)
     90 {
     91 	if (indent_enabled == indent_on)
     92 		buf_clear(&out.indent_off_text);
     93 	buf_add_chars(&out.indent_off_text, in.line.s, in.line.len);
     94 	inp_read_next_line();
     95 }
     96 
     97 void
     98 inp_skip(void)
     99 {
    100 	in.p++;
    101 	if ((size_t)(in.p - in.line.s) >= in.line.len)
    102 		inp_read_line();
    103 }
    104 
    105 char
    106 inp_next(void)
    107 {
    108 	char ch = in.p[0];
    109 	inp_skip();
    110 	return ch;
    111 }
    112 
    113 
    114 static void
    115 add_buffered_newline(void)
    116 {
    117 	buffered_newlines++;
    118 	newlines++;
    119 	out_ind = 0;
    120 }
    121 
    122 static void
    123 write_buffered_newlines(void)
    124 {
    125 	for (; buffered_newlines > 0; buffered_newlines--) {
    126 		if (fputc('\n', output) == EOF)
    127 			err(1, "cannot write output");
    128 		debug_println("write_newline");
    129 	}
    130 }
    131 
    132 static void
    133 write_range(const char *s, size_t len)
    134 {
    135 	write_buffered_newlines();
    136 	if (fwrite(s, 1, len, output) != len)
    137 		err(1, "cannot write output");
    138 	debug_printf("write_range ");
    139 	debug_vis_range(s, len);
    140 	debug_println("");
    141 	for (size_t i = 0; i < len; i++)
    142 		newlines = s[i] == '\n' ? newlines + 1 : 0;
    143 	out_ind = ind_add(out_ind, s, len);
    144 }
    145 
    146 static void
    147 write_indent(int new_ind)
    148 {
    149 	write_buffered_newlines();
    150 
    151 	int ind = out_ind;
    152 
    153 	if (opt.use_tabs) {
    154 		int n = new_ind / opt.tabsize - ind / opt.tabsize;
    155 		if (n > 0) {
    156 			ind = ind - ind % opt.tabsize + n * opt.tabsize;
    157 			while (n-- > 0)
    158 				if (fputc('\t', output) == EOF)
    159 					err(1, "cannot write output");
    160 			newlines = 0;
    161 		}
    162 	}
    163 
    164 	for (; ind < new_ind; ind++) {
    165 		if (fputc(' ', output) == EOF)
    166 			err(1, "cannot write output");
    167 		newlines = 0;
    168 	}
    169 
    170 	debug_println("write_indent %d", ind);
    171 	out_ind = ind;
    172 }
    173 
    174 static bool
    175 want_blank_line(void)
    176 {
    177 	debug_println("%s: %s -> %s", __func__,
    178 	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
    179 
    180 	if (((ps.blank_line_after_decl && ps.declaration == decl_no)
    181 	    || ps.badp == badp_yes)
    182 	    && (lab.len > 0 || code.len > 0)) {
    183 		ps.blank_line_after_decl = false;
    184 		ps.badp = badp_none;
    185 		return true;
    186 	}
    187 
    188 	if (opt.blank_line_around_conditional_compilation) {
    189 		if (out.prev_line_kind != lk_pre_if
    190 		    && out.line_kind == lk_pre_if)
    191 			return true;
    192 		if (out.prev_line_kind == lk_pre_endif
    193 		    && out.line_kind != lk_pre_endif)
    194 			return true;
    195 	}
    196 	if (opt.blank_line_after_proc && out.prev_line_kind == lk_func_end
    197 	    && out.line_kind != lk_pre_endif && out.line_kind != lk_pre_other)
    198 		return true;
    199 	if (opt.blank_line_before_block_comment
    200 	    && out.line_kind == lk_block_comment)
    201 		return true;
    202 	return false;
    203 }
    204 
    205 static bool
    206 is_blank_line_optional(void)
    207 {
    208 	if (out.prev_line_kind == lk_stmt_head)
    209 		return newlines >= 1;
    210 	if (ps.psyms.len >= 3)
    211 		return newlines >= 2;
    212 	return newlines >= 3;
    213 }
    214 
    215 static int
    216 compute_case_label_indent(void)
    217 {
    218 	size_t i = ps.psyms.len - 1;
    219 	while (i > 0 && ps.psyms.sym[i] != psym_switch_expr)
    220 		i--;
    221 	float case_ind = (float)ps.psyms.ind_level[i] + opt.case_indent;
    222 	// TODO: case_ind may become negative here.
    223 	return (int)(case_ind * (float)opt.indent_size);
    224 }
    225 
    226 int
    227 compute_label_indent(void)
    228 {
    229 	if (out.line_kind == lk_case_or_default)
    230 		return compute_case_label_indent();
    231 	if (lab.s[0] == '#')
    232 		return 0;
    233 	// TODO: the indentation may become negative here.
    234 	return opt.indent_size * (ps.ind_level - 2);
    235 }
    236 
    237 static void
    238 output_line_label(void)
    239 {
    240 	write_indent(compute_label_indent());
    241 	write_range(lab.s, lab.len);
    242 }
    243 
    244 static int
    245 compute_lined_up_code_indent(int base_ind)
    246 {
    247 	int ind = paren_indent;
    248 	int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
    249 	if (overflow >= 0
    250 	    && ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
    251 		ind -= 2 + overflow;
    252 		if (ind < base_ind)
    253 			ind = base_ind;
    254 	}
    255 
    256 	if (ps.extra_expr_indent != eei_no
    257 	    && ind == base_ind + opt.indent_size)
    258 		ind += opt.continuation_indent;
    259 	return ind;
    260 }
    261 
    262 int
    263 compute_code_indent(void)
    264 {
    265 	int base_ind = ps.ind_level * opt.indent_size;
    266 
    267 	if (ps.ind_paren_level == 0) {
    268 		if (ps.line_is_stmt_cont)
    269 			return base_ind + opt.continuation_indent;
    270 		return base_ind;
    271 	}
    272 
    273 	if (opt.lineup_to_parens) {
    274 		if (opt.lineup_to_parens_always)
    275 			return paren_indent;
    276 		return compute_lined_up_code_indent(base_ind);
    277 	}
    278 
    279 	int rel_ind = opt.continuation_indent * ps.ind_paren_level;
    280 	if (ps.extra_expr_indent != eei_no && rel_ind == opt.indent_size)
    281 		rel_ind += opt.continuation_indent;
    282 	return base_ind + rel_ind;
    283 }
    284 
    285 static void
    286 output_line_code(void)
    287 {
    288 	int target_ind = compute_code_indent();
    289 	for (size_t i = 0; i < ps.paren.len; i++) {
    290 		int paren_ind = ps.paren.item[i].indent;
    291 		if (paren_ind >= 0) {
    292 			ps.paren.item[i].indent =
    293 			    -1 - (paren_ind + target_ind);
    294 			debug_println(
    295 			    "setting paren_indents[%zu] from %d to %d "
    296 			    "for column %d",
    297 			    i, paren_ind,
    298 			    ps.paren.item[i].indent, target_ind + 1);
    299 		}
    300 	}
    301 
    302 	if (lab.len > 0 && target_ind <= out_ind)
    303 		write_range(" ", 1);
    304 	write_indent(target_ind);
    305 	write_range(code.s, code.len);
    306 }
    307 
    308 static void
    309 output_comment(void)
    310 {
    311 	int target_ind = ps.comment_ind;
    312 	const char *p;
    313 
    314 	if (ps.comment_cont)
    315 		target_ind += ps.comment_shift;
    316 	ps.comment_cont = true;
    317 
    318 	/* consider the original indentation in case this is a box comment */
    319 	for (p = com.s; *p == '\t'; p++)
    320 		target_ind += opt.tabsize;
    321 
    322 	for (; target_ind < 0; p++) {
    323 		if (*p == ' ')
    324 			target_ind++;
    325 		else if (*p == '\t')
    326 			target_ind = next_tab(target_ind);
    327 		else {
    328 			target_ind = 0;
    329 			break;
    330 		}
    331 	}
    332 
    333 	if (out_ind > target_ind)
    334 		add_buffered_newline();
    335 
    336 	while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
    337 		com.len--;
    338 	buf_terminate(&com);
    339 
    340 	write_indent(target_ind);
    341 	write_range(p, com.len - (size_t)(p - com.s));
    342 }
    343 
    344 /*
    345  * Write a line of formatted source to the output file. The line consists of
    346  * the label, the code and the comment.
    347  */
    348 static void
    349 output_indented_line(void)
    350 {
    351 	if (lab.len == 0 && code.len == 0 && com.len == 0)
    352 		out.line_kind = lk_blank;
    353 
    354 	if (want_blank_line() && newlines < 2 && out.line_kind != lk_blank)
    355 		add_buffered_newline();
    356 
    357 	/* This kludge aligns function definitions correctly. */
    358 	if (ps.ind_level == 0)
    359 		ps.line_is_stmt_cont = false;
    360 
    361 	if (opt.blank_line_after_decl && ps.declaration == decl_end
    362 	    && ps.psyms.len > 2) {
    363 		ps.declaration = decl_no;
    364 		ps.blank_line_after_decl = true;
    365 	}
    366 
    367 	if (opt.swallow_optional_blank_lines
    368 	    && out.line_kind == lk_blank
    369 	    && is_blank_line_optional())
    370 		return;
    371 
    372 	if (lab.len > 0)
    373 		output_line_label();
    374 	if (code.len > 0)
    375 		output_line_code();
    376 	if (com.len > 0)
    377 		output_comment();
    378 	add_buffered_newline();
    379 	if (out.line_kind != lk_blank)
    380 		write_buffered_newlines();
    381 
    382 	out.prev_line_kind = out.line_kind;
    383 }
    384 
    385 static bool
    386 is_stmt_cont(void)
    387 {
    388 	if (ps.psyms.len >= 2
    389 	    && ps.psyms.sym[ps.psyms.len - 2] == psym_lbrace_enum
    390 	    && ps.prev_lsym == lsym_comma
    391 	    && ps.paren.len == 0)
    392 		return false;
    393 	return ps.in_stmt_or_decl
    394 	    && (!ps.in_decl || ps.in_init)
    395 	    && ps.init_level == 0;
    396 }
    397 
    398 static void
    399 prepare_next_line(void)
    400 {
    401 	ps.line_has_decl = ps.in_decl;
    402 	ps.line_has_func_def = false;
    403 	ps.line_is_stmt_cont = is_stmt_cont();
    404 	ps.decl_indent_done = false;
    405 	if (ps.extra_expr_indent == eei_last)
    406 		ps.extra_expr_indent = eei_no;
    407 	if (!(ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt_else
    408 		&& ps.paren.len > 0))
    409 		ps.ind_level = ps.ind_level_follow;
    410 	ps.ind_paren_level = (int)ps.paren.len;
    411 	ps.want_blank = false;
    412 	if ((ps.badp == badp_seen_lbrace || ps.badp == badp_seen_decl)
    413 	    && !ps.in_decl)
    414 		ps.badp = badp_yes;
    415 
    416 	if (ps.paren.len > 0) {
    417 		/* TODO: explain what negative indentation means */
    418 		paren_indent = -1 - ps.paren.item[ps.paren.len - 1].indent;
    419 		debug_println("paren_indent is now %d", paren_indent);
    420 	}
    421 
    422 	out.line_kind = lk_other;
    423 }
    424 
    425 void
    426 output_line(void)
    427 {
    428 	debug_blank_line();
    429 	debug_printf("%s", __func__);
    430 	debug_buffers();
    431 
    432 	if (indent_enabled == indent_on)
    433 		output_indented_line();
    434 	else if (indent_enabled == indent_last_off_line) {
    435 		indent_enabled = indent_on;
    436 		write_range(out.indent_off_text.s, out.indent_off_text.len);
    437 		buf_clear(&out.indent_off_text);
    438 	}
    439 
    440 	buf_clear(&lab);
    441 	buf_clear(&code);
    442 	buf_clear(&com);
    443 
    444 	prepare_next_line();
    445 }
    446 
    447 void
    448 finish_output(void)
    449 {
    450 	output_line();
    451 	if (indent_enabled != indent_on) {
    452 		indent_enabled = indent_last_off_line;
    453 		output_line();
    454 	}
    455 	if (fflush(output) != 0)
    456 		err(1, "output file");
    457 }
    458