Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.201
      1 /*	$NetBSD: io.c,v 1.201 2023/06/06 05:27:56 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.201 2023/06/06 05:27:56 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 n = new_ind / opt.tabsize - ind / opt.tabsize;
    134 		if (n > 0) {
    135 			ind = ind - ind % opt.tabsize + n * opt.tabsize;
    136 			while (n-- > 0)
    137 				fputc('\t', output);
    138 			wrote_newlines = 0;
    139 		}
    140 	}
    141 
    142 	for (; ind < new_ind; ind++) {
    143 		fputc(' ', output);
    144 		wrote_newlines = 0;
    145 	}
    146 
    147 	debug_println("output_indent %d", ind);
    148 	out_ind = ind;
    149 }
    150 
    151 static bool
    152 want_blank_line(void)
    153 {
    154 	debug_println("%s: %s -> %s", __func__,
    155 	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
    156 
    157 	if (ps.blank_line_after_decl && ps.declaration == decl_no) {
    158 		ps.blank_line_after_decl = false;
    159 		return true;
    160 	}
    161 	if (opt.blanklines_around_conditional_compilation) {
    162 		if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
    163 			return true;
    164 		if (out.prev_line_kind == lk_endif
    165 		    && out.line_kind != lk_endif)
    166 			return true;
    167 	}
    168 	if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end
    169 	    && out.line_kind != lk_endif)
    170 		return true;
    171 	if (opt.blanklines_before_block_comments
    172 	    && out.line_kind == lk_block_comment)
    173 		return true;
    174 	return false;
    175 }
    176 
    177 static bool
    178 is_blank_line_optional(void)
    179 {
    180 	if (out.prev_line_kind == lk_stmt_head)
    181 		return wrote_newlines >= 1;
    182 	if (ps.tos >= 2)
    183 		return wrote_newlines >= 2;
    184 	return wrote_newlines >= 3;
    185 }
    186 
    187 static int
    188 compute_case_label_indent(void)
    189 {
    190 	int i = ps.tos;
    191 	while (i > 0 && ps.s_sym[i] != psym_switch_expr)
    192 		i--;
    193 	float case_ind = (float)ps.s_ind_level[i] + opt.case_indent;
    194 	return (int)(case_ind * (float)opt.indent_size);
    195 }
    196 
    197 int
    198 compute_label_indent(void)
    199 {
    200 	if (out.line_kind == lk_case_or_default)
    201 		return compute_case_label_indent();
    202 	if (lab.s[0] == '#')
    203 		return 0;
    204 	return opt.indent_size * (ps.ind_level - 2);
    205 }
    206 
    207 static void
    208 output_line_label(void)
    209 {
    210 	output_indent(compute_label_indent());
    211 	output_range(lab.s, lab.len);
    212 }
    213 
    214 static int
    215 compute_code_indent_lineup(int base_ind)
    216 {
    217 	int ind = paren_indent;
    218 	int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
    219 	if (overflow < 0)
    220 		return ind;
    221 
    222 	if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
    223 		ind -= overflow + 2;
    224 		if (ind > base_ind)
    225 			return ind;
    226 		return base_ind;
    227 	}
    228 
    229 	return ind;
    230 }
    231 
    232 int
    233 compute_code_indent(void)
    234 {
    235 	int base_ind = ps.ind_level * opt.indent_size;
    236 
    237 	if (ps.line_start_nparen == 0) {
    238 		if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum)
    239 			return base_ind;
    240 		if (ps.in_stmt_cont)
    241 			return base_ind + opt.continuation_indent;
    242 		return base_ind;
    243 	}
    244 
    245 	if (opt.lineup_to_parens) {
    246 		if (opt.lineup_to_parens_always)
    247 			return paren_indent;
    248 		return compute_code_indent_lineup(base_ind);
    249 	}
    250 
    251 	if (ps.extra_expr_indent != eei_no)
    252 		return base_ind + 2 * opt.continuation_indent;
    253 
    254 	if (2 * opt.continuation_indent == opt.indent_size)
    255 		return base_ind + opt.continuation_indent;
    256 	else
    257 		return base_ind +
    258 		    opt.continuation_indent * ps.line_start_nparen;
    259 }
    260 
    261 static void
    262 output_line_code(void)
    263 {
    264 	int target_ind = compute_code_indent();
    265 	for (int i = 0; i < ps.nparen; i++) {
    266 		int paren_ind = ps.paren[i].indent;
    267 		if (paren_ind >= 0) {
    268 			ps.paren[i].indent = -1 - (paren_ind + target_ind);
    269 			debug_println(
    270 			    "setting paren_indents[%d] from %d to %d "
    271 			    "for column %d",
    272 			    i, paren_ind, ps.paren[i].indent, target_ind + 1);
    273 		}
    274 	}
    275 
    276 	if (lab.len > 0 && target_ind <= out_ind)
    277 		output_range(" ", 1);
    278 	output_indent(target_ind);
    279 	output_range(code.s, code.len);
    280 }
    281 
    282 static void
    283 output_line_comment(void)
    284 {
    285 	int target_ind = ps.com_ind + ps.comment_delta;
    286 	const char *p;
    287 
    288 	/* consider original indentation in case this is a box comment */
    289 	for (p = com.s; *p == '\t'; p++)
    290 		target_ind += opt.tabsize;
    291 
    292 	for (; target_ind < 0; p++) {
    293 		if (*p == ' ')
    294 			target_ind++;
    295 		else if (*p == '\t')
    296 			target_ind = next_tab(target_ind);
    297 		else {
    298 			target_ind = 0;
    299 			break;
    300 		}
    301 	}
    302 
    303 	if (out_ind > target_ind)
    304 		output_newline();
    305 
    306 	while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
    307 		com.len--;
    308 
    309 	output_indent(target_ind);
    310 	output_range(p, com.len - (size_t)(p - com.s));
    311 
    312 	ps.comment_delta = ps.n_comment_delta;
    313 }
    314 
    315 /*
    316  * Write a line of formatted source to the output file. The line consists of
    317  * the label, the code and the comment.
    318  */
    319 void
    320 output_line(void)
    321 {
    322 	debug_blank_line();
    323 	debug_printf("%s", __func__);
    324 	debug_buffers();
    325 
    326 	ps.is_function_definition = false;
    327 
    328 	if (indent_enabled == indent_on) {
    329 		if (lab.len == 0 && code.len == 0 && com.len == 0)
    330 			out.line_kind = lk_blank;
    331 
    332 		if (want_blank_line() && wrote_newlines < 2
    333 		    && out.line_kind != lk_blank)
    334 			output_newline();
    335 
    336 		/* This kludge aligns function definitions correctly. */
    337 		if (ps.ind_level == 0)
    338 			ps.in_stmt_cont = false;
    339 
    340 		if (opt.blank_line_after_decl && ps.declaration == decl_end
    341 		    && ps.tos > 1) {
    342 			ps.declaration = decl_no;
    343 			ps.blank_line_after_decl = true;
    344 		}
    345 
    346 		if (opt.swallow_optional_blanklines
    347 		    && out.line_kind == lk_blank
    348 		    && is_blank_line_optional())
    349 			goto dont_write_line;
    350 
    351 		if (lab.len > 0)
    352 			output_line_label();
    353 		if (code.len > 0)
    354 			output_line_code();
    355 		if (com.len > 0)
    356 			output_line_comment();
    357 
    358 		output_newline();
    359 		out.prev_line_kind = out.line_kind;
    360 	}
    361 
    362 	if (indent_enabled == indent_last_off_line) {
    363 		indent_enabled = indent_on;
    364 		output_range(out.indent_off_text.s, out.indent_off_text.len);
    365 		out.indent_off_text.len = 0;
    366 	}
    367 
    368 dont_write_line:
    369 	ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
    370 	ps.in_stmt_cont = ps.in_stmt_or_decl
    371 	    && !ps.in_decl && ps.block_init_level == 0;
    372 	ps.decl_indent_done = false;
    373 	if (ps.extra_expr_indent == eei_last)
    374 		ps.extra_expr_indent = eei_no;
    375 
    376 	lab.len = 0;
    377 	code.len = 0;
    378 	com.len = 0;
    379 
    380 	ps.ind_level = ps.ind_level_follow;
    381 	ps.line_start_nparen = ps.nparen;
    382 
    383 	if (ps.nparen > 0) {
    384 		/* TODO: explain what negative indentation means */
    385 		paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
    386 		debug_println("paren_indent is now %d", paren_indent);
    387 	}
    388 
    389 	ps.want_blank = false;
    390 	out.line_kind = lk_other;
    391 }
    392