xref: /csrg-svn/usr.bin/indent/pr_comment.c (revision 24457)
121972Sdist /*
221972Sdist  * Copyright (c) 1980 Regents of the University of California.
321972Sdist  * All rights reserved.  The Berkeley software License Agreement
421972Sdist  * specifies the terms and conditions for redistribution.
521972Sdist  */
68806Smckusick 
721972Sdist #ifndef lint
8*24457Smckusick static char sccsid[] = "@(#)pr_comment.c	5.2 (Berkeley) 08/28/85";
921972Sdist #endif not lint
1021972Sdist 
11*24457Smckusick /*-
12*24457Smckusick  *
13*24457Smckusick  *			  Copyright (C) 1976
14*24457Smckusick  *				by the
15*24457Smckusick  *			  Board of Trustees
16*24457Smckusick  *				of the
17*24457Smckusick  *			University of Illinois
18*24457Smckusick  *
19*24457Smckusick  *			 All rights reserved
20*24457Smckusick  *
21*24457Smckusick  *
22*24457Smckusick  * NAME:
23*24457Smckusick  *	pr_comment
24*24457Smckusick  *
25*24457Smckusick  * FUNCTION:
26*24457Smckusick  *	This routine takes care of scanning and printing comments.
27*24457Smckusick  *
28*24457Smckusick  * ALGORITHM:
29*24457Smckusick  *	1) Decide where the comment should be aligned, and if lines should
30*24457Smckusick  *	   be broken.
31*24457Smckusick  *	2) If lines should not be broken and filled, just copy up to end of
32*24457Smckusick  *	   comment.
33*24457Smckusick  *	3) If lines should be filled, then scan thru input_buffer copying
34*24457Smckusick  *	   characters to com_buf.  Remember where the last blank, tab, or
35*24457Smckusick  *	   newline was.  When line is filled, print up to last blank and
36*24457Smckusick  *	   continue copying.
37*24457Smckusick  *
38*24457Smckusick  * HISTORY:
39*24457Smckusick  *	November 1976	D A Willcox of CAC	Initial coding
40*24457Smckusick  *	12/6/76		D A Willcox of CAC	Modification to handle
41*24457Smckusick  *						UNIX-style comments
42*24457Smckusick  *
43*24457Smckusick  */
44*24457Smckusick 
458806Smckusick /*
46*24457Smckusick  * this routine processes comments.  It makes an attempt to keep comments
47*24457Smckusick  * from going over the max line length.  If a line is too long, it moves
48*24457Smckusick  * everything from the last blank to the next comment line.  Blanks and
49*24457Smckusick  * tabs from the beginning of the input line are removed
50*24457Smckusick  */
518806Smckusick 
528806Smckusick #include "indent_globs.h";
538806Smckusick 
548806Smckusick 
55*24457Smckusick pr_comment()
56*24457Smckusick {
57*24457Smckusick     int         now_col;	/* column we are in now */
58*24457Smckusick     int         adj_max_col;	/* Adjusted max_col for when we decide to
59*24457Smckusick 				 * spill comments over the right margin */
60*24457Smckusick     int         col_1_com;	/* this comment should not be touched */
61*24457Smckusick     char       *last_bl;	/* points to the last blank in the output
62*24457Smckusick 				 * buffer */
63*24457Smckusick     char        achar;
64*24457Smckusick     char       *t_ptr;		/* used for moving string */
65*24457Smckusick     int         unix_comment;	/* tri-state variable used to decide if it
66*24457Smckusick 				 * is a unix-style comment. 0 means only
67*24457Smckusick 				 * blanks since /*, 1 means regular style
68*24457Smckusick 				 * comment, 2 means unix style comment */
69*24457Smckusick     int         break_delim = comment_delimiter_on_blankline;
70*24457Smckusick     int		l_just_saw_decl = ps.just_saw_decl;
71*24457Smckusick     /*
72*24457Smckusick      * int         ps.last_nl = 0;	/* true iff the last significant
73*24457Smckusick      * thing weve seen is a newline
74*24457Smckusick      */
75*24457Smckusick     int         one_liner = 1;	/* true iff this comment is a one-liner */
76*24457Smckusick     adj_max_col = max_col;
77*24457Smckusick     ps.just_saw_decl = 0;
78*24457Smckusick     last_bl = 0;		/* no blanks found so far */
79*24457Smckusick     ps.box_com = col_1_com = false;	/* at first, assume that we are
80*24457Smckusick 					 * not in a boxed comment or some
81*24457Smckusick 					 * other comment that should not
82*24457Smckusick 					 * be touched */
83*24457Smckusick     ++ps.out_coms;		/* keep track of number of comments */
84*24457Smckusick     unix_comment = 1;		/* set flag to let us figure out if there
85*24457Smckusick 				 * is a unix-style comment ** DISABLED:
86*24457Smckusick 				 * use 0 to reenable this hack! */
878806Smckusick 
88*24457Smckusick     /* Figure where to align and how to treat the comment */
898806Smckusick 
90*24457Smckusick     if (ps.col_1 && !format_col1_comments) {	/* if comment starts in
91*24457Smckusick 						 * column 1 it should not
92*24457Smckusick 						 * be touched */
93*24457Smckusick 	col_1_com = ps.box_com = true;
94*24457Smckusick 	ps.com_col = 1;
95*24457Smckusick     } else {
96*24457Smckusick 	if (*buf_ptr == '-' || *buf_ptr == '*') {
97*24457Smckusick 	    ps.box_com = true;	/* a comment with a '-' or '*' immediately
98*24457Smckusick 				 * after the /* is assumed to be a boxed
99*24457Smckusick 				 * comment */
100*24457Smckusick 	    col_1_com = true;
101*24457Smckusick 	    break_delim = 0;
1028806Smckusick 	}
103*24457Smckusick 	if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
104*24457Smckusick 	    /* klg: check only if this line is blank */
105*24457Smckusick 	    /*
106*24457Smckusick 	     * If this (*and previous lines are*) blank, dont put comment
107*24457Smckusick 	     * way out at left
108*24457Smckusick 	     */
109*24457Smckusick 	    ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
110*24457Smckusick 	    adj_max_col = block_comment_max_col;
111*24457Smckusick 	    if (ps.com_col <= 1)
112*24457Smckusick 		ps.com_col = 1 + !format_col1_comments;
113*24457Smckusick 	} else {
114*24457Smckusick 	    register    target_col;
115*24457Smckusick 	    break_delim = 0;
116*24457Smckusick 	    if (s_code != e_code)
117*24457Smckusick 		target_col = count_spaces(compute_code_target(), s_code);
118*24457Smckusick 	    else {
119*24457Smckusick 		target_col = 1;
120*24457Smckusick 		if (s_lab != e_lab)
121*24457Smckusick 		    target_col = count_spaces(compute_label_target(), s_lab);
122*24457Smckusick 	    }
123*24457Smckusick 	    ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
124*24457Smckusick 	    if (ps.com_col < target_col)
125*24457Smckusick 		ps.com_col = ((target_col + 7) & ~7) + 1;
126*24457Smckusick 	    if (ps.com_col + 24 > adj_max_col)
127*24457Smckusick 		adj_max_col = ps.com_col + 24;
1288806Smckusick 	}
1298806Smckusick     }
130*24457Smckusick     if (ps.box_com) {
131*24457Smckusick 	buf_ptr[-2] = 0;
132*24457Smckusick 	ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
133*24457Smckusick 	ps.comment_delta = 0;
134*24457Smckusick 	buf_ptr[-2] = '/';
135*24457Smckusick     } else {
136*24457Smckusick 	ps.n_comment_delta = 0;
137*24457Smckusick 	ps.comment_delta = 0;
138*24457Smckusick 	while (*buf_ptr == ' ' || *buf_ptr == '\t')
139*24457Smckusick 	    buf_ptr++;
140*24457Smckusick     }
141*24457Smckusick     ps.comment_delta = 0;
142*24457Smckusick     *e_com++ = '/';		/* put '/*' into buffer */
1438806Smckusick     *e_com++ = '*';
144*24457Smckusick     if (*buf_ptr != ' ' && !ps.box_com)
1458806Smckusick 	*e_com++ = ' ';
1468806Smckusick 
1478806Smckusick     *e_com = '\0';
148*24457Smckusick     now_col = count_spaces(ps.com_col, s_com);	/* figure what column we
149*24457Smckusick 						 * would be in if we
150*24457Smckusick 						 * printed the comment now */
1518806Smckusick 
152*24457Smckusick     /* Start to copy the comment */
1538806Smckusick 
154*24457Smckusick     while (1) {			/* this loop will go until the comment is
155*24457Smckusick 				 * copied */
156*24457Smckusick 	if (*buf_ptr > 040 && *buf_ptr != '*')
157*24457Smckusick 	    ps.last_nl = 0;
158*24457Smckusick 	switch (*buf_ptr) {	/* this checks for various spcl cases */
159*24457Smckusick 	    case 014:		/* check for a form feed */
160*24457Smckusick 		if (!ps.box_com) {	/* in a text comment, break the
161*24457Smckusick 					 * line here */
162*24457Smckusick 		    ps.use_ff = true;
163*24457Smckusick 		    /* fix so dump_line uses a form feed */
164*24457Smckusick 		    dump_line();
1658806Smckusick 		    last_bl = 0;
1668806Smckusick 		    *e_com++ = ' ';
167*24457Smckusick 		    *e_com++ = '*';
1688806Smckusick 		    *e_com++ = ' ';
169*24457Smckusick 		    while (*++buf_ptr == ' ' || *buf_ptr == '\t');
170*24457Smckusick 		} else {
1718806Smckusick 		    if (++buf_ptr >= buf_end)
172*24457Smckusick 			fill_buffer();
1738806Smckusick 		    *e_com++ = 014;
1748806Smckusick 		}
1758806Smckusick 		break;
1768806Smckusick 
177*24457Smckusick 	    case '\n':
178*24457Smckusick 		if (had_eof) {	/* check for unexpected eof */
179*24457Smckusick 		    printf("Unterminated comment\n");
1808806Smckusick 		    *e_com = '\0';
181*24457Smckusick 		    dump_line();
1828806Smckusick 		    return;
1838806Smckusick 		}
184*24457Smckusick 		one_liner = 0;
185*24457Smckusick 		if (ps.box_com || ps.last_nl) {	/* if this is a boxed
186*24457Smckusick 						 * comment, we dont ignore
187*24457Smckusick 						 * the newline */
188*24457Smckusick 		    if (s_com == e_com) {
189*24457Smckusick 			*e_com++ = ' ';
190*24457Smckusick 			*e_com++ = ' ';
191*24457Smckusick 		    }
1928806Smckusick 		    *e_com = '\0';
193*24457Smckusick 		    if (!ps.box_com && e_com - s_com > 3) {
194*24457Smckusick 			if (break_delim == 1 && s_com[0] == '/'
195*24457Smckusick 			    && s_com[1] == '*' && s_com[2] == ' ') {
196*24457Smckusick 			    char       *t = e_com;
197*24457Smckusick 			    break_delim = 2;
198*24457Smckusick 			    e_com = s_com + 2;
199*24457Smckusick 			    *e_com = 0;
200*24457Smckusick 			    if (blanklines_before_blockcomments) prefix_blankline_requested = 1;
201*24457Smckusick 			    dump_line();
202*24457Smckusick 			    e_com = t;
203*24457Smckusick 			    s_com[0] = s_com[1] = s_com[2] = ' ';
204*24457Smckusick 			}
205*24457Smckusick 			dump_line();
206*24457Smckusick 			*e_com++ = ' ';
207*24457Smckusick 			*e_com++ = ' ';
2088806Smckusick 		    }
209*24457Smckusick 		    dump_line();
210*24457Smckusick 		    now_col = ps.com_col;
211*24457Smckusick 		} else {
212*24457Smckusick 		ps.last_nl = 1;
213*24457Smckusick 		if (unix_comment != 1) {	/* we not are in
214*24457Smckusick 						 * unix_style comment */
2158806Smckusick 		    if (unix_comment == 0 && s_code == e_code) {
216*24457Smckusick 			/*
217*24457Smckusick 			 * if it is a UNIX-style comment, ignore the
218*24457Smckusick 			 * requirement that previous line be blank for
219*24457Smckusick 			 * unindention
220*24457Smckusick 			 */
221*24457Smckusick 			ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
222*24457Smckusick 			if (ps.com_col <= 1)
223*24457Smckusick 			    ps.com_col = 2;
2248806Smckusick 		    }
225*24457Smckusick 		    unix_comment = 2;	/* permanently remember that we
226*24457Smckusick 					 * are in this type of comment */
227*24457Smckusick 		    dump_line();
2288806Smckusick 		    ++line_no;
229*24457Smckusick 		    now_col = ps.com_col;
2308806Smckusick 		    *e_com++ = ' ';
231*24457Smckusick 		    /*
232*24457Smckusick 		     * fix so that the star at the start of the line will
233*24457Smckusick 		     * line up
234*24457Smckusick 		     */
235*24457Smckusick 		    do		/* flush leading white space */
2368806Smckusick 			if (++buf_ptr >= buf_end)
237*24457Smckusick 			    fill_buffer();
2388806Smckusick 		    while (*buf_ptr == ' ' || *buf_ptr == '\t');
2398806Smckusick 		    break;
2408806Smckusick 		}
2418806Smckusick 		if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
2428806Smckusick 		    last_bl = e_com - 1;
243*24457Smckusick 		/*
244*24457Smckusick 		 * if there was a space at the end of the last line,
245*24457Smckusick 		 * remember where it was
246*24457Smckusick 		 */
247*24457Smckusick 		else {		/* otherwise, insert one */
2488806Smckusick 		    last_bl = e_com;
2498806Smckusick 		    *e_com++ = ' ';
2508806Smckusick 		    ++now_col;
2518806Smckusick 		}
252*24457Smckusick 		}
253*24457Smckusick 		++line_no;	/* keep track of input line number */
254*24457Smckusick 		if (!ps.box_com) {
255*24457Smckusick 		    int         nstar = 1;
256*24457Smckusick 		    do {	/* flush any blanks and/or tabs at start
257*24457Smckusick 				 * of next line */
258*24457Smckusick 			if (++buf_ptr >= buf_end)
259*24457Smckusick 			    fill_buffer();
260*24457Smckusick 			if (*buf_ptr == '*' && --nstar >= 0) {
261*24457Smckusick 			    if (++buf_ptr >= buf_end)
262*24457Smckusick 				fill_buffer();
263*24457Smckusick 			    if (*buf_ptr == '/')
264*24457Smckusick 				goto end_of_comment;
265*24457Smckusick 			}
266*24457Smckusick 		    } while (*buf_ptr == ' ' || *buf_ptr == '\t');
267*24457Smckusick 		} else if (++buf_ptr >= buf_end) fill_buffer();
268*24457Smckusick 		break;		/* end of case for newline */
2698806Smckusick 
270*24457Smckusick 	    case '*':		/* must check for possibility of being at
271*24457Smckusick 				 * end of comment */
272*24457Smckusick 		if (++buf_ptr >= buf_end)	/* get to next char after * */
273*24457Smckusick 		    fill_buffer();
2748806Smckusick 
275*24457Smckusick 		if (unix_comment == 0)	/* set flag to show we are not in
276*24457Smckusick 					 * unix-style comment */
2778806Smckusick 		    unix_comment = 1;
2788806Smckusick 
279*24457Smckusick 		if (*buf_ptr == '/') {	/* it is the end!!! */
280*24457Smckusick 	    end_of_comment:
2818806Smckusick 		    if (++buf_ptr >= buf_end)
282*24457Smckusick 			fill_buffer();
2838806Smckusick 
284*24457Smckusick 		    if (*(e_com - 1) != ' ' && !ps.box_com) {	/* insure blank before
285*24457Smckusick 								 * end */
2868806Smckusick 			*e_com++ = ' ';
2878806Smckusick 			++now_col;
2888806Smckusick 		    }
289*24457Smckusick 		    if (break_delim == 1 && !one_liner && s_com[0] == '/'
290*24457Smckusick 			&& s_com[1] == '*' && s_com[2] == ' ') {
291*24457Smckusick 			char       *t = e_com;
292*24457Smckusick 			break_delim = 2;
293*24457Smckusick 			e_com = s_com + 2;
294*24457Smckusick 			*e_com = 0;
295*24457Smckusick 			    if (blanklines_before_blockcomments) prefix_blankline_requested = 1;
296*24457Smckusick 			dump_line();
297*24457Smckusick 			e_com = t;
298*24457Smckusick 			s_com[0] = s_com[1] = s_com[2] = ' ';
299*24457Smckusick 		    }
300*24457Smckusick 		    if (break_delim == 2 && e_com > s_com + 3
301*24457Smckusick 			 /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
3028806Smckusick 			*e_com = '\0';
303*24457Smckusick 			dump_line();
304*24457Smckusick 			now_col = ps.com_col;
3058806Smckusick 		    }
3068806Smckusick 		    *e_com++ = '*';
3078806Smckusick 		    *e_com++ = '/';
3088806Smckusick 		    *e_com = '\0';
309*24457Smckusick 		    ps.just_saw_decl = l_just_saw_decl;
310*24457Smckusick 		    return;
311*24457Smckusick 		} else {	/* handle isolated '*' */
3128806Smckusick 		    *e_com++ = '*';
3138806Smckusick 		    ++now_col;
3148806Smckusick 		}
315*24457Smckusick 		break;
316*24457Smckusick 	    default:		/* we have a random char */
3178806Smckusick 		if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
318*24457Smckusick 		    unix_comment = 1;	/* we are not in unix-style
319*24457Smckusick 					 * comment */
3208806Smckusick 
3218806Smckusick 		*e_com = *buf_ptr++;
3228806Smckusick 		if (buf_ptr >= buf_end)
323*24457Smckusick 		    fill_buffer();
3248806Smckusick 
325*24457Smckusick 		if (*e_com == '\t')	/* keep track of column */
3268806Smckusick 		    now_col = ((now_col - 1) & tabmask) + tabsize + 1;
327*24457Smckusick 		else if (*e_com == '\b')	/* this is a backspace */
328*24457Smckusick 		    --now_col;
3298806Smckusick 		else
330*24457Smckusick 		    ++now_col;
3318806Smckusick 
3328806Smckusick 		if (*e_com == ' ' || *e_com == '\t')
3338806Smckusick 		    last_bl = e_com;
334*24457Smckusick 		/* remember we saw a blank */
3358806Smckusick 
3368806Smckusick 		++e_com;
337*24457Smckusick 		if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
338*24457Smckusick 		    /* the comment is too long, it must be broken up */
339*24457Smckusick 		    if (break_delim == 1 && s_com[0] == '/'
340*24457Smckusick 			&& s_com[1] == '*' && s_com[2] == ' ') {
341*24457Smckusick 			char       *t = e_com;
342*24457Smckusick 			break_delim = 2;
343*24457Smckusick 			e_com = s_com + 2;
344*24457Smckusick 			*e_com = 0;
345*24457Smckusick 			    if (blanklines_before_blockcomments) prefix_blankline_requested = 1;
346*24457Smckusick 			dump_line();
347*24457Smckusick 			e_com = t;
348*24457Smckusick 			s_com[0] = s_com[1] = s_com[2] = ' ';
349*24457Smckusick 		    }
350*24457Smckusick 		    if (last_bl == 0) {	/* we have seen no blanks */
351*24457Smckusick 			last_bl = e_com;	/* fake it */
3528806Smckusick 			*e_com++ = ' ';
3538806Smckusick 		    }
354*24457Smckusick 		    *e_com = '\0';	/* print what we have */
3558806Smckusick 		    *last_bl = '\0';
356*24457Smckusick 		    while (last_bl > s_com && last_bl[-1] < 040)
357*24457Smckusick 			*--last_bl = 0;
3588806Smckusick 		    e_com = last_bl;
359*24457Smckusick 		    dump_line();
3608806Smckusick 
361*24457Smckusick 		    *e_com++ = ' ';	/* add blanks for continuation */
3628806Smckusick 		    *e_com++ = ' ';
3638806Smckusick 		    *e_com++ = ' ';
3648806Smckusick 
3658806Smckusick 		    t_ptr = last_bl + 1;
3668806Smckusick 		    last_bl = 0;
367*24457Smckusick 		    if (t_ptr >= e_com) {
368*24457Smckusick 			while (*t_ptr == ' ' || *t_ptr == '\t')
369*24457Smckusick 			    t_ptr++;
370*24457Smckusick 			while (*t_ptr != '\0') {	/* move unprinted part
371*24457Smckusick 							 * of comment down in
372*24457Smckusick 							 * buffer */
373*24457Smckusick 			    if (*t_ptr == ' ' || *t_ptr == '\t')
374*24457Smckusick 				last_bl = e_com;
375*24457Smckusick 			    *e_com++ = *t_ptr++;
376*24457Smckusick 			}
3778806Smckusick 		    }
3788806Smckusick 		    *e_com = '\0';
379*24457Smckusick 		    now_col = count_spaces(ps.com_col, s_com);	/* recompute current
380*24457Smckusick 								 * position */
381*24457Smckusick 		}
382*24457Smckusick 		break;
383*24457Smckusick 	}
384*24457Smckusick     }
385*24457Smckusick }
386