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