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