-
Notifications
You must be signed in to change notification settings - Fork 2
/
lesspipe
executable file
·388 lines (319 loc) · 8.19 KB
/
lesspipe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/sh
##
## lessfile/lesspipe filter for less pager
## Copyright (c) 2006,2007 by Michal Nazarewicz (mina86/AT/mina86.com)
##
## Written by: Behan Webster <[email protected]>
## Darren Stalder
## Thomas Schoepf <[email protected]>
## Y.Dirson
## and Michal Nazarewicz <mina86/AT/mina86.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
##
## See lesspipe --help for usage.
##
##
## Escape string
##
esc () {
if [ -n "$*" ]; then
printf %s "$*" | sed -e "s/'/'\\\\''/g"
fi
}
##
## No arguments
##
if [ $# -eq 0 ] || [ X"$1" = X--install-lessopen ] || \
[ X"$1" = X--install-lessfile ] || [ X"$1" = X--install-lesspipe ]; then
FULLPATH="$(cd "$(dirname "$0")"; pwd)/${0##*/}"
FULLPATH="$(esc "'$(esc "$FULLPATH")'")"
LESSFILE=
if [ X"$1" = X--install-lessopen ] || [ X"$1" = X--install-lessfile ]
then LESSFILE=y
elif [ X"$1" != X--install-lesspipe ] && [ X"${0##*/}" = Xlessfile ]
then LESSFILE=y
fi
case "$SHELL" in
*csh) _SET=setenv; _UNSET=unsetenv; _EQ=' ';;
*) _SET=export; _UNSET=unset; _EQ='=';;
esac
if [ -n "$LESSFILE" ]; then
printf "$_SET LESSOPEN$_EQ'%s';\\n$_SET LESSCLOSE$_EQ'%s';\\n" \
"$FULLPATH --open %s" "$FULLPATH --close %s %s"
else
printf "$_SET LESSOPEN$_EQ'%s';\\n$_UNSET LESSCLOSE;\\n" \
"|$FULLPATH --pipe %s"
fi
exit 0
fi
##
## Called as LESSCLOSE
##
if [ $# -eq 3 ] && [ X"$1" = X--close ]; then
shift
if [ -n "$BASH" ]; then
if [ -O "$2" ]; then
rm -f -- "$2"
else
echo "Error in deleting $2" >&2
fi
else
if [ -f "$2" ]; then
rm -f -- "$2"
else
echo "Error in deleting $2" >&2
fi
fi
exit 0
fi
##
## Ugh?
##
if [ $# -ne 2 ] || ( [ X"$1" != X--open ] && [ X"$1" != X--pipe ] ); then
FULLPATH="$(cd "$(dirname "$0")"; pwd)/${0##*/}"
FULLPATH="'$(esc "$FULLPATH")'"
cat <<EOF
usage: eval "\$($FULLPATH [ <option> ])"
<option> can be:
--install-lesspipe installs input processor as lesspipe
--install-lessopen installs input processor as lessopwn
--install-lessfile synonym of --install-lessopen
To enable cache when downloading files create ~/.lesscache directory.
EOF
[ X"$1" = X--help ] || [ X"$1" = X-h ]
exit $?
fi
##
## We were called as LESSOPEN
##
set -e
LESSFILE=; [ X"$1" = X--pipe ] || LESSFILE=y
shift
## Helper function to list contents of ISO files (CD images)
iso_list() {
set -- "$1" "$(isoinfo -d -i "$1")"
printf %s "$2" | grep ^Rock\.Ridge >/dev/null && set -- "$1" "$2" "$3 -R"
printf %s "$2" | grep ^Joliet >/dev/null && set -- "$1" "$2" "$3 -J"
printf %s\n "$2"
isoinfo -f $3 -i "$1"
}
## Creates temporary file and echos it's path
tmpfile () {
set -- "${1-less-}" "$(umask)" .
# Find temp dir
for __TMP in "$TMPDIR" "$TMP" "$TEMP" ~/tmp /tmp; do
if [ -n "$__TMP" ] && [ -d "$__TMP" ] && [ -w "$__TMP" ]; then
set -- "$1" "$2" "$__TMP"
break
fi
done
# tempfile exists
if ! which mktemp >/dev/null 2>&1 && ! which tempfile >/dev/null 2>&1
then
echo unable to create temporary file, \
mktemp or tempfile required >&2
exit 1
fi
umask 077
if which mktemp >/dev/null 2>&1; then
mktemp "$3/$1.XXXXXXXX"
else
tempfile -d "$3" -p "$1"
fi
umask "$2"
}
## Check if application is available and executes command or prints
## error and other stuff
run () {
if [ X"$1" = X-- ]; then
which "$2" >/dev/null 2>&1
return $?
fi
if ! which "$1" >/dev/null 2>&1; then
printf 'No %s available.\n' "$1"
return 1
fi
if [ $# -eq 1 ]; then
return 0
else
"$@"
fi
}
## Exit function
exit_function () {
if [ X"$3" = Xdownloaded ]; then
[ X"$5" = Xcached ] || rm -f -- "$1"
rm -f -- "$1-err"
fi
if [ -n "$2" ]; then
if [ -s "$2" ]; then
printf %s "$2" >&3
else
rm -f -- "$3"
fi
fi
}
trap 'exit_function "$@"' 0
## Redirect output
if [ -n "$LESSFILE" ]; then
set -- "$1" "$(tmpfile lessf-)"
exec 3>&1 >"$2"
else
set -- "$1" ''
fi
#exec 2>/dev/null
## User has his own fitler
[ -x ~/.lessfilter ] && ~/.lessfilter "$1" && exit 0
## URL
FILENAME="${1##*/}"
if ! [ -e "$1" ] && [ X"${1#*://}" != X"$1" ]; then
which wget >/dev/null 2>&1 || which curl >/dev/null 2>&1 || exit 0
FILENAME="${FILENAME%%\?*}"
CACHE=; GET=y
if which md5sum >/dev/null 2>&1 && [ -d ~/.lesscache ]; then
CACHE=$(printf %s "$3" | md5sum | cut -d\ -f1)
if [ -n "$(find "$HOME/.lesscache" -maxdepth 1 -type f \
-name "$CACHE" -mtime -7)" ]; then
GET=
fi
set -- "$HOME/.lesscache/$CACHE" "$2" downloaded "$1" cached
else
set -- "$(tmpfile lessg-)" "$2" downloaded "$1"
fi
ERR=
if [ -z "$GET" ]; then
:
elif which wget >/dev/null 2>&1; then
wget --no-check-certificate -S -o "$1-err" -O "$1" -- "$4" || ERR=y
else
curl -o "$1" -s -- "$4" 2>"$1-err" || ERR=y
fi
if [ -n "$ERR" ]; then
echo "Could not downlaod $4"
echo
cat -- "$1-err"
rm -f -- "$1"
exit 0
fi
fi
## File is not readable
[ -r "$1" ] || exit 0
## Show contetn of directories
if [ -d "$1" ]; then
/bin/ls -l -- "$1" 2>&1
exit 0
fi
## Compressed?
FILENAME="$(printf %s "$FILENAME" | tr QWERTYUIOPASDFGHJKLZXCVBNM qwertyuiopasdfghjklzxcvbnm)"
DECOMPRESSOR=cat
case "$FILENAME" in
*.gz) DECOMPRESSOR=gzip ; FILENAME=c${FILENAME%.*} ;;
*.tgz) DECOMPRESSOR=gzip ; FILENAME=c${FILENAME%.*}.tar;;
*.bz2) DECOMPRESSOR=bzip2; FILENAME=c${FILENAME%.*} ;;
*.tbz) DECOMPRESSOR=bzip2; FILENAME=c${FILENAME%.*}.tar;;
*.xz) DECOMPRESSOR=xz ; FILENAME=c${FILENAME%.*} ;;
*.txz) DECOMPRESSOR=xz ; FILENAME=c${FILENAME%.*}.tar;;
*.lzma) DECOMPRESSOR=lzma ; FILENAME=c${FILENAME%.*} ;;
*) FILENAME=u$FILENAME
esac
if [ x"$DECOMPRESSOR" != xcat ]; then
run $DECOMPRESSOR || exit 0
DECOMPRESSOR="$DECOMPRESSOR -d"
fi
# Filter
case "$FILENAME" in
*.[1-9n]|*.man)
if run nroff; then $DECOMPRESSOR <"$1" | nroff -S -mandoc -; fi
;;
*.tar)
if run tar ; then $DECOMPRESSOR <"$1" | tar tvv; fi
;;
# Other archives
u*.arj) run unarj l "$1" ;;
u*.lha|u*.lzh) run lha v "$1" ;;
u*.cab) run cabextract -l "$1" ;;
u*.rar|u*.r[0-9][0-9])
if run -- rar; then rar v "$1"
elif run -- unrar; then unrar v "$1"
else echo "No rar or unrar available"
fi
;;
u*.jar|u*.war|u*.xpi|u*.zip)
if run -- unzip; then unzip -v "$1"
elif run -- miniunzip; then miniunzip -l "$1"
elif run -- miniunz ; then miniunz -l "$1"
else echo "No unzip, miniunzip or miniunz available"
fi
;;
u*.zoo)
if run -- zoo; then zoo v "$1"
elif run -- unzoo; then unzoo -l "$1"
else echo "No unzoo or zoo available"
fi
;;
## CD Images
u*.iso) run isoinfo && iso_list "$1" ;;
u*.bin|u*.raw)
if run file && run isoinfo && file "$1" | grep -F ISO\.9660 >/dev/null;
then iso_list "$1"; fi
;;
## Binary software packages
u*.deb|u*.udeb)
if run dpkg; then
echo "$1:"
dpkg --info "$1"
echo
echo '*** Contents:'
dpkg-deb --contents "$1"
fi
;;
u*.rpm)
if run rpm; then
echo "$1:"
rpm -q -i -p "$1"
echo
echo '*** Contents:'
rpm -q -l -p "$1"
else echo "rpm isn't available, no query on rpm package possible"; fi
;;
## Documents, images, etc
u*.doc) run catdoc "$1" ;;
u*.dvi) run dvi2tty "$1" ;;
*.ps) if run ps2ascii; then $DECOMPRESSOR <"$1" | run ps2ascii; fi ;;
u*.pdf) run pdftotext "$1" - ;;
u*.gif|u*.jpeg|u*.jpg|u*.pcd|u*.png|u*.tga|u*.tiff|u*.tif)
run identify -verbose "$1"
;;
## Compressed file needs decompression
c*)
$DECOMPRESSOR <"$1"
;;
## If file was downloaded it needs to be cated
*)
if [ X"$3" != Xdownloaded ]; then
:
elif [ -z "$2" ]; then
cat -- "$1"
elif [ X"$5" = Xcached ]; then
cp -f -- "$1" "$2"
else
mv -f -- "$1" "$2"
fi
;;
esac