2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
4 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley, the University of Illinois,
13 * Urbana, and Sun Microsystems, Inc. The name of either University
14 * or Sun Microsystems may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 * FUNCTION: This routine takes care of scanning and printing comments.
26 * ALGORITHM: 1) Decide where the comment should be aligned, and if lines should
27 * be broken. 2) If lines should not be broken and filled, just copy up to
28 * end of comment. 3) If lines should be filled, then scan thru input_buffer
29 * copying characters to com_buf. Remember where the last blank, tab, or
30 * newline was. When line is filled, print up to last blank and continue
33 * HISTORY: November 1976 D A Willcox of CAC Initial coding 12/6/76
34 * A Willcox of CAC Modification to handle UNIX-style comments
39 * this routine processes comments. It makes an attempt to keep comments
40 * from going over the max line length. If a line is too long, it moves
41 * everything from the last blank to the next comment line. Blanks and tabs
42 * from the beginning of the input line are removed
54 int now_col
; /* column we are in now */
56 int adj_max_col
; /* Adjusted max_col for when we
57 * decide to spill comments
58 * over the right margin
61 char *last_bl
; /* points to the last blank in
65 char *t_ptr
; /* used for moving string */
67 int unix_comment
; /* tri-state variable used to
68 * decide if it is a unix-style
69 * comment. 0 means only blanks
70 * since / *, 1 means regular
71 * style comment, 2 means unix
75 int break_delim
= del_on_bl
;
76 int l_just_saw_decl
= ps
.just_saw_decl
;
77 /* int ps.last_nl = 0; */ /* true iff the last
78 sig thing we have seen is a nl */
80 int one_liner
= 1; /* true iff this comment is a
82 adj_max_col
= max_col
;
84 last_bl
= 0; /* no blanks found so far */
85 ps
.box_com
= false; /* at first, assume that we are
86 not in a boxed comment or
87 some other comment that
88 should not be touched */
89 ++ps
.out_coms
; /* keep track of number of
91 unix_comment
= 1; /* set flag to let us figure
92 out if there is a unix-style
93 comment ** DISABLED: use 0
94 to reenable this hack! */
96 /* Figure where to align and how to treat the comment */
98 if (ps
.col_1
&& !format_col1_comments
)
99 { /* if comment starts in column
100 1 it should not be touched */
105 if (*buf_ptr
== '-' || *buf_ptr
== '*')
107 ps
.box_com
= true; /* a comment with a '-' or '*'
108 immediately after the / * is
109 assumed to be a boxed
113 if ( /* ps.bl_line && */ (s_lab
== e_lab
) && (s_code
== e_code
))
115 /* klg: check only if this line is blank */
116 /* If this (*and previous lines are*) blank, dont put comment
118 ps
.com_col
= (ps
.ind_level
- ps
.unindent_displace
) * ps
.ind_size
+ 1;
119 adj_max_col
= bk_max_col
;
121 ps
.com_col
= 1 + !format_col1_comments
;
126 if (s_code
!= e_code
)
127 target_col
= count_spaces(code_target(), s_code
);
132 target_col
= count_spaces(label_target(), s_lab
);
134 ps
.com_col
= ps
.decl_on_line
|| ps
.ind_level
== 0 ? ps
.decl_com_ind
: ps
.com_ind
;
135 if (ps
.com_col
< target_col
)
136 ps
.com_col
= ((target_col
+ 7) & ~7) + 1;
137 if (ps
.com_col
+ 24 > adj_max_col
)
138 adj_max_col
= ps
.com_col
+ 24;
144 ps
.n_comment_delta
= 1 - count_spaces(1, in_buffer
);
148 ps
.n_comment_delta
= 0;
149 while (*buf_ptr
== ' ' || *buf_ptr
== '\t')
152 ps
.comment_delta
= 0;
153 *e_com
++ = '/'; /* put '/ *' into buffer */
155 if (*buf_ptr
!= ' ' && !ps
.box_com
)
164 now_col
= count_spaces(ps
.com_col
, s_com
); /* figure what column we
169 /* Start to copy the comment */
172 { /* this loop will go until the
174 if (*buf_ptr
> 040 && *buf_ptr
!= '*')
178 register nsize
= l_com
- s_com
+ 400;
179 combuf
= (char *) realloc(combuf
, nsize
);
180 e_com
= combuf
+ (e_com
- s_com
) + 1;
181 l_com
= combuf
+ nsize
- 5;
185 { /* this checks for various spcl
187 case 014: /* check for a form feed */
189 { /* in a text comment, break the
192 /* fix so dump_line uses a form feed */
198 while (*++buf_ptr
== ' ' || *buf_ptr
== '\t');
201 if (++buf_ptr
>= buf_end
)
209 { /* check for unexpected eof */
210 printf("Unterminated comment\n");
216 if (ps
.box_com
|| ps
.last_nl
)
217 { /* if this is a boxed comment,
218 we dont ignore the newline */
225 if (!ps
.box_com
&& e_com
- s_com
> 3)
227 if (break_delim
== 1 && s_com
[0] == '/'
228 && s_com
[1] == '*' && s_com
[2] == ' ')
235 prefix_blankline_requested
= 1;
238 s_com
[0] = s_com
[1] = s_com
[2] = ' ';
243 register nsize
= l_com
- s_com
+ 400;
244 combuf
= (char *) realloc(combuf
, nsize
);
245 e_com
= combuf
+ (e_com
- s_com
) + 1;
246 l_com
= combuf
+ nsize
- 5;
253 now_col
= ps
.com_col
;
257 if (unix_comment
!= 1)
258 { /* we not are in unix_style
260 if (unix_comment
== 0 && s_code
== e_code
)
262 /* if it is a UNIX-style comment, ignore the
263 requirement that previous line be blank for
265 ps
.com_col
= (ps
.ind_level
- ps
.unindent_displace
) * ps
.ind_size
+ 1;
269 unix_comment
= 2; /* permanently remember that we
270 are in this type of comment */
273 now_col
= ps
.com_col
;
275 /* fix so that the star at the start of the line will
277 do /* flush leading white space */
278 if (++buf_ptr
>= buf_end
)
280 while (*buf_ptr
== ' ' || *buf_ptr
== '\t');
283 if (*(e_com
- 1) == ' ' || *(e_com
- 1) == '\t')
285 /* if there was a space at the end of the last line,
286 remember where it was */
288 { /* otherwise, insert one */
293 register nsize
= l_com
- s_com
+ 400;
294 combuf
= (char *) realloc(combuf
, nsize
);
295 e_com
= combuf
+ (e_com
- s_com
) + 1;
296 l_com
= combuf
+ nsize
- 5;
302 ++line_no
; /* keep track of input line
308 { /* flush any blanks and/or tabs
309 at start of next line */
310 if (++buf_ptr
>= buf_end
)
312 if (*buf_ptr
== '*' && --nstar
>= 0)
314 if (++buf_ptr
>= buf_end
)
319 } while (*buf_ptr
== ' ' || *buf_ptr
== '\t');
320 } else if (++buf_ptr
>= buf_end
)
322 break; /* end of case for newline */
324 case '*': /* must check for possibility
325 of being at end of comment */
326 if (++buf_ptr
>= buf_end
) /* get to next char after * */
329 if (unix_comment
== 0) /* set flag to show we are not
330 in unix-style comment */
334 { /* it is the end!!! */
336 if (++buf_ptr
>= buf_end
)
339 if (*(e_com
- 1) != ' ' && !ps
.box_com
)
340 { /* insure blank before end */
344 if (break_delim
== 1 && !one_liner
&& s_com
[0] == '/'
345 && s_com
[1] == '*' && s_com
[2] == ' ')
352 prefix_blankline_requested
= 1;
355 s_com
[0] = s_com
[1] = s_com
[2] = ' ';
357 if (break_delim
== 2 && e_com
> s_com
+ 3
358 /* now_col > adj_max_col - 2 && !ps.box_com */ )
362 now_col
= ps
.com_col
;
366 register nsize
= l_com
- s_com
+ 400;
367 combuf
= (char *) realloc(combuf
, nsize
);
368 e_com
= combuf
+ (e_com
- s_com
) + 1;
369 l_com
= combuf
+ nsize
- 5;
375 ps
.just_saw_decl
= l_just_saw_decl
;
378 { /* handle isolated '*' */
383 default: /* we have a random char */
384 if (unix_comment
== 0 && *buf_ptr
!= ' ' && *buf_ptr
!= '\t')
385 unix_comment
= 1; /* we are not in unix-style
389 if (buf_ptr
>= buf_end
)
392 if (*e_com
== '\t') /* keep track of column */
393 now_col
= ((now_col
- 1) & tabmask
) + tabsize
+ 1;
394 else if (*e_com
== '\b') /* this is a backspace */
399 if (*e_com
== ' ' || *e_com
== '\t')
401 /* remember we saw a blank */
404 if (now_col
> adj_max_col
&& !ps
.box_com
&& unix_comment
== 1 && e_com
[-1] > ' ')
406 /* the comment is too long, it must be broken up */
407 if (break_delim
== 1 && s_com
[0] == '/'
408 && s_com
[1] == '*' && s_com
[2] == ' ')
415 prefix_blankline_requested
= 1;
418 s_com
[0] = s_com
[1] = s_com
[2] = ' ';
421 { /* we have seen no blanks */
422 last_bl
= e_com
; /* fake it */
425 *e_com
= '\0'; /* print what we have */
427 while (last_bl
> s_com
&& last_bl
[-1] < 040)
432 *e_com
++ = ' '; /* add blanks for continuation */
440 while (*t_ptr
== ' ' || *t_ptr
== '\t')
442 while (*t_ptr
!= '\0')
443 { /* move unprinted part of
444 comment down in buffer */
445 if (*t_ptr
== ' ' || *t_ptr
== '\t')
451 now_col
= count_spaces(ps
.com_col
, s_com
); /* recompute current