Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / bin / lesspipe
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                         *.a)
81                                 if [ -x "`which ar`" ]; then ar tv "$1"
82                                 else echo "No ar available"; fi ;;
83
84                         *.arj)
85                                 if [ -x "`which unarj`" ]; then unarj l "$1"
86                                 else echo "No unarj available"; fi ;;
87
88                         *.tar.bz2)
89                                 if [ -x "`which bunzip2`" ]; then
90                                         bunzip2 -dc "$1" | tar tvvf -
91                                 else echo "No bunzip2 available"; fi ;;
92
93                         *.bz)
94                                 if [ -x "`which bunzip`" ]; then bunzip -c "$1"
95                                 else echo "No bunzip available"; fi ;;
96
97                         *.bz2)
98                                 if [ -x "`which bunzip2`" ]; then bunzip2 -dc "$1"
99                                 else echo "No bunzip2 available"; fi ;;
100
101                         *.deb|*.udeb)
102                                 echo "$1:"; dpkg --info "$1"
103                                 echo
104                                 echo '*** Contents:'; dpkg-deb --contents "$1"
105                                 ;;
106
107                         *.doc)
108                                 if [ -x "`which catdoc`" ]; then
109                                         catdoc "$1"
110                                 else
111                                         # no catdoc, read normally if file is text.
112                                         if ( file "$1" | grep ASCII 2>/dev/null >/dev/null); then
113                                                 cat "$1"
114                                         else
115                                                 echo "No catdoc available";
116                                         fi
117                                 fi
118                                 ;;
119
120                         *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
121                                 if [ -x "`which identify`" ]; then
122                                         identify "$1"
123                                 else
124                                         echo "No identify available"
125                                         echo "Install ImageMagick to browse images"
126                                 fi
127                                 ;;
128
129                         *.iso)
130                                 if [ -x "`which isoinfo`" ]; then iso_list "$1"
131                                 else
132                                         echo "No isoinfo available"
133                                         echo "Install mkisofs to view ISO images"
134                                 fi
135                                 ;;
136
137                         *.bin|*.raw)
138                                 if [ -x "`which isoinfo`" ]; then
139                                         file "$1" | grep -q ISO\.9660 && iso_list "$1"
140                                 else
141                                         echo "No isoinfo available"
142                                         echo "Install mkisofs to view ISO images"
143                                 fi
144                                 ;;
145
146                         *.lha|*.lzh)
147                                 if [ -x "`which lha`" ]; then lha v "$1"
148                                 else echo "No lha available"; fi ;;
149
150                         *.tar.lz|*.tlz)
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 ;;
156
157                         *.lz)
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 ;;
161
162                         *.tar.lzma)
163                                 if [ -x "`which lzma`" ]; then
164                                         lzma -dc "$1" | tar tfvv -
165                                 else
166                                         echo "No lzma available"
167                                 fi
168                                 ;;
169
170                         *.lzma)
171                                 if [ -x "`which lzma`" ]; then
172                                         lzma -dc "$1"
173                                 else
174                                         echo "No lzma available"
175                                 fi
176                                 ;;
177
178                         *.pdf)
179                                 if [ -x "`which pdftotext`" ]; then pdftotext -layout "$1" -
180                                 else echo "No pdftotext available"; fi ;;
181
182                         *.rar|*.r[0-9][0-9])
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 ;;
186
187                         *.rpm)
188                                 if [ -x "`which rpm`" ]; then
189                                         echo "$1:"; rpm -q -i -p "$1"
190                                         echo
191                                         echo '*** Contents:'
192                                         rpm -q -l -p "$1"
193                                 else echo "rpm isn't available, no query on rpm package possible"; fi ;;
194
195                         *.tar.gz|*.tgz|*.tar.z|*.tar.dz)
196                                 tar tzvf "$1" --force-local
197                                 ;;
198
199                         # Note that this is out of alpha order so that we don't catch
200                         # the gzipped tar files.
201                         *.gz|*.z|*.dz)
202                                 gzip -dc "$1" ;;
203
204                         *.tar)
205                                 tar tvf "$1" --force-local
206                                 ;;
207
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 ;;
213
214                         *.7z)
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 ;;
218
219                         *.zoo)
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 ;;
223
224                 esac
225         ) 2>/dev/null
226
227         if [ $BASENAME = $LESSFILE ]; then
228                 if [ -s $TMPFILE ]; then
229                         echo $TMPFILE
230                 else
231                         rm -f $TMPFILE
232                 fi
233         fi
234
235 elif [ $# -eq 2 ] ; then
236         #
237         # we were called as LESSCLOSE
238         # delete the file created if we were lessfile
239         #
240         if [ $BASENAME = $LESSFILE ]; then
241                 if [ -n "$BASH" ]; then
242                         if [ ! -O "$2" ]; then
243                                 echo "Error in deleting $2" > /dev/tty
244                         fi
245                 fi
246
247                 if [ -f "$2" ]; then
248                         rm -f "$2"
249                 else
250                         echo "Error in deleting $2" > /dev/tty
251                 fi
252         fi
253
254 elif [ $# -eq 0 ] ; then
255         #
256         # must setup shell to use LESSOPEN/LESSCLOSE
257         #
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.
261         #
262
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
267
268         case "$SHELL" in
269                 *csh)
270                         if [ $BASENAME = $LESSFILE ]; then
271                                 echo "setenv LESSOPEN \"$FULLPATH %s\";"
272                                 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
273                         else
274                                 echo "setenv LESSOPEN \"| $FULLPATH %s\";"
275                                 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
276                         fi
277                         ;;
278                 *)
279                         if [ $BASENAME = $LESSFILE ]; then
280                                 echo "export LESSOPEN=\"$FULLPATH %s\";"
281                                 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
282                         else
283                                 echo "export LESSOPEN=\"| $FULLPATH %s\";"
284                                 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
285                         fi
286                         ;;
287         esac
288
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."
293
294 else
295         echo "Usage: eval \`$BASENAME\`"
296         exit
297 fi