|
|
@ -52,89 +52,122 @@ for (@ARGV) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (@files) { |
|
|
|
for (@files) { |
|
|
|
my $file = $_; |
|
|
|
# Number of nested #if directives. Initially 0, will be increased |
|
|
|
my $branch = "none"; |
|
|
|
# on every #if directive and decreased on every #endif directive. |
|
|
|
my $level = 0; |
|
|
|
my $level = 0; |
|
|
|
rename "$file", "$file.orig" or do { |
|
|
|
|
|
|
|
|
|
|
|
# The number of the outermost level, whose #if directive was |
|
|
|
|
|
|
|
# false. This is 0, if there wasn't an false #if directive, yet. |
|
|
|
|
|
|
|
# As long as it is != 0, we comment every line, and ignore |
|
|
|
|
|
|
|
# directives except for increasing/decreasing $level. |
|
|
|
|
|
|
|
my $falselevel = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Tells if an error occured and the transformation shouldn't |
|
|
|
|
|
|
|
# be done. |
|
|
|
|
|
|
|
my $error = 0; |
|
|
|
|
|
|
|
my $changes = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $file = $_; |
|
|
|
|
|
|
|
open OLD, "<$file" or do { |
|
|
|
print STDERR "Can't open file $file\n"; |
|
|
|
print STDERR "Can't open file $file\n"; |
|
|
|
next; |
|
|
|
next; |
|
|
|
}; |
|
|
|
}; |
|
|
|
open OLD, "<$file.orig"; |
|
|
|
open NEW, ">$file.tmp" or do { |
|
|
|
open NEW, ">$file"; |
|
|
|
print STDERR "Can't open tmp file $file.tmp\n"; |
|
|
|
|
|
|
|
next; |
|
|
|
|
|
|
|
}; |
|
|
|
my $linenr = 0; |
|
|
|
my $linenr = 0; |
|
|
|
|
|
|
|
LINE: |
|
|
|
while (<OLD>) { |
|
|
|
while (<OLD>) { |
|
|
|
$linenr++; |
|
|
|
$linenr++; |
|
|
|
if (/^\/\/\/\#/) { |
|
|
|
if (m'^///#') { |
|
|
|
# This is a directive. First we print it out. |
|
|
|
# This is a directive. First we print it out. |
|
|
|
print NEW $_; |
|
|
|
print NEW $_; |
|
|
|
if (/^\/\/\/\#ifdef (\S*)$/) { |
|
|
|
if (m'^///# *if') { |
|
|
|
my $label=$1; |
|
|
|
|
|
|
|
# An ifdef directive, look if -D is defined, but only |
|
|
|
|
|
|
|
# if we are not in a false branch. In a false branch |
|
|
|
|
|
|
|
# we simply increase level. |
|
|
|
|
|
|
|
if ($branch eq "false") { |
|
|
|
|
|
|
|
$level++; |
|
|
|
$level++; |
|
|
|
} else { |
|
|
|
if (m'^///# *ifdef (\S*)$') { |
|
|
|
if (defined $defs{$label}) { |
|
|
|
# If there was an outer false #if directive, we ignore the |
|
|
|
$branch = "true"; |
|
|
|
# condition. |
|
|
|
} else { |
|
|
|
next LINE if ($falselevel); |
|
|
|
$branch = "false"; |
|
|
|
|
|
|
|
$level = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} elsif (/^\/\/\/\#ifndef (\S*)$/) { |
|
|
|
|
|
|
|
my $label=$1; |
|
|
|
my $label=$1; |
|
|
|
# An ifndef directive, look if -D is defined, but only |
|
|
|
|
|
|
|
# if we are not in a false branch. In a false branch |
|
|
|
# An ifdef directive, look if -D is defined. |
|
|
|
# we simply increase level. |
|
|
|
$falselevel = $level |
|
|
|
if ($branch eq "false") { |
|
|
|
unless (defined $defs{$label}); |
|
|
|
$level++; |
|
|
|
} elsif (m'^///# *ifndef (\S*)$') { |
|
|
|
} else { |
|
|
|
# If there was an outer false #if directive, we ignore the |
|
|
|
if (defined $defs{$label}) { |
|
|
|
# condition. |
|
|
|
$branch = "false"; |
|
|
|
next LINE if ($falselevel); |
|
|
|
$level = 0; |
|
|
|
|
|
|
|
} else { |
|
|
|
my $label=$1; |
|
|
|
$branch = "true"; |
|
|
|
# An ifndef directive, look if -D is defined |
|
|
|
} |
|
|
|
$falselevel = $level |
|
|
|
|
|
|
|
if (defined $defs{$label}); |
|
|
|
} |
|
|
|
} |
|
|
|
} elsif (/^\/\/\/\#else/) { |
|
|
|
} elsif (m'^///# *else') { |
|
|
|
# An else directive. We switch from true to false and |
|
|
|
# An else directive. We switch from true to false and |
|
|
|
# if level is zero we switch from false to true |
|
|
|
# if level is falselevel we switch from false to true |
|
|
|
if ($branch eq "true") { |
|
|
|
if ($level == 0) { |
|
|
|
$branch = "false"; |
|
|
|
|
|
|
|
$level = 0; |
|
|
|
|
|
|
|
} elsif ($branch eq "false") { |
|
|
|
|
|
|
|
$branch = "true" if ($level == 0); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
# An else outside of any directives; warn. |
|
|
|
# An else outside of any directives; warn. |
|
|
|
print STDERR "$file: $linenr: ignoring unmatched $_"; |
|
|
|
print STDERR "$file: $linenr: unmatched $_"; |
|
|
|
|
|
|
|
$error = 1; |
|
|
|
|
|
|
|
} elsif ($falselevel == $level) { |
|
|
|
|
|
|
|
$falselevel = 0; |
|
|
|
|
|
|
|
} elsif ($falselevel == 0) { |
|
|
|
|
|
|
|
$falselevel = $level; |
|
|
|
} |
|
|
|
} |
|
|
|
} elsif (/^\/\/\/\#endif/) { |
|
|
|
} elsif (m'^///# *endif') { |
|
|
|
if ($branch eq "none") { |
|
|
|
# set $falselevel to 0, if the false branch is over now. |
|
|
|
# An endif outside of any directives; warn. |
|
|
|
$falselevel = 0 if ($falselevel == $level); |
|
|
|
print STDERR "$file: $linenr: ignoring unmatched $_"; |
|
|
|
# decrease level. |
|
|
|
} elsif ($branch eq "false") { |
|
|
|
if ($level == 0) { |
|
|
|
$branch = "true" if ($level-- == 0); |
|
|
|
print STDERR "$file: $linenr: unmatched $_"; |
|
|
|
|
|
|
|
$error = 1; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$level--; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
print STDERR "$file: $linenr: ignoring unknown directive $_"; |
|
|
|
print STDERR "$file: $linenr: ignoring unknown directive $_"; |
|
|
|
|
|
|
|
$error = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} elsif (/^\/\/\/(.*)/) { |
|
|
|
} elsif (m'^///(.*)') { |
|
|
|
$line = $1; |
|
|
|
$line = $1; |
|
|
|
if ($branch eq "true") { |
|
|
|
if ($falselevel == 0 && $level > 0) { |
|
|
|
# remove comments in true branch; |
|
|
|
# remove comments in true branch, but not in outermost level. |
|
|
|
print NEW "$line\n"; |
|
|
|
print NEW "$line\n"; |
|
|
|
|
|
|
|
$changes = 1; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
# print out the full line with comments: |
|
|
|
# print out the full line with comments: |
|
|
|
print NEW $_; |
|
|
|
print NEW $_; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if ($branch eq "false") { |
|
|
|
if ($falselevel != 0) { |
|
|
|
# add comments in false branch |
|
|
|
# add comments in false branch |
|
|
|
print NEW "///$_"; |
|
|
|
print NEW "///$_"; |
|
|
|
|
|
|
|
$changes = 1; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
print NEW $_; |
|
|
|
print NEW $_; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($level != 0 || $falselevel != 0) { |
|
|
|
|
|
|
|
# something got wrong |
|
|
|
|
|
|
|
print STDERR "$file: unmatched directives: level $level, ". |
|
|
|
|
|
|
|
"falselevel $falselevel\n"; |
|
|
|
|
|
|
|
$error = 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($error == 0) { |
|
|
|
|
|
|
|
if ($changes == 0) { |
|
|
|
|
|
|
|
unlink "$file.tmp"; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(rename "$file", "$file.orig" |
|
|
|
|
|
|
|
and rename "$file.tmp", "$file") |
|
|
|
|
|
|
|
or print STDERR "$file: Couldn't rename files.\n"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
print STDERR "$file: errors occured, file not transformed.\n"; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|