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