]> git.pld-linux.org Git - packages/lesspipe.git/blob - lesspipe.sh
suggest some commonly used tools (except for file archivers),
[packages/lesspipe.git] / lesspipe.sh
1 #!/bin/sh
2 #
3 # Copyright 1997, 1998 Patrick Volkerding, Moorhead, Minnesota USA
4 # All rights reserved.
5 #
6 # Redistribution and use of this script, with or without modification, is
7 # permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of this script must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 #
12 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 #  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
15 #  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16 #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
18 #  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19 #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
20 #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21 #  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 # This is a preprocessor for 'less'.  It is used when this environment
24 # variable is set:   LESSOPEN="|lesspipe.sh %s"
25
26 # attempt to reveal info about initrd image
27 initrd() {
28         local ft=$(file "$1") dec tmp ft2
29
30         case "$ft" in
31         *LZMA?compressed?data*)
32                 dec="lzma -dc"
33                 ;;
34         *gzip?compressed?data*)
35                 dec="gzip -dc"
36                 ;;
37         *data*)
38                 dec="xz -dc"
39                 ;;
40         esac
41
42         [ "$ft" ] || return 1
43         tmp=$(mktemp -d) || return 1
44         $dec "$1" > $tmp/initrd.img || {
45                 rm -rf $tmp
46                 return 1
47         }
48
49         ft2=$(file $tmp/initrd.img)
50         echo "$ft:${ft2#$tmp/initrd.img:}"
51         case "$ft2" in
52         *cpio?archive*)
53                 install -d $tmp/initrd
54                 (cd $tmp/initrd && cpio -dimu --quiet < $tmp/initrd.img)
55                 ;;
56         *romfs?filesystem*)
57                 install -d $tmp/initrd
58                 mount -ro loop $tmp/initrd.img $tmp/initrd
59                 ;;
60         *)
61                 rm -rf $tmp
62                 return 0
63                 ;;
64         esac
65
66         (cd $tmp/initrd; ls -lR)
67
68         # also display linuxrc
69         if [ -f $tmp/initrd/linuxrc ]; then
70                 echo ""
71                 echo "/linuxrc program:"
72                 cat $tmp/initrd/linuxrc
73         fi
74
75         mountpoint -q $tmp/initrd && umount $tmp/initrd
76
77         rm -rf $tmp
78         return 0
79 }
80
81 # display library info
82 # DT_NEEDED, SONAME etc
83 library_info() {
84         local file="$1"
85
86         objdump -p "$file"
87 }
88
89 lesspipe() {
90         case "$1" in
91         # possible initrd images
92         *initrd-*.gz)
93                 initrd "$1" && return 0
94         ;;
95
96         # archives
97         *.7z) 7z l "$1" ;;
98         *.a) ar tvf "$1" ;;
99         *.tar.bz|*.tbz) bzip -d < "$1" | tar tvvf - ;;
100         *.bz) bzip -d < "$1" ;;
101         *.tar.bz2|*.tbz2) bzip2 -dc -- "$1" | tar tvvf - ;;
102         *.bz2) bzip2 -dc -- "$1" ;;
103         *.cab|*.CAB) cabextract -l -- "$1" ;;
104         *.cpi|*.cpio) cpio -itv < "$1" ;;
105         *.cpio.gz) gzip -dc -- "$1" | cpio -itv ;;
106         *.deb) dpkg -c "$1" ;;
107         *.tar.gz|*.tar.[Zz]|*.tgz) tar tzvvf "$1" ;;
108         *.gz|*.[Zz]) gzip -dc -- "$1" ;;
109         *.tar.lzma|*.tar.xz) tar tJvvf "$1" ;;
110         *.lzma|*.xz) xz -dc -- "$1" || lzma -dc -- "$1" ;;
111         *.phar) phar info -f "$1"; phar list -f "$1" ;;
112         *.rar) unrar -p- vb -- "$1" ;;
113         *.rpm) rpm -qpivl --changelog -- "$1" ;;
114         *.tar|*.ova|*.gem) tar tvvf "$1" ;;
115         *.sqf) unsquashfs -d . -ll "$1" ;;
116         *.zip|*.jar|*.xpi|*.[hj]pi|*.pk3|*.skz|*.gg|*.ipa|*.whl|*.crx) 7z l "$1" || unzip -l "$1" ;;
117         # .war could be Zip (limewire) or tar.gz file (konqueror web archives)
118         *.war) 7z l "$1" || unzip -l "$1" || tar tzvvf "$1" ;;
119         # other file types not handled via mailcap (no mimetype)
120         *.rrd) rrdtool info "$1" ;;
121         # SSL certs
122         *.csr) openssl req -noout -text -in "$1" ;;
123         *.crl) openssl crl -noout -text -in "$1" ;;
124         *.crt) openssl x509 -noout -text -in "$1" ;;
125         *.p7s) openssl pkcs7 -noout -text -in "$1" -print_certs -inform DER;;
126         # gnupg armored files
127         *.asc) command -v gpg >/dev/null && { gpg -nv --homedir=/dev/null "$1" || : ; } || gpg2 -nv --homedir=/dev/null "$1" ;;
128         *.gpg) gpg -d "$1" ;;
129         *.so) library_info "$1" ;;
130         # Possible manual pages
131         *.1|*.2|*.3|*.4|*.5|*.6|*.7|*.8|*.9|*.l|*.n|*.man)
132                 FILE=$(file -L "$1") # groff src
133                 FILE=$(echo $FILE | cut -d ' ' -f 2)
134                 if [ "$FILE" = "troff" ]; then
135                         groff -s -p -t -e -Tlatin1 -mandoc "$1"
136                 fi
137                 ;;
138         # possible sqlite3
139         *.db)
140                 FILE=$(file -bL "$1")
141                 FILE=$(echo $FILE | cut -d , -f 1)
142                 [ "$FILE" = "SQLite 3.x database" ] && sqlite3 "$1" .dump
143                 ;;
144         *)
145                 case $TERM in
146                 xterm|xterm-color|xterm*88color|xterm*256color)
147                         output=xterm256;;
148                 *)
149                         output=ansi;;
150                 esac
151                 run-mailcap "$1" || {
152                         echo "$LESS" | grep -qi r || ps -p $PPID,$(ps -p $PPID -oppid=) -oargs= | grep -qiw -- -r && highlight --$output --style=darkblue "$1";
153                 }
154         # Check to see if binary, if so -- view with 'strings'
155         # FILE=$(file -L "$1")
156         esac
157 }
158
159 if [ -d "$1" ] ; then
160         /bin/ls -alF -- "$1"
161 else
162         lesspipe "$1" 2> /dev/null
163 fi
This page took 0.081723 seconds and 3 git commands to generate.