X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=blobdiff_plain;f=bin%2Fexx;h=97f021b2200a28213c514f973d1866b64d0ec9a5;hp=df78524508419e6376969cc60433164f3a1c9501;hb=c36d3924d6aa4ea711f344e215e2f5b8525fd0a0;hpb=43d855aea466f7f8e1e7a3e31440abf8b3bd6404 diff --git a/bin/exx b/bin/exx index df78524..97f021b 100755 --- a/bin/exx +++ b/bin/exx @@ -6,38 +6,74 @@ use strict; use warnings; use IO::Handle; -use Cwd qw(chdir fastgetcwd); +use Cwd qw(chdir cwd); +use constant FORMAT_TAR=>'tar xf $pathname;i=`echo *`;echo "$i"|grep -q " " || test * != $base ||' + .'(mv "$i" "$i".$$;(set +x;mv "$i".$$/* .);rmdir "$i".$$)'; use constant FORMATS=>{ - "rpm"=>'rpm2cpio !|cpio -id --quiet', #-v #FIXME: --sparse doesn't work, why? - "zip"=>'unzip -Lq !', - "a" =>'ar x !', - "deb"=>'ar x !;for i in *.tar.gz;do j=`basename $i .tar.gz`;mkdir -p $j;cd $j;tar xzf ../$i;cd ..;rm -f $i;done', + "rpm" =>'rpm2cpio $pathname|cpio -id --quiet', #-v #FIXME: --sparse doesn't work, why? + "zip" =>'unzip -Lq $pathname', + "jar" =>'unzip -Lq $pathname', + "a" =>'ar x $pathname', + "deb" =>'ar x $pathname;' + .'for i in *.tar.gz;do j=`basename $i .tar.gz`;mkdir -p $j;cd $j;tar xzf ../$i;cd ..;rm -f $i;done', + "arj" =>'unarj x $pathname', + "tar" =>FORMAT_TAR, + "tar.gz" =>do { $_=FORMAT_TAR; s/xf/xzf/; $_; }, + "tgz" =>"tar.gz", + "tar.bz2"=>do { $_=FORMAT_TAR; s/xf/xjf/; $_; }, + "tar.bz" =>"tar.bz2", + "tbz" =>"tar.bz", + "tar.xz" =>do { $_=FORMAT_TAR; s/xf/xJf/; $_; }, + "txz" =>do { $_=FORMAT_TAR; s/xf/xJf/; $_; }, + "msi" =>'cabextract -q $pathname', }; die "Syntax: $0 )>..." if !@ARGV; -my $origdir=fastgetcwd; +my @extdirs; +my $origdir=cwd; for my $fname (@ARGV) { - $fname=~m#([^/]+)([.=])(\L[^./]+\E)$# or die "Extension not found for archive: $fname"; - my($path,$base,$ext)=($`,$1,$3); - my($pathname)=($2 eq "=" ? "$path$base" : $fname); - my $cmd=FORMATS->{$ext} or die "Extension \"$ext\" not known for archive: $fname"; + my @parsed; + for my $fmt (sort { length $b<=>length $a; } keys %{+FORMATS}) { + last if @parsed=$fname=~m#^(.*?)([^/]+)([.=])(\Q$fmt\E)$#i; + } + $parsed[3] or die "Extension not found for archive: $fname"; + my($path,$base,$ext)=@parsed[0,1,3]; + my($pathname)=($parsed[2] eq "=" ? "$path$base" : $fname); + my($cmdtry,$cmd)=(lc $ext); + do { + $cmd=$cmdtry; + $cmdtry=FORMATS->{$cmdtry}; + } while ($cmdtry); -r $pathname && !-d $pathname or die "Archive not readable: $pathname"; my($extdir)=(-e $base && !-d $base ? "$base.dir" : $base); - -d $extdir or mkdir $extdir or die "Unable to create directory \"$extdir\": $!"; + # Extraction-overwriting cowardly not supported: + # -d $extdir or ... + mkdir $extdir or die "Unable to create directory \"$extdir\": $!"; chdir $extdir or die "Unable to chdir to \"$extdir\": $!"; $pathname="../$pathname" if $pathname!~m#^/#; - $pathname=~s/'/'\\''/g; - $pathname="'$pathname'"; - $cmd=~s/!/$pathname/g; + $path ="../$path" if $path !~m#^/#; $cmd="set -ex;$cmd"; + my %substvars=( + "pathname"=>\$pathname, + "path" =>\$path, + "base" =>\$base, + "ext" =>\$ext, + "extdir" =>\$extdir, + ); + while (my($name,$var)=each %substvars) { + ($_=$$var)=~s/'/'\\''/g; + $cmd="$name='$_';$cmd"; + } print "\t$extdir/:\n"; STDOUT->flush(); my $rc; $rc=system $cmd and die "$cmd (rc=".($rc>>8)."): $!"; + push @extdirs,$extdir; } continue { chdir $origdir; } +print "extdir=".$extdirs[0]."\n" if 1==@extdirs; exit 0;