Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.200
      1 /*	$NetBSD: io.c,v 1.200 2023/06/06 05:11:11 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.200 2023/06/06 05:11:11 rillig Exp $");
     42 
     43 #include <stdio.h>
     44 
     45 #include "indent.h"
     46 
     47 struct buffer inp;
     48 const char *inp_p;
     49 
     50 struct output_state out;
     51 static int out_ind;		/* width of the line that is being written */
     52 static unsigned wrote_newlines = 2;	/* 0 in the middle of a line, 1 after a
     53 					 * single '\n', > 1 means there were (n
     54 					 * - 1) blank lines above */
     55 static int paren_indent;
     56 
     57 
     58 static void
     59 inp_read_next_line(FILE *f)
     60 {
     61 	inp.len = 0;
     62 
     63 	for (;;) {
     64 		int ch = getc(f);
     65 		if (ch == EOF) {
     66 			if (indent_enabled == indent_on) {
     67 				buf_add_char(&inp, ' ');
     68 				buf_add_char(&inp, '\n');
     69 			}
     70 			had_eof = true;
     71 			break;
     72 		}
     73 
     74 		if (ch != '\0')
     75 			buf_add_char(&inp, (char)ch);
     76 		if (ch == '\n')
     77 			break;
     78 	}
     79 	inp_p = inp.s;
     80 }
     81 
     82 void
     83 inp_read_line(void)
     84 {
     85 	if (indent_enabled == indent_on)
     86 		out.indent_off_text.len = 0;
     87 	buf_add_chars(&out.indent_off_text, inp.s, inp.len);
     88 	inp_read_next_line(input);
     89 }
     90 
     91 void
     92 inp_skip(void)
     93 {
     94 	inp_p++;
     95 	if ((size_t)(inp_p - inp.s) >= inp.len)
     96 		inp_read_line();
     97 }
     98 
     99 char
    100 inp_next(void)
    101 {
    102 	char ch = inp_p[0];
    103 	inp_skip();
    104 	return ch;
    105 }
    106 
    107 
    108 static void
    109 output_newline(void)
    110 {
    111 	fputc('\n', output);
    112 	debug_println("output_newline");
    113 	wrote_newlines++;
    114 	out_ind = 0;
    115 }
    116 
    117 static void
    118 output_range(const char *s, size_t len)
    119 {
    120 	fwrite(s, 1, len, output);
    121 	debug_vis_range("output_range \"", s, len, "\"\n");
    122 	for (size_t i = 0; i < len; i++)
    123 		wrote_newlines = s[i] == '\n' ? wrote_newlines + 1 : 0;
    124 	out_ind = ind_add(out_ind, s, len);
    125 }
    126 
    127 static void
    128 output_indent(int new_ind)
    129 {
    130 	int ind = out_ind;
    131 
    132 	if (opt.use_tabs) {
    133 		int tabsize = opt.tabsize;
    134 		int n = new_ind / tabsize - ind / tabsize;
    135 		if (n > 0)
    136 			ind -= ind % tabsize;
    137 		for (int i = 0; i < n; i++) {
    138 			fputc('\t', output);
    139 			ind += tabsize;
    140 			wrote_newlines = 0;
    141 		}
    142 	}
    143 
    144 	for (; ind < new_ind; ind++) {
    145 		fputc(' ', output);
    146 		wrote_newlines = 0;
    147 	}
    148 
    149 	debug_println("output_indent %d", ind);
    150 	out_ind = ind;
    151 }
    152 
    153 static bool
    154 want_blank_line(void)
    155 {
    156 	debug_println("%s: %s -> %s", __func__,
    157 	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
    158 
    159 	if (ps.blank_line_after_decl && ps.declaration == decl_no) {
    160 		ps.blank_line_after_decl = false;
    161 		return true;
    162 	}
    163 	if (opt.blanklines_around_conditional_compilation) {
    164 		if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
    165 			return true;
    166 		if (out.prev_line_kind == lk_endif
    167 		    && out.line_kind != lk_endif)
    168 			return true;
    169 	}
    170 	if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end
    171 	    && out.line_kind != lk_endif)
    172 		return true;
    173 	if (opt.blanklines_before_block_comments
    174 	    && out.line_kind == lk_block_comment)
    175 		return true;
    176 	return false;
    177 }
    178 
    179 static bool
    180 is_blank_line_optional(void)
    181 {
    182 	if (out.prev_line_kind == lk_stmt_head)
    183 		return wrote_newlines >= 1;
    184 	if (ps.tos >= 2)
    185 		return wrote_newlines >= 2;
    186 	return wrote_newlines >= 3;
    187 }
    188 
    189 static int
    190 compute_case_label_indent(void)
    191 {
    192 	int i = ps.tos;
    193 	while (i > 0 && ps.s_sym[i] != psym_switch_expr)
    194 		i--;
    195 	float case_ind = (float)ps.s_ind_level[i] + opt.case_indent;
    196 	return (int)(case_ind * (float)opt.indent_size);
    197 }
    198 
    199 int
    200 compute_label_indent(void)
    201 {
    202 	if (out.line_kind == lk_case_or_default)
    203 		return compute_case_label_indent();
    204 	if (lab.s[0] == '#')
    205 		return 0;
    206 	return opt.indent_size * (ps.ind_level - 2);
    207 }
    208 
    209 static void
    210 output_line_label(void)
    211 {
    212 	output_indent(compute_label_indent());
    213 	output_range(lab.s, lab.len);
    214 }
    215 
    216 static int
    217 compute_code_indent_lineup(int base_ind)
    218 {
    219 	int ind = paren_indent;
    220 	int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
    221 	if (overflow < 0)
    222 		return ind;
    223 
    224 	if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
    225 		ind -= overflow + 2;
    226 		if (ind > base_ind)
    227 			return ind;
    228 		return base_ind;
    229 	}
    230 
    231 	return ind;
    232 }
    233 
    234 int
    235 compute_code_indent(void)
    236 {
    237 	int base_ind = ps.ind_level * opt.indent_size;
    238 
    239 	if (ps.line_start_nparen == 0) {
    240 		if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum)
    241 			return base_ind;
    242 		if (ps.in_stmt_cont)
    243 			return base_ind + opt.continuation_indent;
    244 		return base_ind;
    245 	}
    246 
    247 	if (opt.lineup_to_parens) {
    248 		if (opt.lineup_to_parens_always)
    249 			return paren_indent;
    250 		return compute_code_indent_lineup(base_ind);
    251 	}
    252 
    253 	if (ps.extra_expr_indent != eei_no)
    254 		return base_ind + 2 * opt.continuation_indent;
    255 
    256 	if (2 * opt.continuation_indent == opt.indent_size)
    257 		return base_ind + opt.continuation_indent;
    258 	else
    259 		return base_ind +
    260 		    opt.continuation_indent * ps.line_start_nparen;
    261 }
    262 
    263 static void
    264 output_line_code(void)
    265 {
    266 	int target_ind = compute_code_indent();
    267 	for (int i = 0; i < ps.nparen; i++) {
    268 		int paren_ind = ps.paren[i].indent;
    269 		if (paren_ind >= 0) {
    270 			ps.paren[i].indent = -1 - (paren_ind + target_ind);
    271 			debug_println(
    272 			    "setting paren_indents[%d] from %d to %d "
    273 			    "for column %d",
    274 			    i, paren_ind, ps.paren[i].indent, target_ind + 1);
    275 		}
    276 	}
    277 
    278 	if (lab.len > 0 && target_ind <= out_ind)
    279 		output_range(" ", 1);
    280 	output_indent(target_ind);
    281 	output_range(code.s, code.len);
    282 }
    283 
    284 static void
    285 output_line_comment(void)
    286 {
    287 	int target_ind = ps.com_ind + ps.comment_delta;
    288 	const char *p;
    289 
    290 	/* consider original indentation in case this is a box comment */
    291 	for (p = com.s; *p == '\t'; p++)
    292 		target_ind += opt.tabsize;
    293 
    294 	for (; target_ind < 0; p++) {
    295 		if (*p == ' ')
    296 			target_ind++;
    297 		else if (*p == '\t')
    298 			target_ind = next_tab(target_ind);
    299 		else {
    300 			target_ind = 0;
    301 			break;
    302 		}
    303 	}
    304 
    305 	if (out_ind > target_ind)
    306 		output_newline();
    307 
    308 	while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
    309 		com.len--;
    310 
    311 	output_indent(target_ind);
    312 	output_range(p, com.len - (size_t)(p - com.s));
    313 
    314 	ps.comment_delta = ps.n_comment_delta;
    315 }
    316 
    317 /*
    318  * Write a line of formatted source to the output file. The line consists of
    319  * the label, the code and the comment.
    320  */
    321 void
    322 output_line(void)
    323 {
    324 	debug_blank_line();
    325 	debug_printf("%s", __func__);
    326 	debug_buffers();
    327 
    328 	ps.is_function_definition = false;
    329 
    330 	if (indent_enabled == indent_on) {
    331 		if (lab.len == 0 && code.len == 0 && com.len == 0)
    332 			out.line_kind = lk_blank;
    333 
    334 		if (want_blank_line() && wrote_newlines < 2
    335 		    && out.line_kind != lk_blank)
    336 			output_newline();
    337 
    338 		/* This kludge aligns function definitions correctly. */
    339 		if (ps.ind_level == 0)
    340 			ps.in_stmt_cont = false;
    341 
    342 		if (opt.blank_line_after_decl && ps.declaration == decl_end
    343 		    && ps.tos > 1) {
    344 			ps.declaration = decl_no;
    345 			ps.blank_line_after_decl = true;
    346 		}
    347 
    348 		if (opt.swallow_optional_blanklines
    349 		    && out.line_kind == lk_blank
    350 		    && is_blank_line_optional())
    351 			goto dont_write_line;
    352 
    353 		if (lab.len > 0)
    354 			output_line_label();
    355 		if (code.len > 0)
    356 			output_line_code();
    357 		if (com.len > 0)
    358 			output_line_comment();
    359 
    360 		output_newline();
    361 		out.prev_line_kind = out.line_kind;
    362 	}
    363 
    364 	if (indent_enabled == indent_last_off_line) {
    365 		indent_enabled = indent_on;
    366 		output_range(out.indent_off_text.s, out.indent_off_text.len);
    367 		out.indent_off_text.len = 0;
    368 	}
    369 
    370 dont_write_line:
    371 	ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
    372 	ps.in_stmt_cont = ps.in_stmt_or_decl
    373 	    && !ps.in_decl && ps.block_init_level == 0;
    374 	ps.decl_indent_done = false;
    375 	if (ps.extra_expr_indent == eei_last)
    376 		ps.extra_expr_indent = eei_no;
    377 
    378 	lab.len = 0;
    379 	code.len = 0;
    380 	com.len = 0;
    381 
    382 	ps.ind_level = ps.ind_level_follow;
    383 	ps.line_start_nparen = ps.nparen;
    384 
    385 	if (ps.nparen > 0) {
    386 		/* TODO: explain what negative indentation means */
    387 		paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
    388 		debug_println("paren_indent is now %d", paren_indent);
    389 	}
    390 
    391 	ps.want_blank = false;
    392 	out.line_kind = lk_other;
    393 }
    394