------java--------
#dd_traces.pl (C) Marko Kivij?rvi 2006
# Dummy checks
die "Specify an input file!/n" if $ARGV[0] eq "";
die "File not found!/n" unless -e $ARGV[0];
die "Incorrect file extension for a C/C++ file!\n"
if ( $ARGV[0] !~ /(.*)\.(java)$/ );
# Constants
my $IMPORT_LOG_PACKAG = "\nimport android.util.Log;";
my $TAG="";
my $LOG;
# Parse output filename from the input filename
my $file = $ARGV[0];
my $origFile = $1."-orig.".$2;
system( "cp $file $origFile" );
# Reset the input record separator (newline) so the entire file is read at once
undef $/;
# Read the input file
$_ = <>; # All there
s/
String
\s+
TAG
\s*
=
\s*
"
(\w+)
"
/
TagFind($&,$1) # Print the match and add the macro
/gxe; # g = repeat, x = split regex to multiple lines, e = evaluate substitution
s/
import
\s+
android\.util\.Log;
/
LogFind($&) # Print the match and add the macro
/gxe;
# Adds a tracer macro after each function definition
s/
package #package
\s+
[^{;\/(\s]*\.[^{;\/(\s]* # package name
;
/
AddLogImport($&) # Print the match and add the macro
/gxe; # g = repeat, x = split regex to multiple lines, e = evaluate substitution
s/
(public|private|protected)? #api property type
(\s+)?
(abstract)?
(\s+)?
class # Possible function return type
\s+ # One or more empty spaces
(\b\w+?) # class name
(\b|\<\w+|\<\w+\>)?
\s+
([^{;\/]*)? # extends or implements
\{ # Opening curly brace
/
AddTAG($&,$1,$2,$3,$4,$5,$6,$7) # Print the match and add the macro
/gxe; # g = repeat, x = split regex to multiple lines, e = evaluate substitution
s/
(public|private|protected) #api property type
\s+
(\b\w*?.?\w*?\b)? # Possible function return type
\s+ # One or more empty spaces
(\b\w+?\b) # Function name
\s*? # Possible empty space
\( # Opening brace
([^)]*?) # Possible function parameters
\) # Closing brace
[^={!();#"\*\/\\\r\n]*?
\r?
\n?
\{ # Opening curly brace
/
Parse($&,$1,$2,$3,$4) # Print the match and add the macro
/gxe; # g = repeat, x = split regex to multiple lines, e = evaluate substitution
open OUT, ">$file" or die "Cannot open file $file $!\n";
print OUT;
close OUT;
exit 0;
sub TagFind {
my $match = shift;
$TAG = shift;
return $match;
}
sub LogFind {
my $match = shift;
$LOG = 1;
return $match;
}
sub AddLogImport {
my $match = shift;
if ($LOG eq "1" ) {
return $match;
}
my $debug = $match."\n";
$debug .= $IMPORT_LOG_PACKAG;
return $debug;
}
sub AddTAG {
my $match = shift;
if ( $TAG eq "") {
my $s0 = shift;
my $property = shift;
my $abstract = shift;
my $s1 = shift;
# my $class = shift;
my $classnam = shift;
my $s2 = shift;
my $extends_implements = shift;
my $debug = $match;
$debug .= "\n private static final String TAG = \"$classnam\";" ;
return $debug;
}
return $match;
}
sub Parse {
my $match = shift;
my $property = shift;
my $ret = shift;
my $func = shift;
my $param = shift;
foreach ( $property, $ret, $func, $param ) {
s/^\s+|\s+$//g;
s/\n//g;
}
my $debug = $match."\n ";
$debug .= "Log.d(TAG,\"$func\");";
return $debug;
}