1#!/bin/bash
2#!/bin/zsh -y
3#!/bin/ksh
4# lesspipe.sh, a preprocessor for less (version 1.35)
5#===============================================================================
6### THIS FILE IS GENERATED FROM lesspipe.sh.in, PLEASE GET THE TAR FILE
7### ftp://ftp.ifh.de/pub/unix/utility/lesspipe.tar.gz
8### AND RUN configure TO GENERATE A lesspipe.sh THAT WORKS IN YOUR ENVIRONMENT
9#===============================================================================
10#
11# Usage:   lesspipe.sh is called when the environment variable LESSOPEN is set:
12#	   LESSOPEN="|lesspipe.sh %s"; export LESSOPEN	(sh like shells)
13#	   setenv LESSOPEN "|lesspipe.sh %s"		(csh, tcsh)
14#	   Use the fully qualified path if lesspipe.sh is not in the search path
15#	   View files in multifile archives:
16#			less archive_file:contained_file
17#	   This can be used to extract ASCII files from a multifile archive:
18#			less archive_file:contained_file>extracted_file
19#          As less is not good for extracting binary data use instead:
20#			lesspipe.sh archive_file:contained_file>extracted_file
21#          Even a file in a multifile archive that itself is contained in yet
22#          another archive can be viewed this way:
23#			less super_archive:archive_file:contained_file
24#	   Display the last file in the file1:..:fileN chain in raw format:
25#	   Suppress input filtering:	less file1:..:fileN:   (append a colon)
26#	   Suppress decompression:	less file1:..:fileN::  (append 2 colons)
27# Required programs:
28#	   see the separate file README
29# Supported formats:
30#	   gzip, compress, bzip2, zip, tar, nroff, ar library, pdf, ps, dvi,
31#	   shared library, executable, directory, RPM, Microsoft Word, Debian,
32#	   mp3 files and filesystems on removable media via /dev/xxx
33#
34# License: GPL (see file LICENSE)
35#
36# History: see separate file ChangeLog or
37# 	   http://www.desy.de/zeuthen/~friebel/unix/lesspipe.html
38#
39# Author:  Wolfgang Friebel DESY Zeuthen (Wolfgang.Friebel@desy.de)
40#
41#===============================================================================
42tarcmd=gtar
43if [[ `tar --version 2>&1` = *GNU* ]]; then
44  tarcmd=tar
45fi
46filecmd='/usr/bin/file -L -s';
47sep=:						# file name separator
48altsep==					# alternate separator character
49if [[ -f "$1" && "$1" = *$sep* || "$1" = *$altsep ]]; then
50  sep=$altsep
51fi
52tmp=/tmp/.lesspipe.$$				# temp file name
53trap 'rm -f $tmp $tmp.dvi $tmp. $tmp.. $tmp.1' 0
54trap PIPE
55
56show () {
57  file1=${1%%$sep*}
58  rest1=${1#$file1}
59  rest11=${rest1#$sep}
60  file2=${rest11%%$sep*}
61  rest2=${rest11#$file2}
62  rest11=$rest1
63  if [[ $# = 1 ]]; then
64    type=`$filecmd "$file1" | cut -d : -f 2-`
65    get_cmd "$type" "$file1" $rest1
66    if [[ "$cmd" != "" ]]; then
67      show "-$rest1" "$cmd"
68    else
69      isfinal "$type" "$file1" $rest11
70    fi
71  elif [[ $# = 2 ]]; then
72    type=`$2 | $filecmd - | cut -d : -f 2-`
73    get_cmd "$type" "$file1" $rest1
74    if [[ "$cmd" != "" ]]; then
75      show "-$rest1" "$2" "$cmd"
76    else
77      $2 | isfinal "$type" - $rest11
78    fi
79  elif [[ $# = 3 ]]; then
80    type=`$2 | $3 | $filecmd - | cut -d : -f 2-`
81    get_cmd "$type" "$file1" $rest1
82    if [[ "$cmd" != "" ]]; then
83      show "-$rest1" "$2" "$3" "$cmd"
84    else
85      $2 | $3 | isfinal "$type" - $rest11
86    fi
87  elif [[ $# = 4 ]]; then
88    type=`$2 | $3 | $4 | $filecmd - | cut -d : -f 2-`
89    get_cmd "$type" "$file1" $rest1
90    if [[ "$cmd" != "" ]]; then
91      show "-$rest1" "$2" "$3" "$4" "$cmd"
92    else
93      $2 | $3 | $4 | isfinal "$type" - $rest11
94    fi
95  elif [[ $# = 5 ]]; then
96    type=`$2 | $3 | $4 | $5 | $filecmd - | cut -d : -f 2-`
97    get_cmd "$type" "$file1" $rest1
98    if [[ "$cmd" != "" ]]; then
99      show "-$rest1" "$2" "$3" "$4" "$5" "$cmd"
100    else
101      $2 | $3 | $4 | $5 | isfinal "$type" - $rest11
102    fi
103  elif [[ $# = 6 ]]; then
104    type=`$2 | $3 | $4 | $5 | $6 | $filecmd - | cut -d : -f 2-`
105    get_cmd "$type" "$file1" $rest1
106    if [[ "$cmd" != "" ]]; then
107      echo "$0: Too many levels of encapsulation"
108    else
109      $2 | $3 | $4 | $5 | $6 | isfinal "$type" - $rest11
110    fi
111  fi
112}
113
114get_cmd () {
115  cmd=
116  if [[ "$2" = /*\ * ]]; then
117    ln -s "$2" $tmp..
118    set "$1" $tmp..
119  elif [[ "$2" = *\ * ]]; then
120    ln -s $PWD/"$2" $tmp..
121    set "$1" $tmp..
122  fi
123
124  if [[ "$1" = *bzip* || "$1" = *compress[\'e]d\ * || "$1" = *packed\ data* ]]; then
125    if [[ "$3" = $sep$sep ]]; then
126      return
127    elif [[ "$1" = *bzip* ]]; then
128      cmd="bzip2 -cd $2"
129    else
130      cmd="gzip -cd $2"
131    fi
132    return
133  fi
134
135  rest1=$rest2
136  if [[ "$file2" != "" ]]; then
137    if [[ "$1" = *tar* ]]; then
138      cmd="$tarcmd Oxf $2 $file2"
139    elif [[ "$1" = *Debian* ]]; then
140      cmd="isdeb $2 $file2"
141    elif [[ "$1" = *RPM* ]]; then
142      cmd="isrpm $2 $file2"
143    elif [[ "$1" = *Zip* ]]; then
144      cmd="iszip $2 $file2"
145    elif [[ "$1" = *\ ar\ archive* ]]; then
146      cmd="isar $2 $file2"
147    elif [[ "$1" = *x86\ boot\ sector* ]]; then
148      cmd="isfloppy $2 $file2"
149    fi
150  fi
151}
152
153iszip () {
154  if [[ "$1" = - ]]; then
155    rm -f $tmp
156    cat > $tmp
157    set $tmp "$2"
158  fi
159  /usr/bin/unzip -avp "$1" "$2"
160}
161
162isdeb () {
163  if [[ "$1" = - ]]; then
164    rm -f $tmp
165    cat > $tmp
166    set $tmp "$2"
167  fi
168  dpkg -I "$1"
169  dpkg --fsys-tarfile "$1" | $tarcmd Oxf - "$2"
170}
171
172isdvi () {
173  if [[ "$1" = - ]]; then
174    set $1 ""
175  fi
176  if [[ "$1" != *.dvi ]]; then
177    rm -f $tmp.dvi
178    cat $1 > $tmp.dvi
179    set $tmp.dvi "$1"
180  fi
181  dvi2tty "$1"
182}
183
184isar () {
185  if [[ "$1" = - ]]; then
186    rm -f $tmp
187    cat > $tmp
188    set $tmp "$2"
189  fi
190  ar p "$1" "$2"
191}
192
193isrpm () {
194  if [[ "$1" = - ]]; then
195    rm -f $tmp
196    cat > $tmp
197    set $tmp "$2"
198  fi
199  echo $tmp.1 > $tmp.
200# GNU cpio has an undocumented but most useful --rename-batch-file switch
201  rm -f $tmp.1
202  rpm2cpio $1|cpio -i --quiet --rename-batch-file $tmp. ${2##/}
203  cat $tmp.1
204}
205
206
207isfloppy () {
208# get the device to drive mapping
209  mtoolstest |
210  while read i1 i2
211  do
212    if [[ "$i1" = *$1* ]]; then
213      if [[ "$2" = "" ]]; then
214	/usr/bin/mdir $drive
215      else
216	mtype $drive$2
217      fi
218      return
219    elif [[ "$i1" = drive ]]; then
220      drive=$i2
221    fi
222  done
223}
224
225
226isfinal() {
227
228  if [[ "$3" = $sep || "$3" = $sep$sep ]]; then
229    cat $2
230    return
231  elif [[ "$2" = - ]]; then
232    case "$1" in
233    *RPM*|*\ ar\ archive*|*shared*|*Zip*)
234      cat > $tmp.dvi
235      set "$1" $tmp.dvi
236    esac
237  fi
238  if [[ "$1" = *No\ such* ]]; then
239    return
240  elif [[ "$1" = *directory* ]]; then
241    echo "==> This is a directory, showing the output of ls -lAL"
242    ls -lAL "$2"
243  elif [[ "$1" = *tar* ]]; then
244    echo "==> use tar_file${sep}contained_file to view a file in the archive"
245    $tarcmd tvf "$2"
246  elif [[ "$1" = *RPM* ]]; then
247    echo "==> use RPM_file${sep}contained_file to view a file in the RPM"
248    /bin/rpm -qivp "$2"
249    echo "================================= Content ======================================"
250    rpm2cpio $2|cpio -i -tv --quiet
251  elif [[ "$1" = *roff* ]]; then
252    DEV=latin1
253    if [[ "$LANG" = ja* ]]; then
254      DEV=nippon
255    fi
256    MACRO=andoc
257    if [[ "$2" = *.me ]]; then
258      MACRO=e
259    elif [[ "$2" = *.ms ]]; then
260      MACRO=s
261    fi
262    echo "==> append $sep to filename to view the nroff source"
263    groff -s -p -t -e -T$DEV -m$MACRO ${2#-}
264  elif [[ "$1" = *Debian* ]]; then
265    echo "==> use Deb_file${sep}contained_file to view a file in the Deb"
266    dpkg -I ${2#-}
267    dpkg -c ${2#-}
268  elif [[ "$1" = *text\ executable* ]]; then
269    if [[ "$2" = - ]]; then
270      cat
271    fi
272  elif [[ "$1" = *PostScript* ]]; then
273    echo "==> append $sep to filename to view the postscript file"
274    ps2ascii ${2#-}
275  elif [[ "$1" = *executable* ]]; then
276    echo "==> append $sep to filename to view the binary file"
277    strings ${2#-}
278  elif [[ "$1" = *\ ar\ archive* ]]; then
279    echo "==> use library${sep}contained_file to view a file in the archive"
280    ar vt "$2"
281  elif [[ "$1" = *shared* ]]; then
282    echo "==> This is a dynamic library, showing the output of nm"
283    nm "$2"
284  elif [[ "$1" = *Zip* ]]; then
285    echo "==> use zip_file${sep}contained_file to view a file in the archive"
286    /usr/bin/unzip -lv "$2"
287  elif [[ "$1" = *x86\ boot\ sector* ]]; then
288    echo "==> use $2${sep}contained_file to view a file on the floppy"
289    isfloppy $2
290  elif [[ "$1" = *DVI* ]]; then
291    echo "==> append $sep to filename to view the binary DVI file"
292    isdvi $2
293  elif [[ "$1" = *HTML* ]]; then
294    echo "==> append $sep to filename to view the HTML source"
295    lynx -dump -force_html "$2"
296  elif [[ "$1" = *PDF* ]]; then
297    echo "==> append $sep to filename to view the PDF source"
298    pdftotext "$2" -
299  elif [[ "$1" = *Microsoft\ Word* || "$1" = *Microsoft\ Office* ]]; then
300    antiword "$2"
301  elif [[ "$1" = *Rich\ Text\ Format* ]]; then
302    echo "==> append $sep to filename to view the RTF source"
303    /usr/local/bin/unrtf --text "$2" 2> /dev/null | sed -e "s/^### .*//" | fmt -s
304##ifdef jpeg2ascii,convert
305## get jpeg2ascii (CVS) from http://dyne.org/cgi-bin/cvsweb.cgi/jpeg2ascii/
306# very experimental attempt to display images using ASCII art (do not use)
307#  elif [[ "$1" = *image\ data*  || "$1" = *image\ text* || "$1" = *JPEG\ file* || "$1" = *JPG\ file* ]]; then
308#    convert -colorspace gray -geometry 100%x50% -contrast -geometry 320x1024 "$2" /tmp/.lesspipe1$$.jpg
309#    jpeg2ascii < /tmp/.lesspipe$$.jpg 2> /dev/null
310#    rm  /tmp/.lesspipe$$.jpg /tmp/.lesspipe1$$.jpg
311##elif pbmtoascii,convert
312# ASCII Art conversion using netbpm
313# elif [[ "$1" = *image\ data*  || "$1" = *image\ text* || "$1" = *JPEG\ file*  || "$1" = *JPG\ file* ]]; then
314#    convert -contrast -geometry 80x2048 "$2" /tmp/.lesspipe$$.pbm
315#    pbmtoascii  /tmp/.lesspipe$$.pbm 2> /dev/null
316#    rm  /tmp/.lesspipe$$.pbm
317##endif
318##ifdef mplayer
319#  elif [[ "$1" = *MPEG\ system\ stream*  || "$1" = *RIFF* || "$1" = *AVI* ]]; then
320#    mplayer -vo aa -aadriver slang -aanodim -aanobold -aacontrast 50 -aabright 1  "$2" 2> /dev/null
321##endif
322  elif [[ "$1" = *MPEG\ *layer\ 3\ audio* || "$1" = *mp3\ file* || "$1" = *MP3* ]]; then
323    mp3info "$2"
324  elif [[ "$1" = *data* ]]; then
325    echo "==> append $sep to filename to view the $1 source"
326    strings $2
327  elif [[ "$2" = - ]]; then
328    cat
329  fi
330}
331
332# calling show with arg1 arg2 ... is equivalent to calling with arg1:arg2:...
333IFS=$sep a="$*"
334IFS=' '
335show "$a"
336