1#! /bin/bash 2# 3# original from: 4# 5# @(#) p.ksh 1.1 93/11/09 6# p: page compressed & plain files in the order given 7# 92/01/23 john h. dubois iii (john@armory.com) 8# 92/02/14 changed incorrect zpack to pcat 9# 92/02/16 added help 10# 92/10/11 search for file.Z and file.z if file not found 11# 92/10/18 pass options to pager 12# 93/11/09 Understand gzipped files too 13# Wait after printing message about unreadable files 14# Make less prompt include name of file being uncompressed 15# 16# conversion to bash v2 by Chet Ramey; renamed to pf 17# 18DefPager=/local/bin/less 19 20istrue() 21{ 22 test 0 -ne "$1" 23} 24 25warn() 26{ 27 echo "$@" 1>&2 28} 29 30if [ "$1" = -h ]; then 31 echo \ 32"$0: page a file. 33Usage: $0 [pager-option ...] [filename ...] 34Files are paged by the program specified in the user's PAGER 35environment variable, or by $DefPager if PAGER is not set. 36If no filename is given, text to page is read from the standard input. 37If filenames are given, they are either paged directly, or unpacked/ 38uncompressed and then paged. Files are assumed to be in packed, compressed, 39or gzipped format if the filename ends in .Z, .z, or .gz respectively. 40If a filename that does not end in .Z, .z, or .gz is not found, it is 41searched for with one of those extensions attached. 42Each group of plain files is paged by a single instance of the pager. 43Each packed or compressed file is paged by a separate instance of the 44pager. 45Initial arguments beginning with + or - are taken to be pager options and 46are passed to each instance of the pager. 47If a pager option takes a value it should be given with the option as a 48single argument (with no space between the option and the value)." 49 exit 0 50fi 51 52# Get pager options 53while [ $# -gt 0 ]; do 54 case "$1" in 55 -*|+*) Opts="$Opts $1" ; shift;; 56 *) break;; 57 esac 58done 59 60[ -z "$PAGER" ] && PAGER=$DefPager 61 62# Read from stdin 63[ $# = 0 ] && exec $PAGER $Opts 64 65typeset -i filenum=0 badfile=0 66 67for file; do 68 if [ ! -r "$file" ]; then 69 case "$file" in 70 *.[Zz]|*.gz) 71 # Check if user specified a compressed file without giving its extension 72 for ext in Z z gz; do 73 if [ -r "$file.$ext" ]; then 74 file="$file.$ext" 75 break 76 fi 77 done;; 78 esac 79 fi 80 if [ ! -r "$file" ]; then 81 warn "$file: cannot read." 82 badfile=1 83 else 84 files[filenum]=$file 85 let filenum+=1 86 fi 87done 88 89if istrue $badfile && [ $filenum -gt 0 ]; then 90 echo -n "Press return to continue..." 1>&2 91 read 92fi 93 94unset plain 95 96for file in "${files[@]}"; do 97 case "$file" in 98 *.[zZ]|*.gz) 99 set -- Z zcat z pcat gz gzcat 100 # Find correct uncompression program 101 while [ $# -gt 0 ]; do 102 case "$file" in 103 *.$1) 104 # Page any uncompressed files so that they will be read 105 # in the correct order 106 [ ${#plain[@]} -gt 0 ] && $PAGER $Opts "${plain[@]}" 107 unset plain[*] 108 # If page is less, set the prompt to include the name of 109 # the file being uncompressed. Escape the . in the extension 110 # because less treats is specially in prompts (other dots 111 # in filenames will still be mucked with). 112 case "$PAGER" in 113 *less) Prompt="-P[${file%.$1}\\.$1] (%pb\\%)" ;; 114 *) unset Prompt ;; 115 esac 116 $2 "$file" | $PAGER "$Prompt" $Opts 117 break 118 esac 119 shift 2 120 done 121 ;; 122 *) plain[${#plain[@]}]=$file;; 123 esac 124done 125 126# Page any uncompressed files that haven't been paged yet 127[ ${#plain[@]} -gt 0 ] && exec $PAGER $Opts "${plain[@]}" 128