| 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[] = "@(#)pr_comment.c 5.12 (Berkeley) 2/26/91"; |
| 38 | #endif /* not lint */ |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include "indent_globs.h" |
| 43 | |
| 44 | /* |
| 45 | * NAME: |
| 46 | * pr_comment |
| 47 | * |
| 48 | * FUNCTION: |
| 49 | * This routine takes care of scanning and printing comments. |
| 50 | * |
| 51 | * ALGORITHM: |
| 52 | * 1) Decide where the comment should be aligned, and if lines should |
| 53 | * be broken. |
| 54 | * 2) If lines should not be broken and filled, just copy up to end of |
| 55 | * comment. |
| 56 | * 3) If lines should be filled, then scan thru input_buffer copying |
| 57 | * characters to com_buf. Remember where the last blank, tab, or |
| 58 | * newline was. When line is filled, print up to last blank and |
| 59 | * continue copying. |
| 60 | * |
| 61 | * HISTORY: |
| 62 | * November 1976 D A Willcox of CAC Initial coding |
| 63 | * 12/6/76 D A Willcox of CAC Modification to handle |
| 64 | * UNIX-style comments |
| 65 | * |
| 66 | */\f |
| 67 | |
| 68 | /* |
| 69 | * this routine processes comments. It makes an attempt to keep comments from |
| 70 | * going over the max line length. If a line is too long, it moves everything |
| 71 | * from the last blank to the next comment line. Blanks and tabs from the |
| 72 | * beginning of the input line are removed |
| 73 | */ |
| 74 | |
| 75 | |
| 76 | pr_comment() |
| 77 | { |
| 78 | int now_col; /* column we are in now */ |
| 79 | int adj_max_col; /* Adjusted max_col for when we decide to |
| 80 | * spill comments over the right margin */ |
| 81 | char *last_bl; /* points to the last blank in the output |
| 82 | * buffer */ |
| 83 | char *t_ptr; /* used for moving string */ |
| 84 | int unix_comment; /* tri-state variable used to decide if it is |
| 85 | * a unix-style comment. 0 means only blanks |
| 86 | * since /*, 1 means regular style comment, 2 |
| 87 | * means unix style comment */ |
| 88 | int break_delim = comment_delimiter_on_blankline; |
| 89 | int l_just_saw_decl = ps.just_saw_decl; |
| 90 | /* |
| 91 | * int ps.last_nl = 0; /* true iff the last significant thing |
| 92 | * weve seen is a newline |
| 93 | */ |
| 94 | int one_liner = 1; /* true iff this comment is a one-liner */ |
| 95 | adj_max_col = max_col; |
| 96 | ps.just_saw_decl = 0; |
| 97 | last_bl = 0; /* 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 | unix_comment = 1; /* set flag to let us figure out if there is a |
| 103 | * unix-style comment ** DISABLED: use 0 to |
| 104 | * reenable this hack! */ |
| 105 | |
| 106 | /* Figure where to align and how to treat the comment */ |
| 107 | |
| 108 | if (ps.col_1 && !format_col1_comments) { /* if comment starts in column |
| 109 | * 1 it should not be touched */ |
| 110 | ps.box_com = true; |
| 111 | ps.com_col = 1; |
| 112 | } |
| 113 | else { |
| 114 | if (*buf_ptr == '-' || *buf_ptr == '*') { |
| 115 | ps.box_com = true; /* a comment with a '-' or '*' immediately |
| 116 | * after the /* is assumed to be a boxed |
| 117 | * comment */ |
| 118 | break_delim = 0; |
| 119 | } |
| 120 | if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { |
| 121 | /* klg: check only if this line is blank */ |
| 122 | /* |
| 123 | * If this (*and previous lines are*) blank, dont put comment way |
| 124 | * out at left |
| 125 | */ |
| 126 | ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; |
| 127 | adj_max_col = block_comment_max_col; |
| 128 | if (ps.com_col <= 1) |
| 129 | ps.com_col = 1 + !format_col1_comments; |
| 130 | } |
| 131 | else { |
| 132 | register target_col; |
| 133 | break_delim = 0; |
| 134 | if (s_code != e_code) |
| 135 | target_col = count_spaces(compute_code_target(), s_code); |
| 136 | else { |
| 137 | target_col = 1; |
| 138 | if (s_lab != e_lab) |
| 139 | target_col = count_spaces(compute_label_target(), s_lab); |
| 140 | } |
| 141 | ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind; |
| 142 | if (ps.com_col < target_col) |
| 143 | ps.com_col = ((target_col + 7) & ~7) + 1; |
| 144 | if (ps.com_col + 24 > adj_max_col) |
| 145 | adj_max_col = ps.com_col + 24; |
| 146 | } |
| 147 | } |
| 148 | if (ps.box_com) { |
| 149 | buf_ptr[-2] = 0; |
| 150 | ps.n_comment_delta = 1 - count_spaces(1, in_buffer); |
| 151 | buf_ptr[-2] = '/'; |
| 152 | } |
| 153 | else { |
| 154 | ps.n_comment_delta = 0; |
| 155 | while (*buf_ptr == ' ' || *buf_ptr == '\t') |
| 156 | buf_ptr++; |
| 157 | } |
| 158 | ps.comment_delta = 0; |
| 159 | *e_com++ = '/'; /* put '/*' into buffer */ |
| 160 | *e_com++ = '*'; |
| 161 | if (*buf_ptr != ' ' && !ps.box_com) |
| 162 | *e_com++ = ' '; |
| 163 | |
| 164 | *e_com = '\0'; |
| 165 | if (troff) { |
| 166 | now_col = 1; |
| 167 | adj_max_col = 80; |
| 168 | } |
| 169 | else |
| 170 | now_col = count_spaces(ps.com_col, s_com); /* figure what column we |
| 171 | * would be in if we |
| 172 | * printed the comment |
| 173 | * now */ |
| 174 | |
| 175 | /* Start to copy the comment */ |
| 176 | |
| 177 | while (1) { /* this loop will go until the comment is |
| 178 | * copied */ |
| 179 | if (*buf_ptr > 040 && *buf_ptr != '*') |
| 180 | ps.last_nl = 0; |
| 181 | CHECK_SIZE_COM; |
| 182 | switch (*buf_ptr) { /* this checks for various spcl cases */ |
| 183 | case 014: /* check for a form feed */ |
| 184 | if (!ps.box_com) { /* in a text comment, break the line here */ |
| 185 | ps.use_ff = true; |
| 186 | /* fix so dump_line uses a form feed */ |
| 187 | dump_line(); |
| 188 | last_bl = 0; |
| 189 | *e_com++ = ' '; |
| 190 | *e_com++ = '*'; |
| 191 | *e_com++ = ' '; |
| 192 | while (*++buf_ptr == ' ' || *buf_ptr == '\t'); |
| 193 | } |
| 194 | else { |
| 195 | if (++buf_ptr >= buf_end) |
| 196 | fill_buffer(); |
| 197 | *e_com++ = 014; |
| 198 | } |
| 199 | break; |
| 200 | |
| 201 | case '\n': |
| 202 | if (had_eof) { /* check for unexpected eof */ |
| 203 | printf("Unterminated comment\n"); |
| 204 | *e_com = '\0'; |
| 205 | dump_line(); |
| 206 | return; |
| 207 | } |
| 208 | one_liner = 0; |
| 209 | if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, |
| 210 | * we dont ignore the newline */ |
| 211 | if (s_com == e_com) { |
| 212 | *e_com++ = ' '; |
| 213 | *e_com++ = ' '; |
| 214 | } |
| 215 | *e_com = '\0'; |
| 216 | if (!ps.box_com && e_com - s_com > 3) { |
| 217 | if (break_delim == 1 && s_com[0] == '/' |
| 218 | && s_com[1] == '*' && s_com[2] == ' ') { |
| 219 | char *t = e_com; |
| 220 | break_delim = 2; |
| 221 | e_com = s_com + 2; |
| 222 | *e_com = 0; |
| 223 | if (blanklines_before_blockcomments) |
| 224 | prefix_blankline_requested = 1; |
| 225 | dump_line(); |
| 226 | e_com = t; |
| 227 | s_com[0] = s_com[1] = s_com[2] = ' '; |
| 228 | } |
| 229 | dump_line(); |
| 230 | CHECK_SIZE_COM; |
| 231 | *e_com++ = ' '; |
| 232 | *e_com++ = ' '; |
| 233 | } |
| 234 | dump_line(); |
| 235 | now_col = ps.com_col; |
| 236 | } |
| 237 | else { |
| 238 | ps.last_nl = 1; |
| 239 | if (unix_comment != 1) { /* we not are in unix_style |
| 240 | * comment */ |
| 241 | if (unix_comment == 0 && s_code == e_code) { |
| 242 | /* |
| 243 | * if it is a UNIX-style comment, ignore the |
| 244 | * requirement that previous line be blank for |
| 245 | * unindention |
| 246 | */ |
| 247 | ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; |
| 248 | if (ps.com_col <= 1) |
| 249 | ps.com_col = 2; |
| 250 | } |
| 251 | unix_comment = 2; /* permanently remember that we are in |
| 252 | * this type of comment */ |
| 253 | dump_line(); |
| 254 | ++line_no; |
| 255 | now_col = ps.com_col; |
| 256 | *e_com++ = ' '; |
| 257 | /* |
| 258 | * fix so that the star at the start of the line will line |
| 259 | * up |
| 260 | */ |
| 261 | do /* flush leading white space */ |
| 262 | if (++buf_ptr >= buf_end) |
| 263 | fill_buffer(); |
| 264 | while (*buf_ptr == ' ' || *buf_ptr == '\t'); |
| 265 | break; |
| 266 | } |
| 267 | if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') |
| 268 | last_bl = e_com - 1; |
| 269 | /* |
| 270 | * if there was a space at the end of the last line, remember |
| 271 | * where it was |
| 272 | */ |
| 273 | else { /* otherwise, insert one */ |
| 274 | last_bl = e_com; |
| 275 | CHECK_SIZE_COM; |
| 276 | *e_com++ = ' '; |
| 277 | ++now_col; |
| 278 | } |
| 279 | } |
| 280 | ++line_no; /* keep track of input line number */ |
| 281 | if (!ps.box_com) { |
| 282 | int nstar = 1; |
| 283 | do { /* flush any blanks and/or tabs at start of |
| 284 | * next line */ |
| 285 | if (++buf_ptr >= buf_end) |
| 286 | fill_buffer(); |
| 287 | if (*buf_ptr == '*' && --nstar >= 0) { |
| 288 | if (++buf_ptr >= buf_end) |
| 289 | fill_buffer(); |
| 290 | if (*buf_ptr == '/') |
| 291 | goto end_of_comment; |
| 292 | } |
| 293 | } while (*buf_ptr == ' ' || *buf_ptr == '\t'); |
| 294 | } |
| 295 | else if (++buf_ptr >= buf_end) |
| 296 | fill_buffer(); |
| 297 | break; /* end of case for newline */ |
| 298 | |
| 299 | case '*': /* must check for possibility of being at end |
| 300 | * of comment */ |
| 301 | if (++buf_ptr >= buf_end) /* get to next char after * */ |
| 302 | fill_buffer(); |
| 303 | |
| 304 | if (unix_comment == 0) /* set flag to show we are not in |
| 305 | * unix-style comment */ |
| 306 | unix_comment = 1; |
| 307 | |
| 308 | if (*buf_ptr == '/') { /* it is the end!!! */ |
| 309 | end_of_comment: |
| 310 | if (++buf_ptr >= buf_end) |
| 311 | fill_buffer(); |
| 312 | |
| 313 | if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before |
| 314 | * end */ |
| 315 | *e_com++ = ' '; |
| 316 | ++now_col; |
| 317 | } |
| 318 | if (break_delim == 1 && !one_liner && s_com[0] == '/' |
| 319 | && s_com[1] == '*' && s_com[2] == ' ') { |
| 320 | char *t = e_com; |
| 321 | break_delim = 2; |
| 322 | e_com = s_com + 2; |
| 323 | *e_com = 0; |
| 324 | if (blanklines_before_blockcomments) |
| 325 | prefix_blankline_requested = 1; |
| 326 | dump_line(); |
| 327 | e_com = t; |
| 328 | s_com[0] = s_com[1] = s_com[2] = ' '; |
| 329 | } |
| 330 | if (break_delim == 2 && e_com > s_com + 3 |
| 331 | /* now_col > adj_max_col - 2 && !ps.box_com */ ) { |
| 332 | *e_com = '\0'; |
| 333 | dump_line(); |
| 334 | now_col = ps.com_col; |
| 335 | } |
| 336 | CHECK_SIZE_COM; |
| 337 | *e_com++ = '*'; |
| 338 | *e_com++ = '/'; |
| 339 | *e_com = '\0'; |
| 340 | ps.just_saw_decl = l_just_saw_decl; |
| 341 | return; |
| 342 | } |
| 343 | else { /* handle isolated '*' */ |
| 344 | *e_com++ = '*'; |
| 345 | ++now_col; |
| 346 | } |
| 347 | break; |
| 348 | default: /* we have a random char */ |
| 349 | if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') |
| 350 | unix_comment = 1; /* we are not in unix-style comment */ |
| 351 | |
| 352 | *e_com = *buf_ptr++; |
| 353 | if (buf_ptr >= buf_end) |
| 354 | fill_buffer(); |
| 355 | |
| 356 | if (*e_com == '\t') /* keep track of column */ |
| 357 | now_col = ((now_col - 1) & tabmask) + tabsize + 1; |
| 358 | else if (*e_com == '\b') /* this is a backspace */ |
| 359 | --now_col; |
| 360 | else |
| 361 | ++now_col; |
| 362 | |
| 363 | if (*e_com == ' ' || *e_com == '\t') |
| 364 | last_bl = e_com; |
| 365 | /* remember we saw a blank */ |
| 366 | |
| 367 | ++e_com; |
| 368 | if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') { |
| 369 | /* |
| 370 | * the comment is too long, it must be broken up |
| 371 | */ |
| 372 | if (break_delim == 1 && s_com[0] == '/' |
| 373 | && s_com[1] == '*' && s_com[2] == ' ') { |
| 374 | char *t = e_com; |
| 375 | break_delim = 2; |
| 376 | e_com = s_com + 2; |
| 377 | *e_com = 0; |
| 378 | if (blanklines_before_blockcomments) |
| 379 | prefix_blankline_requested = 1; |
| 380 | dump_line(); |
| 381 | e_com = t; |
| 382 | s_com[0] = s_com[1] = s_com[2] = ' '; |
| 383 | } |
| 384 | if (last_bl == 0) { /* we have seen no blanks */ |
| 385 | last_bl = e_com; /* fake it */ |
| 386 | *e_com++ = ' '; |
| 387 | } |
| 388 | *e_com = '\0'; /* print what we have */ |
| 389 | *last_bl = '\0'; |
| 390 | while (last_bl > s_com && last_bl[-1] < 040) |
| 391 | *--last_bl = 0; |
| 392 | e_com = last_bl; |
| 393 | dump_line(); |
| 394 | |
| 395 | *e_com++ = ' '; /* add blanks for continuation */ |
| 396 | *e_com++ = ' '; |
| 397 | *e_com++ = ' '; |
| 398 | |
| 399 | t_ptr = last_bl + 1; |
| 400 | last_bl = 0; |
| 401 | if (t_ptr >= e_com) { |
| 402 | while (*t_ptr == ' ' || *t_ptr == '\t') |
| 403 | t_ptr++; |
| 404 | while (*t_ptr != '\0') { /* move unprinted part of |
| 405 | * comment down in buffer */ |
| 406 | if (*t_ptr == ' ' || *t_ptr == '\t') |
| 407 | last_bl = e_com; |
| 408 | *e_com++ = *t_ptr++; |
| 409 | } |
| 410 | } |
| 411 | *e_com = '\0'; |
| 412 | now_col = count_spaces(ps.com_col, s_com); /* recompute current |
| 413 | * position */ |
| 414 | } |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |