xref: /minix3/usr.bin/indent/pr_comment.c (revision 97c7d358ea5970ca59196ed90e975115245f9e76)
1*97c7d358SVivek Prakash /*	$NetBSD: pr_comment.c,v 1.9 2003/08/07 11:14:09 agc Exp $	*/
2*97c7d358SVivek Prakash 
3*97c7d358SVivek Prakash /*
4*97c7d358SVivek Prakash  * Copyright (c) 1980, 1993
5*97c7d358SVivek Prakash  *	The Regents of the University of California.  All rights reserved.
6*97c7d358SVivek Prakash  *
7*97c7d358SVivek Prakash  * Redistribution and use in source and binary forms, with or without
8*97c7d358SVivek Prakash  * modification, are permitted provided that the following conditions
9*97c7d358SVivek Prakash  * are met:
10*97c7d358SVivek Prakash  * 1. Redistributions of source code must retain the above copyright
11*97c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer.
12*97c7d358SVivek Prakash  * 2. Redistributions in binary form must reproduce the above copyright
13*97c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer in the
14*97c7d358SVivek Prakash  *    documentation and/or other materials provided with the distribution.
15*97c7d358SVivek Prakash  * 3. Neither the name of the University nor the names of its contributors
16*97c7d358SVivek Prakash  *    may be used to endorse or promote products derived from this software
17*97c7d358SVivek Prakash  *    without specific prior written permission.
18*97c7d358SVivek Prakash  *
19*97c7d358SVivek Prakash  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*97c7d358SVivek Prakash  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*97c7d358SVivek Prakash  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*97c7d358SVivek Prakash  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*97c7d358SVivek Prakash  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*97c7d358SVivek Prakash  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*97c7d358SVivek Prakash  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*97c7d358SVivek Prakash  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*97c7d358SVivek Prakash  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*97c7d358SVivek Prakash  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*97c7d358SVivek Prakash  * SUCH DAMAGE.
30*97c7d358SVivek Prakash  */
31*97c7d358SVivek Prakash 
32*97c7d358SVivek Prakash /*
33*97c7d358SVivek Prakash  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
34*97c7d358SVivek Prakash  * Copyright (c) 1985 Sun Microsystems, Inc.
35*97c7d358SVivek Prakash  * All rights reserved.
36*97c7d358SVivek Prakash  *
37*97c7d358SVivek Prakash  * Redistribution and use in source and binary forms, with or without
38*97c7d358SVivek Prakash  * modification, are permitted provided that the following conditions
39*97c7d358SVivek Prakash  * are met:
40*97c7d358SVivek Prakash  * 1. Redistributions of source code must retain the above copyright
41*97c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer.
42*97c7d358SVivek Prakash  * 2. Redistributions in binary form must reproduce the above copyright
43*97c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer in the
44*97c7d358SVivek Prakash  *    documentation and/or other materials provided with the distribution.
45*97c7d358SVivek Prakash  * 3. All advertising materials mentioning features or use of this software
46*97c7d358SVivek Prakash  *    must display the following acknowledgement:
47*97c7d358SVivek Prakash  *	This product includes software developed by the University of
48*97c7d358SVivek Prakash  *	California, Berkeley and its contributors.
49*97c7d358SVivek Prakash  * 4. Neither the name of the University nor the names of its contributors
50*97c7d358SVivek Prakash  *    may be used to endorse or promote products derived from this software
51*97c7d358SVivek Prakash  *    without specific prior written permission.
52*97c7d358SVivek Prakash  *
53*97c7d358SVivek Prakash  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54*97c7d358SVivek Prakash  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55*97c7d358SVivek Prakash  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56*97c7d358SVivek Prakash  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57*97c7d358SVivek Prakash  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58*97c7d358SVivek Prakash  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59*97c7d358SVivek Prakash  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60*97c7d358SVivek Prakash  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61*97c7d358SVivek Prakash  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62*97c7d358SVivek Prakash  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63*97c7d358SVivek Prakash  * SUCH DAMAGE.
64*97c7d358SVivek Prakash  */
65*97c7d358SVivek Prakash 
66*97c7d358SVivek Prakash #include <sys/cdefs.h>
67*97c7d358SVivek Prakash #ifndef lint
68*97c7d358SVivek Prakash #if 0
69*97c7d358SVivek Prakash static char sccsid[] = "@(#)pr_comment.c	8.1 (Berkeley) 6/6/93";
70*97c7d358SVivek Prakash #else
71*97c7d358SVivek Prakash __RCSID("$NetBSD: pr_comment.c,v 1.9 2003/08/07 11:14:09 agc Exp $");
72*97c7d358SVivek Prakash #endif
73*97c7d358SVivek Prakash #endif				/* not lint */
74*97c7d358SVivek Prakash 
75*97c7d358SVivek Prakash #include <stdio.h>
76*97c7d358SVivek Prakash #include <stdlib.h>
77*97c7d358SVivek Prakash #include <ctype.h>
78*97c7d358SVivek Prakash #include "indent_globs.h"
79*97c7d358SVivek Prakash 
80*97c7d358SVivek Prakash /*
81*97c7d358SVivek Prakash  * NAME:
82*97c7d358SVivek Prakash  *	pr_comment
83*97c7d358SVivek Prakash  *
84*97c7d358SVivek Prakash  * FUNCTION:
85*97c7d358SVivek Prakash  *	This routine takes care of scanning and printing comments.
86*97c7d358SVivek Prakash  *
87*97c7d358SVivek Prakash  * ALGORITHM:
88*97c7d358SVivek Prakash  *	1) Decide where the comment should be aligned, and if lines should
89*97c7d358SVivek Prakash  *	   be broken.
90*97c7d358SVivek Prakash  *	2) If lines should not be broken and filled, just copy up to end of
91*97c7d358SVivek Prakash  *	   comment.
92*97c7d358SVivek Prakash  *	3) If lines should be filled, then scan thru input_buffer copying
93*97c7d358SVivek Prakash  *	   characters to com_buf.  Remember where the last blank, tab, or
94*97c7d358SVivek Prakash  *	   newline was.  When line is filled, print up to last blank and
95*97c7d358SVivek Prakash  *	   continue copying.
96*97c7d358SVivek Prakash  *
97*97c7d358SVivek Prakash  * HISTORY:
98*97c7d358SVivek Prakash  *	November 1976	D A Willcox of CAC	Initial coding
99*97c7d358SVivek Prakash  *	12/6/76		D A Willcox of CAC	Modification to handle
100*97c7d358SVivek Prakash  *						UNIX-style comments
101*97c7d358SVivek Prakash  *
102*97c7d358SVivek Prakash  */
103*97c7d358SVivek Prakash 
104*97c7d358SVivek Prakash /*
105*97c7d358SVivek Prakash  * this routine processes comments.  It makes an attempt to keep comments from
106*97c7d358SVivek Prakash  * going over the max line length.  If a line is too long, it moves everything
107*97c7d358SVivek Prakash  * from the last blank to the next comment line.  Blanks and tabs from the
108*97c7d358SVivek Prakash  * beginning of the input line are removed
109*97c7d358SVivek Prakash  */
110*97c7d358SVivek Prakash 
111*97c7d358SVivek Prakash 
112*97c7d358SVivek Prakash void
pr_comment(void)113*97c7d358SVivek Prakash pr_comment(void)
114*97c7d358SVivek Prakash {
115*97c7d358SVivek Prakash 	int     now_col;	/* column we are in now */
116*97c7d358SVivek Prakash 	int     adj_max_col;	/* Adjusted max_col for when we decide to
117*97c7d358SVivek Prakash 				 * spill comments over the right margin */
118*97c7d358SVivek Prakash 	char   *last_bl;	/* points to the last blank in the output
119*97c7d358SVivek Prakash 				 * buffer */
120*97c7d358SVivek Prakash 	char   *t_ptr;		/* used for moving string */
121*97c7d358SVivek Prakash 	int     unix_comment;	/* tri-state variable used to decide if it is
122*97c7d358SVivek Prakash 				 * a unix-style comment. 0 means only blanks
123*97c7d358SVivek Prakash 				 * since start, 1 means regular style comment,
124*97c7d358SVivek Prakash 				 * 2 means unix style comment */
125*97c7d358SVivek Prakash 	int     break_delim = comment_delimiter_on_blankline;
126*97c7d358SVivek Prakash 	int     l_just_saw_decl = ps.just_saw_decl;
127*97c7d358SVivek Prakash 	/*
128*97c7d358SVivek Prakash          * int         ps.last_nl = 0;	/ * true iff the last significant thing
129*97c7d358SVivek Prakash          * weve seen is a newline
130*97c7d358SVivek Prakash          */
131*97c7d358SVivek Prakash 	int     one_liner = 1;	/* true iff this comment is a one-liner */
132*97c7d358SVivek Prakash 	adj_max_col = max_col;
133*97c7d358SVivek Prakash 	ps.just_saw_decl = 0;
134*97c7d358SVivek Prakash 	last_bl = 0;		/* no blanks found so far */
135*97c7d358SVivek Prakash 	ps.box_com = false;	/* at first, assume that we are not in a boxed
136*97c7d358SVivek Prakash 				 * comment or some other comment that should
137*97c7d358SVivek Prakash 				 * not be touched */
138*97c7d358SVivek Prakash 	++ps.out_coms;		/* keep track of number of comments */
139*97c7d358SVivek Prakash 	unix_comment = 1;	/* set flag to let us figure out if there is a
140*97c7d358SVivek Prakash 				 * unix-style comment ** DISABLED: use 0 to
141*97c7d358SVivek Prakash 				 * reenable this hack! */
142*97c7d358SVivek Prakash 
143*97c7d358SVivek Prakash 	/* Figure where to align and how to treat the comment */
144*97c7d358SVivek Prakash 
145*97c7d358SVivek Prakash 	if (ps.col_1 && !format_col1_comments) {	/* if comment starts in
146*97c7d358SVivek Prakash 							 * column 1 it should
147*97c7d358SVivek Prakash 							 * not be touched */
148*97c7d358SVivek Prakash 		ps.box_com = true;
149*97c7d358SVivek Prakash 		ps.com_col = 1;
150*97c7d358SVivek Prakash 	} else {
151*97c7d358SVivek Prakash 		if (*buf_ptr == '-' || *buf_ptr == '*' || *buf_ptr == '\n') {
152*97c7d358SVivek Prakash 			ps.box_com = true;	/* a comment with a '-', '*'
153*97c7d358SVivek Prakash 						 * or newline immediately
154*97c7d358SVivek Prakash 						 * after the start comment is
155*97c7d358SVivek Prakash 						 * assumed to be a boxed
156*97c7d358SVivek Prakash 						 * comment */
157*97c7d358SVivek Prakash 			break_delim = 0;
158*97c7d358SVivek Prakash 		}
159*97c7d358SVivek Prakash 		if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
160*97c7d358SVivek Prakash 			/* klg: check only if this line is blank */
161*97c7d358SVivek Prakash 			/*
162*97c7d358SVivek Prakash 		         * If this (*and previous lines are*) blank, dont put comment way
163*97c7d358SVivek Prakash 		         * out at left
164*97c7d358SVivek Prakash 		         */
165*97c7d358SVivek Prakash 			ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
166*97c7d358SVivek Prakash 			adj_max_col = block_comment_max_col;
167*97c7d358SVivek Prakash 			if (ps.com_col <= 1)
168*97c7d358SVivek Prakash 				ps.com_col = 1 + !format_col1_comments;
169*97c7d358SVivek Prakash 		} else {
170*97c7d358SVivek Prakash 			int     target_col;
171*97c7d358SVivek Prakash 			break_delim = 0;
172*97c7d358SVivek Prakash 			if (s_code != e_code)
173*97c7d358SVivek Prakash 				target_col = count_spaces(compute_code_target(), s_code);
174*97c7d358SVivek Prakash 			else {
175*97c7d358SVivek Prakash 				target_col = 1;
176*97c7d358SVivek Prakash 				if (s_lab != e_lab)
177*97c7d358SVivek Prakash 					target_col = count_spaces(compute_label_target(), s_lab);
178*97c7d358SVivek Prakash 			}
179*97c7d358SVivek Prakash 			ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
180*97c7d358SVivek Prakash 			if (ps.com_col < target_col)
181*97c7d358SVivek Prakash 				ps.com_col = ((target_col + 7) & ~7) + 1;
182*97c7d358SVivek Prakash 			if (ps.com_col + 24 > adj_max_col)
183*97c7d358SVivek Prakash 				adj_max_col = ps.com_col + 24;
184*97c7d358SVivek Prakash 		}
185*97c7d358SVivek Prakash 	}
186*97c7d358SVivek Prakash 	if (ps.box_com) {
187*97c7d358SVivek Prakash 		buf_ptr[-2] = 0;
188*97c7d358SVivek Prakash 		ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
189*97c7d358SVivek Prakash 		buf_ptr[-2] = '/';
190*97c7d358SVivek Prakash 	} else {
191*97c7d358SVivek Prakash 		ps.n_comment_delta = 0;
192*97c7d358SVivek Prakash 		while (*buf_ptr == ' ' || *buf_ptr == '\t')
193*97c7d358SVivek Prakash 			buf_ptr++;
194*97c7d358SVivek Prakash 	}
195*97c7d358SVivek Prakash 	ps.comment_delta = 0;
196*97c7d358SVivek Prakash 	*e_com++ = '/';		/* put '/' + '*' into buffer */
197*97c7d358SVivek Prakash 	*e_com++ = '*';
198*97c7d358SVivek Prakash 	if (*buf_ptr != ' ' && !ps.box_com)
199*97c7d358SVivek Prakash 		*e_com++ = ' ';
200*97c7d358SVivek Prakash 
201*97c7d358SVivek Prakash 	*e_com = '\0';
202*97c7d358SVivek Prakash 	if (troff) {
203*97c7d358SVivek Prakash 		now_col = 1;
204*97c7d358SVivek Prakash 		adj_max_col = 80;
205*97c7d358SVivek Prakash 	} else
206*97c7d358SVivek Prakash 		now_col = count_spaces(ps.com_col, s_com);	/* figure what column we
207*97c7d358SVivek Prakash 								 * would be in if we
208*97c7d358SVivek Prakash 								 * printed the comment
209*97c7d358SVivek Prakash 								 * now */
210*97c7d358SVivek Prakash 
211*97c7d358SVivek Prakash 	/* Start to copy the comment */
212*97c7d358SVivek Prakash 
213*97c7d358SVivek Prakash 	while (1) {		/* this loop will go until the comment is
214*97c7d358SVivek Prakash 				 * copied */
215*97c7d358SVivek Prakash 		if (!iscntrl((unsigned char)*buf_ptr) && *buf_ptr != '*')
216*97c7d358SVivek Prakash 			ps.last_nl = 0;
217*97c7d358SVivek Prakash 		CHECK_SIZE_COM;
218*97c7d358SVivek Prakash 		switch (*buf_ptr) {	/* this checks for various spcl cases */
219*97c7d358SVivek Prakash 		case 014:	/* check for a form feed */
220*97c7d358SVivek Prakash 			if (!ps.box_com) {	/* in a text comment, break
221*97c7d358SVivek Prakash 						 * the line here */
222*97c7d358SVivek Prakash 				ps.use_ff = true;
223*97c7d358SVivek Prakash 				/* fix so dump_line uses a form feed */
224*97c7d358SVivek Prakash 				dump_line();
225*97c7d358SVivek Prakash 				last_bl = 0;
226*97c7d358SVivek Prakash 				*e_com++ = ' ';
227*97c7d358SVivek Prakash 				*e_com++ = '*';
228*97c7d358SVivek Prakash 				*e_com++ = ' ';
229*97c7d358SVivek Prakash 				while (*++buf_ptr == ' ' || *buf_ptr == '\t');
230*97c7d358SVivek Prakash 			} else {
231*97c7d358SVivek Prakash 				if (++buf_ptr >= buf_end)
232*97c7d358SVivek Prakash 					fill_buffer();
233*97c7d358SVivek Prakash 				*e_com++ = 014;
234*97c7d358SVivek Prakash 			}
235*97c7d358SVivek Prakash 			break;
236*97c7d358SVivek Prakash 
237*97c7d358SVivek Prakash 		case '\n':
238*97c7d358SVivek Prakash 			if (had_eof) {	/* check for unexpected eof */
239*97c7d358SVivek Prakash 				printf("Unterminated comment\n");
240*97c7d358SVivek Prakash 				*e_com = '\0';
241*97c7d358SVivek Prakash 				dump_line();
242*97c7d358SVivek Prakash 				return;
243*97c7d358SVivek Prakash 			}
244*97c7d358SVivek Prakash 			one_liner = 0;
245*97c7d358SVivek Prakash 			if (ps.box_com || ps.last_nl) {	/* if this is a boxed
246*97c7d358SVivek Prakash 							 * comment, we dont
247*97c7d358SVivek Prakash 							 * ignore the newline */
248*97c7d358SVivek Prakash 				if (s_com == e_com) {
249*97c7d358SVivek Prakash 					*e_com++ = ' ';
250*97c7d358SVivek Prakash 					*e_com++ = ' ';
251*97c7d358SVivek Prakash 				}
252*97c7d358SVivek Prakash 				*e_com = '\0';
253*97c7d358SVivek Prakash 				if (!ps.box_com && e_com - s_com > 3) {
254*97c7d358SVivek Prakash 					if (break_delim == 1 && s_com[0] == '/'
255*97c7d358SVivek Prakash 					    && s_com[1] == '*' && s_com[2] == ' ') {
256*97c7d358SVivek Prakash 						char   *t = e_com;
257*97c7d358SVivek Prakash 						break_delim = 2;
258*97c7d358SVivek Prakash 						e_com = s_com + 2;
259*97c7d358SVivek Prakash 						*e_com = 0;
260*97c7d358SVivek Prakash 						if (blanklines_before_blockcomments)
261*97c7d358SVivek Prakash 							prefix_blankline_requested = 1;
262*97c7d358SVivek Prakash 						dump_line();
263*97c7d358SVivek Prakash 						e_com = t;
264*97c7d358SVivek Prakash 						s_com[0] = s_com[1] = s_com[2] = ' ';
265*97c7d358SVivek Prakash 					}
266*97c7d358SVivek Prakash 					dump_line();
267*97c7d358SVivek Prakash 					CHECK_SIZE_COM;
268*97c7d358SVivek Prakash 					*e_com++ = ' ';
269*97c7d358SVivek Prakash 					*e_com++ = ' ';
270*97c7d358SVivek Prakash 				}
271*97c7d358SVivek Prakash 				dump_line();
272*97c7d358SVivek Prakash 				now_col = ps.com_col;
273*97c7d358SVivek Prakash 			} else {
274*97c7d358SVivek Prakash 				ps.last_nl = 1;
275*97c7d358SVivek Prakash 				if (unix_comment != 1) {	/* we not are in
276*97c7d358SVivek Prakash 								 * unix_style comment */
277*97c7d358SVivek Prakash 					if (unix_comment == 0 && s_code == e_code) {
278*97c7d358SVivek Prakash 						/*
279*97c7d358SVivek Prakash 						 * if it is a UNIX-style comment, ignore the
280*97c7d358SVivek Prakash 						 * requirement that previous line be blank for
281*97c7d358SVivek Prakash 						 * unindention
282*97c7d358SVivek Prakash 						 */
283*97c7d358SVivek Prakash 						ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
284*97c7d358SVivek Prakash 						if (ps.com_col <= 1)
285*97c7d358SVivek Prakash 							ps.com_col = 2;
286*97c7d358SVivek Prakash 					}
287*97c7d358SVivek Prakash 					unix_comment = 2;	/* permanently remember
288*97c7d358SVivek Prakash 								 * that we are in this
289*97c7d358SVivek Prakash 								 * type of comment */
290*97c7d358SVivek Prakash 					dump_line();
291*97c7d358SVivek Prakash 					++line_no;
292*97c7d358SVivek Prakash 					now_col = ps.com_col;
293*97c7d358SVivek Prakash 					*e_com++ = ' ';
294*97c7d358SVivek Prakash 					/*
295*97c7d358SVivek Prakash 				         * fix so that the star at the start of the line will line
296*97c7d358SVivek Prakash 				         * up
297*97c7d358SVivek Prakash 				         */
298*97c7d358SVivek Prakash 					do	/* flush leading white space */
299*97c7d358SVivek Prakash 						if (++buf_ptr >= buf_end)
300*97c7d358SVivek Prakash 							fill_buffer();
301*97c7d358SVivek Prakash 					while (*buf_ptr == ' ' || *buf_ptr == '\t');
302*97c7d358SVivek Prakash 					break;
303*97c7d358SVivek Prakash 				}
304*97c7d358SVivek Prakash 				if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
305*97c7d358SVivek Prakash 					last_bl = e_com - 1;
306*97c7d358SVivek Prakash 				/*
307*97c7d358SVivek Prakash 				 * if there was a space at the end of the last line, remember
308*97c7d358SVivek Prakash 				 * where it was
309*97c7d358SVivek Prakash 				 */
310*97c7d358SVivek Prakash 				else {	/* otherwise, insert one */
311*97c7d358SVivek Prakash 					last_bl = e_com;
312*97c7d358SVivek Prakash 					CHECK_SIZE_COM;
313*97c7d358SVivek Prakash 					*e_com++ = ' ';
314*97c7d358SVivek Prakash 					++now_col;
315*97c7d358SVivek Prakash 				}
316*97c7d358SVivek Prakash 			}
317*97c7d358SVivek Prakash 			++line_no;	/* keep track of input line number */
318*97c7d358SVivek Prakash 			if (!ps.box_com) {
319*97c7d358SVivek Prakash 				int     nstar = 1;
320*97c7d358SVivek Prakash 				do {	/* flush any blanks and/or tabs at
321*97c7d358SVivek Prakash 					 * start of next line */
322*97c7d358SVivek Prakash 					if (++buf_ptr >= buf_end)
323*97c7d358SVivek Prakash 						fill_buffer();
324*97c7d358SVivek Prakash 					if (*buf_ptr == '*' && --nstar >= 0) {
325*97c7d358SVivek Prakash 						if (++buf_ptr >= buf_end)
326*97c7d358SVivek Prakash 							fill_buffer();
327*97c7d358SVivek Prakash 						if (*buf_ptr == '/')
328*97c7d358SVivek Prakash 							goto end_of_comment;
329*97c7d358SVivek Prakash 					}
330*97c7d358SVivek Prakash 				} while (*buf_ptr == ' ' || *buf_ptr == '\t');
331*97c7d358SVivek Prakash 			} else
332*97c7d358SVivek Prakash 				if (++buf_ptr >= buf_end)
333*97c7d358SVivek Prakash 					fill_buffer();
334*97c7d358SVivek Prakash 			break;	/* end of case for newline */
335*97c7d358SVivek Prakash 
336*97c7d358SVivek Prakash 		case '*':	/* must check for possibility of being at end
337*97c7d358SVivek Prakash 				 * of comment */
338*97c7d358SVivek Prakash 			if (++buf_ptr >= buf_end)	/* get to next char
339*97c7d358SVivek Prakash 							 * after * */
340*97c7d358SVivek Prakash 				fill_buffer();
341*97c7d358SVivek Prakash 
342*97c7d358SVivek Prakash 			if (unix_comment == 0)	/* set flag to show we are not
343*97c7d358SVivek Prakash 						 * in unix-style comment */
344*97c7d358SVivek Prakash 				unix_comment = 1;
345*97c7d358SVivek Prakash 
346*97c7d358SVivek Prakash 			if (*buf_ptr == '/') {	/* it is the end!!! */
347*97c7d358SVivek Prakash 		end_of_comment:
348*97c7d358SVivek Prakash 				if (++buf_ptr >= buf_end)
349*97c7d358SVivek Prakash 					fill_buffer();
350*97c7d358SVivek Prakash 
351*97c7d358SVivek Prakash 				if (*(e_com - 1) != ' ' && !ps.box_com) {	/* insure blank before
352*97c7d358SVivek Prakash 										 * end */
353*97c7d358SVivek Prakash 					*e_com++ = ' ';
354*97c7d358SVivek Prakash 					++now_col;
355*97c7d358SVivek Prakash 				}
356*97c7d358SVivek Prakash 				if (break_delim == 1 && !one_liner && s_com[0] == '/'
357*97c7d358SVivek Prakash 				    && s_com[1] == '*' && s_com[2] == ' ') {
358*97c7d358SVivek Prakash 					char   *t = e_com;
359*97c7d358SVivek Prakash 					break_delim = 2;
360*97c7d358SVivek Prakash 					e_com = s_com + 2;
361*97c7d358SVivek Prakash 					*e_com = 0;
362*97c7d358SVivek Prakash 					if (blanklines_before_blockcomments)
363*97c7d358SVivek Prakash 						prefix_blankline_requested = 1;
364*97c7d358SVivek Prakash 					dump_line();
365*97c7d358SVivek Prakash 					e_com = t;
366*97c7d358SVivek Prakash 					s_com[0] = s_com[1] = s_com[2] = ' ';
367*97c7d358SVivek Prakash 				}
368*97c7d358SVivek Prakash 				if (break_delim == 2 && e_com > s_com + 3
369*97c7d358SVivek Prakash 				     /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
370*97c7d358SVivek Prakash 					*e_com = '\0';
371*97c7d358SVivek Prakash 					dump_line();
372*97c7d358SVivek Prakash 					now_col = ps.com_col;
373*97c7d358SVivek Prakash 				}
374*97c7d358SVivek Prakash 				CHECK_SIZE_COM;
375*97c7d358SVivek Prakash 				*e_com++ = '*';
376*97c7d358SVivek Prakash 				*e_com++ = '/';
377*97c7d358SVivek Prakash 				*e_com = '\0';
378*97c7d358SVivek Prakash 				ps.just_saw_decl = l_just_saw_decl;
379*97c7d358SVivek Prakash 				return;
380*97c7d358SVivek Prakash 			} else {/* handle isolated '*' */
381*97c7d358SVivek Prakash 				*e_com++ = '*';
382*97c7d358SVivek Prakash 				++now_col;
383*97c7d358SVivek Prakash 			}
384*97c7d358SVivek Prakash 			break;
385*97c7d358SVivek Prakash 		default:	/* we have a random char */
386*97c7d358SVivek Prakash 			if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
387*97c7d358SVivek Prakash 				unix_comment = 1;	/* we are not in
388*97c7d358SVivek Prakash 							 * unix-style comment */
389*97c7d358SVivek Prakash 
390*97c7d358SVivek Prakash 			*e_com = *buf_ptr++;
391*97c7d358SVivek Prakash 			if (buf_ptr >= buf_end)
392*97c7d358SVivek Prakash 				fill_buffer();
393*97c7d358SVivek Prakash 
394*97c7d358SVivek Prakash 			if (*e_com == '\t')	/* keep track of column */
395*97c7d358SVivek Prakash 				now_col = ((now_col - 1) & tabmask) + tabsize + 1;
396*97c7d358SVivek Prakash 			else
397*97c7d358SVivek Prakash 				if (*e_com == '\b')	/* this is a backspace */
398*97c7d358SVivek Prakash 					--now_col;
399*97c7d358SVivek Prakash 				else
400*97c7d358SVivek Prakash 					++now_col;
401*97c7d358SVivek Prakash 
402*97c7d358SVivek Prakash 			if (*e_com == ' ' || *e_com == '\t')
403*97c7d358SVivek Prakash 				last_bl = e_com;
404*97c7d358SVivek Prakash 			/* remember we saw a blank */
405*97c7d358SVivek Prakash 
406*97c7d358SVivek Prakash 			++e_com;
407*97c7d358SVivek Prakash 			if (now_col > adj_max_col && !ps.box_com && unix_comment == 1
408*97c7d358SVivek Prakash 				&& !iscntrl((unsigned char)e_com[-1])
409*97c7d358SVivek Prakash 				&& !isblank((unsigned char)e_com[-1])) {
410*97c7d358SVivek Prakash 				/*
411*97c7d358SVivek Prakash 				 * the comment is too long, it must be broken up
412*97c7d358SVivek Prakash 				 */
413*97c7d358SVivek Prakash 				if (break_delim == 1 && s_com[0] == '/'
414*97c7d358SVivek Prakash 				    && s_com[1] == '*' && s_com[2] == ' ') {
415*97c7d358SVivek Prakash 					char   *t = e_com;
416*97c7d358SVivek Prakash 					break_delim = 2;
417*97c7d358SVivek Prakash 					e_com = s_com + 2;
418*97c7d358SVivek Prakash 					*e_com = 0;
419*97c7d358SVivek Prakash 					if (blanklines_before_blockcomments)
420*97c7d358SVivek Prakash 						prefix_blankline_requested = 1;
421*97c7d358SVivek Prakash 					dump_line();
422*97c7d358SVivek Prakash 					e_com = t;
423*97c7d358SVivek Prakash 					s_com[0] = s_com[1] = s_com[2] = ' ';
424*97c7d358SVivek Prakash 				}
425*97c7d358SVivek Prakash 				if (last_bl == 0) {	/* we have seen no
426*97c7d358SVivek Prakash 							 * blanks */
427*97c7d358SVivek Prakash 					last_bl = e_com;	/* fake it */
428*97c7d358SVivek Prakash 					*e_com++ = ' ';
429*97c7d358SVivek Prakash 				}
430*97c7d358SVivek Prakash 				*e_com = '\0';	/* print what we have */
431*97c7d358SVivek Prakash 				*last_bl = '\0';
432*97c7d358SVivek Prakash 				while (last_bl > s_com && iscntrl((unsigned char)last_bl[-1]) )
433*97c7d358SVivek Prakash 					*--last_bl = 0;
434*97c7d358SVivek Prakash 				e_com = last_bl;
435*97c7d358SVivek Prakash 				dump_line();
436*97c7d358SVivek Prakash 
437*97c7d358SVivek Prakash 				*e_com++ = ' ';	/* add blanks for continuation */
438*97c7d358SVivek Prakash 				*e_com++ = ' ';
439*97c7d358SVivek Prakash 				*e_com++ = ' ';
440*97c7d358SVivek Prakash 
441*97c7d358SVivek Prakash 				t_ptr = last_bl + 1;
442*97c7d358SVivek Prakash 				last_bl = 0;
443*97c7d358SVivek Prakash 				if (t_ptr >= e_com) {
444*97c7d358SVivek Prakash 					while (*t_ptr == ' ' || *t_ptr == '\t')
445*97c7d358SVivek Prakash 						t_ptr++;
446*97c7d358SVivek Prakash 					while (*t_ptr != '\0') {	/* move unprinted part
447*97c7d358SVivek Prakash 									 * of comment down in
448*97c7d358SVivek Prakash 									 * buffer */
449*97c7d358SVivek Prakash 						if (*t_ptr == ' ' || *t_ptr == '\t')
450*97c7d358SVivek Prakash 							last_bl = e_com;
451*97c7d358SVivek Prakash 						*e_com++ = *t_ptr++;
452*97c7d358SVivek Prakash 					}
453*97c7d358SVivek Prakash 				}
454*97c7d358SVivek Prakash 				*e_com = '\0';
455*97c7d358SVivek Prakash 				now_col = count_spaces(ps.com_col, s_com);	/* recompute current
456*97c7d358SVivek Prakash 										 * position */
457*97c7d358SVivek Prakash 			}
458*97c7d358SVivek Prakash 			break;
459*97c7d358SVivek Prakash 		}
460*97c7d358SVivek Prakash 	}
461*97c7d358SVivek Prakash }
462