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 * 4. 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
31 * @(#)pr_comment.c 8.1 (Berkeley) 6/6/93
32 * $FreeBSD: src/usr.bin/indent/pr_comment.c,v 1.8 2003/06/15 09:28:17 charnier Exp $
38 #include "indent_globs.h"
45 * This routine takes care of scanning and printing comments.
48 * 1) Decide where the comment should be aligned, and if lines should
50 * 2) If lines should not be broken and filled, just copy up to end of
52 * 3) If lines should be filled, then scan thru input_buffer copying
53 * characters to com_buf. Remember where the last blank, tab, or
54 * newline was. When line is filled, print up to last blank and
58 * November 1976 D A Willcox of CAC Initial coding
59 * 12/6/76 D A Willcox of CAC Modification to handle
65 * this routine processes comments. It makes an attempt to keep comments from
66 * going over the max line length. If a line is too long, it moves everything
67 * from the last blank to the next comment line. Blanks and tabs from the
68 * beginning of the input line are removed
74 int now_col; /* column we are in now */
75 int adj_max_col; /* Adjusted max_col for when we decide to
76 * spill comments over the right margin */
77 char *last_bl; /* points to the last blank in the output
79 char *t_ptr; /* used for moving string */
80 int unix_comment; /* tri-state variable used to decide if it is
81 * a unix-style comment. 0 means only blanks
82 * since /+*, 1 means regular style comment, 2
83 * means unix style comment */
84 int break_delim = comment_delimiter_on_blankline;
85 int l_just_saw_decl = ps.just_saw_decl;
87 * int ps.last_nl = 0; true iff the last significant thing
88 * weve seen is a newline
90 int one_liner = 1; /* true iff this comment is a one-liner */
91 adj_max_col = max_col;
93 last_bl = NULL; /* no blanks found so far */
94 ps.box_com = false; /* at first, assume that we are not in
95 * a boxed comment or some other
96 * comment that should not be touched */
97 ++ps.out_coms; /* keep track of number of comments */
98 unix_comment = 1; /* set flag to let us figure out if there is a
99 * unix-style comment ** DISABLED: use 0 to
100 * reenable this hack! */
102 /* Figure where to align and how to treat the comment */
104 if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
105 * 1 it should not be touched */
110 if (*buf_ptr == '-' || *buf_ptr == '*' ||
111 (*buf_ptr == '\n' && !format_block_comments)) {
112 ps.box_com = true; /* A comment with a '-' or '*' immediately
113 * after the /+* is assumed to be a boxed
114 * comment. A comment with a newline
115 * immediately after the /+* is assumed to
116 * be a block comment and is treated as a
117 * box comment unless format_block_comments
118 * is nonzero (the default). */
121 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
122 /* klg: check only if this line is blank */
124 * If this (*and previous lines are*) blank, dont put comment way
127 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
128 adj_max_col = block_comment_max_col;
130 ps.com_col = 1 + !format_col1_comments;
135 if (s_code != e_code)
136 target_col = count_spaces(compute_code_target(), s_code);
140 target_col = count_spaces(compute_label_target(), s_lab);
142 ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
143 if (ps.com_col < target_col)
144 ps.com_col = ((target_col + 7) & ~7) + 1;
145 if (ps.com_col + 24 > adj_max_col)
146 adj_max_col = ps.com_col + 24;
151 ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
155 ps.n_comment_delta = 0;
156 while (*buf_ptr == ' ' || *buf_ptr == '\t')
159 ps.comment_delta = 0;
160 *e_com++ = '/'; /* put '/' followed by '*' into buffer */
162 if (*buf_ptr != ' ' && !ps.box_com)
171 now_col = count_spaces(ps.com_col, s_com); /* figure what column we
173 * printed the comment
176 /* Start to copy the comment */
178 while (1) { /* this loop will go until the comment is
180 if (*buf_ptr > 040 && *buf_ptr != '*')
183 switch (*buf_ptr) { /* this checks for various spcl cases */
184 case 014: /* check for a form feed */
185 if (!ps.box_com) { /* in a text comment, break the line here */
187 /* fix so dump_line uses a form feed */
193 while (*++buf_ptr == ' ' || *buf_ptr == '\t');
196 if (++buf_ptr >= buf_end)
203 if (had_eof) { /* check for unexpected eof */
204 printf("Unterminated comment\n");
210 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
211 * we dont ignore the newline */
212 if (s_com == e_com) {
217 if (!ps.box_com && e_com - s_com > 3) {
218 if (break_delim == 1 && s_com[0] == '/'
219 && s_com[1] == '*' && s_com[2] == ' ') {
224 if (blanklines_before_blockcomments)
225 prefix_blankline_requested = 1;
228 s_com[0] = s_com[1] = s_com[2] = ' ';
236 now_col = ps.com_col;
240 if (unix_comment != 1) { /* we not are in unix_style
242 if (unix_comment == 0 && s_code == e_code) {
244 * if it is a UNIX-style comment, ignore the
245 * requirement that previous line be blank for
248 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
252 unix_comment = 2; /* permanently remember that we are in
253 * this type of comment */
256 now_col = ps.com_col;
259 * fix so that the star at the start of the line will line
262 do /* flush leading white space */
263 if (++buf_ptr >= buf_end)
265 while (*buf_ptr == ' ' || *buf_ptr == '\t');
268 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
271 * if there was a space at the end of the last line, remember
274 else { /* otherwise, insert one */
281 ++line_no; /* keep track of input line number */
284 do { /* flush any blanks and/or tabs at start of
286 if (++buf_ptr >= buf_end)
288 if (*buf_ptr == '*' && --nstar >= 0) {
289 if (++buf_ptr >= buf_end)
294 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
296 else if (++buf_ptr >= buf_end)
298 break; /* end of case for newline */
300 case '*': /* must check for possibility of being at end
302 if (++buf_ptr >= buf_end) /* get to next char after * */
305 if (unix_comment == 0) /* set flag to show we are not in
306 * unix-style comment */
309 if (*buf_ptr == '/') { /* it is the end!!! */
311 if (++buf_ptr >= buf_end)
314 if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before
319 if (break_delim == 1 && !one_liner && s_com[0] == '/'
320 && s_com[1] == '*' && s_com[2] == ' ') {
325 if (blanklines_before_blockcomments)
326 prefix_blankline_requested = 1;
329 s_com[0] = s_com[1] = s_com[2] = ' ';
331 if (break_delim == 2 && e_com > s_com + 3
332 /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
335 now_col = ps.com_col;
341 ps.just_saw_decl = l_just_saw_decl;
344 else { /* handle isolated '*' */
349 default: /* we have a random char */
350 if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
351 unix_comment = 1; /* we are not in unix-style comment */
354 if (buf_ptr >= buf_end)
357 if (*e_com == '\t') /* keep track of column */
358 now_col = ((now_col - 1) & tabmask) + tabsize + 1;
359 else if (*e_com == '\b') /* this is a backspace */
364 if (*e_com == ' ' || *e_com == '\t')
366 /* remember we saw a blank */
369 if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
371 * the comment is too long, it must be broken up
373 if (break_delim == 1 && s_com[0] == '/'
374 && s_com[1] == '*' && s_com[2] == ' ') {
379 if (blanklines_before_blockcomments)
380 prefix_blankline_requested = 1;
383 s_com[0] = s_com[1] = s_com[2] = ' ';
385 if (last_bl == NULL) { /* we have seen no blanks */
386 last_bl = e_com; /* fake it */
389 *e_com = '\0'; /* print what we have */
391 while (last_bl > s_com && last_bl[-1] < 040)
396 *e_com++ = ' '; /* add blanks for continuation */
402 if (t_ptr >= e_com) {
403 while (*t_ptr == ' ' || *t_ptr == '\t')
405 while (*t_ptr != '\0') { /* move unprinted part of
406 * comment down in buffer */
407 if (*t_ptr == ' ' || *t_ptr == '\t')
413 now_col = count_spaces(ps.com_col, s_com); /* recompute current