xref: /csrg-svn/usr.bin/indent/pr_comment.c (revision 33767)
121972Sdist /*
221972Sdist  * Copyright (c) 1980 Regents of the University of California.
3*33767Sbostic  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
4*33767Sbostic  * All rights reserved.
5*33767Sbostic  *
6*33767Sbostic  * Redistribution and use in source and binary forms are permitted
7*33767Sbostic  * provided that this notice is preserved and that due credit is given
8*33767Sbostic  * to the University of California at Berkeley and the University of
9*33767Sbostic  * Illinois at Urbana.  The name of either University may not be used
10*33767Sbostic  * to endorse or promote products derived from this software without
11*33767Sbostic  * specific prior written permission. This software is provided
12*33767Sbostic  * ``as is'' without express or implied warranty.
1321972Sdist  */
148806Smckusick 
1521972Sdist #ifndef lint
16*33767Sbostic static char sccsid[] = "@(#)pr_comment.c	5.5 (Berkeley) 03/22/88";
17*33767Sbostic #endif /* not lint */
1821972Sdist 
19*33767Sbostic /*
2024457Smckusick  * NAME:
2124457Smckusick  *	pr_comment
2224457Smckusick  *
2324457Smckusick  * FUNCTION:
2424457Smckusick  *	This routine takes care of scanning and printing comments.
2524457Smckusick  *
2624457Smckusick  * ALGORITHM:
2724457Smckusick  *	1) Decide where the comment should be aligned, and if lines should
2824457Smckusick  *	   be broken.
2924457Smckusick  *	2) If lines should not be broken and filled, just copy up to end of
3024457Smckusick  *	   comment.
3124457Smckusick  *	3) If lines should be filled, then scan thru input_buffer copying
3224457Smckusick  *	   characters to com_buf.  Remember where the last blank, tab, or
3324457Smckusick  *	   newline was.  When line is filled, print up to last blank and
3424457Smckusick  *	   continue copying.
3524457Smckusick  *
3624457Smckusick  * HISTORY:
3724457Smckusick  *	November 1976	D A Willcox of CAC	Initial coding
3824457Smckusick  *	12/6/76		D A Willcox of CAC	Modification to handle
3924457Smckusick  *						UNIX-style comments
4024457Smckusick  *
4124457Smckusick  */
4224457Smckusick 
438806Smckusick /*
4424457Smckusick  * this routine processes comments.  It makes an attempt to keep comments
4524457Smckusick  * from going over the max line length.  If a line is too long, it moves
4624457Smckusick  * everything from the last blank to the next comment line.  Blanks and
4724457Smckusick  * tabs from the beginning of the input line are removed
4824457Smckusick  */
498806Smckusick 
5033230Sbostic #include "indent_globs.h"
518806Smckusick 
528806Smckusick 
5324457Smckusick pr_comment()
5424457Smckusick {
5524457Smckusick     int         now_col;	/* column we are in now */
5624457Smckusick     int         adj_max_col;	/* Adjusted max_col for when we decide to
5724457Smckusick 				 * spill comments over the right margin */
5824457Smckusick     int         col_1_com;	/* this comment should not be touched */
5924457Smckusick     char       *last_bl;	/* points to the last blank in the output
6024457Smckusick 				 * buffer */
6124457Smckusick     char        achar;
6224457Smckusick     char       *t_ptr;		/* used for moving string */
6324457Smckusick     int         unix_comment;	/* tri-state variable used to decide if it
6424457Smckusick 				 * is a unix-style comment. 0 means only
6524457Smckusick 				 * blanks since /*, 1 means regular style
6624457Smckusick 				 * comment, 2 means unix style comment */
6724457Smckusick     int         break_delim = comment_delimiter_on_blankline;
6824457Smckusick     int		l_just_saw_decl = ps.just_saw_decl;
6924457Smckusick     /*
7024457Smckusick      * int         ps.last_nl = 0;	/* true iff the last significant
7124457Smckusick      * thing weve seen is a newline
7224457Smckusick      */
7324457Smckusick     int         one_liner = 1;	/* true iff this comment is a one-liner */
7424457Smckusick     adj_max_col = max_col;
7524457Smckusick     ps.just_saw_decl = 0;
7624457Smckusick     last_bl = 0;		/* no blanks found so far */
7724457Smckusick     ps.box_com = col_1_com = false;	/* at first, assume that we are
7824457Smckusick 					 * not in a boxed comment or some
7924457Smckusick 					 * other comment that should not
8024457Smckusick 					 * be touched */
8124457Smckusick     ++ps.out_coms;		/* keep track of number of comments */
8224457Smckusick     unix_comment = 1;		/* set flag to let us figure out if there
8324457Smckusick 				 * is a unix-style comment ** DISABLED:
8424457Smckusick 				 * use 0 to reenable this hack! */
858806Smckusick 
8624457Smckusick     /* Figure where to align and how to treat the comment */
878806Smckusick 
8824457Smckusick     if (ps.col_1 && !format_col1_comments) {	/* if comment starts in
8924457Smckusick 						 * column 1 it should not
9024457Smckusick 						 * be touched */
9124457Smckusick 	col_1_com = ps.box_com = true;
9224457Smckusick 	ps.com_col = 1;
9324457Smckusick     } else {
9424457Smckusick 	if (*buf_ptr == '-' || *buf_ptr == '*') {
9524457Smckusick 	    ps.box_com = true;	/* a comment with a '-' or '*' immediately
9624457Smckusick 				 * after the /* is assumed to be a boxed
9724457Smckusick 				 * comment */
9824457Smckusick 	    col_1_com = true;
9924457Smckusick 	    break_delim = 0;
1008806Smckusick 	}
10124457Smckusick 	if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
10224457Smckusick 	    /* klg: check only if this line is blank */
10324457Smckusick 	    /*
10424457Smckusick 	     * If this (*and previous lines are*) blank, dont put comment
10524457Smckusick 	     * way out at left
10624457Smckusick 	     */
10724457Smckusick 	    ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
10824457Smckusick 	    adj_max_col = block_comment_max_col;
10924457Smckusick 	    if (ps.com_col <= 1)
11024457Smckusick 		ps.com_col = 1 + !format_col1_comments;
11124457Smckusick 	} else {
11224457Smckusick 	    register    target_col;
11324457Smckusick 	    break_delim = 0;
11424457Smckusick 	    if (s_code != e_code)
11524457Smckusick 		target_col = count_spaces(compute_code_target(), s_code);
11624457Smckusick 	    else {
11724457Smckusick 		target_col = 1;
11824457Smckusick 		if (s_lab != e_lab)
11924457Smckusick 		    target_col = count_spaces(compute_label_target(), s_lab);
12024457Smckusick 	    }
12124457Smckusick 	    ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
12224457Smckusick 	    if (ps.com_col < target_col)
12324457Smckusick 		ps.com_col = ((target_col + 7) & ~7) + 1;
12424457Smckusick 	    if (ps.com_col + 24 > adj_max_col)
12524457Smckusick 		adj_max_col = ps.com_col + 24;
1268806Smckusick 	}
1278806Smckusick     }
12824457Smckusick     if (ps.box_com) {
12924457Smckusick 	buf_ptr[-2] = 0;
13024457Smckusick 	ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
13124457Smckusick 	ps.comment_delta = 0;
13224457Smckusick 	buf_ptr[-2] = '/';
13324457Smckusick     } else {
13424457Smckusick 	ps.n_comment_delta = 0;
13524457Smckusick 	ps.comment_delta = 0;
13624457Smckusick 	while (*buf_ptr == ' ' || *buf_ptr == '\t')
13724457Smckusick 	    buf_ptr++;
13824457Smckusick     }
13924457Smckusick     ps.comment_delta = 0;
14024457Smckusick     *e_com++ = '/';		/* put '/*' into buffer */
1418806Smckusick     *e_com++ = '*';
14224457Smckusick     if (*buf_ptr != ' ' && !ps.box_com)
1438806Smckusick 	*e_com++ = ' ';
1448806Smckusick 
1458806Smckusick     *e_com = '\0';
14624457Smckusick     now_col = count_spaces(ps.com_col, s_com);	/* figure what column we
14724457Smckusick 						 * would be in if we
14824457Smckusick 						 * printed the comment now */
1498806Smckusick 
15024457Smckusick     /* Start to copy the comment */
1518806Smckusick 
15224457Smckusick     while (1) {			/* this loop will go until the comment is
15324457Smckusick 				 * copied */
15424457Smckusick 	if (*buf_ptr > 040 && *buf_ptr != '*')
15524457Smckusick 	    ps.last_nl = 0;
15624457Smckusick 	switch (*buf_ptr) {	/* this checks for various spcl cases */
15724457Smckusick 	    case 014:		/* check for a form feed */
15824457Smckusick 		if (!ps.box_com) {	/* in a text comment, break the
15924457Smckusick 					 * line here */
16024457Smckusick 		    ps.use_ff = true;
16124457Smckusick 		    /* fix so dump_line uses a form feed */
16224457Smckusick 		    dump_line();
1638806Smckusick 		    last_bl = 0;
1648806Smckusick 		    *e_com++ = ' ';
16524457Smckusick 		    *e_com++ = '*';
1668806Smckusick 		    *e_com++ = ' ';
16724457Smckusick 		    while (*++buf_ptr == ' ' || *buf_ptr == '\t');
16824457Smckusick 		} else {
1698806Smckusick 		    if (++buf_ptr >= buf_end)
17024457Smckusick 			fill_buffer();
1718806Smckusick 		    *e_com++ = 014;
1728806Smckusick 		}
1738806Smckusick 		break;
1748806Smckusick 
17524457Smckusick 	    case '\n':
17624457Smckusick 		if (had_eof) {	/* check for unexpected eof */
17724457Smckusick 		    printf("Unterminated comment\n");
1788806Smckusick 		    *e_com = '\0';
17924457Smckusick 		    dump_line();
1808806Smckusick 		    return;
1818806Smckusick 		}
18224457Smckusick 		one_liner = 0;
18324457Smckusick 		if (ps.box_com || ps.last_nl) {	/* if this is a boxed
18424457Smckusick 						 * comment, we dont ignore
18524457Smckusick 						 * the newline */
18624457Smckusick 		    if (s_com == e_com) {
18724457Smckusick 			*e_com++ = ' ';
18824457Smckusick 			*e_com++ = ' ';
18924457Smckusick 		    }
1908806Smckusick 		    *e_com = '\0';
19124457Smckusick 		    if (!ps.box_com && e_com - s_com > 3) {
19224457Smckusick 			if (break_delim == 1 && s_com[0] == '/'
19324457Smckusick 			    && s_com[1] == '*' && s_com[2] == ' ') {
19424457Smckusick 			    char       *t = e_com;
19524457Smckusick 			    break_delim = 2;
19624457Smckusick 			    e_com = s_com + 2;
19724457Smckusick 			    *e_com = 0;
19824457Smckusick 			    if (blanklines_before_blockcomments) prefix_blankline_requested = 1;
19924457Smckusick 			    dump_line();
20024457Smckusick 			    e_com = t;
20124457Smckusick 			    s_com[0] = s_com[1] = s_com[2] = ' ';
20224457Smckusick 			}
20324457Smckusick 			dump_line();
20424457Smckusick 			*e_com++ = ' ';
20524457Smckusick 			*e_com++ = ' ';
2068806Smckusick 		    }
20724457Smckusick 		    dump_line();
20824457Smckusick 		    now_col = ps.com_col;
20924457Smckusick 		} else {
21024457Smckusick 		ps.last_nl = 1;
21124457Smckusick 		if (unix_comment != 1) {	/* we not are in
21224457Smckusick 						 * unix_style comment */
2138806Smckusick 		    if (unix_comment == 0 && s_code == e_code) {
21424457Smckusick 			/*
21524457Smckusick 			 * if it is a UNIX-style comment, ignore the
21624457Smckusick 			 * requirement that previous line be blank for
21724457Smckusick 			 * unindention
21824457Smckusick 			 */
21924457Smckusick 			ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
22024457Smckusick 			if (ps.com_col <= 1)
22124457Smckusick 			    ps.com_col = 2;
2228806Smckusick 		    }
22324457Smckusick 		    unix_comment = 2;	/* permanently remember that we
22424457Smckusick 					 * are in this type of comment */
22524457Smckusick 		    dump_line();
2268806Smckusick 		    ++line_no;
22724457Smckusick 		    now_col = ps.com_col;
2288806Smckusick 		    *e_com++ = ' ';
22924457Smckusick 		    /*
23024457Smckusick 		     * fix so that the star at the start of the line will
23124457Smckusick 		     * line up
23224457Smckusick 		     */
23324457Smckusick 		    do		/* flush leading white space */
2348806Smckusick 			if (++buf_ptr >= buf_end)
23524457Smckusick 			    fill_buffer();
2368806Smckusick 		    while (*buf_ptr == ' ' || *buf_ptr == '\t');
2378806Smckusick 		    break;
2388806Smckusick 		}
2398806Smckusick 		if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
2408806Smckusick 		    last_bl = e_com - 1;
24124457Smckusick 		/*
24224457Smckusick 		 * if there was a space at the end of the last line,
24324457Smckusick 		 * remember where it was
24424457Smckusick 		 */
24524457Smckusick 		else {		/* otherwise, insert one */
2468806Smckusick 		    last_bl = e_com;
2478806Smckusick 		    *e_com++ = ' ';
2488806Smckusick 		    ++now_col;
2498806Smckusick 		}
25024457Smckusick 		}
25124457Smckusick 		++line_no;	/* keep track of input line number */
25224457Smckusick 		if (!ps.box_com) {
25324457Smckusick 		    int         nstar = 1;
25424457Smckusick 		    do {	/* flush any blanks and/or tabs at start
25524457Smckusick 				 * of next line */
25624457Smckusick 			if (++buf_ptr >= buf_end)
25724457Smckusick 			    fill_buffer();
25824457Smckusick 			if (*buf_ptr == '*' && --nstar >= 0) {
25924457Smckusick 			    if (++buf_ptr >= buf_end)
26024457Smckusick 				fill_buffer();
26124457Smckusick 			    if (*buf_ptr == '/')
26224457Smckusick 				goto end_of_comment;
26324457Smckusick 			}
26424457Smckusick 		    } while (*buf_ptr == ' ' || *buf_ptr == '\t');
26524457Smckusick 		} else if (++buf_ptr >= buf_end) fill_buffer();
26624457Smckusick 		break;		/* end of case for newline */
2678806Smckusick 
26824457Smckusick 	    case '*':		/* must check for possibility of being at
26924457Smckusick 				 * end of comment */
27024457Smckusick 		if (++buf_ptr >= buf_end)	/* get to next char after * */
27124457Smckusick 		    fill_buffer();
2728806Smckusick 
27324457Smckusick 		if (unix_comment == 0)	/* set flag to show we are not in
27424457Smckusick 					 * unix-style comment */
2758806Smckusick 		    unix_comment = 1;
2768806Smckusick 
27724457Smckusick 		if (*buf_ptr == '/') {	/* it is the end!!! */
27824457Smckusick 	    end_of_comment:
2798806Smckusick 		    if (++buf_ptr >= buf_end)
28024457Smckusick 			fill_buffer();
2818806Smckusick 
28224457Smckusick 		    if (*(e_com - 1) != ' ' && !ps.box_com) {	/* insure blank before
28324457Smckusick 								 * end */
2848806Smckusick 			*e_com++ = ' ';
2858806Smckusick 			++now_col;
2868806Smckusick 		    }
28724457Smckusick 		    if (break_delim == 1 && !one_liner && s_com[0] == '/'
28824457Smckusick 			&& s_com[1] == '*' && s_com[2] == ' ') {
28924457Smckusick 			char       *t = e_com;
29024457Smckusick 			break_delim = 2;
29124457Smckusick 			e_com = s_com + 2;
29224457Smckusick 			*e_com = 0;
29324457Smckusick 			    if (blanklines_before_blockcomments) prefix_blankline_requested = 1;
29424457Smckusick 			dump_line();
29524457Smckusick 			e_com = t;
29624457Smckusick 			s_com[0] = s_com[1] = s_com[2] = ' ';
29724457Smckusick 		    }
29824457Smckusick 		    if (break_delim == 2 && e_com > s_com + 3
29924457Smckusick 			 /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
3008806Smckusick 			*e_com = '\0';
30124457Smckusick 			dump_line();
30224457Smckusick 			now_col = ps.com_col;
3038806Smckusick 		    }
3048806Smckusick 		    *e_com++ = '*';
3058806Smckusick 		    *e_com++ = '/';
3068806Smckusick 		    *e_com = '\0';
30724457Smckusick 		    ps.just_saw_decl = l_just_saw_decl;
30824457Smckusick 		    return;
30924457Smckusick 		} else {	/* handle isolated '*' */
3108806Smckusick 		    *e_com++ = '*';
3118806Smckusick 		    ++now_col;
3128806Smckusick 		}
31324457Smckusick 		break;
31424457Smckusick 	    default:		/* we have a random char */
3158806Smckusick 		if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
31624457Smckusick 		    unix_comment = 1;	/* we are not in unix-style
31724457Smckusick 					 * comment */
3188806Smckusick 
3198806Smckusick 		*e_com = *buf_ptr++;
3208806Smckusick 		if (buf_ptr >= buf_end)
32124457Smckusick 		    fill_buffer();
3228806Smckusick 
32324457Smckusick 		if (*e_com == '\t')	/* keep track of column */
3248806Smckusick 		    now_col = ((now_col - 1) & tabmask) + tabsize + 1;
32524457Smckusick 		else if (*e_com == '\b')	/* this is a backspace */
32624457Smckusick 		    --now_col;
3278806Smckusick 		else
32824457Smckusick 		    ++now_col;
3298806Smckusick 
3308806Smckusick 		if (*e_com == ' ' || *e_com == '\t')
3318806Smckusick 		    last_bl = e_com;
33224457Smckusick 		/* remember we saw a blank */
3338806Smckusick 
3348806Smckusick 		++e_com;
33524457Smckusick 		if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
33624457Smckusick 		    /* the comment is too long, it must be broken up */
33724457Smckusick 		    if (break_delim == 1 && s_com[0] == '/'
33824457Smckusick 			&& s_com[1] == '*' && s_com[2] == ' ') {
33924457Smckusick 			char       *t = e_com;
34024457Smckusick 			break_delim = 2;
34124457Smckusick 			e_com = s_com + 2;
34224457Smckusick 			*e_com = 0;
34324457Smckusick 			    if (blanklines_before_blockcomments) prefix_blankline_requested = 1;
34424457Smckusick 			dump_line();
34524457Smckusick 			e_com = t;
34624457Smckusick 			s_com[0] = s_com[1] = s_com[2] = ' ';
34724457Smckusick 		    }
34824457Smckusick 		    if (last_bl == 0) {	/* we have seen no blanks */
34924457Smckusick 			last_bl = e_com;	/* fake it */
3508806Smckusick 			*e_com++ = ' ';
3518806Smckusick 		    }
35224457Smckusick 		    *e_com = '\0';	/* print what we have */
3538806Smckusick 		    *last_bl = '\0';
35424457Smckusick 		    while (last_bl > s_com && last_bl[-1] < 040)
35524457Smckusick 			*--last_bl = 0;
3568806Smckusick 		    e_com = last_bl;
35724457Smckusick 		    dump_line();
3588806Smckusick 
35924457Smckusick 		    *e_com++ = ' ';	/* add blanks for continuation */
3608806Smckusick 		    *e_com++ = ' ';
3618806Smckusick 		    *e_com++ = ' ';
3628806Smckusick 
3638806Smckusick 		    t_ptr = last_bl + 1;
3648806Smckusick 		    last_bl = 0;
36524457Smckusick 		    if (t_ptr >= e_com) {
36624457Smckusick 			while (*t_ptr == ' ' || *t_ptr == '\t')
36724457Smckusick 			    t_ptr++;
36824457Smckusick 			while (*t_ptr != '\0') {	/* move unprinted part
36924457Smckusick 							 * of comment down in
37024457Smckusick 							 * buffer */
37124457Smckusick 			    if (*t_ptr == ' ' || *t_ptr == '\t')
37224457Smckusick 				last_bl = e_com;
37324457Smckusick 			    *e_com++ = *t_ptr++;
37424457Smckusick 			}
3758806Smckusick 		    }
3768806Smckusick 		    *e_com = '\0';
37724457Smckusick 		    now_col = count_spaces(ps.com_col, s_com);	/* recompute current
37824457Smckusick 								 * position */
37924457Smckusick 		}
38024457Smckusick 		break;
38124457Smckusick 	}
38224457Smckusick     }
38324457Smckusick }
384