[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / lesspipe
blobdef32fd002385e889fe3e4e55cceacb420046332
1 #!/bin/sh
2 ##
3 ## lessfile/lesspipe filter for less pager
4 ## Copyright (c) 2006,2007 by Michal Nazarewicz (mina86/AT/mina86.com)
5 ##
6 ## Written by: Behan Webster <behanw@pobox.com>
7 ## Darren Stalder
8 ## Thomas Schoepf <schoepf@debian.org>
9 ## Y.Dirson
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.
35 ## Escape string
37 esc () {
38 if [ -n "$*" ]; then
39 printf %s "$*" | sed -e "s/'/'\\\\''/g"
46 ## No arguments
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")'")"
53 LESSFILE=
54 if [ X"$1" = X--install-lessopen ] || [ X"$1" = X--install-lessfile ]
55 then LESSFILE=y
56 elif [ X"$1" != X--install-lesspipe ] && [ X"${0##*/}" = Xlessfile ]
57 then LESSFILE=y
60 case "$SHELL" in
61 *csh) _SET=setenv; _UNSET=unsetenv; _EQ=' ';;
62 *) _SET=export; _UNSET=unset; _EQ='=';;
63 esac
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"
68 else
69 printf "$_SET LESSOPEN$_EQ'%s';\\n$_UNSET LESSCLOSE;\\n" \
70 "|$FULLPATH --pipe %s"
73 exit 0
79 ## Called as LESSCLOSE
81 if [ $# -eq 3 ] && [ X"$1" = X--close ]; then
82 shift
83 if [ -n "$BASH" ]; then
84 if [ -O "$2" ]; then
85 rm -f -- "$2"
86 else
87 echo "Error in deleting $2" >&2
89 else
90 if [ -f "$2" ]; then
91 rm -f -- "$2"
92 else
93 echo "Error in deleting $2" >&2
96 exit 0
102 ## Ugh?
104 if [ $# -ne 2 ] || ( [ X"$1" != X--open ] && [ X"$1" != X--pipe ] ); then
105 FULLPATH="$(cd "$(dirname "$0")"; pwd)/${0##*/}"
106 FULLPATH="'$(esc "$FULLPATH")'"
108 cat <<EOF
109 usage: eval "\$($FULLPATH [ <option> ])"
110 <option> can be:
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 ]
117 exit $?
123 ## We were called as LESSOPEN
125 set -e
126 LESSFILE=; [ X"$1" = X--pipe ] || LESSFILE=y
127 shift
130 ## Helper function to list contents of ISO files (CD images)
131 iso_list() {
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"
135 printf %s\n "$2"
136 isoinfo -f $3 -i "$1"
140 ## Creates temporary file and echos it's path
141 tmpfile () {
142 set -- "${1-less-}" "$(umask)" .
144 # Find temp dir
145 for __TMP in "$TMPDIR" "$TMP" "$TEMP" ~/tmp /tmp; do
146 if [ -n "$__TMP" ] && [ -d "$__TMP" ] && [ -w "$__TMP" ]; then
147 set -- "$1" "$2" "$__TMP"
148 break
150 done
152 # tempfile exists
153 if ! which mktemp >/dev/null 2>&1 && ! which tempfile >/dev/null 2>&1
154 then
155 echo unable to create temporary file, \
156 mktemp or tempfile required >&2
157 exit 1
160 umask 077
161 if which mktemp >/dev/null 2>&1; then
162 mktemp "$3/$1.XXXXXXXX"
163 else
164 tempfile -d "$3" -p "$1"
166 umask "$2"
170 ## Check if application is available and executes command or prints
171 ## error and other stuff
172 run () {
173 if [ X"$1" = X-- ]; then
174 which "$2" >/dev/null 2>&1
175 return $?
178 if ! which "$1" >/dev/null 2>&1; then
179 printf 'No %s available.\n' "$1"
180 return 1
183 if [ $# -eq 1 ]; then
184 return 0
185 else
186 "$@"
191 ## Exit function
192 exit_function () {
193 if [ X"$3" = Xdownloaded ]; then
194 [ X"$5" = Xcached ] || rm -f -- "$1"
195 rm -f -- "$1-err"
198 if [ -n "$2" ]; then
199 if [ -s "$2" ]; then
200 printf %s "$2" >&3
201 else
202 rm -f -- "$3"
207 trap 'exit_function "$@"' 0
210 ## Redirect output
211 if [ -n "$LESSFILE" ]; then
212 set -- "$1" "$(tmpfile lessf-)"
213 exec 3>&1 >"$2"
214 else
215 set -- "$1" ''
217 #exec 2>/dev/null
220 ## User has his own fitler
221 [ -x ~/.lessfilter ] && ~/.lessfilter "$1" && exit 0
223 ## URL
224 FILENAME="${1##*/}"
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%%\?*}"
229 CACHE=; GET=y
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
234 GET=
236 set -- "$HOME/.lesscache/$CACHE" "$2" downloaded "$1" cached
237 else
238 set -- "$(tmpfile lessg-)" "$2" downloaded "$1"
241 ERR=
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
246 else
247 curl -o "$1" -s -- "$4" 2>"$1-err" || ERR=y
250 if [ -n "$ERR" ]; then
251 echo "Could not downlaod $4"
252 echo
253 cat -- "$1-err"
254 rm -f -- "$1"
255 exit 0
260 ## File is not readable
261 [ -r "$1" ] || exit 0
264 ## Show contetn of directories
265 if [ -d "$1" ]; then
266 /bin/ls -l -- "$1" 2>&1
267 exit 0
271 ## Compressed?
272 FILENAME="$(printf %s "$FILENAME" | tr QWERTYUIOPASDFGHJKLZXCVBNM qwertyuiopasdfghjklzxcvbnm)"
273 DECOMPRESSOR=cat
274 case "$FILENAME" in
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
283 esac
285 if [ x"$DECOMPRESSOR" != xcat ]; then
286 run $DECOMPRESSOR || exit 0
287 DECOMPRESSOR="$DECOMPRESSOR -d"
291 # Filter
292 case "$FILENAME" in
293 *.[1-9n]|*.man)
294 if run nroff; then $DECOMPRESSOR <"$1" | nroff -S -mandoc -; fi
296 *.tar)
297 if run tar ; then $DECOMPRESSOR <"$1" | tar tvv; fi
301 # Other archives
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"
321 u*.zoo)
322 if run -- zoo; then zoo v "$1"
323 elif run -- unzoo; then unzoo -l "$1"
324 else echo "No unzoo or zoo available"
329 ## CD Images
330 u*.iso) run isoinfo && iso_list "$1" ;;
332 u*.bin|u*.raw)
333 if run file && run isoinfo && file "$1" | grep -F ISO\.9660 >/dev/null;
334 then iso_list "$1"; fi
338 ## Binary software packages
339 u*.deb|u*.udeb)
340 if run dpkg; then
341 echo "$1:"
342 dpkg --info "$1"
343 echo
344 echo '*** Contents:'
345 dpkg-deb --contents "$1"
349 u*.rpm)
350 if run rpm; then
351 echo "$1:"
352 rpm -q -i -p "$1"
353 echo
354 echo '*** Contents:'
355 rpm -q -l -p "$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
373 $DECOMPRESSOR <"$1"
376 ## If file was downloaded it needs to be cated
378 if [ X"$3" != Xdownloaded ]; then
380 elif [ -z "$2" ]; then
381 cat -- "$1"
382 elif [ X"$5" = Xcached ]; then
383 cp -f -- "$1" "$2"
384 else
385 mv -f -- "$1" "$2"
388 esac