Home | History | Annotate | Line # | Download | only in indent
pr_comment.c revision 1.29
      1 /*	$NetBSD: pr_comment.c,v 1.29 2021/03/13 13:51:08 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[] = "@(#)pr_comment.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: pr_comment.c,v 1.29 2021/03/13 13:51:08 rillig Exp $");
     50 #elif defined(__FreeBSD__)
     51 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
     52 #endif
     53 #endif
     54 
     55 #include <err.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 
     60 #include "indent.h"
     61 
     62 static void
     63 check_size_comment(size_t desired_size, char **last_bl_ptr)
     64 {
     65     if (e_com + (desired_size) < l_com)
     66         return;
     67 
     68     size_t nsize = l_com - s_com + 400 + desired_size;
     69     size_t com_len = e_com - s_com;
     70     ssize_t blank_pos = *last_bl_ptr != NULL ? *last_bl_ptr - combuf : -1;
     71     combuf = realloc(combuf, nsize);
     72     if (combuf == NULL)
     73 	err(1, NULL);
     74     e_com = combuf + com_len + 1;
     75     if (blank_pos > 0)
     76 	*last_bl_ptr = combuf + blank_pos;
     77     l_com = combuf + nsize - 5;
     78     s_com = combuf + 1;
     79 }
     80 
     81 /*
     82  * Scan, reformat and output a single comment, which is either a block comment
     83  * starting with '/' '*' or an end-of-line comment starting with '//'.
     84  *
     85  * Try to keep comments from going over the maximum line length.  If a line is
     86  * too long, move everything starting from the last blank to the next comment
     87  * line.  Blanks and tabs from the beginning of the input line are removed.
     88  *
     89  * ALGORITHM:
     90  *	1) Decide where the comment should be aligned, and if lines should
     91  *	   be broken.
     92  *	2) If lines should not be broken and filled, just copy up to end of
     93  *	   comment.
     94  *	3) If lines should be filled, then scan through the input buffer,
     95  *	   copying characters to com_buf.  Remember where the last blank,
     96  *	   tab, or newline was.  When line is filled, print up to last blank
     97  *	   and continue copying.
     98  */
     99 void
    100 process_comment(void)
    101 {
    102     int         adj_max_line_length; /* Adjusted max_line_length for comments
    103 				 * that spill over the right margin */
    104     char       *last_bl;	/* points to the last blank in the output
    105 				 * buffer */
    106     char       *t_ptr;		/* used for moving string */
    107     int         break_delim = opt.comment_delimiter_on_blankline;
    108     int         l_just_saw_decl = ps.just_saw_decl;
    109 
    110     adj_max_line_length = opt.max_line_length;
    111     ps.just_saw_decl = 0;
    112     last_bl = NULL;		/* no blanks found so far */
    113     ps.box_com = false;		/* at first, assume that we are not in
    114 				 * a boxed comment or some other
    115 				 * comment that should not be touched */
    116     ++ps.out_coms;		/* keep track of number of comments */
    117 
    118     /* Figure where to align and how to treat the comment */
    119 
    120     if (ps.col_1 && !opt.format_col1_comments) { /* if the comment starts in
    121 				 * column 1, it should not be touched */
    122 	ps.box_com = true;
    123 	break_delim = false;
    124 	ps.com_col = 1;
    125     } else {
    126 	if (*buf_ptr == '-' || *buf_ptr == '*' || e_token[-1] == '/' ||
    127 	    (*buf_ptr == '\n' && !opt.format_block_comments)) {
    128 	    ps.box_com = true;	/* A comment with a '-' or '*' immediately
    129 				 * after the /+* is assumed to be a boxed
    130 				 * comment. A comment with a newline
    131 				 * immediately after the /+* is assumed to
    132 				 * be a block comment and is treated as a
    133 				 * box comment unless format_block_comments
    134 				 * is nonzero (the default). */
    135 	    break_delim = false;
    136 	}
    137 	if ( /* ps.bl_line && */ s_lab == e_lab && s_code == e_code) {
    138 	    /* klg: check only if this line is blank */
    139 	    /*
    140 	     * If this (*and previous lines are*) blank, dont put comment way
    141 	     * out at left
    142 	     */
    143 	    ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.indent_size + 1;
    144 	    adj_max_line_length = opt.block_comment_max_line_length;
    145 	    if (ps.com_col <= 1)
    146 		ps.com_col = 1 + !opt.format_col1_comments;
    147 	} else {
    148 	    int target_col;
    149 	    break_delim = false;
    150 	    if (s_code != e_code)
    151 		target_col = 1 + indentation_after(compute_code_indent(), s_code);
    152 	    else {
    153 		target_col = 1;
    154 		if (s_lab != e_lab)
    155 		    target_col = 1 + indentation_after(compute_label_indent(), s_lab);
    156 	    }
    157 	    ps.com_col = ps.decl_on_line || ps.ind_level == 0
    158 		? opt.decl_comment_column : opt.comment_column;
    159 	    if (ps.com_col <= target_col)
    160 		ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
    161 	    if (ps.com_col + 24 > adj_max_line_length)
    162 	        /* XXX: mismatch between column and length */
    163 		adj_max_line_length = ps.com_col + 24;
    164 	}
    165     }
    166     if (ps.box_com) {
    167 	/*
    168 	 * Find out how much indentation there was originally, because that
    169 	 * much will have to be ignored by pad_output() in dump_line(). This
    170 	 * is a box comment, so nothing changes -- not even indentation.
    171 	 *
    172 	 * The comment we're about to read usually comes from in_buffer,
    173 	 * unless it has been copied into save_com.
    174 	 */
    175 	char *start;
    176 
    177 	/*
    178 	 * XXX: ordered comparison between pointers from different objects
    179 	 * invokes undefined behavior (C99 6.5.8).
    180 	 */
    181 	start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
    182 	    sc_buf : in_buffer;
    183 	ps.n_comment_delta = -indentation_after_range(0, start, buf_ptr - 2);
    184     } else {
    185 	ps.n_comment_delta = 0;
    186 	while (*buf_ptr == ' ' || *buf_ptr == '\t')
    187 	    buf_ptr++;
    188     }
    189     ps.comment_delta = 0;
    190     *e_com++ = '/';
    191     *e_com++ = e_token[-1];
    192     if (*buf_ptr != ' ' && !ps.box_com)
    193 	*e_com++ = ' ';
    194 
    195     /*
    196      * Don't put a break delimiter if this is a one-liner that won't wrap.
    197      */
    198     if (break_delim)
    199 	for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
    200 	    if (t_ptr >= buf_end)
    201 		fill_buffer();
    202 	    if (t_ptr[0] == '*' && t_ptr[1] == '/') {
    203 	        /* XXX: strange mixture between indentation, column, length */
    204 		if (adj_max_line_length >= 1 + indentation_after_range(ps.com_col - 1, buf_ptr, t_ptr + 2))
    205 		    break_delim = false;
    206 		break;
    207 	    }
    208 	}
    209 
    210     if (break_delim) {
    211 	char       *t = e_com;
    212 	e_com = s_com + 2;
    213 	*e_com = 0;
    214 	if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
    215 	    prefix_blankline_requested = 1;
    216 	dump_line();
    217 	e_com = s_com = t;
    218 	if (!ps.box_com && opt.star_comment_cont)
    219 	    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    220     }
    221 
    222     /* Start to copy the comment */
    223 
    224     while (1) {			/* this loop will go until the comment is
    225 				 * copied */
    226 	switch (*buf_ptr) {	/* this checks for various special cases */
    227 	case 014:		/* check for a form feed */
    228 	    check_size_comment(3, &last_bl);
    229 	    if (!ps.box_com) {	/* in a text comment, break the line here */
    230 		ps.use_ff = true;
    231 		/* fix so dump_line uses a form feed */
    232 		dump_line();
    233 		last_bl = NULL;
    234 		if (!ps.box_com && opt.star_comment_cont)
    235 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    236 		while (*++buf_ptr == ' ' || *buf_ptr == '\t')
    237 		    ;
    238 	    } else {
    239 		if (++buf_ptr >= buf_end)
    240 		    fill_buffer();
    241 		*e_com++ = 014;
    242 	    }
    243 	    break;
    244 
    245 	case '\n':
    246 	    if (e_token[-1] == '/') {
    247 		++line_no;
    248 		goto end_of_comment;
    249 	    }
    250 	    if (had_eof) {	/* check for unexpected eof */
    251 		printf("Unterminated comment\n");
    252 		dump_line();
    253 		return;
    254 	    }
    255 	    last_bl = NULL;
    256 	    check_size_comment(4, &last_bl);
    257 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
    258 						 * we dont ignore the newline */
    259 		if (s_com == e_com)
    260 		    *e_com++ = ' ';
    261 		if (!ps.box_com && e_com - s_com > 3) {
    262 		    dump_line();
    263 		    if (opt.star_comment_cont)
    264 			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    265 		}
    266 		dump_line();
    267 		if (!ps.box_com && opt.star_comment_cont)
    268 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    269 	    } else {
    270 		ps.last_nl = 1;
    271 		if (e_com[-1] == ' ' || e_com[-1] == '\t')
    272 		    last_bl = e_com - 1;
    273 		/*
    274 		 * if there was a space at the end of the last line, remember
    275 		 * where it was
    276 		 */
    277 		else {		/* otherwise, insert one */
    278 		    last_bl = e_com;
    279 		    *e_com++ = ' ';
    280 		}
    281 	    }
    282 	    ++line_no;		/* keep track of input line number */
    283 	    if (!ps.box_com) {
    284 		int         nstar = 1;
    285 		do {		/* flush any blanks and/or tabs at start of
    286 				 * next line */
    287 		    if (++buf_ptr >= buf_end)
    288 			fill_buffer();
    289 		    if (*buf_ptr == '*' && --nstar >= 0) {
    290 			if (++buf_ptr >= buf_end)
    291 			    fill_buffer();
    292 			if (*buf_ptr == '/')
    293 			    goto end_of_comment;
    294 		    }
    295 		} while (*buf_ptr == ' ' || *buf_ptr == '\t');
    296 	    } else if (++buf_ptr >= buf_end)
    297 		fill_buffer();
    298 	    break;		/* end of case for newline */
    299 
    300 	case '*':		/* must check for possibility of being at end
    301 				 * of comment */
    302 	    if (++buf_ptr >= buf_end)	/* get to next char after * */
    303 		fill_buffer();
    304 	    check_size_comment(4, &last_bl);
    305 	    if (*buf_ptr == '/') {	/* it is the end!!! */
    306 	end_of_comment:
    307 		if (++buf_ptr >= buf_end)
    308 		    fill_buffer();
    309 		if (break_delim) {
    310 		    if (e_com > s_com + 3)
    311 			dump_line();
    312 		    else
    313 			s_com = e_com;
    314 		    *e_com++ = ' ';
    315 		}
    316 		if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
    317 		    *e_com++ = ' ';	/* ensure blank before end */
    318 		if (e_token[-1] == '/')
    319 		    *e_com++ = '\n', *e_com = '\0';
    320 		else
    321 		    *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
    322 		ps.just_saw_decl = l_just_saw_decl;
    323 		return;
    324 	    } else		/* handle isolated '*' */
    325 		*e_com++ = '*';
    326 	    break;
    327 	default:		/* we have a random char */
    328 	    ;
    329 	    int now_len = indentation_after_range(ps.com_col - 1, s_com, e_com);
    330 	    do {
    331 		check_size_comment(1, &last_bl);
    332 		*e_com = *buf_ptr++;
    333 		if (buf_ptr >= buf_end)
    334 		    fill_buffer();
    335 		if (*e_com == ' ' || *e_com == '\t')
    336 		    last_bl = e_com;	/* remember we saw a blank */
    337 		++e_com;
    338 		now_len++;
    339 	    } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
    340 		(now_len < adj_max_line_length || !last_bl));
    341 	    ps.last_nl = false;
    342 	    /* XXX: signed character comparison '>' does not work for UTF-8 */
    343 	    if (now_len >= adj_max_line_length &&
    344 		    !ps.box_com && e_com[-1] > ' ') {
    345 		/*
    346 		 * the comment is too long, it must be broken up
    347 		 */
    348 		if (last_bl == NULL) {
    349 		    dump_line();
    350 		    if (!ps.box_com && opt.star_comment_cont)
    351 			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    352 		    break;
    353 		}
    354 		*e_com = '\0';
    355 		e_com = last_bl;
    356 		dump_line();
    357 		if (!ps.box_com && opt.star_comment_cont)
    358 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    359 		for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
    360 		    t_ptr++)
    361 			;
    362 		last_bl = NULL;
    363 		/*
    364 		 * t_ptr will be somewhere between e_com (dump_line() reset)
    365 		 * and l_com. So it's safe to copy byte by byte from t_ptr
    366 		 * to e_com without any check_size_comment().
    367 		 */
    368 		while (*t_ptr != '\0') {
    369 		    if (*t_ptr == ' ' || *t_ptr == '\t')
    370 			last_bl = e_com;
    371 		    *e_com++ = *t_ptr++;
    372 		}
    373 	    }
    374 	    break;
    375 	}
    376     }
    377 }
    378