@@ -21,16 +21,28 @@ my $indent_opts =
2121
2222my $devnull = File::Spec-> devnull;
2323
24- my ($typedefs_file , $typedef_str , $code_base , $excludes , $indent , $build );
24+ my ($typedefs_file , $typedef_str , $code_base ,
25+ $excludes , $indent , $build ,
26+ $show_diff , $silent_diff , $help );
27+
28+ $help = 0;
2529
2630my %options = (
31+ " help" => \$help ,
2732 " typedefs=s" => \$typedefs_file ,
2833 " list-of-typedefs=s" => \$typedef_str ,
2934 " code-base=s" => \$code_base ,
3035 " excludes=s" => \$excludes ,
3136 " indent=s" => \$indent ,
32- " build" => \$build ,);
33- GetOptions(%options ) || die " bad command line argument\n " ;
37+ " build" => \$build ,
38+ " show-diff" => \$show_diff ,
39+ " silent-diff" => \$silent_diff ,);
40+ GetOptions(%options ) || usage(" bad command line argument" );
41+
42+ usage() if $help ;
43+
44+ usage(" Cannot have both --silent-diff and --show-diff" )
45+ if $silent_diff && $show_diff ;
3446
3547run_build($code_base ) if ($build );
3648
@@ -229,8 +241,7 @@ sub pre_indent
229241
230242sub post_indent
231243{
232- my $source = shift ;
233- my $source_filename = shift ;
244+ my $source = shift ;
234245
235246 # Restore CATALOG lines
236247 $source =~ s ! ^/\* (CATALOG\( .*)\* /$! $1 ! gm ;
@@ -280,33 +291,21 @@ sub run_indent
280291 close ($src_out );
281292
282293 return $source ;
283-
284294}
285295
286-
287- # for development diagnostics
288- sub diff
296+ sub show_diff
289297{
290- my $pre = shift ;
291- my $post = shift ;
292- my $flags = shift || " " ;
293-
294- print STDERR " running diff\n " ;
298+ my $indented = shift ;
299+ my $source_filename = shift ;
295300
296- my $pre_fh = new File::Temp(TEMPLATE => " pgdiffbXXXXX" );
297- my $post_fh = new File::Temp(TEMPLATE => " pgdiffaXXXXX" );
301+ my $post_fh = new File::Temp(TEMPLATE => " pgdiffXXXXX" );
298302
299- print $pre_fh $pre ;
300- print $post_fh $post ;
303+ print $post_fh $indented ;
301304
302- $pre_fh -> close ();
303305 $post_fh -> close ();
304306
305- system ( " diff $flags "
306- . $pre_fh -> filename . " "
307- . $post_fh -> filename
308- . " >&2" );
309- return ;
307+ my $diff = ` diff -upd $source_filename $post_fh ->filename 2>&1` ;
308+ return $diff ;
310309}
311310
312311
@@ -377,6 +376,34 @@ sub build_clean
377376 return ;
378377}
379378
379+ sub usage
380+ {
381+ my $message = shift ;
382+ my $helptext = <<'EOF' ;
383+ Usage:
384+ pgindent [OPTION]... [FILE]...
385+ Options:
386+ --help show this message and quit
387+ --typedefs=FILE file containing a list of typedefs
388+ --list-of-typedefs=STR string containing typedefs, space separated
389+ --code-base=DIR path to the base of PostgreSQL source code
390+ --excludes=PATH file containing list of filename patterns to ignore
391+ --indent=PATH path to pg_bsd_indent program
392+ --build build the pg_bsd_indent program
393+ --show-diff show the changes that would be made
394+ --silent-diff exit with status 2 if any changes would be made
395+ EOF
396+ if ($help )
397+ {
398+ print $helptext ;
399+ exit 0;
400+ }
401+ else
402+ {
403+ print STDERR " $message \n " , $helptext ;
404+ exit 1;
405+ }
406+ }
380407
381408# main
382409
@@ -404,6 +431,8 @@ push(@files, @ARGV);
404431
405432foreach my $source_filename (@files )
406433{
434+ # ignore anything that's not a .c or .h file
435+ next unless $source_filename =~ / \. [ch]$ / ;
407436
408437 # Automatically ignore .c and .h files that correspond to a .y or .l
409438 # file. indent tends to get badly confused by Bison/flex output,
@@ -427,9 +456,26 @@ foreach my $source_filename (@files)
427456 next ;
428457 }
429458
430- $source = post_indent($source , $source_filename );
459+ $source = post_indent($source );
460+
461+ if ($source ne $orig_source )
462+ {
463+ if ($silent_diff )
464+ {
465+ exit 2;
466+ }
467+ elsif ($show_diff )
468+ {
469+ print show_diff($source , $source_filename );
470+ }
471+ else
472+ {
473+ write_source($source , $source_filename );
474+ }
475+ }
431476
432- write_source($source , $source_filename ) if $source ne $orig_source ;
433477}
434478
435479build_clean($code_base ) if $build ;
480+
481+ exit 0;
0 commit comments