3 ## lessfile/lesspipe filter for less pager
4 ## Copyright (c) 2006,2007 by Michal Nazarewicz (mina86/AT/mina86.com)
6 ## Written by: Behan Webster <behanw@pobox.com>
8 ## Thomas Schoepf <schoepf@debian.org>
10 ## and Michal Nazarewicz <mina86/AT/mina86.com>
12 ## This program is free software; you can redistribute it and/or modify
13 ## it under the terms of the GNU General Public License as published by
14 ## the Free Software Foundation; either version 3 of the License, or
15 ## (at your option) any later version.
17 ## This program is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
22 ## You should have received a copy of the GNU General Public License
23 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
25 ## This is part of Tiny Applications Collection
26 ## -> http://tinyapps.sourceforge.net/
30 ## See lesspipe --help for usage.
39 printf %s
"$*" |
sed -e "s/'/'\\\\''/g"
48 if [ $# -eq 0 ] ||
[ X
"$1" = X--install-lessopen
] || \
49 [ X
"$1" = X--install-lessfile
] ||
[ X
"$1" = X--install-lesspipe
]; then
50 FULLPATH
="$(cd "$
(dirname "$0")"; pwd)/${0##*/}"
51 FULLPATH
="$(esc "'$(esc "$FULLPATH")'")"
54 if [ X
"$1" = X--install-lessopen
] ||
[ X
"$1" = X--install-lessfile
]
56 elif [ X
"$1" != X--install-lesspipe
] && [ X
"${0##*/}" = Xlessfile
]
61 *csh
) _SET
=setenv
; _UNSET
=unsetenv
; _EQ
=' ';;
62 *) _SET
=export; _UNSET
=unset; _EQ
='=';;
65 if [ -n "$LESSFILE" ]; then
66 printf "$_SET LESSOPEN$_EQ'%s';\\n$_SET LESSCLOSE$_EQ'%s';\\n" \
67 "$FULLPATH --open %s" "$FULLPATH --close %s %s"
69 printf "$_SET LESSOPEN$_EQ'%s';\\n$_UNSET LESSCLOSE;\\n" \
70 "|$FULLPATH --pipe %s"
79 ## Called as LESSCLOSE
81 if [ $# -eq 3 ] && [ X
"$1" = X--close
]; then
83 if [ -n "$BASH" ]; then
87 echo "Error in deleting $2" >&2
93 echo "Error in deleting $2" >&2
104 if [ $# -ne 2 ] ||
( [ X
"$1" != X--open
] && [ X
"$1" != X--pipe
] ); then
105 FULLPATH
="$(cd "$
(dirname "$0")"; pwd)/${0##*/}"
106 FULLPATH
="'$(esc "$FULLPATH")'"
109 usage: eval "\$($FULLPATH [ <option> ])"
111 --install-lesspipe installs input processor as lesspipe
112 --install-lessopen installs input processor as lessopwn
113 --install-lessfile synonym of --install-lessopen
114 To enable cache when downloading files create ~/.lesscache directory.
116 [ X
"$1" = X--help
] ||
[ X
"$1" = X-h
]
123 ## We were called as LESSOPEN
126 LESSFILE
=; [ X
"$1" = X--pipe
] || LESSFILE
=y
130 ## Helper function to list contents of ISO files (CD images)
132 set -- "$1" "$(isoinfo -d -i "$1")"
133 printf %s
"$2" |
grep ^Rock\.Ridge
>/dev
/null
&& set -- "$1" "$2" "$3 -R"
134 printf %s
"$2" |
grep ^Joliet
>/dev
/null
&& set -- "$1" "$2" "$3 -J"
136 isoinfo
-f $3 -i "$1"
140 ## Creates temporary file and echos it's path
142 set -- "${1-less-}" "$(umask)" .
145 for __TMP
in "$TMPDIR" "$TMP" "$TEMP" ~
/tmp
/tmp
; do
146 if [ -n "$__TMP" ] && [ -d "$__TMP" ] && [ -w "$__TMP" ]; then
147 set -- "$1" "$2" "$__TMP"
153 if ! which mktemp
>/dev
/null
2>&1 && ! which tempfile
>/dev
/null
2>&1
155 echo unable to create temporary
file, \
156 mktemp or tempfile required
>&2
161 if which mktemp
>/dev
/null
2>&1; then
162 mktemp
"$3/$1.XXXXXXXX"
164 tempfile
-d "$3" -p "$1"
170 ## Check if application is available and executes command or prints
171 ## error and other stuff
173 if [ X
"$1" = X--
]; then
174 which "$2" >/dev
/null
2>&1
178 if ! which "$1" >/dev
/null
2>&1; then
179 printf 'No %s available.\n' "$1"
183 if [ $# -eq 1 ]; then
193 if [ X
"$3" = Xdownloaded
]; then
194 [ X
"$5" = Xcached
] ||
rm -f -- "$1"
207 trap 'exit_function "$@"' 0
211 if [ -n "$LESSFILE" ]; then
212 set -- "$1" "$(tmpfile lessf-)"
220 ## User has his own fitler
221 [ -x ~
/.lessfilter
] && ~
/.lessfilter
"$1" && exit 0
225 if ! [ -e "$1" ] && [ X
"${1#*://}" != X
"$1" ]; then
226 which wget
>/dev
/null
2>&1 ||
which curl
>/dev
/null
2>&1 ||
exit 0
227 FILENAME
="${FILENAME%%\?*}"
230 if which md5sum >/dev
/null
2>&1 && [ -d ~
/.lesscache
]; then
231 CACHE
=$
(printf %s
"$3" |
md5sum | cut
-d\
-f1)
232 if [ -n "$(find "$HOME/.lesscache
" -maxdepth 1 -type f \
233 -name "$CACHE" -mtime -7)" ]; then
236 set -- "$HOME/.lesscache/$CACHE" "$2" downloaded
"$1" cached
238 set -- "$(tmpfile lessg-)" "$2" downloaded
"$1"
242 if [ -z "$GET" ]; then
244 elif which wget
>/dev
/null
2>&1; then
245 wget
--no-check-certificate -S -o "$1-err" -O "$1" -- "$4" || ERR
=y
247 curl
-o "$1" -s -- "$4" 2>"$1-err" || ERR
=y
250 if [ -n "$ERR" ]; then
251 echo "Could not downlaod $4"
260 ## File is not readable
261 [ -r "$1" ] ||
exit 0
264 ## Show contetn of directories
266 /bin
/ls -l -- "$1" 2>&1
272 FILENAME
="$(printf %s "$FILENAME" | tr QWERTYUIOPASDFGHJKLZXCVBNM qwertyuiopasdfghjklzxcvbnm)"
275 *.gz
) DECOMPRESSOR
=gzip ; FILENAME
=c
${FILENAME%.*} ;;
276 *.tgz
) DECOMPRESSOR
=gzip ; FILENAME
=c
${FILENAME%.*}.
tar;;
277 *.bz2
) DECOMPRESSOR
=bzip2; FILENAME
=c
${FILENAME%.*} ;;
278 *.tbz
) DECOMPRESSOR
=bzip2; FILENAME
=c
${FILENAME%.*}.
tar;;
279 *.xz
) DECOMPRESSOR
=xz
; FILENAME
=c
${FILENAME%.*} ;;
280 *.txz
) DECOMPRESSOR
=xz
; FILENAME
=c
${FILENAME%.*}.
tar;;
281 *.lzma
) DECOMPRESSOR
=lzma
; FILENAME
=c
${FILENAME%.*} ;;
282 *) FILENAME
=u
$FILENAME
285 if [ x
"$DECOMPRESSOR" != xcat
]; then
286 run
$DECOMPRESSOR ||
exit 0
287 DECOMPRESSOR
="$DECOMPRESSOR -d"
294 if run nroff
; then $DECOMPRESSOR <"$1" | nroff
-S -mandoc -; fi
297 if run
tar ; then $DECOMPRESSOR <"$1" |
tar tvv
; fi
302 u
*.arj
) run unarj l
"$1" ;;
303 u
*.lha|u
*.lzh
) run lha v
"$1" ;;
304 u
*.cab
) run cabextract
-l "$1" ;;
306 u
*.rar|u
*.r
[0-9][0-9])
307 if run
-- rar
; then rar v
"$1"
308 elif run
-- unrar
; then unrar v
"$1"
309 else echo "No rar or unrar available"
313 u
*.jar|u
*.war|u
*.xpi|u
*.
zip)
314 if run
-- unzip; then unzip -v "$1"
315 elif run
-- miniunzip
; then miniunzip
-l "$1"
316 elif run
-- miniunz
; then miniunz
-l "$1"
317 else echo "No unzip, miniunzip or miniunz available"
322 if run
-- zoo
; then zoo v
"$1"
323 elif run
-- unzoo
; then unzoo
-l "$1"
324 else echo "No unzoo or zoo available"
330 u
*.iso
) run isoinfo
&& iso_list
"$1" ;;
333 if run
file && run isoinfo
&& file "$1" |
grep -F ISO\
.9660 >/dev
/null
;
334 then iso_list
"$1"; fi
338 ## Binary software packages
345 dpkg-deb
--contents "$1"
356 else echo "rpm isn't available, no query on rpm package possible"; fi
360 ## Documents, images, etc
361 u
*.doc
) run catdoc
"$1" ;;
362 u
*.dvi
) run dvi2tty
"$1" ;;
363 *.ps
) if run ps2ascii
; then $DECOMPRESSOR <"$1" | run ps2ascii
; fi ;;
364 u
*.pdf
) run pdftotext
"$1" - ;;
366 u
*.gif|u
*.jpeg|u
*.jpg|u
*.pcd|u
*.png|u
*.tga|u
*.tiff|u
*.tif
)
367 run identify
-verbose "$1"
371 ## Compressed file needs decompression
376 ## If file was downloaded it needs to be cated
378 if [ X
"$3" != Xdownloaded
]; then
380 elif [ -z "$2" ]; then
382 elif [ X
"$5" = Xcached
]; then