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