Debian Codegraph @

debian/lesspipe

raw
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 <[email protected]>
10 # Many Modifications by Darren Stalder
11 # Further Modifications by Thomas Schoepf <[email protected]>
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 CONFIGDIR=${XDG_CONFIG_HOME:-~/.config}
29 BASENAME=`basename $0`
30 LESSFILE=lessfile
31
32 # Helper function to list contents of ISO files (CD images)
33 iso_list() {
34 isoinfo -d -i "$1"
35 isoinfo -d -i "$1" | grep -q ^Rock\.Ridge && iiopts="$iiopts -R"
36 isoinfo -d -i "$1" | grep -q ^Joliet && iiopts="$iiopts -J"
37 echo
38 isoinfo -f $iiopts -i "$1"
39 }
40
41 if [ $# -eq 1 ] ; then
42 # we were called as LESSOPEN
43
44 # if the file doesn't exist, we don't do anything
45 if [ ! -r "$1" ]; then
46 exit 0
47 fi
48
49 # generate filename for possible use by lesspipe
50 umask 077
51 if [ $BASENAME = $LESSFILE ]; then
52 TMPFILE=`mktemp -p $TMPDIR lessfXXXXXX`
53 if [ -z "$TMPFILE" ]; then
54 echo >&2 "TMPFILE variable is empty. Exiting"
55 exit 1
56 fi
57 fi
58
59 (
60 # Allow for user defined filters
61 USERFILTER=
62 if [ -x "$CONFIGDIR/lessfilter" ]; then
63 USERFILTER="$CONFIGDIR/lessfilter"
64 elif [ -x ~/.lessfilter ]; then
65 USERFILTER=~/.lessfilter
66 fi
67 if [ -n "$USERFILTER" ]; then
68 if [ $BASENAME = $LESSFILE ]; then
69 "$USERFILTER" "$1" > $TMPFILE
70 if [ $? -eq 0 ]; then
71 if [ -s $TMPFILE ]; then
72 echo $TMPFILE
73 else
74 rm -f $TMPFILE
75 fi
76 exit 0
77 else
78 rm -f $TMPFILE
79 fi
80 else
81 "$USERFILTER" "$1" && exit 0
82 fi
83 fi
84
85 # possibly redirect stdout to a file for lessfile
86 if [ $BASENAME = $LESSFILE ]; then exec > $TMPFILE; fi
87
88 # Decode file for less
89 case `echo "$1" | tr '[:upper:]' '[:lower:]'` in
90 *.a)
91 if [ -x "`which ar`" ]; then ar tv "$1"
92 else echo "No ar available"; fi ;;
93
94 *.arj)
95 if [ -x "`which unarj`" ]; then unarj l "$1"
96 else echo "No unarj available"; fi ;;
97
98 *.tar.bz2)
99 if [ -x "`which bunzip2`" ]; then
100 bunzip2 -dc "$1" | tar tvvf -
101 else echo "No bunzip2 available"; fi ;;
102
103 *.bz)
104 if [ -x "`which bunzip`" ]; then bunzip -c "$1"
105 else echo "No bunzip available"; fi ;;
106
107 *.bz2)
108 if [ -x "`which bunzip2`" ]; then bunzip2 -dc "$1"
109 else echo "No bunzip2 available"; fi ;;
110
111 *.deb|*.udeb|*.ddeb|*.ipk)
112 echo "$1:"; dpkg --info "$1"
113 echo
114 echo '*** Contents:'; dpkg-deb --contents "$1"
115 ;;
116
117 *.doc)
118 if [ -x "`which catdoc`" ]; then
119 catdoc "$1"
120 else
121 # no catdoc, read normally if file is text.
122 if ( file "$1" | grep ASCII 2>/dev/null >/dev/null); then
123 cat "$1"
124 else
125 echo "No catdoc available";
126 fi
127 fi
128 ;;
129
130 *.egg)
131 if [ -x "`which unzip`" ]; then
132 unzip -p "$1" EGG-INFO/PKG-INFO | \
133 sed '/^Description:/,/^\S/{/^Description:/d; /^\S/b; d}'
134 echo
135 unzip -v "$1"
136 else
137 echo "No unzip available"
138 fi
139 ;;
140
141 *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
142 if [ -x "`which identify`" ]; then
143 identify "$1"
144 else
145 echo "No identify available"
146 echo "Install ImageMagick to browse images"
147 fi
148 ;;
149
150 *.iso)
151 if [ -x "`which isoinfo`" ]; then iso_list "$1"
152 else
153 echo "No isoinfo available"
154 echo "Install mkisofs to view ISO images"
155 fi
156 ;;
157
158 *.bin|*.raw)
159 if [ -x "`which isoinfo`" ]; then
160 file "$1" | grep -q ISO\.9660 && iso_list "$1"
161 else
162 echo "No isoinfo available"
163 echo "Install mkisofs to view ISO images"
164 fi
165 ;;
166
167 *.lha|*.lzh)
168 if [ -x "`which lha`" ]; then lha v "$1"
169 else echo "No lha available"; fi ;;
170
171 *.tar.lz|*.tlz)
172 if [ -x "`which lzip`" ]; then
173 lzip -dc "$1" | tar tvvf -
174 elif [ -x "`which lunzip`" ]; then
175 lunzip -dc "$1" | tar tvvf -
176 else echo "No lzip or lunzip available"; fi ;;
177
178 *.lz)
179 if [ -x "`which lzip`" ]; then lzip -dc "$1"
180 elif [ -x "`which lunzip`" ]; then lunzip -dc "$1"
181 else echo "No lzip or lunzip available"; fi ;;
182
183 *.tar.lzma)
184 if [ -x "`which lzma`" ]; then
185 lzma -dc "$1" | tar tfvv -
186 else
187 echo "No lzma available"
188 fi
189 ;;
190
191 *.lzma)
192 if [ -x "`which lzma`" ]; then
193 lzma -dc "$1"
194 else
195 echo "No lzma available"
196 fi
197 ;;
198
199 *.pdf)
200 if [ -x "`which pdftotext`" ]; then pdftotext -layout "$1" -
201 else echo "No pdftotext available"; fi ;;
202
203 *.rar|*.r[0-9][0-9])
204 if [ -x "`which rar`" ]; then rar v "$1"
205 elif [ -x "`which unrar`" ]; then unrar v "$1"
206 else echo "No rar or unrar available"; fi ;;
207
208 *.rpm)
209 if [ -x "`which rpm`" ]; then
210 echo "$1:"; rpm -q -i -p "$1"
211 echo
212 echo '*** Contents:'
213 rpm -q -l -p "$1"
214 else echo "rpm isn't available, no query on rpm package possible"; fi ;;
215
216 *.snap)
217 if [ -x "`which snap`" ]; then snap info "$1"; fi
218 if [ -x "`which unsquashfs`" ]; then
219 echo
220 echo '*** Contents:'; unsquashfs -ll -d '' "$1"
221 else echo "No unsquashfs available"; fi ;;
222
223 *.tar.gz|*.tgz|*.tar.z|*.tar.dz)
224 tar tzvf "$1" --force-local
225 ;;
226
227 *.tar.xz|*.txz)
228 if [ -x "`which xz`" ]; then
229 xz -dc "$1" | tar tfvv -
230 else
231 echo "No xz available"
232 fi
233 ;;
234
235 *.tar.zst|*.tzst)
236 if [ -x "`which zstd`" ]; then
237 zstd -qdc "$1" | tar tfvv -
238 else
239 echo "No zstd available"
240 fi
241 ;;
242
243 *.whl)
244 if [ -x "`which unzip`" ]; then
245 unzip -p "$1" '*.dist-info/METADATA' | sed '/^$/q'
246 unzip -v "$1"
247 else
248 echo "No unzip available"
249 fi
250 ;;
251
252 *.xz)
253 if [ -x "`which xz`" ]; then
254 xz -dc "$1"
255 else
256 echo "No xz available"
257 fi
258 ;;
259
260 *.zst)
261 if [ -x "`which zstd`" ]; then
262 zstd -qdc "$1"
263 else
264 echo "No zstd available"
265 fi
266 ;;
267
268 # Note that this is out of alpha order so that we don't catch
269 # the gzipped tar files.
270 *.gz|*.z|*.dz)
271 gzip -dc "$1" ;;
272
273 *.tar)
274 tar tvf "$1" --force-local
275 ;;
276
277 *.jar|*.war|*.ear|*.xpi|*.zip)
278 if [ -x "`which unzip`" ]; then unzip -v "$1";
279 elif [ -x "`which miniunzip`" ]; then miniunzip -l "$1";
280 elif [ -x "`which miniunz`" ]; then miniunz -l "$1";
281 else echo "No unzip, miniunzip or miniunz available"; fi ;;
282
283 *.7z)
284 if [ -x "`which 7za`" ]; then 7za l "$1";
285 elif [ -x "`which 7zr`" ]; then 7zr l "$1";
286 else echo "No 7za or 7zr available"; fi ;;
287
288 *.zoo)
289 if [ -x "`which zoo`" ]; then zoo v "$1";
290 elif [ -x "`which unzoo`" ]; then unzoo -l "$1";
291 else echo "No unzoo or zoo available"; fi ;;
292
293 esac
294 ) 2>/dev/null
295
296 if [ $BASENAME = $LESSFILE ]; then
297 if [ -s $TMPFILE ]; then
298 echo $TMPFILE
299 else
300 rm -f $TMPFILE
301 fi
302 fi
303
304 elif [ $# -eq 2 ] ; then
305 #
306 # we were called as LESSCLOSE
307 # delete the file created if we were lessfile
308 #
309 if [ $BASENAME = $LESSFILE ]; then
310 if [ -n "$BASH" ]; then
311 if [ ! -O "$2" ]; then
312 echo "Error in deleting $2" > /dev/tty
313 exit 1
314 fi
315 fi
316
317 if [ -f "$2" ]; then
318 rm -f "$2"
319 else
320 echo "Error in deleting $2" > /dev/tty
321 fi
322 fi
323
324 elif [ $# -eq 0 ] ; then
325 #
326 # must setup shell to use LESSOPEN/LESSCLOSE
327 #
328 # I have no idea how some of the more esoteric shells (es, rc) do
329 # things. If they don't do things in a Bourne manner, send me a patch
330 # and I'll incorporate it.
331 #
332
333 # first determine the full path of lessfile/lesspipe
334 # if you can determine a better way to do this, send me a patch, I've
335 # not shell-scripted for many a year.
336 FULLPATH=`cd \`dirname $0\`;pwd`/$BASENAME
337
338 case "$SHELL" in
339 *csh)
340 if [ $BASENAME = $LESSFILE ]; then
341 echo "setenv LESSOPEN \"$FULLPATH %s\";"
342 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
343 else
344 echo "setenv LESSOPEN \"| $FULLPATH %s\";"
345 echo "setenv LESSCLOSE \"$FULLPATH %s %s\";"
346 fi
347 ;;
348 *)
349 if [ $BASENAME = $LESSFILE ]; then
350 echo "export LESSOPEN=\"$FULLPATH %s\";"
351 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
352 else
353 echo "export LESSOPEN=\"| $FULLPATH %s\";"
354 echo "export LESSCLOSE=\"$FULLPATH %s %s\";"
355 fi
356 ;;
357 esac
358
359 #echo "# If you tried to view a file with a name that starts with '#', you"
360 #echo "# might see this message instead of the file's contents."
361 #echo "# To view the contents, try to put './' ahead of the filename when"
362 #echo "# calling less."
363
364 else
365 echo "Usage: eval \`$BASENAME\`"
366 exit
367 fi