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