aboutsummaryrefslogtreecommitdiffstats
path: root/gprintify
blob: 925e4afde29f4bb5b3937ddf41796f7cc9ef6b96 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/perl
# $Id: gprintify 264358 2010-01-01 22:06:32Z guillomovitch $
# convert end of line patterns from DOS to UNIX

use strict;
use warnings;
use File::Find;
use File::Temp;
use File::Slurp;
use List::MoreUtils qw(none);

my $string_pattern = qr/
    ^
    ([^"]*)
    \$?
    "
    (
        (?:
            \\.    # an escaped character
            |      # or
            [^\\"] # anything but a backslash or a quote
        )*
    )
    "
    ([^>|\[\]]*|.*\|\|.*)
    $
    /x;
my $echo_pattern = qr/
    ^
    (.*)
    echo
    \s+
    (-[en]+)?
    /x;
my $initfunc_pattern = qr/
    (
        .*
        (?:action|success|failure|passed)
        \s*
        .*
    )
    /x;

my $var_pattern = qr/
    (?<!\\)                            # don't catch escaped variables
    (
        (?:
            \$[a-zA-Z0-9_]+            # simple variable name
            |
            \${                        # protected variable name
                [a-zA-Z0-9_]+
                (?:[#%]{1,2}[^}]+)?    # with optional expansion pattern
            }
        )
    )
    /x;

main(@ARGV) unless caller();

sub main {
    my $buildroot = $ENV{RPM_BUILD_ROOT};
    die "No build root defined" unless $buildroot;
    die "Invalid build root" unless -d $buildroot;
    # normalize build root
    $buildroot =~ s|/$||;

    my $exclude_pattern = $ENV{EXCLUDE_FROM_GPRINTIFICATION} ?
        qr/$ENV{EXCLUDE_FROM_GPRINTIFICATION}/ : undef;

    my $initrddir1 = "$buildroot/etc/rc.d/init.d";
    my $initrddir2 = "$buildroot/etc/init.d";

    foreach my $file (<$initrddir1/*>, <$initrddir2/*>) {
        # skip everything but files
        next unless -f $file;

        # skip excluded files
        next if $exclude_pattern && $file =~ $exclude_pattern;

        my @lines = read_file($file);

        # skip scripts not sourcing /etc/init.d/functions
        next if none { m{\s* \. \s+ /etc/(rc.d/)?init.d/functions}x } @lines;

        # convert lines
        @lines = map { process_line($_) } @lines;

        # rewrite script
        open(my $out, '>', $file) or die "Unable to open file $file: $!";
        print $out @lines;
        close($out);
    }
}

sub process_line {
    my ($line) = @_;

    if ($line =~ /$string_pattern/) {
        my $start  = $1;
        my $string = $2;
        my $end    = $3;

        my ($new_start, $string_end) = process_start($start);
        return $line if !$new_start;

        my ($new_string, $variables) = process_string($string);
        chomp $end;
        my $final = $new_start .  $new_string .  $string_end .  $variables;

        if ($end =~ /$string_pattern/) {
            my $start2  = $1;
            my $string2 = $2;
            my $end2    = $2;
            my ($new_start2, $string_end2) = process_start($start2);
            return $final . $end . "\n"
                if !$new_start2;

            my ($new_string2, $variables2) = process_string($string2);
            my $final2 = $new_start2 .  $new_string2 .  $string_end2 .  $variables2;
            return $final . $final2 . "\n";
        } else {
            return $final . $end . "\n";
        }

    } else {
        return $line;
    }
        
}

sub process_start {
    my ($start) = @_;

    if ($start =~ /$echo_pattern/) {
        my $before = $1;
        my $option = $2;
        my $new_start = $before . 'gprintf "';
        my $string_end = ($option && $option eq '-n') ? '"' : '\\n"';
        return ($new_start, $string_end);
    }

    if ($start =~ /$initfunc_pattern/) {
        return ($1 . '"', '"');
    }

    return ();
}

sub process_string {
    my ($string) = @_;

    my $variables = '';
    while ($string =~ m/$var_pattern/g) {
        $variables .= ' "' . $1 . '"';
    }
    $string =~ s/$var_pattern/\%s/g;

    return ($string, $variables);
}

1;