]> git.nbdom.net Git - nb.git/commitdiff
bin/shell-replace
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 10 Dec 2022 21:58:04 +0000 (22:58 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 10 Dec 2022 21:58:04 +0000 (22:58 +0100)
bin/shell-replace

index ea89b09dc334729b158cf77dc60522067a02768e..28c2db0e6f14727915802804b86886d3da450cda 100755 (executable)
@@ -41,9 +41,16 @@ $main::_DATA_ = undef;
 #################################################################################
 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 = "-";
 }
 
@@ -86,24 +93,24 @@ for $file (@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;
@@ -112,7 +119,6 @@ for $file (@ARGV) {
        }
        print map { $_ } @lines if $is_stdin;
 }
-use File::Copy;
 
 #################################################################################
 #
@@ -136,20 +142,37 @@ sub verbose {
 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/;
@@ -160,9 +183,21 @@ sub wanted {
                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
@@ -188,9 +223,9 @@ sub shell_replace {
                        $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;
@@ -206,10 +241,10 @@ sub shell_replace {
                        # 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 {
                        #
@@ -292,7 +327,8 @@ sub get_options {
        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'};
 
 }