xref: /csrg-svn/usr.bin/indent/pr_comment.c (revision 35500)
121972Sdist /*
2*35500Sbostic  * Copyright (c) 1985 Sun Microsystems, Inc.
3*35500Sbostic  * Copyright (c) 1980 The Regents of the University of California.
433767Sbostic  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
533767Sbostic  * All rights reserved.
633767Sbostic  *
733767Sbostic  * Redistribution and use in source and binary forms are permitted
834885Sbostic  * provided that the above copyright notice and this paragraph are
934885Sbostic  * duplicated in all such forms and that any documentation,
1034885Sbostic  * advertising materials, and other materials related to such
1134885Sbostic  * distribution and use acknowledge that the software was developed
12*35500Sbostic  * by the University of California, Berkeley, the University of Illinois,
13*35500Sbostic  * Urbana, and Sun Microsystems, Inc.  The name of either University
14*35500Sbostic  * or Sun Microsystems may not be used to endorse or promote products
15*35500Sbostic  * derived from this software without specific prior written permission.
1634885Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734885Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834885Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1921972Sdist  */
208806Smckusick 
2121972Sdist #ifndef lint
22*35500Sbostic static char sccsid[] = "@(#)pr_comment.c	5.7 (Berkeley) 09/15/88";
2333767Sbostic #endif /* not lint */
2421972Sdist 
2533767Sbostic /*
2624457Smckusick  * NAME:
2724457Smckusick  *	pr_comment
2824457Smckusick  *
2924457Smckusick  * FUNCTION:
3024457Smckusick  *	This routine takes care of scanning and printing comments.
3124457Smckusick  *
3224457Smckusick  * ALGORITHM:
3324457Smckusick  *	1) Decide where the comment should be aligned, and if lines should
3424457Smckusick  *	   be broken.
3524457Smckusick  *	2) If lines should not be broken and filled, just copy up to end of
3624457Smckusick  *	   comment.
3724457Smckusick  *	3) If lines should be filled, then scan thru input_buffer copying
3824457Smckusick  *	   characters to com_buf.  Remember where the last blank, tab, or
39*35500Sbostic  *	   newline was.  When line is filled, print up to last blank and
4024457Smckusick  *	   continue copying.
4124457Smckusick  *
4224457Smckusick  * HISTORY:
4324457Smckusick  *	November 1976	D A Willcox of CAC	Initial coding
44*35500Sbostic  *	12/6/76		D A Willcox of CAC	Modification to handle
4524457Smckusick  *						UNIX-style comments
4624457Smckusick  *
4724457Smckusick  */
4824457Smckusick 
498806Smckusick /*
50*35500Sbostic  * this routine processes comments.  It makes an attempt to keep comments from
51*35500Sbostic  * going over the max line length.  If a line is too long, it moves everything
52*35500Sbostic  * from the last blank to the next comment line.  Blanks and tabs from the
53*35500Sbostic  * beginning of the input line are removed
5424457Smckusick  */
558806Smckusick 
568806Smckusick 
57*35500Sbostic #include "indent_globs.h";
588806Smckusick 
59*35500Sbostic 
6024457Smckusick pr_comment()
6124457Smckusick {
6224457Smckusick     int         now_col;	/* column we are in now */
6324457Smckusick     int         adj_max_col;	/* Adjusted max_col for when we decide to
6424457Smckusick 				 * spill comments over the right margin */
6524457Smckusick     int         col_1_com;	/* this comment should not be touched */
6624457Smckusick     char       *last_bl;	/* points to the last blank in the output
6724457Smckusick 				 * buffer */
6824457Smckusick     char        achar;
6924457Smckusick     char       *t_ptr;		/* used for moving string */
70*35500Sbostic     int         unix_comment;	/* tri-state variable used to decide if it is
71*35500Sbostic 				 * a unix-style comment. 0 means only blanks
72*35500Sbostic 				 * since /*, 1 means regular style comment, 2
73*35500Sbostic 				 * means unix style comment */
7424457Smckusick     int         break_delim = comment_delimiter_on_blankline;
75*35500Sbostic     int         l_just_saw_decl = ps.just_saw_decl;
7624457Smckusick     /*
77*35500Sbostic      * int         ps.last_nl = 0;	/* true iff the last significant thing
78*35500Sbostic      * weve seen is a newline
7924457Smckusick      */
8024457Smckusick     int         one_liner = 1;	/* true iff this comment is a one-liner */
8124457Smckusick     adj_max_col = max_col;
8224457Smckusick     ps.just_saw_decl = 0;
8324457Smckusick     last_bl = 0;		/* no blanks found so far */
84*35500Sbostic     ps.box_com = col_1_com = false;	/* at first, assume that we are not in
85*35500Sbostic 					 * a boxed comment or some other
86*35500Sbostic 					 * comment that should not be touched */
8724457Smckusick     ++ps.out_coms;		/* keep track of number of comments */
88*35500Sbostic     unix_comment = 1;		/* set flag to let us figure out if there is a
89*35500Sbostic 				 * unix-style comment ** DISABLED: use 0 to
90*35500Sbostic 				 * reenable this hack! */
918806Smckusick 
9224457Smckusick     /* Figure where to align and how to treat the comment */
938806Smckusick 
94*35500Sbostic     if (ps.col_1 && !format_col1_comments) {	/* if comment starts in column
95*35500Sbostic 						 * 1 it should not be touched */
9624457Smckusick 	col_1_com = ps.box_com = true;
9724457Smckusick 	ps.com_col = 1;
98*35500Sbostic     }
99*35500Sbostic     else {
10024457Smckusick 	if (*buf_ptr == '-' || *buf_ptr == '*') {
10124457Smckusick 	    ps.box_com = true;	/* a comment with a '-' or '*' immediately
10224457Smckusick 				 * after the /* is assumed to be a boxed
10324457Smckusick 				 * comment */
10424457Smckusick 	    col_1_com = true;
10524457Smckusick 	    break_delim = 0;
1068806Smckusick 	}
10724457Smckusick 	if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
10824457Smckusick 	    /* klg: check only if this line is blank */
10924457Smckusick 	    /*
110*35500Sbostic 	     * If this (*and previous lines are*) blank, dont put comment way
111*35500Sbostic 	     * out at left
11224457Smckusick 	     */
11324457Smckusick 	    ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
11424457Smckusick 	    adj_max_col = block_comment_max_col;
11524457Smckusick 	    if (ps.com_col <= 1)
11624457Smckusick 		ps.com_col = 1 + !format_col1_comments;
117*35500Sbostic 	}
118*35500Sbostic 	else {
11924457Smckusick 	    register    target_col;
12024457Smckusick 	    break_delim = 0;
12124457Smckusick 	    if (s_code != e_code)
12224457Smckusick 		target_col = count_spaces(compute_code_target(), s_code);
12324457Smckusick 	    else {
12424457Smckusick 		target_col = 1;
12524457Smckusick 		if (s_lab != e_lab)
12624457Smckusick 		    target_col = count_spaces(compute_label_target(), s_lab);
12724457Smckusick 	    }
12824457Smckusick 	    ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
12924457Smckusick 	    if (ps.com_col < target_col)
13024457Smckusick 		ps.com_col = ((target_col + 7) & ~7) + 1;
13124457Smckusick 	    if (ps.com_col + 24 > adj_max_col)
13224457Smckusick 		adj_max_col = ps.com_col + 24;
1338806Smckusick 	}
1348806Smckusick     }
13524457Smckusick     if (ps.box_com) {
13624457Smckusick 	buf_ptr[-2] = 0;
13724457Smckusick 	ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
13824457Smckusick 	buf_ptr[-2] = '/';
139*35500Sbostic     }
140*35500Sbostic     else {
14124457Smckusick 	ps.n_comment_delta = 0;
14224457Smckusick 	while (*buf_ptr == ' ' || *buf_ptr == '\t')
14324457Smckusick 	    buf_ptr++;
14424457Smckusick     }
14524457Smckusick     ps.comment_delta = 0;
14624457Smckusick     *e_com++ = '/';		/* put '/*' into buffer */
1478806Smckusick     *e_com++ = '*';
14824457Smckusick     if (*buf_ptr != ' ' && !ps.box_com)
1498806Smckusick 	*e_com++ = ' ';
1508806Smckusick 
1518806Smckusick     *e_com = '\0';
152*35500Sbostic     if (troff) {
153*35500Sbostic 	now_col = 1;
154*35500Sbostic 	adj_max_col = 80;
155*35500Sbostic     }
156*35500Sbostic     else
157*35500Sbostic 	now_col = count_spaces(ps.com_col, s_com);	/* figure what column we
158*35500Sbostic 							 * would be in if we
159*35500Sbostic 							 * printed the comment
160*35500Sbostic 							 * now */
1618806Smckusick 
16224457Smckusick     /* Start to copy the comment */
1638806Smckusick 
16424457Smckusick     while (1) {			/* this loop will go until the comment is
16524457Smckusick 				 * copied */
16624457Smckusick 	if (*buf_ptr > 040 && *buf_ptr != '*')
16724457Smckusick 	    ps.last_nl = 0;
168*35500Sbostic 	check_size(com);
16924457Smckusick 	switch (*buf_ptr) {	/* this checks for various spcl cases */
170*35500Sbostic 	case 014:		/* check for a form feed */
171*35500Sbostic 	    if (!ps.box_com) {	/* in a text comment, break the line here */
172*35500Sbostic 		ps.use_ff = true;
173*35500Sbostic 		/* fix so dump_line uses a form feed */
174*35500Sbostic 		dump_line();
175*35500Sbostic 		last_bl = 0;
176*35500Sbostic 		*e_com++ = ' ';
177*35500Sbostic 		*e_com++ = '*';
178*35500Sbostic 		*e_com++ = ' ';
179*35500Sbostic 		while (*++buf_ptr == ' ' || *buf_ptr == '\t');
180*35500Sbostic 	    }
181*35500Sbostic 	    else {
182*35500Sbostic 		if (++buf_ptr >= buf_end)
183*35500Sbostic 		    fill_buffer();
184*35500Sbostic 		*e_com++ = 014;
185*35500Sbostic 	    }
186*35500Sbostic 	    break;
187*35500Sbostic 
188*35500Sbostic 	case '\n':
189*35500Sbostic 	    if (had_eof) {	/* check for unexpected eof */
190*35500Sbostic 		printf("Unterminated comment\n");
191*35500Sbostic 		*e_com = '\0';
192*35500Sbostic 		dump_line();
193*35500Sbostic 		return;
194*35500Sbostic 	    }
195*35500Sbostic 	    one_liner = 0;
196*35500Sbostic 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
197*35500Sbostic 						 * we dont ignore the newline */
198*35500Sbostic 		if (s_com == e_com) {
1998806Smckusick 		    *e_com++ = ' ';
2008806Smckusick 		    *e_com++ = ' ';
2018806Smckusick 		}
202*35500Sbostic 		*e_com = '\0';
203*35500Sbostic 		if (!ps.box_com && e_com - s_com > 3) {
204*35500Sbostic 		    if (break_delim == 1 && s_com[0] == '/'
20524457Smckusick 			    && s_com[1] == '*' && s_com[2] == ' ') {
206*35500Sbostic 			char       *t = e_com;
207*35500Sbostic 			break_delim = 2;
208*35500Sbostic 			e_com = s_com + 2;
209*35500Sbostic 			*e_com = 0;
210*35500Sbostic 			if (blanklines_before_blockcomments)
211*35500Sbostic 			    prefix_blankline_requested = 1;
21224457Smckusick 			dump_line();
213*35500Sbostic 			e_com = t;
214*35500Sbostic 			s_com[0] = s_com[1] = s_com[2] = ' ';
2158806Smckusick 		    }
21624457Smckusick 		    dump_line();
217*35500Sbostic 		    check_size(com);
218*35500Sbostic 		    *e_com++ = ' ';
219*35500Sbostic 		    *e_com++ = ' ';
220*35500Sbostic 		}
221*35500Sbostic 		dump_line();
222*35500Sbostic 		now_col = ps.com_col;
223*35500Sbostic 	    }
224*35500Sbostic 	    else {
22524457Smckusick 		ps.last_nl = 1;
226*35500Sbostic 		if (unix_comment != 1) {	/* we not are in unix_style
227*35500Sbostic 						 * comment */
2288806Smckusick 		    if (unix_comment == 0 && s_code == e_code) {
22924457Smckusick 			/*
23024457Smckusick 			 * if it is a UNIX-style comment, ignore the
23124457Smckusick 			 * requirement that previous line be blank for
232*35500Sbostic 			 * unindention
23324457Smckusick 			 */
23424457Smckusick 			ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
23524457Smckusick 			if (ps.com_col <= 1)
23624457Smckusick 			    ps.com_col = 2;
2378806Smckusick 		    }
238*35500Sbostic 		    unix_comment = 2;	/* permanently remember that we are in
239*35500Sbostic 					 * this type of comment */
24024457Smckusick 		    dump_line();
2418806Smckusick 		    ++line_no;
24224457Smckusick 		    now_col = ps.com_col;
2438806Smckusick 		    *e_com++ = ' ';
24424457Smckusick 		    /*
245*35500Sbostic 		     * fix so that the star at the start of the line will line
246*35500Sbostic 		     * up
24724457Smckusick 		     */
24824457Smckusick 		    do		/* flush leading white space */
2498806Smckusick 			if (++buf_ptr >= buf_end)
25024457Smckusick 			    fill_buffer();
2518806Smckusick 		    while (*buf_ptr == ' ' || *buf_ptr == '\t');
2528806Smckusick 		    break;
2538806Smckusick 		}
2548806Smckusick 		if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
2558806Smckusick 		    last_bl = e_com - 1;
25624457Smckusick 		/*
257*35500Sbostic 		 * if there was a space at the end of the last line, remember
258*35500Sbostic 		 * where it was
25924457Smckusick 		 */
26024457Smckusick 		else {		/* otherwise, insert one */
2618806Smckusick 		    last_bl = e_com;
262*35500Sbostic 		    check_size(com);
2638806Smckusick 		    *e_com++ = ' ';
2648806Smckusick 		    ++now_col;
2658806Smckusick 		}
266*35500Sbostic 	    }
267*35500Sbostic 	    ++line_no;		/* keep track of input line number */
268*35500Sbostic 	    if (!ps.box_com) {
269*35500Sbostic 		int         nstar = 1;
270*35500Sbostic 		do {		/* flush any blanks and/or tabs at start of
271*35500Sbostic 				 * next line */
272*35500Sbostic 		    if (++buf_ptr >= buf_end)
273*35500Sbostic 			fill_buffer();
274*35500Sbostic 		    if (*buf_ptr == '*' && --nstar >= 0) {
27524457Smckusick 			if (++buf_ptr >= buf_end)
27624457Smckusick 			    fill_buffer();
277*35500Sbostic 			if (*buf_ptr == '/')
278*35500Sbostic 			    goto end_of_comment;
279*35500Sbostic 		    }
280*35500Sbostic 		} while (*buf_ptr == ' ' || *buf_ptr == '\t');
281*35500Sbostic 	    }
282*35500Sbostic 	    else if (++buf_ptr >= buf_end)
283*35500Sbostic 		fill_buffer();
284*35500Sbostic 	    break;		/* end of case for newline */
2858806Smckusick 
286*35500Sbostic 	case '*':		/* must check for possibility of being at end
287*35500Sbostic 				 * of comment */
288*35500Sbostic 	    if (++buf_ptr >= buf_end)	/* get to next char after * */
289*35500Sbostic 		fill_buffer();
2908806Smckusick 
291*35500Sbostic 	    if (unix_comment == 0)	/* set flag to show we are not in
29224457Smckusick 					 * unix-style comment */
293*35500Sbostic 		unix_comment = 1;
2948806Smckusick 
295*35500Sbostic 	    if (*buf_ptr == '/') {	/* it is the end!!! */
296*35500Sbostic 	end_of_comment:
297*35500Sbostic 		if (++buf_ptr >= buf_end)
298*35500Sbostic 		    fill_buffer();
2998806Smckusick 
300*35500Sbostic 		if (*(e_com - 1) != ' ' && !ps.box_com) {	/* insure blank before
30124457Smckusick 								 * end */
302*35500Sbostic 		    *e_com++ = ' ';
303*35500Sbostic 		    ++now_col;
304*35500Sbostic 		}
305*35500Sbostic 		if (break_delim == 1 && !one_liner && s_com[0] == '/'
30624457Smckusick 			&& s_com[1] == '*' && s_com[2] == ' ') {
307*35500Sbostic 		    char       *t = e_com;
308*35500Sbostic 		    break_delim = 2;
309*35500Sbostic 		    e_com = s_com + 2;
310*35500Sbostic 		    *e_com = 0;
311*35500Sbostic 		    if (blanklines_before_blockcomments)
312*35500Sbostic 			prefix_blankline_requested = 1;
313*35500Sbostic 		    dump_line();
314*35500Sbostic 		    e_com = t;
315*35500Sbostic 		    s_com[0] = s_com[1] = s_com[2] = ' ';
316*35500Sbostic 		}
317*35500Sbostic 		if (break_delim == 2 && e_com > s_com + 3
31824457Smckusick 			 /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
3198806Smckusick 		    *e_com = '\0';
320*35500Sbostic 		    dump_line();
321*35500Sbostic 		    now_col = ps.com_col;
3228806Smckusick 		}
323*35500Sbostic 		check_size(com);
324*35500Sbostic 		*e_com++ = '*';
325*35500Sbostic 		*e_com++ = '/';
326*35500Sbostic 		*e_com = '\0';
327*35500Sbostic 		ps.just_saw_decl = l_just_saw_decl;
328*35500Sbostic 		return;
329*35500Sbostic 	    }
330*35500Sbostic 	    else {		/* handle isolated '*' */
331*35500Sbostic 		*e_com++ = '*';
332*35500Sbostic 		++now_col;
333*35500Sbostic 	    }
334*35500Sbostic 	    break;
335*35500Sbostic 	default:		/* we have a random char */
336*35500Sbostic 	    if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
337*35500Sbostic 		unix_comment = 1;	/* we are not in unix-style comment */
3388806Smckusick 
339*35500Sbostic 	    *e_com = *buf_ptr++;
340*35500Sbostic 	    if (buf_ptr >= buf_end)
341*35500Sbostic 		fill_buffer();
3428806Smckusick 
343*35500Sbostic 	    if (*e_com == '\t')	/* keep track of column */
344*35500Sbostic 		now_col = ((now_col - 1) & tabmask) + tabsize + 1;
345*35500Sbostic 	    else if (*e_com == '\b')	/* this is a backspace */
346*35500Sbostic 		--now_col;
347*35500Sbostic 	    else
348*35500Sbostic 		++now_col;
3498806Smckusick 
350*35500Sbostic 	    if (*e_com == ' ' || *e_com == '\t')
351*35500Sbostic 		last_bl = e_com;
352*35500Sbostic 	    /* remember we saw a blank */
3538806Smckusick 
354*35500Sbostic 	    ++e_com;
355*35500Sbostic 	    if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
356*35500Sbostic 		/*
357*35500Sbostic 		 * the comment is too long, it must be broken up
358*35500Sbostic 		 */
359*35500Sbostic 		if (break_delim == 1 && s_com[0] == '/'
36024457Smckusick 			&& s_com[1] == '*' && s_com[2] == ' ') {
361*35500Sbostic 		    char       *t = e_com;
362*35500Sbostic 		    break_delim = 2;
363*35500Sbostic 		    e_com = s_com + 2;
364*35500Sbostic 		    *e_com = 0;
365*35500Sbostic 		    if (blanklines_before_blockcomments)
366*35500Sbostic 			prefix_blankline_requested = 1;
36724457Smckusick 		    dump_line();
368*35500Sbostic 		    e_com = t;
369*35500Sbostic 		    s_com[0] = s_com[1] = s_com[2] = ' ';
370*35500Sbostic 		}
371*35500Sbostic 		if (last_bl == 0) {	/* we have seen no blanks */
372*35500Sbostic 		    last_bl = e_com;	/* fake it */
3738806Smckusick 		    *e_com++ = ' ';
374*35500Sbostic 		}
375*35500Sbostic 		*e_com = '\0';	/* print what we have */
376*35500Sbostic 		*last_bl = '\0';
377*35500Sbostic 		while (last_bl > s_com && last_bl[-1] < 040)
378*35500Sbostic 		    *--last_bl = 0;
379*35500Sbostic 		e_com = last_bl;
380*35500Sbostic 		dump_line();
3818806Smckusick 
382*35500Sbostic 		*e_com++ = ' ';	/* add blanks for continuation */
383*35500Sbostic 		*e_com++ = ' ';
384*35500Sbostic 		*e_com++ = ' ';
385*35500Sbostic 
386*35500Sbostic 		t_ptr = last_bl + 1;
387*35500Sbostic 		last_bl = 0;
388*35500Sbostic 		if (t_ptr >= e_com) {
389*35500Sbostic 		    while (*t_ptr == ' ' || *t_ptr == '\t')
390*35500Sbostic 			t_ptr++;
391*35500Sbostic 		    while (*t_ptr != '\0') {	/* move unprinted part of
392*35500Sbostic 						 * comment down in buffer */
393*35500Sbostic 			if (*t_ptr == ' ' || *t_ptr == '\t')
394*35500Sbostic 			    last_bl = e_com;
395*35500Sbostic 			*e_com++ = *t_ptr++;
3968806Smckusick 		    }
397*35500Sbostic 		}
398*35500Sbostic 		*e_com = '\0';
399*35500Sbostic 		now_col = count_spaces(ps.com_col, s_com);	/* recompute current
40024457Smckusick 								 * position */
401*35500Sbostic 	    }
402*35500Sbostic 	    break;
40324457Smckusick 	}
40424457Smckusick     }
40524457Smckusick }
406