Home | History | Annotate | Line # | Download | only in indent
pr_comment.c revision 1.1.1.2
      1 /*-
      2  * SPDX-License-Identifier: BSD-4-Clause
      3  *
      4  * Copyright (c) 1985 Sun Microsystems, Inc.
      5  * Copyright (c) 1980, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #if 0
     39 #ifndef lint
     40 static char sccsid[] = "@(#)pr_comment.c	8.1 (Berkeley) 6/6/93";
     41 #endif /* not lint */
     42 #endif
     43 
     44 #include <sys/cdefs.h>
     45 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
     46 
     47 #include <err.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include "indent_globs.h"
     52 #include "indent_codes.h"
     53 #include "indent.h"
     54 /*
     55  * NAME:
     56  *	pr_comment
     57  *
     58  * FUNCTION:
     59  *	This routine takes care of scanning and printing comments.
     60  *
     61  * ALGORITHM:
     62  *	1) Decide where the comment should be aligned, and if lines should
     63  *	   be broken.
     64  *	2) If lines should not be broken and filled, just copy up to end of
     65  *	   comment.
     66  *	3) If lines should be filled, then scan thru input_buffer copying
     67  *	   characters to com_buf.  Remember where the last blank, tab, or
     68  *	   newline was.  When line is filled, print up to last blank and
     69  *	   continue copying.
     70  *
     71  * HISTORY:
     72  *	November 1976	D A Willcox of CAC	Initial coding
     73  *	12/6/76		D A Willcox of CAC	Modification to handle
     74  *						UNIX-style comments
     75  *
     76  */
     77 
     79 /*
     80  * this routine processes comments.  It makes an attempt to keep comments from
     81  * going over the max line length.  If a line is too long, it moves everything
     82  * from the last blank to the next comment line.  Blanks and tabs from the
     83  * beginning of the input line are removed
     84  */
     85 
     86 void
     87 pr_comment(void)
     88 {
     89     int         now_col;	/* column we are in now */
     90     int         adj_max_col;	/* Adjusted max_col for when we decide to
     91 				 * spill comments over the right margin */
     92     char       *last_bl;	/* points to the last blank in the output
     93 				 * buffer */
     94     char       *t_ptr;		/* used for moving string */
     95     int         break_delim = opt.comment_delimiter_on_blankline;
     96     int         l_just_saw_decl = ps.just_saw_decl;
     97 
     98     adj_max_col = opt.max_col;
     99     ps.just_saw_decl = 0;
    100     last_bl = NULL;		/* no blanks found so far */
    101     ps.box_com = false;		/* at first, assume that we are not in
    102 					 * a boxed comment or some other
    103 					 * comment that should not be touched */
    104     ++ps.out_coms;		/* keep track of number of comments */
    105 
    106     /* Figure where to align and how to treat the comment */
    107 
    108     if (ps.col_1 && !opt.format_col1_comments) {	/* if comment starts in column
    109 						 * 1 it should not be touched */
    110 	ps.box_com = true;
    111 	break_delim = false;
    112 	ps.com_col = 1;
    113     }
    114     else {
    115 	if (*buf_ptr == '-' || *buf_ptr == '*' ||
    116 	    (*buf_ptr == '\n' && !opt.format_block_comments)) {
    117 	    ps.box_com = true;	/* A comment with a '-' or '*' immediately
    118 				 * after the /+* is assumed to be a boxed
    119 				 * comment. A comment with a newline
    120 				 * immediately after the /+* is assumed to
    121 				 * be a block comment and is treated as a
    122 				 * box comment unless format_block_comments
    123 				 * is nonzero (the default). */
    124 	    break_delim = false;
    125 	}
    126 	if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
    127 	    /* klg: check only if this line is blank */
    128 	    /*
    129 	     * If this (*and previous lines are*) blank, dont put comment way
    130 	     * out at left
    131 	     */
    132 	    ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
    133 	    adj_max_col = opt.block_comment_max_col;
    134 	    if (ps.com_col <= 1)
    135 		ps.com_col = 1 + !opt.format_col1_comments;
    136 	}
    137 	else {
    138 	    int target_col;
    139 	    break_delim = false;
    140 	    if (s_code != e_code)
    141 		target_col = count_spaces(compute_code_target(), s_code);
    142 	    else {
    143 		target_col = 1;
    144 		if (s_lab != e_lab)
    145 		    target_col = count_spaces(compute_label_target(), s_lab);
    146 	    }
    147 	    ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
    148 	    if (ps.com_col <= target_col)
    149 		ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
    150 	    if (ps.com_col + 24 > adj_max_col)
    151 		adj_max_col = ps.com_col + 24;
    152 	}
    153     }
    154     if (ps.box_com) {
    155 	/*
    156 	 * Find out how much indentation there was originally, because that
    157 	 * much will have to be ignored by pad_output() in dump_line(). This
    158 	 * is a box comment, so nothing changes -- not even indentation.
    159 	 *
    160 	 * The comment we're about to read usually comes from in_buffer,
    161 	 * unless it has been copied into save_com.
    162 	 */
    163 	char *start;
    164 
    165 	start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
    166 	    sc_buf : in_buffer;
    167 	ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
    168     }
    169     else {
    170 	ps.n_comment_delta = 0;
    171 	while (*buf_ptr == ' ' || *buf_ptr == '\t')
    172 	    buf_ptr++;
    173     }
    174     ps.comment_delta = 0;
    175     *e_com++ = '/';		/* put '/' followed by '*' into buffer */
    176     *e_com++ = '*';
    177     if (*buf_ptr != ' ' && !ps.box_com)
    178 	*e_com++ = ' ';
    179 
    180     /*
    181      * Don't put a break delimiter if this is a one-liner that won't wrap.
    182      */
    183     if (break_delim)
    184 	for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
    185 	    if (t_ptr >= buf_end)
    186 		fill_buffer();
    187 	    if (t_ptr[0] == '*' && t_ptr[1] == '/') {
    188 		if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
    189 		    break_delim = false;
    190 		break;
    191 	    }
    192 	}
    193 
    194     if (break_delim) {
    195 	char       *t = e_com;
    196 	e_com = s_com + 2;
    197 	*e_com = 0;
    198 	if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
    199 	    prefix_blankline_requested = 1;
    200 	dump_line();
    201 	e_com = s_com = t;
    202 	if (!ps.box_com && opt.star_comment_cont)
    203 	    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    204     }
    205 
    206     /* Start to copy the comment */
    207 
    208     while (1) {			/* this loop will go until the comment is
    209 				 * copied */
    210 	switch (*buf_ptr) {	/* this checks for various spcl cases */
    211 	case 014:		/* check for a form feed */
    212 	    CHECK_SIZE_COM(3);
    213 	    if (!ps.box_com) {	/* in a text comment, break the line here */
    214 		ps.use_ff = true;
    215 		/* fix so dump_line uses a form feed */
    216 		dump_line();
    217 		last_bl = NULL;
    218 		if (!ps.box_com && opt.star_comment_cont)
    219 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    220 		while (*++buf_ptr == ' ' || *buf_ptr == '\t')
    221 		    ;
    222 	    }
    223 	    else {
    224 		if (++buf_ptr >= buf_end)
    225 		    fill_buffer();
    226 		*e_com++ = 014;
    227 	    }
    228 	    break;
    229 
    230 	case '\n':
    231 	    if (had_eof) {	/* check for unexpected eof */
    232 		printf("Unterminated comment\n");
    233 		dump_line();
    234 		return;
    235 	    }
    236 	    last_bl = NULL;
    237 	    CHECK_SIZE_COM(4);
    238 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
    239 						 * we dont ignore the newline */
    240 		if (s_com == e_com)
    241 		    *e_com++ = ' ';
    242 		if (!ps.box_com && e_com - s_com > 3) {
    243 		    dump_line();
    244 		    if (opt.star_comment_cont)
    245 			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    246 		}
    247 		dump_line();
    248 		if (!ps.box_com && opt.star_comment_cont)
    249 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    250 	    }
    251 	    else {
    252 		ps.last_nl = 1;
    253 		if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
    254 		    last_bl = e_com - 1;
    255 		/*
    256 		 * if there was a space at the end of the last line, remember
    257 		 * where it was
    258 		 */
    259 		else {		/* otherwise, insert one */
    260 		    last_bl = e_com;
    261 		    *e_com++ = ' ';
    262 		}
    263 	    }
    264 	    ++line_no;		/* keep track of input line number */
    265 	    if (!ps.box_com) {
    266 		int         nstar = 1;
    267 		do {		/* flush any blanks and/or tabs at start of
    268 				 * next line */
    269 		    if (++buf_ptr >= buf_end)
    270 			fill_buffer();
    271 		    if (*buf_ptr == '*' && --nstar >= 0) {
    272 			if (++buf_ptr >= buf_end)
    273 			    fill_buffer();
    274 			if (*buf_ptr == '/')
    275 			    goto end_of_comment;
    276 		    }
    277 		} while (*buf_ptr == ' ' || *buf_ptr == '\t');
    278 	    }
    279 	    else if (++buf_ptr >= buf_end)
    280 		fill_buffer();
    281 	    break;		/* end of case for newline */
    282 
    283 	case '*':		/* must check for possibility of being at end
    284 				 * of comment */
    285 	    if (++buf_ptr >= buf_end)	/* get to next char after * */
    286 		fill_buffer();
    287 	    CHECK_SIZE_COM(4);
    288 	    if (*buf_ptr == '/') {	/* it is the end!!! */
    289 	end_of_comment:
    290 		if (++buf_ptr >= buf_end)
    291 		    fill_buffer();
    292 		if (break_delim) {
    293 		    if (e_com > s_com + 3) {
    294 			dump_line();
    295 		    }
    296 		    else
    297 			s_com = e_com;
    298 		    *e_com++ = ' ';
    299 		}
    300 		if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
    301 		    *e_com++ = ' ';	/* ensure blank before end */
    302 		*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
    303 		ps.just_saw_decl = l_just_saw_decl;
    304 		return;
    305 	    }
    306 	    else		/* handle isolated '*' */
    307 		*e_com++ = '*';
    308 	    break;
    309 	default:		/* we have a random char */
    310 	    now_col = count_spaces_until(ps.com_col, s_com, e_com);
    311 	    do {
    312 		CHECK_SIZE_COM(1);
    313 		*e_com = *buf_ptr++;
    314 		if (buf_ptr >= buf_end)
    315 		    fill_buffer();
    316 		if (*e_com == ' ' || *e_com == '\t')
    317 		    last_bl = e_com;	/* remember we saw a blank */
    318 		++e_com;
    319 		now_col++;
    320 	    } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
    321 		(now_col <= adj_max_col || !last_bl));
    322 	    ps.last_nl = false;
    323 	    if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
    324 		/*
    325 		 * the comment is too long, it must be broken up
    326 		 */
    327 		if (last_bl == NULL) {
    328 		    dump_line();
    329 		    if (!ps.box_com && opt.star_comment_cont)
    330 			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    331 		    break;
    332 		}
    333 		*e_com = '\0';
    334 		e_com = last_bl;
    335 		dump_line();
    336 		if (!ps.box_com && opt.star_comment_cont)
    337 		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
    338 		for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
    339 		    t_ptr++)
    340 			;
    341 		last_bl = NULL;
    342 		/*
    343 		 * t_ptr will be somewhere between e_com (dump_line() reset)
    344 		 * and l_com. So it's safe to copy byte by byte from t_ptr
    345 		 * to e_com without any CHECK_SIZE_COM().
    346 		 */
    347 		while (*t_ptr != '\0') {
    348 		    if (*t_ptr == ' ' || *t_ptr == '\t')
    349 			last_bl = e_com;
    350 		    *e_com++ = *t_ptr++;
    351 		}
    352 	    }
    353 	    break;
    354 	}
    355     }
    356 }
    357