Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.31
      1 /*	$NetBSD: io.c,v 1.31 2021/03/12 23:16:00 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 #ifndef lint
     42 static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
     43 #endif /* not lint */
     44 #endif
     45 
     46 #include <sys/cdefs.h>
     47 #ifndef lint
     48 #if defined(__NetBSD__)
     49 __RCSID("$NetBSD: io.c,v 1.31 2021/03/12 23:16:00 rillig Exp $");
     50 #elif defined(__FreeBSD__)
     51 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
     52 #endif
     53 #endif
     54 
     55 #include <ctype.h>
     56 #include <err.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <stdarg.h>
     61 
     62 #include "indent.h"
     63 
     64 int         comment_open;
     65 static int  paren_indent;
     66 static int pad_output(int current, int target);
     67 
     68 static void
     69 output_char(char ch)
     70 {
     71     fputc(ch, output);
     72 }
     73 
     74 static void
     75 output_range(const char *s, const char *e)
     76 {
     77     fwrite(s, 1, (size_t)(e - s), output);
     78 }
     79 
     80 static inline void
     81 output_string(const char *s)
     82 {
     83     output_range(s, s + strlen(s));
     84 }
     85 
     86 static void
     87 output_int(int i)
     88 {
     89     fprintf(output, "%d", i);
     90 }
     91 
     92 /*
     93  * dump_line is the routine that actually effects the printing of the new
     94  * source. It prints the label section, followed by the code section with
     95  * the appropriate nesting level, followed by any comments.
     96  */
     97 void
     98 dump_line(void)
     99 {
    100     int cur_col, target_col;
    101     static int  not_first_line;
    102 
    103     if (ps.procname[0]) {
    104 	ps.ind_level = 0;
    105 	ps.procname[0] = 0;
    106     }
    107 
    108     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
    109 	if (suppress_blanklines > 0)
    110 	    suppress_blanklines--;
    111 	else {
    112 	    ps.bl_line = true;
    113 	    n_real_blanklines++;
    114 	}
    115     } else if (!inhibit_formatting) {
    116 	suppress_blanklines = 0;
    117 	ps.bl_line = false;
    118 	if (prefix_blankline_requested && not_first_line) {
    119 	    if (opt.swallow_optional_blanklines) {
    120 		if (n_real_blanklines == 1)
    121 		    n_real_blanklines = 0;
    122 	    } else {
    123 		if (n_real_blanklines == 0)
    124 		    n_real_blanklines = 1;
    125 	    }
    126 	}
    127 	while (--n_real_blanklines >= 0)
    128 	    output_char('\n');
    129 	n_real_blanklines = 0;
    130 	if (ps.ind_level == 0)
    131 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
    132 				 * additional statement indentation if we are
    133 				 * at bracket level 0 */
    134 
    135 	if (e_lab != s_lab || e_code != s_code)
    136 	    ++code_lines;	/* keep count of lines with code */
    137 
    138 
    139 	if (e_lab != s_lab) {	/* print lab, if any */
    140 	    if (comment_open) {
    141 		comment_open = 0;
    142 		output_string(".*/\n");
    143 	    }
    144 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
    145 		e_lab--;
    146 	    *e_lab = '\0';
    147 	    cur_col = pad_output(1, compute_label_indent());
    148 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
    149 				    || strncmp(s_lab, "#endif", 6) == 0)) {
    150 		char *s = s_lab;
    151 		if (e_lab[-1] == '\n') e_lab--;
    152 		do {
    153 		    output_char(*s++);
    154 		} while (s < e_lab && 'a' <= *s && *s <= 'z');
    155 		while ((*s == ' ' || *s == '\t') && s < e_lab)
    156 		    s++;
    157 		if (s < e_lab) {
    158 		    if (s[0] == '/' && s[1] == '*') {
    159 			output_char('\t');
    160 			output_range(s, e_lab);
    161 		    } else {
    162 		        output_string("\t/* ");
    163 			output_range(s, e_lab);
    164 			output_string(" */");
    165 		    }
    166 		}
    167 	    } else
    168 	        output_range(s_lab, e_lab);
    169 	    cur_col = count_spaces(cur_col, s_lab);
    170 	} else
    171 	    cur_col = 1;	/* there is no label section */
    172 
    173 	ps.pcase = false;
    174 
    175 	if (s_code != e_code) {	/* print code section, if any */
    176 	    char *p;
    177 
    178 	    if (comment_open) {
    179 		comment_open = 0;
    180 		output_string(".*/\n");
    181 	    }
    182 	    target_col = compute_code_indent();
    183 	    {
    184 		int i;
    185 
    186 		for (i = 0; i < ps.p_l_follow; i++)
    187 		    if (ps.paren_indents[i] >= 0)
    188 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
    189 	    }
    190 	    cur_col = pad_output(cur_col, target_col);
    191 	    for (p = s_code; p < e_code; p++)
    192 		if (*p == (char) 0200)
    193 		    output_int(target_col * 7);
    194 		else
    195 		    output_char(*p);
    196 	    cur_col = count_spaces(cur_col, s_code);
    197 	}
    198 	if (s_com != e_com) {		/* print comment, if any */
    199 	    int target = ps.com_col;
    200 	    char *com_st = s_com;
    201 
    202 	    target += ps.comment_delta;
    203 	    while (*com_st == '\t')	/* consider original indentation in
    204 				     * case this is a box comment */
    205 		com_st++, target += opt.tabsize;
    206 	    while (target <= 0)
    207 		if (*com_st == ' ')
    208 		    target++, com_st++;
    209 		else if (*com_st == '\t') {
    210 		    target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
    211 		    com_st++;
    212 		} else
    213 		    target = 1;
    214 	    if (cur_col > target) {	/* if comment can't fit on this line,
    215 				     * put it on next line */
    216 		output_char('\n');
    217 		cur_col = 1;
    218 		++ps.out_lines;
    219 	    }
    220 	    while (e_com > com_st && isspace((unsigned char)e_com[-1]))
    221 		e_com--;
    222 	    (void)pad_output(cur_col, target);
    223 	    output_range(com_st, e_com);
    224 	    ps.comment_delta = ps.n_comment_delta;
    225 	    ++ps.com_lines;	/* count lines with comments */
    226 	}
    227 	if (ps.use_ff)
    228 	    output_char('\014');
    229 	else
    230 	    output_char('\n');
    231 	++ps.out_lines;
    232 	if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
    233 	    prefix_blankline_requested = 1;
    234 	    ps.just_saw_decl = 0;
    235 	} else
    236 	    prefix_blankline_requested = postfix_blankline_requested;
    237 	postfix_blankline_requested = 0;
    238     }
    239 
    240     /* keep blank lines after '//' comments */
    241     if (e_com - s_com > 1 && s_com[1] == '/')
    242 	output_range(s_token, e_token);
    243 
    244     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
    245 					 * declaration, remember that fact for
    246 					 * proper comment indentation */
    247     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
    248 						 * indented if we have not
    249 						 * completed this stmt and if
    250 						 * we are not in the middle of
    251 						 * a declaration */
    252     ps.use_ff = false;
    253     ps.dumped_decl_indent = 0;
    254     *(e_lab = s_lab) = '\0';	/* reset buffers */
    255     *(e_code = s_code) = '\0';
    256     *(e_com = s_com = combuf + 1) = '\0';
    257     ps.ind_level = ps.i_l_follow;
    258     ps.paren_level = ps.p_l_follow;
    259     if (ps.paren_level > 0)
    260 	paren_indent = -ps.paren_indents[ps.paren_level - 1];
    261     not_first_line = 1;
    262 }
    263 
    264 int
    265 compute_code_indent(void)
    266 {
    267     int target_col = opt.ind_size * ps.ind_level + 1;
    268 
    269     if (ps.paren_level) {
    270 	if (!opt.lineup_to_parens)
    271 	    target_col += opt.continuation_indent *
    272 		(2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
    273 	else if (opt.lineup_to_parens_always)
    274 	    target_col = paren_indent;
    275 	else {
    276 	    int w;
    277 	    int t = paren_indent;
    278 
    279 	    if ((w = count_spaces(t, s_code) - opt.max_col) > 0
    280 		    && count_spaces(target_col, s_code) <= opt.max_col) {
    281 		t -= w + 1;
    282 		if (t > target_col)
    283 		    target_col = t;
    284 	    } else
    285 		target_col = t;
    286 	}
    287     } else if (ps.ind_stmt)
    288 	target_col += opt.continuation_indent;
    289     return target_col;
    290 }
    291 
    292 int
    293 compute_label_indent(void)
    294 {
    295     return
    296 	ps.pcase ? (int) (case_ind * opt.ind_size) + 1
    297 	: *s_lab == '#' ? 1
    298 	: opt.ind_size * (ps.ind_level - label_offset) + 1;
    299 }
    300 
    301 
    302 /*
    303  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    304  *
    305  * All rights reserved
    306  *
    307  *
    308  * NAME: fill_buffer
    309  *
    310  * FUNCTION: Reads one block of input into input_buffer
    311  *
    312  * HISTORY: initial coding	November 1976	D A Willcox of CAC 1/7/77 A
    313  * Willcox of CAC	Added check for switch back to partly full input
    314  * buffer from temporary buffer
    315  *
    316  */
    317 void
    318 fill_buffer(void)
    319 {				/* this routine reads stuff from the input */
    320     char *p;
    321     int i;
    322     FILE *f = input;
    323 
    324     if (bp_save != NULL) {	/* there is a partly filled input buffer left */
    325 	buf_ptr = bp_save;	/* do not read anything, just switch buffers */
    326 	buf_end = be_save;
    327 	bp_save = be_save = NULL;
    328 	if (buf_ptr < buf_end)
    329 	    return;		/* only return if there is really something in
    330 				 * this buffer */
    331     }
    332     for (p = in_buffer;;) {
    333 	if (p >= in_buffer_limit) {
    334 	    int size = (in_buffer_limit - in_buffer) * 2 + 10;
    335 	    int offset = p - in_buffer;
    336 	    in_buffer = realloc(in_buffer, size);
    337 	    if (in_buffer == NULL)
    338 		errx(1, "input line too long");
    339 	    p = in_buffer + offset;
    340 	    in_buffer_limit = in_buffer + size - 2;
    341 	}
    342 	if ((i = getc(f)) == EOF) {
    343 		*p++ = ' ';
    344 		*p++ = '\n';
    345 		had_eof = true;
    346 		break;
    347 	}
    348 	if (i != '\0')
    349 	    *p++ = i;
    350 	if (i == '\n')
    351 		break;
    352     }
    353     buf_ptr = in_buffer;
    354     buf_end = p;
    355     if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
    356 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
    357 	    fill_buffer();	/* flush indent error message */
    358 	else {
    359 	    int         com = 0;
    360 
    361 	    p = in_buffer;
    362 	    while (*p == ' ' || *p == '\t')
    363 		p++;
    364 	    if (*p == '/' && p[1] == '*') {
    365 		p += 2;
    366 		while (*p == ' ' || *p == '\t')
    367 		    p++;
    368 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
    369 			&& p[4] == 'N' && p[5] == 'T') {
    370 		    p += 6;
    371 		    while (*p == ' ' || *p == '\t')
    372 			p++;
    373 		    if (*p == '*')
    374 			com = 1;
    375 		    else if (*p == 'O') {
    376 			if (*++p == 'N')
    377 			    p++, com = 1;
    378 			else if (*p == 'F' && *++p == 'F')
    379 			    p++, com = 2;
    380 		    }
    381 		    while (*p == ' ' || *p == '\t')
    382 			p++;
    383 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
    384 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
    385 			    dump_line();
    386 			if (!(inhibit_formatting = com - 1)) {
    387 			    n_real_blanklines = 0;
    388 			    postfix_blankline_requested = 0;
    389 			    prefix_blankline_requested = 0;
    390 			    suppress_blanklines = 1;
    391 			}
    392 		    }
    393 		}
    394 	    }
    395 	}
    396     }
    397     if (inhibit_formatting) {
    398 	p = in_buffer;
    399 	do {
    400 	    output_char(*p);
    401 	} while (*p++ != '\n');
    402     }
    403 }
    404 
    405 /*
    406  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    407  *
    408  * All rights reserved
    409  *
    410  *
    411  * NAME: pad_output
    412  *
    413  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
    414  * position.
    415  *
    416  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
    417  *
    418  * PARAMETERS: current		integer		The current column target
    419  * nteger		The desired column
    420  *
    421  * RETURNS: Integer value of the new column.  (If current >= target, no action is
    422  * taken, and current is returned.
    423  *
    424  * GLOBALS: None
    425  *
    426  * CALLS: write (sys)
    427  *
    428  * CALLED BY: dump_line
    429  *
    430  * HISTORY: initial coding	November 1976	D A Willcox of CAC
    431  *
    432  */
    433 static int
    434 pad_output(int current, int target)
    435 			        /* writes tabs and blanks (if necessary) to
    436 				 * get the current output position up to the
    437 				 * target column */
    438     /* current: the current column value */
    439     /* target: position we want it at */
    440 {
    441     int curr;			/* internal column pointer */
    442 
    443     if (current >= target)
    444 	return current;		/* line is already long enough */
    445     curr = current;
    446     if (opt.use_tabs) {
    447 	int tcur;
    448 
    449 	while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) {
    450 	    output_char('\t');
    451 	    curr = tcur;
    452 	}
    453     }
    454     while (curr++ < target)
    455 	output_char(' ');	/* pad with final blanks */
    456 
    457     return target;
    458 }
    459 
    460 /*
    461  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    462  *
    463  * All rights reserved
    464  *
    465  *
    466  * NAME: count_spaces
    467  *
    468  * FUNCTION: Find out where printing of a given string will leave the current
    469  * character position on output.
    470  *
    471  * ALGORITHM: Run thru input string and add appropriate values to current
    472  * position.
    473  *
    474  * RETURNS: Integer value of position after printing "buffer" starting in column
    475  * "current".
    476  *
    477  * HISTORY: initial coding	November 1976	D A Willcox of CAC
    478  *
    479  */
    480 int
    481 count_spaces_until(int cur, char *buffer, char *end)
    482 /*
    483  * this routine figures out where the character position will be after
    484  * printing the text in buffer starting at column "current"
    485  */
    486 {
    487     char *buf;		/* used to look thru buffer */
    488 
    489     for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
    490 	switch (*buf) {
    491 
    492 	case '\n':
    493 	case 014:		/* form feed */
    494 	    cur = 1;
    495 	    break;
    496 
    497 	case '\t':
    498 	    cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1;
    499 	    break;
    500 
    501 	case 010:		/* backspace */
    502 	    --cur;
    503 	    break;
    504 
    505 	default:
    506 	    ++cur;
    507 	    break;
    508 	}			/* end of switch */
    509     }				/* end of for loop */
    510     return cur;
    511 }
    512 
    513 int
    514 count_spaces(int cur, char *buffer)
    515 {
    516     return count_spaces_until(cur, buffer, NULL);
    517 }
    518 
    519 void
    520 diag(int level, const char *msg, ...)
    521 {
    522     va_list ap;
    523     const char *s, *e;
    524 
    525     if (level)
    526 	found_err = 1;
    527 
    528     if (output == stdout) {
    529 	s = "/**INDENT** ";
    530 	e = " */";
    531     } else {
    532 	s = e = "";
    533     }
    534 
    535     va_start(ap, msg);
    536     fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
    537     vfprintf(stderr, msg, ap);
    538     fprintf(stderr, "%s\n", e);
    539     va_end(ap);
    540 }
    541