~n3npq/lsb/scripts

« back to all changes in this revision

Viewing changes to trim_macros.pl

  • Committer: FSG Patch Queue Manager
  • Date: 2006-12-01 20:30:42 UTC
  • mfrom: (159.1.1 scripts)
  • Revision ID: pqm@freestandards.org-20061201203042-aefc90ae5d3d7da1
Merge xrender changes from Tracy Camp

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
#
 
4
# script takes the output of cpp -dD and a header name and
 
5
# trims the cpp output to only include output that came from
 
6
# the given header.
 
7
#
 
8
# This program treats the output of cpp as a state machine
 
9
# inputs and only outputs lines that exist while the state
 
10
# machine is in the state that represents the given header.
 
11
#
 
12
 
 
13
my $macros_only = 0;
 
14
my $compress_functions = 0;
 
15
my $header = '';
 
16
 
 
17
for (my $i = 0; $i <= $#ARGV; $i++) {
 
18
        if ($ARGV[$i] eq '-m') {
 
19
                $macros_only = 1;
 
20
        } if ($ARGV[$i] eq '-f') {
 
21
                $compress_functions = 1;
 
22
        } else {
 
23
                $header = $ARGV[$i];
 
24
        }
 
25
}
 
26
 
 
27
if ($header eq '') {
 
28
        printf "Need the name of a header!\n";
 
29
        die;
 
30
}
 
31
 
 
32
my $state_on = 0;
 
33
 
 
34
while(<STDIN>) {
 
35
        my $line = $_;
 
36
        #
 
37
        # check for lines beginning with '# <number> \"'
 
38
        #
 
39
        if ($line =~ m/^# \d+ \"*/) {
 
40
                #
 
41
                # If the line matches this pattern CPP switched
 
42
                # state to the given header from another or this
 
43
                # header, and we will print output until state 
 
44
                # changes again.
 
45
                #
 
46
                # if the line does not match this pattern CPP
 
47
                # switched state away from this header or state
 
48
                # remains away from this header.
 
49
                #
 
50
                if ($line =~ m/^# \d+ \"$header\"*/) {
 
51
#printf "state $state_on to 1 because of $line";
 
52
                        $state_on = 1;
 
53
                } else {
 
54
#printf "state $state_on to 0 because of $line";
 
55
                        $state_on = 0;
 
56
                        $macro_on = 0;
 
57
                }
 
58
        } else {
 
59
                if ($state_on) {
 
60
                        if ($macros_only) {
 
61
                                #
 
62
                                # cpp already compresses multi-line macros down to a single 
 
63
                                # line for us so we don't have to handle multi-line macros here.
 
64
                                #
 
65
                                if ($line =~ m/^\s*#/) {
 
66
                                        print $line;
 
67
                                }
 
68
                        } else {
 
69
                                #
 
70
                                # detect and print multi-line functions as a single line
 
71
                                # so that we can do simple grep filtering later.
 
72
                                #
 
73
                                if ($compress_functions) {
 
74
                                        #
 
75
                                        # ignore macros here
 
76
                                        #
 
77
                                        if (! ($line =~ m/^\s*#/)) {
 
78
                                                #
 
79
                                                # ensure line wrapping won't occur.
 
80
                                                #
 
81
#print "function line $line";
 
82
                                                chop($line);
 
83
                                        line_again:
 
84
                                                if ($line =~ m/^.*;.*$/) {
 
85
                                                        my $last_terminated = ($line =~ m/^.*;$/) ? 1 : 0;
 
86
                                                        @line = split(";", $line);
 
87
                                                        for (my $i = 0; $i <= $#line; $i++) {
 
88
#print "\nindex $i terminated? $last_terminated token $line[$i]\n";
 
89
                                                                if ($i == $#line) {
 
90
                                                                        if ($last_terminated) {
 
91
                                                                                print $line[$i] . ";\n";
 
92
                                                                        } else {
 
93
                                                                                $line = $line[$i];
 
94
                                                                                goto line_again;
 
95
                                                                        }
 
96
                                                                } else {
 
97
                                                                        print $line[$i] . ";\n";
 
98
                                                                }
 
99
                                                        }
 
100
                                                } else {
 
101
                                                        $line =~ s/^\s+//;
 
102
                                                        print $line;
 
103
                                                }
 
104
                                        } else {
 
105
                                                print $line;
 
106
                                        }
 
107
                                } else {
 
108
                                        print $line;
 
109
                                }
 
110
                        }
 
111
                }
 
112
        }
 
113
}
 
114