/[gentoo-x86]/sys-apps/less/files/lesspipe.sh
Gentoo

Contents of /sys-apps/less/files/lesspipe.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.54 - (show annotations) (download) (as text)
Tue Dec 31 02:25:30 2013 UTC (4 years, 11 months ago) by vapier
Branch: MAIN
CVS Tags: HEAD
Changes since 1.53: +2 -2 lines
File MIME type: text/x-sh
Fallback to rpm2tar when rpm is not available

(Portage version: 2.2.7/cvs/Linux x86_64, signed Manifest commit with key FB7C4156)

1 #!/bin/bash
2 #
3 # Preprocessor for 'less'. Used when this environment variable is set:
4 # LESSOPEN="|lesspipe %s"
5
6 # TODO: handle compressed files better
7
8 [[ -n ${LESSDEBUG} ]] && set -x
9
10 trap 'exit 0' PIPE
11
12 guesscompress() {
13 case "$1" in
14 *.gz|*.z) echo "gunzip -c" ;;
15 *.bz2|*.bz) echo "bunzip2 -c" ;;
16 *.lz) echo "lzip -dc" ;;
17 *.lzma) echo "unlzma -c" ;;
18 *.lzo) echo "lzop -dc" ;;
19 *.xz) echo "xzdec" ;;
20 *) echo "cat" ;;
21 esac
22 }
23
24 lesspipe_file() {
25 local out=$(file -L -- "$1")
26 local suffix
27 case ${out} in
28 *" 7-zip archive"*) suffix="7z";;
29 *" ar archive"*) suffix="a";;
30 *" CAB-Installer"*) suffix="cab";;
31 *" cpio archive"*) suffix="cpio";;
32 *" ELF "*) suffix="elf";;
33 *" LHa"*archive*) suffix="lha";;
34 *" troff "*) suffix="man";;
35 *" script text"*) suffix="sh";;
36 *" shared object"*) suffix="so";;
37 *" tar archive"*) suffix="tar";;
38 *" Zip archive"*) suffix="zip";;
39 *": data") hexdump -C -- "$1"; return 0;;
40 *) return 1;;
41 esac
42 lesspipe "$1" ".${suffix}"
43 return 0
44 }
45
46 lesspipe() {
47 local match=$2
48 [[ -z ${match} ]] && match=$1
49
50 local DECOMPRESSOR=$(guesscompress "${match}")
51
52 # User filters
53 if [[ -x ~/.lessfilter ]] ; then
54 ~/.lessfilter "$1" && exit 0
55 fi
56
57 local ignore
58 for ignore in ${LESSIGNORE} ; do
59 [[ ${match} == *.${ignore} ]] && exit 0
60 done
61
62 case "${match}" in
63
64 ### Doc files ###
65 *.[0-9n]|*.man|\
66 *.[0-9n].bz2|*.man.bz2|\
67 *.[0-9n].gz|*.man.gz|\
68 *.[0-9n].lzma|*.man.lzma|\
69 *.[0-9n].xz|*.man.xz|\
70 *.[0-9][a-z].gz|*.[0-9][a-z].gz)
71 local out=$(${DECOMPRESSOR} -- "$1" | file -)
72 case ${out} in
73 *troff*)
74 # Need to make sure we pass path to man or it will try
75 # to locate "$1" in the man search paths
76 if [[ $1 == /* ]] ; then
77 man -- "$1"
78 else
79 man -- "./$1"
80 fi
81 ;;
82 *text*)
83 ${DECOMPRESSOR} -- "$1"
84 ;;
85 *)
86 # We could have matched a library (libc.so.6), so let
87 # `file` figure out what the hell this thing is
88 lesspipe_file "$1"
89 ;;
90 esac
91 ;;
92 *.dvi) dvi2tty "$1" ;;
93 *.ps|*.pdf) ps2ascii "$1" || pstotext "$1" || pdftotext "$1" ;;
94 *.doc) antiword "$1" || catdoc "$1" ;;
95 *.rtf) unrtf --nopict --text "$1" ;;
96 *.conf|*.txt|*.log) ;; # force less to work on these directly #150256
97
98 ### URLs ###
99 ftp://*|http://*|*.htm|*.html)
100 for b in links2 links lynx ; do
101 ${b} -dump "$1" && exit 0
102 done
103 html2text -style pretty "$1"
104 ;;
105
106 ### Tar files ###
107 *.tar|\
108 *.tar.bz2|*.tar.bz|*.tar.gz|*.tar.z|\
109 *.tar.lz|*.tar.tlz|\
110 *.tar.lzma|*.tar.xz)
111 ${DECOMPRESSOR} -- "$1" | tar tvvf -;;
112 *.tbz2|*.tbz|*.tgz|*.tlz|*.txz)
113 lesspipe "$1" "$1".tar.${1##*.t} ;;
114
115 ### Misc archives ###
116 *.bz2|\
117 *.gz|*.z|\
118 *.lz|\
119 *.lzma|*.xz) ${DECOMPRESSOR} -- "$1" ;;
120 *.rpm) rpm -qpivl --changelog -- "$1" || rpm2tar -O "$1" | tar tvvf -;;
121 *.cpi|*.cpio) cpio -itv < "$1" ;;
122 *.ace) unace l "$1" ;;
123 *.arc) arc v "$1" ;;
124 *.arj) unarj l -- "$1" ;;
125 *.cab) cabextract -l -- "$1" ;;
126 *.lha|*.lzh) lha v "$1" ;;
127 *.zoo) zoo -list "$1" || unzoo -l "$1" ;;
128 *.7z|*.exe) 7z l -- "$1" || 7za l -- "$1" || 7zr l -- "$1" ;;
129 *.a) ar tv "$1" ;;
130 *.elf) readelf -a -W -- "$1" ;;
131 *.so) readelf -h -d -s -W -- "$1" ;;
132 *.mo|*.gmo) msgunfmt -- "$1" ;;
133
134 *.rar|.r[0-9][0-9]) unrar l -- "$1" ;;
135
136 *.jar|*.war|*.ear|*.xpi|*.zip)
137 unzip -v "$1" || miniunzip -l "$1" || miniunz -l "$1" || zipinfo -v "$1"
138 ;;
139
140 *.deb|*.udeb)
141 if type -P dpkg > /dev/null ; then
142 dpkg --info "$1"
143 dpkg --contents "$1"
144 else
145 ar tv "$1"
146 ar p "$1" data.tar.gz | tar tzvvf -
147 fi
148 ;;
149
150 ### Filesystems ###
151 *.squashfs) unsquashfs -s "$1" && unsquashfs -ll "$1" ;;
152
153 ### Media ###
154 *.bmp|*.gif|*.jpeg|*.jpg|*.ico|*.pcd|*.pcx|*.png|*.ppm|*.tga|*.tiff|*.tif|*.webp)
155 identify "$1" || file -L -- "$1"
156 ;;
157 *.asf|*.avi|*.mov|*.mp4|*.mpeg|*.mpg|*.qt|*.ram|*.rm|*.webm|*.wmv)
158 midentify "$1" || file -L -- "$1"
159 ;;
160 *.mp3) mp3info "$1" || id3info "$1" ;;
161 *.ogg) ogginfo "$1" ;;
162 *.flac) metaflac --list "$1" ;;
163 *.torrent) torrentinfo "$1" || torrentinfo-console "$1" || ctorrent -x "$1" ;;
164 *.bin|*.cue|*.raw)
165 # not all .bin/.raw files are cd images #285507
166 # fall back to lesspipe_file if .cue doesn't exist, or if
167 # cd-info failed to parse things sanely
168 [[ -e ${1%.*}.cue ]] \
169 && cd-info --no-header --no-device-info "$1" \
170 || lesspipe_file "$1"
171 ;;
172 *.iso)
173 iso_info=$(isoinfo -d -i "$1")
174 echo "${iso_info}"
175 # Joliet output overrides Rock Ridge, so prefer the better Rock
176 case ${iso_info} in
177 *$'\n'"Rock Ridge"*) iso_opts="-R";;
178 *$'\n'"Joliet"*) iso_opts="-J";;
179 *) iso_opts="";;
180 esac
181 isoinfo -l ${iso_opts} -i "$1"
182 ;;
183
184 ### Encryption stuff ###
185 *.crl) openssl crl -hash -text -noout -in "$1" ;;
186 *.csr) openssl req -text -noout -in "$1" ;;
187 *.crt|*.pem) openssl x509 -hash -text -noout -in "$1" ;;
188
189 # May not be such a good idea :)
190 # ### Device nodes ###
191 # /dev/[hs]d[a-z]*)
192 # fdisk -l "${1:0:8}"
193 # [[ $1 == *hd* ]] && hdparm -I "${1:0:8}"
194 # ;;
195
196 ### Everything else ###
197 *)
198 case $(( recur++ )) in
199 # Maybe we didn't match due to case issues ...
200 0) lesspipe "$1" "$(echo $1 | LC_ALL=C tr '[:upper:]' '[:lower:]')" ;;
201
202 # Maybe we didn't match because the file is named weird ...
203 1) lesspipe_file "$1" ;;
204 esac
205
206 # So no matches from above ... finally fall back to an external
207 # coloring package. No matching here so we don't have to worry
208 # about keeping in sync with random packages. Any coloring tool
209 # you use should not output errors about unsupported files to
210 # stdout. If it does, it's your problem.
211
212 # Allow people to flip color off if they dont want it
213 case ${LESSCOLOR} in
214 always) LESSCOLOR=2;;
215 [yY][eE][sS]|[yY]|1|true) LESSCOLOR=1;;
216 [nN][oO]|[nN]|0|false) LESSCOLOR=0;;
217 *) LESSCOLOR=0;; # default to no color #188835
218 esac
219 if [[ ${LESSCOLOR} != "0" ]] && [[ -n ${LESSCOLORIZER=code2color} ]] ; then
220 # 2: Only colorize if user forces it ...
221 # 1: ... or we know less will handle raw codes -- this will
222 # not detect -seiRM, so set LESSCOLORIZER yourself
223 if [[ ${LESSCOLOR} == "2" ]] || [[ " ${LESS} " == *" -"[rR]" "* ]] ; then
224 LESSQUIET=true ${LESSCOLORIZER} "$1"
225 fi
226 fi
227
228 # Nothing left to do but let less deal
229 exit 0
230 ;;
231 esac
232 }
233
234 if [[ -z $1 ]] ; then
235 echo "Usage: lesspipe <file>"
236 elif [[ $1 == "-V" || $1 == "--version" ]] ; then
237 Id="cvsid"
238 cat <<-EOF
239 $Id: lesspipe.sh,v 1.53 2013/08/06 21:35:30 vapier Exp $
240 Copyright 2001-2013 Gentoo Foundation
241 Mike Frysinger <vapier@gentoo.org>
242 (with plenty of ideas stolen from other projects/distros)
243
244
245 EOF
246 less -V
247 elif [[ $1 == "-h" || $1 == "--help" ]] ; then
248 cat <<-EOF
249 lesspipe: preprocess files before sending them to less
250
251 Usage: lesspipe <file>
252
253 lesspipe specific settings:
254 LESSCOLOR env - toggle colorizing of output (no/yes/always)
255 LESSCOLORIZER env - program used to colorize output (default: code2color)
256 LESSIGNORE - list of extensions to ignore (don't do anything fancy)
257
258 You can create per-user filters as well by creating the executable file:
259 ~/.lessfilter
260 One argument is passed to it: the file to display.
261
262 To use lesspipe, simply add to your environment:
263 export LESSOPEN="|lesspipe %s"
264
265 Run 'less --help' or 'man less' for more info
266 EOF
267 elif [[ -d $1 ]] ; then
268 ls -alF -- "$1"
269 else
270 recur=0
271 [[ -n ${LESSDEBUG} ]] \
272 && lesspipe "$1" \
273 || lesspipe "$1" 2> /dev/null
274 fi

  ViewVC Help
Powered by ViewVC 1.1.20