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. 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.
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
40 static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
51 #include "indent_globs.h"
52 #include "indent_codes.h"
59 * This routine takes care of scanning and printing comments.
62 * 1) Decide where the comment should be aligned, and if lines should
64 * 2) If lines should not be broken and filled, just copy up to end of
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
72 * November 1976 D A Willcox of CAC Initial coding
73 * 12/6/76 D A Willcox of CAC Modification to handle
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
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
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;
97 adj_max_col = opt.max_col;
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 */
105 /* Figure where to align and how to treat the comment */
107 if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in column
108 * 1 it should not be touched */
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). */
125 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
126 /* klg: check only if this line is blank */
128 * If this (*and previous lines are*) blank, dont put comment way
131 ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
132 adj_max_col = opt.block_comment_max_col;
134 ps.com_col = 1 + !opt.format_col1_comments;
139 if (s_code != e_code)
140 target_col = count_spaces(compute_code_target(), s_code);
144 target_col = count_spaces(compute_label_target(), s_lab);
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;
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.
159 * The comment we're about to read usually comes from in_buffer,
160 * unless it has been copied into save_com.
164 start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
166 ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
169 ps.n_comment_delta = 0;
170 while (*buf_ptr == ' ' || *buf_ptr == '\t')
173 ps.comment_delta = 0;
174 *e_com++ = '/'; /* put '/' followed by '*' into buffer */
176 if (*buf_ptr != ' ' && !ps.box_com)
180 * Don't put a break delimiter if this is a one-liner that won't wrap.
183 for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
184 if (t_ptr >= buf_end)
186 if (t_ptr[0] == '*' && t_ptr[1] == '/') {
187 if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
197 if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
198 prefix_blankline_requested = 1;
201 if (!ps.box_com && opt.star_comment_cont)
202 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
205 /* Start to copy the comment */
207 while (1) { /* this loop will go until the comment is
209 switch (*buf_ptr) { /* this checks for various spcl cases */
210 case 014: /* check for a form feed */
212 if (!ps.box_com) { /* in a text comment, break the line here */
214 /* fix so dump_line uses a form feed */
217 if (!ps.box_com && opt.star_comment_cont)
218 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
219 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
223 if (++buf_ptr >= buf_end)
230 if (had_eof) { /* check for unexpected eof */
231 printf("Unterminated comment\n");
237 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
238 * we dont ignore the newline */
241 if (!ps.box_com && e_com - s_com > 3) {
243 if (opt.star_comment_cont)
244 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
247 if (!ps.box_com && opt.star_comment_cont)
248 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
252 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
255 * if there was a space at the end of the last line, remember
258 else { /* otherwise, insert one */
263 ++line_no; /* keep track of input line number */
266 do { /* flush any blanks and/or tabs at start of
268 if (++buf_ptr >= buf_end)
270 if (*buf_ptr == '*' && --nstar >= 0) {
271 if (++buf_ptr >= buf_end)
276 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
278 else if (++buf_ptr >= buf_end)
280 break; /* end of case for newline */
282 case '*': /* must check for possibility of being at end
284 if (++buf_ptr >= buf_end) /* get to next char after * */
287 if (*buf_ptr == '/') { /* it is the end!!! */
289 if (++buf_ptr >= buf_end)
292 if (e_com > s_com + 3) {
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;
305 else /* handle isolated '*' */
308 default: /* we have a random char */
309 now_col = count_spaces_until(ps.com_col, s_com, e_com);
313 if (buf_ptr >= buf_end)
315 if (*e_com == ' ' || *e_com == '\t')
316 last_bl = e_com; /* remember we saw a blank */
319 } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
320 (now_col <= adj_max_col || !last_bl));
322 if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
324 * the comment is too long, it must be broken up
326 if (last_bl == NULL) {
328 if (!ps.box_com && opt.star_comment_cont)
329 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
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';
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().
346 while (*t_ptr != '\0') {
347 if (*t_ptr == ' ' || *t_ptr == '\t')