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