#include <sys/param.h>
#include <sys/capsicum.h>
#include <capsicum_helpers.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "indent_globs.h"
#include "indent_codes.h"
#include "indent.h"
FILE *input, *output;
char *labbuf, *s_lab, *e_lab, *l_lab;
char *codebuf, *s_code, *e_code, *l_code;
char *combuf, *s_com, *e_com, *l_com;
char *tokenbuf, *s_token, *e_token, *l_token;
char *in_buffer, *in_buffer_limit;
char *buf_ptr, *buf_end;
char sc_buf[sc_size];
char *save_com, *sc_end;
char *bp_save;
char *be_save;
struct options opt;
int line_no;
struct parser_state ps;
int ifdef_level;
struct parser_state state_stack[5];
struct parser_state match_state[5];
static void bakcopy(void);
static void indent_declaration(int, int);
const char *in_name = "Standard Input";
const char *out_name = "Standard Output";
const char *simple_backup_suffix = ".BAK";
int
main(int argc, char **argv)
{
cap_rights_t rights;
int dec_ind;
int di_stack[20];
int force_nl;
int hd_type = 0;
int i;
int scase;
int sp_sw;
int squest;
const char *t_ptr;
int tabs_to_var;
int type_code;
int last_else = 0;
const char *profile_name = NULL;
const char *envval = NULL;
struct parser_state transient_state;
found_err = 0;
ps.p_stack[0] = stmt;
ps.last_nl = true;
ps.last_token = semicolon;
combuf = (char *) malloc(bufsize);
if (combuf == NULL)
err(1, NULL);
labbuf = (char *) malloc(bufsize);
if (labbuf == NULL)
err(1, NULL);
codebuf = (char *) malloc(bufsize);
if (codebuf == NULL)
err(1, NULL);
tokenbuf = (char *) malloc(bufsize);
if (tokenbuf == NULL)
err(1, NULL);
alloc_typenames();
init_constant_tt();
l_com = combuf + bufsize - 5;
l_lab = labbuf + bufsize - 5;
l_code = codebuf + bufsize - 5;
l_token = tokenbuf + bufsize - 5;
combuf[0] = codebuf[0] = labbuf[0] = ' ';
combuf[1] = codebuf[1] = labbuf[1] = '\0';
opt.else_if = 1;
s_lab = e_lab = labbuf + 1;
s_code = e_code = codebuf + 1;
s_com = e_com = combuf + 1;
s_token = e_token = tokenbuf + 1;
in_buffer = (char *) malloc(10);
if (in_buffer == NULL)
err(1, NULL);
in_buffer_limit = in_buffer + 8;
buf_ptr = buf_end = in_buffer;
line_no = 1;
had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
sp_sw = force_nl = false;
ps.in_or_st = false;
ps.bl_line = true;
dec_ind = 0;
di_stack[ps.dec_nest = 0] = 0;
ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
scase = ps.pcase = false;
squest = 0;
sc_end = NULL;
bp_save = NULL;
be_save = NULL;
output = NULL;
tabs_to_var = 0;
envval = getenv("SIMPLE_BACKUP_SUFFIX");
if (envval)
simple_backup_suffix = envval;
#ifdef undef
max_col = 78;
lineup_to_parens = 1;
lineup_to_parens_always = 0;
ps.ljust_decl = 0;
ps.com_ind = 33;
star_comment_cont = 1;
ps.ind_size = 8;
verbose = 0;
ps.decl_indent = 16;
ps.local_decl_indent = -1;
ps.indent_parameters = 1;
ps.decl_com_ind = 0;
btype_2 = 1;
cuddle_else = 1;
ps.unindent_displace = 0;
ps.case_indent = 0;
format_block_comments = 1;
format_col1_comments = 1;
procnames_start_line = 1;
proc_calls_space = 0;
comment_delimiter_on_blankline = 1;
ps.leave_comma = 1;
#endif
for (i = 1; i < argc; ++i)
if (strcmp(argv[i], "-npro") == 0)
break;
else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
profile_name = argv[i];
set_defaults();
if (i >= argc)
set_profile(profile_name);
for (i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
if (input == NULL) {
in_name = argv[i];
input = fopen(in_name, "r");
if (input == NULL)
err(1, "%s", in_name);
continue;
}
else if (output == NULL) {
out_name = argv[i];
if (strcmp(in_name, out_name) == 0) {
errx(1, "input and output files must be different");
}
output = fopen(out_name, "w");
if (output == NULL)
err(1, "%s", out_name);
continue;
}
errx(1, "unknown parameter: %s", argv[i]);
}
else
set_option(argv[i]);
}
if (input == NULL)
input = stdin;
if (output == NULL) {
if (input == stdin)
output = stdout;
else {
out_name = in_name;
bakcopy();
}
}
cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
if (caph_rights_limit(fileno(output), &rights) < 0)
err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
if (caph_rights_limit(fileno(input), &rights) < 0)
err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
if (caph_enter() < 0)
err(EXIT_FAILURE, "unable to enter capability mode");
if (opt.com_ind <= 1)
opt.com_ind = 2;
if (opt.block_comment_max_col <= 0)
opt.block_comment_max_col = opt.max_col;
if (opt.local_decl_indent < 0)
opt.local_decl_indent = opt.decl_indent;
if (opt.decl_com_ind <= 0)
opt.decl_com_ind = opt.ljust_decl ? (opt.com_ind <= 10 ? 2 : opt.com_ind - 8) : opt.com_ind;
if (opt.continuation_indent == 0)
opt.continuation_indent = opt.ind_size;
fill_buffer();
parse(semicolon);
{
char *p = buf_ptr;
int col = 1;
while (1) {
if (*p == ' ')
col++;
else if (*p == '\t')
col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1;
else
break;
p++;
}
if (col > opt.ind_size)
ps.ind_level = ps.i_l_follow = col / opt.ind_size;
}
while (1) {
int comment_buffered = false;
type_code = lexi(&ps);
while (ps.search_brace) {
switch (type_code) {
case newline:
if (sc_end == NULL) {
save_com = sc_buf;
save_com[0] = save_com[1] = ' ';
sc_end = &save_com[2];
}
*sc_end++ = '\n';
force_nl = false;
case form_feed:
break;
case comment:
if (sc_end == NULL) {
memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
save_com = sc_buf + (buf_ptr - in_buffer - 4);
save_com[0] = save_com[1] = ' ';
sc_end = &save_com[2];
}
comment_buffered = true;
*sc_end++ = '/';
*sc_end++ = '*';
for (;;) {
*sc_end = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
if (*sc_end++ == '*' && *buf_ptr == '/')
break;
if (sc_end >= &save_com[sc_size]) {
diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
fflush(output);
exit(1);
}
}
*sc_end++ = '/';
if (++buf_ptr >= buf_end)
fill_buffer();
break;
case lbrace:
if (sc_end != NULL && opt.btype_2) {
save_com[0] = '{';
while (isspace((unsigned char)*buf_ptr)) {
if (++buf_ptr >= buf_end)
fill_buffer();
if (*buf_ptr == '\n')
break;
}
goto sw_buffer;
}
default:
{
int remove_newlines;
remove_newlines =
(type_code == sp_nparen && *token == 'e' &&
e_code != s_code && e_code[-1] == '}')
|| (type_code == sp_paren && *token == 'i' &&
last_else && opt.else_if);
if (remove_newlines)
force_nl = false;
if (sc_end == NULL) {
ps.search_brace = false;
goto check_type;
}
while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) {
sc_end--;
}
if (opt.swallow_optional_blanklines ||
(!comment_buffered && remove_newlines)) {
force_nl = !remove_newlines;
while (sc_end > save_com && sc_end[-1] == '\n') {
sc_end--;
}
}
if (force_nl) {
force_nl = false;
--line_no;
*sc_end++ = '\n';
*sc_end++ = ' ';
if (opt.verbose)
diag2(0, "Line broken");
}
for (t_ptr = token; *t_ptr; ++t_ptr)
*sc_end++ = *t_ptr;
sw_buffer:
ps.search_brace = false;
bp_save = buf_ptr;
be_save = buf_end;
buf_ptr = save_com;
*sc_end++ = ' ';
buf_end = sc_end;
sc_end = NULL;
break;
}
}
if (type_code != 0) {
if (sc_end != NULL) {
while (*buf_ptr == ' ' || *buf_ptr == '\t') {
*sc_end++ = *buf_ptr++;
if (sc_end >= &save_com[sc_size]) {
errx(1, "input too long");
}
}
if (buf_ptr >= buf_end) {
fill_buffer();
}
}
transient_state = ps;
type_code = lexi(&transient_state);
if (type_code != newline && type_code != form_feed &&
type_code != comment && !transient_state.search_brace) {
ps = transient_state;
}
}
}
last_else = 0;
check_type:
if (type_code == 0) {
if (s_lab != e_lab || s_code != e_code
|| s_com != e_com)
dump_line();
if (ps.tos > 1)
diag2(1, "Stuff missing from end of file");
if (opt.verbose) {
printf("There were %d output lines and %d comments\n",
ps.out_lines, ps.out_coms);
printf("(Lines with comments)/(Lines with code): %6.3f\n",
(1.0 * ps.com_lines) / code_lines);
}
fflush(output);
exit(found_err);
}
if (
(type_code != comment) &&
(type_code != newline) &&
(type_code != preesc) &&
(type_code != form_feed)) {
if (force_nl &&
(type_code != semicolon) &&
(type_code != lbrace || !opt.btype_2)) {
if (opt.verbose)
diag2(0, "Line broken");
dump_line();
ps.want_blank = false;
force_nl = false;
}
ps.in_stmt = true;
if (s_com != e_com) {
int len = e_com - s_com;
CHECK_SIZE_CODE(len + 3);
*e_code++ = ' ';
memcpy(e_code, s_com, len);
e_code += len;
*e_code++ = ' ';
*e_code = '\0';
ps.want_blank = false;
e_com = s_com;
}
}
else if (type_code != comment)
force_nl = false;
CHECK_SIZE_CODE(3);
switch (type_code) {
case form_feed:
ps.use_ff = true;
dump_line();
ps.want_blank = false;
break;
case newline:
if (ps.last_token != comma || ps.p_l_follow > 0
|| !opt.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
dump_line();
ps.want_blank = false;
}
++line_no;
break;
case lparen:
if (++ps.p_l_follow == nitems(ps.paren_indents)) {
diag3(0, "Reached internal limit of %d unclosed parens",
nitems(ps.paren_indents));
ps.p_l_follow--;
}
if (*token == '[')
;
else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
ps.procname[0] == '\0' && ps.paren_level == 0) {
indent_declaration(dec_ind, tabs_to_var);
ps.dumped_decl_indent = true;
}
else if (ps.want_blank &&
((ps.last_token != ident && ps.last_token != funcname) ||
opt.proc_calls_space ||
ps.keyword + opt.Bill_Shannon > 2))
*e_code++ = ' ';
ps.want_blank = false;
*e_code++ = token[0];
ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
&& ps.paren_indents[0] < 2 * opt.ind_size)
ps.paren_indents[0] = 2 * opt.ind_size;
if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
parse(semicolon);
ps.in_or_st = false;
}
if (ps.keyword == 1 || ps.keyword == 2)
ps.not_cast_mask |= 1 << ps.p_l_follow;
break;
case rparen:
if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) {
ps.last_u_d = true;
ps.cast_mask &= (1 << ps.p_l_follow) - 1;
ps.want_blank = opt.space_after_cast;
} else
ps.want_blank = true;
ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
if (--ps.p_l_follow < 0) {
ps.p_l_follow = 0;
diag3(0, "Extra %c", *token);
}
if (e_code == s_code)
ps.paren_level = ps.p_l_follow;
*e_code++ = token[0];
if (sp_sw && (ps.p_l_follow == 0)) {
sp_sw = false;
force_nl = true;
ps.last_u_d = true;
ps.in_stmt = false;
parse(hd_type);
}
ps.search_brace = opt.btype_2;
break;
case unary_op:
if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
ps.procname[0] == '\0' && ps.paren_level == 0) {
for (i = 0; token[i]; ++i)
;
indent_declaration(dec_ind - i, tabs_to_var);
ps.dumped_decl_indent = true;
}
else if (ps.want_blank)
*e_code++ = ' ';
{
int len = e_token - s_token;
CHECK_SIZE_CODE(len);
memcpy(e_code, token, len);
e_code += len;
}
ps.want_blank = false;
break;
case binary_op:
{
int len = e_token - s_token;
CHECK_SIZE_CODE(len + 1);
if (ps.want_blank)
*e_code++ = ' ';
memcpy(e_code, token, len);
e_code += len;
}
ps.want_blank = true;
break;
case postop:
*e_code++ = token[0];
*e_code++ = token[1];
ps.want_blank = true;
break;
case question:
squest++;
if (ps.want_blank)
*e_code++ = ' ';
*e_code++ = '?';
ps.want_blank = true;
break;
case casestmt:
scase = true;
goto copy_id;
case colon:
if (squest > 0) {
--squest;
if (ps.want_blank)
*e_code++ = ' ';
*e_code++ = ':';
ps.want_blank = true;
break;
}
if (ps.in_or_st) {
*e_code++ = ':';
ps.want_blank = false;
break;
}
ps.in_stmt = false;
{
int len = e_code - s_code;
CHECK_SIZE_LAB(len + 3);
memcpy(e_lab, s_code, len);
e_lab += len;
*e_lab++ = ':';
*e_lab = '\0';
e_code = s_code;
}
force_nl = ps.pcase = scase;
scase = false;
ps.want_blank = false;
break;
case semicolon:
if (ps.dec_nest == 0)
ps.in_or_st = false;
scase = false;
squest = 0;
if (ps.last_token == rparen)
ps.in_parameter_declaration = 0;
ps.cast_mask = 0;
ps.not_cast_mask = 0;
ps.block_init = 0;
ps.block_init_level = 0;
ps.just_saw_decl--;
if (ps.in_decl && s_code == e_code && !ps.block_init &&
!ps.dumped_decl_indent && ps.paren_level == 0) {
indent_declaration(dec_ind - 1, tabs_to_var);
ps.dumped_decl_indent = true;
}
ps.in_decl = (ps.dec_nest > 0);
if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
diag2(1, "Unbalanced parens");
ps.p_l_follow = 0;
if (sp_sw) {
sp_sw = false;
parse(hd_type);
}
}
*e_code++ = ';';
ps.want_blank = true;
ps.in_stmt = (ps.p_l_follow > 0);
if (!sp_sw) {
parse(semicolon);
force_nl = true;
}
break;
case lbrace:
ps.in_stmt = false;
if (!ps.block_init)
force_nl = true;
else if (ps.block_init_level <= 0)
ps.block_init_level = 1;
else
ps.block_init_level++;
if (s_code != e_code && !ps.block_init) {
if (!opt.btype_2) {
dump_line();
ps.want_blank = false;
}
else if (ps.in_parameter_declaration && !ps.in_or_st) {
ps.i_l_follow = 0;
if (opt.function_brace_split) {
dump_line();
ps.want_blank = false;
} else
ps.want_blank = true;
}
}
if (ps.in_parameter_declaration)
prefix_blankline_requested = 0;
if (ps.p_l_follow > 0) {
diag2(1, "Unbalanced parens");
ps.p_l_follow = 0;
if (sp_sw) {
sp_sw = false;
parse(hd_type);
ps.ind_level = ps.i_l_follow;
}
}
if (s_code == e_code)
ps.ind_stmt = false;
if (ps.in_decl && ps.in_or_st) {
di_stack[ps.dec_nest] = dec_ind;
if (++ps.dec_nest == nitems(di_stack)) {
diag3(0, "Reached internal limit of %d struct levels",
nitems(di_stack));
ps.dec_nest--;
}
}
else {
ps.decl_on_line = false;
if (opt.blanklines_after_declarations_at_proctop
&& ps.in_parameter_declaration)
postfix_blankline_requested = 1;
ps.in_parameter_declaration = 0;
ps.in_decl = false;
}
dec_ind = 0;
parse(lbrace);
if (ps.want_blank)
*e_code++ = ' ';
ps.want_blank = false;
*e_code++ = '{';
ps.just_saw_decl = 0;
break;
case rbrace:
if (ps.p_stack[ps.tos] == decl && !ps.block_init)
parse(semicolon);
if (ps.p_l_follow) {
diag2(1, "Unbalanced parens");
ps.p_l_follow = 0;
sp_sw = false;
}
ps.just_saw_decl = 0;
ps.block_init_level--;
if (s_code != e_code && !ps.block_init) {
if (opt.verbose)
diag2(0, "Line broken");
dump_line();
}
*e_code++ = '}';
ps.want_blank = true;
ps.in_stmt = ps.ind_stmt = false;
if (ps.dec_nest > 0) {
dec_ind = di_stack[--ps.dec_nest];
if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
ps.just_saw_decl = 2;
ps.in_decl = true;
}
prefix_blankline_requested = 0;
parse(rbrace);
ps.search_brace = opt.cuddle_else && ps.p_stack[ps.tos] == ifhead
&& ps.il[ps.tos] >= ps.ind_level;
if (ps.tos <= 1 && opt.blanklines_after_procs && ps.dec_nest <= 0)
postfix_blankline_requested = 1;
break;
case swstmt:
sp_sw = true;
hd_type = swstmt;
goto copy_id;
case sp_paren:
sp_sw = true;
hd_type = (*token == 'i' ? ifstmt :
(*token == 'w' ? whilestmt : forstmt));
goto copy_id;
case sp_nparen:
ps.in_stmt = false;
if (*token == 'e') {
if (e_code != s_code && (!opt.cuddle_else || e_code[-1] != '}')) {
if (opt.verbose)
diag2(0, "Line broken");
dump_line();
ps.want_blank = false;
}
force_nl = true;
last_else = 1;
parse(elselit);
}
else {
if (e_code != s_code) {
if (opt.verbose)
diag2(0, "Line broken");
dump_line();
ps.want_blank = false;
}
force_nl = true;
last_else = 0;
parse(dolit);
}
goto copy_id;
case type_def:
case storage:
prefix_blankline_requested = 0;
goto copy_id;
case structure:
if (ps.p_l_follow > 0)
goto copy_id;
case decl:
parse(decl);
if (ps.last_token == rparen && ps.tos <= 1) {
if (s_code != e_code) {
dump_line();
ps.want_blank = 0;
}
}
if (ps.in_parameter_declaration && opt.indent_parameters && ps.dec_nest == 0) {
ps.ind_level = ps.i_l_follow = 1;
ps.ind_stmt = 0;
}
ps.in_or_st = true;
ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
if ( ps.dec_nest <= 0)
ps.just_saw_decl = 2;
prefix_blankline_requested = 0;
for (i = 0; token[i++];);
if (ps.ind_level == 0 || ps.dec_nest > 0) {
dec_ind = opt.decl_indent > 0 ? opt.decl_indent : i;
tabs_to_var = (opt.use_tabs ? opt.decl_indent > 0 : 0);
} else {
dec_ind = opt.local_decl_indent > 0 ? opt.local_decl_indent : i;
tabs_to_var = (opt.use_tabs ? opt.local_decl_indent > 0 : 0);
}
goto copy_id;
case funcname:
case ident:
if (ps.in_decl) {
if (type_code == funcname) {
ps.in_decl = false;
if (opt.procnames_start_line && s_code != e_code) {
*e_code = '\0';
dump_line();
}
else if (ps.want_blank) {
*e_code++ = ' ';
}
ps.want_blank = false;
}
else if (!ps.block_init && !ps.dumped_decl_indent &&
ps.paren_level == 0) {
indent_declaration(dec_ind, tabs_to_var);
ps.dumped_decl_indent = true;
ps.want_blank = false;
}
}
else if (sp_sw && ps.p_l_follow == 0) {
sp_sw = false;
force_nl = true;
ps.last_u_d = true;
ps.in_stmt = false;
parse(hd_type);
}
copy_id:
{
int len = e_token - s_token;
CHECK_SIZE_CODE(len + 1);
if (ps.want_blank)
*e_code++ = ' ';
memcpy(e_code, s_token, len);
e_code += len;
}
if (type_code != funcname)
ps.want_blank = true;
break;
case strpfx:
{
int len = e_token - s_token;
CHECK_SIZE_CODE(len + 1);
if (ps.want_blank)
*e_code++ = ' ';
memcpy(e_code, token, len);
e_code += len;
}
ps.want_blank = false;
break;
case period:
*e_code++ = '.';
ps.want_blank = false;
break;
case comma:
ps.want_blank = (s_code != e_code);
if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
!ps.dumped_decl_indent && ps.paren_level == 0) {
indent_declaration(dec_ind - 1, tabs_to_var);
ps.dumped_decl_indent = true;
}
*e_code++ = ',';
if (ps.p_l_follow == 0) {
if (ps.block_init_level <= 0)
ps.block_init = 0;
if (break_comma && (!opt.leave_comma ||
count_spaces_until(compute_code_target(), s_code, e_code) >
opt.max_col - opt.tabsize))
force_nl = true;
}
break;
case preesc:
if ((s_com != e_com) ||
(s_lab != e_lab) ||
(s_code != e_code))
dump_line();
CHECK_SIZE_LAB(1);
*e_lab++ = '#';
{
int in_comment = 0;
int com_start = 0;
char quote = 0;
int com_end = 0;
while (*buf_ptr == ' ' || *buf_ptr == '\t') {
buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
}
while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
CHECK_SIZE_LAB(2);
*e_lab = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
switch (*e_lab++) {
case BACKSLASH:
if (!in_comment) {
*e_lab++ = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
}
break;
case '/':
if (*buf_ptr == '*' && !in_comment && !quote) {
in_comment = 1;
*e_lab++ = *buf_ptr++;
com_start = e_lab - s_lab - 2;
}
break;
case '"':
if (quote == '"')
quote = 0;
break;
case '\'':
if (quote == '\'')
quote = 0;
break;
case '*':
if (*buf_ptr == '/' && in_comment) {
in_comment = 0;
*e_lab++ = *buf_ptr++;
com_end = e_lab - s_lab;
}
break;
}
}
while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
e_lab--;
if (e_lab - s_lab == com_end && bp_save == NULL) {
if (sc_end == NULL) {
save_com = sc_buf;
sc_end = &save_com[0];
}
else {
*sc_end++ = '\n';
*sc_end++ = ' ';
--line_no;
}
if (sc_end - save_com + com_end - com_start > sc_size)
errx(1, "input too long");
memmove(sc_end, s_lab + com_start, com_end - com_start);
sc_end += com_end - com_start;
e_lab = s_lab + com_start;
while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
e_lab--;
bp_save = buf_ptr;
be_save = buf_end;
buf_ptr = save_com;
*sc_end++ = ' ';
buf_end = sc_end;
sc_end = NULL;
}
CHECK_SIZE_LAB(1);
*e_lab = '\0';
ps.pcase = false;
}
if (strncmp(s_lab, "#if", 3) == 0) {
if ((size_t)ifdef_level < nitems(state_stack)) {
match_state[ifdef_level].tos = -1;
state_stack[ifdef_level++] = ps;
}
else
diag2(1, "#if stack overflow");
}
else if (strncmp(s_lab, "#el", 3) == 0) {
if (ifdef_level <= 0)
diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
else {
match_state[ifdef_level - 1] = ps;
ps = state_stack[ifdef_level - 1];
}
}
else if (strncmp(s_lab, "#endif", 6) == 0) {
if (ifdef_level <= 0)
diag2(1, "Unmatched #endif");
else
ifdef_level--;
} else {
struct directives {
int size;
const char *string;
}
recognized[] = {
{7, "include"},
{6, "define"},
{5, "undef"},
{4, "line"},
{5, "error"},
{6, "pragma"}
};
int d = nitems(recognized);
while (--d >= 0)
if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
break;
if (d < 0) {
diag2(1, "Unrecognized cpp directive");
break;
}
}
if (opt.blanklines_around_conditional_compilation) {
postfix_blankline_requested++;
n_real_blanklines = 0;
}
else {
postfix_blankline_requested = 0;
prefix_blankline_requested = 0;
}
break;
case comment:
pr_comment();
break;
}
*e_code = '\0';
if (type_code != comment && type_code != newline && type_code != preesc)
ps.last_token = type_code;
}
}
static void
bakcopy(void)
{
static char buff[8 * 1024];
char *bakfile;
ssize_t len;
int bakfd;
if (asprintf(&bakfile, "%s%s", in_name, simple_backup_suffix) < 0)
err(1, "%s%s", in_name, simple_backup_suffix);
bakfd = open(bakfile, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (bakfd < 0)
err(1, "%s", bakfile);
while ((len = read(fileno(input), buff, sizeof(buff))) > 0)
if (write(bakfd, buff, len) != len)
err(1, "%s", bakfile);
if (len < 0)
err(1, "%s", in_name);
fclose(input);
input = fdopen(bakfd, "r");
if (input == NULL || fseek(input, 0, SEEK_SET) != 0)
err(1, "%s", bakfile);
output = fopen(in_name, "w");
if (output == NULL) {
unlink(bakfile);
err(1, "%s", in_name);
}
free(bakfile);
}
static void
indent_declaration(int cur_dec_ind, int tabs_to_var)
{
int pos = e_code - s_code;
char *startpos = e_code;
if ((ps.ind_level * opt.ind_size) % opt.tabsize != 0) {
pos += (ps.ind_level * opt.ind_size) % opt.tabsize;
cur_dec_ind += (ps.ind_level * opt.ind_size) % opt.tabsize;
}
if (tabs_to_var) {
int tpos;
CHECK_SIZE_CODE(cur_dec_ind / opt.tabsize);
while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_dec_ind) {
*e_code++ = '\t';
pos = tpos;
}
}
CHECK_SIZE_CODE(cur_dec_ind - pos + 1);
while (pos < cur_dec_ind) {
*e_code++ = ' ';
pos++;
}
if (e_code == startpos && ps.want_blank) {
*e_code++ = ' ';
ps.want_blank = false;
}
}