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