1 /* 2 * Copyright (c) 1985 Sun Microsystems, Inc. 3 * Copyright (c) 1980 The Regents of the University of California. 4 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 /*static char sccsid[] = "from: @(#)pr_comment.c 5.12 (Berkeley) 2/26/91";*/ 38 static char rcsid[] = "$Id: pr_comment.c,v 1.3 1993/08/07 06:56:51 cgd Exp $"; 39 #endif /* not lint */ 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include "indent_globs.h" 44 45 /* 46 * NAME: 47 * pr_comment 48 * 49 * FUNCTION: 50 * This routine takes care of scanning and printing comments. 51 * 52 * ALGORITHM: 53 * 1) Decide where the comment should be aligned, and if lines should 54 * be broken. 55 * 2) If lines should not be broken and filled, just copy up to end of 56 * comment. 57 * 3) If lines should be filled, then scan thru input_buffer copying 58 * characters to com_buf. Remember where the last blank, tab, or 59 * newline was. When line is filled, print up to last blank and 60 * continue copying. 61 * 62 * HISTORY: 63 * November 1976 D A Willcox of CAC Initial coding 64 * 12/6/76 D A Willcox of CAC Modification to handle 65 * UNIX-style comments 66 * 67 */ 68 69 /* 70 * this routine processes comments. It makes an attempt to keep comments from 71 * going over the max line length. If a line is too long, it moves everything 72 * from the last blank to the next comment line. Blanks and tabs from the 73 * beginning of the input line are removed 74 */ 75 76 77 pr_comment() 78 { 79 int now_col; /* column we are in now */ 80 int adj_max_col; /* Adjusted max_col for when we decide to 81 * spill comments over the right margin */ 82 char *last_bl; /* points to the last blank in the output 83 * buffer */ 84 char *t_ptr; /* used for moving string */ 85 int unix_comment; /* tri-state variable used to decide if it is 86 * a unix-style comment. 0 means only blanks 87 * since /*, 1 means regular style comment, 2 88 * means unix style comment */ 89 int break_delim = comment_delimiter_on_blankline; 90 int l_just_saw_decl = ps.just_saw_decl; 91 /* 92 * int ps.last_nl = 0; /* true iff the last significant thing 93 * weve seen is a newline 94 */ 95 int one_liner = 1; /* true iff this comment is a one-liner */ 96 adj_max_col = max_col; 97 ps.just_saw_decl = 0; 98 last_bl = 0; /* no blanks found so far */ 99 ps.box_com = false; /* at first, assume that we are not in 100 * a boxed comment or some other 101 * comment that should not be touched */ 102 ++ps.out_coms; /* keep track of number of comments */ 103 unix_comment = 1; /* set flag to let us figure out if there is a 104 * unix-style comment ** DISABLED: use 0 to 105 * reenable this hack! */ 106 107 /* Figure where to align and how to treat the comment */ 108 109 if (ps.col_1 && !format_col1_comments) { /* if comment starts in column 110 * 1 it should not be touched */ 111 ps.box_com = true; 112 ps.com_col = 1; 113 } 114 else { 115 if (*buf_ptr == '-' || *buf_ptr == '*' || *buf_ptr == '\n') { 116 ps.box_com = true; /* a comment with a '-', '*' or newline 117 * immediately after the /* is assumed to be 118 * a boxed comment */ 119 break_delim = 0; 120 } 121 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { 122 /* klg: check only if this line is blank */ 123 /* 124 * If this (*and previous lines are*) blank, dont put comment way 125 * out at left 126 */ 127 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 128 adj_max_col = block_comment_max_col; 129 if (ps.com_col <= 1) 130 ps.com_col = 1 + !format_col1_comments; 131 } 132 else { 133 register target_col; 134 break_delim = 0; 135 if (s_code != e_code) 136 target_col = count_spaces(compute_code_target(), s_code); 137 else { 138 target_col = 1; 139 if (s_lab != e_lab) 140 target_col = count_spaces(compute_label_target(), s_lab); 141 } 142 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind; 143 if (ps.com_col < target_col) 144 ps.com_col = ((target_col + 7) & ~7) + 1; 145 if (ps.com_col + 24 > adj_max_col) 146 adj_max_col = ps.com_col + 24; 147 } 148 } 149 if (ps.box_com) { 150 buf_ptr[-2] = 0; 151 ps.n_comment_delta = 1 - count_spaces(1, in_buffer); 152 buf_ptr[-2] = '/'; 153 } 154 else { 155 ps.n_comment_delta = 0; 156 while (*buf_ptr == ' ' || *buf_ptr == '\t') 157 buf_ptr++; 158 } 159 ps.comment_delta = 0; 160 *e_com++ = '/'; /* put '/*' into buffer */ 161 *e_com++ = '*'; 162 if (*buf_ptr != ' ' && !ps.box_com) 163 *e_com++ = ' '; 164 165 *e_com = '\0'; 166 if (troff) { 167 now_col = 1; 168 adj_max_col = 80; 169 } 170 else 171 now_col = count_spaces(ps.com_col, s_com); /* figure what column we 172 * would be in if we 173 * printed the comment 174 * now */ 175 176 /* Start to copy the comment */ 177 178 while (1) { /* this loop will go until the comment is 179 * copied */ 180 if (*buf_ptr > 040 && *buf_ptr != '*') 181 ps.last_nl = 0; 182 CHECK_SIZE_COM; 183 switch (*buf_ptr) { /* this checks for various spcl cases */ 184 case 014: /* check for a form feed */ 185 if (!ps.box_com) { /* in a text comment, break the line here */ 186 ps.use_ff = true; 187 /* fix so dump_line uses a form feed */ 188 dump_line(); 189 last_bl = 0; 190 *e_com++ = ' '; 191 *e_com++ = '*'; 192 *e_com++ = ' '; 193 while (*++buf_ptr == ' ' || *buf_ptr == '\t'); 194 } 195 else { 196 if (++buf_ptr >= buf_end) 197 fill_buffer(); 198 *e_com++ = 014; 199 } 200 break; 201 202 case '\n': 203 if (had_eof) { /* check for unexpected eof */ 204 printf("Unterminated comment\n"); 205 *e_com = '\0'; 206 dump_line(); 207 return; 208 } 209 one_liner = 0; 210 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, 211 * we dont ignore the newline */ 212 if (s_com == e_com) { 213 *e_com++ = ' '; 214 *e_com++ = ' '; 215 } 216 *e_com = '\0'; 217 if (!ps.box_com && e_com - s_com > 3) { 218 if (break_delim == 1 && s_com[0] == '/' 219 && s_com[1] == '*' && s_com[2] == ' ') { 220 char *t = e_com; 221 break_delim = 2; 222 e_com = s_com + 2; 223 *e_com = 0; 224 if (blanklines_before_blockcomments) 225 prefix_blankline_requested = 1; 226 dump_line(); 227 e_com = t; 228 s_com[0] = s_com[1] = s_com[2] = ' '; 229 } 230 dump_line(); 231 CHECK_SIZE_COM; 232 *e_com++ = ' '; 233 *e_com++ = ' '; 234 } 235 dump_line(); 236 now_col = ps.com_col; 237 } 238 else { 239 ps.last_nl = 1; 240 if (unix_comment != 1) { /* we not are in unix_style 241 * comment */ 242 if (unix_comment == 0 && s_code == e_code) { 243 /* 244 * if it is a UNIX-style comment, ignore the 245 * requirement that previous line be blank for 246 * unindention 247 */ 248 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; 249 if (ps.com_col <= 1) 250 ps.com_col = 2; 251 } 252 unix_comment = 2; /* permanently remember that we are in 253 * this type of comment */ 254 dump_line(); 255 ++line_no; 256 now_col = ps.com_col; 257 *e_com++ = ' '; 258 /* 259 * fix so that the star at the start of the line will line 260 * up 261 */ 262 do /* flush leading white space */ 263 if (++buf_ptr >= buf_end) 264 fill_buffer(); 265 while (*buf_ptr == ' ' || *buf_ptr == '\t'); 266 break; 267 } 268 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') 269 last_bl = e_com - 1; 270 /* 271 * if there was a space at the end of the last line, remember 272 * where it was 273 */ 274 else { /* otherwise, insert one */ 275 last_bl = e_com; 276 CHECK_SIZE_COM; 277 *e_com++ = ' '; 278 ++now_col; 279 } 280 } 281 ++line_no; /* keep track of input line number */ 282 if (!ps.box_com) { 283 int nstar = 1; 284 do { /* flush any blanks and/or tabs at start of 285 * next line */ 286 if (++buf_ptr >= buf_end) 287 fill_buffer(); 288 if (*buf_ptr == '*' && --nstar >= 0) { 289 if (++buf_ptr >= buf_end) 290 fill_buffer(); 291 if (*buf_ptr == '/') 292 goto end_of_comment; 293 } 294 } while (*buf_ptr == ' ' || *buf_ptr == '\t'); 295 } 296 else if (++buf_ptr >= buf_end) 297 fill_buffer(); 298 break; /* end of case for newline */ 299 300 case '*': /* must check for possibility of being at end 301 * of comment */ 302 if (++buf_ptr >= buf_end) /* get to next char after * */ 303 fill_buffer(); 304 305 if (unix_comment == 0) /* set flag to show we are not in 306 * unix-style comment */ 307 unix_comment = 1; 308 309 if (*buf_ptr == '/') { /* it is the end!!! */ 310 end_of_comment: 311 if (++buf_ptr >= buf_end) 312 fill_buffer(); 313 314 if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before 315 * end */ 316 *e_com++ = ' '; 317 ++now_col; 318 } 319 if (break_delim == 1 && !one_liner && s_com[0] == '/' 320 && s_com[1] == '*' && s_com[2] == ' ') { 321 char *t = e_com; 322 break_delim = 2; 323 e_com = s_com + 2; 324 *e_com = 0; 325 if (blanklines_before_blockcomments) 326 prefix_blankline_requested = 1; 327 dump_line(); 328 e_com = t; 329 s_com[0] = s_com[1] = s_com[2] = ' '; 330 } 331 if (break_delim == 2 && e_com > s_com + 3 332 /* now_col > adj_max_col - 2 && !ps.box_com */ ) { 333 *e_com = '\0'; 334 dump_line(); 335 now_col = ps.com_col; 336 } 337 CHECK_SIZE_COM; 338 *e_com++ = '*'; 339 *e_com++ = '/'; 340 *e_com = '\0'; 341 ps.just_saw_decl = l_just_saw_decl; 342 return; 343 } 344 else { /* handle isolated '*' */ 345 *e_com++ = '*'; 346 ++now_col; 347 } 348 break; 349 default: /* we have a random char */ 350 if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') 351 unix_comment = 1; /* we are not in unix-style comment */ 352 353 *e_com = *buf_ptr++; 354 if (buf_ptr >= buf_end) 355 fill_buffer(); 356 357 if (*e_com == '\t') /* keep track of column */ 358 now_col = ((now_col - 1) & tabmask) + tabsize + 1; 359 else if (*e_com == '\b') /* this is a backspace */ 360 --now_col; 361 else 362 ++now_col; 363 364 if (*e_com == ' ' || *e_com == '\t') 365 last_bl = e_com; 366 /* remember we saw a blank */ 367 368 ++e_com; 369 if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') { 370 /* 371 * the comment is too long, it must be broken up 372 */ 373 if (break_delim == 1 && s_com[0] == '/' 374 && s_com[1] == '*' && s_com[2] == ' ') { 375 char *t = e_com; 376 break_delim = 2; 377 e_com = s_com + 2; 378 *e_com = 0; 379 if (blanklines_before_blockcomments) 380 prefix_blankline_requested = 1; 381 dump_line(); 382 e_com = t; 383 s_com[0] = s_com[1] = s_com[2] = ' '; 384 } 385 if (last_bl == 0) { /* we have seen no blanks */ 386 last_bl = e_com; /* fake it */ 387 *e_com++ = ' '; 388 } 389 *e_com = '\0'; /* print what we have */ 390 *last_bl = '\0'; 391 while (last_bl > s_com && last_bl[-1] < 040) 392 *--last_bl = 0; 393 e_com = last_bl; 394 dump_line(); 395 396 *e_com++ = ' '; /* add blanks for continuation */ 397 *e_com++ = ' '; 398 *e_com++ = ' '; 399 400 t_ptr = last_bl + 1; 401 last_bl = 0; 402 if (t_ptr >= e_com) { 403 while (*t_ptr == ' ' || *t_ptr == '\t') 404 t_ptr++; 405 while (*t_ptr != '\0') { /* move unprinted part of 406 * comment down in buffer */ 407 if (*t_ptr == ' ' || *t_ptr == '\t') 408 last_bl = e_com; 409 *e_com++ = *t_ptr++; 410 } 411 } 412 *e_com = '\0'; 413 now_col = count_spaces(ps.com_col, s_com); /* recompute current 414 * position */ 415 } 416 break; 417 } 418 } 419 } 420