: Subject says it all. I just want to take input from the
: keyboard as soon as it's typed. I'm trying to make a pager
: that takes both file arguments and piped data and that
: pronpts you like "more" and requires only one key-press
: (no <return> needed) to continue. I can't use the "stty"
: command because it won't work when I do something like:
: % cat somefile | mypager.pl
How about:
system 'stty -icannon -echo </dev/tty';
?
--
Tony Cook - to...@online.tmx.com.au
10023...@compuserve.com
% cat somefile | mypager.pl
I've tried setting $/ to various things and doing a read(STDIN,$in,1);
but it doesn't want to work. Suggestions? Has anyone written a pager
or perl script that behaves like this?
-John
You do have you use stty, and there's nothing wrong with that.
Or you could go get the ReadKey module.
--tom
--
Tom Christiansen Perl Consultant, Gamer, Hiker tch...@mox.perl.com
"Make is like Pascal: everybody likes it, so they go in and change it. "
--Dennis Ritchie
Regards.
#
# This module contains subroutines which set and work with
# non-blocking tty mode on either Solaris or BSD systems
#
sub SetNonBlockingMode
{
local($ostype, $termio, $termio_t, $sgttyb_t, $sgttyb, @ioctl_array);
if (($ostype = $ENV{'OSTYPE'}) eq "solaris")
{
# set non-blocking I/O for solaris
require "sys/termio.ph";
require "sys/ioctl.ph";
$termio_t = "SSSSCcccccccc";
ioctl(STDIN, &TCGETA, $termio) || die "Can't ioctl with TCGETA: $!";
@ioctl_array = unpack($termio_t, $termio);
$ioctl_array[3] &= ~&ECHO;
$ioctl_array[3] &= ~&ICANON;
$ioctl_array[8] = 1;
$ioctl_array[9] = 0;
$termio = pack($termio_t, @ioctl_array);
ioctl(STDIN, &TCSETA, $termio) || die "Can't ioctl with TCSETA: $!";
}
elsif ($ostype eq '')
{
# set non-blocking I/O for BSD
require "ioctl.pl";
$sgttyb_t = "c4 s";
ioctl(STDIN, $TIOCGETP, $sgttyb) || die "Can't ioctl with TIOCGETP: $!";
@ioctl_array = unpack($sgttyb_t, $sgttyb);
@ioctl_array[4] |= $CBREAK;
@ioctl_array[4] &= ~$ECHO;
$sgttyb = pack($sgttyb_t, @ioctl_array);
ioctl(STDIN, $TIOCSETP, $sgttyb) || die "Can't ioctl with TIOCSETP: $!";
}
else
{
die "The value of the environmental variable OSTYPE is unexpectedly \"$ostype\".\n";
}
}
sub UnsetNonBlockingMode
{
local($ostype, $termio_t, $termio, $sgttyb_t, $sgttyb, @ioctl_array);
if (($ostype = $ENV{'OSTYPE'}) eq "solaris")
{
# clear non-blocking I/O for solaris
require "sys/termio.ph";
require "sys/ioctl.ph";
$termio_t = "SSSSCcccccccc";
ioctl(STDIN, &TCGETA, $termio) || die "Can't ioctl with TCGETA: $!";
@ioctl_array = unpack($termio_t, $termio);
$ioctl_array[3] |= &ECHO;
$ioctl_array[3] |= &ICANON;
$ioctl_array[8] = 1;
$ioctl_array[9] = 0;
$termio = pack($termio_t, @ioctl_array);
ioctl(STDIN, &TCSETA, $termio) || die "Can't ioctl with TCSETA: $!";
}
elsif ($ostype eq '')
{
# clear non-blocking I/O for BSD
require "ioctl.pl";
$sgttyb_t = "c4 s";
ioctl(STDIN, $TIOCGETP, $sgttyb) || die "Can't ioctl with TIOCGETP: $!";
@ioctl_array = unpack($sgttyb_t, $sgttyb);
@ioctl_array[4] &= ~$CBREAK;
@ioctl_array[4] |= $ECHO;
$sgttyb = pack($sgttyb_t, @ioctl_array);
ioctl(STDIN, $TIOCSETP, $sgttyb) || die "Can't ioctl with TIOCSETP: $!";
}
else
{
die "The value of the environmental variable OSTYPE is unexpectedly \"$ostype\".\n";
}
}
# The following subroutine returns the current keystroke if one
# has been pressed, otherwise it returns an UNDEF
sub Keystroke
{
local($rin, $rout, $Keystroke) = ("", "", "");
vec($rin, fileno(STDIN), 1) = 1;
if (select($rout=$rin, undef, undef, 0))
{
sysread(STDIN, $Keystroke, 1);
}
$Keystroke;
}
sub WaitForAnyKey
{
while (&Keystroke eq "")
{
select (undef, undef, undef, 0.01);
}
}
1;
--
Richard Baumann (805) 447-8391
International Clinical Safety
AMGEN
---------------------------------------------------------------
The opinions expressed herein are strictly those of the author.
The facts, however, are irrefutable.