| 1 | package Term::Completion::_POSIX; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use POSIX qw(:termios_h); | ||
| 5 | use base qw(Term::Completion::_termsize); | ||
| 6 | |||
| 7 | # we use POSIX termios to set the 'raw' tty properties | ||
| 8 | |||
| 9 | sub set_raw_tty | ||
| 10 | { | ||
| 11 | my __PACKAGE__ $this = shift; | ||
| 12 | |||
| 13 | # check if is a TTY | ||
| 14 | return unless -t $this->{in}; | ||
| 15 | |||
| 16 | my $fd = fileno($this->{in}); | ||
| 17 | my $termios = ($this->{_termios} ||= POSIX::Termios->new($fd)); | ||
| 18 | |||
| 19 | # now we want 'raw' mode with echo off | ||
| 20 | # according to IO::Stty this is in detail: | ||
| 21 | # -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr | ||
| 22 | # -icrnl -ixon -ixoff -opost -isig -icanon min 1 time 0 -echo | ||
| 23 | |||
| 24 | # according to Solaris' stty: | ||
| 25 | # cs8 -icanon min 1 time 0 -isig -xcase -inpck -opost -echo | ||
| 26 | |||
| 27 | # Linux? | ||
| 28 | my $set_ccflags = CS8; | ||
| 29 | my $unset_ciflags = IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF; | ||
| 30 | my $unset_clflags = ISIG | ICANON | ECHO; | ||
| 31 | my $unset_coflags = OPOST; | ||
| 32 | |||
| 33 | # get & save the original values: | ||
| 34 | $termios->getattr(); | ||
| 35 | my $c_cflag = $this->{_tty_cflag} = $termios->getcflag; | ||
| 36 | my $c_iflag = $this->{_tty_iflag} = $termios->getiflag; | ||
| 37 | my $c_lflag = $this->{_tty_lflag} = $termios->getlflag; | ||
| 38 | my $c_oflag = $this->{_tty_oflag} = $termios->getoflag; | ||
| 39 | $this->{_tty_vmin} = $termios->getcc(VMIN); | ||
| 40 | $this->{_tty_vtime} = $termios->getcc(VTIME); | ||
| 41 | |||
| 42 | # now set the values | ||
| 43 | $c_cflag |= $set_ccflags; | ||
| 44 | $c_iflag &= ~$unset_ciflags; | ||
| 45 | $c_lflag &= ~$unset_clflags; | ||
| 46 | $c_oflag &= ~$unset_coflags; | ||
| 47 | |||
| 48 | $termios->setcflag($c_cflag); | ||
| 49 | $termios->setiflag($c_iflag); | ||
| 50 | $termios->setlflag($c_lflag); | ||
| 51 | $termios->setoflag($c_oflag); | ||
| 52 | $termios->setcc(VMIN,1); # 1 | ||
| 53 | $termios->setcc(VTIME,0); # 0 | ||
| 54 | $termios->setattr($fd, TCSANOW); | ||
| 55 | 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub reset_tty | ||
| 59 | { | ||
| 60 | my __PACKAGE__ $this = shift; | ||
| 61 | return unless -t $this->{in}; | ||
| 62 | my $fd = fileno($this->{in}); | ||
| 63 | my $termios = delete $this->{_termios}; | ||
| 64 | $termios->setcflag($this->{_tty_cflag}); | ||
| 65 | $termios->setiflag($this->{_tty_iflag}); | ||
| 66 | $termios->setlflag($this->{_tty_lflag}); | ||
| 67 | $termios->setoflag($this->{_tty_oflag}); | ||
| 68 | $termios->setcc(VMIN, $this->{_tty_vmin}); | ||
| 69 | $termios->setcc(VTIME, $this->{_tty_vtime}); | ||
| 70 | $termios->setattr($fd, TCSANOW); | ||
| 71 | delete $this->{grep(/^_tty_/, keys %$this)}; | ||
| 72 | 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub get_key | ||
| 76 | { | ||
| 77 | my __PACKAGE__ $this = shift; | ||
| 78 | getc($this->{in}); | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |