pr_comment.c revision 1.14 1 /* $NetBSD: pr_comment.c,v 1.14 2021/03/08 20:15: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 #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.14 2021/03/08 20:15:42 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, char **last_bl_ptr)
64 {
65 if (e_com + (desired_size) >= l_com) {
66 int nsize = l_com - s_com + 400 + desired_size;
67 int com_len = e_com - s_com;
68 int blank_pos;
69 if (*last_bl_ptr != NULL)
70 blank_pos = *last_bl_ptr - combuf;
71 else
72 blank_pos = -1;
73 combuf = (char *)realloc(combuf, nsize);
74 if (combuf == NULL)
75 err(1, NULL);
76 e_com = combuf + com_len + 1;
77 if (blank_pos > 0)
78 *last_bl_ptr = combuf + blank_pos;
79 l_com = combuf + nsize - 5;
80 s_com = combuf + 1;
81 }
82 }
83
84 /*
85 * NAME:
86 * pr_comment
87 *
88 * FUNCTION:
89 * This routine takes care of scanning and printing comments.
90 *
91 * ALGORITHM:
92 * 1) Decide where the comment should be aligned, and if lines should
93 * be broken.
94 * 2) If lines should not be broken and filled, just copy up to end of
95 * comment.
96 * 3) If lines should be filled, then scan thru input_buffer copying
97 * characters to com_buf. Remember where the last blank, tab, or
98 * newline was. When line is filled, print up to last blank and
99 * continue copying.
100 *
101 * HISTORY:
102 * November 1976 D A Willcox of CAC Initial coding
103 * 12/6/76 D A Willcox of CAC Modification to handle
104 * UNIX-style comments
105 *
106 */
107
109 /*
110 * this routine processes comments. It makes an attempt to keep comments from
111 * going over the max line length. If a line is too long, it moves everything
112 * from the last blank to the next comment line. Blanks and tabs from the
113 * beginning of the input line are removed
114 */
115
116 void
117 pr_comment(void)
118 {
119 int now_col; /* column we are in now */
120 int adj_max_col; /* Adjusted max_col for when we decide to
121 * spill comments over the right margin */
122 char *last_bl; /* points to the last blank in the output
123 * buffer */
124 char *t_ptr; /* used for moving string */
125 int break_delim = opt.comment_delimiter_on_blankline;
126 int l_just_saw_decl = ps.just_saw_decl;
127
128 adj_max_col = opt.max_col;
129 ps.just_saw_decl = 0;
130 last_bl = NULL; /* no blanks found so far */
131 ps.box_com = false; /* at first, assume that we are not in
132 * a boxed comment or some other
133 * comment that should not be touched */
134 ++ps.out_coms; /* keep track of number of comments */
135
136 /* Figure where to align and how to treat the comment */
137
138 if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in column
139 * 1 it should not be touched */
140 ps.box_com = true;
141 break_delim = false;
142 ps.com_col = 1;
143 }
144 else {
145 if (*buf_ptr == '-' || *buf_ptr == '*' || e_token[-1] == '/' ||
146 (*buf_ptr == '\n' && !opt.format_block_comments)) {
147 ps.box_com = true; /* A comment with a '-' or '*' immediately
148 * after the /+* is assumed to be a boxed
149 * comment. A comment with a newline
150 * immediately after the /+* is assumed to
151 * be a block comment and is treated as a
152 * box comment unless format_block_comments
153 * is nonzero (the default). */
154 break_delim = false;
155 }
156 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
157 /* klg: check only if this line is blank */
158 /*
159 * If this (*and previous lines are*) blank, dont put comment way
160 * out at left
161 */
162 ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
163 adj_max_col = opt.block_comment_max_col;
164 if (ps.com_col <= 1)
165 ps.com_col = 1 + !opt.format_col1_comments;
166 }
167 else {
168 int target_col;
169 break_delim = false;
170 if (s_code != e_code)
171 target_col = count_spaces(compute_code_target(), s_code);
172 else {
173 target_col = 1;
174 if (s_lab != e_lab)
175 target_col = count_spaces(compute_label_target(), s_lab);
176 }
177 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
178 if (ps.com_col <= target_col)
179 ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
180 if (ps.com_col + 24 > adj_max_col)
181 adj_max_col = ps.com_col + 24;
182 }
183 }
184 if (ps.box_com) {
185 /*
186 * Find out how much indentation there was originally, because that
187 * much will have to be ignored by pad_output() in dump_line(). This
188 * is a box comment, so nothing changes -- not even indentation.
189 *
190 * The comment we're about to read usually comes from in_buffer,
191 * unless it has been copied into save_com.
192 */
193 char *start;
194
195 start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
196 sc_buf : in_buffer;
197 ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
198 }
199 else {
200 ps.n_comment_delta = 0;
201 while (*buf_ptr == ' ' || *buf_ptr == '\t')
202 buf_ptr++;
203 }
204 ps.comment_delta = 0;
205 *e_com++ = '/';
206 *e_com++ = e_token[-1];
207 if (*buf_ptr != ' ' && !ps.box_com)
208 *e_com++ = ' ';
209
210 /*
211 * Don't put a break delimiter if this is a one-liner that won't wrap.
212 */
213 if (break_delim)
214 for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
215 if (t_ptr >= buf_end)
216 fill_buffer();
217 if (t_ptr[0] == '*' && t_ptr[1] == '/') {
218 if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
219 break_delim = false;
220 break;
221 }
222 }
223
224 if (break_delim) {
225 char *t = e_com;
226 e_com = s_com + 2;
227 *e_com = 0;
228 if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
229 prefix_blankline_requested = 1;
230 dump_line();
231 e_com = s_com = t;
232 if (!ps.box_com && opt.star_comment_cont)
233 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
234 }
235
236 /* Start to copy the comment */
237
238 while (1) { /* this loop will go until the comment is
239 * copied */
240 switch (*buf_ptr) { /* this checks for various spcl cases */
241 case 014: /* check for a form feed */
242 check_size_comment(3, &last_bl);
243 if (!ps.box_com) { /* in a text comment, break the line here */
244 ps.use_ff = true;
245 /* fix so dump_line uses a form feed */
246 dump_line();
247 last_bl = NULL;
248 if (!ps.box_com && opt.star_comment_cont)
249 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
250 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
251 ;
252 }
253 else {
254 if (++buf_ptr >= buf_end)
255 fill_buffer();
256 *e_com++ = 014;
257 }
258 break;
259
260 case '\n':
261 if (e_token[-1] == '/') {
262 ++line_no;
263 goto end_of_comment;
264 }
265 if (had_eof) { /* check for unexpected eof */
266 printf("Unterminated comment\n");
267 dump_line();
268 return;
269 }
270 last_bl = NULL;
271 check_size_comment(4, &last_bl);
272 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
273 * we dont ignore the newline */
274 if (s_com == e_com)
275 *e_com++ = ' ';
276 if (!ps.box_com && e_com - s_com > 3) {
277 dump_line();
278 if (opt.star_comment_cont)
279 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
280 }
281 dump_line();
282 if (!ps.box_com && opt.star_comment_cont)
283 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
284 }
285 else {
286 ps.last_nl = 1;
287 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
288 last_bl = e_com - 1;
289 /*
290 * if there was a space at the end of the last line, remember
291 * where it was
292 */
293 else { /* otherwise, insert one */
294 last_bl = e_com;
295 *e_com++ = ' ';
296 }
297 }
298 ++line_no; /* keep track of input line number */
299 if (!ps.box_com) {
300 int nstar = 1;
301 do { /* flush any blanks and/or tabs at start of
302 * next line */
303 if (++buf_ptr >= buf_end)
304 fill_buffer();
305 if (*buf_ptr == '*' && --nstar >= 0) {
306 if (++buf_ptr >= buf_end)
307 fill_buffer();
308 if (*buf_ptr == '/')
309 goto end_of_comment;
310 }
311 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
312 }
313 else if (++buf_ptr >= buf_end)
314 fill_buffer();
315 break; /* end of case for newline */
316
317 case '*': /* must check for possibility of being at end
318 * of comment */
319 if (++buf_ptr >= buf_end) /* get to next char after * */
320 fill_buffer();
321 check_size_comment(4, &last_bl);
322 if (*buf_ptr == '/') { /* it is the end!!! */
323 end_of_comment:
324 if (++buf_ptr >= buf_end)
325 fill_buffer();
326 if (break_delim) {
327 if (e_com > s_com + 3) {
328 dump_line();
329 }
330 else
331 s_com = e_com;
332 *e_com++ = ' ';
333 }
334 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
335 *e_com++ = ' '; /* ensure blank before end */
336 if (e_token[-1] == '/')
337 *e_com++ = '\n', *e_com = '\0';
338 else
339 *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
340 ps.just_saw_decl = l_just_saw_decl;
341 return;
342 }
343 else /* handle isolated '*' */
344 *e_com++ = '*';
345 break;
346 default: /* we have a random char */
347 now_col = count_spaces_until(ps.com_col, s_com, e_com);
348 do {
349 check_size_comment(1, &last_bl);
350 *e_com = *buf_ptr++;
351 if (buf_ptr >= buf_end)
352 fill_buffer();
353 if (*e_com == ' ' || *e_com == '\t')
354 last_bl = e_com; /* remember we saw a blank */
355 ++e_com;
356 now_col++;
357 } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
358 (now_col <= adj_max_col || !last_bl));
359 ps.last_nl = false;
360 if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
361 /*
362 * the comment is too long, it must be broken up
363 */
364 if (last_bl == NULL) {
365 dump_line();
366 if (!ps.box_com && opt.star_comment_cont)
367 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
368 break;
369 }
370 *e_com = '\0';
371 e_com = last_bl;
372 dump_line();
373 if (!ps.box_com && opt.star_comment_cont)
374 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
375 for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
376 t_ptr++)
377 ;
378 last_bl = NULL;
379 /*
380 * t_ptr will be somewhere between e_com (dump_line() reset)
381 * and l_com. So it's safe to copy byte by byte from t_ptr
382 * to e_com without any check_size_comment().
383 */
384 while (*t_ptr != '\0') {
385 if (*t_ptr == ' ' || *t_ptr == '\t')
386 last_bl = e_com;
387 *e_com++ = *t_ptr++;
388 }
389 }
390 break;
391 }
392 }
393 }
394