Changeset View
Changeset View
Standalone View
Standalone View
pr_comment.c
Show All 39 Lines | |||||
#endif | #endif | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 116390 2003-06-15 09:28:17Z charnier $"); | __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 116390 2003-06-15 09:28:17Z charnier $"); | ||||
#include <err.h> | #include <err.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | |||||
#include "indent_globs.h" | #include "indent_globs.h" | ||||
#include "indent.h" | #include "indent.h" | ||||
/* | /* | ||||
* NAME: | * NAME: | ||||
* pr_comment | * pr_comment | ||||
* | * | ||||
* FUNCTION: | * FUNCTION: | ||||
* This routine takes care of scanning and printing comments. | * This routine takes care of scanning and printing comments. | ||||
Show All 26 Lines | |||||
pr_comment(void) | pr_comment(void) | ||||
{ | { | ||||
int now_col; /* column we are in now */ | int now_col; /* column we are in now */ | ||||
int adj_max_col; /* Adjusted max_col for when we decide to | int adj_max_col; /* Adjusted max_col for when we decide to | ||||
* spill comments over the right margin */ | * spill comments over the right margin */ | ||||
char *last_bl; /* points to the last blank in the output | char *last_bl; /* points to the last blank in the output | ||||
* buffer */ | * buffer */ | ||||
char *t_ptr; /* used for moving string */ | char *t_ptr; /* used for moving string */ | ||||
int unix_comment; /* tri-state variable used to decide if it is | |||||
* a unix-style comment. 0 means only blanks | |||||
* since /+*, 1 means regular style comment, 2 | |||||
* means unix style comment */ | |||||
int break_delim = comment_delimiter_on_blankline; | int break_delim = comment_delimiter_on_blankline; | ||||
int l_just_saw_decl = ps.just_saw_decl; | int l_just_saw_decl = ps.just_saw_decl; | ||||
/* | |||||
* int ps.last_nl = 0; true iff the last significant thing | |||||
* weve seen is a newline | |||||
*/ | |||||
int one_liner = 1; /* true iff this comment is a one-liner */ | |||||
adj_max_col = max_col; | adj_max_col = max_col; | ||||
ps.just_saw_decl = 0; | ps.just_saw_decl = 0; | ||||
last_bl = 0; /* no blanks found so far */ | last_bl = NULL; /* no blanks found so far */ | ||||
ps.box_com = false; /* at first, assume that we are not in | ps.box_com = false; /* at first, assume that we are not in | ||||
* a boxed comment or some other | * a boxed comment or some other | ||||
* comment that should not be touched */ | * comment that should not be touched */ | ||||
++ps.out_coms; /* keep track of number of comments */ | ++ps.out_coms; /* keep track of number of comments */ | ||||
unix_comment = 1; /* set flag to let us figure out if there is a | |||||
* unix-style comment ** DISABLED: use 0 to | |||||
* reenable this hack! */ | |||||
/* Figure where to align and how to treat the comment */ | /* Figure where to align and how to treat the comment */ | ||||
if (ps.col_1 && !format_col1_comments) { /* if comment starts in column | if (ps.col_1 && !format_col1_comments) { /* if comment starts in column | ||||
* 1 it should not be touched */ | * 1 it should not be touched */ | ||||
ps.box_com = true; | ps.box_com = true; | ||||
break_delim = false; | |||||
ps.com_col = 1; | ps.com_col = 1; | ||||
} | } | ||||
else { | else { | ||||
if (*buf_ptr == '-' || *buf_ptr == '*' || | if (*buf_ptr == '-' || *buf_ptr == '*' || | ||||
(*buf_ptr == '\n' && !format_block_comments)) { | (*buf_ptr == '\n' && !format_block_comments)) { | ||||
ps.box_com = true; /* A comment with a '-' or '*' immediately | ps.box_com = true; /* A comment with a '-' or '*' immediately | ||||
* after the /+* is assumed to be a boxed | * after the /+* is assumed to be a boxed | ||||
* comment. A comment with a newline | * comment. A comment with a newline | ||||
* immediately after the /+* is assumed to | * immediately after the /+* is assumed to | ||||
* be a block comment and is treated as a | * be a block comment and is treated as a | ||||
* box comment unless format_block_comments | * box comment unless format_block_comments | ||||
* is nonzero (the default). */ | * is nonzero (the default). */ | ||||
break_delim = 0; | break_delim = false; | ||||
} | } | ||||
if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { | if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) { | ||||
/* klg: check only if this line is blank */ | /* klg: check only if this line is blank */ | ||||
/* | /* | ||||
* If this (*and previous lines are*) blank, dont put comment way | * If this (*and previous lines are*) blank, dont put comment way | ||||
* out at left | * out at left | ||||
*/ | */ | ||||
ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; | ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; | ||||
adj_max_col = block_comment_max_col; | adj_max_col = block_comment_max_col; | ||||
if (ps.com_col <= 1) | if (ps.com_col <= 1) | ||||
ps.com_col = 1 + !format_col1_comments; | ps.com_col = 1 + !format_col1_comments; | ||||
} | } | ||||
else { | else { | ||||
int target_col; | int target_col; | ||||
break_delim = 0; | break_delim = false; | ||||
if (s_code != e_code) | if (s_code != e_code) | ||||
target_col = count_spaces(compute_code_target(), s_code); | target_col = count_spaces(compute_code_target(), s_code); | ||||
else { | else { | ||||
target_col = 1; | target_col = 1; | ||||
if (s_lab != e_lab) | if (s_lab != e_lab) | ||||
target_col = count_spaces(compute_label_target(), s_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; | ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind; | ||||
Show All 14 Lines | while (*buf_ptr == ' ' || *buf_ptr == '\t') | ||||
buf_ptr++; | buf_ptr++; | ||||
} | } | ||||
ps.comment_delta = 0; | ps.comment_delta = 0; | ||||
*e_com++ = '/'; /* put '/' followed by '*' into buffer */ | *e_com++ = '/'; /* put '/' followed by '*' into buffer */ | ||||
*e_com++ = '*'; | *e_com++ = '*'; | ||||
if (*buf_ptr != ' ' && !ps.box_com) | if (*buf_ptr != ' ' && !ps.box_com) | ||||
*e_com++ = ' '; | *e_com++ = ' '; | ||||
*e_com = '\0'; | /* 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(); | |||||
e_com = s_com = t; | |||||
if (!ps.box_com && star_comment_cont) | |||||
*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; | |||||
} | |||||
if (troff) { | if (troff) { | ||||
now_col = 1; | |||||
adj_max_col = 80; | adj_max_col = 80; | ||||
} | } | ||||
else | |||||
now_col = count_spaces(ps.com_col, s_com); /* figure what column we | |||||
* would be in if we | |||||
* printed the comment | |||||
* now */ | |||||
/* Start to copy the comment */ | /* Start to copy the comment */ | ||||
while (1) { /* this loop will go until the comment is | while (1) { /* this loop will go until the comment is | ||||
* copied */ | * copied */ | ||||
if (*buf_ptr > 040 && *buf_ptr != '*') | |||||
ps.last_nl = 0; | |||||
CHECK_SIZE_COM; | CHECK_SIZE_COM; | ||||
switch (*buf_ptr) { /* this checks for various spcl cases */ | switch (*buf_ptr) { /* this checks for various spcl cases */ | ||||
case 014: /* check for a form feed */ | case 014: /* check for a form feed */ | ||||
if (!ps.box_com) { /* in a text comment, break the line here */ | if (!ps.box_com) { /* in a text comment, break the line here */ | ||||
ps.use_ff = true; | ps.use_ff = true; | ||||
/* fix so dump_line uses a form feed */ | /* fix so dump_line uses a form feed */ | ||||
dump_line(); | dump_line(); | ||||
last_bl = 0; | last_bl = NULL; | ||||
*e_com++ = ' '; | if (!ps.box_com && star_comment_cont) | ||||
*e_com++ = '*'; | *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; | ||||
*e_com++ = ' '; | while (*++buf_ptr == ' ' || *buf_ptr == '\t') | ||||
lattera-gmail.com: Does this need a bounds check to make sure that buf_ptr is always within the bounds of the… | |||||
Not Done Inline ActionsPerhaps, but again - it has been like that for years and I haven't changed it in any of the proposed patches of mine. Not to say it doesn't need fixing, but that's perhaps for another patch. pstef: Perhaps, but again - it has been like that for years and I haven't changed it in any of the… | |||||
while (*++buf_ptr == ' ' || *buf_ptr == '\t'); | ; | ||||
} | } | ||||
else { | else { | ||||
if (++buf_ptr >= buf_end) | if (++buf_ptr >= buf_end) | ||||
fill_buffer(); | fill_buffer(); | ||||
*e_com++ = 014; | *e_com++ = 014; | ||||
} | } | ||||
break; | break; | ||||
case '\n': | case '\n': | ||||
if (had_eof) { /* check for unexpected eof */ | if (had_eof) { /* check for unexpected eof */ | ||||
printf("Unterminated comment\n"); | printf("Unterminated comment\n"); | ||||
*e_com = '\0'; | |||||
dump_line(); | dump_line(); | ||||
return; | return; | ||||
} | } | ||||
one_liner = 0; | last_bl = NULL; | ||||
if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, | if (ps.box_com || ps.last_nl) { /* if this is a boxed comment, | ||||
* we dont ignore the newline */ | * we dont ignore the newline */ | ||||
if (s_com == e_com) { | if (s_com == e_com) | ||||
*e_com++ = ' '; | *e_com++ = ' '; | ||||
*e_com++ = ' '; | |||||
} | |||||
*e_com = '\0'; | |||||
if (!ps.box_com && e_com - s_com > 3) { | if (!ps.box_com && e_com - s_com > 3) { | ||||
if (break_delim == 1 && s_com[0] == '/' | |||||
&& s_com[1] == '*' && s_com[2] == ' ') { | |||||
char *t = e_com; | |||||
break_delim = 2; | |||||
e_com = s_com + 2; | |||||
*e_com = 0; | |||||
if (blanklines_before_blockcomments) | |||||
prefix_blankline_requested = 1; | |||||
dump_line(); | dump_line(); | ||||
e_com = t; | if (star_comment_cont) | ||||
s_com[0] = s_com[1] = s_com[2] = ' '; | *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; | ||||
} | } | ||||
dump_line(); | dump_line(); | ||||
CHECK_SIZE_COM; | if (!ps.box_com && star_comment_cont) | ||||
*e_com++ = ' '; | *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; | ||||
*e_com++ = ' '; | |||||
} | } | ||||
dump_line(); | |||||
now_col = ps.com_col; | |||||
} | |||||
else { | else { | ||||
ps.last_nl = 1; | ps.last_nl = 1; | ||||
if (unix_comment != 1) { /* we not are in unix_style | |||||
* comment */ | |||||
if (unix_comment == 0 && s_code == e_code) { | |||||
/* | |||||
* if it is a UNIX-style comment, ignore the | |||||
* requirement that previous line be blank for | |||||
* unindention | |||||
*/ | |||||
ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1; | |||||
if (ps.com_col <= 1) | |||||
ps.com_col = 2; | |||||
} | |||||
unix_comment = 2; /* permanently remember that we are in | |||||
* this type of comment */ | |||||
dump_line(); | |||||
++line_no; | |||||
now_col = ps.com_col; | |||||
*e_com++ = ' '; | |||||
/* | |||||
* fix so that the star at the start of the line will line | |||||
* up | |||||
*/ | |||||
do /* flush leading white space */ | |||||
if (++buf_ptr >= buf_end) | |||||
fill_buffer(); | |||||
while (*buf_ptr == ' ' || *buf_ptr == '\t'); | |||||
break; | |||||
} | |||||
if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') | if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t') | ||||
last_bl = e_com - 1; | last_bl = e_com - 1; | ||||
/* | /* | ||||
* if there was a space at the end of the last line, remember | * if there was a space at the end of the last line, remember | ||||
* where it was | * where it was | ||||
*/ | */ | ||||
else { /* otherwise, insert one */ | else { /* otherwise, insert one */ | ||||
last_bl = e_com; | last_bl = e_com; | ||||
CHECK_SIZE_COM; | CHECK_SIZE_COM; | ||||
*e_com++ = ' '; | *e_com++ = ' '; | ||||
++now_col; | |||||
} | } | ||||
} | } | ||||
++line_no; /* keep track of input line number */ | ++line_no; /* keep track of input line number */ | ||||
if (!ps.box_com) { | if (!ps.box_com) { | ||||
int nstar = 1; | int nstar = 1; | ||||
do { /* flush any blanks and/or tabs at start of | do { /* flush any blanks and/or tabs at start of | ||||
* next line */ | * next line */ | ||||
if (++buf_ptr >= buf_end) | if (++buf_ptr >= buf_end) | ||||
Show All 10 Lines | case '\n': | ||||
fill_buffer(); | fill_buffer(); | ||||
break; /* end of case for newline */ | break; /* end of case for newline */ | ||||
case '*': /* must check for possibility of being at end | case '*': /* must check for possibility of being at end | ||||
* of comment */ | * of comment */ | ||||
if (++buf_ptr >= buf_end) /* get to next char after * */ | if (++buf_ptr >= buf_end) /* get to next char after * */ | ||||
fill_buffer(); | fill_buffer(); | ||||
if (unix_comment == 0) /* set flag to show we are not in | |||||
* unix-style comment */ | |||||
unix_comment = 1; | |||||
if (*buf_ptr == '/') { /* it is the end!!! */ | if (*buf_ptr == '/') { /* it is the end!!! */ | ||||
end_of_comment: | end_of_comment: | ||||
if (++buf_ptr >= buf_end) | if (++buf_ptr >= buf_end) | ||||
fill_buffer(); | fill_buffer(); | ||||
CHECK_SIZE_COM; | |||||
if (*(e_com - 1) != ' ' && !ps.box_com) { /* insure blank before | if (break_delim) { | ||||
* end */ | if (e_com > s_com + 3) { | ||||
*e_com++ = ' '; | |||||
++now_col; | |||||
} | |||||
if (break_delim == 1 && !one_liner && s_com[0] == '/' | |||||
&& s_com[1] == '*' && s_com[2] == ' ') { | |||||
char *t = e_com; | |||||
break_delim = 2; | |||||
e_com = s_com + 2; | |||||
*e_com = 0; | |||||
if (blanklines_before_blockcomments) | |||||
prefix_blankline_requested = 1; | |||||
dump_line(); | dump_line(); | ||||
e_com = t; | |||||
s_com[0] = s_com[1] = s_com[2] = ' '; | |||||
} | } | ||||
Not Done Inline Actionsstyle extract return (repeated in other part of the code) bapt: style extract return (repeated in other part of the code) | |||||
Not Done Inline ActionsI don't understand this comment. pstef: I don't understand this comment. | |||||
if (break_delim == 2 && e_com > s_com + 3 | else | ||||
/* now_col > adj_max_col - 2 && !ps.box_com */ ) { | s_com = e_com; | ||||
*e_com = '\0'; | *e_com++ = ' '; | ||||
dump_line(); | |||||
now_col = ps.com_col; | |||||
} | } | ||||
CHECK_SIZE_COM; | if (e_com[-1] != ' ' && !ps.box_com) { | ||||
*e_com++ = '*'; | *e_com++ = ' '; /* ensure blank before end */ | ||||
*e_com++ = '/'; | } | ||||
*e_com = '\0'; | *e_com++ = '*', *e_com++ = '/', *e_com = '\0'; | ||||
ps.just_saw_decl = l_just_saw_decl; | ps.just_saw_decl = l_just_saw_decl; | ||||
return; | return; | ||||
} | } | ||||
else { /* handle isolated '*' */ | else { /* handle isolated '*' */ | ||||
*e_com++ = '*'; | *e_com++ = '*'; | ||||
++now_col; | |||||
} | } | ||||
break; | break; | ||||
default: /* we have a random char */ | default: /* we have a random char */ | ||||
if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t') | now_col = count_spaces_until(ps.com_col, s_com, e_com); | ||||
unix_comment = 1; /* we are not in unix-style comment */ | do { | ||||
*e_com = *buf_ptr++; | *e_com = *buf_ptr++; | ||||
if (buf_ptr >= buf_end) | if (buf_ptr >= buf_end) | ||||
fill_buffer(); | fill_buffer(); | ||||
if (*e_com == '\t') /* keep track of column */ | |||||
now_col = ((now_col - 1) & tabmask) + tabsize + 1; | |||||
else if (*e_com == '\b') /* this is a backspace */ | |||||
--now_col; | |||||
else | |||||
++now_col; | |||||
if (*e_com == ' ' || *e_com == '\t') | if (*e_com == ' ' || *e_com == '\t') | ||||
last_bl = e_com; | last_bl = e_com; /* remember we saw a blank */ | ||||
/* remember we saw a blank */ | |||||
++e_com; | ++e_com; | ||||
if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') { | now_col++; | ||||
/* | } while (!memchr("*\n\r\b\t", *buf_ptr, 6) && (now_col <= adj_max_col || !last_bl)); | ||||
* the comment is too long, it must be broken up | ps.last_nl = false; | ||||
*/ | if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') { | ||||
if (break_delim == 1 && s_com[0] == '/' | /* the comment is too long, it must be broken up */ | ||||
&& s_com[1] == '*' && s_com[2] == ' ') { | if (last_bl == NULL) { | ||||
char *t = e_com; | |||||
break_delim = 2; | |||||
e_com = s_com + 2; | |||||
*e_com = 0; | |||||
if (blanklines_before_blockcomments) | |||||
prefix_blankline_requested = 1; | |||||
dump_line(); | dump_line(); | ||||
e_com = t; | if (!ps.box_com && star_comment_cont) | ||||
s_com[0] = s_com[1] = s_com[2] = ' '; | *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; | ||||
Not Done Inline ActionsAre commas here valid C syntax? Should they be semicolons? lattera-gmail.com: Are commas here valid C syntax? Should they be semicolons? | |||||
Not Done Inline ActionsYes, they are. A more canonical way of expressing code doing effectively the same would be: if (!ps.box_com && star_comment_cont) { *e_com++ = ' '; *e_com++ = '*'; *e_com++ = ' '; } But I find it visually inferior to the one-liner from my patch. If someone is to propose a patch that does the latter instead of the former consistently, I won't oppose. pstef: Yes, they are. A more canonical way of expressing code doing effectively the same would be… | |||||
break; | |||||
} | } | ||||
if (last_bl == 0) { /* we have seen no blanks */ | *e_com = '\0'; | ||||
last_bl = e_com; /* fake it */ | |||||
*e_com++ = ' '; | |||||
} | |||||
*e_com = '\0'; /* print what we have */ | |||||
*last_bl = '\0'; | |||||
while (last_bl > s_com && last_bl[-1] < 040) | |||||
*--last_bl = 0; | |||||
e_com = last_bl; | e_com = last_bl; | ||||
dump_line(); | dump_line(); | ||||
if (!ps.box_com && star_comment_cont) | |||||
*e_com++ = ' '; /* add blanks for continuation */ | *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' '; | ||||
*e_com++ = ' '; | for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t'; t_ptr++) | ||||
*e_com++ = ' '; | ; | ||||
last_bl = NULL; | |||||
t_ptr = last_bl + 1; | while (*t_ptr != '\0') { | ||||
last_bl = 0; | |||||
if (t_ptr >= e_com) { | |||||
while (*t_ptr == ' ' || *t_ptr == '\t') | |||||
t_ptr++; | |||||
while (*t_ptr != '\0') { /* move unprinted part of | |||||
* comment down in buffer */ | |||||
if (*t_ptr == ' ' || *t_ptr == '\t') | if (*t_ptr == ' ' || *t_ptr == '\t') | ||||
last_bl = e_com; | last_bl = e_com; | ||||
*e_com++ = *t_ptr++; | *e_com++ = *t_ptr++; | ||||
} | } | ||||
} | |||||
*e_com = '\0'; | |||||
now_col = count_spaces(ps.com_col, s_com); /* recompute current | |||||
* position */ | |||||
} | } | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } |
Does this need a bounds check to make sure that buf_ptr is always within the bounds of the buffer (prevent out-of-bound read)?