Changeset View
Standalone View
indent.c
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include "indent_globs.h" | #include "indent_globs.h" | ||||
#include "indent_codes.h" | #include "indent_codes.h" | ||||
#include "indent.h" | #include "indent.h" | ||||
static void bakcopy(void); | static void bakcopy(void); | ||||
static void indent_declaration(int, int); | |||||
const char *in_name = "Standard Input"; /* will always point to name of input | const char *in_name = "Standard Input"; /* will always point to name of input | ||||
* file */ | * file */ | ||||
const char *out_name = "Standard Output"; /* will always point to name | const char *out_name = "Standard Output"; /* will always point to name | ||||
* of output file */ | * of output file */ | ||||
char bakfile[MAXPATHLEN] = ""; | char bakfile[MAXPATHLEN] = ""; | ||||
int | int | ||||
Show All 39 Lines | err(1, NULL); | ||||
if (labbuf == NULL) | if (labbuf == NULL) | ||||
err(1, NULL); | err(1, NULL); | ||||
codebuf = (char *) malloc(bufsize); | codebuf = (char *) malloc(bufsize); | ||||
if (codebuf == NULL) | if (codebuf == NULL) | ||||
err(1, NULL); | err(1, NULL); | ||||
tokenbuf = (char *) malloc(bufsize); | tokenbuf = (char *) malloc(bufsize); | ||||
if (tokenbuf == NULL) | if (tokenbuf == NULL) | ||||
err(1, NULL); | err(1, NULL); | ||||
alloc_typenames(); | |||||
l_com = combuf + bufsize - 5; | l_com = combuf + bufsize - 5; | ||||
l_lab = labbuf + bufsize - 5; | l_lab = labbuf + bufsize - 5; | ||||
l_code = codebuf + bufsize - 5; | l_code = codebuf + bufsize - 5; | ||||
l_token = tokenbuf + bufsize - 5; | l_token = tokenbuf + bufsize - 5; | ||||
combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and | combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and | ||||
* comment buffers */ | * comment buffers */ | ||||
combuf[1] = codebuf[1] = labbuf[1] = '\0'; | combuf[1] = codebuf[1] = labbuf[1] = '\0'; | ||||
ps.else_if = 1; /* Default else-if special processing to on */ | ps.else_if = 1; /* Default else-if special processing to on */ | ||||
▲ Show 20 Lines • Show All 185 Lines • ▼ Show 20 Lines | while (ps.search_brace) { /* if we scanned an if(), while(), | ||||
* etc., we might need to copy stuff | * etc., we might need to copy stuff | ||||
* into a buffer we must loop, copying | * into a buffer we must loop, copying | ||||
* stuff into save_com, until we find | * stuff into save_com, until we find | ||||
* the start of the stmt which follows | * the start of the stmt which follows | ||||
* the if, or whatever */ | * the if, or whatever */ | ||||
switch (type_code) { | switch (type_code) { | ||||
case newline: | case newline: | ||||
++line_no; | ++line_no; | ||||
if (sc_end != NULL) | |||||
goto sw_buffer; /* dump comment, if any */ | |||||
flushed_nl = true; | flushed_nl = true; | ||||
case form_feed: | case form_feed: | ||||
break; /* form feeds and newlines found here will be | break; /* form feeds and newlines found here will be | ||||
* ignored */ | * ignored */ | ||||
case lbrace: /* this is a brace that starts the compound | case lbrace: /* this is a brace that starts the compound | ||||
* stmt */ | * stmt */ | ||||
if (sc_end == 0) { /* ignore buffering if a comment wasn't | if (sc_end == 0) { /* ignore buffering if a comment wasn't | ||||
▲ Show 20 Lines • Show All 173 Lines • ▼ Show 20 Lines | ps.want_blank = false; | ||||
} | } | ||||
++line_no; /* keep track of input line number */ | ++line_no; /* keep track of input line number */ | ||||
break; | break; | ||||
case lparen: /* got a '(' or '[' */ | case lparen: /* got a '(' or '[' */ | ||||
++ps.p_l_follow; /* count parens to make Healy happy */ | ++ps.p_l_follow; /* count parens to make Healy happy */ | ||||
if (ps.want_blank && *token != '[' && | if (ps.want_blank && *token != '[' && | ||||
(ps.last_token != ident || proc_calls_space | (ps.last_token != ident || proc_calls_space | ||||
|| (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon)))) | /* offsetof (1) is never allowed a space; sizeof (2) gets | ||||
* one iff -bs; all other keywords (>2) always get a space | |||||
* before lparen */ | |||||
|| (ps.keyword + Bill_Shannon > 2))) | |||||
*e_code++ = ' '; | *e_code++ = ' '; | ||||
if (ps.in_decl && !ps.block_init) | ps.want_blank = false; | ||||
if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) { | if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent && !is_procname) { | ||||
ps.dumped_decl_indent = 1; | /* function pointer declarations */ | ||||
if (troff) { | |||||
sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); | sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); | ||||
e_code += strlen(e_code); | e_code += strlen(e_code); | ||||
} | } | ||||
else { | else { | ||||
while ((e_code - s_code) < dec_ind) { | indent_declaration(dec_ind, tabs_to_var); | ||||
CHECK_SIZE_CODE; | |||||
*e_code++ = ' '; | |||||
} | } | ||||
*e_code++ = token[0]; | ps.dumped_decl_indent = true; | ||||
} | } | ||||
else | if (!troff) | ||||
*e_code++ = token[0]; | *e_code++ = token[0]; | ||||
ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code; | ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code; | ||||
if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent | if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent | ||||
&& ps.paren_indents[0] < 2 * ps.ind_size) | && ps.paren_indents[0] < 2 * ps.ind_size) | ||||
ps.paren_indents[0] = 2 * ps.ind_size; | ps.paren_indents[0] = 2 * ps.ind_size; | ||||
ps.want_blank = false; | |||||
if (ps.in_or_st && *token == '(' && ps.tos <= 2) { | if (ps.in_or_st && *token == '(' && ps.tos <= 2) { | ||||
/* | /* | ||||
* this is a kluge to make sure that declarations will be | * this is a kluge to make sure that declarations will be | ||||
* aligned right if proc decl has an explicit type on it, i.e. | * aligned right if proc decl has an explicit type on it, i.e. | ||||
* "int a(x) {..." | * "int a(x) {..." | ||||
*/ | */ | ||||
parse(semicolon); /* I said this was a kluge... */ | parse(semicolon); /* I said this was a kluge... */ | ||||
ps.in_or_st = false; /* turn off flag for structure decl or | ps.in_or_st = false; /* turn off flag for structure decl or | ||||
* initialization */ | * initialization */ | ||||
} | } | ||||
if (ps.sizeof_keyword) | /* parenthesized type following sizeof or offsetof is not a cast */ | ||||
ps.sizeof_mask |= 1 << ps.p_l_follow; | if (ps.keyword == 1 || ps.keyword == 2) | ||||
ps.not_cast_mask |= 1 << ps.p_l_follow; | |||||
break; | break; | ||||
case rparen: /* got a ')' or ']' */ | case rparen: /* got a ')' or ']' */ | ||||
rparen_count--; | rparen_count--; | ||||
if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) { | if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) { | ||||
ps.last_u_d = true; | ps.last_u_d = true; | ||||
ps.cast_mask &= (1 << ps.p_l_follow) - 1; | ps.cast_mask &= (1 << ps.p_l_follow) - 1; | ||||
ps.want_blank = false; | ps.want_blank = space_after_cast; | ||||
} else | } | ||||
else | |||||
ps.want_blank = true; | ps.want_blank = true; | ||||
ps.sizeof_mask &= (1 << ps.p_l_follow) - 1; | ps.not_cast_mask &= (1 << ps.p_l_follow) - 1; | ||||
if (--ps.p_l_follow < 0) { | if (--ps.p_l_follow < 0) { | ||||
ps.p_l_follow = 0; | ps.p_l_follow = 0; | ||||
diag3(0, "Extra %c", *token); | diag3(0, "Extra %c", *token); | ||||
} | } | ||||
if (e_code == s_code) /* if the paren starts the line */ | if (e_code == s_code) /* if the paren starts the line */ | ||||
ps.paren_level = ps.p_l_follow; /* then indent it */ | ps.paren_level = ps.p_l_follow; /* then indent it */ | ||||
*e_code++ = token[0]; | *e_code++ = token[0]; | ||||
Show All 11 Lines | parse(hd_type); /* let parser worry about if, or whatever */ | ||||
} | } | ||||
ps.search_brace = btype_2; /* this should insure that constructs | ps.search_brace = btype_2; /* this should insure that constructs | ||||
* such as main(){...} and int[]{...} | * such as main(){...} and int[]{...} | ||||
* have their braces put in the right | * have their braces put in the right | ||||
* place */ | * place */ | ||||
break; | break; | ||||
case unary_op: /* this could be any unary operation */ | case unary_op: /* this could be any unary operation */ | ||||
if (!ps.dumped_decl_indent && ps.in_decl && !is_procname && !ps.block_init) { | |||||
/* pointer declarations */ | |||||
if (troff) { | |||||
if (ps.want_blank) | if (ps.want_blank) | ||||
*e_code++ = ' '; | *e_code++ = ' '; | ||||
if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) { | |||||
sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); | sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); | ||||
lattera-gmail.com: Please use snprintf to prevent buffer overflows. | |||||
Not Done Inline ActionsI don't intend to change this fragment, at least not yet, and it has been like this for years. None of my patches address potentially unsafe practices in general, like replacing sprintf() with snprintf() as you suggest. It's shown in the diff as changed merely because of whitespace change. pstef: I don't intend to change this fragment, at least not yet, and it has been like this for years. | |||||
ps.dumped_decl_indent = 1; | |||||
e_code += strlen(e_code); | e_code += strlen(e_code); | ||||
} | } | ||||
else { | else { | ||||
const char *res = token; | /* if this is a unary op in a declaration, we should indent | ||||
* this token */ | |||||
if (ps.in_decl && !ps.block_init) { /* if this is a unary op | for (i = 0; token[i]; ++i) | ||||
* in a declaration, we | /* find length of token */; | ||||
* should indent this | indent_declaration(dec_ind - i, tabs_to_var); | ||||
* token */ | |||||
for (i = 0; token[i]; ++i); /* find length of token */ | |||||
while ((e_code - s_code) < (dec_ind - i)) { | |||||
CHECK_SIZE_CODE; | |||||
*e_code++ = ' '; /* pad it */ | |||||
} | } | ||||
ps.dumped_decl_indent = true; | |||||
} | } | ||||
else if (ps.want_blank) | |||||
*e_code++ = ' '; | |||||
{ | |||||
const char *res = token; | |||||
if (troff && token[0] == '-' && token[1] == '>') | if (troff && token[0] == '-' && token[1] == '>') | ||||
res = "\\(->"; | res = "\\(->"; | ||||
for (t_ptr = res; *t_ptr; ++t_ptr) { | for (t_ptr = res; *t_ptr; ++t_ptr) { | ||||
CHECK_SIZE_CODE; | CHECK_SIZE_CODE; | ||||
*e_code++ = *t_ptr; | *e_code++ = *t_ptr; | ||||
} | } | ||||
} | } | ||||
ps.want_blank = false; | ps.want_blank = false; | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | *e_lab++ = *t_ptr; /* turn everything so far into a label */ | ||||
* indent the label. force_nl | * indent the label. force_nl | ||||
* will force a case n: to be | * will force a case n: to be | ||||
* on a line by itself */ | * on a line by itself */ | ||||
scase = false; | scase = false; | ||||
ps.want_blank = false; | ps.want_blank = false; | ||||
break; | break; | ||||
case semicolon: /* got a ';' */ | case semicolon: /* got a ';' */ | ||||
ps.in_or_st = false;/* we are not in an initialization or | if (ps.dec_nest == 0) { | ||||
* structure declaration */ | /* we are not in an initialization or structure declaration */ | ||||
ps.in_or_st = false; | |||||
} | |||||
scase = false; /* these will only need resetting in an error */ | scase = false; /* these will only need resetting in an error */ | ||||
squest = 0; | squest = 0; | ||||
if (ps.last_token == rparen && rparen_count == 0) | if (ps.last_token == rparen && rparen_count == 0) | ||||
ps.in_parameter_declaration = 0; | ps.in_parameter_declaration = 0; | ||||
ps.cast_mask = 0; | ps.cast_mask = 0; | ||||
ps.sizeof_mask = 0; | ps.not_cast_mask = 0; | ||||
ps.block_init = 0; | ps.block_init = 0; | ||||
ps.block_init_level = 0; | ps.block_init_level = 0; | ||||
ps.just_saw_decl--; | ps.just_saw_decl--; | ||||
if (ps.in_decl && s_code == e_code && !ps.block_init) | if (ps.in_decl && s_code == e_code && !ps.block_init && !ps.dumped_decl_indent) { | ||||
while ((e_code - s_code) < (dec_ind - 1)) { | /* indent stray semicolons in declarations */ | ||||
CHECK_SIZE_CODE; | indent_declaration(dec_ind - 1, tabs_to_var); | ||||
*e_code++ = ' '; | ps.dumped_decl_indent = true; | ||||
} | } | ||||
Not Done Inline Actionsline too long. for our style(9) there are several like this. pfg: line too long. for our style(9) there are several like this. | |||||
Not Done Inline ActionsThis appears to have been commited with the style improvements. Would you like me to fix it in the proposed patch as well or do you think it doesn't matter anymore? pstef: This appears to have been commited with the style improvements. Would you like me to fix it in… | |||||
Not Done Inline ActionsAt this time I am using the github patches. It would be better to rebase the differential review but wait until later today when I commit a couple more. pfg: At this time I am using the github patches.
It would be better to rebase the differential… | |||||
ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level | ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level | ||||
* structure declaration, we | * structure declaration, we | ||||
* arent any more */ | * arent any more */ | ||||
if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) { | if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) { | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | case decl: /* we have a declaration type (int, register, | ||||
tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0); | tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0); | ||||
} | } | ||||
goto copy_id; | goto copy_id; | ||||
case ident: /* got an identifier or constant */ | case ident: /* got an identifier or constant */ | ||||
if (ps.in_decl) { /* if we are in a declaration, we must indent | if (ps.in_decl) { /* if we are in a declaration, we must indent | ||||
* identifier */ | * identifier */ | ||||
if (is_procname == 0 || !procnames_start_line) { | if (is_procname == 0 || !procnames_start_line) { | ||||
if (!ps.block_init) { | if (!ps.block_init && !ps.dumped_decl_indent) { | ||||
if (troff && !ps.dumped_decl_indent) { | if (troff) { | ||||
if (ps.want_blank) | if (ps.want_blank) | ||||
*e_code++ = ' '; | *e_code++ = ' '; | ||||
ps.want_blank = false; | |||||
sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7); | sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7); | ||||
ps.dumped_decl_indent = 1; | |||||
e_code += strlen(e_code); | e_code += strlen(e_code); | ||||
} else { | |||||
int cur_dec_ind; | |||||
int pos, startpos; | |||||
/* | |||||
* in order to get the tab math right for | |||||
* indentations that are not multiples of 8 we | |||||
* need to modify both startpos and dec_ind | |||||
* (cur_dec_ind) here by eight minus the | |||||
* remainder of the current starting column | |||||
* divided by eight. This seems to be a | |||||
* properly working fix | |||||
*/ | |||||
startpos = e_code - s_code; | |||||
cur_dec_ind = dec_ind; | |||||
pos = startpos; | |||||
if ((ps.ind_level * ps.ind_size) % 8 != 0) { | |||||
pos += (ps.ind_level * ps.ind_size) % 8; | |||||
cur_dec_ind += (ps.ind_level * ps.ind_size) % 8; | |||||
} | } | ||||
else { | |||||
Not Done Inline ActionsUnnecessary brackets for one statement (style). pfg: Unnecessary brackets for one statement (style). | |||||
if (tabs_to_var) { | indent_declaration(dec_ind, tabs_to_var); | ||||
while ((pos & ~7) + 8 <= cur_dec_ind) { | |||||
CHECK_SIZE_CODE; | |||||
*e_code++ = '\t'; | |||||
pos = (pos & ~7) + 8; | |||||
} | } | ||||
} | ps.dumped_decl_indent = true; | ||||
while (pos < cur_dec_ind) { | |||||
CHECK_SIZE_CODE; | |||||
*e_code++ = ' '; | |||||
pos++; | |||||
} | |||||
if (ps.want_blank && e_code - s_code == startpos) | |||||
*e_code++ = ' '; | |||||
ps.want_blank = false; | ps.want_blank = false; | ||||
} | } | ||||
} | } | ||||
} else { | else { | ||||
Not Done Inline ActionsThis two line change is unnecessary. pfg: This two line change is unnecessary. | |||||
if (ps.want_blank) | if (ps.want_blank) | ||||
*e_code++ = ' '; | *e_code++ = ' '; | ||||
ps.want_blank = false; | ps.want_blank = false; | ||||
if (dec_ind && s_code != e_code) | if (dec_ind && s_code != e_code) { | ||||
*e_code = '\0'; | |||||
dump_line(); | dump_line(); | ||||
} | |||||
dec_ind = 0; | dec_ind = 0; | ||||
} | } | ||||
} | } | ||||
else if (sp_sw && ps.p_l_follow == 0) { | else if (sp_sw && ps.p_l_follow == 0) { | ||||
sp_sw = false; | sp_sw = false; | ||||
force_nl = true; | force_nl = true; | ||||
ps.last_u_d = true; | ps.last_u_d = true; | ||||
ps.in_stmt = false; | ps.in_stmt = false; | ||||
parse(hd_type); | parse(hd_type); | ||||
} | } | ||||
copy_id: | copy_id: | ||||
if (ps.want_blank) | if (ps.want_blank) | ||||
*e_code++ = ' '; | *e_code++ = ' '; | ||||
if (troff && ps.its_a_keyword) { | if (troff && ps.keyword) { | ||||
e_code = chfont(&bodyf, &keywordf, e_code); | e_code = chfont(&bodyf, &keywordf, e_code); | ||||
for (t_ptr = token; *t_ptr; ++t_ptr) { | for (t_ptr = token; *t_ptr; ++t_ptr) { | ||||
CHECK_SIZE_CODE; | CHECK_SIZE_CODE; | ||||
*e_code++ = keywordf.allcaps && islower(*t_ptr) | *e_code++ = keywordf.allcaps && islower(*t_ptr) | ||||
? toupper(*t_ptr) : *t_ptr; | ? toupper(*t_ptr) : *t_ptr; | ||||
} | } | ||||
e_code = chfont(&keywordf, &bodyf, e_code); | e_code = chfont(&keywordf, &bodyf, e_code); | ||||
} | } | ||||
Show All 10 Lines | case period: /* treat a period kind of like a binary | ||||
*e_code++ = '.'; /* move the period into line */ | *e_code++ = '.'; /* move the period into line */ | ||||
ps.want_blank = false; /* dont put a blank after a period */ | ps.want_blank = false; /* dont put a blank after a period */ | ||||
break; | break; | ||||
case comma: | case comma: | ||||
ps.want_blank = (s_code != e_code); /* only put blank after comma | ps.want_blank = (s_code != e_code); /* only put blank after comma | ||||
* if comma does not start the | * if comma does not start the | ||||
* line */ | * line */ | ||||
if (ps.in_decl && is_procname == 0 && !ps.block_init) | if (ps.in_decl && is_procname == 0 && !ps.block_init && !ps.dumped_decl_indent) { | ||||
while ((e_code - s_code) < (dec_ind - 1)) { | /* indent leading commas and not the actual identifiers */ | ||||
CHECK_SIZE_CODE; | indent_declaration(dec_ind - 1, tabs_to_var); | ||||
*e_code++ = ' '; | ps.dumped_decl_indent = true; | ||||
} | } | ||||
Not Done Inline Actionsline too long for our style(0) pfg: line too long for our style(0) | |||||
*e_code++ = ','; | *e_code++ = ','; | ||||
if (ps.p_l_follow == 0) { | if (ps.p_l_follow == 0) { | ||||
if (ps.block_init_level <= 0) | if (ps.block_init_level <= 0) | ||||
ps.block_init = 0; | ps.block_init = 0; | ||||
if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8)) | if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8)) | ||||
force_nl = true; | force_nl = true; | ||||
} | } | ||||
break; | break; | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on | ||||
*sc_end++ = ' '; /* add trailing blank, just in case */ | *sc_end++ = ' '; /* add trailing blank, just in case */ | ||||
buf_end = sc_end; | buf_end = sc_end; | ||||
sc_end = 0; | sc_end = 0; | ||||
} | } | ||||
*e_lab = '\0'; /* null terminate line */ | *e_lab = '\0'; /* null terminate line */ | ||||
ps.pcase = false; | ps.pcase = false; | ||||
} | } | ||||
if (strncmp(s_lab, "#if", 3) == 0) { | if (!strncmp(s_lab, "#if", 3)) { /* also ifdef, ifndef */ | ||||
Not Done Inline ActionsWhat is the point of changing that if and not keeping the original form ? bapt: What is the point of changing that if and not keeping the original form ? | |||||
Not Done Inline ActionsI presume you mean the change from strncmp() == 0 to !strncmp(). I did that to make the line shorter so that I could fit the comment that I added on that line. Initially the comment was a bit longer and didn't fit in 80 columns. pstef: I presume you mean the change from strncmp() == 0 to !strncmp(). I did that to make the line… | |||||
Not Done Inline ActionsFixed. pstef: Fixed. | |||||
if (blanklines_around_conditional_compilation) { | |||||
int c; | |||||
prefix_blankline_requested++; | |||||
while ((c = getc(input)) == '\n'); | |||||
ungetc(c, input); | |||||
} | |||||
if ((size_t)ifdef_level < sizeof(state_stack)/sizeof(state_stack[0])) { | if ((size_t)ifdef_level < sizeof(state_stack)/sizeof(state_stack[0])) { | ||||
match_state[ifdef_level].tos = -1; | match_state[ifdef_level].tos = -1; | ||||
state_stack[ifdef_level++] = ps; | state_stack[ifdef_level++] = ps; | ||||
} | } | ||||
else | else | ||||
diag2(1, "#if stack overflow"); | diag2(1, "#if stack overflow"); | ||||
} | } | ||||
else if (strncmp(s_lab, "#else", 5) == 0) | else if (!strncmp(s_lab, "#el", 3)) { /* else, elif */ | ||||
if (ifdef_level <= 0) | if (ifdef_level <= 0) | ||||
diag2(1, "Unmatched #else"); | diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else"); | ||||
else { | else { | ||||
match_state[ifdef_level - 1] = ps; | match_state[ifdef_level - 1] = ps; | ||||
ps = state_stack[ifdef_level - 1]; | ps = state_stack[ifdef_level - 1]; | ||||
} | } | ||||
} | |||||
else if (strncmp(s_lab, "#endif", 6) == 0) { | else if (strncmp(s_lab, "#endif", 6) == 0) { | ||||
if (ifdef_level <= 0) | if (ifdef_level <= 0) | ||||
diag2(1, "Unmatched #endif"); | diag2(1, "Unmatched #endif"); | ||||
else { | else | ||||
ifdef_level--; | ifdef_level--; | ||||
#ifdef undef | |||||
/* | |||||
* This match needs to be more intelligent before the | |||||
* message is useful | |||||
*/ | |||||
if (match_state[ifdef_level].tos >= 0 | |||||
&& bcmp(&ps, &match_state[ifdef_level], sizeof ps)) | |||||
diag2(0, "Syntactically inconsistent #ifdef alternatives"); | |||||
#endif | |||||
} | } | ||||
else { | |||||
struct directives { | |||||
int size; char *string; | |||||
} | |||||
recognized[] = { | |||||
{7, "include"}, {6, "define"}, {5, "undef"}, | |||||
{4, "line"}, {5, "error"}, {6, "pragma"} | |||||
}; | |||||
int d = sizeof(recognized) / sizeof(recognized[0]); | |||||
while (--d >= 0) | |||||
if (!strncmp(s_lab + 1, recognized[d].string, recognized[d].size)) | |||||
break; | |||||
if (d < 0) { | |||||
diag2(1, "Unrecognized cpp directive"); | |||||
break; | |||||
Not Done Inline ActionsNot sure if I am missing something but I am getting these when I get to 01f36f4141c71754b3a73a91886fb425bab0df3e @pstef pstef committed on Jun 25 indent.c:1131:11: error: initializing 'char *' with an expression of type 'const char [8]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] {7, "include"}, ^~~~~~~~~ indent.c:1132:11: error: initializing 'char *' with an expression of type 'const char [7]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] {6, "define"}, ^~~~~~~~ indent.c:1133:11: error: initializing 'char *' with an expression of type 'const char [6]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] {5, "undef"}, ^~~~~~~ indent.c:1134:11: error: initializing 'char *' with an expression of type 'const char [5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] {4, "line"}, pfg: Not sure if I am missing something but I am getting these when I get to… | |||||
Not Done Inline ActionsI'm unable to reproduce this. Should be fixed by constifying char *string: struct directives { int size; - char *string; + const char *string; } pstef: I'm unable to reproduce this. Should be fixed by constifying char *string:
```… | |||||
} | |||||
} | |||||
if (blanklines_around_conditional_compilation) { | if (blanklines_around_conditional_compilation) { | ||||
postfix_blankline_requested++; | postfix_blankline_requested++; | ||||
n_real_blanklines = 0; | n_real_blanklines = 0; | ||||
} | } | ||||
else { | |||||
postfix_blankline_requested = 0; | |||||
prefix_blankline_requested = 0; | |||||
} | } | ||||
break; /* subsequent processing of the newline | break; /* subsequent processing of the newline | ||||
* character will cause the line to be printed */ | * character will cause the line to be printed */ | ||||
case comment: /* we have gotten a / followed by * this is a biggie */ | case comment: /* we have gotten a / followed by * this is a biggie */ | ||||
if (flushed_nl) { /* we should force a broken line here */ | if (flushed_nl) { /* we should force a broken line here */ | ||||
flushed_nl = false; | flushed_nl = false; | ||||
dump_line(); | dump_line(); | ||||
Show All 30 Lines | p--; | ||||
if (*p == '/') | if (*p == '/') | ||||
p++; | p++; | ||||
sprintf(bakfile, "%s.BAK", p); | sprintf(bakfile, "%s.BAK", p); | ||||
/* copy in_name to backup file */ | /* copy in_name to backup file */ | ||||
bakchn = creat(bakfile, 0600); | bakchn = creat(bakfile, 0600); | ||||
if (bakchn < 0) | if (bakchn < 0) | ||||
err(1, "%s", bakfile); | err(1, "%s", bakfile); | ||||
while ((n = read(fileno(input), buff, sizeof buff)) != 0) | while ((n = read(fileno(input), buff, sizeof(buff))) != 0) | ||||
if (write(bakchn, buff, n) != n) | if (write(bakchn, buff, n) != n) | ||||
err(1, "%s", bakfile); | err(1, "%s", bakfile); | ||||
if (n < 0) | if (n < 0) | ||||
err(1, "%s", in_name); | err(1, "%s", in_name); | ||||
close(bakchn); | close(bakchn); | ||||
fclose(input); | fclose(input); | ||||
/* re-open backup file as the input file */ | /* re-open backup file as the input file */ | ||||
input = fopen(bakfile, "r"); | input = fopen(bakfile, "r"); | ||||
if (input == NULL) | if (input == NULL) | ||||
err(1, "%s", bakfile); | err(1, "%s", bakfile); | ||||
/* now the original input file will be the output */ | /* now the original input file will be the output */ | ||||
output = fopen(in_name, "w"); | output = fopen(in_name, "w"); | ||||
if (output == NULL) { | if (output == NULL) { | ||||
unlink(bakfile); | unlink(bakfile); | ||||
err(1, "%s", in_name); | err(1, "%s", in_name); | ||||
} | |||||
} | |||||
static void | |||||
indent_declaration(int cur_dec_ind, int tabs_to_var) | |||||
{ | |||||
int pos = e_code - s_code; | |||||
char *startpos = e_code; | |||||
/* | |||||
* get the tab math right for indentations that are not multiples of 8 | |||||
*/ | |||||
if ((ps.ind_level * ps.ind_size) % 8 != 0) { | |||||
pos += (ps.ind_level * ps.ind_size) % 8; | |||||
cur_dec_ind += (ps.ind_level * ps.ind_size) % 8; | |||||
} | |||||
if (tabs_to_var) { | |||||
while ((pos & ~7) + 8 <= cur_dec_ind) { | |||||
CHECK_SIZE_CODE; | |||||
*e_code++ = '\t'; | |||||
pos = (pos & ~7) + 8; | |||||
} | |||||
} | |||||
while (pos < cur_dec_ind) { | |||||
CHECK_SIZE_CODE; | |||||
*e_code++ = ' '; | |||||
pos++; | |||||
} | |||||
if (e_code == startpos && ps.want_blank) { | |||||
*e_code++ = ' '; | |||||
ps.want_blank = false; | |||||
} | } | ||||
} | } |
Please use snprintf to prevent buffer overflows.