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