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