@@ -22,7 +22,7 @@ my $indent_opts =
2222my $devnull = File::Spec-> devnull;
2323
2424my ($typedefs_file ,$typedef_str ,$code_base ,
25- $ excludes ,$indent ,$build ,
25+ @ excludes ,$indent ,$build ,
2626$show_diff ,$silent_diff ,$help );
2727
2828$help = 0;
@@ -32,7 +32,7 @@ my %options = (
3232" typedefs=s" => \$typedefs_file ,
3333" list-of-typedefs=s" => \$typedef_str ,
3434" code-base=s" => \$code_base ,
35- " excludes=s" => \$ excludes ,
35+ " excludes=s" => \@ excludes ,
3636" indent=s" => \$indent ,
3737" build" => \$build ,
3838" show-diff" => \$show_diff ,
@@ -46,10 +46,8 @@ usage("Cannot have both --silent-diff and --show-diff")
4646
4747run_build($code_base )if ($build );
4848
49- # command line option wins, then first non-option arg,
50- # then environment (which is how --build sets it) ,
49+ # command line option wins, then environment (which is how --build sets it) ,
5150# then locations. based on current dir, then default location
52- $typedefs_file ||=shift if @ARGV &&$ARGV [0] !~/ \. [ch]$ / ;
5351$typedefs_file ||=$ENV {PGTYPEDEFS };
5452
5553# build mode sets PGINDENT
@@ -58,14 +56,15 @@ $indent ||= $ENV{PGINDENT} || $ENV{INDENT} || "pg_bsd_indent";
5856# no non-option arguments given. so do everything in the current directory
5957$code_base ||=' .' unless @ARGV ;
6058
59+ my $sourcedir = locate_sourcedir();
60+
6161# if it's the base of a postgres tree, we will exclude the files
6262# postgres wants excluded
63- $excludes ||=" $code_base /src/tools/pgindent/exclude_file_patterns"
64- if $code_base &&-f " $code_base /src/tools/pgindent/exclude_file_patterns" ;
65-
66- # also look under the current directory for the exclude patterns file
67- $excludes ||=" src/tools/pgindent/exclude_file_patterns"
68- if -f " src/tools/pgindent/exclude_file_patterns" ;
63+ if ($sourcedir )
64+ {
65+ my $exclude_candidate =" $sourcedir /exclude_file_patterns" ;
66+ push (@excludes ,$exclude_candidate )if -f $exclude_candidate ;
67+ }
6968
7069# The typedef list that's mechanically extracted by the buildfarm may omit
7170# some names we want to treat like typedefs, e.g. "bool" (which is a macro
@@ -85,7 +84,6 @@ my %excluded = map { +"$_\n" => 1 } qw(
8584my @files ;
8685my $filtered_typedefs_fh ;
8786
88-
8987sub check_indent
9088{
9189system (" $indent -? <$devnull >$devnull 2>&1" );
@@ -114,26 +112,34 @@ sub check_indent
114112return ;
115113}
116114
115+ sub locate_sourcedir
116+ {
117+ # try fairly hard to locate the sourcedir
118+ my $where =$code_base ||' .' ;
119+ my $sub =" $where /src/tools/pgindent" ;
120+ return $sub if -d $sub ;
121+ # try to find it from an ancestor directory
122+ $sub =" ../src/tools/pgindent" ;
123+ foreach (1..4)
124+ {
125+ return $sub if -d $sub ;
126+ $sub =" ../$sub " ;
127+ }
128+ return ;# undef if nothing found
129+ }
117130
118131sub load_typedefs
119132{
120-
121133# try fairly hard to find the typedefs file if it's not set
122134
123- foreach my $try (' .' ,' src/tools/pgindent ' ,' /usr/local/etc' )
135+ foreach my $try (' .' ,$sourcedir ,' /usr/local/etc' )
124136{
125- $typedefs_file ||=" $try /typedefs.list"
137+ last if $typedefs_file ;
138+ next unless defined $try ;
139+ $typedefs_file =" $try /typedefs.list"
126140if (-f " $try /typedefs.list" );
127141}
128142
129- # try to find typedefs by moving up directory levels
130- my $tdtry =" .." ;
131- foreach (1 .. 5)
132- {
133- $typedefs_file ||=" $tdtry /src/tools/pgindent/typedefs.list"
134- if (-f " $tdtry /src/tools/pgindent/typedefs.list" );
135- $tdtry =" $tdtry /.." ;
136- }
137143die " cannot locate typedefs file\" $typedefs_file \"\n "
138144unless $typedefs_file &&-f $typedefs_file ;
139145
@@ -166,13 +172,13 @@ sub load_typedefs
166172return $filter_typedefs_fh ;
167173}
168174
169-
170175sub process_exclude
171176{
172- if ( $excludes && @files )
177+ foreach my $excl ( @excludes )
173178{
174- open (my $eh ,' <' ,$excludes )
175- ||die " cannot open exclude file\" $excludes \"\n " ;
179+ last unless @files ;
180+ open (my $eh ,' <' ,$excl )
181+ ||die " cannot open exclude file\" $excl \"\n " ;
176182while (my $line = <$eh >)
177183{
178184chomp $line ;
@@ -185,7 +191,6 @@ sub process_exclude
185191return ;
186192}
187193
188-
189194sub read_source
190195{
191196my $source_filename =shift ;
@@ -200,7 +205,6 @@ sub read_source
200205return $source ;
201206}
202207
203-
204208sub write_source
205209{
206210my $source =shift ;
@@ -213,7 +217,6 @@ sub write_source
213217return ;
214218}
215219
216-
217220sub pre_indent
218221{
219222my $source =shift ;
@@ -242,7 +245,6 @@ sub pre_indent
242245return $source ;
243246}
244247
245-
246248sub post_indent
247249{
248250my $source =shift ;
@@ -270,7 +272,6 @@ sub post_indent
270272return $source ;
271273}
272274
273-
274275sub run_indent
275276{
276277my $source =shift ;
@@ -313,7 +314,6 @@ sub show_diff
313314return $diff ;
314315}
315316
316-
317317sub run_build
318318{
319319eval " use LWP::Simple;" ;# # no critic (ProhibitStringyEval);
@@ -359,7 +359,6 @@ sub run_build
359359return ;
360360}
361361
362-
363362sub build_clean
364363{
365364my $code_base =shift ||' .' ;
@@ -397,6 +396,7 @@ Options:
397396--build build the pg_bsd_indent program
398397--show-diff show the changes that would be made
399398--silent-diff exit with status 2 if any changes would be made
399+ The --excludes option can be given more than once.
400400EOF
401401if ($help )
402402{
@@ -479,7 +479,6 @@ foreach my $source_filename (@files)
479479write_source($source ,$source_filename );
480480}
481481}
482-
483482}
484483
485484build_clean($code_base )if $build ;