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