Update soundtest
[grml-scripts.git] / usr_bin / lesspipe.sh
1 #!/bin/bash
2 # lesspipe.sh, a preprocessor for less (version 1.53)
3 #===============================================================================
4 ### THIS FILE IS GENERATED FROM lesspipe.sh.in, PLEASE GET THE TAR FILE
5 ### ftp://ftp.ifh.de/pub/unix/utility/lesspipe.tar.gz
6 ### AND RUN configure TO GENERATE A lesspipe.sh THAT WORKS IN YOUR ENVIRONMENT
7 #===============================================================================
8 #
9 # Usage:   lesspipe.sh is called when the environment variable LESSOPEN is set:
10 #          LESSOPEN="|lesspipe.sh %s"; export LESSOPEN  (sh like shells)
11 #          setenv LESSOPEN "|lesspipe.sh %s"            (csh, tcsh)
12 #          Use the fully qualified path if lesspipe.sh is not in the search path
13 #          View files in multifile archives:
14 #                       less archive_file:contained_file
15 #          This can be used to extract ASCII files from a multifile archive:
16 #                       less archive_file:contained_file>extracted_file
17 #          As less is not good for extracting binary data use instead:
18 #                       lesspipe.sh archive_file:contained_file>extracted_file
19 #          Even a file in a multifile archive that itself is contained in yet
20 #          another archive can be viewed this way:
21 #                       less super_archive:archive_file:contained_file
22 #          Display the last file in the file1:..:fileN chain in raw format:
23 #          Suppress input filtering:    less file1:..:fileN:   (append a colon)
24 #          Suppress decompression:      less file1:..:fileN::  (append 2 colons)
25 # Required programs:
26 #          see the separate file README
27 # Supported formats:
28 #          gzip, compress, bzip2, zip, rar, tar, nroff, ar archive, pdf, ps,
29 #          dvi, shared library, executable, directory, RPM, Microsoft Word,
30 #          Openoffice 1.x and OASIS (Opendocument) formats, Debian, mp3 files,
31 #          image formats (png, gif, jpeg, tiff, ...), utf-16 text,
32 #          iso images 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 AT desy.de)
40 #
41 #===============================================================================
42 #setopt KSH_ARRAYS SH_WORD_SPLIT
43 tarcmd=gtar
44 if [[ `tar --version 2>&1` = *GNU* ]]; then
45   tarcmd=tar
46 fi
47 filecmd='file -L -s';
48 sep=:                                           # file name separator
49 altsep==                                        # alternate separator character
50 if [[ -f "$1" && "$1" = *$sep* || "$1" = *$altsep ]]; then
51   sep=$altsep
52 fi
53 tmp=/tmp/.lesspipe.$$                           # temp file name
54 trap 'rm -f $tmp $tmp.dvi $tmp. $tmp.. $tmp... $tmp.1' 0
55 trap - PIPE
56
57 show () {
58 #  if [[ "$1" = *.pod || "$1" = *.pm ]]; then
59 #    pod=1
60 #  fi
61   file1="${1%%$sep*}"
62   rest1="${1#$file1}"
63   while [[ "$rest1" = ::* ]]; do
64     if [[ "$rest1" = "::" ]]; then
65       break
66     else
67       rest1="${rest1#$sep$sep}"
68       file1="${rest1%%$sep*}"
69       rest1="${rest1#$file1}"
70       file1="${1%$rest1}"
71     fi
72   done
73   rest11="${rest1#$sep}"
74   file2="${rest11%%$sep*}"
75   rest2="${rest11#$file2}"
76   while [[ "$rest2" = ::* ]]; do
77     if [[ "$rest2" = "::" ]]; then
78       break
79     else
80       rest2="${rest2#$sep$sep}"
81       file2="${rest2%%$sep*}"
82       rest2="${rest2#$file2}"
83       file2="${rest11%$rest2}"
84     fi
85   done
86   if [[ "$file2" != "" ]]; then
87     in_file="-i$file2"
88   fi
89   rest2="${rest11#$file2}"
90   rest11="$rest1"
91   if [[ "$cmd" = "" ]]; then
92     type=`$filecmd "$file1" | cut -d : -f 2-`
93     if [[ ! -f "$file1" ]]; then
94       if [[ "$type" = *directory* ]]; then
95         if [[ "$file1" = *.pkg ]]; then
96           if [[ -f "$file1/Contents/Archive.bom" ]]; then
97             type="bill of materials"
98             file1="$file1/Contents/Archive.bom"
99             echo "==> This is a Mac OS X archive directory, showing its contents (bom file)"
100           fi
101         fi
102       fi
103     fi
104     get_cmd "$type" "$file1" "$rest1"
105     if [[ "$cmd" != "" ]]; then
106       show "-$rest1"
107     else
108       isfinal "$type" "$file1" "$rest11"
109     fi
110   elif [[ "$c1" = "" ]]; then
111     c1[0]=${cmd[0]};c1[1]=${cmd[1]};c1[2]=${cmd[2]}
112     if [[ "${cmd[3]}" != "" ]]; then
113       c1[3]=${cmd[3]};
114     fi
115     type=`"${c1[@]}" | dd bs=40000 count=1 2>/dev/null | filepipe | cut -d : -f 2-`
116     get_cmd "$type" "$file1" "$rest1"
117     if [[ "$cmd" != "" ]]; then
118       show "-$rest1"
119     else
120       "${c1[@]}" | isfinal "$type" - "$rest11"
121     fi
122   elif [[ "$c2" = "" ]]; then
123     c2[0]=${cmd[0]};c2[1]=${cmd[1]};c2[2]=${cmd[2]}
124     if [[ "${cmd[3]}" != "" ]]; then
125       c2[3]=${cmd[3]};
126     fi
127     type=`"${c1[@]}" | "${c2[@]}" | dd bs=40000 count=1 2>/dev/null | filepipe | cut -d : -f 2-`
128     get_cmd "$type" "$file1" "$rest1"
129     if [[ "$cmd" != "" ]]; then
130       show "-$rest1"
131     else
132       "${c1[@]}" | "${c2[@]}" | isfinal "$type" - "$rest11"
133     fi
134   elif [[ "$c3" = "" ]]; then
135     c3[0]=${cmd[0]};c3[1]=${cmd[1]};c3[2]=${cmd[2]}
136     if [[ "${cmd[3]}" != "" ]]; then
137       c3[3]=${cmd[3]};
138     fi
139     type=`"${c1[@]}" | "${c2[@]}" | "${c3[@]}" | dd bs=40000 count=1 2>/dev/null | filepipe | cut -d : -f 2-`
140     get_cmd "$type" "$file1" "$rest1"
141     if [[ "$cmd" != "" ]]; then
142       show "-$rest1"
143     else
144       "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | isfinal "$type" - "$rest11"
145     fi
146   elif [[ "$c4" = "" ]]; then
147     c4[0]=${cmd[0]};c4[1]=${cmd[1]};c4[2]=${cmd[2]}
148     if [[ "${cmd[3]}" != "" ]]; then
149       c4[3]=${cmd[3]};
150     fi
151     type=`"${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | dd bs=40000 count=1 2>/dev/null | filepipe | cut -d : -f 2-`
152     get_cmd "$type" "$file1" "$rest1"
153     if [[ "$cmd" != "" ]]; then
154       show "-$rest1"
155     else
156       "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | isfinal "$type" - "$rest11"
157     fi
158   elif [[ "$c5" = "" ]]; then
159     c5[0]=${cmd[0]};c5[1]=${cmd[1]};c5[2]=${cmd[2]}
160     if [[ "${cmd[3]}" != "" ]]; then
161       c5[3]=${cmd[3]};
162     fi
163     type=`"${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | dd bs=40000 count=1 2>/dev/null | filepipe | cut -d : -f 2-`
164     get_cmd "$type" "$file1" "$rest1"
165     if [[ "$cmd" != "" ]]; then
166       echo "$0: Too many levels of encapsulation"
167     else
168       "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | isfinal "$type" - "$rest11"
169     fi
170   fi
171 }
172
173 get_cmd () {
174   cmd=
175   if [[ "$2" = /*\ * ]]; then
176     ln -s "$2" $tmp..
177     set "$1" $tmp..
178   elif [[ "$2" = *\ * ]]; then
179     ln -s "$PWD"/"$2" $tmp..
180     set "$1" $tmp..
181   fi
182
183   if [[ "$1" = *bzip*compress* || "$1" = *compress[\'e]d\ * || "$1" = *packed\ data* ]]; then
184     if [[ "$3" = $sep$sep ]]; then
185       return
186     elif [[ "$1" = *bzip*compress* ]]; then
187       cmd=(bzip2 -cd "$2")
188     else
189       cmd=(gzip -cd "$2")
190     fi
191     return
192   fi
193     
194   rest1="$rest2"
195   if [[ "$file2" != "" ]]; then
196     if [[ "$1" = *\ tar* ]]; then
197       cmd=($tarcmd Oxf "$2" "$file2")
198     elif [[ "$1" = *Debian* ]]; then
199       istemp "ar p" "$2" data.tar.gz | gzip -dc - > $tmp.
200       cmd=($tarcmd Oxf $tmp. "$file2")
201     elif [[ "$1" = *RPM* ]]; then
202       cmd=(isrpm "$2" "$file2")
203     elif [[ "$1" = *Zip* ]]; then
204       cmd=(istemp "unzip -avp" "$2" "$file2")
205     elif [[ "$1" = *\ RAR\ archive* ]]; then
206       cmd=(istemp "unrar p -inul" "$2" "$file2")
207     elif [[ "$1" = *\ 7-zip\ archive* ]]; then
208       if [[ "$2" = - ]]; then
209         cmd=(iscmd2 "7za e -so" - "$file2")
210       else
211         cmd=(iscmd "7za e -so" "$2" "$file2")
212       fi
213     elif [[ "$1" = *[Cc]abinet* ]]; then
214       cmd=(iscab "$2" "$file2")
215     elif [[ "$1" = *\ ar\ archive* ]]; then
216       cmd=(istemp "ar p" "$2" "$file2")
217     elif [[ "$1" = *x86\ boot\ sector* ]]; then
218       cmd=(isfloppy "$2" "$file2")
219     elif [[ "$1" = *ISO\ 9660* ]]; then
220       cmd=(isoinfo "-i$2" "-x$file2")
221     fi
222   else
223     if [[ "$1" = *\ 7-zip\ archive* ]]; then
224       if [[ "$2" != - ]]; then
225         if [[ `7za l "$2" | tail -1` = *\ 1\ file ]]; then
226           cmd=("7za e -so" "$2")
227         fi
228       fi
229     fi
230   fi
231 }
232
233 filepipe () {
234   rm -f $tmp...
235   cat > $tmp...
236   $filecmd $tmp...
237 }
238
239 iscab () {
240   if [[ "$1" = - ]]; then
241     rm -f $tmp
242     cat > $tmp
243     set $tmp "$2"
244   fi
245   cabextract -pF "$2" "$1"
246 }
247
248 isdvi () {
249   if [[ "$1" = - ]]; then
250     set $1 ""
251   fi
252   if [[ "$1" != *.dvi ]]; then
253     rm -f $tmp.dvi
254     cat $1 > $tmp.dvi
255     set $tmp.dvi "$1"
256   fi
257   dvi2tty "$1"
258 }
259
260 iscmd () {
261   $1 "$2" "$3" 2>/dev/null
262 }
263
264 iscmd2 () {
265   cat > $tmp.
266   $1 $tmp. "$3" 2>/dev/null
267 }
268
269 istemp () {
270   if [[ "$2" = - ]]; then
271     rm -f $tmp
272     cat > $tmp
273     set $1 $tmp "$3"
274   fi
275   $1 "$2" "$3"
276 }
277
278 isrpm () {
279   if [[ "$1" = - ]]; then
280     rm -f $tmp
281     cat > $tmp
282     set $tmp "$2"
283   fi
284   echo $tmp.1 > $tmp.
285 # GNU cpio has an undocumented but most useful --rename-batch-file switch
286   rm -f $tmp.1
287   rpm2cpio "$1"|cpio -i --quiet --rename-batch-file $tmp. "${2##/}"
288   cat $tmp.1
289 }
290
291
292 isfloppy () {
293 # get the device to drive mapping
294   mtoolstest |
295   while read i1 i2
296   do
297     if [[ "$i1" = *$1* ]]; then
298       if [[ "$2" = "" ]]; then
299         mdir $drive
300       else
301         mtype $drive"$2"
302       fi
303       return
304     elif [[ "$i1" = drive ]]; then
305       drive=$i2
306     fi
307   done
308 }
309
310
311 isfinal() {
312
313   if [[ "$3" = $sep$sep ]]; then
314     cat "$2"
315     return
316   elif [[ "$3" = $sep* ]]; then
317     if [[ "$3" = $sep ]]; then
318       echo "==> append :. or :<filetype> to activate syntax highlighting"
319     else
320       lang=${3#$sep}
321       lang="-l ${lang#.}"
322       lang=${lang%%-l }
323       dir=${LESSOPEN#\|}
324       dir=${dir%%lesspipe.sh*\%s}
325       ${dir}code2color $PPID ${in_file:+"$in_file"} $lang "$2"
326       if [[ $? = 0 ]]; then
327         return
328       fi
329     fi
330     cat "$2"
331     return
332   elif [[ "$2" = - ]]; then
333     case "$1" in 
334     *RPM*|*\ ar\ archive*|*shared*|*Zip*|*\ RAR\ archive*)
335       cat > $tmp.dvi
336       set "$1" $tmp.dvi
337     esac
338   fi
339   if [[ "$1" = *No\ such* ]]; then
340     return
341   elif [[ "$1" = *directory* ]]; then
342     echo "==> This is a directory, showing the output of ls -lAL"
343     ls -lAL "$2"
344   elif [[ "$1" = *\ tar* ]]; then
345     echo "==> use tar_file${sep}contained_file to view a file in the archive"
346     $tarcmd tvf "$2"
347   elif [[ "$1" = *RPM* ]]; then
348     echo "==> use RPM_file${sep}contained_file to view a file in the RPM"
349     rpm -qivp "$2"
350     echo "================================= Content ======================================"
351     rpm2cpio "$2"|cpio -i -tv --quiet
352   elif [[ "$1" = *roff* ]]; then
353     DEV=latin1
354     if [[ "$LANG" = ja* ]]; then
355       DEV=nippon
356     fi
357     MACRO=andoc
358     if [[ "$2" = *.me ]]; then
359       MACRO=e
360     elif [[ "$2" = *.ms ]]; then
361       MACRO=s
362     fi
363     echo "==> append $sep to filename to view the nroff source"
364     groff -s -p -t -e -T$DEV -m$MACRO "$2"
365   elif [[ "$1" = *Debian* ]]; then
366     echo "==> use Deb_file${sep}contained_file to view a file in the Deb"
367     dpkg -I "${2#-}"
368     istemp "ar p" "$2" data.tar.gz | gzip -dc - | $tarcmd tvf -
369 #  elif [[ "$1" = *perl\ *script\ text* || "$pod" = 1 ]]; then
370 #      pod2text "$2" > $tmp.dvi
371 #      if [[ -s $tmp.dvi ]]; then
372 #       echo "==> append $sep to filename to view the Perl source"
373 #       cat $tmp.dvi
374 #      fi
375   elif [[ "$1" = *\ script* ]]; then
376     set "plain text" "$2"
377   elif [[ "$1" = *text\ executable* ]]; then
378     set "plain text" "$2"
379   elif [[ "$1" = *PostScript* ]]; then
380     echo "==> append $sep to filename to view the postscript file"
381     which pstotext >/dev/null 2>&1
382     if [[ $? = 0 ]]; then
383       pstotext "${2#-}"
384     else
385       ps2ascii "$2"
386     fi
387   elif [[ "$1" = *executable* ]]; then
388     echo "==> append $sep to filename to view the binary file"
389     if [[ "$2" = "-" ]]; then
390       strings
391     else
392       strings "$2"
393     fi
394   elif [[ "$1" = *\ ar\ archive* ]]; then
395     echo "==> use library${sep}contained_file to view a file in the archive"
396     ar vt "$2"
397   elif [[ "$1" = *shared* ]]; then
398     echo "==> This is a dynamic library, showing the output of nm"
399     nm "$2"
400   elif [[ "$1" = *Zip* ]]; then
401     echo "==> use zip_file${sep}contained_file to view a file in the archive"
402     unzip -lv "$2"
403   elif [[ "$1" = *\ RAR\ archive* ]]; then
404     echo "==> use rar_file${sep}contained_file to view a file in the archive"
405     unrar v "$2"
406   elif [[ "$1" = *\ 7-zip\ archive* ]]; then
407     echo "==> use 7-zip_file${sep}contained_file to view a file in the archive"
408     if [[ "$2" = - ]]; then
409       istemp "7za l" -
410     else
411       7za l "$2"
412     fi
413   elif [[ "$1" = *[Cc]abinet* ]]; then
414     echo "==> use cab_file${sep}contained_file to view a file in the cabinet"
415     cabextract -l "$2"
416   elif [[ "$1" = *x86\ boot\ sector* ]]; then
417     echo "==> use $2${sep}contained_file to view a file on the floppy"
418     isfloppy "$2"
419   elif [[ "$1" = *\ DVI* ]]; then
420     echo "==> append $sep to filename to view the binary DVI file"
421     isdvi "$2"
422   elif [[ "$1" = *HTML* ]]; then
423     echo "==> append $sep to filename to view the HTML source"
424     html2text -style pretty "$2"
425   elif [[ "$1" = *PDF* ]]; then
426     echo "==> append $sep to filename to view the PDF source"
427     istemp pdftotext "$2" -
428   elif [[ "$1" = *Microsoft\ Word* || "$1" = *Microsoft\ Office* ]]; then
429     antiword "$2"
430   elif [[ "$1" = *Rich\ Text\ Format* ]]; then
431     echo "==> append $sep to filename to view the RTF source"
432     unrtf --html "$2" 2>/dev/null | html2text -style pretty
433   elif [[ "$1" = *OpenDocument\ [CHMPST]* || "$1" = *OpenOffice\.org\ 1\.x\ [CIWdgpst]* ]]; then
434     conv="utf8tolatin1"
435     if [[ "$LANG" = *UTF-8 ]]; then
436       conv="cat"
437     fi
438     echo "==> append $sep to filename to view the OpenOffice or OpenDocument source"
439     istemp "unzip -avp" "$2" content.xml | o3tohtml | $conv | html2text -style pretty
440   elif [[ "$1" = *ISO\ 9660* ]]; then
441     if [[ "$2" != - ]]; then
442       isoinfo -d -i "$2"
443       joliet=`isoinfo -d -i "$2" | egrep '^Joliet'|cut -c1`
444       echo "================================= Content ======================================"
445       isoinfo -lR$joliet -i "$2"
446     fi
447   elif [[ "$1" = *image\ data*  || "$1" = *image\ text* || "$1" = *JPEG\ file* || "$1" = *JPG\ file* ]]; then
448     identify -verbose "$2"
449 ##ifdef jpeg2ascii,convert
450 ## get jpeg2ascii (CVS) from http://dyne.org/cgi-bin/cvsweb.cgi/jpeg2ascii/
451 # very experimental attempt to display images using ASCII art (do not use)
452 #  elif [[ "$1" = *image\ data*  || "$1" = *image\ text* || "$1" = *JPEG\ file* || "$1" = *JPG\ file* ]]; then
453 #    convert -colorspace gray -geometry 100%x50% -contrast -geometry 320x1024 "$2" /tmp/.lesspipe1$$.jpg
454 #    jpeg2ascii < /tmp/.lesspipe$$.jpg 2> /dev/null
455 #    rm  /tmp/.lesspipe$$.jpg /tmp/.lesspipe1$$.jpg
456 ##elif pbmtoascii,convert
457 # ASCII Art conversion using netbpm
458 # elif [[ "$1" = *image\ data*  || "$1" = *image\ text* || "$1" = *JPEG\ file*  || "$1" = *JPG\ file* ]]; then
459 #    convert -contrast -geometry 80x2048 "$2" /tmp/.lesspipe$$.pbm
460 #    pbmtoascii  /tmp/.lesspipe$$.pbm 2> /dev/null
461 #    rm  /tmp/.lesspipe$$.pbm
462 ##endif
463 ##ifdef mplayer
464 #  elif [[ "$1" = *MPEG\ system\ stream*  || "$1" = *RIFF* || "$1" = *AVI* ]]; then
465 #    mplayer -vo aa -aadriver slang -aanodim -aanobold -aacontrast 50 -aabright 1  "$2" 2> /dev/null
466 ##endif
467   elif [[ "$1" = *MPEG\ *layer\ 3\ audio* || "$1" = *mp3\ file* || "$1" = *MP3* ]]; then
468     mp3info "$2"
469   elif [[ "$1" = *bill\ of\ materials* ]]; then
470     lsbom -p MUGsf "$2"
471   elif [[ "$1" = *perl\ Storable* ]]; then
472     perl -MStorable=retrieve -MData::Dumper -e '$Data::Dumper::Indent=1;print Dumper retrieve shift' "$2"
473   elif [[ "$1" = *UTF-16* ]]; then
474       iconv -f utf-16 "$2"
475   elif [[ "$1" = *data* ]]; then
476     echo "==> append $sep to filename to view the $1 source"
477     if [[ "$2" = "-" ]]; then
478       strings
479     else
480       strings "$2"
481     fi
482   else
483     set "plain text" "$2"
484   fi
485   if [[ "$1" = *plain\ text* ]]; then
486     dir=${LESSOPEN#\|}
487     dir=${dir%%lesspipe.sh*\%s}
488     ${dir}code2color $PPID ${in_file:+"$in_file"} "$2"
489     if [[ $? = 0 ]]; then
490       return
491     fi
492   fi
493   if [[ "$2" = - ]]; then
494     cat
495   fi  
496 }
497
498 # calling show with arg1 arg2 ... is equivalent to calling with arg1:arg2:...
499 IFS=$sep a="$@"
500 IFS=' '
501 if [[ "$a" = "" ]]; then
502   if [[ "$SHELL" = *csh ]]; then
503     echo "setenv LESSOPEN \"|$0 %s\""
504   else
505     echo "LESSOPEN=\"|$0 %s\""
506     echo "export LESSOPEN"
507   fi
508 else
509   show "$a"
510 fi