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