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