withlibctx.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #! /usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use File::Temp qw/tempfile/;
  5. my $topdir = shift;
  6. processallfiles($topdir);
  7. print "Success\n";
  8. sub processallfiles {
  9. my $dir = shift;
  10. my @files = glob "$dir/*.c $dir/*.h $dir/*.h.in $dir/*.pod *dir/*.pod.in";
  11. open (my $STDOUT_ORIG, '>&', STDOUT);
  12. foreach my $file (@files) {
  13. my ($tmpfh, $tmpfile) = tempfile();
  14. print "Processing $file\n";
  15. open(STDOUT, '>>', $tmpfile);
  16. open(INFILE, $file);
  17. processfile(\*INFILE);
  18. close(STDOUT);
  19. rename($tmpfile, $file);
  20. unlink($tmpfile);
  21. # restore STDOUT
  22. open (STDOUT, '>&', $STDOUT_ORIG);
  23. }
  24. #Recurse through subdirs
  25. opendir my $dh, $dir or die "Cannot open directory";
  26. while (defined(my $subdir = readdir $dh)) {
  27. next unless -d "$dir/$subdir";
  28. next if (rindex $subdir, ".", 0) == 0;
  29. processallfiles("$dir/$subdir");
  30. }
  31. closedir $dh;
  32. }
  33. sub processfile {
  34. my $fh = shift;
  35. my $multiline = 0;
  36. my @params;
  37. my $indent;
  38. my $paramstr = "";
  39. foreach my $line (<$fh>) {
  40. chomp($line);
  41. if (!$multiline) {
  42. if ($line =~ /^(.+)_with_libctx\((.*[^\\])$/) {
  43. my $preline = $1;
  44. my $postline = $2;
  45. #Strip trailing whitespace
  46. $postline =~ s/\s+$//;
  47. print $preline.'_ex(';
  48. my @rets = extracttoclose($postline);
  49. if (@rets) {
  50. print "$postline\n";
  51. $multiline = 0;
  52. } else {
  53. $multiline = 1;
  54. $paramstr = $postline;
  55. $indent = (length $preline) + (length '_ex(');
  56. }
  57. } else {
  58. #Any other reference to _with_libctx we just replace
  59. $line =~ s/_with_libctx/_ex/g;
  60. print $line."\n";
  61. }
  62. } else {
  63. #Strip leading whitespace
  64. $line =~ s/^\s+//;
  65. #Strip trailing whitespace
  66. $line =~ s/\s+$//;
  67. my @rets = extracttoclose($paramstr.$line);
  68. if (@rets) {
  69. my $pre = shift @rets;
  70. my $post = shift @rets;
  71. @params = split(",", $pre);
  72. my @params = grep(s/^\s*|\s*$//g, @params);
  73. formatparams($indent, @params);
  74. print ')'.$post."\n";
  75. $multiline = 0;
  76. } else {
  77. $paramstr .= $line;
  78. }
  79. }
  80. }
  81. die "End of multiline not found" if $multiline;
  82. }
  83. sub formatparams {
  84. my $indent = shift;
  85. my @params = @_;
  86. if (@params) {
  87. my $param = shift @params;
  88. my $lensofar += $indent + (length $param) + 1;
  89. print "$param";
  90. print "," if @params;
  91. while (@params) {
  92. my $param = shift @params;
  93. if (($lensofar + (length $param) + 2) > 80) {
  94. print "\n".(" " x $indent);
  95. print $param;
  96. $lensofar = $indent + (length $param) + 1;
  97. } else {
  98. print ' '.$param;
  99. $lensofar += (length $param) + 2;
  100. }
  101. print "," if @params;
  102. }
  103. }
  104. }
  105. sub extracttoclose {
  106. my $inline = shift;
  107. my $outline = "";
  108. while ($inline =~ /^([^\)]*?)\((.*)$/) {
  109. my @rets = extracttoclose($2);
  110. if (!@rets) {
  111. return ();
  112. }
  113. my $inside = shift @rets;
  114. my $post = shift @rets;
  115. $outline .= $1.'('.$inside.')';
  116. $inline = $post;
  117. }
  118. if ($inline =~ /^(.*?)\)(.*)$/) {
  119. return ($outline.$1, $2);
  120. }
  121. return ();
  122. }