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