#!/usr/bin/perl my $dir; my $debug = 0; my $strip = 0; while (@ARGV) { if ($ARGV[0] eq "--debug") { $debug++; shift @ARGV; } elsif ($ARGV[0] eq "--strip") { $strip = 1; shift @ARGV; } elsif (-d $ARGV[0] && !defined($dir)) { $dir = shift @ARGV; } else { print "Cannot understand: $ARGV[0]\n"; exit; } } $dir = "." unless defined($dir); my %TAG = ("logprintf" => 2, "logputs" => 2, "debug_logprintf" => 1, "DEBUGP" => 1); my %IGNORE = ("log.c" => 1, "progress.c" => 1); opendir DIR, $dir or die "Cannot read directory $dir: $!\n"; foreach $file (readdir DIR) { next if $file eq "." || $file eq ".."; next if defined($IGNORE{$file}); next unless $file =~ m/\.c$/; next if -d "$dir/$file"; open F, "$dir/$file" or die "Cannot read $dir/$file: $!\n"; my $content = join("", ); close F; my $fn = $file; $fn =~ s/\.c$//; # Determine the numbering of the lines my $p = 0; my $n = 1; my @c, @line; push @line, 0; while (($p = index($content,"\n",$p))>= 0) { push @line, $p + 1; while (scalar(@c) < $p) { push @c, $n; } $n++; $p++; } # Find all comments $p = 0; my %COMMENT = (); while (($p = index($content,"/*",$p))>= 0) { my $pp = index($content,"*/",$p); # if ($pp >= 0) # { # my $comment = substr($content,$p,$pp-$p+2); # if ($comment =~ m/{{#\d+}}/) # { # my $found = 0; # map { $found = 1 if index($comment,$_) >= 0 } keys %TAG; # # print '-'x20," $fn ",'-'x20,"\n$comment\n" if $found; # $COMMENT{$p} = $pp if $found; # } # } $COMMENT{$p} = $pp if $pp >= 0; $p = $pp; } # Find and processes instances of the tagged strings that are not within comments $p = 0; my $c = 0; my $changed = 0; my $newContent = ""; while ($p <= length($content)) { print "*** [$fn:$p]" if $debug > 1; my $pp = length($content); my $tag = ""; foreach my $key (keys %TAG) { my $qq = $p; while (($qq = index($content,$key,$qq)) >= 0) { last if $qq > $pp; print " {$key:$qq,$pp}" if $debug > 1; if ($qq < $pp && substr($content,$qq) =~ m/^$key\s*\(/) { my $inComment = 0; map { $inComment = 1 if $_ < $qq && $COMMENT{$_} > $qq } keys %COMMENT; # map { print '-'x20," $fn ",'-'x20,"\n",substr($content,$_,$COMMENT{$_}-$_+2),"\n" # if $_ < $qq && $COMMENT{$_} > $qq } keys %COMMENT; if ($inComment) { print "*" if $debug > 1; $qq += length($key); next; } $pp = $qq; $tag = $key; last; } else { print "#" if $debug > 1; if ($debug > 2) { my $x = rindex($content,"{{#",$qq); my $y = index($content,"\n",$x); print "\n",substr($content,$x,$qq-$x),"-->",substr($content,$qq,$y-$qq); } $qq += length($key); } } } print "\n" if $debug > 1; $p = $pp; last if $p >= length($content); $newContent .= substr($content,$c,$p-$c); $c = $p; my $depth = 0; my $inQuote = 0; my $haveArg = 0; my $pp = $p; my @args; while ((my $c = substr($content,$pp,1)) ne ";" || $depth > 0 || $inQuote) { if ($c eq "\\") { $pp++; # Skip the next character } elsif ($c eq "\"") { $inQuote = !$inQuote; } elsif ($c eq "(" && !$inQuote) { $depth++; if ($depth == 1) { my ($skip) = substr($content,$pp) =~ m/\((\s*)/; push @args, ($pp + 1 + length($skip)); } } elsif ($c eq ")" && !$inQuote) { $depth--; } elsif ($c eq "," && !$inQuote && $depth == 1) { my ($skip) = substr($content,$pp) =~ m/,(\s*)/; push @args, ($pp + 1 + length($skip)); } $pp++; } print "+++ [$fn:$p:$pp]\n<<< ",substr($content,$p,$pp-$p+1),"\n" if $debug; my $nArg = $TAG{$tag}-1; my $revised = substr($content,$p,$pp-$p+1); if (defined($args[$nArg])) { my $n = $args[$nArg] - $p; my $nn = $args[$nArg+1] || $pp; print "%%% ",substr($revised,$n,$nn-$n+1),"\n" if $debug > 1; $n = index($revised,"\"", $n); if ($n >= 0 && substr($revised,$n,4) ne "\"\\n\"") { $n++; my ($tagged) = substr($revised,$n) =~ m/(\[[^\:]*\:\d+\] )/; substr($revised,$n,length($tagged)) = "" if defined($tagged); if (!$strip) { substr($revised,$n,0) = "\[$fn:$c[$p]\] "; $changed = 1; } $changed = 1 if substr($content,$p,$pp-$p+1) ne $revised; } else { print " ^ just a newline\n" if $debug; } } else { print " ^ no argument at position $nArg\n" if $debug; } print ">>> $revised\n" if $debug > 1; $newContent .= $revised; $p = $pp + 1; $c = $p; } $newContent .= substr($content,$c) if $c <= length($content); if ($changed && $debug < 2) { rename "$fn\.c", "orig/$fn\.c" unless -f "orig/$fn\.c"; print "$fn\.c\n"; open F, ">$dir/$fn\.c" or die "Cannot write $fn\.new: $!\n"; print F $newContent; close F; } }