indent(1): add new -sac and -U options.

Add -sac (space after cast) and -nsac options.
These control whether space character is put after a cast operator or not.
Default is -nsac.

Add -U option for providing a file containing list of types.
This is needed for properly deciding which asterisks denote unary
operation and which denote binary.

These come from PostgreSQL.

Reference:
84b00e3d46
49c52cf383

Differential Revision: https://reviews.freebsd.org/D6966  (Partial)
Submitted by:	Piotr Stefaniak
This commit is contained in:
Pedro F. Giffuni 2016-08-03 22:08:07 +00:00
parent 9c46493e50
commit 771aff0aa5
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=303735
5 changed files with 50 additions and 6 deletions

View File

@ -74,8 +74,12 @@ __FBSDID("$FreeBSD$");
static void scan_profile(FILE *);
#define KEY_FILE 5 /* only used for args */
const char *option_source = "?";
void add_typedefs_from_file(const char *str);
/*
* N.B.: because of the way the table here is scanned, options whose names are
* substrings of other options must occur later; that is, with -lp vs -l, -lp
@ -91,6 +95,7 @@ struct pro {
} pro[] = {
{"T", PRO_SPECIAL, 0, KEY, 0},
{"U", PRO_SPECIAL, 0, KEY_FILE, 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},
@ -147,6 +152,7 @@ struct pro {
{"npro", PRO_SPECIAL, 0, IGN, 0},
{"npsl", PRO_BOOL, true, OFF, &procnames_start_line},
{"nps", PRO_BOOL, false, OFF, &pointer_as_binop},
{"nsac", PRO_BOOL, false, OFF, &space_after_cast},
{"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
{"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
{"nut", PRO_BOOL, true, OFF, &use_tabs},
@ -154,6 +160,7 @@ struct pro {
{"pcs", PRO_BOOL, false, ON, &proc_calls_space},
{"psl", PRO_BOOL, true, ON, &procnames_start_line},
{"ps", PRO_BOOL, false, ON, &pointer_as_binop},
{"sac", PRO_BOOL, false, ON, &space_after_cast},
{"sc", PRO_BOOL, true, ON, &star_comment_cont},
{"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
{"st", PRO_SPECIAL, 0, STDIN, 0},
@ -295,6 +302,12 @@ set_option(char *arg)
}
break;
case KEY_FILE:
if (*param_start == 0)
goto need_param;
add_typedefs_from_file(param_start);
break;
default:
errx(1, "set_option: internal error: p_special %d", p->p_special);
}
@ -323,3 +336,21 @@ set_option(char *arg)
errx(1, "set_option: internal error: p_type %d", p->p_type);
}
}
void
add_typedefs_from_file(const char *str)
{
FILE *file;
char line[BUFSIZ];
if ((file = fopen(str, "r")) == NULL) {
fprintf(stderr, "indent: cannot open file %s\n", str);
exit(1);
}
while ((fgets(line, BUFSIZ, file)) != NULL) {
/* Remove trailing whitespace */
*(line + strcspn(line, " \t\n\r")) = '\0';
addkey(strdup(line), 4);
}
fclose(file);
}

View File

@ -30,7 +30,7 @@
.\" @(#)indent.1 8.1 (Berkeley) 7/1/93
.\" $FreeBSD$
.\"
.Dd March 3, 2012
.Dd August 3, 2016
.Dt INDENT 1
.Os
.Sh NAME
@ -74,6 +74,7 @@
.Op Fl npro
.Op Fl pcs | Fl npcs
.Op Fl psl | Fl npsl
.Op Fl sac | Fl nsac
.Op Fl \&sc | Fl nsc
.Bk -words
.Op Fl sob | Fl nsob
@ -81,6 +82,7 @@
.Op Fl \&st
.Op Fl \&ta
.Op Fl troff
.Op Fl U Ns Ar file
.Op Fl ut | Fl nut
.Op Fl v | Fl \&nv
.Sh DESCRIPTION
@ -378,6 +380,11 @@ column 1 \- their types, if any, will be left on the previous lines.
The
default is
.Fl psl .
.It Fl sac , nsac
Control whether parenthesized type names in casts are followed by a space or
not.
The default is
.Fl nsac .
.It Fl \&sc , nsc
Enables (disables) the placement of asterisks (`*'s) at the left edge of all
comments.
@ -430,6 +437,10 @@ listing in much the same spirit as
.Xr vgrind 1 .
If the output file is not specified, the default is standard output,
rather than formatting in place.
.It Fl U Ns Ar file
Adds type names from
.Ar file
to the list of type keywords.
.It Fl ut , nut
Enables (disables) the use of tab characters in the output.
Tabs are assumed to be aligned on columns divisible by 8.

View File

@ -555,7 +555,7 @@ main(int argc, char **argv)
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 = false;
ps.want_blank = space_after_cast;
} else
ps.want_blank = true;
ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;

View File

@ -212,6 +212,7 @@ int use_tabs; /* set true to use tabs for spacing,
* false uses all spaces */
int auto_typedefs; /* set true to recognize identifiers
* ending in "_t" like typedefs */
int space_after_cast; /* "b = (int) a" vs "b = (int)a" */
/* -troff font state information */

View File

@ -64,7 +64,7 @@ struct templ {
int rwcode;
};
struct templ specials[1000] =
struct templ specials[16384] =
{
{"switch", 7},
{"case", 8},
@ -595,9 +595,10 @@ addkey(char *key, int val)
return;
else
p++;
if (p >= specials + sizeof specials / sizeof specials[0])
return; /* For now, table overflows are silently
* ignored */
if (p >= specials + sizeof(specials) / sizeof(specials[0])) {
fprintf(stderr, "indent: typedef table overflow\n");
exit(1);
}
p->rwd = key;
p->rwcode = val;
p[1].rwd = NULL;