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