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