@@ -21,16 +21,28 @@ my $indent_opts =
21
21
22
22
my $devnull = File::Spec-> devnull;
23
23
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;
25
29
26
30
my %options = (
31
+ " help" => \$help ,
27
32
" typedefs=s" => \$typedefs_file ,
28
33
" list-of-typedefs=s" => \$typedef_str ,
29
34
" code-base=s" => \$code_base ,
30
35
" excludes=s" => \$excludes ,
31
36
" 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 ;
34
46
35
47
run_build($code_base ) if ($build );
36
48
@@ -229,8 +241,7 @@ sub pre_indent
229
241
230
242
sub post_indent
231
243
{
232
- my $source = shift ;
233
- my $source_filename = shift ;
244
+ my $source = shift ;
234
245
235
246
# Restore CATALOG lines
236
247
$source =~ s ! ^/\* (CATALOG\( .*)\* /$! $1 ! gm ;
@@ -280,33 +291,21 @@ sub run_indent
280
291
close ($src_out );
281
292
282
293
return $source ;
283
-
284
294
}
285
295
286
-
287
- # for development diagnostics
288
- sub diff
296
+ sub show_diff
289
297
{
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 ;
295
300
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" );
298
302
299
- print $pre_fh $pre ;
300
- print $post_fh $post ;
303
+ print $post_fh $indented ;
301
304
302
- $pre_fh -> close ();
303
305
$post_fh -> close ();
304
306
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 ;
310
309
}
311
310
312
311
@@ -377,6 +376,34 @@ sub build_clean
377
376
return ;
378
377
}
379
378
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
+ }
380
407
381
408
# main
382
409
@@ -404,6 +431,8 @@ push(@files, @ARGV);
404
431
405
432
foreach my $source_filename (@files )
406
433
{
434
+ # ignore anything that's not a .c or .h file
435
+ next unless $source_filename =~ / \. [ch]$ / ;
407
436
408
437
# Automatically ignore .c and .h files that correspond to a .y or .l
409
438
# file. indent tends to get badly confused by Bison/flex output,
@@ -427,9 +456,26 @@ foreach my $source_filename (@files)
427
456
next ;
428
457
}
429
458
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
+ }
431
476
432
- write_source($source , $source_filename ) if $source ne $orig_source ;
433
477
}
434
478
435
479
build_clean($code_base ) if $build ;
480
+
481
+ exit 0;
0 commit comments