no_chdir => 1
}, @ARGV);
#exit;
- #die Dumper(\@WANTED_FILES);
@ARGV = @WANTED_FILES;
}
#
-# Process
+# Process each files
#
my $hfile;
my $file;
for $file (@ARGV) {
+ my $is_stdin = 0;
+
if ($file eq "-") {
#$hfile = \*ARGV;
$hfile = \*STDIN;
+ $is_stdin = 1;
+
} else {
next if myself($file);
open($hfile,"<",$file);
}
+
my @original = <$hfile>;
seek($hfile,0,SEEK_SET);
- verbose(1,"Processing: $file\n");
+ verbose(1,"Processing file $file");
my @lines = &shell_replace($hfile);
- print map { $_ } @lines;
close $hfile;
- verbose(1,"Changed: $file\n") if join('',@original) ne join('',@lines);
+
+ if ( join('',@original) ne join('',@lines) ) {
+ verbose(1," Change detected from $file");
+ verbose(2,' lines '.@lines.'/'.@original);
+ #&shell_replace_write($file,@lines) if !$is_stdin and !$DEBUG;
+
+ # Write changes
+ if (!$is_stdin and !$DEBUG) {
+ if ($Opt{extension}) {
+ verbose(1," Copy $file to ".$file.'.'.$Opt{extension});
+ File::Copy::cp($file,$file.'.'.$Opt{extension}) or die $!;
+ }
+ verbose(1,' Write '.$file);
+ open $hfile,">",$file or die $!;
+ print $hfile @lines;
+ close $hfile;
+ }
+
+ }
+ print map { $_ } @lines if $is_stdin;
}
+use File::Copy;
#################################################################################
#
sub myself {
my $file = shift @_;
if ($file eq $0 or $file =~ /(^|\/)$NAME$/) {
- verbose(1,"ignore file $file\n");
+ verbose(1,"ignore file $file");
return 1
}
return 0;
next unless /^.{1,3}<SHELL_REPLACE/;
- verbose(1,"Found: $file\n");
+ verbose(1,"Found: $file");
push @WANTED_FILES, $file;
last;
}
}
sub shell_replace {
+#
# Search tags and execute command, return new lines
-my $ident;
-my $i;
-my $change;
-my $cmd;
-my @lines = ();
+#
+ my $blank;
+ my $i;
+ my $cmd;
+ my @lines = ();
-my $hfile = shift @_;
+ my $hfile = shift @_;
while (my $line = <$hfile>) {
if (
..
$line =~ /^\s*.{1,3}<SHELL_REPLACE.*/
) {
+ #
+ # Start / End tags
+ #
+
+ $blank = $1 ? $1 : "";
+ $i++;
+
+ if ($i==1) {
+ $cmd = "bash -lc \"$2\"";
+ push @lines, $line;
+ push @lines, map{s/^/$blank/;$_} `$cmd`;
+
+ } elsif ($line =~ /^\s*.{1,3}<SHELL_REPLACE/) {
+ $i = 0;
+ push @lines, $line;
+
+ } else {
+ # In between is ignored
+ }
- $change = 1;
- $ident = $1 ? $1 : "";
- #die $line unless $2;
- #warn ">>".$line; next;
- $i++;
- if ($2) {
- $cmd = "bash -lc \"$2\"";
- if ($i==1) { push @lines, $line; push @lines, map{s/^/$ident/;$_} `$cmd`; }
- elsif (/^\s*.{1,3}<SHELL_REPLACE/) { push @lines, $line; }
- }
} elsif ($line =~ /^(\s*).{1,3}=SHELL_REPLACE (.*)$/) {
- $ident = $1;
- <>;
+ #
+ # Single line tags
+ #
+ $blank = $1;
+ $cmd = "bash -lc \"$2\"";
+ #$line = <$hfile>;
push @lines, $line;
- push @lines, map{ s/^/$ident/; $_ } `$cmd`;
- $change = 1;
+ push @lines, map{ s/^/$blank/; $_ } `$cmd`;
} else {
+ #
+ # No matched tags line
+ #
$i=0;
push @lines, $line;
=head1 OPTIONS
-option[find|f!] Search first files containing tags from args
+ -option[extension|e=s] Copy original to a new file with this value as an extension
+
-option[verbose|v+] Verbose mode: increase the verbosity level.
-option[debug+] Debug mode: increase the verbosity level.
-option[version|V] Print version (default: $VERSION)