From b672f8f719914fee87324d2cb1e417a471ca42c2 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Sat, 10 Dec 2022 22:58:04 +0100 Subject: [PATCH] bin/shell-replace --- bin/shell-replace | 62 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/bin/shell-replace b/bin/shell-replace index ea89b09d..28c2db0e 100755 --- a/bin/shell-replace +++ b/bin/shell-replace @@ -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}; 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'}; } -- 2.47.3