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