Update lesspipe.sh to upstream version 1.82
[grml-scripts-core.git] / usr_bin / lesspipe.sh
1 #!/bin/bash
2 # lesspipe.sh, a preprocessor for less (version 1.82)
3 #===============================================================================
4 ### THIS FILE IS GENERATED FROM lesspipe.sh.in, PLEASE GET THE TAR FILE
5 ### from http://sourceforge.net/projects/lesspipe/
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 raw 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 #
26 # Required programs and supported formats: see the separate file README
27 # License: GPL (see file LICENSE)
28 # History: see the separate file ChangeLog
29 # Author:  Wolfgang Friebel, DESY (Wolfgang.Friebel AT desy.de)
30 #
31 #===============================================================================
32 ( [[ -n 1 && -n 2 ]] ) > /dev/null 2>&1 || exec zsh -y --ksh-arrays -- "$0" ${1+"$@"}
33 #setopt KSH_ARRAYS SH_WORD_SPLIT 2>/dev/null
34 set +o noclobber
35 tarcmd='tar'
36
37 dir=${LESSOPEN#\|}
38 dir=${dir%%lesspipe.sh*\%s}
39 dir=${dir%%/}
40 PATH=$PATH:$dir
41
42 cmd_exist () {
43   command -v "$1" > /dev/null 2>&1 && return 0 || return 1
44 }
45
46 filecmd() {
47   file -L -s "$@"
48   file -L -s -i "$@" 2> /dev/null | sed -n 's/.*charset=/;/p' | tr a-z A-Z
49 }
50
51 sep=:                                           # file name separator
52 altsep==                                        # alternate separator character
53 if [[ -f "$1" && "$1" = *$sep* || "$1" = *$altsep ]]; then
54   sep=$altsep
55   xxx="${1%=}"
56   set "$xxx"
57 fi
58 if cmd_exist mktemp; then
59   tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/lesspipe.XXXXXXXXXX")
60
61   nexttmp () {
62     # nexttmp -d returns a directory
63     mktemp $1 "${tmpdir}/XXXXXXXX"
64   }
65 else
66   tmpdir=${TMPDIR:-/tmp}/lesspipe.$RANDOM
67   mkdir $tmpdir
68
69   nexttmp () {
70     new="$tmpdir/lesspipe.$RANDOM"
71     [[ "$1" = -d ]] && mkdir $new
72     echo $new
73   }
74 fi
75 [[ -d "$tmpdir" ]] || exit 1
76 trap "rm -rf '$tmpdir'" 0
77 trap - PIPE
78
79 unset iconv
80 iconv() {
81   if [[ -z "$iconv" ]]; then
82     arg=$(printf "%s$(command iconv --help 2>/dev/null | \
83       sed -n 's/.*\(--.*-subst=\)\(FORMATSTRING\).*/\1\\033[7m?\\033[m/p' | \
84       tr \\n ' ')")
85     if [[ -n "$arg" ]]; then
86       iconv="command iconv -c $arg  -t //TRANSLIT"
87     else
88       iconv="command iconv -c"
89     fi
90   fi
91   if $iconv "$@" > /dev/null 2>&1; then
92     msg "append $sep to filename to view the $2 encoded data"
93     $iconv "$@"
94   fi
95 }
96
97 msg () {
98   if [[ -n "$LESSQUIET" ]]; then
99     return
100   fi
101   echo "==> $@"
102 }
103
104 filetype () {
105   # wrapper for 'file' command
106   typeset name
107   name="$1"
108   if [[ "$1" = - ]]; then
109     dd bs=40000 count=1 > "$tmpdir/file" 2>/dev/null
110     set "$tmpdir/file" "$2"
111     name="$filen"
112   fi
113   typeset type
114   # type=" $(filecmd -b "$1")" # not supported by all versions of 'file'
115   type=$(filecmd "$1" | cut -d : -f 2-)
116   if [[ "$type" = " empty" ]]; then
117     # exit if file returns "empty" (e.g., with "less archive:nonexisting_file")
118     exit 1
119   elif [[ "$type" = *XML* && "$name" = *html ]]; then
120     type=" HTML document text"
121   elif [[ ("$type" = *HTML* || "$type" = *ASCII*) && "$name" = *xml ]]; then
122     type=" XML document text"
123   elif [[ "$type" != *lzip\ compressed* && ("$name" = *.lzma || "$name" = *.tlz) ]]; then
124     type=" LZMA compressed data"
125   elif [[ ("$type" = *Zip* || "$type" = *ZIP*) && ("$name" = *.jar || "$name" = *.xpi) ]]; then
126     type=" Zip compressed Jar archive"
127   elif [[ "$type" = *Microsoft\ Office\ Document* && ("$name" = *.ppt) ]]; then
128        type=" PowerPoint document"
129   elif [[ "$type" = *Microsoft\ Office\ Document* && ("$name" = *.xls) ]]; then
130        type=" Excel document"
131   fi
132   echo "$type"
133 }
134
135 show () {
136   file1="${1%%$sep*}"
137   rest1="${1#$file1}"
138   while [[ "$rest1" = ::* ]]; do
139     if [[ "$rest1" = "::" ]]; then
140       break
141     else
142       rest1="${rest1#$sep$sep}"
143       file1="${rest1%%$sep*}"
144       rest1="${rest1#$file1}"
145       file1="${1%$rest1}"
146     fi
147   done
148   rest11="${rest1#$sep}"
149   file2="${rest11%%$sep*}"
150   rest2="${rest11#$file2}"
151   while [[ "$rest2" = ::* ]]; do
152     if [[ "$rest2" = "::" ]]; then
153       break
154     else
155       rest2="${rest2#$sep$sep}"
156       file2="${rest2%%$sep*}"
157       rest2="${rest2#$file2}"
158       file2="${rest11%$rest2}"
159     fi
160   done
161   if [[ "$file2" != "" ]]; then
162     in_file="-i$file2"
163   fi
164   rest2="${rest11#$file2}"
165   rest11="$rest1"
166   if [[ "$cmd" = "" ]]; then
167     type=$(filetype "$file1") || exit 1
168     get_cmd "$type" "$file1" "$rest1"
169     if [[ "$cmd" != "" ]]; then
170       show "-$rest1"
171     else
172       isfinal "$type" "$file1" "$rest11"
173     fi
174   elif [[ "$c1" = "" ]]; then
175     c1=("${cmd[@]}")
176     type=$("${c1[@]}" | filetype -) || exit 1
177     get_cmd "$type" "$file1" "$rest1"
178     if [[ "$cmd" != "" ]]; then
179       show "-$rest1"
180     else
181       "${c1[@]}" | isfinal "$type" - "$rest11"
182     fi
183   elif [[ "$c2" = "" ]]; then
184     c2=("${cmd[@]}")
185     type=$("${c1[@]}" | "${c2[@]}" | filetype -) || exit 1
186     get_cmd "$type" "$file1" "$rest1"
187     if [[ "$cmd" != "" ]]; then
188       show "-$rest1"
189     else
190       "${c1[@]}" | "${c2[@]}" | isfinal "$type" - "$rest11"
191     fi
192   elif [[ "$c3" = "" ]]; then
193     c3=("${cmd[@]}")
194     type=$("${c1[@]}" | "${c2[@]}" | "${c3[@]}" | filetype -) || exit 1
195     get_cmd "$type" "$file1" "$rest1"
196     if [[ "$cmd" != "" ]]; then
197       show "-$rest1"
198     else
199       "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | isfinal "$type" - "$rest11"
200     fi
201   elif [[ "$c4" = "" ]]; then
202     c4=("${cmd[@]}")
203     type=$("${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | filetype -) || exit 1
204     get_cmd "$type" "$file1" "$rest1"
205     if [[ "$cmd" != "" ]]; then
206       show "-$rest1"
207     else
208       "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | isfinal "$type" - "$rest11"
209     fi
210   elif [[ "$c5" = "" ]]; then
211     c5=("${cmd[@]}")
212     type=$("${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | filetype -) || exit 1
213     get_cmd "$type" "$file1" "$rest1"
214     if [[ "$cmd" != "" ]]; then
215       echo "$0: Too many levels of encapsulation"
216     else
217       "${c1[@]}" | "${c2[@]}" | "${c3[@]}" | "${c4[@]}" | "${c5[@]}" | isfinal "$type" - "$rest11"
218     fi
219   fi
220 }
221
222 get_cmd () {
223   cmd=
224   typeset t
225   if [[ "$1" = *[bg]zip*compress* || "$1" = *compress\'d\ * || "$1" = *packed\ data* || "$1" = *LZMA\ compressed* || "$1" = *lzip\ compressed* || "$1" = *[Xx][Zz]\ compressed* ]]; then ## added '#..then' to fix vim's syntax parsing
226     if [[ "$3" = $sep$sep ]]; then
227       return
228     elif [[ "$1" = *bzip*compress* ]] && cmd_exist bzip2; then
229       cmd=(bzip2 -cd "$2")
230       if [[ "$2" != - ]]; then filen="$2"; fi
231       case "$filen" in
232         *.bz2) filen="${filen%.bz2}";;
233         *.tbz) filen="${filen%.tbz}.tar";;
234       esac
235       return
236     elif [[ "$1" = *LZMA\ compressed* ]] && cmd_exist lzma; then
237       cmd=(lzma -cd "$2")
238       if [[ "$2" != - ]]; then filen="$2"; fi
239       case "$filen" in
240         *.lzma) filen="${filen%.lzma}";;
241         *.tlz) filen="${filen%.tlz}.tar";;
242       esac
243     elif [[ "$1" = *gzip\ compress* || "$1" =  *compress\'d\ * || "$1" = *packed\ data* ]]; then ## added '#..then' to fix vim's syntax parsing
244       cmd=(gzip -cd "$2")
245       if [[ "$2" != - ]]; then filen="$2"; fi
246       case "$filen" in
247         *.gz) filen="${filen%.gz}";;
248         *.tgz) filen="${filen%.tgz}.tar";;
249       esac
250     elif [[ "$1" = *[Xx][Zz]\ compressed* ]] && cmd_exist xz; then
251       cmd=(xz -cd "$2")
252       if [[ "$2" != - ]]; then filen="$2"; fi
253       case "$filen" in
254        *.xz) filen="${filen%.xz}";;
255        *.txz) filen="${filen%.txz}.tar";;
256       esac
257     fi
258     return
259   fi
260
261   rsave="$rest1"
262   rest1="$rest2"
263   if [[ "$file2" != "" ]]; then
264     if [[ "$1" = *\ tar* || "$1" = *\   tar* ]]; then
265       cmd=(istar "$2" "$file2")
266     elif [[ "$1" = *Debian* ]]; then
267       t=$(nexttmp)
268       if [[ "$file2" = control/* ]]; then
269         istemp "ar p" "$2" control.tar.gz | gzip -dc - > "$t"
270         file2=".${file2:7}"
271       else
272         istemp "ar p" "$2" data.tar.gz | gzip -dc - > "$t"
273       fi
274       cmd=(istar "$t" "$file2")
275     elif [[ "$1" = *RPM* ]] && cmd_exist cpio && ( cmd_exist rpm2cpio || cmd_exist rpmunpack ); then
276       cmd=(isrpm "$2" "$file2")
277     elif [[ "$1" = *Zip* || "$1" = *ZIP* ]] && cmd_exist unzip; then
278       cmd=(istemp "unzip -avp" "$2" "$file2")
279     elif [[ "$1" = *RAR\ archive* ]]; then
280       if cmd_exist unrar; then
281         cmd=(istemp "unrar p -inul" "$2" "$file2")
282       elif cmd_exist rar; then
283         cmd=(istemp "rar p -inul" "$2" "$file2")
284       fi
285     elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7za; then
286       cmd=(istemp "7za e -so" "$2" "$file2")
287     elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7zr; then
288       cmd=(istemp "7zr e -so" "$2" "$file2")
289     elif [[ "$1" = *[Cc]abinet* ]] && cmd_exist cabextract; then
290       cmd=(iscab "$2" "$file2")
291     elif [[ "$1" = *\ ar\ archive* ]]; then
292       cmd=(istemp "ar p" "$2" "$file2")
293     elif [[ "$1" = *ISO\ 9660* ]] && cmd_exist isoinfo; then
294       cmd=(isoinfo "-i$2" "-x$file2")
295     fi
296     if [[ "$cmd" != "" ]]; then
297       filen="$file2"
298     fi
299   fi
300 }
301
302 iscab () {
303   typeset t
304   if [[ "$1" = - ]]; then
305     t=$(nexttmp)
306     cat > "$t"
307     set "$t" "$2"
308   fi
309   cabextract -pF "$2" "$1"
310 }
311
312 istar () {
313   $tarcmd Oxf "$1" "$2" 2>/dev/null
314 }
315
316 isdvi () {
317   typeset t
318   if [[ "$1" != *.dvi ]]; then
319     t="$tmpdir/tmp.dvi"
320     cat "$1" > "$t"
321     set "$t"
322   fi
323   dvi2tty -q "$1"
324 }
325
326 istemp () {
327   typeset prog
328   typeset t
329   prog="$1"
330   t="$2"
331   shift
332   shift
333   if [[ "$t" = - ]]; then
334     t=$(nexttmp)
335     cat > "$t"
336   fi
337   if [[ $# -gt 0 ]]; then
338     $prog "$t" "$@" 2>/dev/null
339   else
340     $prog "$t" 2>/dev/null
341   fi
342 }
343
344 nodash () {
345   typeset prog
346   prog="$1"
347   shift
348   if [[ "$1" = - ]]; then
349     shift
350     if [[ $# -gt 0 ]]; then
351       $prog "$@" 2>/dev/null
352     else
353       $prog 2>/dev/null
354     fi
355   else
356     $prog "$@" 2>/dev/null
357   fi
358 }
359
360 isrpm () {
361   if cmd_exist rpm2cpio && cmd_exist cpio; then
362     typeset t
363     if [[ "$1" = - ]]; then
364       t=$(nexttmp)
365       cat > "$t"
366       set "$t" "$2"
367     fi
368     # setup $b as a batch file containing "$b.out"
369     typeset b
370     b=$(nexttmp)
371     echo "$b.out" > "$b"
372     # to support older versions of cpio the --to-stdout option is not used here
373     rpm2cpio "$1" 2>/dev/null|cpio -i --quiet --rename-batch-file "$b" "$2"
374     cat "$b.out"
375   elif cmd_exist rpmunpack && cmd_exist cpio; then
376     # rpmunpack will write to stdout if it gets file from stdin
377     # extract file $2 from archive $1, assume that cpio is sufficiently new
378     # (option --to-stdout existing) if rpmunpack is installed
379     cat "$1" | rpmunpack | gzip -cd | cpio -i --quiet --to-stdout "$2"
380   fi
381 }
382
383
384 if cmd_exist html2text || cmd_exist elinks || cmd_exist links || cmd_exist lynx || cmd_exist w3m; then
385   PARSEHTML=yes
386 else
387   PARSEHTML=no
388 fi
389 #parsexml () { nodash "elinks -dump -default-mime-type text/xml" "$1"; }
390 parsehtml () {
391   if [[ "$PARSEHTML" = no ]]; then
392     msg "No suitable tool for HTML parsing found, install one of html2text, elinks, links, lynx or w3m"
393     return
394   elif cmd_exist html2text; then
395     html2text -style pretty "$1"
396   elif cmd_exist lynx; then
397     if [[ "$1" = - ]]; then set - -stdin; fi
398     lynx -dump -force_html "$1" && return
399   elif cmd_exist w3m; then
400     nodash "w3m -dump -T text/html" "$1"
401   elif cmd_exist elinks; then
402     nodash "elinks -dump -force-html" "$1"
403   elif cmd_exist links; then
404     if [[ "$1" = - ]]; then set - -stdin; fi
405     links -dump -force_html "$1"
406   fi
407 }
408
409 isfinal() {
410   typeset t
411   if [[ "$3" = $sep$sep ]]; then
412     cat "$2"
413     return
414   elif [[ "$3" = $sep* ]]; then
415     if [[ "$3" = $sep ]]; then
416       msg "append :. or :<filetype> to activate syntax highlighting"
417     else
418       lang=${3#$sep}
419       lang="-l ${lang#.}"
420       lang=${lang%%-l }
421       if cmd_exist code2color; then
422         code2color $PPID ${in_file:+"$in_file"} $lang "$2"
423         if [[ $? = 0 ]]; then
424           return
425         fi
426       fi
427     fi
428     cat "$2"
429     return
430   fi
431
432   # color requires -r or -R when calling less
433   typeset COLOR
434   if [[ $(tput colors) -ge 8 && ("$LESS" = *-*r* || "$LESS" = *-*R*) ]]; then
435     COLOR="--color=always"
436   fi
437
438   if [[ "$1" = *No\ such* ]]; then
439     exit 1
440   elif [[ "$1" = *directory* ]]; then
441     cmd=(ls -lA $COLOR "$2")
442     if ! ls $COLOR > /dev/null 2>&1; then
443       cmd=(ls -lA -G "$2")
444       if ! ls -lA -G > /dev/null 2>&1; then
445         cmd=(ls -lA "$2")
446       fi
447     fi
448     msg "This is a directory, showing the output of ${cmd[@]}"
449     if [[ ${cmd[2]} = '-G' ]]; then
450       CLICOLOR_FORCE=1 "${cmd[@]}"
451     else
452       "${cmd[@]}"
453     fi
454   elif [[ "$1" = *\ tar* || "$1" = *\   tar* ]]; then
455     msg "use tar_file${sep}contained_file to view a file in the archive"
456     if [[ -n $COLOR ]] && cmd_exist tarcolor; then
457       $tarcmd tvf "$2" | tarcolor
458     else
459       $tarcmd tvf "$2"
460     fi
461   elif [[ "$1" = *RPM* ]]; then
462     header="use RPM_file${sep}contained_file to view a file in the RPM"
463     if cmd_exist rpm; then
464       echo $header
465       istemp "rpm -qivp" "$2"
466       header="";
467     fi
468     if cmd_exist cpio && cmd_exist rpm2cpio; then
469       echo $header
470       echo "================================= Content ======================================"
471       istemp rpm2cpio "$2" 2>/dev/null|cpio -i -tv 2>/dev/null
472     elif cmd_exist cpio && cmd_exist rpmunpack; then
473       echo $header
474       echo "================================= Content ======================================"
475       cat "$2" | rpmunpack | gzip -cd | cpio -i -tv 2>/dev/null
476     else
477       msg "please install rpm2cpio or rpmunpack to see the contents of RPM files"
478     fi
479   elif [[ "$1" = *roff* ]] && cmd_exist groff; then
480     DEV=utf8
481     if [[ $LANG != *UTF*8* && $LANG != *utf*8* ]]; then
482       if [[ "$LANG" = ja* ]]; then
483         DEV=nippon
484       else
485         DEV=latin1
486       fi
487     fi
488     MACRO=andoc
489     if [[ "$2" = *.me ]]; then
490       MACRO=e
491     elif [[ "$2" = *.ms ]]; then
492       MACRO=s
493     fi
494     msg "append $sep to filename to view the nroff source"
495     groff -s -p -t -e -T$DEV -m$MACRO "$2"
496   elif [[ "$1" = *Debian* ]]; then
497     msg "use Deb_file${sep}contained_file to view a file in the Deb"
498     if cmd_exist dpkg; then
499       nodash "dpkg -I" "$2"
500     else
501       echo
502       istemp "ar p" "$2" control.tar.gz | gzip -dc - | $tarcmd tvf - | sed -r 's/(.{48})\./\1control/'
503     fi
504     echo
505     istemp "ar p" "$2" data.tar.gz | gzip -dc - | $tarcmd tvf -
506   # do not display all perl text containing pod using perldoc
507   #elif [[ "$1" = *Perl\ POD\ document\ text* || "$1" = *Perl5\ module\ source\ text* ]]; then
508   elif [[ "$1" = *Perl\ POD\ document\ text* ]] && cmd_exist perldoc; then
509     msg "append $sep to filename to view the perl source"
510     istemp perldoc "$2"
511   elif [[ "$1" = *\ script* ]]; then
512     set "plain text" "$2"
513   elif [[ "$1" = *text\ executable* ]]; then
514     set "plain text" "$2"
515   elif [[ "$1" = *PostScript* ]]; then
516     if cmd_exist pstotext; then
517       msg "append $sep to filename to view the postscript file"
518       nodash pstotext "$2"
519     elif cmd_exist ps2ascii; then
520       msg "append $sep to filename to view the postscript file"
521       istemp ps2ascii "$2"
522     else
523       msg "install pstotext or ps2ascii to view a textual representation of the file contents"
524     fi
525   elif [[ "$1" = *executable* ]]; then
526     msg "append $sep to filename to view the raw file"
527     nodash strings "$2"
528   elif [[ "$1" = *\ ar\ archive* ]]; then
529     msg "use library${sep}contained_file to view a file in the archive"
530     istemp "ar vt" "$2"
531   elif [[ "$1" = *shared* ]] && cmd_exist nm; then
532     msg "This is a dynamic library, showing the output of nm"
533     istemp nm "$2"
534   elif [[ "$1" = *Zip* || "$1" = *ZIP* ]] && cmd_exist unzip; then
535     msg "use zip_file${sep}contained_file to view a file in the archive"
536     istemp "unzip -lv" "$2"
537   elif [[ "$1" = *RAR\ archive* ]]; then
538     if cmd_exist unrar; then
539       msg "use rar_file${sep}contained_file to view a file in the archive"
540       istemp "unrar v" "$2"
541     elif cmd_exist rar; then
542       msg "use rar_file${sep}contained_file to view a file in the archive"
543       istemp "rar v" "$2"
544     fi 
545   elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7za; then
546     typeset res
547     res=$(istemp "7za l" "$2")
548     if [[ "$res" = *\ 1\ file* ]]; then
549       msg "a 7za archive containing one file was silently unpacked"
550       if [[ "$2" != - ]]; then
551         7za e -so "$2" 2>/dev/null
552       else
553         # extract name of temporary file containing the 7za archive
554         t=${res#*Listing\ archive:\ }
555         t2="
556 "
557         t=${t%%$t2*}
558         7za e -so $t 2>/dev/null
559       fi
560     else
561       msg "use 7za_file${sep}contained_file to view a file in the archive"
562       echo "$res"
563     fi
564   elif [[ "$1" = *7-zip\ archive* || "$1" = *7z\ archive* ]] && cmd_exist 7zr; then
565     typeset res
566     res=$(istemp "7zr l" "$2")
567     if [[ "$res" = *\ 1\ file* ]]; then
568       msg "a 7za archive containing one file was silently unpacked"
569       if [[ "$2" != - ]]; then
570         7zr e -so "$2" 2>/dev/null
571       else
572         # extract name of temporary file containing the 7za archive
573         t=${res#*Listing\ archive:\ }
574         t2="
575 "
576         t=${t%%$t2*}
577         7zr e -so $t 2>/dev/null
578       fi
579     else
580       msg "use 7za_file${sep}contained_file to view a file in the archive"
581       echo "$res"
582     fi
583   elif [[ "$1" = *[Cc]abinet* ]] && cmd_exist cabextract; then
584     msg "use cab_file${sep}contained_file to view a file in the cabinet"
585     istemp "cabextract -l" "$2"
586   elif [[ "$1" = *\ DVI* ]] && cmd_exist dvi2tty; then
587     msg "append $sep to filename to view the raw DVI file"
588     isdvi "$2"
589   elif [[ "$PARSEHTML" = yes && "$1" = *HTML* ]]; then
590     msg "append $sep to filename to view the HTML source"
591     parsehtml "$2"
592   elif [[ "$PARSEHTML" = yes && "$1" = *PDF* ]] && cmd_exist pdftohtml; then
593     msg "append $sep to filename to view the PDF source"
594     t=$(nexttmp)
595     cat "$2" > "$t"; pdftohtml -stdout "$t" | parsehtml -
596   elif [[ "$1" = *PDF* ]] && cmd_exist pdftotext; then
597     msg "append $sep to filename to view the PDF source"
598     istemp pdftotext "$2" -
599   elif [[ "$1" = *DjVu* ]] && cmd_exist djvutxt; then
600     msg "append $sep to filename to view the DjVu source"
601     djvutxt "$2"
602   elif [[ "$1" = *Microsoft\ Word* || "$1" = *Microsoft\ Office* ]]; then
603     if cmd_exist antiword; then
604       msg "append $sep to filename to view the raw word document"
605       antiword "$2"
606     elif cmd_exist catdoc; then
607       msg "append $sep to filename to view the raw word document"
608       catdoc "$2"
609     else
610       msg "install antiword or catdoc to view human readable text"
611       cat "$2"
612     fi
613   elif [[ "$1" = *Rich\ Text\ Format* ]]  && cmd_exist unrtf; then
614     if [[ "$PARSEHTML" = yes ]]; then
615       msg "append $sep to filename to view the RTF source"
616       istemp "unrtf --html" "$2" | parsehtml -
617     else
618       msg "append $sep to filename to view the RTF source"
619       istemp "unrtf --text" "$2" | sed -e "s/^### .*//" | fmt -s
620     fi
621   elif [[ "$PARSEHTML" = yes && "$1" = *Excel\ document* ]] && cmd_exist xlhtml; then
622     msg "append $sep to filename to view the spreadsheet source"
623     xlhtml -te "$2" | parsehtml -
624   elif [[ "$PARSEHTML" = yes && "$1" = *PowerPoint\ document* ]] && cmd_exist ppthtml; then
625     msg "append $sep to filename to view the PowerPoint source"
626     ppthtml "$2" | parsehtml -
627   elif [[ "$PARSEHTML" = yes && ("$1" = *OpenDocument\ [CHMPST]* || "$1" = *OpenOffice\.org\ 1\.x\ [CIWdgpst]*) ]] && cmd_exist unzip; then
628     if cmd_exist o3tohtml; then
629       msg "append $sep to filename to view the OpenOffice or OpenDocument source"
630       istemp "unzip -avp" "$2" content.xml | o3tohtml | parsehtml -
631     elif cmd_exist sxw2txt; then
632       msg "append $sep to filename to view the OpenOffice or OpenDocument source"
633       istemp sxw2txt "$2"
634     else
635       msg "install at least sxw2txt from the lesspipe package to see plain text in openoffice documents"
636     fi
637   elif [[ "$1" = *ISO\ 9660* ]] && cmd_exist isoinfo; then
638     if [[ "$2" != - ]]; then
639       msg "append $sep to filename to view the raw data"
640       isoinfo -d -i "$2"
641       joliet=`isoinfo -d -i "$2" | egrep '^Joliet'|cut -c1`
642       echo "================================= Content ======================================"
643       isoinfo -lR$joliet -i "$2"
644     fi
645   elif [[ "$1" = *image\ data*  || "$1" = *JPEG\ file* || "$1" = *JPG\ file* ]] && cmd_exist identify; then
646     msg "append $sep to filename to view the raw data"
647     identify -verbose "$2"
648   elif [[ "$1" = *MPEG\ *layer\ 3\ audio* || "$1" = *MPEG\ *layer\ III* || "$1" = *mp3\ file* || "$1" = *MP3* ]]; then
649     if cmd_exist id3v2; then
650       msg "append $sep to filename to view the raw data"
651       istemp "id3v2 -l" "$2"
652     elif cmd_exist mp3info2; then
653       msg "append $sep to filename to view the raw data"
654       mp3info2 "$2"
655     elif cmd_exist mp3info; then
656       msg "append $sep to filename to view the raw data"
657       mp3info "$2"
658     fi
659   elif [[ "$1" = *perl\ Storable* ]]; then
660     msg "append $sep to filename to view the raw data"
661     perl -MStorable=retrieve -MData::Dumper -e '$Data::Dumper::Indent=1;print Dumper retrieve shift' "$2"
662   elif [[ "$1" = *UTF-8* && $LANG != *UTF-8 ]] && cmd_exist iconv; then
663     iconv -f UTF-8 "$2"
664   elif [[ "$1" = *ISO-8859* && $LANG != *ISO-8859-1 ]] && cmd_exist iconv; then
665     iconv -f ISO-8859-1 "$2"
666   elif [[ "$1" = *UTF-16* && $LANG != *UTF-16 ]] && cmd_exist iconv; then
667     iconv -f UTF-16 "$2"
668   elif [[ "$1" = *GPG\ encrypted\ data* ]] && cmd_exist gpg; then
669     msg "append $sep to filename to view the encrypted file"
670     gpg -d "$2"
671   elif [[ "$1" = *data* ]]; then
672     msg "append $sep to filename to view the raw data"
673     nodash strings "$2"
674   else
675     set "plain text" "$2"
676   fi
677   if [[ "$1" = *plain\ text* ]]; then
678     if cmd_exist code2color; then
679       code2color $PPID ${in_file:+"$in_file"} "$2"
680       if [[ $? = 0 ]]; then
681         return
682       fi
683     fi
684   fi
685   if [[ "$2" = - ]]; then
686     cat
687   fi  
688 }
689
690 IFS=$sep a="$@"
691 IFS=' '
692 if [[ "$a" = "" ]]; then
693   if [[ "$0" != /* ]]; then
694      pat=`pwd`/
695   fi
696   if [[ "$SHELL" = *csh ]]; then
697     echo "setenv LESSOPEN \"|$pat$0 %s\""
698   else
699     echo "LESSOPEN=\"|$pat$0 %s\""
700     echo "export LESSOPEN"
701   fi
702 else
703   # check for pipes so that "less -f ... <(cmd) ..." works properly
704   [[ -p "$1" ]] && exit 1
705   show "$a"
706 fi