2010-03-17 07:36:54 +01:00
|
|
|
package PBot::StdinReader;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use vars qw($VERSION);
|
|
|
|
|
|
|
|
$VERSION = '1.0.0';
|
|
|
|
|
|
|
|
use IO::Select;
|
|
|
|
use POSIX qw(tcgetpgrp getpgrp); # to check whether process is in background or foreground
|
|
|
|
|
|
|
|
# used to listen for STDIN in non-blocking mode
|
|
|
|
my $stdin = IO::Select->new();
|
|
|
|
$stdin->add(\*STDIN);
|
|
|
|
|
|
|
|
# used to check whether process is in background or foreground, for stdin reading
|
|
|
|
open TTY, "</dev/tty" or die $!;
|
|
|
|
my $tty_fd = fileno(TTY);
|
|
|
|
my $foreground = (tcgetpgrp($tty_fd) == getpgrp()) ? 1 : 0;
|
|
|
|
|
|
|
|
sub check_stdin {
|
|
|
|
# make sure we're in the foreground first
|
|
|
|
$foreground = (tcgetpgrp($tty_fd) == getpgrp()) ? 1 : 0;
|
|
|
|
return if not $foreground;
|
|
|
|
|
|
|
|
if ($stdin->can_read(.5)) {
|
2010-03-22 08:33:44 +01:00
|
|
|
sysread(STDIN, my $input, 1024);
|
|
|
|
chomp $input;
|
2010-03-17 07:36:54 +01:00
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|