-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_kernel_version.pl
executable file
·67 lines (55 loc) · 1.33 KB
/
filter_kernel_version.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
#!/usr/bin/perl -w
use strict;
my $version = 0;
sub version_code($$$)
{
my $result = shift;
$result = $result * 256 + shift;
$result = $result * 256 + shift;
return $result;
}
#######################################
sub reject_section()
{
my $level = 0;
while (<STDIN>) {
return $_ if ($level == 0 && /^#(?:endif|else)/);
$level++ if (/^#if/);
$level-- if (/^#endif/);
}
}
#######################################
sub accept_section()
{
my $level = 0;
my $line;
while (<STDIN>) {
if (/^#if\s/ && /\bLINUX_VERSION_CODE\b/ && /\bKERNEL_VERSION\b/) {
chomp;
s/^#if\s+//;
s/\bLINUX_VERSION_CODE\b/$version/;
s/\bKERNEL_VERSION\b/&version_code/;
if (eval($_)) {
$line = &accept_section();
&reject_section() if ($line =~ /#else/);
}
else {
$line = &reject_section();
&accept_section() if ($line =~ /#else/);
}
next;
}
return $_ if ($level == 0 && /^#(?:endif|else)/);
$level++ if (/^#if/);
$level-- if (/^#endif/);
print $_;
}
}
#######################################
if ((scalar(@ARGV) < 1) || ($ARGV[0] eq "-h")) {
print STDERR "Usage: filter_kernel_version.pl <version> <prog.c >prog-flt.c\n";
exit 1;
}
my @kvers = split(/\./, $ARGV[0]);
$version = &version_code(@kvers);
accept_section();