4 # $Id: lessopen,v 1.4 1998/05/12 09:37:46 torin Exp $
5 # Plus POSIX sh changes by Y.Dirson
7 # Less filter for viewing non text files.
9 # Written by: Behan Webster <behanw@pobox.com>
10 # Many Modifications by Darren Stalder
11 # Further Modifications by Thomas Schoepf <schoepf@debian.org>
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
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.
20 # Usage: eval `lessfile` or eval `lesspipe`
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
27 TMPDIR=${TMPDIR:-/tmp}
28 BASENAME=`basename $0`
31 # Helper function to list contents of ISO files (CD images)
34 isoinfo -d -i "$1" | grep -q ^Rock\.Ridge && iiopts="$iiopts -R"
35 isoinfo -d -i "$1" | grep -q ^Joliet && iiopts="$iiopts -J"
37 isoinfo -f $iiopts -i "$1"
40 if [ $# -eq 1 ] ; then
41 # we were called as LESSOPEN
43 # if the file doesn't exist, we don't do anything
44 if [ ! -r "$1" ]; then
48 # generate filename for possible use by lesspipe
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"
59 # possibly redirect stdout to a file for lessfile
60 if [ $BASENAME = $LESSFILE ]; then exec > $TMPFILE; fi
62 # Allow for user defined filters
63 #if [ -x ~/.lessfilter -a -O ~/.lessfilter ]; then
64 if [ -x ~/.lessfilter ]; then
67 if [ $BASENAME = $LESSFILE ]; then
68 if [ -s $TMPFILE ]; then
78 # Decode file for less
79 case `echo "$1" | tr '[:upper:]' '[:lower:]'` in
81 if [ -x "`which ar`" ]; then ar tv "$1"
82 else echo "No ar available"; fi ;;
85 if [ -x "`which unarj`" ]; then unarj l "$1"
86 else echo "No unarj available"; fi ;;
89 if [ -x "`which bunzip2`" ]; then
90 bunzip2 -dc "$1" | tar tvvf -
91 else echo "No bunzip2 available"; fi ;;
94 if [ -x "`which bunzip`" ]; then bunzip -c "$1"
95 else echo "No bunzip available"; fi ;;
98 if [ -x "`which bunzip2`" ]; then bunzip2 -dc "$1"
99 else echo "No bunzip2 available"; fi ;;
102 echo "$1:"; dpkg --info "$1"
104 echo '*** Contents:'; dpkg-deb --contents "$1"
108 if [ -x "`which catdoc`" ]; then
111 # no catdoc, read normally if file is text.
112 if ( file "$1" | grep ASCII 2>/dev/null >/dev/null); then
115 echo "No catdoc available";
120 *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
121 if [ -x "`which identify`" ]; then
124 echo "No identify available"
125 echo "Install ImageMagick to browse images"
130 if [ -x "`which isoinfo`" ]; then iso_list "$1"
132 echo "No isoinfo available"
133 echo "Install mkisofs to view ISO images"
138 if [ -x "`which isoinfo`" ]; then
139 file "$1" | grep -q ISO\.9660 && iso_list "$1"
141 echo "No isoinfo available"
142 echo "Install mkisofs to view ISO images"
147 if [ -x "`which lha`" ]; then lha v "$1"
148 else echo "No lha available"; fi ;;
151 if [ -x "`which lzip`" ]; then
152 lzip -dc "$1" | tar tvvf -
153 elif [ -x "`which lunzip`" ]; then
154 lunzip -dc "$1" | tar tvvf -
155 else echo "No lzip or lunzip available"; fi ;;
158 if [ -x "`which lzip`" ]; then lzip -dc "$1"
159 elif [ -x "`which lunzip`" ]; then lunzip -dc "$1"
160 else echo "No lzip or lunzip available"; fi ;;
163 if [ -x "`which lzma`" ]; then
164 lzma -dc "$1" | tar tfvv -
166 echo "No lzma available"
171 if [ -x "`which lzma`" ]; then
174 echo "No lzma available"
179 if [ -x "`which pdftotext`" ]; then pdftotext -layout "$1" -
180 else echo "No pdftotext available"; fi ;;
183 if [ -x "`which rar`" ]; then rar v "$1"
184 elif [ -x "`which unrar`" ]; then unrar v "$1"
185 else echo "No rar or unrar available"; fi ;;
188 if [ -x "`which rpm`" ]; then
189 echo "$1:"; rpm -q -i -p "$1"
193 else echo "rpm isn't available, no query on rpm package possible"; fi ;;
195 *.tar.gz|*.tgz|*.tar.z|*.tar.dz)
196 tar tzvf "$1" --force-local
199 # Note that this is out of alpha order so that we don't catch
200 # the gzipped tar files.
205 tar tvf "$1" --force-local
208 *.jar|*.war|*.ear|*.xpi|*.zip)
209 if [ -x "`which unzip`" ]; then unzip -v "$1";
210 elif [ -x "`which miniunzip`" ]; then miniunzip -l "$1";
211 elif [ -x "`which miniunz`" ]; then miniunz -l "$1";
212 else echo "No unzip, miniunzip or miniunz available"; fi ;;
215 if [ -x "`which 7za`" ]; then 7za l "$1";
216 elif [ -x "`which 7zr`" ]; then 7zr l "$1";
217 else echo "No 7za or 7zr available"; fi ;;
220 if [ -x "`which zoo`" ]; then zoo v "$1";
221 elif [ -x "`which unzoo`" ]; then unzoo -l "$1";
222 else echo "No unzoo or zoo available"; fi ;;
227 if [ $BASENAME = $LESSFILE ]; then
228 if [ -s $TMPFILE ]; then
235 elif [ $# -eq 2 ] ; then
237 # we were called as LESSCLOSE
238 # delete the file created if we were lessfile
240 if [ $BASENAME = $LESSFILE ]; then
241 if [ -n "$BASH" ]; then
242 if [ ! -O "$2" ]; then
243 echo "Error in deleting $2" > /dev/tty
250 echo "Error in deleting $2" > /dev/tty
254 elif [ $# -eq 0 ] ; then
256 # must setup shell to use LESSOPEN/LESSCLOSE
258 # I have no idea how some of the more esoteric shells (es, rc) do
259 # things. If they don't do things in a Bourne manner, send me a patch
260 # and I'll incorporate it.
263 # first determine the full path of lessfile/lesspipe
264 # if you can determine a better way to do this, send me a patch, I've
265 # not shell-scripted for many a year.
266 FULLPATH=`cd \`dirname $0\`;pwd`/$BASENAME
270 if [ $BASENAME = $LESSFILE ]; then
271 echo "setenv LESSOPEN \"$FULLPATH %s\";"
272 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
274 echo "setenv LESSOPEN \"| $FULLPATH %s\";"
275 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
279 if [ $BASENAME = $LESSFILE ]; then
280 echo "export LESSOPEN=\"$FULLPATH %s\";"
281 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
283 echo "export LESSOPEN=\"| $FULLPATH %s\";"
284 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
289 #echo "# If you tried to view a file with a name that starts with '#', you"
290 #echo "# might see this message instead of the file's contents."
291 #echo "# To view the contents, try to put './' ahead of the filename when"
292 #echo "# calling less."
295 echo "Usage: eval \`$BASENAME\`"