Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.191
      1  1.191  rillig /*	$NetBSD: io.c,v 1.191 2023/06/04 17:54:11 rillig Exp $	*/
      2    1.3     tls 
      3   1.19   kamil /*-
      4   1.19   kamil  * SPDX-License-Identifier: BSD-4-Clause
      5   1.19   kamil  *
      6   1.19   kamil  * Copyright (c) 1985 Sun Microsystems, Inc.
      7    1.4     mrg  * Copyright (c) 1980, 1993
      8    1.4     mrg  *	The Regents of the University of California.  All rights reserved.
      9    1.1     cgd  * All rights reserved.
     10    1.1     cgd  *
     11    1.1     cgd  * Redistribution and use in source and binary forms, with or without
     12    1.1     cgd  * modification, are permitted provided that the following conditions
     13    1.1     cgd  * are met:
     14    1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     15    1.1     cgd  *    notice, this list of conditions and the following disclaimer.
     16    1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17    1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     18    1.1     cgd  *    documentation and/or other materials provided with the distribution.
     19    1.1     cgd  * 3. All advertising materials mentioning features or use of this software
     20    1.1     cgd  *    must display the following acknowledgement:
     21    1.1     cgd  *	This product includes software developed by the University of
     22    1.1     cgd  *	California, Berkeley and its contributors.
     23    1.1     cgd  * 4. Neither the name of the University nor the names of its contributors
     24    1.1     cgd  *    may be used to endorse or promote products derived from this software
     25    1.1     cgd  *    without specific prior written permission.
     26    1.1     cgd  *
     27    1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28    1.1     cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29    1.1     cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30    1.1     cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31    1.1     cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32    1.1     cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33    1.1     cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34    1.1     cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35    1.1     cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36    1.1     cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37    1.1     cgd  * SUCH DAMAGE.
     38    1.1     cgd  */
     39    1.1     cgd 
     40    1.5   lukem #include <sys/cdefs.h>
     41  1.191  rillig __RCSID("$NetBSD: io.c,v 1.191 2023/06/04 17:54:11 rillig Exp $");
     42    1.1     cgd 
     43    1.1     cgd #include <stdio.h>
     44   1.22  rillig 
     45   1.19   kamil #include "indent.h"
     46    1.1     cgd 
     47  1.175  rillig struct buffer inp;
     48  1.183  rillig struct output_state out;
     49  1.183  rillig static unsigned wrote_newlines = 2;	/* 0 in the middle of a line, 1 after a
     50  1.183  rillig 					 * single '\n', > 1 means there were (n
     51  1.183  rillig 					 * - 1) blank lines above */
     52   1.67  rillig static int paren_indent;
     53    1.1     cgd 
     54  1.118  rillig 
     55  1.118  rillig void
     56  1.118  rillig inp_skip(void)
     57  1.118  rillig {
     58  1.177  rillig 	inp.st++;
     59  1.177  rillig 	if ((size_t)(inp.st - inp.mem) >= inp.len)
     60  1.177  rillig 		inp_read_line();
     61  1.118  rillig }
     62  1.118  rillig 
     63  1.118  rillig char
     64  1.118  rillig inp_next(void)
     65  1.118  rillig {
     66  1.177  rillig 	char ch = inp.st[0];
     67  1.177  rillig 	inp_skip();
     68  1.177  rillig 	return ch;
     69  1.118  rillig }
     70  1.118  rillig 
     71  1.138  rillig static void
     72  1.137  rillig inp_read_next_line(FILE *f)
     73  1.137  rillig {
     74  1.177  rillig 	inp.st = inp.mem;
     75  1.177  rillig 	inp.len = 0;
     76  1.137  rillig 
     77  1.177  rillig 	for (;;) {
     78  1.177  rillig 		int ch = getc(f);
     79  1.177  rillig 		if (ch == EOF) {
     80  1.177  rillig 			if (indent_enabled == indent_on) {
     81  1.177  rillig 				buf_add_char(&inp, ' ');
     82  1.177  rillig 				buf_add_char(&inp, '\n');
     83  1.177  rillig 			}
     84  1.177  rillig 			had_eof = true;
     85  1.177  rillig 			break;
     86  1.177  rillig 		}
     87  1.177  rillig 
     88  1.177  rillig 		if (ch != '\0')
     89  1.177  rillig 			buf_add_char(&inp, (char)ch);
     90  1.177  rillig 		if (ch == '\n')
     91  1.177  rillig 			break;
     92  1.177  rillig 	}
     93  1.137  rillig }
     94  1.137  rillig 
     95   1.28  rillig static void
     96  1.179  rillig output_newline(void)
     97   1.28  rillig {
     98  1.179  rillig 	fputc('\n', output);
     99  1.179  rillig 	debug_println("output_newline");
    100  1.179  rillig 	wrote_newlines++;
    101   1.28  rillig }
    102   1.28  rillig 
    103   1.28  rillig static void
    104  1.166  rillig output_range(const char *s, size_t len)
    105   1.28  rillig {
    106  1.177  rillig 	fwrite(s, 1, len, output);
    107  1.177  rillig 	debug_vis_range("output_range \"", s, len, "\"\n");
    108  1.179  rillig 	for (size_t i = 0; i < len; i++)
    109  1.179  rillig 		wrote_newlines = s[i] == '\n' ? wrote_newlines + 1 : 0;
    110   1.28  rillig }
    111   1.28  rillig 
    112   1.34  rillig static int
    113   1.34  rillig output_indent(int old_ind, int new_ind)
    114   1.34  rillig {
    115  1.177  rillig 	int ind = old_ind;
    116   1.34  rillig 
    117  1.177  rillig 	if (opt.use_tabs) {
    118  1.177  rillig 		int tabsize = opt.tabsize;
    119  1.177  rillig 		int n = new_ind / tabsize - ind / tabsize;
    120  1.177  rillig 		if (n > 0)
    121  1.177  rillig 			ind -= ind % tabsize;
    122  1.177  rillig 		for (int i = 0; i < n; i++) {
    123  1.177  rillig 			fputc('\t', output);
    124  1.177  rillig 			ind += tabsize;
    125  1.179  rillig 			wrote_newlines = 0;
    126  1.177  rillig 		}
    127   1.34  rillig 	}
    128   1.34  rillig 
    129  1.179  rillig 	for (; ind < new_ind; ind++) {
    130  1.177  rillig 		fputc(' ', output);
    131  1.179  rillig 		wrote_newlines = 0;
    132  1.179  rillig 	}
    133   1.34  rillig 
    134  1.177  rillig 	debug_println("output_indent %d", ind);
    135  1.177  rillig 	return ind;
    136   1.34  rillig }
    137   1.34  rillig 
    138  1.180  rillig static bool
    139  1.180  rillig want_blank_line(void)
    140  1.179  rillig {
    141  1.183  rillig 	debug_println("%s: %s -> %s", __func__,
    142  1.183  rillig 	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
    143  1.183  rillig 
    144  1.179  rillig 	if (ps.blank_line_after_decl && ps.declaration == decl_no) {
    145  1.179  rillig 		ps.blank_line_after_decl = false;
    146  1.180  rillig 		return true;
    147  1.179  rillig 	}
    148  1.179  rillig 	if (opt.blanklines_around_conditional_compilation) {
    149  1.183  rillig 		if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
    150  1.180  rillig 			return true;
    151  1.183  rillig 		if (out.prev_line_kind == lk_endif
    152  1.183  rillig 		    && out.line_kind != lk_endif)
    153  1.180  rillig 			return true;
    154  1.179  rillig 	}
    155  1.184  rillig 	if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end
    156  1.184  rillig 	    && out.line_kind != lk_endif)
    157  1.181  rillig 		return true;
    158  1.183  rillig 	if (opt.blanklines_before_block_comments
    159  1.183  rillig 	    && out.line_kind == lk_block_comment)
    160  1.182  rillig 		return true;
    161  1.180  rillig 	return false;
    162  1.179  rillig }
    163  1.179  rillig 
    164  1.185  rillig static bool
    165  1.185  rillig is_blank_line_optional(void)
    166  1.185  rillig {
    167  1.185  rillig 	if (out.prev_line_kind == lk_stmt_head)
    168  1.185  rillig 		return wrote_newlines >= 1;
    169  1.185  rillig 	if (ps.tos >= 2)
    170  1.185  rillig 		return wrote_newlines >= 2;
    171  1.185  rillig 	return wrote_newlines >= 3;
    172  1.185  rillig }
    173  1.185  rillig 
    174   1.86  rillig static int
    175  1.141  rillig output_line_label(void)
    176   1.86  rillig {
    177   1.86  rillig 
    178  1.177  rillig 	while (lab.len > 0 && ch_isblank(lab.mem[lab.len - 1]))
    179  1.177  rillig 		lab.len--;
    180   1.86  rillig 
    181  1.188  rillig 	int ind = output_indent(0, compute_label_indent());
    182  1.177  rillig 	output_range(lab.st, lab.len);
    183  1.188  rillig 	return ind_add(ind, lab.st, lab.len);
    184   1.86  rillig }
    185   1.86  rillig 
    186   1.86  rillig static int
    187  1.141  rillig output_line_code(int ind)
    188   1.86  rillig {
    189   1.86  rillig 
    190  1.177  rillig 	int target_ind = compute_code_indent();
    191  1.177  rillig 	for (int i = 0; i < ps.nparen; i++) {
    192  1.177  rillig 		int paren_ind = ps.paren[i].indent;
    193  1.177  rillig 		if (paren_ind >= 0) {
    194  1.177  rillig 			ps.paren[i].indent = -1 - (paren_ind + target_ind);
    195  1.177  rillig 			debug_println(
    196  1.178  rillig 			    "setting paren_indents[%d] from %d to %d "
    197  1.178  rillig 			    "for column %d",
    198  1.177  rillig 			    i, paren_ind, ps.paren[i].indent, target_ind + 1);
    199  1.177  rillig 		}
    200  1.177  rillig 	}
    201  1.177  rillig 
    202  1.177  rillig 	ind = output_indent(ind, target_ind);
    203  1.177  rillig 	output_range(code.st, code.len);
    204  1.177  rillig 	return ind_add(ind, code.st, code.len);
    205   1.86  rillig }
    206   1.86  rillig 
    207   1.86  rillig static void
    208  1.141  rillig output_line_comment(int ind)
    209   1.86  rillig {
    210  1.177  rillig 	int target_ind = ps.com_ind;
    211  1.177  rillig 	const char *p = com.st;
    212  1.177  rillig 
    213  1.177  rillig 	target_ind += ps.comment_delta;
    214   1.86  rillig 
    215  1.177  rillig 	/* consider original indentation in case this is a box comment */
    216  1.177  rillig 	for (; *p == '\t'; p++)
    217  1.177  rillig 		target_ind += opt.tabsize;
    218  1.177  rillig 
    219  1.177  rillig 	for (; target_ind < 0; p++) {
    220  1.177  rillig 		if (*p == ' ')
    221  1.177  rillig 			target_ind++;
    222  1.177  rillig 		else if (*p == '\t')
    223  1.177  rillig 			target_ind = next_tab(target_ind);
    224  1.177  rillig 		else {
    225  1.177  rillig 			target_ind = 0;
    226  1.177  rillig 			break;
    227  1.177  rillig 		}
    228  1.177  rillig 	}
    229   1.86  rillig 
    230  1.177  rillig 	/* if comment can't fit on this line, put it on the next line */
    231  1.177  rillig 	if (ind > target_ind) {
    232  1.179  rillig 		output_newline();
    233  1.177  rillig 		ind = 0;
    234  1.177  rillig 	}
    235   1.86  rillig 
    236  1.177  rillig 	while (com.mem + com.len > p && ch_isspace(com.mem[com.len - 1]))
    237  1.177  rillig 		com.len--;
    238   1.86  rillig 
    239  1.177  rillig 	(void)output_indent(ind, target_ind);
    240  1.177  rillig 	output_range(p, com.len - (size_t)(p - com.mem));
    241   1.86  rillig 
    242  1.177  rillig 	ps.comment_delta = ps.n_comment_delta;
    243   1.86  rillig }
    244   1.86  rillig 
    245   1.26  rillig /*
    246  1.105  rillig  * Write a line of formatted source to the output file. The line consists of
    247  1.105  rillig  * the label, the code and the comment.
    248  1.156  rillig  *
    249  1.156  rillig  * Comments are written directly, bypassing this function.
    250   1.26  rillig  */
    251  1.174  rillig void
    252  1.174  rillig output_line(void)
    253   1.26  rillig {
    254  1.186  rillig 	debug_blank_line();
    255  1.177  rillig 	debug_printf("%s", __func__);
    256  1.177  rillig 	debug_buffers();
    257  1.177  rillig 
    258  1.177  rillig 	ps.is_function_definition = false;
    259  1.177  rillig 
    260  1.180  rillig 	if (indent_enabled == indent_on) {
    261  1.185  rillig 		if (lab.len == 0 && code.len == 0 && com.len == 0)
    262  1.185  rillig 			out.line_kind = lk_blank;
    263  1.185  rillig 
    264  1.180  rillig 		if (want_blank_line() && wrote_newlines < 2
    265  1.185  rillig 		    && out.line_kind != lk_blank)
    266  1.180  rillig 			output_newline();
    267  1.177  rillig 
    268  1.177  rillig 		if (ps.ind_level == 0)
    269  1.177  rillig 			ps.in_stmt_cont = false;	/* this is a class A
    270  1.177  rillig 							 * kludge */
    271  1.177  rillig 
    272  1.177  rillig 		if (opt.blank_line_after_decl && ps.declaration == decl_end
    273  1.177  rillig 		    && ps.tos > 1) {
    274  1.177  rillig 			ps.declaration = decl_no;
    275  1.177  rillig 			ps.blank_line_after_decl = true;
    276  1.177  rillig 		}
    277  1.177  rillig 
    278  1.185  rillig 		if (opt.swallow_optional_blanklines
    279  1.185  rillig 		    && out.line_kind == lk_blank
    280  1.185  rillig 		    && is_blank_line_optional())
    281  1.185  rillig 			goto dont_write_line;
    282  1.185  rillig 
    283  1.177  rillig 		int ind = 0;
    284  1.177  rillig 		if (lab.len > 0)
    285  1.177  rillig 			ind = output_line_label();
    286  1.177  rillig 		if (code.len > 0)
    287  1.177  rillig 			ind = output_line_code(ind);
    288  1.177  rillig 		if (com.len > 0)
    289  1.177  rillig 			output_line_comment(ind);
    290  1.177  rillig 
    291  1.179  rillig 		output_newline();
    292  1.185  rillig 		out.prev_line_kind = out.line_kind;
    293  1.177  rillig 	}
    294  1.177  rillig 
    295  1.177  rillig 	if (indent_enabled == indent_last_off_line) {
    296  1.177  rillig 		indent_enabled = indent_on;
    297  1.183  rillig 		output_range(out.indent_off_text.st, out.indent_off_text.len);
    298  1.183  rillig 		out.indent_off_text.len = 0;
    299  1.177  rillig 	}
    300  1.173  rillig 
    301  1.185  rillig dont_write_line:
    302  1.177  rillig 	ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
    303  1.189  rillig 	ps.in_stmt_cont = ps.in_stmt_or_decl
    304  1.190  rillig 	    && !ps.in_decl && ps.block_init_level == 0;
    305  1.177  rillig 	ps.decl_indent_done = false;
    306  1.177  rillig 	if (ps.extra_expr_indent == eei_last)
    307  1.177  rillig 		ps.extra_expr_indent = eei_no;
    308  1.177  rillig 
    309  1.177  rillig 	lab.len = 0;
    310  1.177  rillig 	code.len = 0;
    311  1.177  rillig 	com.len = 0;
    312  1.177  rillig 
    313  1.177  rillig 	ps.ind_level = ps.ind_level_follow;
    314  1.177  rillig 	ps.line_start_nparen = ps.nparen;
    315  1.177  rillig 
    316  1.177  rillig 	if (ps.nparen > 0) {
    317  1.177  rillig 		/* TODO: explain what negative indentation means */
    318  1.177  rillig 		paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
    319  1.177  rillig 		debug_println("paren_indent is now %d", paren_indent);
    320  1.177  rillig 	}
    321  1.161  rillig 
    322  1.177  rillig 	ps.want_blank = false;
    323  1.183  rillig 	out.line_kind = lk_other;
    324    1.1     cgd }
    325    1.1     cgd 
    326  1.113  rillig static int
    327  1.113  rillig compute_code_indent_lineup(int base_ind)
    328  1.113  rillig {
    329  1.177  rillig 	int ind = paren_indent;
    330  1.177  rillig 	int overflow = ind_add(ind, code.st, code.len) - opt.max_line_length;
    331  1.177  rillig 	if (overflow < 0)
    332  1.177  rillig 		return ind;
    333  1.177  rillig 
    334  1.177  rillig 	if (ind_add(base_ind, code.st, code.len) < opt.max_line_length) {
    335  1.177  rillig 		ind -= overflow + 2;
    336  1.177  rillig 		if (ind > base_ind)
    337  1.177  rillig 			return ind;
    338  1.177  rillig 		return base_ind;
    339  1.177  rillig 	}
    340  1.177  rillig 
    341  1.162  rillig 	return ind;
    342  1.113  rillig }
    343  1.113  rillig 
    344    1.5   lukem int
    345   1.39  rillig compute_code_indent(void)
    346    1.1     cgd {
    347  1.177  rillig 	int base_ind = ps.ind_level * opt.indent_size;
    348   1.19   kamil 
    349  1.177  rillig 	if (ps.line_start_nparen == 0) {
    350  1.191  rillig 		if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum)
    351  1.187  rillig 			return base_ind;
    352  1.187  rillig 		if (ps.in_stmt_cont)
    353  1.177  rillig 			return base_ind + opt.continuation_indent;
    354  1.177  rillig 		return base_ind;
    355  1.177  rillig 	}
    356  1.177  rillig 
    357  1.177  rillig 	if (opt.lineup_to_parens) {
    358  1.177  rillig 		if (opt.lineup_to_parens_always)
    359  1.177  rillig 			return paren_indent;
    360  1.177  rillig 		return compute_code_indent_lineup(base_ind);
    361  1.177  rillig 	}
    362  1.177  rillig 
    363  1.177  rillig 	if (ps.extra_expr_indent != eei_no)
    364  1.177  rillig 		return base_ind + 2 * opt.continuation_indent;
    365  1.177  rillig 
    366  1.177  rillig 	if (2 * opt.continuation_indent == opt.indent_size)
    367  1.177  rillig 		return base_ind + opt.continuation_indent;
    368  1.177  rillig 	else
    369  1.178  rillig 		return base_ind +
    370  1.178  rillig 		    opt.continuation_indent * ps.line_start_nparen;
    371    1.1     cgd }
    372    1.1     cgd 
    373    1.5   lukem int
    374   1.38  rillig compute_label_indent(void)
    375    1.1     cgd {
    376  1.188  rillig 	if (out.line_kind == lk_case_or_default)
    377  1.177  rillig 		return (int)(case_ind * (float)opt.indent_size);
    378  1.177  rillig 	if (lab.st[0] == '#')
    379  1.177  rillig 		return 0;
    380  1.177  rillig 	return opt.indent_size * (ps.ind_level - 2);
    381    1.1     cgd }
    382    1.1     cgd 
    383    1.5   lukem void
    384  1.116  rillig inp_read_line(void)
    385   1.67  rillig {
    386  1.177  rillig 	if (indent_enabled == indent_on)
    387  1.183  rillig 		out.indent_off_text.len = 0;
    388  1.183  rillig 	buf_add_chars(&out.indent_off_text, inp.mem, inp.len);
    389  1.177  rillig 	inp_read_next_line(input);
    390  1.173  rillig }
    391