[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: zless?



(Moving from policy to devel, hopefully with enough context for those
who missed the start.)

On Wed, 2004-02-18 at 12:36, Bill Allombert wrote:
> On Wed, Feb 18, 2004 at 09:41:37AM -0600, Joe Wreschnig wrote:
> > On Tue, 2004-02-17 at 23:22, Bernd S. Brentrup wrote:
> > > On Tue, Feb 17, 2004 at 11:15:36PM -0600, Joe Wreschnig wrote:
> > > > On Tue, 2004-02-17 at 11:19, Douglas F. Calvert wrote:
> > > > > Hello,
> > > > >  I did not find a definitive answer to my question by scanning
> > > > > debian-policy/* but I may have missed something. Is it a problem that
> > > > > zless is installed in /bin but requires less which is installed in /usr
> > > > > and is not an essential package or a dependancy of gzip?	
> > > > 
> > > > The best idea might be to merge zless with zmore, or use the same script
> > > > (except with a default $PAGER of less instead of more). Personally I'd
> > > > want zless to start $PAGER anyway.
> > > 
> > > Partially seconded but call it zpager then.
> > 
> > zmore already uses ${PAGER-more}. I see no reason why zless shouldn't
> > use ${PAGER-less}.
> > 
> > I'll write a script this afternoon that should handle both cases. But
> > this is less of a policy issue at this point, and more of "file a bug on
> > gzip" issue.
> 
> FWIW, zless script has changed completly between woody and sid.

Yeah. The new version (in sid) is much better; using LESSOPEN lets you
use less's internal multiple buffer support, rather than zmore's
(one-way) buffer switching.

Philippe Troin <phil@fifi.org> wrote:
> While we're at it, can we make it handle bz* as well?

Yes, with the caveats I mention below.

Attached is a script that should be able to replace bzless, bzmore,
zless, and zmore.

1. It looks at $0 to decide whether to use bzip2 or gzip. It can only
use one of them, though, so you can't mix gzip and bzip2 files on the
command line. While that might be possible, it would mean I couldn't use
LESSOPEN (or, would have have another 'unzip-smartly' script that less
could use). I prefer having proper less buffers to being able to mix
command lines.

2. It looks at $0 to decide if it should use less, more, or pager (any
name that doesn't end in less or more, results in pager being used).
Setting $PAGER overrides all of these.

3. If $PAGER is invalid (can't be executed), it gives up. Though if it's
called as (b)zless and less isn't available (and $PAGER is not less), it
will fall back to more. So it's safe to put in /bin, even as zless.

So, the question is, where do we put this? :) debianutils might be good.
It only depends on a valid pager (and more is essential). It doesn't
bother checking for gzip either, because that's essential, but will spit
out a useful error if bz* is used and bzip2 isn't available.

This also relies on removing zless, zmore, bzless, and bzmore from the
gzip and bzip2 packages, and replacing them with symlinks to this
script.

Of course, I'm sure there's at least one stupid bug in this. Testing is
appreciated.

(Regardless of whether this script gets used or not, the fact that
/bin/zless calls /usr/bin/less explicitly does need to be fixed. I just
think this is a good way.)
-- 
Joe Wreschnig <piman@debian.org>
#!/bin/sh

# Wrapper for less and more that uncompresses files.
# Adapted from bzless by Joe Wreschnig <piman@debian.org> (which was in turn
# adapted from zmore by Philippe Troin <phil@fifi.org>) for Debian GNU/Linux.

# This doesn't allow full support for decompression because it doesn't
# scan the filenames to see which are bzip2 and which are gzip. Using
# the LESSOPEN optimization makes this more difficult to do, too.

PATH="/usr/bin:$PATH"; export PATH

self=`echo $0 | sed 's|.*/||'`
case "$self" in
    *less) more="less"  ;;
    *more) more="more"  ;;
    *)     more="pager" ;;
esac

case "$self" in
    bz*)
	if ! which bzip2; then
	    echo "E: you need to install bzip2 to view .bz2 files"
	    exit 1
	fi
	decomp="bzip2 -cdfq"
	;;
    z*) decomp="gzip -cdfq"  ;;
    *)  decomp="cat"         ;;
esac

# FIXME: Should we also make sure that $PAGER exists and fall back to more
# if it doesn't, or make the user fix their broken environment?
if ! which $more > /dev/null; then more="more" ; fi
pager=${PAGER-$more}

case "$pager" in
    *less)
	LESSOPEN="| $decomp %s"; export LESSOPEN
	exec $pager "$@"
esac

if test "`echo -n a`" = "-n a"; then
    # looks like a SysV system:
    n1=''; n2='\c'
else
    n1='-n'; n2=''
fi
oldtty=`stty -g 2>/dev/null`
if stty -cbreak 2>/dev/null; then
    cb='cbreak'; ncb='-cbreak'
else
    # 'stty min 1' resets eof to ^a on both SunOS and SysV!
    cb='min 1 -icanon'; ncb='icanon eof ^d'
fi

if test $? -eq 0 -a -n "$oldtty"; then
    trap 'stty $oldtty 2>/dev/null; exit' 0 2 3 5 10 13 15
else
    trap 'stty $ncb echo 2>/dev/null; exit' 0 2 3 5 10 13 15
fi

if test $# = 0; then
    if test -t 0; then
	echo usage: $0 files...
    else
	$decomp | eval $pager
    fi
else
    FIRST=1
    for FILE; do
	if test $FIRST -eq 0; then
	  echo $n1 "--More--(Next file: $FILE)$n2"
	  stty $cb -echo 2>/dev/null
	  ANS=`dd bs=1 count=1 2>/dev/null` 
	  stty $ncb echo 2>/dev/null
	  echo " "
	  if test "$ANS" = 'e' -o "$ANS" = 'q'; then
	      exit
	  fi
      fi
      if test "$ANS" != 's'; then
	  echo "------> $FILE <------"
	  $decomp "$FILE" | eval $pager
      fi
      if test -t; then
	  FIRST=0
      fi
    done
fi

Attachment: signature.asc
Description: This is a digitally signed message part


Reply to: