/[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 - (hide annotations) (download) (as text)
Tue Dec 31 02:25:30 2013 UTC (5 years 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 vapier 1.2 #!/bin/bash
2 vapier 1.12 #
3 woodchip 1.1 # Preprocessor for 'less'. Used when this environment variable is set:
4 vapier 1.43 # LESSOPEN="|lesspipe %s"
5 woodchip 1.1
6 vapier 1.9 # TODO: handle compressed files better
7    
8 vapier 1.37 [[ -n ${LESSDEBUG} ]] && set -x
9    
10 vapier 1.2 trap 'exit 0' PIPE
11    
12 vapier 1.3 guesscompress() {
13     case "$1" in
14 vapier 1.37 *.gz|*.z) echo "gunzip -c" ;;
15     *.bz2|*.bz) echo "bunzip2 -c" ;;
16 vapier 1.49 *.lz) echo "lzip -dc" ;;
17 vapier 1.37 *.lzma) echo "unlzma -c" ;;
18 vapier 1.51 *.lzo) echo "lzop -dc" ;;
19 vapier 1.37 *.xz) echo "xzdec" ;;
20     *) echo "cat" ;;
21 vapier 1.3 esac
22     }
23 woodchip 1.1
24 vapier 1.6 lesspipe_file() {
25     local out=$(file -L -- "$1")
26 vapier 1.27 local suffix
27 vapier 1.6 case ${out} in
28 vapier 1.30 *" 7-zip archive"*) suffix="7z";;
29 vapier 1.27 *" 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 vapier 1.6 esac
42 vapier 1.27 lesspipe "$1" ".${suffix}"
43 vapier 1.9 return 0
44 vapier 1.6 }
45    
46 vapier 1.3 lesspipe() {
47 vapier 1.6 local match=$2
48     [[ -z ${match} ]] && match=$1
49 vapier 1.3
50 vapier 1.51 local DECOMPRESSOR=$(guesscompress "${match}")
51 vapier 1.9
52 vapier 1.28 # User filters
53     if [[ -x ~/.lessfilter ]] ; then
54     ~/.lessfilter "$1" && exit 0
55     fi
56    
57 vapier 1.29 local ignore
58     for ignore in ${LESSIGNORE} ; do
59     [[ ${match} == *.${ignore} ]] && exit 0
60     done
61    
62 vapier 1.51 case "${match}" in
63 vapier 1.3
64 vapier 1.6 ### Doc files ###
65 vapier 1.3 *.[0-9n]|*.man|\
66     *.[0-9n].bz2|*.man.bz2|\
67 vapier 1.9 *.[0-9n].gz|*.man.gz|\
68 vapier 1.26 *.[0-9n].lzma|*.man.lzma|\
69 vapier 1.51 *.[0-9n].xz|*.man.xz|\
70 vapier 1.9 *.[0-9][a-z].gz|*.[0-9][a-z].gz)
71     local out=$(${DECOMPRESSOR} -- "$1" | file -)
72     case ${out} in
73     *troff*)
74 vapier 1.25 # Need to make sure we pass path to man or it will try
75 vapier 1.9 # 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 vapier 1.3 ;;
92 vapier 1.6 *.dvi) dvi2tty "$1" ;;
93 vapier 1.7 *.ps|*.pdf) ps2ascii "$1" || pstotext "$1" || pdftotext "$1" ;;
94     *.doc) antiword "$1" || catdoc "$1" ;;
95 vapier 1.6 *.rtf) unrtf --nopict --text "$1" ;;
96 vapier 1.19 *.conf|*.txt|*.log) ;; # force less to work on these directly #150256
97 vapier 1.6
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 vapier 1.3
106     ### Tar files ###
107 vapier 1.32 *.tar|\
108 vapier 1.40 *.tar.bz2|*.tar.bz|*.tar.gz|*.tar.z|\
109 vapier 1.32 *.tar.lz|*.tar.tlz|\
110     *.tar.lzma|*.tar.xz)
111     ${DECOMPRESSOR} -- "$1" | tar tvvf -;;
112 vapier 1.41 *.tbz2|*.tbz|*.tgz|*.tlz|*.txz)
113 vapier 1.37 lesspipe "$1" "$1".tar.${1##*.t} ;;
114 vapier 1.3
115     ### Misc archives ###
116 vapier 1.32 *.bz2|\
117     *.gz|*.z|\
118     *.lz|\
119     *.lzma|*.xz) ${DECOMPRESSOR} -- "$1" ;;
120 vapier 1.54 *.rpm) rpm -qpivl --changelog -- "$1" || rpm2tar -O "$1" | tar tvvf -;;
121 vapier 1.3 *.cpi|*.cpio) cpio -itv < "$1" ;;
122 vapier 1.16 *.ace) unace l "$1" ;;
123 vapier 1.13 *.arc) arc v "$1" ;;
124 vapier 1.3 *.arj) unarj l -- "$1" ;;
125     *.cab) cabextract -l -- "$1" ;;
126 vapier 1.7 *.lha|*.lzh) lha v "$1" ;;
127 vapier 1.28 *.zoo) zoo -list "$1" || unzoo -l "$1" ;;
128 vapier 1.47 *.7z|*.exe) 7z l -- "$1" || 7za l -- "$1" || 7zr l -- "$1" ;;
129 vapier 1.5 *.a) ar tv "$1" ;;
130 vapier 1.48 *.elf) readelf -a -W -- "$1" ;;
131     *.so) readelf -h -d -s -W -- "$1" ;;
132 vapier 1.15 *.mo|*.gmo) msgunfmt -- "$1" ;;
133 vapier 1.7
134     *.rar|.r[0-9][0-9]) unrar l -- "$1" ;;
135    
136 vapier 1.28 *.jar|*.war|*.ear|*.xpi|*.zip)
137     unzip -v "$1" || miniunzip -l "$1" || miniunz -l "$1" || zipinfo -v "$1"
138     ;;
139    
140 vapier 1.7 *.deb|*.udeb)
141 vapier 1.28 if type -P dpkg > /dev/null ; then
142 vapier 1.7 dpkg --info "$1"
143     dpkg --contents "$1"
144 vapier 1.6 else
145     ar tv "$1"
146     ar p "$1" data.tar.gz | tar tzvvf -
147     fi
148     ;;
149 vapier 1.3
150 vapier 1.36 ### Filesystems ###
151     *.squashfs) unsquashfs -s "$1" && unsquashfs -ll "$1" ;;
152    
153 vapier 1.3 ### Media ###
154 vapier 1.52 *.bmp|*.gif|*.jpeg|*.jpg|*.ico|*.pcd|*.pcx|*.png|*.ppm|*.tga|*.tiff|*.tif|*.webp)
155 vapier 1.6 identify "$1" || file -L -- "$1"
156     ;;
157 vapier 1.52 *.asf|*.avi|*.mov|*.mp4|*.mpeg|*.mpg|*.qt|*.ram|*.rm|*.webm|*.wmv)
158 vapier 1.6 midentify "$1" || file -L -- "$1"
159 vapier 1.3 ;;
160 vapier 1.6 *.mp3) mp3info "$1" || id3info "$1" ;;
161     *.ogg) ogginfo "$1" ;;
162     *.flac) metaflac --list "$1" ;;
163 vapier 1.44 *.torrent) torrentinfo "$1" || torrentinfo-console "$1" || ctorrent -x "$1" ;;
164 vapier 1.28 *.bin|*.cue|*.raw)
165 vapier 1.46 # 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 vapier 1.28 ;;
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 vapier 1.6
184 vapier 1.42 ### Encryption stuff ###
185     *.crl) openssl crl -hash -text -noout -in "$1" ;;
186 vapier 1.53 *.csr) openssl req -text -noout -in "$1" ;;
187     *.crt|*.pem) openssl x509 -hash -text -noout -in "$1" ;;
188 vapier 1.42
189 vapier 1.6 # 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 vapier 1.3 ### Everything else ###
197     *)
198 vapier 1.45 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 vapier 1.6
202 vapier 1.45 # Maybe we didn't match because the file is named weird ...
203     1) lesspipe_file "$1" ;;
204     esac
205 vapier 1.3
206 vapier 1.39 # 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 vapier 1.50 LESSQUIET=true ${LESSCOLORIZER} "$1"
225 vapier 1.39 fi
226     fi
227    
228     # Nothing left to do but let less deal
229 vapier 1.6 exit 0
230 vapier 1.3 ;;
231     esac
232     }
233    
234 vapier 1.7 if [[ -z $1 ]] ; then
235 vapier 1.43 echo "Usage: lesspipe <file>"
236 vapier 1.28 elif [[ $1 == "-V" || $1 == "--version" ]] ; then
237 vapier 1.13 Id="cvsid"
238 vapier 1.12 cat <<-EOF
239 vapier 1.54 $Id: lesspipe.sh,v 1.53 2013/08/06 21:35:30 vapier Exp $
240 vapier 1.51 Copyright 2001-2013 Gentoo Foundation
241 vapier 1.12 Mike Frysinger <vapier@gentoo.org>
242     (with plenty of ideas stolen from other projects/distros)
243 vapier 1.28
244    
245 vapier 1.12 EOF
246     less -V
247     elif [[ $1 == "-h" || $1 == "--help" ]] ; then
248     cat <<-EOF
249 vapier 1.49 lesspipe: preprocess files before sending them to less
250 vapier 1.28
251 vapier 1.43 Usage: lesspipe <file>
252 vapier 1.28
253 vapier 1.43 lesspipe specific settings:
254 vapier 1.35 LESSCOLOR env - toggle colorizing of output (no/yes/always)
255 vapier 1.12 LESSCOLORIZER env - program used to colorize output (default: code2color)
256 vapier 1.29 LESSIGNORE - list of extensions to ignore (don't do anything fancy)
257 vapier 1.28
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 vapier 1.43 To use lesspipe, simply add to your environment:
263     export LESSOPEN="|lesspipe %s"
264 vapier 1.28
265 vapier 1.12 Run 'less --help' or 'man less' for more info
266     EOF
267 vapier 1.7 elif [[ -d $1 ]] ; then
268 vapier 1.3 ls -alF -- "$1"
269     else
270     recur=0
271 vapier 1.37 [[ -n ${LESSDEBUG} ]] \
272     && lesspipe "$1" \
273     || lesspipe "$1" 2> /dev/null
274 vapier 1.3 fi

  ViewVC Help
Powered by ViewVC 1.1.20