2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1985 Sun Microsystems, Inc.
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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 $
41 #include "indent_globs.h"
42 #include "indent_codes.h"
49 * This routine takes care of scanning and printing comments.
52 * 1) Decide where the comment should be aligned, and if lines should
54 * 2) If lines should not be broken and filled, just copy up to end of
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
62 * November 1976 D A Willcox of CAC Initial coding
63 * 12/6/76 D A Willcox of CAC Modification to handle
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
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
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
;
87 adj_max_col
= opt
.max_col
;
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 */
95 /* Figure where to align and how to treat the comment */
97 if (ps
.col_1
&& !opt
.format_col1_comments
) { /* if comment starts in column
98 * 1 it should not be touched */
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). */
115 if ( /* ps.bl_line && */ (s_lab
== e_lab
) && (s_code
== e_code
)) {
116 /* klg: check only if this line is blank */
118 * If this (*and previous lines are*) blank, dont put comment way
121 ps
.com_col
= (ps
.ind_level
- opt
.unindent_displace
) * opt
.ind_size
+ 1;
122 adj_max_col
= opt
.block_comment_max_col
;
124 ps
.com_col
= 1 + !opt
.format_col1_comments
;
129 if (s_code
!= e_code
)
130 target_col
= count_spaces(compute_code_target(), s_code
);
134 target_col
= count_spaces(compute_label_target(), s_lab
);
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;
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.
149 * The comment we're about to read usually comes from in_buffer,
150 * unless it has been copied into save_com.
154 start
= buf_ptr
>= save_com
&& buf_ptr
< save_com
+ sc_size
?
156 ps
.n_comment_delta
= 1 - count_spaces_until(1, start
, buf_ptr
- 2);
159 ps
.n_comment_delta
= 0;
160 while (*buf_ptr
== ' ' || *buf_ptr
== '\t')
163 ps
.comment_delta
= 0;
164 *e_com
++ = '/'; /* put '/' followed by '*' into buffer */
166 if (*buf_ptr
!= ' ' && !ps
.box_com
)
170 * Don't put a break delimiter if this is a one-liner that won't wrap.
173 for (t_ptr
= buf_ptr
; *t_ptr
!= '\0' && *t_ptr
!= '\n'; t_ptr
++) {
174 if (t_ptr
>= buf_end
)
176 if (t_ptr
[0] == '*' && t_ptr
[1] == '/') {
177 if (adj_max_col
>= count_spaces_until(ps
.com_col
, buf_ptr
, t_ptr
+ 2))
187 if (opt
.blanklines_before_blockcomments
&& ps
.last_token
!= lbrace
)
188 prefix_blankline_requested
= 1;
191 if (!ps
.box_com
&& opt
.star_comment_cont
)
192 *e_com
++ = ' ', *e_com
++ = '*', *e_com
++ = ' ';
195 /* Start to copy the comment */
197 while (1) { /* this loop will go until the comment is
199 switch (*buf_ptr
) { /* this checks for various spcl cases */
200 case 014: /* check for a form feed */
202 if (!ps
.box_com
) { /* in a text comment, break the line here */
204 /* fix so dump_line uses a form feed */
207 if (!ps
.box_com
&& opt
.star_comment_cont
)
208 *e_com
++ = ' ', *e_com
++ = '*', *e_com
++ = ' ';
209 while (*++buf_ptr
== ' ' || *buf_ptr
== '\t')
213 if (++buf_ptr
>= buf_end
)
220 if (had_eof
) { /* check for unexpected eof */
221 printf("Unterminated comment\n");
227 if (ps
.box_com
|| ps
.last_nl
) { /* if this is a boxed comment,
228 * we dont ignore the newline */
231 if (!ps
.box_com
&& e_com
- s_com
> 3) {
233 if (opt
.star_comment_cont
)
234 *e_com
++ = ' ', *e_com
++ = '*', *e_com
++ = ' ';
237 if (!ps
.box_com
&& opt
.star_comment_cont
)
238 *e_com
++ = ' ', *e_com
++ = '*', *e_com
++ = ' ';
242 if (*(e_com
- 1) == ' ' || *(e_com
- 1) == '\t')
245 * if there was a space at the end of the last line, remember
248 else { /* otherwise, insert one */
253 ++line_no
; /* keep track of input line number */
256 do { /* flush any blanks and/or tabs at start of
258 if (++buf_ptr
>= buf_end
)
260 if (*buf_ptr
== '*' && --nstar
>= 0) {
261 if (++buf_ptr
>= buf_end
)
266 } while (*buf_ptr
== ' ' || *buf_ptr
== '\t');
268 else if (++buf_ptr
>= buf_end
)
270 break; /* end of case for newline */
272 case '*': /* must check for possibility of being at end
274 if (++buf_ptr
>= buf_end
) /* get to next char after * */
277 if (*buf_ptr
== '/') { /* it is the end!!! */
279 if (++buf_ptr
>= buf_end
)
282 if (e_com
> s_com
+ 3) {
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
;
295 else /* handle isolated '*' */
298 default: /* we have a random char */
299 now_col
= count_spaces_until(ps
.com_col
, s_com
, e_com
);
303 if (buf_ptr
>= buf_end
)
305 if (*e_com
== ' ' || *e_com
== '\t')
306 last_bl
= e_com
; /* remember we saw a blank */
309 } while (!memchr("*\n\r\b\t", *buf_ptr
, 6) &&
310 (now_col
<= adj_max_col
|| !last_bl
));
312 if (now_col
> adj_max_col
&& !ps
.box_com
&& e_com
[-1] > ' ') {
314 * the comment is too long, it must be broken up
316 if (last_bl
== NULL
) {
318 if (!ps
.box_com
&& opt
.star_comment_cont
)
319 *e_com
++ = ' ', *e_com
++ = '*', *e_com
++ = ' ';
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';
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().
336 while (*t_ptr
!= '\0') {
337 if (*t_ptr
== ' ' || *t_ptr
== '\t')