2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. 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.
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
37 static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
44 #include "indent_globs.h"
51 * This routine takes care of scanning and printing comments.
54 * 1) Decide where the comment should be aligned, and if lines should
56 * 2) If lines should not be broken and filled, just copy up to end of
58 * 3) If lines should be filled, then scan thru input_buffer copying
59 * characters to com_buf. Remember where the last blank, tab, or
60 * newline was. When line is filled, print up to last blank and
64 * November 1976 D A Willcox of CAC Initial coding
65 * 12/6/76 D A Willcox of CAC Modification to handle
71 * this routine processes comments. It makes an attempt to keep comments from
72 * going over the max line length. If a line is too long, it moves everything
73 * from the last blank to the next comment line. Blanks and tabs from the
74 * beginning of the input line are removed
80 int now_col; /* column we are in now */
81 int adj_max_col; /* Adjusted max_col for when we decide to
82 * spill comments over the right margin */
83 char *last_bl; /* points to the last blank in the output
85 char *t_ptr; /* used for moving string */
86 int unix_comment; /* tri-state variable used to decide if it is
87 * a unix-style comment. 0 means only blanks
88 * since /+*, 1 means regular style comment, 2
89 * means unix style comment */
90 int break_delim = comment_delimiter_on_blankline;
91 int l_just_saw_decl = ps.just_saw_decl;
93 * int ps.last_nl = 0; true iff the last significant thing
94 * weve seen is a newline
96 int one_liner = 1; /* true iff this comment is a one-liner */
97 adj_max_col = max_col;
99 last_bl = 0; /* no blanks found so far */
100 ps.box_com = false; /* at first, assume that we are not in
101 * a boxed comment or some other
102 * comment that should not be touched */
103 ++ps.out_coms; /* keep track of number of comments */
104 unix_comment = 1; /* set flag to let us figure out if there is a
105 * unix-style comment ** DISABLED: use 0 to
106 * reenable this hack! */
108 /* Figure where to align and how to treat the comment */
110 if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
111 * 1 it should not be touched */
116 if (*buf_ptr == '-' || *buf_ptr == '*' ||
117 (*buf_ptr == '\n' && !format_block_comments)) {
118 ps.box_com = true; /* A comment with a '-' or '*' immediately
119 * after the /+* is assumed to be a boxed
120 * comment. A comment with a newline
121 * immediately after the /+* is assumed to
122 * be a block comment and is treated as a
123 * box comment unless format_block_comments
124 * is nonzero (the default). */
127 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
128 /* klg: check only if this line is blank */
130 * If this (*and previous lines are*) blank, dont put comment way
133 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
134 adj_max_col = block_comment_max_col;
136 ps.com_col = 1 + !format_col1_comments;
139 register int target_col;
141 if (s_code != e_code)
142 target_col = count_spaces(compute_code_target(), s_code);
146 target_col = count_spaces(compute_label_target(), s_lab);
148 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
149 if (ps.com_col < target_col)
150 ps.com_col = ((target_col + 7) & ~7) + 1;
151 if (ps.com_col + 24 > adj_max_col)
152 adj_max_col = ps.com_col + 24;
157 ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
161 ps.n_comment_delta = 0;
162 while (*buf_ptr == ' ' || *buf_ptr == '\t')
165 ps.comment_delta = 0;
166 *e_com++ = '/'; /* put '/' followed by '*' into buffer */
168 if (*buf_ptr != ' ' && !ps.box_com)
177 now_col = count_spaces(ps.com_col, s_com); /* figure what column we
179 * printed the comment
182 /* Start to copy the comment */
184 while (1) { /* this loop will go until the comment is
186 if (*buf_ptr > 040 && *buf_ptr != '*')
189 switch (*buf_ptr) { /* this checks for various spcl cases */
190 case 014: /* check for a form feed */
191 if (!ps.box_com) { /* in a text comment, break the line here */
193 /* fix so dump_line uses a form feed */
199 while (*++buf_ptr == ' ' || *buf_ptr == '\t');
202 if (++buf_ptr >= buf_end)
209 if (had_eof) { /* check for unexpected eof */
210 printf("Unterminated comment\n");
216 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
217 * we dont ignore the newline */
218 if (s_com == e_com) {
223 if (!ps.box_com && e_com - s_com > 3) {
224 if (break_delim == 1 && s_com[0] == '/'
225 && s_com[1] == '*' && s_com[2] == ' ') {
230 if (blanklines_before_blockcomments)
231 prefix_blankline_requested = 1;
234 s_com[0] = s_com[1] = s_com[2] = ' ';
242 now_col = ps.com_col;
246 if (unix_comment != 1) { /* we not are in unix_style
248 if (unix_comment == 0 && s_code == e_code) {
250 * if it is a UNIX-style comment, ignore the
251 * requirement that previous line be blank for
254 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
258 unix_comment = 2; /* permanently remember that we are in
259 * this type of comment */
262 now_col = ps.com_col;
265 * fix so that the star at the start of the line will line
268 do /* flush leading white space */
269 if (++buf_ptr >= buf_end)
271 while (*buf_ptr == ' ' || *buf_ptr == '\t');
274 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
277 * if there was a space at the end of the last line, remember
280 else { /* otherwise, insert one */
287 ++line_no; /* keep track of input line number */
290 do { /* flush any blanks and/or tabs at start of
292 if (++buf_ptr >= buf_end)
294 if (*buf_ptr == '*' && --nstar >= 0) {
295 if (++buf_ptr >= buf_end)
300 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
302 else if (++buf_ptr >= buf_end)
304 break; /* end of case for newline */
306 case '*': /* must check for possibility of being at end
308 if (++buf_ptr >= buf_end) /* get to next char after * */
311 if (unix_comment == 0) /* set flag to show we are not in
312 * unix-style comment */
315 if (*buf_ptr == '/') { /* it is the end!!! */
317 if (++buf_ptr >= buf_end)
320 if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before
325 if (break_delim == 1 && !one_liner && s_com[0] == '/'
326 && s_com[1] == '*' && s_com[2] == ' ') {
331 if (blanklines_before_blockcomments)
332 prefix_blankline_requested = 1;
335 s_com[0] = s_com[1] = s_com[2] = ' ';
337 if (break_delim == 2 && e_com > s_com + 3
338 /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
341 now_col = ps.com_col;
347 ps.just_saw_decl = l_just_saw_decl;
350 else { /* handle isolated '*' */
355 default: /* we have a random char */
356 if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
357 unix_comment = 1; /* we are not in unix-style comment */
360 if (buf_ptr >= buf_end)
363 if (*e_com == '\t') /* keep track of column */
364 now_col = ((now_col - 1) & tabmask) + tabsize + 1;
365 else if (*e_com == '\b') /* this is a backspace */
370 if (*e_com == ' ' || *e_com == '\t')
372 /* remember we saw a blank */
375 if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
377 * the comment is too long, it must be broken up
379 if (break_delim == 1 && s_com[0] == '/'
380 && s_com[1] == '*' && s_com[2] == ' ') {
385 if (blanklines_before_blockcomments)
386 prefix_blankline_requested = 1;
389 s_com[0] = s_com[1] = s_com[2] = ' ';
391 if (last_bl == 0) { /* we have seen no blanks */
392 last_bl = e_com; /* fake it */
395 *e_com = '\0'; /* print what we have */
397 while (last_bl > s_com && last_bl[-1] < 040)
402 *e_com++ = ' '; /* add blanks for continuation */
408 if (t_ptr >= e_com) {
409 while (*t_ptr == ' ' || *t_ptr == '\t')
411 while (*t_ptr != '\0') { /* move unprinted part of
412 * comment down in buffer */
413 if (*t_ptr == ' ' || *t_ptr == '\t')
419 now_col = count_spaces(ps.com_col, s_com); /* recompute current