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