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