1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1985 Sun Microsystems, Inc. 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93"; 41 #endif /* not lint */ 42 #endif 43 44 #include <sys/cdefs.h> 45 #include <err.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include "indent_globs.h" 50 #include "indent_codes.h" 51 #include "indent.h" 52 /* 53 * NAME: 54 * pr_comment 55 * 56 * FUNCTION: 57 * This routine takes care of scanning and printing comments. 58 * 59 * ALGORITHM: 60 * 1) Decide where the comment should be aligned, and if lines should 61 * be broken. 62 * 2) If lines should not be broken and filled, just copy up to end of 63 * comment. 64 * 3) If lines should be filled, then scan thru input_buffer copying 65 * characters to com_buf. Remember where the last blank, tab, or 66 * newline was. When line is filled, print up to last blank and 67 * continue copying. 68 * 69 * HISTORY: 70 * November 1976 D A Willcox of CAC Initial coding 71 * 12/6/76 D A Willcox of CAC Modification to handle 72 * UNIX-style comments 73 * 74 */ 75 76 /* 77 * this routine processes comments. It makes an attempt to keep comments from 78 * going over the max line length. If a line is too long, it moves everything 79 * from the last blank to the next comment line. Blanks and tabs from the 80 * beginning of the input line are removed 81 */ 82 83 void 84 pr_comment(void) 85 { 86 int now_col; /* column we are in now */ 87 int adj_max_col; /* Adjusted max_col for when we decide to 88 * spill comments over the right margin */ 89 char *last_bl; /* points to the last blank in the output 90 * buffer */ 91 char *t_ptr; /* used for moving string */ 92 int break_delim = opt.comment_delimiter_on_blankline; 93 int l_just_saw_decl = ps.just_saw_decl; 94 95 adj_max_col = opt.max_col; 96 ps.just_saw_decl = 0; 97 last_bl = NULL; /* no blanks found so far */ 98 ps.box_com = false; /* at first, assume that we are not in 99 * a boxed comment or some other 100 * comment that should not be touched */ 101 ++ps.out_coms; /* keep track of number of comments */ 102 103 /* Figure where to align and how to treat the comment */ 104 105 if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in column 106 * 1 it should not be touched */ 107 ps.box_com = true; 108 break_delim = false; 109 ps.com_col = 1; 110 } 111 else { 112 if (*buf_ptr == '-' || *buf_ptr == '*' || 113 (*buf_ptr == '\n' && !opt.format_block_comments)) { 114 ps.box_com = true; /* A comment with a '-' or '*' immediately 115 * after the /+* is assumed to be a boxed 116 * comment. A comment with a newline 117 * immediately after the /+* is assumed to 118 * be a block comment and is treated as a 119 * box comment unless format_block_comments 120 * is nonzero (the default). */ 121 break_delim = false; 122 } 123 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 124 /* klg: check only if this line is blank */ 125 /* 126 * If this (*and previous lines are*) blank, dont put comment way 127 * out at left 128 */ 129 ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1; 130 adj_max_col = opt.block_comment_max_col; 131 if (ps.com_col <= 1) 132 ps.com_col = 1 + !opt.format_col1_comments; 133 } 134 else { 135 int target_col; 136 break_delim = false; 137 if (s_code != e_code) 138 target_col = count_spaces(compute_code_target(), s_code); 139 else { 140 target_col = 1; 141 if (s_lab != e_lab) 142 target_col = count_spaces(compute_label_target(), s_lab); 143 } 144 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind; 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_col) 148 adj_max_col = ps.com_col + 24; 149 } 150 } 151 if (ps.box_com) { 152 /* 153 * Find out how much indentation there was originally, because that 154 * much will have to be ignored by pad_output() in dump_line(). This 155 * is a box comment, so nothing changes -- not even indentation. 156 * 157 * The comment we're about to read usually comes from in_buffer, 158 * unless it has been copied into save_com. 159 */ 160 char *start; 161 162 start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? 163 sc_buf : in_buffer; 164 ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2); 165 } 166 else { 167 ps.n_comment_delta = 0; 168 while (*buf_ptr == ' ' || *buf_ptr == '\t') 169 buf_ptr++; 170 } 171 ps.comment_delta = 0; 172 *e_com++ = '/'; /* put '/' followed by '*' into buffer */ 173 *e_com++ = '*'; 174 if (*buf_ptr != ' ' && !ps.box_com) 175 *e_com++ = ' '; 176 177 /* 178 * Don't put a break delimiter if this is a one-liner that won't wrap. 179 */ 180 if (break_delim) 181 for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) { 182 if (t_ptr >= buf_end) 183 fill_buffer(); 184 if (t_ptr[0] == '*' && t_ptr[1] == '/') { 185 if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2)) 186 break_delim = false; 187 break; 188 } 189 } 190 191 if (break_delim) { 192 char *t = e_com; 193 e_com = s_com + 2; 194 *e_com = 0; 195 if (opt.blanklines_before_blockcomments && ps.last_token != lbrace) 196 prefix_blankline_requested = 1; 197 dump_line(); 198 e_com = s_com = t; 199 if (!ps.box_com && opt.star_comment_cont) 200 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 201 } 202 203 /* Start to copy the comment */ 204 205 while (1) { /* this loop will go until the comment is 206 * copied */ 207 switch (*buf_ptr) { /* this checks for various spcl cases */ 208 case 014: /* check for a form feed */ 209 CHECK_SIZE_COM(3); 210 if (!ps.box_com) { /* in a text comment, break the line here */ 211 ps.use_ff = true; 212 /* fix so dump_line uses a form feed */ 213 dump_line(); 214 last_bl = NULL; 215 if (!ps.box_com && opt.star_comment_cont) 216 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 217 while (*++buf_ptr == ' ' || *buf_ptr == '\t') 218 ; 219 } 220 else { 221 if (++buf_ptr >= buf_end) 222 fill_buffer(); 223 *e_com++ = 014; 224 } 225 break; 226 227 case '\n': 228 if (had_eof) { /* check for unexpected eof */ 229 printf("Unterminated comment\n"); 230 dump_line(); 231 return; 232 } 233 last_bl = NULL; 234 CHECK_SIZE_COM(4); 235 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, 236 * we dont ignore the newline */ 237 if (s_com == e_com) 238 *e_com++ = ' '; 239 if (!ps.box_com && e_com - s_com > 3) { 240 dump_line(); 241 if (opt.star_comment_cont) 242 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 243 } 244 dump_line(); 245 if (!ps.box_com && opt.star_comment_cont) 246 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 247 } 248 else { 249 ps.last_nl = 1; 250 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') 251 last_bl = e_com - 1; 252 /* 253 * if there was a space at the end of the last line, remember 254 * where it was 255 */ 256 else { /* otherwise, insert one */ 257 last_bl = e_com; 258 *e_com++ = ' '; 259 } 260 } 261 ++line_no; /* keep track of input line number */ 262 if (!ps.box_com) { 263 int nstar = 1; 264 do { /* flush any blanks and/or tabs at start of 265 * next line */ 266 if (++buf_ptr >= buf_end) 267 fill_buffer(); 268 if (*buf_ptr == '*' && --nstar >= 0) { 269 if (++buf_ptr >= buf_end) 270 fill_buffer(); 271 if (*buf_ptr == '/') 272 goto end_of_comment; 273 } 274 } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 275 } 276 else if (++buf_ptr >= buf_end) 277 fill_buffer(); 278 break; /* end of case for newline */ 279 280 case '*': /* must check for possibility of being at end 281 * of comment */ 282 if (++buf_ptr >= buf_end) /* get to next char after * */ 283 fill_buffer(); 284 CHECK_SIZE_COM(4); 285 if (*buf_ptr == '/') { /* it is the end!!! */ 286 end_of_comment: 287 if (++buf_ptr >= buf_end) 288 fill_buffer(); 289 if (break_delim) { 290 if (e_com > s_com + 3) { 291 dump_line(); 292 } 293 else 294 s_com = e_com; 295 *e_com++ = ' '; 296 } 297 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com) 298 *e_com++ = ' '; /* ensure blank before end */ 299 *e_com++ = '*', *e_com++ = '/', *e_com = '\0'; 300 ps.just_saw_decl = l_just_saw_decl; 301 return; 302 } 303 else /* handle isolated '*' */ 304 *e_com++ = '*'; 305 break; 306 default: /* we have a random char */ 307 now_col = count_spaces_until(ps.com_col, s_com, e_com); 308 do { 309 CHECK_SIZE_COM(1); 310 *e_com = *buf_ptr++; 311 if (buf_ptr >= buf_end) 312 fill_buffer(); 313 if (*e_com == ' ' || *e_com == '\t') 314 last_bl = e_com; /* remember we saw a blank */ 315 ++e_com; 316 now_col++; 317 } while (!memchr("*\n\r\b\t", *buf_ptr, 6) && 318 (now_col <= adj_max_col || !last_bl)); 319 ps.last_nl = false; 320 if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') { 321 /* 322 * the comment is too long, it must be broken up 323 */ 324 if (last_bl == NULL) { 325 dump_line(); 326 if (!ps.box_com && opt.star_comment_cont) 327 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 328 break; 329 } 330 *e_com = '\0'; 331 e_com = last_bl; 332 dump_line(); 333 if (!ps.box_com && opt.star_comment_cont) 334 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; 335 for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t'; 336 t_ptr++) 337 ; 338 last_bl = NULL; 339 /* 340 * t_ptr will be somewhere between e_com (dump_line() reset) 341 * and l_com. So it's safe to copy byte by byte from t_ptr 342 * to e_com without any CHECK_SIZE_COM(). 343 */ 344 while (*t_ptr != '\0') { 345 if (*t_ptr == ' ' || *t_ptr == '\t') 346 last_bl = e_com; 347 *e_com++ = *t_ptr++; 348 } 349 } 350 break; 351 } 352 } 353 } 354