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