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