home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / lesspipe < prev    next >
Encoding:
Text File  |  2006-06-19  |  6.1 KB  |  245 lines

  1. #!/bin/sh
  2. #
  3. # lessfile/lesspipe
  4. # $Id: lessopen,v 1.4 1998/05/12 09:37:46 torin Exp $
  5. # Plus POSIX sh changes by Y.Dirson
  6. #
  7. # Less filter for viewing non text files.
  8. #
  9. # Written by: Behan Webster <behanw@pobox.com>
  10. # Many Modifications by Darren Stalder
  11. # Further Modifications by Thomas Schoepf <schoepf@debian.org>
  12. #
  13. # combined lessfile and lesspipe to avoid duplication of decode stage
  14. # shell is sure icky.  I'm real tempted to rewrite the whole thing in Perl 
  15. #
  16. # Unfortunately, this means that I have filename dependencies sprinkled
  17. # throughout the code.  If you don't want lessfile to be called that,
  18. # you'll need to change the LESSFILE envar below.
  19. #
  20. # Usage: eval `lessfile`  or eval `lesspipe`
  21. #
  22. # less passes in:
  23. #    $1  filename to be viewed with less  (used by LESSOPEN)
  24. # and possibly (if used by lessfile)
  25. #    $2  filename that was created during LESSOPEN
  26.  
  27. TMPDIR=${TMPDIR:-/tmp}
  28. BASENAME=`basename $0`
  29. LESSFILE=lessfile
  30.  
  31. # Helper function to list contents of ISO files (CD images)
  32. iso_list() {
  33.     isoinfo -d -i "$1"
  34.     isoinfo -d -i "$1" | grep -q ^Rock\.Ridge && iiopts="$iiopts -R"
  35.     isoinfo -d -i "$1" | grep -q ^Joliet && iiopts="$iiopts -J"
  36.     echo
  37.     isoinfo -f $iiopts -i "$1"
  38. }
  39.  
  40. if [ $# -eq 1 ] ; then
  41.     # we were called as LESSOPEN
  42.  
  43.     # if the file doesn't exist, we don't do anything
  44.     if [ ! -r "$1" ]; then
  45.         exit 0
  46.     fi
  47.  
  48.     # generate filename for possible use by lesspipe
  49.     umask 077
  50.     if [ $BASENAME = $LESSFILE ]; then
  51.         TMPFILE=`tempfile -d $TMPDIR -p lessf`
  52.         if [ -z "$TMPFILE" ]; then
  53.             echo >&2 "Could not find essential program 'tempfile'. Exiting"
  54.       exit 1
  55.         fi
  56.     fi
  57.  
  58.     (
  59.         # possibly redirect stdout to a file for lessfile
  60.         if [ $BASENAME = $LESSFILE ]; then exec > $TMPFILE; fi
  61.  
  62.         # Allow for user defined filters
  63.         #if [ -x ~/.lessfilter -a -O ~/.lessfilter ]; then
  64.         if [ -x ~/.lessfilter ]; then
  65.             ~/.lessfilter "$1"
  66.             if [ $? -eq 0 ]; then
  67.                 if [ $BASENAME = $LESSFILE ]; then
  68.                     if [ -s $TMPFILE ]; then
  69.                         echo $TMPFILE
  70.                     else
  71.                         rm -f $TMPFILE
  72.                     fi
  73.                 fi
  74.                 exit 0
  75.             fi
  76.         fi
  77.  
  78.         # Decode file for less
  79.         case `echo "$1" | tr '[:upper:]' '[:lower:]'` in
  80.             *.arj)
  81.                 if [ -x "`which unarj`" ]; then unarj l "$1"
  82.                 else echo "No unarj available"; fi ;;
  83.  
  84.             *.tar.bz2)
  85.                 if [ -x "`which bunzip2`" ]; then
  86.                     bunzip2 -dc "$1" | tar tvvf -
  87.                 else echo "No bunzip2 available"; fi ;;
  88.  
  89.             *.bz)
  90.                 if [ -x "`which bunzip`" ]; then bunzip -c "$1"
  91.                 else echo "No bunzip available"; fi ;;
  92.  
  93.             *.bz2)
  94.                 if [ -x "`which bunzip2`" ]; then bunzip2 -dc "$1"
  95.                 else echo "No bunzip2 available"; fi ;;
  96.  
  97.             *.deb|*.udeb)
  98.                 echo "$1:"; dpkg --info "$1"
  99.                 echo
  100.                 echo '*** Contents:'; dpkg-deb --contents "$1"
  101.                 ;;
  102.  
  103.             *.doc)
  104.                 if [ -x "`which catdoc`" ]; then catdoc "$1"; fi ;;
  105.  
  106.             *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
  107.                 if [ -x "`which identify`" ]; then
  108.                     identify "$1"
  109.                 else
  110.                     echo "No identify available"
  111.                     echo "Install ImageMagick to browse images"
  112.                 fi
  113.                 ;;
  114.  
  115.             *.iso)
  116.                 if [ -x "`which isoinfo`" ]; then iso_list "$1"; fi ;;
  117.  
  118.             *.bin|*.raw)
  119.                 if [ -x "`which isoinfo`" ]; then
  120.                     file "$1" | grep -q ISO\.9660 && iso_list "$1"
  121.                 fi
  122.                 ;;
  123.  
  124.             *.lha|*.lzh)
  125.                 if [ -x "`which lha`" ]; then lha v "$1"
  126.                 else echo "No lha available"; fi ;;
  127.  
  128.             *.pdf)
  129.                 if [ -x "`which pdftotext`" ]; then pdftotext "$1" -
  130.                 else echo "No pdftotext available"; fi ;;
  131.  
  132.             *.rar|*.r[0-9][0-9])
  133.                 if [ -x "`which rar`" ]; then rar v "$1"
  134.                 elif [ -x "`which unrar`" ]; then unrar v "$1"
  135.                 else echo "No rar or unrar available"; fi ;;
  136.  
  137.             *.rpm)
  138.                 if [ -x "`which rpm`" ]; then
  139.                     echo "$1:"; rpm -q -i -p "$1"
  140.                     echo
  141.                     echo '*** Contents:'
  142.                     rpm -q -l -p "$1"
  143.                 else echo "rpm isn't available, no query on rpm package possible"; fi ;;
  144.  
  145.             *.tar.gz|*.tgz|*.tar.z|*.tar.dz)
  146.                 tar tzvf "$1" ;;
  147.  
  148.             # Note that this is out of alpha order so that we don't catch
  149.             # the gzipped tar files.
  150.             *.gz|*.z|*.dz)
  151.                 gzip -dc "$1" ;;
  152.  
  153.             *.tar)
  154.                 tar tvf "$1" ;;
  155.  
  156.             *.jar|*.war|*.ear|*.xpi|*.zip)
  157.                 if [ -x "`which unzip`" ]; then unzip -v "$1";
  158.                 elif [ -x "`which miniunzip`" ]; then miniunzip -l "$1";
  159.                 elif [ -x "`which miniunz`" ]; then miniunz -l "$1";
  160.                 else echo "No unzip, miniunzip or miniunz available"; fi ;;
  161.  
  162.             *.7z)
  163.                 if [ -x "`which 7za`" ]; then 7za l "$1";
  164.                 else echo "No 7za available"; fi ;;
  165.  
  166.             *.zoo)
  167.                 if [ -x "`which zoo`" ]; then zoo v "$1";
  168.                 elif [ -x "`which unzoo`" ]; then unzoo -l "$1";
  169.                 else echo "No unzoo or zoo available"; fi ;;
  170.  
  171.         esac
  172.     ) 2>/dev/null
  173.  
  174.     if [ $BASENAME = $LESSFILE ]; then
  175.         if [ -s $TMPFILE ]; then
  176.             echo $TMPFILE
  177.         else
  178.             rm -f $TMPFILE
  179.         fi
  180.     fi
  181.  
  182. elif [ $# -eq 2 ] ; then
  183.     #
  184.     # we were called as LESSCLOSE
  185.     # delete the file created if we were lessfile
  186.     #
  187.     if [ $BASENAME = $LESSFILE ]; then
  188.     if [ -n "$BASH" ]; then
  189.       if [ ! -O "$2" ]; then
  190.         echo "Error in deleting $2" > /dev/tty
  191.       fi
  192.     fi
  193.  
  194.         if [ -f "$2" ]; then
  195.             rm -f "$2"
  196.         else
  197.             echo "Error in deleting $2" > /dev/tty
  198.         fi
  199.     fi
  200.  
  201. elif [ $# -eq 0 ] ; then
  202.     #
  203.     # must setup shell to use LESSOPEN/LESSCLOSE
  204.     #
  205.     # I have no idea how some of the more esoteric shells (es, rc) do
  206.     # things. If they don't do things in a Bourne manner, send me a patch
  207.     # and I'll incorporate it.
  208.     #
  209.  
  210.     # first determine the full path of lessfile/lesspipe
  211.     # if you can determine a better way to do this, send me a patch, I've
  212.     # not shell-scripted for many a year.
  213.     FULLPATH=`cd \`dirname $0\`;pwd`/$BASENAME
  214.  
  215.     case "$SHELL" in
  216.         *csh)
  217.             if [ $BASENAME = $LESSFILE ]; then
  218.                 echo "setenv LESSOPEN \"$FULLPATH %s\";"
  219.                 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
  220.             else
  221.                 echo "setenv LESSOPEN \"| $FULLPATH %s\";"
  222.                 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
  223.             fi
  224.             ;;
  225.         *)
  226.             if [ $BASENAME = $LESSFILE ]; then
  227.                 echo "export LESSOPEN=\"$FULLPATH %s\";"
  228.                 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
  229.             else
  230.                 echo "export LESSOPEN=\"| $FULLPATH %s\";"
  231.                 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
  232.             fi
  233.             ;;
  234.     esac
  235.  
  236.     #echo "# If you tried to view a file with a name that starts with '#', you"
  237.     #echo "# might see this message instead of the file's contents."
  238.     #echo "# To view the contents, try to put './' ahead of the filename when"
  239.     #echo "# calling less."
  240.  
  241. else
  242.     echo "Usage: eval \`$BASENAME\`"
  243.     exit
  244. fi
  245.