This commit is contained in:
Alexander Popov 2022-09-30 01:01:29 +03:00
parent 58035f08a7
commit 006c3d1501
Signed by: iiiypuk
GPG Key ID: D8C9B59A9F04A70C
10 changed files with 2941 additions and 2816 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
indent
*.BAK

281
args.c
View File

@ -77,77 +77,209 @@ struct pro {
int *p_obj; /* the associated variable */
} pro[] = {
{ "T", PRO_SPECIAL, 0, KEY, 0 },
{"bacc", PRO_BOOL, false, ON,
&blanklines_around_conditional_compilation },
{"badp", PRO_BOOL, false, ON,
&blanklines_after_declarations_at_proctop },
{"bad", PRO_BOOL, false, ON, &blanklines_after_declarations },
{"bap", PRO_BOOL, false, ON, &blanklines_after_procs },
{"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments },
{"bc", PRO_BOOL, true, OFF, &ps.leave_comma },
{"bl", PRO_BOOL, true, OFF, &btype_2 },
{"br", PRO_BOOL, true, ON, &btype_2 },
{"bs", PRO_BOOL, false, ON, &Bill_Shannon },
{"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline },
{"cd", PRO_INT, 0, 0, &ps.decl_com_ind },
{"ce", PRO_BOOL, true, ON, &cuddle_else },
{"ci", PRO_INT, 0, 0, &continuation_indent },
{"cli", PRO_SPECIAL, 0, CLI, 0 },
{"c", PRO_INT, 33, 0, &ps.com_ind },
{"di", PRO_INT, 16, 0, &ps.decl_indent },
{"dj", PRO_BOOL, false, ON, &ps.ljust_decl },
{"d", PRO_INT, 0, 0, &ps.unindent_displace },
{"eei", PRO_BOOL, false, ON, &extra_expression_indent },
{"ei", PRO_BOOL, true, ON, &ps.else_if },
{"fbc", PRO_FONT, 0, 0, (int *) &blkcomf },
{"fbx", PRO_FONT, 0, 0, (int *) &boxcomf },
{"fb", PRO_FONT, 0, 0, (int *) &bodyf },
{"fc1", PRO_BOOL, true, ON, &format_col1_comments },
{"fc", PRO_FONT, 0, 0, (int *) &scomf },
{"fk", PRO_FONT, 0, 0, (int *) &keywordf },
{"fs", PRO_FONT, 0, 0, (int *) &stringf },
{"ip", PRO_BOOL, true, ON, &ps.indent_parameters },
{"i", PRO_INT, 8, 0, &ps.ind_size },
{"lc", PRO_INT, 0, 0, &block_comment_max_col },
{"lp", PRO_BOOL, true, ON, &lineup_to_parens },
{"l", PRO_INT, 78, 0, &max_col },
{"nbacc", PRO_BOOL, false, OFF,
&blanklines_around_conditional_compilation },
{"nbadp", PRO_BOOL, false, OFF,
&blanklines_after_declarations_at_proctop },
{"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations },
{"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs },
{"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments },
{"nbc", PRO_BOOL, true, ON, &ps.leave_comma },
{"nbs", PRO_BOOL, false, OFF, &Bill_Shannon },
{"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline },
{"nce", PRO_BOOL, true, OFF, &cuddle_else },
{"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl },
{"neei", PRO_BOOL, false, OFF, &extra_expression_indent },
{"nei", PRO_BOOL, true, OFF, &ps.else_if },
{"nfc1", PRO_BOOL, true, OFF, &format_col1_comments },
{"nip", PRO_BOOL, true, OFF, &ps.indent_parameters },
{"nlp", PRO_BOOL, true, OFF, &lineup_to_parens },
{"npcs", PRO_BOOL, false, OFF, &proc_calls_space },
{"npro", PRO_SPECIAL, 0, IGN, 0 },
{"npsl", PRO_BOOL, true, OFF, &procnames_start_line },
{"nps", PRO_BOOL, false, OFF, &pointer_as_binop },
{"nsc", PRO_BOOL, true, OFF, &star_comment_cont },
{"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines },
{"nut", PRO_BOOL, true, OFF, &use_tabs},
{"nv", PRO_BOOL, false, OFF, &verbose },
{"pcs", PRO_BOOL, false, ON, &proc_calls_space },
{"psl", PRO_BOOL, true, ON, &procnames_start_line },
{"ps", PRO_BOOL, false, ON, &pointer_as_binop },
{"sc", PRO_BOOL, true, ON, &star_comment_cont },
{"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines },
{"st", PRO_SPECIAL, 0, STDIN, 0 },
{"troff", PRO_BOOL, false, ON, &troff },
{"ut", PRO_BOOL, true, ON, &use_tabs},
{"v", PRO_BOOL, false, ON, &verbose },
{
"T", PRO_SPECIAL, 0, KEY, 0
},
{
"bacc", PRO_BOOL, false, ON,
&blanklines_around_conditional_compilation
},
{
"badp", PRO_BOOL, false, ON,
&blanklines_after_declarations_at_proctop
},
{
"bad", PRO_BOOL, false, ON, &blanklines_after_declarations
},
{
"bap", PRO_BOOL, false, ON, &blanklines_after_procs
},
{
"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments
},
{
"bc", PRO_BOOL, true, OFF, &ps.leave_comma
},
{
"bl", PRO_BOOL, true, OFF, &btype_2
},
{
"br", PRO_BOOL, true, ON, &btype_2
},
{
"bs", PRO_BOOL, false, ON, &Bill_Shannon
},
{
"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline
},
{
"cd", PRO_INT, 0, 0, &ps.decl_com_ind
},
{
"ce", PRO_BOOL, true, ON, &cuddle_else
},
{
"ci", PRO_INT, 0, 0, &continuation_indent
},
{
"cli", PRO_SPECIAL, 0, CLI, 0
},
{
"c", PRO_INT, 33, 0, &ps.com_ind
},
{
"di", PRO_INT, 16, 0, &ps.decl_indent
},
{
"dj", PRO_BOOL, false, ON, &ps.ljust_decl
},
{
"d", PRO_INT, 0, 0, &ps.unindent_displace
},
{
"eei", PRO_BOOL, false, ON, &extra_expression_indent
},
{
"ei", PRO_BOOL, true, ON, &ps.else_if
},
{
"fbc", PRO_FONT, 0, 0, (int *) &blkcomf
},
{
"fbx", PRO_FONT, 0, 0, (int *) &boxcomf
},
{
"fb", PRO_FONT, 0, 0, (int *) &bodyf
},
{
"fc1", PRO_BOOL, true, ON, &format_col1_comments
},
{
"fc", PRO_FONT, 0, 0, (int *) &scomf
},
{
"fk", PRO_FONT, 0, 0, (int *) &keywordf
},
{
"fs", PRO_FONT, 0, 0, (int *) &stringf
},
{
"ip", PRO_BOOL, true, ON, &ps.indent_parameters
},
{
"i", PRO_INT, 8, 0, &ps.ind_size
},
{
"lc", PRO_INT, 0, 0, &block_comment_max_col
},
{
"lp", PRO_BOOL, true, ON, &lineup_to_parens
},
{
"l", PRO_INT, 78, 0, &max_col
},
{
"nbacc", PRO_BOOL, false, OFF,
&blanklines_around_conditional_compilation
},
{
"nbadp", PRO_BOOL, false, OFF,
&blanklines_after_declarations_at_proctop
},
{
"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations
},
{
"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs
},
{
"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments
},
{
"nbc", PRO_BOOL, true, ON, &ps.leave_comma
},
{
"nbs", PRO_BOOL, false, OFF, &Bill_Shannon
},
{
"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline
},
{
"nce", PRO_BOOL, true, OFF, &cuddle_else
},
{
"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl
},
{
"neei", PRO_BOOL, false, OFF, &extra_expression_indent
},
{
"nei", PRO_BOOL, true, OFF, &ps.else_if
},
{
"nfc1", PRO_BOOL, true, OFF, &format_col1_comments
},
{
"nip", PRO_BOOL, true, OFF, &ps.indent_parameters
},
{
"nlp", PRO_BOOL, true, OFF, &lineup_to_parens
},
{
"npcs", PRO_BOOL, false, OFF, &proc_calls_space
},
{
"npro", PRO_SPECIAL, 0, IGN, 0
},
{
"npsl", PRO_BOOL, true, OFF, &procnames_start_line
},
{
"nps", PRO_BOOL, false, OFF, &pointer_as_binop
},
{
"nsc", PRO_BOOL, true, OFF, &star_comment_cont
},
{
"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines
},
{
"nut", PRO_BOOL, true, OFF, &use_tabs
},
{
"nv", PRO_BOOL, false, OFF, &verbose
},
{
"pcs", PRO_BOOL, false, ON, &proc_calls_space
},
{
"psl", PRO_BOOL, true, ON, &procnames_start_line
},
{
"ps", PRO_BOOL, false, ON, &pointer_as_binop
},
{
"sc", PRO_BOOL, true, ON, &star_comment_cont
},
{
"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines
},
{
"st", PRO_SPECIAL, 0, STDIN, 0
},
{
"troff", PRO_BOOL, false, ON, &troff
},
{
"ut", PRO_BOOL, true, ON, &use_tabs
},
{
"v", PRO_BOOL, false, ON, &verbose
},
/* whew! */
{ 0, 0, 0, 0, 0 }
{
0, 0, 0, 0, 0
}
};
void scan_profile(FILE *);
@ -169,7 +301,8 @@ set_profile(void)
if (home != NULL && *home != '\0') {
if (snprintf(fname, sizeof fname, "%s/%s", home, prof) >= sizeof fname) {
#if defined(__linux__)
printf("%s/%s", home, prof); // FIXIT
printf("%s/%s", home, prof);
//FIXIT
#else
warnc(ENAMETOOLONG, "%s/%s", home, prof);
#endif
@ -197,15 +330,13 @@ scan_profile(FILE *f)
while (1) {
for (p = buf;
(i = getc(f)) != EOF && (*p = i) > ' ' && p + 1 - buf < BUFSIZ;
++p)
;
++p);
if (p != buf) {
*p = 0;
if (verbose)
printf("profile: %s\n", buf);
set_option(buf);
}
else if (i == EOF)
} else if (i == EOF)
return;
}
}

196
indent.c
View File

@ -44,8 +44,8 @@
#include <errno.h>
#include <err.h>
char *in_name = "Standard Input"; /* will always point to name of input
* file */
char *in_name = "Standard Input"; /* will always point to name
* of input file */
char *out_name = "Standard Output"; /* will always point to name
* of output file */
char bakfile[PATH_MAX] = "";
@ -79,8 +79,9 @@ char *buf_ptr; /* ptr to next character to be taken from
* in_buffer */
char *buf_end; /* ptr to first after last char in in_buffer */
char save_com[sc_size]; /* input text is saved here when looking for
* the brace after an if, while, etc */
char save_com[sc_size]; /* input text is saved here when
* looking for the brace after an if,
* while, etc */
char *sc_end; /* pointer into save_com buffer */
char *bp_save; /* saved value of buf_ptr when taking input
@ -106,11 +107,12 @@ int code_lines; /* count of lines with code */
int had_eof; /* set to true when input is exhausted */
int line_no; /* the current line number. */
int max_col; /* the maximum allowable line length */
int verbose; /* when true, non-essential error messages are
* printed */
int verbose; /* when true, non-essential error messages
* are printed */
int cuddle_else; /* true if else should cuddle up to '}' */
int star_comment_cont; /* true iff comment continuation lines should
* have stars at the beginning of each line. */
int star_comment_cont; /* true iff comment continuation
* lines should have stars at the
* beginning of each line. */
int comment_delimiter_on_blankline;
int troff; /* true iff were generating troff input */
int procnames_start_line; /* if true, the names of procedures
@ -118,19 +120,22 @@ int procnames_start_line; /* if true, the names of procedures
* 1 (ie. a newline is placed between
* the type of the procedure and its
* name) */
int proc_calls_space; /* If true, procedure calls look like:
* foo(bar) rather than foo (bar) */
int format_col1_comments; /* If comments which start in column 1
* are to be magically reformatted
int proc_calls_space; /* If true, procedure calls look
* like: foo(bar) rather than foo
* (bar) */
int format_col1_comments; /* If comments which start in column
* 1 are to be magically reformatted
* (just like comments that begin in
* later columns) */
int inhibit_formatting; /* true if INDENT OFF is in effect */
int suppress_blanklines;/* set iff following blanklines should be
* suppressed */
int continuation_indent;/* set to the indentation between the edge of
* code and continuation lines */
int lineup_to_parens; /* if true, continued code within parens will
* be lined up to the open paren */
int suppress_blanklines; /* set iff following blanklines
* should be suppressed */
int continuation_indent; /* set to the indentation between the
* edge of code and continuation
* lines */
int lineup_to_parens; /* if true, continued code within
* parens will be lined up to the
* open paren */
int Bill_Shannon; /* true iff a blank should always be inserted
* after sizeof */
int blanklines_after_declarations_at_proctop; /* This is vaguely
@ -147,14 +152,16 @@ int blanklines_after_declarations_at_proctop; /* This is vaguely
* if there are no
* declarations */
int block_comment_max_col;
int extra_expression_indent; /* True if continuation lines from the
* expression part of "if(e)",
* "while(e)", "for(e;e;e)" should be
* indented an extra tab stop so that
* they don't conflict with the code
int extra_expression_indent; /* True if continuation lines
* from the expression part
* of "if(e)", "while(e)",
* "for(e;e;e)" should be
* indented an extra tab stop
* so that they don't
* conflict with the code
* that follows */
int use_tabs; /* set true to use tabs for spacing,
* false uses all spaces */
int use_tabs; /* set true to use tabs for spacing, false
* uses all spaces */
/* -troff font state information */
@ -182,9 +189,10 @@ main(int argc, char **argv)
extern int found_err; /* flag set in diag() on error */
int dec_ind; /* current indentation for declarations */
int di_stack[20]; /* a stack of structure indentation levels */
int flushed_nl; /* used when buffering up comments to remember
* that a newline was passed over */
int di_stack[20]; /* a stack of structure indentation
* levels */
int flushed_nl; /* used when buffering up comments to
* remember that a newline was passed over */
int force_nl; /* when true, code must be broken */
int hd_type; /* used to store type of stmt for if (...),
* for (...), etc */
@ -311,8 +319,7 @@ main(int argc, char **argv)
if (input == NULL) /* check for open error */
err(1, "%s", in_name);
continue;
}
else if (output == 0) { /* we have the output file */
} else if (output == 0) { /* we have the output file */
out_name = argv[i]; /* remember name of output file */
if (strcmp(in_name, out_name) == 0) /* attempt to overwrite
* the file */
@ -323,8 +330,7 @@ main(int argc, char **argv)
continue;
}
errx(1, "unknown parameter: %s", argv[i]);
}
else
} else
set_option(argv[i]);
} /* end of for */
if (input == NULL) {
@ -386,8 +392,7 @@ main(int argc, char **argv)
ps.ind_level = ps.i_l_follow = col / ps.ind_size;
}
if (troff) {
char *p = in_name,
*beg = in_name;
char *p = in_name, *beg = in_name;
while (*p)
if (*p++ == '/')
@ -398,13 +403,14 @@ main(int argc, char **argv)
* START OF MAIN LOOP
*/
while (1) { /* this is the main loop. it will go until we
* reach eof */
while (1) { /* this is the main loop. it will go until
* we reach eof */
int is_procname;
type_code = lexi(); /* lexi reads one token. The actual
* characters read are stored in "token". lexi
* returns a code indicating the type of token */
* characters read are stored in "token".
* lexi returns a code indicating the type of
* token */
is_procname = ps.procname[0];
/*
@ -416,10 +422,10 @@ main(int argc, char **argv)
flushed_nl = false;
while (ps.search_brace) { /* if we scanned an if(), while(),
* etc., we might need to copy stuff
* into a buffer we must loop, copying
* stuff into save_com, until we find
* the start of the stmt which follows
* the if, or whatever */
* into a buffer we must loop,
* copying stuff into save_com, until
* we find the start of the stmt
* which follows the if, or whatever */
switch (type_code) {
case newline:
++line_no;
@ -430,8 +436,8 @@ main(int argc, char **argv)
case lbrace: /* this is a brace that starts the compound
* stmt */
if (sc_end == 0) { /* ignore buffering if a comment wasnt
* stored up */
if (sc_end == 0) { /* ignore buffering if a comment
* wasnt stored up */
ps.search_brace = false;
goto check_type;
}
@ -448,8 +454,7 @@ main(int argc, char **argv)
* must set up the buffer */
save_com[0] = save_com[1] = ' ';
sc_end = &(save_com[2]);
}
else {
} else {
*sc_end++ = '\n'; /* add newline between
* comments */
*sc_end++ = ' ';
@ -458,7 +463,8 @@ main(int argc, char **argv)
*sc_end++ = '/'; /* copy in start of comment */
*sc_end++ = '*';
for (;;) { /* loop until we get to the end of the comment */
for (;;) { /* loop until we get to the end of the
* comment */
*sc_end = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
@ -500,8 +506,9 @@ main(int argc, char **argv)
* read from the buffer */
*sc_end++ = '\n';
*sc_end++ = ' ';
if (verbose && !flushed_nl) /* print error msg if the line
* was not already broken */
if (verbose && !flushed_nl) /* print error msg if the
* line was not already
* broken */
diag(0, "Line broken");
flushed_nl = false;
}
@ -522,8 +529,8 @@ main(int argc, char **argv)
sc_end = 0;
break;
} /* end of switch */
if (type_code != 0) /* we must make this check, just in case there
* was an unexpected EOF */
if (type_code != 0) /* we must make this check, just in case
* there was an unexpected EOF */
type_code = lexi(); /* read another token */
/* if (ps.search_brace) ps.procname[0] = 0; */
if ((is_procname = ps.procname[0]) && flushed_nl
@ -565,9 +572,9 @@ check_type:
ps.want_blank = false; /* dont insert blank at line start */
force_nl = false;
}
ps.in_stmt = true; /* turn on flag which causes an extra level of
* indentation. this is turned off by a ; or
* '}' */
ps.in_stmt = true; /* turn on flag which causes an extra level
* of indentation. this is turned off by a ;
* or '}' */
if (s_com != e_com) { /* the turkey has embedded a comment
* in a line. fix it */
*e_code++ = ' ';
@ -580,8 +587,8 @@ check_type:
ps.want_blank = false;
e_com = s_com;
}
}
else if (type_code != comment) /* preserve force_nl thru a comment */
} else if (type_code != comment) /* preserve force_nl thru a
* comment */
force_nl = false; /* cancel forced newline after newline, form
* feed, etc */
@ -621,8 +628,7 @@ check_type:
"\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
e_code += strlen(e_code);
CHECK_SIZE_CODE;
}
else {
} else {
while ((e_code - s_code) < dec_ind) {
CHECK_SIZE_CODE;
*e_code++ = ' ';
@ -643,8 +649,8 @@ check_type:
* "int a(x) {..."
*/
parse(semicolon); /* I said this was a kluge... */
ps.in_or_st = false; /* turn off flag for structure decl or
* initialization */
ps.in_or_st = false; /* turn off flag for structure decl
* or initialization */
}
if (ps.sizeof_keyword)
ps.sizeof_mask |= 1 << ps.p_l_follow;
@ -694,8 +700,7 @@ check_type:
ps.dumped_decl_indent = 1;
e_code += strlen(e_code);
CHECK_SIZE_CODE;
}
else {
} else {
char *res = token;
if (ps.in_decl && !ps.block_init) { /* if this is a unary op
@ -790,7 +795,8 @@ check_type:
ps.in_stmt = false; /* seeing a label does not imply we are in a
* stmt */
for (t_ptr = s_code; *t_ptr; ++t_ptr)
*e_lab++ = *t_ptr; /* turn everything so far into a label */
*e_lab++ = *t_ptr; /* turn everything so far into a
* label */
e_code = s_code;
*e_lab++ = ':';
*e_lab++ = ' ';
@ -824,9 +830,10 @@ check_type:
*e_code++ = ' ';
}
ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
* structure declaration, we
* arent any more */
ps.in_decl = (ps.dec_nest > 0); /* if we were in a first
* level structure
* declaration, we arent any
* more */
if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
@ -868,8 +875,7 @@ check_type:
if (!btype_2) {
dump_line();
ps.want_blank = false;
}
else if (ps.in_parameter_declaration && !ps.in_or_st) {
} else if (ps.in_parameter_declaration && !ps.in_or_st) {
ps.i_l_follow = 0;
dump_line();
ps.want_blank = false;
@ -895,11 +901,12 @@ check_type:
* declaration or an init */
di_stack[ps.dec_nest++] = dec_ind;
/* ? dec_ind = 0; */
}
else {
} else {
ps.decl_on_line = false;
/* we can't be in the middle of a declaration, so don't do
* special indentation of comments */
/*
* we can't be in the middle of a declaration, so don't do
* special indentation of comments
*/
if (blanklines_after_declarations_at_proctop
&& ps.in_parameter_declaration)
postfix_blankline_requested = 1;
@ -977,18 +984,19 @@ check_type:
dump_line();/* make sure this starts a line */
ps.want_blank = false;
}
force_nl = true;/* also, following stuff must go onto new line */
force_nl = true;/* also, following stuff must go onto new
* line */
last_else = 1;
parse(elselit);
}
else {
} else {
if (e_code != s_code) { /* make sure this starts a line */
if (verbose)
diag(0, "Line broken");
dump_line();
ps.want_blank = false;
}
force_nl = true;/* also, following stuff must go onto new line */
force_nl = true;/* also, following stuff must go onto new
* line */
last_else = 0;
parse(dolit);
}
@ -1008,8 +1016,8 @@ check_type:
ps.ind_level = ps.i_l_follow = 1;
ps.ind_stmt = 0;
}
ps.in_or_st = true; /* this might be a structure or initialization
* declaration */
ps.in_or_st = true; /* this might be a structure or
* initialization declaration */
ps.in_decl = ps.decl_on_line = true;
if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
ps.just_saw_decl = 2;
@ -1058,7 +1066,6 @@ check_type:
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;
@ -1076,15 +1083,13 @@ check_type:
ps.want_blank = false;
}
}
}
else {
} else {
if (dec_ind && s_code != e_code)
dump_line();
dec_ind = 0;
ps.want_blank = false;
}
}
else if (sp_sw && ps.p_l_follow == 0) {
} else if (sp_sw && ps.p_l_follow == 0) {
sp_sw = false;
force_nl = true;
ps.last_u_d = true;
@ -1103,8 +1108,7 @@ check_type:
toupper((unsigned char) *t_ptr) : *t_ptr;
}
e_code = chfont(&keywordf, &bodyf, e_code);
}
else
} else
for (t_ptr = token; *t_ptr; ++t_ptr) {
CHECK_SIZE_CODE;
*e_code++ = *t_ptr;
@ -1120,8 +1124,8 @@ check_type:
case comma:
ps.want_blank = (s_code != e_code); /* only put blank after comma
* if comma does not start the
* line */
* if comma does not start
* the line */
if (ps.in_decl && is_procname == 0 && !ps.block_init)
while ((e_code - s_code) < (dec_ind - 1)) {
CHECK_SIZE_CODE;
@ -1237,11 +1241,9 @@ check_type:
if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) {
match_state[ifdef_level].tos = -1;
state_stack[ifdef_level++] = ps;
}
else
} else
diag(1, "#if stack overflow");
}
else if (strncmp(s_lab, "#else", 5) == 0)
} else if (strncmp(s_lab, "#else", 5) == 0)
if (ifdef_level <= 0)
diag(1, "Unmatched #else");
else {
@ -1270,7 +1272,8 @@ check_type:
}
}
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 comment this is a biggie */
if (flushed_nl) { /* we should force a broken line here */
@ -1297,8 +1300,7 @@ check_type:
void
bakcopy(void)
{
int n,
bakchn;
int n, bakchn;
char buff[8 * 1024];
char *p;
@ -1310,7 +1312,8 @@ bakcopy(void)
p++;
if (snprintf(bakfile, PATH_MAX, "%s.BAK", p) >= PATH_MAX)
#if defined(__linux__)
printf("%s.BAK", p); // FIXIT
printf("%s.BAK", p);
//FIXIT
#else
errc(1, ENAMETOOLONG, "%s.BAK", p);
#endif
@ -1337,7 +1340,8 @@ bakcopy(void)
int saved_errno = errno;
unlink(bakfile);
#if defined(__linux__)
printf("%s", in_name); // FIXIT
printf("%s", in_name);
//FIXIT
#else
errc(1, saved_errno, "%s", in_name);
#endif

1
indent.txt Normal file
View File

@ -0,0 +1 @@
:indent -nut -i4 -br

View File

@ -123,8 +123,9 @@ extern char *buf_ptr; /* ptr to next character to be taken from
* in_buffer */
extern char *buf_end; /* ptr to first after last char in in_buffer */
extern char save_com[sc_size]; /* input text is saved here when looking for
* the brace after an if, while, etc */
extern char save_com[sc_size]; /* input text is saved here when
* looking for the brace after an if,
* while, etc */
extern char *sc_end; /* pointer into save_com buffer */
extern char *bp_save; /* saved value of buf_ptr when taking input
@ -151,11 +152,12 @@ extern int code_lines; /* count of lines with code */
extern int had_eof; /* set to true when input is exhausted */
extern int line_no; /* the current line number. */
extern int max_col; /* the maximum allowable line length */
extern int verbose; /* when true, non-essential error messages are
* printed */
extern int verbose; /* when true, non-essential error messages
* are printed */
extern int cuddle_else; /* true if else should cuddle up to '}' */
extern int star_comment_cont; /* true iff comment continuation lines should
* have stars at the beginning of each line. */
extern int star_comment_cont; /* true iff comment continuation
* lines should have stars at the
* beginning of each line. */
extern int comment_delimiter_on_blankline;
extern int troff; /* true iff were generating troff input */
extern int procnames_start_line; /* if true, the names of procedures
@ -163,19 +165,22 @@ extern int procnames_start_line; /* if true, the names of procedures
* 1 (ie. a newline is placed between
* the type of the procedure and its
* name) */
extern int proc_calls_space; /* If true, procedure calls look like:
* foo(bar) rather than foo (bar) */
extern int format_col1_comments; /* If comments which start in column 1
* are to be magically reformatted
extern int proc_calls_space; /* If true, procedure calls look
* like: foo(bar) rather than foo
* (bar) */
extern int format_col1_comments; /* If comments which start in column
* 1 are to be magically reformatted
* (just like comments that begin in
* later columns) */
extern int inhibit_formatting; /* true if INDENT OFF is in effect */
extern int suppress_blanklines;/* set iff following blanklines should be
* suppressed */
extern int continuation_indent;/* set to the indentation between the edge of
* code and continuation lines */
extern int lineup_to_parens; /* if true, continued code within parens will
* be lined up to the open paren */
extern int suppress_blanklines; /* set iff following blanklines
* should be suppressed */
extern int continuation_indent; /* set to the indentation between the
* edge of code and continuation
* lines */
extern int lineup_to_parens; /* if true, continued code within
* parens will be lined up to the
* open paren */
extern int Bill_Shannon; /* true iff a blank should always be inserted
* after sizeof */
extern int blanklines_after_declarations_at_proctop; /* This is vaguely
@ -192,14 +197,16 @@ extern int blanklines_after_declarations_at_proctop; /* This is vaguely
* if there are no
* declarations */
extern int block_comment_max_col;
extern int extra_expression_indent; /* True if continuation lines from the
* expression part of "if(e)",
* "while(e)", "for(e;e;e)" should be
* indented an extra tab stop so that
* they don't conflict with the code
extern int extra_expression_indent; /* True if continuation lines
* from the expression part
* of "if(e)", "while(e)",
* "for(e;e;e)" should be
* indented an extra tab stop
* so that they don't
* conflict with the code
* that follows */
extern int use_tabs; /* set true to use tabs for spacing,
* false uses all spaces */
extern int use_tabs; /* set true to use tabs for spacing, false
* uses all spaces */
/* -troff font state information */
@ -224,13 +231,14 @@ extern struct parser_state {
int last_token;
struct fstate cfont; /* Current font */
int p_stack[STACKSIZE]; /* this is the parsers stack */
int il[STACKSIZE]; /* this stack stores indentation levels */
float cstk[STACKSIZE];/* used to store case stmt indentation levels */
int il[STACKSIZE]; /* this stack stores indentation
* levels */
float cstk[STACKSIZE]; /* used to store case stmt
* indentation levels */
int box_com; /* set to true when we are in a "boxed"
* comment. In that case, the first non-blank
* char should be lined up with the / in rem */
int comment_delta,
n_comment_delta;
int comment_delta, n_comment_delta;
int cast_mask; /* indicates which close parens close off
* casts */
int sizeof_mask;/* indicates which close parens close off
@ -243,8 +251,8 @@ extern struct parser_state {
int in_or_st; /* Will be true iff there has been a
* declarator (e.g. int or char) and no left
* paren since the last semicolon. When true,
* a '{' is starting a structure definition or
* an initialization list */
* a '{' is starting a structure definition
* or an initialization list */
int bl_line; /* set to 1 by dump_line if the line is blank */
int col_1; /* set to true if the last token started in
* column 1 */
@ -254,11 +262,12 @@ extern struct parser_state {
* of code should start */
int com_lines; /* the number of lines with comments, set by
* dump_line */
int dec_nest; /* current nesting level for structure or init */
int dec_nest; /* current nesting level for structure or
* init */
int decl_com_ind; /* the column in which comments after
* declarations should be put */
int decl_on_line; /* set to true if this line of code has part
* of a declaration on it */
int decl_on_line; /* set to true if this line of code
* has part of a declaration on it */
int i_l_follow; /* the level to which ind_level should be set
* after the current line is printed */
int in_decl; /* set to true when we are in a declaration
@ -288,26 +297,27 @@ extern struct parser_state {
int pcase; /* set to 1 if the current line label is a
* case. It is printed differently from a
* regular label */
int search_brace; /* set to true by parse when it is necessary
* to buffer up all info up to the start of a
* stmt after an if, while, etc */
int search_brace; /* set to true by parse when it is
* necessary to buffer up all info up
* to the start of a stmt after an
* if, while, etc */
int unindent_displace; /* comments not to the right of code
* will be placed this many
* indentation levels to the left of
* code */
int use_ff; /* set to one if the current line should be
* terminated with a form feed */
int want_blank; /* set to true when the following token should
* be prefixed by a blank. (Said prefixing is
* ignored in some cases.) */
int want_blank; /* set to true when the following token
* should be prefixed by a blank. (Said
* prefixing is ignored in some cases.) */
int else_if; /* True iff else if pairs should be handled
* specially */
int decl_indent;/* column to indent declared identifiers to */
int its_a_keyword;
int sizeof_keyword;
int dumped_decl_indent;
float case_indent; /* The distance to indent case labels from the
* switch statement */
float case_indent;/* The distance to indent case labels from
* the switch statement */
int in_parameter_declaration;
int indent_parameters;
int tos; /* pointer to top of stack */

37
io.c
View File

@ -71,8 +71,7 @@ dump_line(void)
ps.bl_line = true;
n_real_blanklines++;
}
}
else if (!inhibit_formatting) {
} else if (!inhibit_formatting) {
suppress_blanklines = 0;
ps.bl_line = false;
if (prefix_blankline_requested && not_first_line) {
@ -117,11 +116,10 @@ dump_line(void)
if (s < e_lab)
fprintf(output, s[0] == '/' && s[1] == '*' ? "\t%.*s" : "\t/* %.*s */",
(int) (e_lab - s), s);
}
else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
} else
fprintf(output, "%.*s", (int) (e_lab - s_lab), s_lab);
cur_col = count_spaces(cur_col, s_lab);
}
else
} else
cur_col = 1; /* there is no label section */
ps.pcase = false;
@ -252,19 +250,18 @@ inhibit_newline:
if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
prefix_blankline_requested = 1;
ps.just_saw_decl = 0;
}
else
} else
prefix_blankline_requested = postfix_blankline_requested;
postfix_blankline_requested = 0;
}
ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
* declaration, remember that fact for
* proper comment indentation */
* declaration, remember that fact
* for proper comment indentation */
ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
* indented if we have not
* completed this stmt and if
* we are not in the middle of
* a declaration */
* we are not in the middle
* of a declaration */
ps.use_ff = false;
ps.dumped_decl_indent = 0;
*(e_lab = s_lab) = '\0'; /* reset buffers */
@ -295,8 +292,7 @@ compute_code_target(void)
t -= w + 1;
if (t > target_col)
target_col = t;
}
else
} else
target_col = t;
}
else if (ps.ind_stmt)
@ -341,8 +337,8 @@ fill_buffer(void)
buf_end = be_save;
bp_save = be_save = 0;
if (buf_ptr < buf_end)
return; /* only return if there is really something in
* this buffer */
return; /* only return if there is really something
* in this buffer */
}
for (p = in_buffer;;) {
if (p >= in_buffer_limit) {
@ -536,8 +532,7 @@ diag(int level, const char *msg, ...)
fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
vfprintf(stdout, msg, ap);
fprintf(stdout, " */\n");
}
else {
} else {
fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
@ -563,8 +558,7 @@ chfont(struct fstate *of, struct fstate *nf, char *s)
*s++ = '(';
*s++ = nf->font[0];
*s++ = nf->font[1];
}
else
} else
*s++ = nf->font[0];
}
if (nf->size != of->size) {
@ -573,8 +567,7 @@ chfont(struct fstate *of, struct fstate *nf, char *s)
if (nf->size < of->size) {
*s++ = '-';
*s++ = '0' + of->size - nf->size;
}
else {
} else {
*s++ = '+';
*s++ = '0' + nf->size - of->size;
}

47
lexi.c
View File

@ -132,8 +132,8 @@ lexi(void)
ps.last_nl = false;
while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
ps.col_1 = false; /* leading blanks imply token is not in column
* 1 */
ps.col_1 = false; /* leading blanks imply token is not in
* column 1 */
if (++buf_ptr >= buf_end)
fill_buffer();
}
@ -144,13 +144,11 @@ lexi(void)
/*
* we have a character or number
*/
char *j; /* used for searching thru list of
* reserved words */
char *j; /* used for searching thru list of reserved
* words */
if (isdigit((unsigned char) *buf_ptr) ||
(buf_ptr[0] == '.' && isdigit((unsigned char) buf_ptr[1]))) {
int seendot = 0,
seenexp = 0,
seensfx = 0;
int seendot = 0, seenexp = 0, seensfx = 0;
if (*buf_ptr == '0' &&
(buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) {
*e_token++ = *buf_ptr++;
@ -159,8 +157,7 @@ lexi(void)
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
}
}
else
} else
while (1) {
if (*buf_ptr == '.') {
if (seendot)
@ -208,8 +205,7 @@ lexi(void)
*e_token++ = *buf_ptr++;
seensfx |= 1;
}
}
else
} else
while (chartype[(int) *buf_ptr] == alphanum) { /* copy it over */
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
@ -223,8 +219,8 @@ lexi(void)
}
ps.its_a_keyword = false;
ps.sizeof_keyword = false;
if (l_struct) { /* if last token was 'struct', then this token
* should be treated as a declaration */
if (l_struct) { /* if last token was 'struct', then this
* token should be treated as a declaration */
l_struct = false;
last_code = ident;
ps.last_u_d = true;
@ -242,9 +238,9 @@ lexi(void)
j = specials[i].rwd;
if (*j++ != *p++ || *j++ != *p++)
continue; /* This test depends on the fact that
* identifiers are always at least 1 character
* long (ie. the first two bytes of the
* identifier are always meaningful) */
* identifiers are always at least 1
* character long (ie. the first two bytes of
* the identifier are always meaningful) */
if (p[-1] == 0)
break; /* If its a one-character identifier */
while (*p++ == *j)
@ -326,9 +322,7 @@ lexi(void)
last_code = ident;
return (ident); /* the ident is not in the list */
} /* end of procesing for alpanum character */
/* Scan a non-alphanumeric token */
*e_token++ = *buf_ptr; /* if it is only a one-character token, it is
* moved here */
*e_token = '\0';
@ -362,9 +356,10 @@ lexi(void)
printf("%d: Unterminated literal\n", line_no);
goto stop_lit;
}
CHECK_SIZE_TOKEN; /* Only have to do this once in this loop,
* since CHECK_SIZE guarantees that there
* are at least 5 entries left */
CHECK_SIZE_TOKEN; /* Only have to do this once in this
* loop, since CHECK_SIZE guarantees
* that there are at least 5 entries
* left */
*e_token = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
@ -381,8 +376,7 @@ lexi(void)
* copied two chars */
if (buf_ptr >= buf_end)
fill_buffer();
}
else
} else
break; /* we copied one character */
} /* end of while (1) */
} while (*e_token++ != qchar);
@ -473,8 +467,7 @@ stop_lit:
/* check for following ++ or -- */
unary_delim = false;
}
}
else if (*buf_ptr == '=')
} else if (*buf_ptr == '=')
/* check for operator += */
*e_token++ = *buf_ptr++;
else if (*buf_ptr == '>') {
@ -493,7 +486,8 @@ stop_lit:
if (ps.in_or_st)
ps.block_init = 1;
#ifdef undef
if (chartype[*buf_ptr] == opchar) { /* we have two char assignment */
if (chartype[*buf_ptr] == opchar) { /* we have two char
* assignment */
e_token[-1] = *buf_ptr++;
if ((e_token[-1] == '<' || e_token[-1] == '>') && e_token[-1] == *buf_ptr)
*e_token++ = *buf_ptr++;
@ -597,7 +591,6 @@ addkey(char *key, int val)
specials = specials2;
maxspecials = newspecials;
}
p = &specials[nspecials];
p->rwd = key;
p->rwcode = val;

25
parse.c
View File

@ -39,8 +39,8 @@
void reduce(void);
void
parse(int tk) /* the code for the construct scanned */
{
parse(int tk)
{ /* the code for the construct scanned */
int i;
#ifdef debug
@ -127,8 +127,7 @@ parse(int tk) /* the code for the construct scanned */
ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
ps.p_stack[++ps.tos] = whilestmt;
ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
}
else { /* it is a while loop */
} else { /* it is a while loop */
ps.p_stack[++ps.tos] = whilestmt;
ps.il[ps.tos] = ps.i_l_follow;
++ps.i_l_follow;
@ -142,10 +141,10 @@ parse(int tk) /* the code for the construct scanned */
if (ps.p_stack[ps.tos] != ifhead)
diag(1, "Unmatched 'else'");
else {
ps.ind_level = ps.il[ps.tos]; /* indentation for else should
* be same as for if */
ps.i_l_follow = ps.ind_level + 1; /* everything following should
* be in 1 level */
ps.ind_level = ps.il[ps.tos]; /* indentation for else
* should be same as for if */
ps.i_l_follow = ps.ind_level + 1; /* everything following
* should be in 1 level */
ps.p_stack[ps.tos] = elsehead;
/* remember if with else */
ps.search_brace = btype_2 | ps.else_if;
@ -157,8 +156,7 @@ parse(int tk) /* the code for the construct scanned */
if (ps.p_stack[ps.tos - 1] == lbrace) {
ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
ps.p_stack[ps.tos] = stmt;
}
else
} else
diag(1, "Stmt nesting error.");
break;
@ -245,8 +243,8 @@ reduce(void)
int i;
for (;;) { /* keep looping until there is nothing left to
* reduce */
for (;;) { /* keep looping until there is nothing left
* to reduce */
switch (ps.p_stack[ps.tos]) {
@ -310,8 +308,7 @@ reduce(void)
/* it is termination of a do while */
ps.p_stack[--ps.tos] = stmt;
break;
}
else
} else
return;
default: /* anything else on top */

View File

@ -77,9 +77,10 @@ pr_comment(void)
char *last_bl; /* points to the last blank in the output
* buffer */
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
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 l_just_saw_decl = ps.just_saw_decl;
@ -87,26 +88,27 @@ pr_comment(void)
* int ps.last_nl = 0; true iff the last significant thing
* we've seen is a newline
*/
int one_liner = 1; /* true iff this comment is a one-liner */
int one_liner = 1; /* true iff this comment is a
* one-liner */
adj_max_col = max_col;
ps.just_saw_decl = 0;
last_bl = 0; /* no blanks found so far */
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.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 */
unix_comment = 1; /* set flag to let us figure out if there is a
* unix-style comment ** DISABLED: use 0 to
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 */
if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
* 1 it should not be touched */
if (ps.col_1 && !format_col1_comments) { /* if comment starts in
* column 1 it should not be
* touched */
ps.box_com = true;
ps.com_col = 1;
}
else {
} else {
if (*buf_ptr == '-' || *buf_ptr == '*' || *buf_ptr == '\n') {
ps.box_com = true; /* a comment with a '-', '*' or newline
* immediately after the / * is assumed to be
@ -123,8 +125,7 @@ pr_comment(void)
adj_max_col = block_comment_max_col;
if (ps.com_col <= 1)
ps.com_col = 1 + !format_col1_comments;
}
else {
} else {
int target_col;
break_delim = 0;
if (s_code != e_code)
@ -145,8 +146,7 @@ pr_comment(void)
buf_ptr[-2] = 0;
ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
buf_ptr[-2] = '/';
}
else {
} else {
ps.n_comment_delta = 0;
while (*buf_ptr == ' ' || *buf_ptr == '\t')
buf_ptr++;
@ -161,8 +161,7 @@ pr_comment(void)
if (troff) {
now_col = 1;
adj_max_col = 80;
}
else
} else
now_col = count_spaces(ps.com_col, s_com); /* figure what column we
* would be in if we
* printed the comment
@ -186,8 +185,7 @@ pr_comment(void)
*e_com++ = '*';
*e_com++ = ' ';
while (*++buf_ptr == ' ' || *buf_ptr == '\t');
}
else {
} else {
if (++buf_ptr >= buf_end)
fill_buffer();
*e_com++ = 014;
@ -202,9 +200,9 @@ pr_comment(void)
return;
}
one_liner = 0;
if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
* we don't ignore the newline
*/
if (ps.box_com || ps.last_nl) { /* if this is a boxed
* comment, we don't ignore
* the newline */
if (s_com == e_com) {
*e_com++ = ' ';
*e_com++ = ' ';
@ -230,8 +228,7 @@ pr_comment(void)
}
dump_line();
now_col = ps.com_col;
}
else {
} else {
ps.last_nl = 1;
if (unix_comment != 1) { /* we not are in unix_style
* comment */
@ -245,8 +242,8 @@ pr_comment(void)
if (ps.com_col <= 1)
ps.com_col = 2;
}
unix_comment = 2; /* permanently remember that we are in
* this type of comment */
unix_comment = 2; /* permanently remember that we are
* in this type of comment */
dump_line();
++line_no;
now_col = ps.com_col;
@ -288,8 +285,7 @@ pr_comment(void)
goto end_of_comment;
}
} while (*buf_ptr == ' ' || *buf_ptr == '\t');
}
else if (++buf_ptr >= buf_end)
} else if (++buf_ptr >= buf_end)
fill_buffer();
break; /* end of case for newline */
@ -336,8 +332,7 @@ pr_comment(void)
*e_com = '\0';
ps.just_saw_decl = l_just_saw_decl;
return;
}
else { /* handle isolated '*' */
} else { /* handle isolated '*' */
*e_com++ = '*';
++now_col;
}