rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* zmore with rc
@ 1991-12-19 18:10 Brendan Kehoe
  0 siblings, 0 replies; only message in thread
From: Brendan Kehoe @ 1991-12-19 18:10 UTC (permalink / raw)
  To: rc


Any suggestions or comments on style/whatever before I post this?
This is my first "real" rc script, so I don't wanna make any
horrendous gaffes before trying to convince others to use it. :)

-- cut --
#!/bin/rc
#
# zmore - my even more souped-up version of the zmore script that
# comes with the compress(1) distribution.
#
# usage: zmore [name ...]
#
# This version's based on DaviD W. Sanderson's script, and has these
# features (those with *'s are added with this version):
#
# a)  knows about $PAGER (but defaults to "more")
# b)  knows about more kinds of compressed files:
#     foo.Z, foo.F, foo.z (and it's easy to add more)
# c)* will read files that're compressed in more than one way (e.g.
#     it'll read foo.Z then foo.F then...)
# d)  works on both SysV and BSD systems
# e)  is (marginally) commented...have the rc manpage nearby if you're
#     not comfortable with it
# f)  written with rc :)
#
extensions = (.Z .F .z)

# Note I haven't checked why yet, but the Gnu stty doesn't work right
# with this---the stty of min dies.
stty=/bin/stty

# Restore the tty on exit
fn exit {
  if ( ! { ~ $icanon () } ) $stty $icanon
  builtin exit
}

# Prompt for the next file
fn prompt_next {
 if ( ! { ~ $icanon () } ) {
   echo -n (Next file: $f)

   if ( ! { ~ $cbreak () } ) $stty $cbreak
   answer = `{dd bs'='1 count'='1 >[2] /dev/null}
   if ( ! { ~ $icanon () } ) $stty $icanon

   echo ' '
   switch ($answer) {
      case [eEqQ]
	builtin exit
   }
 }
}

fn read_file {
  if ( ! { ~ $icanon () } ) echo ------'>' $f '<'------

  # There must be a better way to do this; I was fooling around with
  # something like:
  # readers=(zcat fcat pcat); list=(1 2 3)
  # Then somehow using $extensions^' '^$list then a shift, have it
  # give you the number from list (being foo) to use for $readers($foo).
  # I gave up after a little while.

  switch ( $f ) {
    case *Z
      eval zcat $f $page
    case *F
      eval fcat $f $page
    case *z
      eval pcat $f $page
    case *
      eval $PAGER $f
  }
}

# If we're on a terminal, get ourselves set to ask for a single char
# using cbreak mode.
if ( test -t 1 ) {
 if ( ! { ~ `{ $stty -a >> /dev/null >[2=1]} () } ) {
   icanon = `{ $stty -g}
   cbreak = (-icanon -echo -echoe -echok -echonl min ^A time ^@)
 } else {
   # probably a BSD-based tty system
   icanon = (-cbreak echo)
   cbreak = (cbreak -echo)
 }
}

# Figure out what pager to use, then do a little trickery.
if ( ~ $PAGER "" ) { PAGER=more }
page='| '^$PAGER

# First check if we were given a file with an explicit extension we
# know about; if it has one, don't bother searching through the rest.
# This loses some of the functionality we tried to add, but it saves
# a lot in speed.
got = junk
for (file) {
 if ( ~ $file *^$extensions ) {
   f = $file
   if ( ~ $got () ) prompt_next
   read_file
 } else {
   for ( f in $file^$extensions ) {
     if ( test -f $f ) {
       if ( ~ $got () ) prompt_next
       got = 1
       read_file
     }
   }

   if ( ~ $got () )
     echo ------'>' $file '<'------ '('cannot find')'
 }

 got = ()
}
-- cut --

--
Brendan Kehoe, Sun Network Manager                      brendan@cs.widener.edu
Widener University                                                 Chester, PA
     You can help encourage creativity---and stop those who want to stifle it.
        Join the League for Programming Freedom: write league@prep.ai.mit.edu.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1991-12-19 18:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-12-19 18:10 zmore with rc Brendan Kehoe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).