roken-h-process.pl revision 1.1.1.1.26.1 1 1.1 elric #!/usr/bin/perl
2 1.1 elric
3 1.1.1.1.26.1 snj use Getopt::Std;
4 1.1 elric
5 1.1 elric my $debug = 0;
6 1.1 elric
7 1.1.1.1.26.1 snj getopts('dc:p:o:') || die "foo";
8 1.1 elric
9 1.1 elric if ($opt_d) {
10 1.1 elric $debug = 1;
11 1.1 elric }
12 1.1 elric
13 1.1 elric die "missing arg" if (!defined $opt_c || !defined $opt_p || !defined $opt_o);
14 1.1 elric
15 1.1 elric my %defines;
16 1.1 elric my $IN;
17 1.1 elric my $OUT;
18 1.1 elric
19 1.1 elric print "parse config.h\n" if ($debug);
20 1.1 elric
21 1.1 elric open IN, $opt_c || die "failed open ${opt_c}";
22 1.1 elric
23 1.1 elric my @nesting;
24 1.1 elric
25 1.1 elric push @nesting, 1;
26 1.1 elric
27 1.1 elric while (<IN>) {
28 1.1 elric if (m/\s*#ifdef\s+(.*)/) {
29 1.1 elric my $var = $1;
30 1.1 elric if (defined $defines{$var}) {
31 1.1 elric push @nesting, 1;
32 1.1 elric } else {
33 1.1 elric push @nesting, 0;
34 1.1 elric }
35 1.1 elric next;
36 1.1 elric } elsif (m/\s*#ifndef\s+(.*)/) {
37 1.1 elric my $var = $1;
38 1.1 elric if (defined $defines{$var}) {
39 1.1 elric push @nesting, 0;
40 1.1 elric } else {
41 1.1 elric push @nesting, 1;
42 1.1 elric }
43 1.1 elric next;
44 1.1 elric } elsif (m/\s*#else/) {
45 1.1 elric my $var = pop @nesting;
46 1.1 elric $var = !$var;
47 1.1 elric push @nesting, $var;
48 1.1 elric next;
49 1.1 elric } elsif ($nesting[$#nesting] and m/\s*#define\s+(\w+)\s+(\S+)/) {
50 1.1 elric my $res = $2;
51 1.1 elric $res = 1 if (!defined $res);
52 1.1 elric $defines{$1} = $res;
53 1.1 elric }
54 1.1 elric }
55 1.1 elric
56 1.1 elric close IN;
57 1.1 elric
58 1.1 elric if ($debug) {
59 1.1 elric foreach my $i (keys %defines) {
60 1.1 elric print "k: $i v: $defines{$i}\n";
61 1.1 elric }
62 1.1 elric }
63 1.1 elric
64 1.1 elric open IN, "$opt_p" || die "failed open ${opt_p}";
65 1.1 elric open OUT, ">$opt_o" || die "failed open ${opt_o}";
66 1.1 elric
67 1.1 elric print "parse roken.h.in\n" if ($debug);
68 1.1 elric
69 1.1 elric print OUT "/* This is an OS dependent, generated file */\n";
70 1.1 elric print OUT "\n";
71 1.1 elric print OUT "\n";
72 1.1 elric print OUT "#ifndef __ROKEN_H__\n";
73 1.1 elric print OUT "#define __ROKEN_H__\n";
74 1.1 elric print OUT "\n";
75 1.1 elric
76 1.1 elric @nesting = (1);
77 1.1 elric
78 1.1 elric while (<IN>) {
79 1.1 elric if (m/\s*#ifdef\s+(.*)/) {
80 1.1 elric my $var = $1;
81 1.1 elric if (defined $defines{$var}) {
82 1.1 elric push @nesting, 1;
83 1.1 elric } else {
84 1.1 elric push @nesting, 0;
85 1.1 elric }
86 1.1 elric next;
87 1.1 elric } elsif (m/\s*#ifndef\s+(.*)/) {
88 1.1 elric my $var = $1;
89 1.1 elric if (defined $defines{$var}) {
90 1.1 elric push @nesting, 0;
91 1.1 elric } else {
92 1.1 elric push @nesting, 1;
93 1.1 elric }
94 1.1 elric next;
95 1.1 elric } elsif (m/\s*#if\s+(.*)/) {
96 1.1 elric my $res = parse_if($1);
97 1.1 elric print "line = $res: $1\n" if ($debug);
98 1.1 elric push @nesting, $res;
99 1.1 elric next;
100 1.1 elric } elsif (m/\s*#elif\s+(.*)/) {
101 1.1 elric my $res = pop @nesting;
102 1.1 elric if ($res gt 0) {
103 1.1 elric $res = -1;
104 1.1 elric } else {
105 1.1 elric my $res = parse_if($1);
106 1.1 elric }
107 1.1 elric push @nesting, $res;
108 1.1 elric next;
109 1.1 elric } elsif (m/\s*#else/) {
110 1.1 elric my $var = pop @nesting;
111 1.1 elric $var = !$var;
112 1.1 elric push @nesting, $var;
113 1.1 elric next;
114 1.1 elric } elsif (m/\s*#endif/) {
115 1.1 elric pop @nesting;
116 1.1 elric next;
117 1.1 elric }
118 1.1 elric print "line: $_\n" if ($debug);
119 1.1 elric print "nesting dep $#{nesting}\n" if ($debug);
120 1.1 elric my $i = 0, $t = 1;
121 1.1 elric while ($i le $#nesting) {
122 1.1 elric $t = 0 if ($nesting[$i] le 0);
123 1.1 elric print "nesting $i val $nesting[$i] -> $t\n" if ($debug);
124 1.1 elric $i++;
125 1.1 elric }
126 1.1 elric if ($t) {
127 1.1 elric print OUT;
128 1.1 elric }
129 1.1 elric }
130 1.1 elric
131 1.1 elric print OUT "\n";
132 1.1 elric print OUT "#endif /* __ROKEN_H__ */\n";
133 1.1 elric
134 1.1 elric
135 1.1 elric close IN;
136 1.1 elric
137 1.1 elric exit 0;
138 1.1 elric
139 1.1 elric sub parse_if
140 1.1 elric {
141 1.1 elric my ($neg, $var);
142 1.1 elric
143 1.1 elric $_ = shift;
144 1.1 elric
145 1.1 elric if (m/^\s*$/) {
146 1.1 elric print "end $_\n" if ($debug);
147 1.1 elric return 1;
148 1.1.1.1.26.1 snj } elsif (m/^\(([^&]+)\&\&(.*)\)\s*\|\|\s*\(([^&]+)\&\&(.*)\)$/) {
149 1.1.1.1.26.1 snj print "($1 and $2) or ($3 and $4)\n" if ($debug);
150 1.1.1.1.26.1 snj return ((parse_if($1) and parse_if($2)) or (parse_if($3) and parse_if($4)));
151 1.1 elric } elsif (m/^([^&]+)\&\&(.*)$/) {
152 1.1 elric print "$1 and $2\n" if ($debug);
153 1.1 elric return parse_if($1) and parse_if($2);
154 1.1 elric } elsif (m/^([^\|]+)\|\|(.*)$/) {
155 1.1 elric print "$1 or $2\n" if ($debug);
156 1.1.1.1.26.1 snj return (parse_if($1) or parse_if($2));
157 1.1 elric } elsif (m/^\s*(\!)?\s*defined\((\w+)\)/) {
158 1.1 elric ($neg, $var) = ($1, $2);
159 1.1 elric print "def: ${neg}-defined(${var})\n" if ($debug);
160 1.1 elric my $res = defined $defines{$var};
161 1.1 elric if ($neg eq "!") {
162 1.1 elric if ($res) {
163 1.1 elric $res = 0;
164 1.1 elric } else {
165 1.1 elric $res = 1;
166 1.1 elric }
167 1.1 elric }
168 1.1 elric print "res: $res\n" if ($debug);
169 1.1 elric return $res;
170 1.1 elric } elsif (m/^\s*(\!)?(\w+)/) {
171 1.1 elric ($neg, $var) = ($1, $2);
172 1.1 elric print "var: $neg $var\n" if ($debug);
173 1.1 elric my $res;
174 1.1 elric if (defined $defines{$var}) {
175 1.1 elric $res = $defines{$var};
176 1.1 elric } else {
177 1.1 elric $res = 0;
178 1.1 elric }
179 1.1 elric $res = ! $res if ($neg =~ m/!/);
180 1.1 elric print "res: $res\n" if ($debug);
181 1.1 elric return $res;
182 1.1 elric }
183 1.1 elric die "failed parse: $_\n";
184 1.1 elric }
185