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