Skip to content

Commit

Permalink
[feature] Add -U option for providing a file containing list of types.
Browse files Browse the repository at this point in the history
This is needed for properly deciding which asterisks denote unary operation and which denote binary.
Ported from PostgreSQL.
  • Loading branch information
pstef committed Jul 31, 2016
1 parent 84b00e3 commit 49c52cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
30 changes: 30 additions & 0 deletions args.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 205989 2010-03-31 17:05:30Z avg $

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
Expand All @@ -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},
Expand Down Expand Up @@ -299,6 +304,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);
}
Expand Down Expand Up @@ -327,3 +338,22 @@ 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);
}
5 changes: 5 additions & 0 deletions indent.1
Original file line number Diff line number Diff line change
Expand Up @@ -82,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
Expand Down Expand Up @@ -436,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.
Expand Down
9 changes: 5 additions & 4 deletions lexi.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct templ {
int rwcode;
};

struct templ specials[1000] =
struct templ specials[16384] =
{
{"switch", 7},
{"case", 8},
Expand Down Expand Up @@ -597,9 +597,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 = 0;
Expand Down

0 comments on commit 49c52cf

Please sign in to comment.