Home | History | Annotate | Line # | Download | only in indent
pr_comment.c revision 1.11
      1 /*	$NetBSD: pr_comment.c,v 1.11 2019/04/04 15:22:13 kamil 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.11 2019/04/04 15:22:13 kamil 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 #include "indent_globs.h"
     60 #include "indent_codes.h"
     61 #include "indent.h"
     62 /*
     63  * NAME:
     64  *	pr_comment
     65  *
     66  * FUNCTION:
     67  *	This routine takes care of scanning and printing comments.
     68  *
     69  * ALGORITHM:
     70  *	1) Decide where the comment should be aligned, and if lines should
     71  *	   be broken.
     72  *	2) If lines should not be broken and filled, just copy up to end of
     73  *	   comment.
     74  *	3) If lines should be filled, then scan thru input_buffer copying
     75  *	   characters to com_buf.  Remember where the last blank, tab, or
     76  *	   newline was.  When line is filled, print up to last blank and
     77  *	   continue copying.
     78  *
     79  * HISTORY:
     80  *	November 1976	D A Willcox of CAC	Initial coding
     81  *	12/6/76		D A Willcox of CAC	Modification to handle
     82  *						UNIX-style comments
     83  *
     84  */
     85 
     87 /*
     88  * this routine processes comments.  It makes an attempt to keep comments from
     89  * going over the max line length.  If a line is too long, it moves everything
     90  * from the last blank to the next comment line.  Blanks and tabs from the
     91  * beginning of the input line are removed
     92  */
     93 
     94 void
     95 pr_comment(void)
     96 {
     97     int         now_col;	/* column we are in now */
     98     int         adj_max_col;	/* Adjusted max_col for when we decide to
     99 				 * spill comments over the right margin */
    100     char       *last_bl;	/* points to the last blank in the output
    101 				 * buffer */
    102     char       *t_ptr;		/* used for moving string */
    103     int         break_delim = opt.comment_delimiter_on_blankline;
    104     int         l_just_saw_decl = ps.just_saw_decl;
    105 
    106     adj_max_col = opt.max_col;
    107     ps.just_saw_decl = 0;
    108     last_bl = NULL;		/* no blanks found so far */
    109     ps.box_com = false;		/* at first, assume that we are not in
    110 					 * a boxed comment or some other
    111 					 * comment that should not be touched */
    112     ++ps.out_coms;		/* keep track of number of comments */
    113 
    114     /* Figure where to align and how to treat the comment */
    115 
    116     if (ps.col_1 && !opt.format_col1_comments) {	/* if comment starts in column
    117 						 * 1 it should not be touched */
    118 	ps.box_com = true;
    119 	break_delim = false;
    120 	ps.com_col = 1;
    121     }
    122     else {
    123 	if (*buf_ptr == '-' || *buf_ptr == '*' ||
    124 	    (*buf_ptr == '\n' && !opt.format_block_comments)) {
    125 	    ps.box_com = true;	/* A comment with a '-' or '*' immediately
    126 				 * after the /+* is assumed to be a boxed
    127 				 * comment. A comment with a newline
    128 				 * immediately after the /+* is assumed to
    129 				 * be a block comment and is treated as a
    130 				 * box comment unless format_block_comments
    131 				 * is nonzero (the default). */
    132 	    break_delim = false;
    133 	}
    134 	if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
    135 	    /* klg: check only if this line is blank */
    136 	    /*
    137 	     * If this (*and previous lines are*) blank, dont put comment way
    138 	     * out at left
    139 	     */
    140 	    ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
    141 	    adj_max_col = opt.block_comment_max_col;
    142 	    if (ps.com_col <= 1)
    143 		ps.com_col = 1 + !opt.format_col1_comments;
    144 	}
    145 	else {
    146 	    int target_col;
    147 	    break_delim = false;
    148 	    if (s_code != e_code)
    149 		target_col = count_spaces(compute_code_target(), s_code);
    150 	    else {
    151 		target_col = 1;
    152 		if (s_lab != e_lab)
    153 		    target_col = count_spaces(compute_label_target(), s_lab);
    154 	    }
    155 	    ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
    156 	    if (ps.com_col <= target_col)
    157 		ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
    158 	    if (ps.com_col + 24 > adj_max_col)
    159 		adj_max_col = ps.com_col + 24;
    160 	}
    161     }
    162     if (ps.box_com) {
    163 	/*
    164 	 * Find out how much indentation there was originally, because that
    165 	 * much will have to be ignored by pad_output() in dump_line(). This
    166 	 * is a box comment, so nothing changes -- not even indentation.
    167 	 *
    168 	 * The comment we're about to read usually comes from in_buffer,
    169 	 * unless it has been copied into save_com.
    170 	 */
    171 	char *start;
    172 
    173 	start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
    174 	    sc_buf : in_buffer;
    175 	ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
    176     }
    177     else {
    178 	ps.n_comment_delta = 0;
    179 	while (*buf_ptr == ' ' || *buf_ptr == '\t')
    180 	    buf_ptr++;
    181     }
    182     ps.comment_delta = 0;
    183     *e_com++ = '/';		/* put '/' followed by '*' into buffer */
    184     *e_com++ = '*';
    185     if (*buf_ptr != ' ' && !ps.box_com)
    186 	*e_com++ = ' ';
    187 
    188     /*
    189      * Don't put a break delimiter if this is a one-liner that won't wrap.
    190      */
    191     if (break_delim)
    192 	for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
    193 	    if (t_ptr >= buf_end)
    194 		fill_buffer();
    195 	    if (t_ptr[0] == '*' && t_ptr[1] == '/') {
    196 		if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
    197 		    break_delim = false;
    198 		break;
    199 	    }
    200 	}
    201 
    202     if (break_delim) {
    203 	char       *t = e_com;
    204 	e_com = s_com + 2;
    205 	*e_com = 0;
    206 	if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
    207 	    prefix_blankline_requested = 1;
    208 	dump_line();
    209 	e_com = s_com = t;
    210 	if (!ps.box_com && opt.star_comment_cont)
    211 	    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    212     }
    213 
    214     /* Start to copy the comment */
    215 
    216     while (1) {			/* this loop will go until the comment is
    217 				 * copied */
    218 	switch (*buf_ptr) {	/* this checks for various spcl cases */
    219 	case 014:		/* check for a form feed */
    220 	    CHECK_SIZE_COM(3);
    221 	    if (!ps.box_com) {	/* in a text comment, break the line here */
    222 		ps.use_ff = true;
    223 		/* fix so dump_line uses a form feed */
    224 		dump_line();
    225 		last_bl = NULL;
    226 		if (!ps.box_com && opt.star_comment_cont)
    227 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    228 		while (*++buf_ptr == ' ' || *buf_ptr == '\t')
    229 		    ;
    230 	    }
    231 	    else {
    232 		if (++buf_ptr >= buf_end)
    233 		    fill_buffer();
    234 		*e_com++ = 014;
    235 	    }
    236 	    break;
    237 
    238 	case '\n':
    239 	    if (had_eof) {	/* check for unexpected eof */
    240 		printf("Unterminated comment\n");
    241 		dump_line();
    242 		return;
    243 	    }
    244 	    last_bl = NULL;
    245 	    CHECK_SIZE_COM(4);
    246 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
    247 						 * we dont ignore the newline */
    248 		if (s_com == e_com)
    249 		    *e_com++ = ' ';
    250 		if (!ps.box_com && e_com - s_com > 3) {
    251 		    dump_line();
    252 		    if (opt.star_comment_cont)
    253 			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    254 		}
    255 		dump_line();
    256 		if (!ps.box_com && opt.star_comment_cont)
    257 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    258 	    }
    259 	    else {
    260 		ps.last_nl = 1;
    261 		if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
    262 		    last_bl = e_com - 1;
    263 		/*
    264 		 * if there was a space at the end of the last line, remember
    265 		 * where it was
    266 		 */
    267 		else {		/* otherwise, insert one */
    268 		    last_bl = e_com;
    269 		    *e_com++ = ' ';
    270 		}
    271 	    }
    272 	    ++line_no;		/* keep track of input line number */
    273 	    if (!ps.box_com) {
    274 		int         nstar = 1;
    275 		do {		/* flush any blanks and/or tabs at start of
    276 				 * next line */
    277 		    if (++buf_ptr >= buf_end)
    278 			fill_buffer();
    279 		    if (*buf_ptr == '*' && --nstar >= 0) {
    280 			if (++buf_ptr >= buf_end)
    281 			    fill_buffer();
    282 			if (*buf_ptr == '/')
    283 			    goto end_of_comment;
    284 		    }
    285 		} while (*buf_ptr == ' ' || *buf_ptr == '\t');
    286 	    }
    287 	    else if (++buf_ptr >= buf_end)
    288 		fill_buffer();
    289 	    break;		/* end of case for newline */
    290 
    291 	case '*':		/* must check for possibility of being at end
    292 				 * of comment */
    293 	    if (++buf_ptr >= buf_end)	/* get to next char after * */
    294 		fill_buffer();
    295 	    CHECK_SIZE_COM(4);
    296 	    if (*buf_ptr == '/') {	/* it is the end!!! */
    297 	end_of_comment:
    298 		if (++buf_ptr >= buf_end)
    299 		    fill_buffer();
    300 		if (break_delim) {
    301 		    if (e_com > s_com + 3) {
    302 			dump_line();
    303 		    }
    304 		    else
    305 			s_com = e_com;
    306 		    *e_com++ = ' ';
    307 		}
    308 		if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
    309 		    *e_com++ = ' ';	/* ensure blank before end */
    310 		*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
    311 		ps.just_saw_decl = l_just_saw_decl;
    312 		return;
    313 	    }
    314 	    else		/* handle isolated '*' */
    315 		*e_com++ = '*';
    316 	    break;
    317 	default:		/* we have a random char */
    318 	    now_col = count_spaces_until(ps.com_col, s_com, e_com);
    319 	    do {
    320 		CHECK_SIZE_COM(1);
    321 		*e_com = *buf_ptr++;
    322 		if (buf_ptr >= buf_end)
    323 		    fill_buffer();
    324 		if (*e_com == ' ' || *e_com == '\t')
    325 		    last_bl = e_com;	/* remember we saw a blank */
    326 		++e_com;
    327 		now_col++;
    328 	    } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
    329 		(now_col <= adj_max_col || !last_bl));
    330 	    ps.last_nl = false;
    331 	    if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
    332 		/*
    333 		 * the comment is too long, it must be broken up
    334 		 */
    335 		if (last_bl == NULL) {
    336 		    dump_line();
    337 		    if (!ps.box_com && opt.star_comment_cont)
    338 			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    339 		    break;
    340 		}
    341 		*e_com = '\0';
    342 		e_com = last_bl;
    343 		dump_line();
    344 		if (!ps.box_com && opt.star_comment_cont)
    345 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    346 		for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
    347 		    t_ptr++)
    348 			;
    349 		last_bl = NULL;
    350 		/*
    351 		 * t_ptr will be somewhere between e_com (dump_line() reset)
    352 		 * and l_com. So it's safe to copy byte by byte from t_ptr
    353 		 * to e_com without any CHECK_SIZE_COM().
    354 		 */
    355 		while (*t_ptr != '\0') {
    356 		    if (*t_ptr == ' ' || *t_ptr == '\t')
    357 			last_bl = e_com;
    358 		    *e_com++ = *t_ptr++;
    359 		}
    360 	    }
    361 	    break;
    362 	}
    363     }
    364 }
    365