@@ -22,7 +22,7 @@ my $indent_opts =
22
22
my $devnull = File::Spec-> devnull;
23
23
24
24
my ($typedefs_file , $typedef_str , $code_base ,
25
- $ excludes , $indent , $build ,
25
+ @ excludes , $indent , $build ,
26
26
$show_diff , $silent_diff , $help );
27
27
28
28
$help = 0;
@@ -32,7 +32,7 @@ my %options = (
32
32
" typedefs=s" => \$typedefs_file ,
33
33
" list-of-typedefs=s" => \$typedef_str ,
34
34
" code-base=s" => \$code_base ,
35
- " excludes=s" => \$ excludes ,
35
+ " excludes=s" => \@ excludes ,
36
36
" indent=s" => \$indent ,
37
37
" build" => \$build ,
38
38
" show-diff" => \$show_diff ,
@@ -46,10 +46,8 @@ usage("Cannot have both --silent-diff and --show-diff")
46
46
47
47
run_build($code_base ) if ($build );
48
48
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) ,
51
50
# then locations. based on current dir, then default location
52
- $typedefs_file ||= shift if @ARGV && $ARGV [0] !~ / \. [ch]$ / ;
53
51
$typedefs_file ||= $ENV {PGTYPEDEFS };
54
52
55
53
# build mode sets PGINDENT
@@ -58,14 +56,15 @@ $indent ||= $ENV{PGINDENT} || $ENV{INDENT} || "pg_bsd_indent";
58
56
# no non-option arguments given. so do everything in the current directory
59
57
$code_base ||= ' .' unless @ARGV ;
60
58
59
+ my $sourcedir = locate_sourcedir();
60
+
61
61
# if it's the base of a postgres tree, we will exclude the files
62
62
# 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
+ }
69
68
70
69
# The typedef list that's mechanically extracted by the buildfarm may omit
71
70
# 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(
85
84
my @files ;
86
85
my $filtered_typedefs_fh ;
87
86
88
-
89
87
sub check_indent
90
88
{
91
89
system (" $indent -? < $devnull > $devnull 2>&1" );
@@ -114,26 +112,34 @@ sub check_indent
114
112
return ;
115
113
}
116
114
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
+ }
117
130
118
131
sub load_typedefs
119
132
{
120
-
121
133
# try fairly hard to find the typedefs file if it's not set
122
134
123
- foreach my $try (' .' , ' src/tools/pgindent ' , ' /usr/local/etc' )
135
+ foreach my $try (' .' , $sourcedir , ' /usr/local/etc' )
124
136
{
125
- $typedefs_file ||= " $try /typedefs.list"
137
+ last if $typedefs_file ;
138
+ next unless defined $try ;
139
+ $typedefs_file = " $try /typedefs.list"
126
140
if (-f " $try /typedefs.list" );
127
141
}
128
142
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
- }
137
143
die " cannot locate typedefs file \" $typedefs_file \"\n "
138
144
unless $typedefs_file && -f $typedefs_file ;
139
145
@@ -166,13 +172,13 @@ sub load_typedefs
166
172
return $filter_typedefs_fh ;
167
173
}
168
174
169
-
170
175
sub process_exclude
171
176
{
172
- if ( $excludes && @files )
177
+ foreach my $excl ( @excludes )
173
178
{
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 " ;
176
182
while (my $line = <$eh >)
177
183
{
178
184
chomp $line ;
@@ -185,7 +191,6 @@ sub process_exclude
185
191
return ;
186
192
}
187
193
188
-
189
194
sub read_source
190
195
{
191
196
my $source_filename = shift ;
@@ -200,7 +205,6 @@ sub read_source
200
205
return $source ;
201
206
}
202
207
203
-
204
208
sub write_source
205
209
{
206
210
my $source = shift ;
@@ -213,7 +217,6 @@ sub write_source
213
217
return ;
214
218
}
215
219
216
-
217
220
sub pre_indent
218
221
{
219
222
my $source = shift ;
@@ -242,7 +245,6 @@ sub pre_indent
242
245
return $source ;
243
246
}
244
247
245
-
246
248
sub post_indent
247
249
{
248
250
my $source = shift ;
@@ -270,7 +272,6 @@ sub post_indent
270
272
return $source ;
271
273
}
272
274
273
-
274
275
sub run_indent
275
276
{
276
277
my $source = shift ;
@@ -313,7 +314,6 @@ sub show_diff
313
314
return $diff ;
314
315
}
315
316
316
-
317
317
sub run_build
318
318
{
319
319
eval " use LWP::Simple;" ; # # no critic (ProhibitStringyEval);
@@ -359,7 +359,6 @@ sub run_build
359
359
return ;
360
360
}
361
361
362
-
363
362
sub build_clean
364
363
{
365
364
my $code_base = shift || ' .' ;
@@ -397,6 +396,7 @@ Options:
397
396
--build build the pg_bsd_indent program
398
397
--show-diff show the changes that would be made
399
398
--silent-diff exit with status 2 if any changes would be made
399
+ The --excludes option can be given more than once.
400
400
EOF
401
401
if ($help )
402
402
{
@@ -479,7 +479,6 @@ foreach my $source_filename (@files)
479
479
write_source($source , $source_filename );
480
480
}
481
481
}
482
-
483
482
}
484
483
485
484
build_clean($code_base ) if $build ;
0 commit comments