1 /* $NetBSD: pr_comment.c,v 1.8 2003/06/19 15:45:22 christos Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
34 * Copyright (c) 1985 Sun Microsystems, Inc.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/cdefs.h>
69 static char sccsid
[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
71 __RCSID("$NetBSD: pr_comment.c,v 1.8 2003/06/19 15:45:22 christos Exp $");
78 #include "indent_globs.h"
85 * This routine takes care of scanning and printing comments.
88 * 1) Decide where the comment should be aligned, and if lines should
90 * 2) If lines should not be broken and filled, just copy up to end of
92 * 3) If lines should be filled, then scan thru input_buffer copying
93 * characters to com_buf. Remember where the last blank, tab, or
94 * newline was. When line is filled, print up to last blank and
98 * November 1976 D A Willcox of CAC Initial coding
99 * 12/6/76 D A Willcox of CAC Modification to handle
100 * UNIX-style comments
105 * this routine processes comments. It makes an attempt to keep comments from
106 * going over the max line length. If a line is too long, it moves everything
107 * from the last blank to the next comment line. Blanks and tabs from the
108 * beginning of the input line are removed
115 int now_col
; /* column we are in now */
116 int adj_max_col
; /* Adjusted max_col for when we decide to
117 * spill comments over the right margin */
118 char *last_bl
; /* points to the last blank in the output
120 char *t_ptr
; /* used for moving string */
121 int unix_comment
; /* tri-state variable used to decide if it is
122 * a unix-style comment. 0 means only blanks
123 * since start, 1 means regular style comment,
124 * 2 means unix style comment */
125 int break_delim
= comment_delimiter_on_blankline
;
126 int l_just_saw_decl
= ps
.just_saw_decl
;
128 * int ps.last_nl = 0; / * true iff the last significant thing
129 * weve seen is a newline
131 int one_liner
= 1; /* true iff this comment is a one-liner */
132 adj_max_col
= max_col
;
133 ps
.just_saw_decl
= 0;
134 last_bl
= 0; /* no blanks found so far */
135 ps
.box_com
= false; /* at first, assume that we are not in a boxed
136 * comment or some other comment that should
138 ++ps
.out_coms
; /* keep track of number of comments */
139 unix_comment
= 1; /* set flag to let us figure out if there is a
140 * unix-style comment ** DISABLED: use 0 to
141 * reenable this hack! */
143 /* Figure where to align and how to treat the comment */
145 if (ps
.col_1
&& !format_col1_comments
) { /* if comment starts in
151 if (*buf_ptr
== '-' || *buf_ptr
== '*' || *buf_ptr
== '\n') {
152 ps
.box_com
= true; /* a comment with a '-', '*'
153 * or newline immediately
154 * after the start comment is
155 * assumed to be a boxed
159 if ( /* ps.bl_line && */ (s_lab
== e_lab
) && (s_code
== e_code
)) {
160 /* klg: check only if this line is blank */
162 * If this (*and previous lines are*) blank, dont put comment way
165 ps
.com_col
= (ps
.ind_level
- ps
.unindent_displace
) * ps
.ind_size
+ 1;
166 adj_max_col
= block_comment_max_col
;
168 ps
.com_col
= 1 + !format_col1_comments
;
172 if (s_code
!= e_code
)
173 target_col
= count_spaces(compute_code_target(), s_code
);
177 target_col
= count_spaces(compute_label_target(), s_lab
);
179 ps
.com_col
= ps
.decl_on_line
|| ps
.ind_level
== 0 ? ps
.decl_com_ind
: ps
.com_ind
;
180 if (ps
.com_col
< target_col
)
181 ps
.com_col
= ((target_col
+ 7) & ~7) + 1;
182 if (ps
.com_col
+ 24 > adj_max_col
)
183 adj_max_col
= ps
.com_col
+ 24;
188 ps
.n_comment_delta
= 1 - count_spaces(1, in_buffer
);
191 ps
.n_comment_delta
= 0;
192 while (*buf_ptr
== ' ' || *buf_ptr
== '\t')
195 ps
.comment_delta
= 0;
196 *e_com
++ = '/'; /* put '/' + '*' into buffer */
198 if (*buf_ptr
!= ' ' && !ps
.box_com
)
206 now_col
= count_spaces(ps
.com_col
, s_com
); /* figure what column we
208 * printed the comment
211 /* Start to copy the comment */
213 while (1) { /* this loop will go until the comment is
215 if (!iscntrl((unsigned char)*buf_ptr
) && *buf_ptr
!= '*')
218 switch (*buf_ptr
) { /* this checks for various spcl cases */
219 case 014: /* check for a form feed */
220 if (!ps
.box_com
) { /* in a text comment, break
223 /* fix so dump_line uses a form feed */
229 while (*++buf_ptr
== ' ' || *buf_ptr
== '\t');
231 if (++buf_ptr
>= buf_end
)
238 if (had_eof
) { /* check for unexpected eof */
239 printf("Unterminated comment\n");
245 if (ps
.box_com
|| ps
.last_nl
) { /* if this is a boxed
247 * ignore the newline */
248 if (s_com
== e_com
) {
253 if (!ps
.box_com
&& e_com
- s_com
> 3) {
254 if (break_delim
== 1 && s_com
[0] == '/'
255 && s_com
[1] == '*' && s_com
[2] == ' ') {
260 if (blanklines_before_blockcomments
)
261 prefix_blankline_requested
= 1;
264 s_com
[0] = s_com
[1] = s_com
[2] = ' ';
272 now_col
= ps
.com_col
;
275 if (unix_comment
!= 1) { /* we not are in
276 * unix_style comment */
277 if (unix_comment
== 0 && s_code
== e_code
) {
279 * if it is a UNIX-style comment, ignore the
280 * requirement that previous line be blank for
283 ps
.com_col
= (ps
.ind_level
- ps
.unindent_displace
) * ps
.ind_size
+ 1;
287 unix_comment
= 2; /* permanently remember
288 * that we are in this
292 now_col
= ps
.com_col
;
295 * fix so that the star at the start of the line will line
298 do /* flush leading white space */
299 if (++buf_ptr
>= buf_end
)
301 while (*buf_ptr
== ' ' || *buf_ptr
== '\t');
304 if (*(e_com
- 1) == ' ' || *(e_com
- 1) == '\t')
307 * if there was a space at the end of the last line, remember
310 else { /* otherwise, insert one */
317 ++line_no
; /* keep track of input line number */
320 do { /* flush any blanks and/or tabs at
321 * start of next line */
322 if (++buf_ptr
>= buf_end
)
324 if (*buf_ptr
== '*' && --nstar
>= 0) {
325 if (++buf_ptr
>= buf_end
)
330 } while (*buf_ptr
== ' ' || *buf_ptr
== '\t');
332 if (++buf_ptr
>= buf_end
)
334 break; /* end of case for newline */
336 case '*': /* must check for possibility of being at end
338 if (++buf_ptr
>= buf_end
) /* get to next char
342 if (unix_comment
== 0) /* set flag to show we are not
343 * in unix-style comment */
346 if (*buf_ptr
== '/') { /* it is the end!!! */
348 if (++buf_ptr
>= buf_end
)
351 if (*(e_com
- 1) != ' ' && !ps
.box_com
) { /* insure blank before
356 if (break_delim
== 1 && !one_liner
&& s_com
[0] == '/'
357 && s_com
[1] == '*' && s_com
[2] == ' ') {
362 if (blanklines_before_blockcomments
)
363 prefix_blankline_requested
= 1;
366 s_com
[0] = s_com
[1] = s_com
[2] = ' ';
368 if (break_delim
== 2 && e_com
> s_com
+ 3
369 /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
372 now_col
= ps
.com_col
;
378 ps
.just_saw_decl
= l_just_saw_decl
;
380 } else {/* handle isolated '*' */
385 default: /* we have a random char */
386 if (unix_comment
== 0 && *buf_ptr
!= ' ' && *buf_ptr
!= '\t')
387 unix_comment
= 1; /* we are not in
388 * unix-style comment */
391 if (buf_ptr
>= buf_end
)
394 if (*e_com
== '\t') /* keep track of column */
395 now_col
= ((now_col
- 1) & tabmask
) + tabsize
+ 1;
397 if (*e_com
== '\b') /* this is a backspace */
402 if (*e_com
== ' ' || *e_com
== '\t')
404 /* remember we saw a blank */
407 if (now_col
> adj_max_col
&& !ps
.box_com
&& unix_comment
== 1
408 && !iscntrl((unsigned char)e_com
[-1])
409 && !isblank((unsigned char)e_com
[-1])) {
411 * the comment is too long, it must be broken up
413 if (break_delim
== 1 && s_com
[0] == '/'
414 && s_com
[1] == '*' && s_com
[2] == ' ') {
419 if (blanklines_before_blockcomments
)
420 prefix_blankline_requested
= 1;
423 s_com
[0] = s_com
[1] = s_com
[2] = ' ';
425 if (last_bl
== 0) { /* we have seen no
427 last_bl
= e_com
; /* fake it */
430 *e_com
= '\0'; /* print what we have */
432 while (last_bl
> s_com
&& iscntrl((unsigned char)last_bl
[-1]) )
437 *e_com
++ = ' '; /* add blanks for continuation */
443 if (t_ptr
>= e_com
) {
444 while (*t_ptr
== ' ' || *t_ptr
== '\t')
446 while (*t_ptr
!= '\0') { /* move unprinted part
449 if (*t_ptr
== ' ' || *t_ptr
== '\t')
455 now_col
= count_spaces(ps
.com_col
, s_com
); /* recompute current