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