#################################################################################
use Data::Dumper;
use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END);
+use File::Copy;
use File::Find;
+no warnings 'File::Find';
+
+#$_ = "nb_api '/pub/mime/ls/type,ext.php_hash' | sed \"s/^/\$(printf '\\t\\t')/\"";
+#$_ = "nb_api /pub/mime/ls/type,ext.php_hash";
+#warn &cmd($_);
+@ARGV = grep {&file_readable($_)} @ARGV if @ARGV;
if (!@ARGV and !-t STDIN) {
- die "$NAME: option require file/path as arguments!\n" if $Opt{find};
+ die "$NAME: option `find` does not work with STDIN!\n" if $Opt{find};
@ARGV = "-";
}
my @original = <$hfile>;
seek($hfile,0,SEEK_SET);
- verbose(1,"Processing file $file");
+ verbose(1,"Processing $file");
my @lines = &shell_replace($hfile);
close $hfile;
if ( join('',@original) ne join('',@lines) ) {
- verbose(1," Change detected from $file");
- verbose(2,' lines '.@lines.'/'.@original);
+ verbose(1,"Change detected in $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});
+ verbose(1,"Copy $file to ".$file.'.'.$Opt{extension});
File::Copy::cp($file,$file.'.'.$Opt{extension}) or die $!;
}
- verbose(1,' Write '.$file);
+ 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");
+ verbose(1,"Ignore $file");
return 1
}
return 0;
}
+sub file_readable {
+ my $file = shift @_;
+ #-f $file || return;
+ -s $file || return;
+ -r $file || return;
+ if (-f $file and $Opt{extension}) {
+ my $exp = $Opt{extension};
+ if ($file =~ /\.$exp$/) {
+ verbose(1,"Ignore extension $file");
+ return;
+ }
+ }
+ return 1;
+}
sub wanted {
my $file = $File::Find::name;
+ &file_readable($file) or return;
+ #warn "$file ".(-r $file ? "YES" : "NO");
# Dont want the myself
return if myself($file);
my $hfile;
open($hfile,"<","$file");
+
while (<$hfile>) {
# Dont want the myself
next if /$SHELL_IGNORE/;
push @WANTED_FILES, $file;
last;
}
+
close $hfile;
}
+sub cmd {
+ my $cmd = join(' ',@_);
+ my $hcmd;
+#die $_;
+ $cmd =~ s/"/\\"/g;
+ open($hcmd,qq/bash -lc "$cmd" |/) or die $!;
+ my @cmd = <$hcmd>;
+ close $hcmd;
+ return wantarray ? @cmd : join('',@cmd);
+}
+
sub shell_replace {
#
# Search tags and execute command, return new lines
$i++;
if ($i==1) {
- $cmd = "bash -lc \"$2\"";
+ $cmd = $2;
push @lines, $line;
- push @lines, map{s/^/$blank/;$_} `$cmd`;
+ push @lines, map{s/^/$blank/;$_} &cmd($cmd);
} elsif ($line =~ /^\s*.{1,3}<SHELL_REPLACE/) {
$i = 0;
# Single line tags
#
$blank = $1;
- $cmd = "bash -lc \"$2\"";
+ $cmd = $2;
#$line = <$hfile>;
push @lines, $line;
- push @lines, map{ s/^/$blank/; $_ } `$cmd`;
+ push @lines, map{ s/^/$blank/; $_ } &cmd($cmd);
} else {
#
version() if $_[0]{'version'};
$main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'};
- $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'};
+ $main::VERBOSE = $VERBOSE = $main::DEBUG = $DEBUG = $_[0]{'debug'}
+ if defined $_[0]{'debug'};
}