Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.120
      1 /*	$NetBSD: io.c,v 1.120 2021/11/19 17:42:45 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.120 2021/11/19 17:42:45 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 <ctype.h>
     52 #include <stdarg.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 
     57 #include "indent.h"
     58 
     59 struct input_buffer inbuf;
     60 
     61 static int paren_indent;
     62 static bool suppress_blanklines;
     63 
     64 
     65 const char *
     66 inp_p(void)
     67 {
     68     return inbuf.inp.s;
     69 }
     70 
     71 const char *
     72 inp_line_end(void)
     73 {
     74     return inbuf.inp.e;
     75 }
     76 
     77 char
     78 inp_peek(void)
     79 {
     80     return *inbuf.inp.s;
     81 }
     82 
     83 char
     84 inp_lookahead(size_t i)
     85 {
     86     return inbuf.inp.s[i];
     87 }
     88 
     89 void
     90 inp_skip(void)
     91 {
     92     inbuf.inp.s++;
     93     if (inbuf.inp.s >= inbuf.inp.e)
     94 	inp_read_line();
     95 }
     96 
     97 char
     98 inp_next(void)
     99 {
    100     char ch = inp_peek();
    101     inp_skip();
    102     return ch;
    103 }
    104 
    105 #ifdef debug
    106 void
    107 debug_inp(const char *prefix)
    108 {
    109     debug_printf("%s:", prefix);
    110     debug_vis_range(" inp \"", inbuf.inp.s, inbuf.inp.e, "\"");
    111     if (inbuf.save_com_s != NULL)
    112 	debug_vis_range(" save_com \"",
    113 			inbuf.save_com_s, inbuf.save_com_e, "\"");
    114     if (inbuf.saved_inp_s != NULL)
    115 	debug_vis_range(" saved_inp \"",
    116 			inbuf.saved_inp_s, inbuf.saved_inp_e, "\"");
    117     debug_printf("\n");
    118 }
    119 #else
    120 #define debug_inp(prefix) do { } while (false)
    121 #endif
    122 
    123 
    124 static void
    125 inp_comment_check_size(size_t n)
    126 {
    127     if ((size_t)(inbuf.save_com_e - inbuf.save_com_buf) + n <=
    128 	array_length(inbuf.save_com_buf))
    129 	return;
    130 
    131     diag(1, "Internal buffer overflow - "
    132 	    "Move big comment from right after if, while, or whatever");
    133     fflush(output);
    134     exit(1);
    135 }
    136 
    137 void
    138 inp_comment_add_char(char ch)
    139 {
    140     inp_comment_check_size(1);
    141     *inbuf.save_com_e++ = ch;
    142 }
    143 
    144 void
    145 inp_comment_add_range(const char *s, const char *e)
    146 {
    147     size_t len = (size_t)(e - s);
    148     inp_comment_check_size(len);
    149     memcpy(inbuf.save_com_e, s, len);
    150     inbuf.save_com_e += len;
    151 }
    152 
    153 void
    154 inp_from_comment(void)
    155 {
    156     inbuf.saved_inp_s = inbuf.inp.s;
    157     inbuf.saved_inp_e = inbuf.inp.e;
    158 
    159     inbuf.inp.s = inbuf.save_com_s;	/* redirect lexi input to save_com_s */
    160     inbuf.inp.e = inbuf.save_com_e;
    161     /* XXX: what about save_com_s? */
    162     inbuf.save_com_e = NULL;
    163     debug_inp(__func__);
    164 }
    165 
    166 static void
    167 output_char(char ch)
    168 {
    169     fputc(ch, output);
    170     debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
    171 }
    172 
    173 static void
    174 output_range(const char *s, const char *e)
    175 {
    176     fwrite(s, 1, (size_t)(e - s), output);
    177     debug_vis_range("output_range \"", s, e, "\"\n");
    178 }
    179 
    180 static inline void
    181 output_string(const char *s)
    182 {
    183     output_range(s, s + strlen(s));
    184 }
    185 
    186 static int
    187 output_indent(int old_ind, int new_ind)
    188 {
    189     int ind = old_ind;
    190 
    191     if (opt.use_tabs) {
    192 	int tabsize = opt.tabsize;
    193 	int n = new_ind / tabsize - ind / tabsize;
    194 	if (n > 0)
    195 	    ind -= ind % tabsize;
    196 	for (int i = 0; i < n; i++) {
    197 	    fputc('\t', output);
    198 	    ind += tabsize;
    199 	}
    200     }
    201 
    202     for (; ind < new_ind; ind++)
    203 	fputc(' ', output);
    204 
    205     debug_println("output_indent %d", ind);
    206     return ind;
    207 }
    208 
    209 static int
    210 dump_line_label(void)
    211 {
    212     int ind;
    213 
    214     while (lab.e > lab.s && ch_isblank(lab.e[-1]))
    215 	lab.e--;
    216     *lab.e = '\0';
    217 
    218     ind = output_indent(0, compute_label_indent());
    219 
    220     if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0
    221 	    || strncmp(lab.s, "#endif", 6) == 0)) {
    222 	const char *s = lab.s;
    223 	if (lab.e[-1] == '\n')
    224 	    lab.e--;
    225 	do {
    226 	    output_char(*s++);
    227 	} while (s < lab.e && 'a' <= *s && *s <= 'z');
    228 
    229 	while (s < lab.e && ch_isblank(*s))
    230 	    s++;
    231 
    232 	if (s < lab.e) {
    233 	    if (s[0] == '/' && s[1] == '*') {
    234 		output_char('\t');
    235 		output_range(s, lab.e);
    236 	    } else {
    237 		output_string("\t/* ");
    238 		output_range(s, lab.e);
    239 		output_string(" */");
    240 	    }
    241 	}
    242     } else
    243 	output_range(lab.s, lab.e);
    244     ind = ind_add(ind, lab.s, lab.e);
    245 
    246     ps.is_case_label = false;
    247     return ind;
    248 }
    249 
    250 static int
    251 dump_line_code(int ind)
    252 {
    253 
    254     int target_ind = compute_code_indent();
    255     for (int i = 0; i < ps.p_l_follow; i++) {
    256 	if (ps.paren_indents[i] >= 0) {
    257 	    int paren_ind = ps.paren_indents[i];
    258 	    ps.paren_indents[i] = (short)(-1 - (paren_ind + target_ind));
    259 	    debug_println(
    260 		"setting paren_indents[%d] from %d to %d for column %d",
    261 		i, paren_ind, ps.paren_indents[i], target_ind + 1);
    262 	}
    263     }
    264 
    265     ind = output_indent(ind, target_ind);
    266     output_range(code.s, code.e);
    267     return ind_add(ind, code.s, code.e);
    268 }
    269 
    270 static void
    271 dump_line_comment(int ind)
    272 {
    273     int target_ind = ps.com_ind;
    274     const char *p = com.s;
    275 
    276     target_ind += ps.comment_delta;
    277 
    278     /* consider original indentation in case this is a box comment */
    279     for (; *p == '\t'; p++)
    280 	target_ind += opt.tabsize;
    281 
    282     for (; target_ind < 0; p++) {
    283 	if (*p == ' ')
    284 	    target_ind++;
    285 	else if (*p == '\t')
    286 	    target_ind = next_tab(target_ind);
    287 	else {
    288 	    target_ind = 0;
    289 	    break;
    290 	}
    291     }
    292 
    293     /* if comment can't fit on this line, put it on the next line */
    294     if (ind > target_ind) {
    295 	output_char('\n');
    296 	ind = 0;
    297 	ps.stats.lines++;
    298     }
    299 
    300     while (com.e > p && isspace((unsigned char)com.e[-1]))
    301 	com.e--;
    302 
    303     (void)output_indent(ind, target_ind);
    304     output_range(p, com.e);
    305 
    306     ps.comment_delta = ps.n_comment_delta;
    307     ps.stats.comment_lines++;
    308 }
    309 
    310 /*
    311  * Write a line of formatted source to the output file. The line consists of
    312  * the label, the code and the comment.
    313  */
    314 static void
    315 output_line(char line_terminator)
    316 {
    317     static bool first_line = true;
    318 
    319     ps.procname[0] = '\0';
    320 
    321     if (code.s == code.e && lab.s == lab.e && com.s == com.e) {
    322 	if (suppress_blanklines)
    323 	    suppress_blanklines = false;
    324 	else
    325 	    blank_lines_to_output++;
    326 
    327     } else if (!inhibit_formatting) {
    328 	suppress_blanklines = false;
    329 	if (blank_line_before && !first_line) {
    330 	    if (opt.swallow_optional_blanklines) {
    331 		if (blank_lines_to_output == 1)
    332 		    blank_lines_to_output = 0;
    333 	    } else {
    334 		if (blank_lines_to_output == 0)
    335 		    blank_lines_to_output = 1;
    336 	    }
    337 	}
    338 
    339 	for (; blank_lines_to_output > 0; blank_lines_to_output--)
    340 	    output_char('\n');
    341 
    342 	if (ps.ind_level == 0)
    343 	    ps.ind_stmt = false;	/* this is a class A kludge. don't do
    344 					 * additional statement indentation if
    345 					 * we are at bracket level 0 */
    346 
    347 	if (lab.e != lab.s || code.e != code.s)
    348 	    ps.stats.code_lines++;
    349 
    350 	int ind = 0;
    351 	if (lab.e != lab.s)
    352 	    ind = dump_line_label();
    353 	if (code.e != code.s)
    354 	    ind = dump_line_code(ind);
    355 	if (com.e != com.s)
    356 	    dump_line_comment(ind);
    357 
    358 	output_char(line_terminator);
    359 	ps.stats.lines++;
    360 
    361 	if (ps.just_saw_decl == 1 && opt.blanklines_after_decl) {
    362 	    blank_line_before = true;
    363 	    ps.just_saw_decl = 0;
    364 	} else
    365 	    blank_line_before = blank_line_after;
    366 	blank_line_after = false;
    367     }
    368 
    369     ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
    370     ps.ind_stmt = ps.in_stmt && !ps.in_decl;
    371     ps.decl_indent_done = false;
    372 
    373     *(lab.e = lab.s) = '\0';	/* reset buffers */
    374     *(code.e = code.s) = '\0';
    375     *(com.e = com.s = com.buf + 1) = '\0';
    376 
    377     ps.ind_level = ps.ind_level_follow;
    378     ps.paren_level = ps.p_l_follow;
    379 
    380     if (ps.paren_level > 0) {
    381 	/* TODO: explain what negative indentation means */
    382 	paren_indent = -1 - ps.paren_indents[ps.paren_level - 1];
    383 	debug_println("paren_indent is now %d", paren_indent);
    384     }
    385 
    386     first_line = false;
    387 }
    388 
    389 void
    390 dump_line(void)
    391 {
    392     output_line('\n');
    393 }
    394 
    395 void
    396 dump_line_ff(void)
    397 {
    398     output_line('\f');
    399 }
    400 
    401 static int
    402 compute_code_indent_lineup(int base_ind)
    403 {
    404     int ti = paren_indent;
    405     int overflow = ind_add(ti, code.s, code.e) - opt.max_line_length;
    406     if (overflow < 0)
    407 	return ti;
    408 
    409     if (ind_add(base_ind, code.s, code.e) < opt.max_line_length) {
    410 	ti -= overflow + 2;
    411 	if (ti > base_ind)
    412 	    return ti;
    413 	return base_ind;
    414     }
    415 
    416     return ti;
    417 }
    418 
    419 int
    420 compute_code_indent(void)
    421 {
    422     int base_ind = ps.ind_level * opt.indent_size;
    423 
    424     if (ps.paren_level == 0) {
    425 	if (ps.ind_stmt)
    426 	    return base_ind + opt.continuation_indent;
    427 	return base_ind;
    428     }
    429 
    430     if (opt.lineup_to_parens) {
    431 	if (opt.lineup_to_parens_always)
    432 	    return paren_indent;
    433 	return compute_code_indent_lineup(base_ind);
    434     }
    435 
    436     if (2 * opt.continuation_indent == opt.indent_size)
    437 	return base_ind + opt.continuation_indent;
    438     else
    439 	return base_ind + opt.continuation_indent * ps.paren_level;
    440 }
    441 
    442 int
    443 compute_label_indent(void)
    444 {
    445     if (ps.is_case_label)
    446 	return (int)(case_ind * (float)opt.indent_size);
    447     if (lab.s[0] == '#')
    448 	return 0;
    449     return opt.indent_size * (ps.ind_level - 2);
    450 }
    451 
    452 static void
    453 skip_blank(const char **pp)
    454 {
    455     while (ch_isblank(**pp))
    456 	(*pp)++;
    457 }
    458 
    459 static bool
    460 skip_string(const char **pp, const char *s)
    461 {
    462     size_t len = strlen(s);
    463     if (strncmp(*pp, s, len) == 0) {
    464 	*pp += len;
    465 	return true;
    466     }
    467     return false;
    468 }
    469 
    470 static void
    471 parse_indent_comment(void)
    472 {
    473     bool on;
    474 
    475     const char *p = inbuf.inp.buf;
    476 
    477     skip_blank(&p);
    478     if (!skip_string(&p, "/*"))
    479 	return;
    480     skip_blank(&p);
    481     if (!skip_string(&p, "INDENT"))
    482 	return;
    483     skip_blank(&p);
    484 
    485     if (*p == '*' || skip_string(&p, "ON"))
    486 	on = true;
    487     else if (skip_string(&p, "OFF"))
    488 	on = false;
    489     else
    490 	return;
    491 
    492     skip_blank(&p);
    493     if (!skip_string(&p, "*/\n"))
    494 	return;
    495 
    496     if (com.s != com.e || lab.s != lab.e || code.s != code.e)
    497 	dump_line();
    498 
    499     inhibit_formatting = !on;
    500     if (on) {
    501 	blank_lines_to_output = 0;
    502 	blank_line_after = false;
    503 	blank_line_before = false;
    504 	suppress_blanklines = true;
    505     }
    506 }
    507 
    508 /*
    509  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    510  *
    511  * All rights reserved
    512  */
    513 void
    514 inp_read_line(void)
    515 {
    516     char *p;
    517     int ch;
    518     FILE *f = input;
    519 
    520     if (inbuf.saved_inp_s != NULL) {	/* there is a partly filled input buffer left */
    521 	inbuf.inp.s = inbuf.saved_inp_s;	/* do not read anything, just switch buffers */
    522 	inbuf.inp.e = inbuf.saved_inp_e;
    523 	inbuf.saved_inp_s = inbuf.saved_inp_e = NULL;
    524 	debug_println("switched inp.s back to saved_inp_s");
    525 	if (inbuf.inp.s < inbuf.inp.e)
    526 	    return;		/* only return if there is really something in
    527 				 * this buffer */
    528     }
    529 
    530     for (p = inbuf.inp.buf;;) {
    531 	if (p >= inbuf.inp.l) {
    532 	    size_t size = (size_t)(inbuf.inp.l - inbuf.inp.buf) * 2 + 10;
    533 	    size_t offset = (size_t)(p - inbuf.inp.buf);
    534 	    inbuf.inp.buf = xrealloc(inbuf.inp.buf, size);
    535 	    p = inbuf.inp.buf + offset;
    536 	    inbuf.inp.l = inbuf.inp.buf + size - 2;
    537 	}
    538 
    539 	if ((ch = getc(f)) == EOF) {
    540 	    if (!inhibit_formatting) {
    541 		*p++ = ' ';
    542 		*p++ = '\n';
    543 	    }
    544 	    had_eof = true;
    545 	    break;
    546 	}
    547 
    548 	if (ch != '\0')
    549 	    *p++ = (char)ch;
    550 	if (ch == '\n')
    551 	    break;
    552     }
    553 
    554     inbuf.inp.s = inbuf.inp.buf;
    555     inbuf.inp.e = p;
    556 
    557     if (p - inbuf.inp.s >= 3 && p[-3] == '*' && p[-2] == '/')
    558 	parse_indent_comment();
    559 
    560     if (inhibit_formatting)
    561 	output_range(inbuf.inp.s, inbuf.inp.e);
    562 }
    563 
    564 int
    565 ind_add(int ind, const char *start, const char *end)
    566 {
    567     for (const char *p = start; p != end; ++p) {
    568 	if (*p == '\n' || *p == '\f')
    569 	    ind = 0;
    570 	else if (*p == '\t')
    571 	    ind = next_tab(ind);
    572 	else if (*p == '\b')
    573 	    --ind;
    574 	else
    575 	    ++ind;
    576     }
    577     return ind;
    578 }
    579