pr_comment.c revision 1.45 1 /* $NetBSD: pr_comment.c,v 1.45 2021/09/25 20:23:42 rillig 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 static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: pr_comment.c,v 1.45 2021/09/25 20:23:42 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
49 #endif
50
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "indent.h"
57
58 static void
59 check_size_comment(size_t desired_size)
60 {
61 if (com.e + (desired_size) < com.l)
62 return;
63
64 size_t nsize = com.l - com.s + 400 + desired_size;
65 size_t com_len = com.e - com.s;
66 com.buf = xrealloc(com.buf, nsize);
67 com.s = com.buf + 1;
68 com.e = com.s + com_len;
69 com.l = com.buf + nsize - 5;
70 }
71
72 /*
73 * Scan, reformat and output a single comment, which is either a block comment
74 * starting with '/' '*' or an end-of-line comment starting with '//'.
75 *
76 * Try to keep comments from going over the maximum line length. If a line is
77 * too long, move everything starting from the last blank to the next comment
78 * line. Blanks and tabs from the beginning of the input line are removed.
79 *
80 * ALGORITHM:
81 * 1) Decide where the comment should be aligned, and if lines should
82 * be broken.
83 * 2) If lines should not be broken and filled, just copy up to end of
84 * comment.
85 * 3) If lines should be filled, then scan through the input buffer,
86 * copying characters to com_buf. Remember where the last blank,
87 * tab, or newline was. When line is filled, print up to last blank
88 * and continue copying.
89 */
90 void
91 process_comment(void)
92 {
93 int adj_max_line_length; /* Adjusted max_line_length for comments
94 * that spill over the right margin */
95 ssize_t last_blank; /* index of the last blank in com.buf */
96 char *t_ptr; /* used for moving string */
97 bool break_delim = opt.comment_delimiter_on_blankline;
98 int l_just_saw_decl = ps.just_saw_decl;
99
100 adj_max_line_length = opt.max_line_length;
101 ps.just_saw_decl = 0;
102 last_blank = -1; /* no blanks found so far */
103 ps.box_com = false; /* at first, assume that we are not in
104 * a boxed comment or some other
105 * comment that should not be touched */
106 ps.stats.comments++;
107
108 /* Figure where to align and how to treat the comment */
109
110 if (ps.col_1 && !opt.format_col1_comments) { /* if the comment starts in
111 * column 1, it should not be touched */
112 ps.box_com = true;
113 break_delim = false;
114 ps.com_col = 1;
115 } else {
116 if (*buf_ptr == '-' || *buf_ptr == '*' || token.e[-1] == '/' ||
117 (*buf_ptr == '\n' && !opt.format_block_comments)) {
118 ps.box_com = true; /* A comment with a '-' or '*' immediately
119 * after the /+* is assumed to be a boxed
120 * comment. A comment with a newline
121 * immediately after the /+* is assumed to
122 * be a block comment and is treated as a
123 * box comment unless format_block_comments
124 * is nonzero (the default). */
125 break_delim = false;
126 }
127 if (lab.s == lab.e && code.s == code.e) {
128 ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.indent_size + 1;
129 adj_max_line_length = opt.block_comment_max_line_length;
130 if (ps.com_col <= 1)
131 ps.com_col = 1 + (!opt.format_col1_comments ? 1 : 0);
132 } else {
133 break_delim = false;
134
135 int target_col;
136 if (code.s != code.e)
137 target_col = 1 + indentation_after(compute_code_indent(), code.s);
138 else if (lab.s != lab.e)
139 target_col = 1 + indentation_after(compute_label_indent(), lab.s);
140 else
141 target_col = 1;
142
143 ps.com_col = ps.decl_on_line || ps.ind_level == 0
144 ? opt.decl_comment_column : opt.comment_column;
145 if (ps.com_col <= target_col)
146 ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
147 if (ps.com_col + 24 > adj_max_line_length)
148 /* XXX: mismatch between column and length */
149 adj_max_line_length = ps.com_col + 24;
150 }
151 }
152 if (ps.box_com) {
153 /*
154 * Find out how much indentation there was originally, because that
155 * much will have to be ignored by pad_output() in dump_line(). This
156 * is a box comment, so nothing changes -- not even indentation.
157 *
158 * The comment we're about to read usually comes from in_buffer,
159 * unless it has been copied into save_com.
160 */
161 char *start;
162
163 /*
164 * XXX: ordered comparison between pointers from different objects
165 * invokes undefined behavior (C99 6.5.8).
166 */
167 start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
168 sc_buf : in_buffer;
169 ps.n_comment_delta = -indentation_after_range(0, start, buf_ptr - 2);
170 } else {
171 ps.n_comment_delta = 0;
172 while (*buf_ptr == ' ' || *buf_ptr == '\t')
173 buf_ptr++;
174 }
175 ps.comment_delta = 0;
176 *com.e++ = '/';
177 *com.e++ = token.e[-1];
178 if (*buf_ptr != ' ' && !ps.box_com)
179 *com.e++ = ' ';
180
181 /*
182 * Don't put a break delimiter if this is a one-liner that won't wrap.
183 */
184 if (break_delim)
185 for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
186 if (t_ptr >= buf_end)
187 fill_buffer();
188 if (t_ptr[0] == '*' && t_ptr[1] == '/') {
189 /*
190 * XXX: This computation ignores the leading " * ", as well
191 * as the trailing ' ' '*' '/'. In simple cases, these cancel
192 * out since they are equally long.
193 */
194 int right_margin = indentation_after_range(ps.com_col - 1,
195 buf_ptr, t_ptr + 2);
196 if (right_margin < adj_max_line_length)
197 break_delim = false;
198 break;
199 }
200 }
201
202 if (break_delim) {
203 char *t = com.e;
204 com.e = com.s + 2;
205 *com.e = 0;
206 if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
207 prefix_blankline_requested = true;
208 dump_line();
209 com.e = com.s = t;
210 if (!ps.box_com && opt.star_comment_cont)
211 *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
212 }
213
214 /* Start to copy the comment */
215
216 for (;;) { /* this loop will go until the comment is
217 * copied */
218 switch (*buf_ptr) { /* this checks for various special cases */
219 case 014: /* check for a form feed */
220 check_size_comment(3);
221 if (!ps.box_com) { /* in a text comment, break the line here */
222 ps.use_ff = true;
223 /* fix so dump_line uses a form feed */
224 dump_line();
225 last_blank = -1;
226 if (!ps.box_com && opt.star_comment_cont)
227 *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
228 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
229 ;
230 } else {
231 if (++buf_ptr >= buf_end)
232 fill_buffer();
233 *com.e++ = 014;
234 }
235 break;
236
237 case '\n':
238 if (token.e[-1] == '/') {
239 ++line_no;
240 goto end_of_comment;
241 }
242 if (had_eof) { /* check for unexpected eof */
243 printf("Unterminated comment\n");
244 dump_line();
245 return;
246 }
247 last_blank = -1;
248 check_size_comment(4);
249 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
250 * we dont ignore the newline */
251 if (com.s == com.e)
252 *com.e++ = ' ';
253 if (!ps.box_com && com.e - com.s > 3) {
254 dump_line();
255 if (opt.star_comment_cont)
256 *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
257 }
258 dump_line();
259 if (!ps.box_com && opt.star_comment_cont)
260 *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
261 } else {
262 ps.last_nl = true;
263 if (!(com.e[-1] == ' ' || com.e[-1] == '\t'))
264 *com.e++ = ' ';
265 last_blank = com.e - 1 - com.buf;
266 }
267 ++line_no; /* keep track of input line number */
268 if (!ps.box_com) {
269 int nstar = 1;
270 do { /* flush any blanks and/or tabs at start of
271 * next line */
272 if (++buf_ptr >= buf_end)
273 fill_buffer();
274 if (*buf_ptr == '*' && --nstar >= 0) {
275 if (++buf_ptr >= buf_end)
276 fill_buffer();
277 if (*buf_ptr == '/')
278 goto end_of_comment;
279 }
280 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
281 } else if (++buf_ptr >= buf_end)
282 fill_buffer();
283 break; /* end of case for newline */
284
285 case '*': /* must check for possibility of being at end
286 * of comment */
287 if (++buf_ptr >= buf_end) /* get to next char after * */
288 fill_buffer();
289 check_size_comment(4);
290 if (*buf_ptr == '/') { /* it is the end!!! */
291 end_of_comment:
292 if (++buf_ptr >= buf_end)
293 fill_buffer();
294 if (break_delim) {
295 if (com.e > com.s + 3)
296 dump_line();
297 else
298 com.s = com.e;
299 *com.e++ = ' ';
300 }
301 if (com.e[-1] != ' ' && com.e[-1] != '\t' && !ps.box_com)
302 *com.e++ = ' '; /* ensure blank before end */
303 if (token.e[-1] == '/')
304 *com.e++ = '\n', *com.e = '\0';
305 else
306 *com.e++ = '*', *com.e++ = '/', *com.e = '\0';
307 ps.just_saw_decl = l_just_saw_decl;
308 return;
309 } else /* handle isolated '*' */
310 *com.e++ = '*';
311 break;
312 default: /* we have a random char */
313 ;
314 int now_len = indentation_after_range(ps.com_col - 1, com.s, com.e);
315 do {
316 check_size_comment(1);
317 *com.e = *buf_ptr++;
318 if (buf_ptr >= buf_end)
319 fill_buffer();
320 if (*com.e == ' ' || *com.e == '\t')
321 last_blank = com.e - com.buf; /* remember we saw a blank */
322 ++com.e;
323 now_len++;
324 } while (memchr("*\n\r\b\t", *buf_ptr, 6) == NULL &&
325 (now_len < adj_max_line_length || last_blank == -1));
326 ps.last_nl = false;
327 /* XXX: signed character comparison '>' does not work for UTF-8 */
328 if (now_len > adj_max_line_length &&
329 !ps.box_com && com.e[-1] > ' ') {
330 /*
331 * the comment is too long, it must be broken up
332 */
333 if (last_blank == -1) {
334 dump_line();
335 if (!ps.box_com && opt.star_comment_cont)
336 *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
337 break;
338 }
339 *com.e = '\0';
340 com.e = com.buf + last_blank;
341 dump_line();
342 if (!ps.box_com && opt.star_comment_cont)
343 *com.e++ = ' ', *com.e++ = '*', *com.e++ = ' ';
344 for (t_ptr = com.buf + last_blank + 1;
345 *t_ptr == ' ' || *t_ptr == '\t'; t_ptr++)
346 continue;
347 last_blank = -1;
348 /*
349 * t_ptr will be somewhere between com.e (dump_line() reset)
350 * and com.l. So it's safe to copy byte by byte from t_ptr
351 * to com.e without any check_size_comment().
352 */
353 while (*t_ptr != '\0') {
354 if (*t_ptr == ' ' || *t_ptr == '\t')
355 last_blank = com.e - com.buf;
356 *com.e++ = *t_ptr++;
357 }
358 }
359 break;
360 }
361 }
362 }
363