2016-08-01 16:40:42 +00:00
|
|
|
|
/*-
|
1994-05-27 12:33:43 +00:00
|
|
|
|
* Copyright (c) 1985 Sun Microsystems, Inc.
|
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
2002-06-30 05:25:07 +00:00
|
|
|
|
|
|
|
|
|
#if 0
|
2003-06-15 09:28:17 +00:00
|
|
|
|
#ifndef lint
|
1994-05-27 12:33:43 +00:00
|
|
|
|
static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
|
2002-06-30 05:25:07 +00:00
|
|
|
|
#endif /* not lint */
|
2003-06-15 09:28:17 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2002-06-30 05:25:07 +00:00
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
2003-06-15 09:28:17 +00:00
|
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2016-07-31 20:04:18 +00:00
|
|
|
|
#include <string.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
#include "indent_globs.h"
|
2001-10-28 18:35:32 +00:00
|
|
|
|
#include "indent.h"
|
1994-05-27 12:33:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* NAME:
|
|
|
|
|
* pr_comment
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION:
|
|
|
|
|
* This routine takes care of scanning and printing comments.
|
|
|
|
|
*
|
|
|
|
|
* ALGORITHM:
|
|
|
|
|
* 1) Decide where the comment should be aligned, and if lines should
|
|
|
|
|
* be broken.
|
|
|
|
|
* 2) If lines should not be broken and filled, just copy up to end of
|
|
|
|
|
* comment.
|
|
|
|
|
* 3) If lines should be filled, then scan thru input_buffer copying
|
|
|
|
|
* characters to com_buf. Remember where the last blank, tab, or
|
|
|
|
|
* newline was. When line is filled, print up to last blank and
|
|
|
|
|
* continue copying.
|
|
|
|
|
*
|
|
|
|
|
* HISTORY:
|
|
|
|
|
* November 1976 D A Willcox of CAC Initial coding
|
|
|
|
|
* 12/6/76 D A Willcox of CAC Modification to handle
|
|
|
|
|
* UNIX-style comments
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* this routine processes comments. It makes an attempt to keep comments from
|
|
|
|
|
* going over the max line length. If a line is too long, it moves everything
|
|
|
|
|
* from the last blank to the next comment line. Blanks and tabs from the
|
|
|
|
|
* beginning of the input line are removed
|
|
|
|
|
*/
|
|
|
|
|
|
2001-10-28 18:35:32 +00:00
|
|
|
|
void
|
|
|
|
|
pr_comment(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
{
|
|
|
|
|
int now_col; /* column we are in now */
|
|
|
|
|
int adj_max_col; /* Adjusted max_col for when we decide to
|
|
|
|
|
* spill comments over the right margin */
|
|
|
|
|
char *last_bl; /* points to the last blank in the output
|
|
|
|
|
* buffer */
|
|
|
|
|
char *t_ptr; /* used for moving string */
|
|
|
|
|
int break_delim = comment_delimiter_on_blankline;
|
|
|
|
|
int l_just_saw_decl = ps.just_saw_decl;
|
|
|
|
|
adj_max_col = max_col;
|
|
|
|
|
ps.just_saw_decl = 0;
|
2016-07-29 19:36:10 +00:00
|
|
|
|
last_bl = NULL; /* no blanks found so far */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ps.box_com = false; /* at first, assume that we are not in
|
|
|
|
|
* a boxed comment or some other
|
|
|
|
|
* comment that should not be touched */
|
|
|
|
|
++ps.out_coms; /* keep track of number of comments */
|
|
|
|
|
|
|
|
|
|
/* Figure where to align and how to treat the comment */
|
|
|
|
|
|
|
|
|
|
if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
|
|
|
|
|
* 1 it should not be touched */
|
|
|
|
|
ps.box_com = true;
|
2016-07-31 20:04:18 +00:00
|
|
|
|
break_delim = false;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ps.com_col = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2000-12-09 09:45:09 +00:00
|
|
|
|
if (*buf_ptr == '-' || *buf_ptr == '*' ||
|
|
|
|
|
(*buf_ptr == '\n' && !format_block_comments)) {
|
|
|
|
|
ps.box_com = true; /* A comment with a '-' or '*' immediately
|
2001-10-28 18:35:32 +00:00
|
|
|
|
* after the /+* is assumed to be a boxed
|
2000-12-09 09:45:09 +00:00
|
|
|
|
* comment. A comment with a newline
|
2001-10-28 18:35:32 +00:00
|
|
|
|
* immediately after the /+* is assumed to
|
2000-12-09 09:45:09 +00:00
|
|
|
|
* be a block comment and is treated as a
|
|
|
|
|
* box comment unless format_block_comments
|
|
|
|
|
* is nonzero (the default). */
|
2016-07-31 20:04:18 +00:00
|
|
|
|
break_delim = false;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
|
|
|
|
|
/* klg: check only if this line is blank */
|
|
|
|
|
/*
|
|
|
|
|
* If this (*and previous lines are*) blank, dont put comment way
|
|
|
|
|
* out at left
|
|
|
|
|
*/
|
|
|
|
|
ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
|
|
|
|
|
adj_max_col = block_comment_max_col;
|
|
|
|
|
if (ps.com_col <= 1)
|
|
|
|
|
ps.com_col = 1 + !format_col1_comments;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2002-06-24 17:40:27 +00:00
|
|
|
|
int target_col;
|
2016-07-31 20:04:18 +00:00
|
|
|
|
break_delim = false;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
if (s_code != e_code)
|
|
|
|
|
target_col = count_spaces(compute_code_target(), s_code);
|
|
|
|
|
else {
|
|
|
|
|
target_col = 1;
|
|
|
|
|
if (s_lab != e_lab)
|
|
|
|
|
target_col = count_spaces(compute_label_target(), s_lab);
|
|
|
|
|
}
|
|
|
|
|
ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
|
|
|
|
|
if (ps.com_col < target_col)
|
|
|
|
|
ps.com_col = ((target_col + 7) & ~7) + 1;
|
|
|
|
|
if (ps.com_col + 24 > adj_max_col)
|
|
|
|
|
adj_max_col = ps.com_col + 24;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ps.box_com) {
|
|
|
|
|
buf_ptr[-2] = 0;
|
|
|
|
|
ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
|
|
|
|
|
buf_ptr[-2] = '/';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ps.n_comment_delta = 0;
|
|
|
|
|
while (*buf_ptr == ' ' || *buf_ptr == '\t')
|
|
|
|
|
buf_ptr++;
|
|
|
|
|
}
|
|
|
|
|
ps.comment_delta = 0;
|
2001-10-28 18:35:32 +00:00
|
|
|
|
*e_com++ = '/'; /* put '/' followed by '*' into buffer */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
*e_com++ = '*';
|
|
|
|
|
if (*buf_ptr != ' ' && !ps.box_com)
|
|
|
|
|
*e_com++ = ' ';
|
|
|
|
|
|
2016-07-31 20:04:18 +00:00
|
|
|
|
/* Don't put a break delimiter if this comment is a one-liner */
|
|
|
|
|
for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
|
|
|
|
|
if (t_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
|
|
|
|
if (t_ptr[0] == '*' && t_ptr[1] == '/') {
|
|
|
|
|
break_delim = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (break_delim) {
|
|
|
|
|
char *t = e_com;
|
|
|
|
|
e_com = s_com + 2;
|
|
|
|
|
*e_com = 0;
|
|
|
|
|
if (blanklines_before_blockcomments)
|
|
|
|
|
prefix_blankline_requested = 1;
|
|
|
|
|
dump_line();
|
2016-07-31 21:09:22 +00:00
|
|
|
|
e_com = s_com = t;
|
|
|
|
|
if (!ps.box_com && star_comment_cont)
|
|
|
|
|
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
|
2016-07-31 20:04:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (troff)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
adj_max_col = 80;
|
|
|
|
|
|
|
|
|
|
/* Start to copy the comment */
|
|
|
|
|
|
|
|
|
|
while (1) { /* this loop will go until the comment is
|
|
|
|
|
* copied */
|
|
|
|
|
CHECK_SIZE_COM;
|
|
|
|
|
switch (*buf_ptr) { /* this checks for various spcl cases */
|
|
|
|
|
case 014: /* check for a form feed */
|
|
|
|
|
if (!ps.box_com) { /* in a text comment, break the line here */
|
|
|
|
|
ps.use_ff = true;
|
|
|
|
|
/* fix so dump_line uses a form feed */
|
|
|
|
|
dump_line();
|
2016-07-29 19:36:10 +00:00
|
|
|
|
last_bl = NULL;
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (!ps.box_com && star_comment_cont)
|
|
|
|
|
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
|
|
|
|
|
while (*++buf_ptr == ' ' || *buf_ptr == '\t')
|
|
|
|
|
;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
|
|
|
|
*e_com++ = 014;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
|
if (had_eof) { /* check for unexpected eof */
|
|
|
|
|
printf("Unterminated comment\n");
|
|
|
|
|
dump_line();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-31 20:13:00 +00:00
|
|
|
|
last_bl = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
|
|
|
|
|
* we dont ignore the newline */
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (s_com == e_com)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
*e_com++ = ' ';
|
|
|
|
|
if (!ps.box_com && e_com - s_com > 3) {
|
|
|
|
|
dump_line();
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (star_comment_cont)
|
|
|
|
|
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
dump_line();
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (!ps.box_com && star_comment_cont)
|
|
|
|
|
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ps.last_nl = 1;
|
|
|
|
|
if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
|
|
|
|
|
last_bl = e_com - 1;
|
|
|
|
|
/*
|
|
|
|
|
* if there was a space at the end of the last line, remember
|
|
|
|
|
* where it was
|
|
|
|
|
*/
|
|
|
|
|
else { /* otherwise, insert one */
|
|
|
|
|
last_bl = e_com;
|
|
|
|
|
CHECK_SIZE_COM;
|
|
|
|
|
*e_com++ = ' ';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
++line_no; /* keep track of input line number */
|
|
|
|
|
if (!ps.box_com) {
|
|
|
|
|
int nstar = 1;
|
|
|
|
|
do { /* flush any blanks and/or tabs at start of
|
|
|
|
|
* next line */
|
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
|
|
|
|
if (*buf_ptr == '*' && --nstar >= 0) {
|
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
|
|
|
|
if (*buf_ptr == '/')
|
|
|
|
|
goto end_of_comment;
|
|
|
|
|
}
|
|
|
|
|
} while (*buf_ptr == ' ' || *buf_ptr == '\t');
|
|
|
|
|
}
|
|
|
|
|
else if (++buf_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
|
|
|
|
break; /* end of case for newline */
|
|
|
|
|
|
|
|
|
|
case '*': /* must check for possibility of being at end
|
|
|
|
|
* of comment */
|
|
|
|
|
if (++buf_ptr >= buf_end) /* get to next char after * */
|
|
|
|
|
fill_buffer();
|
|
|
|
|
|
|
|
|
|
if (*buf_ptr == '/') { /* it is the end!!! */
|
|
|
|
|
end_of_comment:
|
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
2016-07-31 21:09:22 +00:00
|
|
|
|
CHECK_SIZE_COM;
|
2016-07-31 20:04:18 +00:00
|
|
|
|
if (break_delim) {
|
|
|
|
|
if (e_com > s_com + 3) {
|
|
|
|
|
dump_line();
|
|
|
|
|
}
|
2016-07-31 21:09:22 +00:00
|
|
|
|
else
|
|
|
|
|
s_com = e_com;
|
|
|
|
|
*e_com++ = ' ';
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (e_com[-1] != ' ' && !ps.box_com)
|
|
|
|
|
*e_com++ = ' '; /* ensure blank before end */
|
|
|
|
|
*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
|
ps.just_saw_decl = l_just_saw_decl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-31 20:04:18 +00:00
|
|
|
|
else /* handle isolated '*' */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
*e_com++ = '*';
|
|
|
|
|
break;
|
|
|
|
|
default: /* we have a random char */
|
2016-07-31 20:04:18 +00:00
|
|
|
|
now_col = count_spaces_until(ps.com_col, s_com, e_com);
|
|
|
|
|
do {
|
|
|
|
|
*e_com = *buf_ptr++;
|
|
|
|
|
if (buf_ptr >= buf_end)
|
|
|
|
|
fill_buffer();
|
|
|
|
|
if (*e_com == ' ' || *e_com == '\t')
|
|
|
|
|
last_bl = e_com; /* remember we saw a blank */
|
|
|
|
|
++e_com;
|
|
|
|
|
now_col++;
|
|
|
|
|
} while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
|
2016-07-31 21:09:22 +00:00
|
|
|
|
(now_col <= adj_max_col || !last_bl));
|
2016-07-31 20:04:18 +00:00
|
|
|
|
ps.last_nl = false;
|
2016-07-31 15:02:53 +00:00
|
|
|
|
if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
|
1994-05-27 12:33:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* the comment is too long, it must be broken up
|
|
|
|
|
*/
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (last_bl == NULL) {
|
|
|
|
|
dump_line();
|
|
|
|
|
if (!ps.box_com && star_comment_cont)
|
|
|
|
|
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
|
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
2016-07-31 21:09:22 +00:00
|
|
|
|
*e_com = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
|
e_com = last_bl;
|
|
|
|
|
dump_line();
|
2016-07-31 21:09:22 +00:00
|
|
|
|
if (!ps.box_com && star_comment_cont)
|
|
|
|
|
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
|
|
|
|
|
for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
|
|
|
|
|
t_ptr++)
|
|
|
|
|
;
|
2016-07-29 19:36:10 +00:00
|
|
|
|
last_bl = NULL;
|
2016-07-31 21:09:22 +00:00
|
|
|
|
while (*t_ptr != '\0') {
|
|
|
|
|
if (*t_ptr == ' ' || *t_ptr == '\t')
|
|
|
|
|
last_bl = e_com;
|
|
|
|
|
*e_com++ = *t_ptr++;
|
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|