#!/bin/bash #================================================================== # lesspipe.sh, a preprocessor for less # Author: Wolfgang Friebel DESY Zeuthen (Wolfgang.Friebel@desy.de) #================================================================== tarcmd=gtar; filecmd='file -L'; # file (recommended file-3.27 or better) sep=: # file name separator altsep== # alternate separator character if [[ -f "$1" && "$1" = *$sep* || "$1" = *$altsep ]]; then sep=$altsep fi tmp=/tmp/.lesspipe.$$ # temp file name trap 'rm -f $tmp $tmp.fin $tmp. $tmp.. $tmp.1' 0 trap PIPE show () { file1=${1%%$sep*} rest1=${1#$file1} rest11=${rest1#$sep} file2=${rest11%%$sep*} rest2=${rest11#$file2} rest11=$rest1 if [[ $# = 1 ]]; then type=`$filecmd "$file1" | cut -d : -f 2-` get_cmd "$type" "$file1" $rest1 if [[ "$cmd" != "" ]]; then show "-$rest1" "$cmd" else isfinal "$type" "$file1" $rest11 fi elif [[ $# = 2 ]]; then type=`$2 | $filecmd - | cut -d : -f 2-` get_cmd "$type" "$file1" $rest1 if [[ "$cmd" != "" ]]; then show "-$rest1" "$2" "$cmd" else $2 | isfinal "$type" - $rest11 fi elif [[ $# = 3 ]]; then type=`$2 | $3 | $filecmd - | cut -d : -f 2-` get_cmd "$type" "$file1" $rest1 if [[ "$cmd" != "" ]]; then show "-$rest1" "$2" "$3" "$cmd" else $2 | $3 | isfinal "$type" - $rest11 fi elif [[ $# = 4 ]]; then type=`$2 | $3 | $4 | $filecmd - | cut -d : -f 2-` get_cmd "$type" "$file1" $rest1 if [[ "$cmd" != "" ]]; then echo "$0: Too many levels of encapsulation" else $2 | $3 | $4 | isfinal "$type" - $rest11 fi fi } get_cmd () { cmd= if [[ "$1" = *bzip* || "$1" = *compress* ]]; then if [[ "$3" = $sep$sep ]]; then return elif [[ "$1" = *bzip* ]]; then cmd="bzip2 -c -d $2" else cmd="gzip -c -d $2" fi return fi rest1=$rest2 if [[ "$file2" != "" ]]; then if [[ "$1" = *tar* ]]; then cmd="$tarcmd Oxf $2 $file2" elif [[ "$1" = *RPM* ]]; then cmd="isrpm $2 $file2" elif [[ "$1" = *Zip* ]]; then cmd="iszip $2 $file2" fi fi } iszip () { if [[ "$1" = - ]]; then rm -f $tmp cat > $tmp set $tmp "$2" fi unzip -avp "$1" "$2" } isrpm () { if [[ "$1" = - ]]; then rm -f $tmp cat > $tmp set $tmp "$2" fi echo $tmp.1 > $tmp. rm -f $tmp.1 rpm2cpio $1|cpio -i --quiet --rename-batch-file $tmp. ${2##/} cat $tmp.1 } isfinal() { if [[ "$3" = $sep || "$3" = $sep$sep ]]; then cat $2 return elif [[ "$2" = - && ( "$1" = *RPM* || "$1" = *Zip* ) ]]; then cat > $tmp.fin set "$1" $tmp.fin fi if [[ "$1" = *No\ such* ]]; then return elif [[ "$1" = *directory* ]]; then echo "==> This is a directory, showing the output of ls -lAL" ls -lAL "$2" elif [[ "$1" = *tar* ]]; then echo "==> use tar_file${sep}contained_file to view a file in the archive" $tarcmd tvf "$2" elif [[ "$1" = *RPM* ]]; then echo "==> use RPM_file${sep}contained_file to view a file in the RPM" rpm -p "$2" -qiv rpm2cpio $2|cpio -i -tv --quiet elif [[ "$1" = *roff* ]]; then echo "==> append $sep to filename to view the nroff source" groff -s -p -t -e -Tascii -mandoc ${2#-} elif [[ "$1" = *executable* ]]; then echo "==> append $sep to filename to view the binary file" strings ${2#-} elif [[ "$1" = *Zip* ]]; then echo "==> use zip_file${sep}contained_file to view a file in the archive" unzip -lv "$2" elif [[ "$1" = *HTML* ]]; then echo "==> append $sep to filename to view the HTML source" LESSOPEN= w3m -dump -X -T text/html "$2" elif [[ "$2" = - ]]; then cat fi } show "$a"