7 use Cwd qw(abs_path getcwd);
9 use File::Spec qw(devnull);
14 # Update for pg_bsd_indent version
15 my $INDENT_VERSION = "1.1";
16 my $devnull = File::Spec->devnull;
18 # Common indent settings
20 "-bad -bap -bc -bl -d0 -cdb -nce -nfc1 -di12 -i4 -l79 -lp -nip -npro -bbb";
22 # indent-dependant settings
25 my ($typedefs_file, $code_base, $excludes, $indent, $build);
28 "typedefs=s" => \$typedefs_file,
29 "code-base=s" => \$code_base,
30 "excludes=s" => \$excludes,
31 "indent=s" => \$indent,
33 GetOptions(%options) || die "bad command line";
35 run_build($code_base) if ($build);
37 # command line option wins, then first non-option arg,
38 # then environment (which is how --build sets it) ,
39 # then locations. based on current dir, then default location
40 $typedefs_file ||= shift if @ARGV && $ARGV[0] !~ /\\.[ch]$/;
41 $typedefs_file ||= $ENV{PGTYPEDEFS};
43 # build mode sets PGINDENT and PGENTAB
44 $indent ||= $ENV{PGINDENT} || $ENV{INDENT} || "pg_bsd_indent";
45 my $entab = $ENV{PGENTAB} || "entab";
47 # no non-option arguments given. so do everything in the current directory
48 $code_base ||= '.' unless @ARGV;
50 # if it's the base of a postgres tree, we will exclude the files
51 # postgres wants excluded
52 $excludes ||= "$code_base/src/tools/pgindent/exclude_file_patterns"
53 if $code_base && -f "$code_base/src/tools/pgindent/exclude_file_patterns";
57 my $filtered_typedefs_fh;
62 system("entab < $devnull");
66 "Go to the src/tools/entab directory and do 'make' and 'make install'.\n",
67 "This will put the 'entab' command in your path.\n",
68 "Then run $0 again.\n";
72 system("$indent -? < $devnull > $devnull 2>&1");
76 "You do not appear to have 'indent' installed on your system.\n";
80 if (`$indent -V` !~ m/ $INDENT_VERSION$/)
83 "You do not appear to have $indent version $INDENT_VERSION installed on your system.\n";
87 system("$indent -gnu < $devnull > $devnull 2>&1");
91 "You appear to have GNU indent rather than BSD indent.\n",
92 "See the pgindent/README file for a description of its problems.\n";
93 $extra_opts = "-cdb -bli0 -npcs -cli4 -sc";
97 $extra_opts = "-cli1";
105 # try fairly hard to find the typedefs file if it's not set
107 foreach my $try ('.', 'src/tools/pgindent', '/usr/local/etc')
109 $typedefs_file ||= "$try/typedefs.list"
110 if (-f "$try/typedefs.list");
113 # try to find typedefs by moving up directory levels
117 $typedefs_file ||= "$tdtry/src/tools/pgindent/typedefs.list"
118 if (-f "$tdtry/src/tools/pgindent/typedefs.list");
119 $tdtry = "$tdtry/..";
121 die "no typedefs file" unless $typedefs_file && -f $typedefs_file;
123 open(my $typedefs_fh, '<', $typedefs_file)
124 || die "opening $typedefs_file: $!";
125 my @typedefs = <$typedefs_fh>;
128 # remove certain entries
130 grep { !m/^(FD_SET|date|interval|timestamp|ANY)\n?$/ } @typedefs;
132 # write filtered typedefs
133 my $filter_typedefs_fh = new File::Temp(TEMPLATE => "pgtypedefXXXXX");
134 print $filter_typedefs_fh @typedefs;
135 $filter_typedefs_fh->close();
137 # temp file remains because we return a file handle reference
138 return $filter_typedefs_fh;
144 if ($excludes && @files)
146 open(my $eh, '<', $excludes) || die "opening $excludes";
147 while (my $line = <$eh>)
151 eval " \$rgx = qr!$line!;";
152 @files = grep { $_ !~ /$rgx/ } @files if $rgx;
161 my $source_filename = shift;
164 open(my $src_fd, '<', $source_filename)
165 || die "opening $source_filename: $!";
177 my $source_filename = shift;
179 open(my $src_fh, '>', $source_filename)
180 || die "opening $source_filename: $!";
181 print $src_fh $source;
190 # remove trailing whitespace
191 $source =~ s/[ \t]+$//gm;
195 # Convert // comments to /* */
196 $source =~ s!^([ \t]*)//(.*)$!$1/* $2 */!gm;
198 # 'else' followed by a single-line comment, followed by
199 # a brace on the next line confuses BSD indent, so we push
200 # the comment down to the next line, then later pull it
201 # back up again. Add space before _PGMV or indent will add
203 # AMD: A symptom of not getting this right is that you see errors like:
204 # FILE: ../../../src/backend/rewrite/rewriteHandler.c
206 # Stuff missing from end of file
207 $source =~ s!(\}|[ \t])else[ \t]*(/\*)(.*\*/)[ \t]*$!$1else\n $2 _PGMV$3!gm;
209 # Indent multi-line after-'else' comment so BSD indent will move it
210 # properly. We already moved down single-line comments above.
211 # Check for '*' to make sure we are not in a single-line comment that
212 # has other text on the line.
213 $source =~ s!(\}|[ \t])else[ \t]*(/\*[^*]*)[ \t]*$!$1else\n $2!gm;
215 # Mark some comments for special treatment later
216 $source =~ s!/\* +---!/*---X_X!g;
220 # Work around bug where function that defines no local variables
221 # misindents switch() case lines and line after #else. Do not do
223 my @srclines = split(/\n/, $source);
224 foreach my $lno (1 .. $#srclines)
226 my $l2 = $srclines[$lno];
228 # Line is only a single open brace in column 0
229 next unless $l2 =~ /^\{[ \t]*$/;
231 # previous line has a closing paren
232 next unless $srclines[ $lno - 1 ] =~ /\)/;
234 # previous line was struct, etc.
236 if $srclines[ $lno - 1 ] =~
237 m!=|^(struct|enum|[ \t]*typedef|extern[ \t]+"C")!;
239 $srclines[$lno] = "$l2\nint pgindent_func_no_var_fix;";
241 $source = join("\n", @srclines) . "\n"; # make sure there's a final \n
243 # Prevent indenting of code in 'extern "C"' blocks.
244 # we replace the braces with comments which we'll reverse later
245 my $extern_c_start = '/* Open extern "C" */';
246 my $extern_c_stop = '/* Close extern "C" */';
248 s!(^#ifdef[ \t]+__cplusplus.*\nextern[ \t]+"C"[ \t]*\n)\{[ \t]*$!$1$extern_c_start!gm;
249 $source =~ s!(^#ifdef[ \t]+__cplusplus.*\n)\}[ \t]*$!$1$extern_c_stop!gm;
258 my $source_filename = shift;
260 # put back braces for extern "C"
261 $source =~ s!^/\* Open extern "C" \*/$!{!gm;
262 $source =~ s!^/\* Close extern "C" \*/$!}!gm;
266 # remove special comment marker
267 $source =~ s!/\*---X_X!/* ---!g;
269 # Pull up single-line comment after 'else' that was pulled down above
270 $source =~ s!else\n[ \t]+/\* _PGMV!else\t/*!g;
272 # Indent single-line after-'else' comment by only one tab.
273 $source =~ s!(\}|[ \t])else[ \t]+(/\*.*\*/)[ \t]*$!$1else\t$2!gm;
275 # Add tab before comments with no whitespace before them (on a tab stop)
276 $source =~ s!(\S)(/\*.*\*/)$!$1\t$2!gm;
278 # Remove blank line between opening brace and block comment.
279 $source =~ s!(\t*\{\n)\n([ \t]+/\*)$!$1$2!gm;
283 # Reduce whitespace between #endif and comments to one tab
284 $source =~ s!^\#endif[ \t]+/\*!#endif /*!gm;
286 # Remove blank line(s) before #else, #elif, and #endif
287 $source =~ s!\n\n+(\#else|\#elif|\#endif)!\n$1!g;
289 # Add blank line before #endif if it is the last line in the file
290 $source =~ s!\n(#endif.*)\n\z!\n\n$1\n!;
294 # Work around misindenting of function with no variables defined.
295 $source =~ s!^[ \t]*int[ \t]+pgindent_func_no_var_fix;[ \t]*\n{1,2}!!gm;
297 # Use a single space before '*' in function return types
298 $source =~ s!^([A-Za-z_]\S*)[ \t]+\*$!$1 *!gm;
300 # Move prototype names to the same line as return type. Useful
301 # for ctags. Indent should do this, but it does not. It formats
302 # prototypes just like real functions.
304 my $ident = qr/[a-zA-Z_][a-zA-Z_0-9]*/;
305 my $comment = qr!/\*.*\*/!;
308 (\n$ident[^(\n]*)\n # e.g. static void
310 $ident\(\n? # func_name(
311 (.*,([ \t]*$comment)?\n)* # args b4 final ln
312 .*\);([ \t]*$comment)?$ # final line
314 !$1 . (substr($1,-1,1) eq '*' ? '' : ' ') . $2!gmxe;
318 # Remove too much indenting after closing brace.
319 $source =~ s!^\}\t[ \t]+!}\t!gm;
321 # Workaround indent bug that places excessive space before 'static'.
322 $source =~ s!^static[ \t]+!static !gm;
324 # Remove leading whitespace from typedefs
325 $source =~ s!^[ \t]+typedef enum!typedef enum!gm
326 if $source_filename =~ 'libpq-(fe|events).h$';
328 # Remove trailing blank lines
329 $source =~ s!\n+\z!\n!;
338 my $error_message = shift;
341 "$indent $indent_opts $extra_opts -U" . $filtered_typedefs_fh->filename;
343 my $tmp_fh = new File::Temp(TEMPLATE => "pgsrcXXXXX");
344 my $filename = $tmp_fh->filename;
345 print $tmp_fh $source;
348 $$error_message = `$cmd $filename 2>&1`;
350 return "" if ($? || length($$error_message) > 0);
352 unlink "$filename.BAK";
354 open(my $src_out, '<', $filename);
356 $source = <$src_out>;
363 # XXX Ideally we'd implement entab/detab in pure perl.
369 my $tmp_fh = new File::Temp(TEMPLATE => "pgdetXXXXX");
370 print $tmp_fh $source;
373 open(my $entab, '-|', "$entab -d -t4 -qc " . $tmp_fh->filename);
386 my $tmp_fh = new File::Temp(TEMPLATE => "pgentXXXXX");
387 print $tmp_fh $source;
390 open(my $entab, '-|',
391 "$entab -d -t8 -qc " . $tmp_fh->filename . " | $entab -t4 -qc");
400 # for development diagnostics
405 my $flags = shift || "";
407 print STDERR "running diff\n";
409 my $pre_fh = new File::Temp(TEMPLATE => "pgdiffbXXXXX");
410 my $post_fh = new File::Temp(TEMPLATE => "pgdiffaXXXXX");
413 print $post_fh $post;
418 system( "diff $flags "
419 . $pre_fh->filename . " "
427 eval "use LWP::Simple;";
429 my $code_base = shift || '.';
430 my $save_dir = getcwd();
432 # look for the code root
435 last if -d "$code_base/src/tools/pgindent";
436 $code_base = "$code_base/..";
439 die "no src/tools/pgindent directory in $code_base"
440 unless -d "$code_base/src/tools/pgindent";
442 chdir "$code_base/src/tools/pgindent";
444 my $rv = getstore("http://buildfarm.postgresql.org/cgi-bin/typedefs.pl",
445 "tmp_typedefs.list");
447 die "fetching typedefs.list" unless is_success($rv);
449 $ENV{PGTYPEDEFS} = abs_path('tmp_typedefs.list');
452 getstore("ftp://ftp.postgresql.org/pub/dev/indent.netbsd.patched.tgz",
453 "indent.netbsd.patched.tgz");
455 die "fetching indent.netbsd.patched.tgz" unless is_success($rv);
457 # XXX add error checking here
461 system("tar -z -xf ../indent.netbsd.patched.tgz");
462 system("make > $devnull 2>&1");
464 $ENV{PGINDENT} = abs_path('indent');
468 system("make > $devnull 2>&1");
470 $ENV{PGENTAB} = abs_path('entab');
479 my $code_base = shift || '.';
481 # look for the code root
484 last if -d "$code_base/src/tools/pgindent";
485 $code_base = "$code_base/..";
488 die "no src/tools/pgindent directory in $code_base"
489 unless -d "$code_base/src/tools/pgindent";
493 system("rm -rf src/tools/pgindent/bsdindent");
494 system("git clean -q -f src/tools/entab src/tools/pgindent");
500 # get the list of files under code base, if it's set
503 my ($dev, $ino, $mode, $nlink, $uid, $gid);
504 (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))
507 && push(@files, $File::Find::name);
510 $code_base) if $code_base;
514 $filtered_typedefs_fh = load_typedefs();
518 # make sure we process any non-option arguments.
521 foreach my $source_filename (@files)
523 my $source = read_source($source_filename);
524 my $error_message = '';
526 $source = pre_indent($source);
528 # Protect backslashes in DATA() and wrapping in CATALOG()
530 $source = detab($source);
531 $source =~ s!^((DATA|CATALOG)\(.*)$!/*$1*/!gm;
533 $source = run_indent($source, \$error_message);
536 print STDERR "Failure in $source_filename: " . $error_message . "\n";
540 # Restore DATA/CATALOG lines; must be done here so tab alignment is preserved
541 $source =~ s!^/\*((DATA|CATALOG)\(.*)\*/$!$1!gm;
542 $source = entab($source);
544 $source = post_indent($source, $source_filename);
546 write_source($source, $source_filename);
549 build_clean($code_base) if $build;