#!/bin/sh -- # A comment mentioning perl, to prevent perl from looping. Indented to work with bash. eval 'exec perl -S $0 ${1+"$@"}' if 0; # AnyView, av, view or print anything (with enuf effort) $Version = "v0.4 Copyright (c) 1995, 1996 Karl J. Runge"; $debug = 'True'; $Metamail = 'metamail'; # Command for "metamail" usage $Multimime = 'multi_mime'; # Homebrew "metamail" $MimeTypes = "$ENV{'HOME'}/.mime.types"; # User's Mime types $Mailcap = "$ENV{'HOME'}/.mailcap"; # User's Mailcap $Wait = ''; # wait for viewer to exit $Loop = ''; $Encoding_Method = 'uuencode'; # or base64 $Use_Tmp = ''; if ($ENV{LD_LIBRARY_PATH} eq $ENV{LD_755}) { $ENV{LD_LIBRARY_PATH} = $ENV{SETPATH_LDPATH}; } if ( $ENV{NEW_PGRP} ne '' ) { setpgrp(0, 0); } # Try to find non-setuid xterm: system("type xterm_755 >/dev/null 2>&1"); if ( $? == 0 ) { $XTERM = 'xterm_755'; } else { $XTERM = 'xterm'; } if ($ARGV[0] eq '-xprompt' || $ARGV[0] eq '-xp') { shift; if (fork) { sleep 1; exit; } else { my $btn3 = 'XTerm*VT100*translations: #override : string(\"\\n\")'; unshift(@ARGV, '-wait'); unshift(@ARGV, '-prompt'); my $file = $ARGV[$#ARGV]; $file =~ s,^.*/,,; my $metas = '`~!$&*()|;?<>/"'; $file =~ s/['$metas]//g; $ENV{PROMPT_XWIT} = 1; exec $XTERM, '-geometry', '90x20+300+300', '-title', "anyview:$file", '-xrm', $btn3, '-e', $0, @ARGV; exit 1; } } if ($ARGV[0] eq '-prompt') { shift; if ($ENV{PROMPT_XWIT}) { system("xwit -warp 15 10 >/dev/null 2>&1"); } my $cmd = "av " . join(' ', @ARGV); $| = 1; $f = $ARGV[$#ARGV]; chomp($d = `dirname '$f'`); chomp($b = `basename '$f'`); print "\nfile '$f' output:\n"; system("cd '$d'; file '$b'"); print "\n\nCommand: $cmd\n"; print "\n (you can click Button3 (on the right) for 'yes')\n\n"; print "Run it? [y]/n "; my $x = ; if ($x =~ /n/i ) { exit; } if ($ENV{PROMPT_XWIT}) { system("(sleep 2; xwit -iconify >/dev/null 2>&1) &"); } } #$Nohup = 'nohup '; $Nohup = ''; # Now begins the section the user can make his own mappings # If a file cannot be found here, ~/.mime.types and ~/.mailcap are tried. $Default = 'TXT'; #--------------------->>>>> Begin User Setup <<<<<----------------------------- $x = 'HTML'; push(@suffix, $x); # HTML file $suffix{$x} = '.html'; $print{$x} = "echo Cannot Print"; $view{$x} = "html %s"; $guess{$x} = "is_html %s | grep 1"; #$guess{$x} = "grep -v '^[ ]*\$' %s | head -8 | egrep -i '<[ ]*(HTML|FONT|BODY|A HREF|IMG )'"; #---------------------------------------------------------------------------- $x = 'TXT'; push(@suffix, $x); # Plain Text $suffix{$x} = '.txt'; $print{$x} = "lpr %s"; $view{$x} = "xvi -wrap -wait %s"; # added -wait, will this be OK? #---------------------------------------------------------------------------- $x = 'RFC822'; push(@suffix, $x); # Nested MIME message. $suffix{$x} = '.rfc822'; $view{$x} = "xmf %s"; $guess{$x} = "topheader 25 %s | egrep -i '^Content-Type:.*(multipart/|message/rfc822)|^Received: '"; #---------------------------------------------------------------------------- $x = 'EML'; push(@suffix, $x); # Nested fwded message "(null).eml" $suffix{$x} = '.eml'; $view{$x} = "xmf %s"; $guess{$x} = "topheader 25 %s | egrep -i '^Content-Type:.*(multipart/|message/rfc822)|^Received: '"; #---------------------------------------------------------------------------- $x = 'N1FWD'; push(@suffix, $x); # Nested fwded message case1. $suffix{$x} = '.n1fwd'; $view{$x} = "add_mime_header %s | xmf"; $guess{$x} = "topheader -5 %s | grep -i 'This.is.*MIME.*format'"; #$guess{$x} = "head -5 %s | tr '\\n' ' ' | grep -i 'This.is.*MIME.*format.*Next'"; #---------------------------------------------------------------------------- $x = 'TEX'; push(@suffix, $x); # TeX $suffix{$x} = '.tex'; $print{$x} = "cd %d; texm -np -p %b.tex; texclean -s 20 %b"; $view{$x} = "cd %d; texm -np %b.tex; texclean -s 20 %b"; #---------------------------------------------------------------------------- $x = 'DVI'; push(@suffix, $x); # TeX dvi file $suffix{$x} = '.dvi'; #$print{$x} = "txprl %s"; $print{$x} = "dvips -f -r %s | lpr"; $view{$x} = "xdvi %s"; #---------------------------------------------------------------------------- $x = 'MID'; push(@suffix, $x); # Midi file $suffix{$x} = '.mid'; $print{$x} = "echo Cannot Print"; #$view{$x} = "Xplaymidi %s"; $view{$x} = "Xtimidity %s"; #---------------------------------------------------------------------------- $x = 'MAN'; push(@suffix, $x); # Man-page (troff) $suffix{$x} = '.man'; $print{$x} = ""; $view{$x} = "$XTERM -bg gainsboro -geom 80x30+200+200 -title `basename %s .man` -e nman %s"; #$view{$x} = "$XTERM -bg gainsboro -geom 80x30+200+200 -title `basename %s .man` -e sh -c '(groff -man %s | less)'"; #---------------------------------------------------------------------------- $x = 'MAN2'; push(@suffix, $x); # Man-page (troff) $suffix{$x} = '.1'; $print{$x} = ""; $view{$x} = "$XTERM -bg gainsboro -geom 80x30+200+200 -title `basename %s .1` -e nman %s"; #$view{$x} = "$XTERM -bg gainsboro -geom 80x30+200+200 -title `basename %s .1` -e sh -c '(groff -man %s | less)'"; #$view{$x} = "gnroff -man %s | less"; # kludge with "xterm || tty"? #---------------------------------------------------------------------------- $x = 'TAR'; push(@suffix, $x); # Tar file $suffix{$x} = '.tar'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; $guess{$x} = "file %s | sed -e 's/^.*://' | grep -i 'tar archive'"; #---------------------------------------------------------------------------- $x = 'TGZ'; push(@suffix, $x); # Tar file $suffix{$x} = '.tgz'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; #---------------------------------------------------------------------------- $x = 'TARGZ'; push(@suffix, $x); # Tar file $suffix{$x} = '.tar.gz'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; #---------------------------------------------------------------------------- $x = 'TARZ'; push(@suffix, $x); # Tar file $suffix{$x} = '.tar.Z'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; #---------------------------------------------------------------------------- $x = 'TARB2Z'; push(@suffix, $x); # Tar file $suffix{$x} = '.tar.bz2'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; #---------------------------------------------------------------------------- $x = 'ZIP'; push(@suffix, $x); # Zip file $suffix{$x} = '.zip'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; $guess{$x} = "file %s | egrep -v '\.(sxi|sxw|stw|sxg|sxc|stc|sti|sxd|std|sxm):' | sed -e 's/^.*://' | grep -i 'zip archive'"; #---------------------------------------------------------------------------- $x = 'RPM'; push(@suffix, $x); # Rpm file $suffix{$x} = '.rpm'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; $guess{$x} = "file %s | sed -e 's/^.*://' | grep -i ' RPM '"; #---------------------------------------------------------------------------- $x = 'DEB'; push(@suffix, $x); # Tar file $suffix{$x} = '.deb'; $print{$x} = "echo Cannot Print"; $view{$x} = "untar %s"; $no_bg{$x} = 'True'; $guess{$x} = "file %s | sed -e 's/^.*://' | grep -i 'Debian.*package'"; #---------------------------------------------------------------------------- $x = 'UUENCODE'; push(@suffix, $x); # uuencoded $suffix{$x} = '.uu'; $print{$x} = "echo Cannot Print"; $view{$x} = "uudo %s"; # will prompt!! $no_bg{$x} = 'True'; $guess{$x} = "head -100 %s | grep '^begin [4567][^ ][^ ][ ]'"; #---------------------------------------------------------------------------- $x = 'UUDEVIEW'; push(@suffix, $x); $suffix{$x} = '.ud'; $print{$x} = "echo Cannot Print"; $view{$x} = "uudo -m %s"; # will prompt!! $no_bg{$x} = 'True'; #---------------------------------------------------------------------------- $x = 'PS'; push(@suffix, $x); # PostScript $suffix{$x} = '.ps'; $print{$x} = "lpr %s"; $view{$x} = "gv -geometry +1+1 %s"; $guess{$x} = "head -1 %s | grep '^%!'"; #---------------------------------------------------------------------------- $x = 'PDF'; push(@suffix, $x); # Portable Doc Format $suffix{$x} = '.pdf'; $view{$x} = "acroread %s"; $print{$x} = $view{$x}; $guess{$x} = "head -1 %s | grep '^%PDF'"; #---------------------------------------------------------------------------- $x = 'RTF'; push(@suffix, $x); # Tar file $suffix{$x} = '.rtf'; $print{$x} = "echo Cannot Print"; $view{$x} = "Soffice %s"; $no_bg{$x} = 'True'; $guess{$x} = "file %s | grep -i 'rich text format'"; #---------------------------------------------------------------------------- $x = 'RAM'; push(@suffix, $x); # Real Audio/Video $suffix{$x} = '.ram'; $view{$x} = "rvplayer %s"; $print{$x} = "echo Cannot Print"; $guess{$x} = "head -3 %s | egrep -i '(^pnm:|^rstp:|\.rm\$|\.ram\$)'"; #---------------------------------------------------------------------------- $x = 'SWF'; push(@suffix, $x); # ShockWave flash anim $suffix{$x} = '.swf'; $view{$x} = "swf -wait %s"; $print{$x} = "echo Cannot Print"; #---------------------------------------------------------------------------- $x = 'PLT'; push(@suffix, $x); # Axis Plot $suffix{$x} = '.plt'; $print{$x} = "xaxa -P %s"; $view{$x} = "xaxa %s"; #---------------------------------------------------------------------------- sub tgrind { my($mode, $lang) = @_; if ( $mode eq 'v' ) { # once had -sp #return "cd %d; cat %f | tgrind -l$lang - > %b.dvi; xdvi %b.dvi; texclean -s 20 %b"; return "(cd %d; prettyp -dvi %f; xdvi %f.dvi; rm -f %f.dvi)"; } elsif ( $mode eq 'p' ) { #return "cd %d; cat %f | tgrind -l$lang - > %b.dvi; txprl %b.dvi; texclean -s 20 %b"; return "(cd %d; prettyp %f; Enscript -ps -eps %f.ps; rm -f f.ps)"; } } #---------------------------------------------------------------------------- $x = 'FCODE'; push(@suffix, $x); # Fortran source $suffix{$x} = '.f'; $print{$x} = &tgrind("p", "r"); $view{$x} = &tgrind("v", "r"); #---------------------------------------------------------------------------- $x = 'CPCODE'; push(@suffix, $x); # C++ source $suffix{$x} = '.C'; $print{$x} = &tgrind("p", "cp"); $view{$x} = &tgrind("v", "cp"); #---------------------------------------------------------------------------- $x = 'CCODE'; push(@suffix, $x); # C source $suffix{$x} = '.c'; $print{$x} = &tgrind("p", "c"); $view{$x} = &tgrind("v", "c"); #---------------------------------------------------------------------------- $x = 'HCODE'; push(@suffix, $x); # C header source $suffix{$x} = '.h'; $print{$x} = &tgrind("p", "c"); $view{$x} = &tgrind("v", "c"); #---------------------------------------------------------------------------- $x = 'PCODE'; push(@suffix, $x); # Perl source $suffix{$x} = '.pl'; $print{$x} = &tgrind("p", "perl"); $view{$x} = &tgrind("v", "perl"); $guess{$x} = "head -2 %s | egrep 'bin/perl|exec.perl'"; #---------------------------------------------------------------------------- $x = 'TCLCODE'; push(@suffix, $x); # tcl/tk source $suffix{$x} = '.tcl'; $print{$x} = &tgrind("p", "tcl"); $view{$x} = &tgrind("v", "tcl"); $guess{$x} = "head -3 %s | egrep 'bin/wish|exec.wish'"; #---------------------------------------------------------------------------- $x = 'z_SHCODE'; push(@suffix, $x); # Bourne sh source $suffix{$x} = '.sh'; $print{$x} = &tgrind("p", "sh"); $view{$x} = &tgrind("v", "sh"); $guess{$x} = "head -1 %s | grep 'bin/sh'"; #---------------------------------------------------------------------------- #---------------------->>>>> End User Setup <<<<<---------------------------- ############################################################################# foreach $key (@suffix) { # protect non word chars $suffix{$key} =~ s/(\W)/\\$1/g; } chomp($program = `basename $0`); # Get calling name of program if ( $ARGV[0] =~ /^(-all|-aw)$/ ) { # -all means a list of files. # -aw is -all -wait if ( $ARGV[0] eq '-aw' ) { $ARGV[0] = '-wait'; } else { shift; } $opts = ''; undef @Files; while (@ARGV) { $_ = shift; if ( $_ =~ /^-/ ) { $opts .= "$_ "; } else { push(@Files, $_); } } $all_sleep = '2'; if ( ! @Files ) { opendir(DIR, "."); foreach $file (sort(readdir(DIR))) { if ( -f $file ) { push(@Files, $file); } } } $ForkTrick = 0; $fcnt = 0; foreach $file (@Files) { $file =~ s/'//g; $fcnt++; if ($ForkTrick) { unless(fork) { unless(fork) { system("$program $opts '$file'"); exit 0; } exit 0; } wait; sleep($all_sleep); } else { if ( $file =~ /\.tar$/ || $file =~ /\.(tar\.(gz|z|Z)|tgz)$/ || $opts =~ /-wait/ ) { system("$program $opts '$file'"); # Hook for interactive de-tar.. } else { system("$program $opts '$file' &"); # used to have "nohup" sleep($all_sleep); } } } if ( ! $fcnt ) { print STDERR "\n$program: *** No files found! ***\n\n"; } exit 0; # We gotta leave now } $Tmp = "/tmp/anyview$$"; # Prefix for tmp files. $UrlFetch = 'wcat -c'; $Uncompress{'.Z'} = 'zcat'; # List of uncompressing possibilities $Uncompress{'.z'} = 'gzip -dc'; $Uncompress{'.gz'} = 'gzip -dc'; $Uncompress{'.bz2'} = 'bzip2 -dc'; #$Gzip = 'gzip -9c'; # this is used for compressing $Gzip = 'gzip -9'; # this is used for compressing $Gzip_ext = '.gz'; # under -uuz flag. # Now Process command line args $got_suffix = '0'; $Guess = 'True'; $Mime = ''; $Suff = ''; ARG: while (@ARGV) { $_ = shift; push(@OrigArgs, $_); foreach $key (@suffix) { # Messy, first check for -tex, etc. next if $key eq "HCODE"; # let -h be "-help" next if $key eq "UUENCODE" && $ENV{'UU_FLAG'} ne ''; next if $key eq "UUENCODE"; # -uu and -UU cause too many problems... $flag = $suffix{$key}; $flag =~ s/^\\//; $flag =~ s/^\.//; $flag = "-"."$flag"; if ( $_ =~ /^$flag$/ && ! $got_suffix ) { $Type_Is{$key} = "$got_suffix"; $got_suffix++; $Guess = ''; next ARG; } } CASE: { # Now regular command line opts. /^-remove$/ && ( $remove = 'True', last CASE); /^-v$/ && ( $View = 'True', last CASE); /^(-wait|-w)$/ && ( $Wait = 'True', last CASE); /^-loop$/ && ( $Loop = 'True', $Wait = 'True', last CASE); /^-uud$/ && ($uudir = shift, (&do_uudir, exit 0), last CASE); /^-uu$/i && ( $Encode = 'True', last CASE); /^-uuz$/i && ( $Encode = 'True', $UuZip = 'True', last CASE); /^-content/ && ( $Encode = 'True', $ContentOnly = 'True', last CASE); /^-base64$/i && ( $Encoding_Method = 'base64', last CASE); /^-q$/ && ( $Quiet = 'True', last CASE); /^-s$/ && ( $s = shift, $Suff = $s, $s =~ y/a-z/A-Z/, $Type_Is{$s} = "0", $Guess = '', last CASE); /^-m$/ && ( $Mime = 'True', $Guess = '', last CASE); /^-mt$/ && ( $Mime = 'True', $MimeType = shift, $Guess = '', last CASE); /^-t$/ && ( $Use_Tmp = 1, last CASE); /^-h$/ && ( (&do_help(), exit 0), last CASE); /^-help$/ && ( (&do_help(), exit 0), last CASE); /^-viewers$/ && ( (&do_viewer_list(), exit 0), last CASE); /^-./ && ( (print "$Usage\n$_ is not an option, Bye!\n"), exit 0, last CASE); ($SOURCE = "$_", pop(@OrigArgs), last CASE); } } if ( $Encode ) { # If we are encoding: $Quiet = 'True'; # Be Quiet $Guess = ''; # Don't Guess file type $Mime = 'True'; # For now.. # Just use mime tables. } if ($SOURCE =~ /'/ || $Use_Tmp) { if ($SOURCE =~ /'/) { print STDERR "\n*** Bad news, source filename has single quote: $SOURCE\n"; } my $newfile = "/tmp/anyview.$$"; if ( $SOURCE =~ /(\.[\w-]+)$/ ) { $newfile .= $1; } open(NEW, ">$newfile") || die "$!"; open(OLD, "<$SOURCE") || die "$!"; while () { print NEW; } close(OLD); close(NEW); my $n = 120; print STDERR "*** Copied contents of $SOURCE to $newfile\n*** and will remove in $n seconds.\n"; system("(sleep $n; rm -f '$newfile') &"); $SOURCE = $newfile; } if ( $program =~ /v$/ ) { $View = 'True'; } # Gotta view if ( $program =~ /vm$/ ) { $Mime = 'True'; $Guess = ''; } # Gotta use mime $SOURCE =~ s/'//g; print STDOUT "\nSource is: $SOURCE\n" if $debug && ! $Quiet; if ( $SOURCE eq '' || $SOURCE eq '-' ) { # Check for stdin. $SOURCE = '-'; $stdin = 'True'; } if ( ! $stdin ) { # If not stdin, check if we need to decompress it. if ( $SOURCE =~ m,^(http|ftp)://, ) { $Http = 1; $base = $SOURCE; $base =~ s,^.*/,,; } elsif ( $SOURCE !~ /\.tar\.[zZ]$/ && $SOURCE !~ /\.tar\.gz$/ ) { $Compress = '.Z' if $SOURCE =~ /\.Z$/; $Compress = '.z' if $SOURCE =~ /\.z$/; $Compress = '.gz' if $SOURCE =~ /\.gz$/; } } if ( ($Http || $Compress) && ! $Encode ) { # If so, uncompress, and call ourselves # again. What happens to command line ARGs, eh???? if ( $Http ) { $Compress = ''; } chomp( $Base = `basename '$SOURCE' $Compress`); $File = "${Tmp}_$Base"; $File =~ s/[&<>'`"*\$(){}?]/__%__/g; if ( $Http ) { system("$UrlFetch '$SOURCE' > '$File'"); } else { system("$Uncompress{$Compress} '$SOURCE' > '$File'; ls -l '$File'"); } foreach $arg (@OrigArgs) { $OrigArgs .= "$arg "; } my $cmd = "$0 $OrigArgs -remove '$File'"; if ($ENV{NEW_PGRP}) { $cmd .= " &"; } print STDERR "$cmd\n"; system($cmd); sleep 1; if ($remove) { unlink($SOURCE); } exit 0; } $Type_is = ''; if ($ENV{TEXTONLY}) { $Type_is = 'TXT'; $Guess = ''; } elsif ($Guess) { foreach $key (@suffix) { next if $key eq 'TXT'; if ( $SOURCE =~ /$suffix{$key}$/ ) { $Type_is = $key; $Guess = ''; last; } } } elsif ($Mime) { &do_mime(); exit 0; } else { foreach $key (@suffix) { if ( $Type_Is{$key} eq '0' ) { $Type_is = $key; last; } } } if ( $stdin ) { $File = "${Tmp}_stdin"; open(IN, "<$SOURCE") || die "could not open $SOURCE for reading"; @IN = ; close(IN); open(TMP, ">$File") || die "could not open $File for writing"; print TMP @IN; close(TMP); $SOURCE = "$File"; $remove = 'True'; } if ( $Guess ) { foreach $key (sort(keys(%guess))) { $cmd = $guess{$key}; $cmd =~ s/\%s/'$SOURCE'/g; # print STDERR "Guess cmd: $cmd\n"; chomp($result = `$cmd`); if ( $result ne '' ) { $Type_is = $key; print STDOUT "Guessed type: $Type_is\n" unless $Quiet; $Guess = ''; last; } } } print STDOUT "Type_is is: $Type_is\n" if $debug && ! $Quiet; if ( ! $Type_is ) { exit 0 if &do_mime(); # print STDERR "do_mime(2) done\n"; } if ( $Guess ) { $Type_is = $Default; $Guess = ''; } chomp($FileName = `basename '$SOURCE'`); chomp($DirName = `dirname '$SOURCE'`); chomp($BaseName = `basename '$SOURCE' $suffix{$Type_is}`); print STDOUT "FileName is: $FileName\n" if $debug; print STDOUT "DirName is: $DirName\n" if $debug; print STDOUT "BaseName is: $BaseName\n" if $debug; if ( $View ) { $cmd = $view{$Type_is}; } else { $cmd = $print{$Type_is}; } $cmd =~ s:\%s:'$SOURCE':g; $cmd =~ s:\%B:$DirName/$BaseName:g; $cmd =~ s:\%b:$BaseName:g; $cmd =~ s:\%d:$DirName:g; $cmd =~ s:\%f:$FileName:g; if ( $remove ) { $cmd .= "; rm -f '$SOURCE'"; } print STDOUT "Command is: $cmd\n" if $debug; if ( $no_bg{$Type_is} || $Wait ) { if ( $Wait && $Loop ) { # XXX what is this? $src = ''; if ( $remove ) { sub quit { unlink("$SOURCE"); exit 0; } $SIG{'INT'} = 'quit'; $SIG{'TERM'} = 'quit'; open(SRC, "<$SOURCE"); while() { $src .= $_; } close(SRC); } while (1) { $t0 = time; print STDERR "loop1: $cmd\n"; system "$cmd"; $t1 = time; if ( $t1 - $t0 < 2 ) { sleep 1; } else { &fsleep(0.05); } if ( $src ne '' ) { open(SRC, ">$SOURCE"); print SRC $src; close(SRC); } } } else { system "$cmd"; } } else { system "$Nohup $cmd &"; } ###unless(fork) { # # ### exit 0; ###} exit 0; sub mime_multipart { my($file) = @_; if ( ! -f "$file" ) { return 0; } else { my $test = ''; chomp($test = `grep -i '^content-type:' '$file' | grep -i 'multipart.mixed'`); return 1 if $test ne ''; } return 0; } sub do_mime { if ( $stdin ) { $File = "${Tmp}_stdin"; open(IN, "<$SOURCE") || die "could not open '$SOURCE' for reading"; @IN = ; close(IN); open(TMP, ">$File") || die "could not open $File for writing"; print TMP @IN; close(TMP); if ( ! $Encode && ! -B $File ) { if ( &mime_multipart($File) ) { system "($XTERM -geom 80x35 -e sh -c \"$Multimime '$File'; csh\"; rm -f '$File') &"; } else { system "($XTERM -geom 80x35 -e sh -c \"$Metamail '$File'; csh\"; rm -f '$File') &"; } return 0; } else { $skipMimes = 'True'; } } if ( ! $Encode && &mime_multipart($SOURCE) && ! -B $SOURCE ) { # TODO: protect $SOURCE from spaces: system "($XTERM -geom 80x35 -e sh -c \"$Multimime '$SOURCE'; csh\") &"; } if (! $skipMimes) { # UGH!!! open(MIMETYPES, "<$MimeTypes") || warn "cannot open $MimeTypes"; $Suffix = ''; $Type = ''; $Command = ''; $Type = $MimeType if $MimeType; if ( ! $Type ) { TYPES: while () { next if /^\s*#/; chomp; ($type, $rest) = split(/\s+/, $_, 2); foreach $suffix (split(/\s+/, $rest)) { if ( $SOURCE =~ /\.${suffix}$/i ) { $Type = $type; $Suffix = $suffix; last TYPES; } } } close(MIMETYPES); } if (! $Type) { # Try again with .gz, etc. open(MIMETYPES, "$MimeTypes") || warn "cannot open $MimeTypes"; TRY2: while () { chomp; next if /^\s*#/; ($type, $rest) = split(/\s+/, $_, 2); foreach $suffix (split(/\s+/, $rest)) { foreach $compress (sort(keys(%Uncompress))) { $compress =~ s/(\W)/\\$1/g; if ( $SOURCE =~ /\.${suffix}${compress}$/i ) { $Type = $type; $Suffix = $suffix; last TRY2; } } } } close(MIMETYPES); } if ( $Type ) { open(MAILCAP, "$Mailcap") || warn "cannot open $Mailcap"; while () { next if /^\s*#/; chomp; ($type, $command) = split(/\s*\;\s*/, $_, 2); if ( $type eq $Type ) { $Command = $command; last; } elsif ( $type =~ /\Q$Type\E/i ) { $Command = $command; last; } } close(MAILCAP); # Try wildcard if ( ! $Command ) { $WildType = $Type; $WildType =~ s/\/.*/\/\\*/; open(MAILCAP, "$Mailcap") || warn "cannot open $Mailcap"; while () { next if /^\s*#/; chomp; ($type, $command) = split(/\s*\;\s*/, $_, 2); if ( $type =~ /$WildType/i ) { $Command = $command; last; } } close(MAILCAP); } } if ($Command !~ /%s/) { $Command =~ s/["']?%u["']/%s/g; } $Command =~ s/%s/'$SOURCE'/g if $Command; if ( $remove && $Command ) { $Command .= "; rm -f '$SOURCE'"; } if ( ! $Quiet ) { print STDOUT "Suffix is: $Suffix\n"; print STDOUT "Type is: $Type\n"; print STDOUT "MimeCmd is: $Command\n"; } } # THIS IS END OF KLUDGE "$skipMimes" if(), basically avoids encoding stdin ENCODE: if ( $Encode ) { if ( $ContentOnly ) { $Type = 'none' unless $Type; print STDOUT "$Type\n"; exit 0; } if (! $Type) { $Type = "application/octet-stream"; } my $enc = $Encoding_Method; chomp($BaseFile = `basename '$SOURCE'`); select STDOUT; $| = 1; if ( $enc eq 'uuencode' ) { print STDOUT "Content-Type: $Type\n"; } elsif ( $enc eq 'base64' ) { my $tmpname = $BaseFile; if ($UuZip) { $tmpname .= ".gz"; } print STDOUT "Content-Type: $Type;\n\tname=\"$tmpname\"\n"; } if ( $UuZip ) { if ( $enc eq 'uuencode' ) { print STDOUT "Content-Transfer-Encoding: x-gzip-uuencode\n"; } elsif ( $enc eq 'base64' ) { print STDOUT "Content-Transfer-Encoding: gzip-base64\n"; } } else { if ( $enc eq 'uuencode' ) { print STDOUT "Content-Transfer-Encoding: x-uuencode\n"; } elsif ( $enc eq 'base64' ) { print STDOUT "Content-Transfer-Encoding: base64\n"; } } print STDOUT "\n"; if ($stdin) { $SOURCE = $File; $remove = 'True'; } if ( ! $UuZip ) { if ( $enc eq 'uuencode' ) { system "uuencode '$SOURCE' '$BaseFile'"; } elsif ( $enc eq 'base64' ) { system "tobase64 '$SOURCE'"; } } else { # print STDERR "BaseFile:$BaseFile:\n"; sleep 5; $Tmp_dir = "/tmp/av_dir$$"; system("mkdir $Tmp_dir"); system("cp -p '$SOURCE' $Tmp_dir"); system("$Gzip '$Tmp_dir/$BaseFile'"); if ( $enc eq 'uuencode' ) { system "uuencode '$Tmp_dir/${BaseFile}${Gzip_ext}' '${BaseFile}${Gzip_ext}'"; } elsif ( $enc eq 'base64' ) { system "tobase64 '$Tmp_dir/${BaseFile}${Gzip_ext}'"; } system("rm -rf $Tmp_dir"); } if ($remove) { unlink("$SOURCE"); # GULP??? } exit 0; # We are truly done. } if ( $Command ) { if ( $Wait ) { if ( $Loop ) { # XXX what is this? $src = ''; if ( $remove ) { sub quit { unlink("$SOURCE"); exit 0; } $SIG{'INT'} = 'quit'; $SIG{'TERM'} = 'quit'; open(SRC, "$SOURCE"); while() { $src .= $_; } close(SRC); } while (1) { $t0 = time; print STDERR "loop2: $Command\n"; system "$Nohup $Command"; $t1 = time; if ( $t1 - $t0 < 2 ) { sleep 1; } else { &fsleep(0.05); } if ( $src ne '' ) { open(SRC, ">$SOURCE"); print SRC $src; close(SRC); } } } else { system "$Nohup $Command"; } } else { system "$Nohup $Command &"; } return $Command; } else { warn "No command to run for '$SOURCE'\n"; # bad idea to remove here... (TXT default will have no file) #if ( $remove ) { # unlink("$SOURCE"); #} return 0; } } sub do_uudir { $uudir =~ s/^\s*//; $uudir =~ s/\s*$//; $uudir =~ s/\/$//; if ( ! -d $uudir ) { print STDERR "$program: \"$uudir\" not a directory"; exit 1; } select STDOUT; $| = 1; chomp($base = `basename '$uudir'`); print STDOUT "\n"; # print STDOUT "du -s $uudir:\n"; print STDOUT "Content-Description: Tarred directory: \"$uudir\"\n"; # system "du -s '$uudir'"; print STDOUT "Content-Transfer-Encoding: x-gzip-uuencode\n\n"; system "cd $uudir; cd ..; tar -czf - '$base' | uuencode '$base.tar.gz'"; } sub do_help { $suffs = ''; foreach $key (@suffix) { $tkey = $suffix{$key}; $tkey =~ s/\\//g; $suffs .= "$tkey "; } $Usage = <<"End_of_Help"; $program: View or Print "anything". $Version Usage: $program [] [] where can be: - (from defined suffix without ".", e.g. -tex) -s pretend suffix is . -mt, Use Mime . -v Force viewing only. -q Be quiet. -m Do Mime only. -uu,-UU uuencode file with "Content-Type:" header for inclusion in an email. Makes semi-MIME compliant *section*. -uuz apply "$Gzip" before uuencoding. -uud Tar up, compress, and uuencode directory . -content Only return the "Content Type: " data. -remove Remove sourcefile upon completion. -t copy file to tmp first. -viewers List viewer commands needed. -wait wait for each view to finish before proceeding to next. -help Print this help. may be stdin. If using stdin with -m, program "$Metamail" is used. Also: $program -all [] [] perform "$program " on each file in . If is empty, all regular files in current dir are used. -aw is a synonym for -all -wait ----------------------------------------------------------------------------- In general, use "av" for viewing and "ap" for printing, "avm" for "av -m" (uses Mimetype files only). (installer's note: just make symbolic links to "ap" and "avm") Uses user defined type directives, placed in top portion of this program file: "$0" along with magic detectors to process files for viewing or printing. Also uses mimetypes: "$MimeTypes", and mailcap: "$Mailcap" to aid in determining file types. User defined suffixes: $suffs Bugs: Does not make a true MIME compatible file as with "mmencode" or "mailto", just makes a possibly acceptable subsection. If .mime.types must be used there is no way to print. In fact, if that file has extra functionality, $program will most likely fail. See Also: multi_mime -help mailf -help End_of_Help if ( $ENV{'PAGER'} ne '' && open(PAGER, "|$ENV{'PAGER'}") ) { print PAGER "$Usage"; close(PAGER); } elsif ( open(LESS, "|less") ) { print LESS "$Usage"; close(LESS); } elsif ( open(MORE, "|more") ) { # open pipeline to "more" print MORE "$Usage"; close(MORE); } else { # wot no more? print STDOUT "$Usage"; } } sub do_viewer_list { $viewers = ''; foreach $key (sort(keys(%view))) { $foo = $suffix{$key}; $foo =~ s/^\\//; $viewers .= ">> $key ($foo):\n"; $viewers .= "$view{$key}\n"; } $viewers .= "\nAlso see \$HOME/.mailcap and \$HOME/.mime.types\n"; if ( open(LESS, "|less") ) { print LESS "$viewers"; close(LESS); } elsif ( open(MORE, "|more") ) { # open pipeline to "more" print MORE "$viewers"; close(MORE); } else { # wot no more? print STDOUT "$viewers"; } } sub fsleep { my ($time) = @_; select(undef, undef, undef, $time) if $time; } __END__ TODO: If $SOURCE does not have suffix, what do we do? cd %d, what if source is my.tex? $dir='.'? bug with xdvi once or twice. Should standardize "magic" file detection, or at least incorporate. .mailcap_print Probably should ignore $Compress when doing $Encode, right?