| 1 | package Term::Completion::_stty; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | use base qw(Term::Completion::_termsize); | ||
| 6 | |||
| 7 | # initialize the stty program | ||
| 8 | our @PATH; | ||
| 9 | unless(@PATH) { | ||
| 10 | @PATH = qw(/bin /usr/bin); | ||
| 11 | } | ||
| 12 | our ($stty, $tty_raw_noecho, $tty_restore); | ||
| 13 | foreach my $p (@PATH) { | ||
| 14 | my $s = "$p/stty"; | ||
| 15 | if (-x $s) { | ||
| 16 | $stty = $s; | ||
| 17 | $tty_raw_noecho = "$s raw -echo"; | ||
| 18 | $tty_restore = "$s -raw echo"; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | unless(defined $stty) { | ||
| 22 | croak "Cannot initialize ".__PACKAGE__.", no stty executable found in @PATH"; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub set_raw_tty | ||
| 26 | { | ||
| 27 | my __PACKAGE__ $this = shift; | ||
| 28 | |||
| 29 | # Attempt to save the current stty state, to be restored later | ||
| 30 | if (defined $stty && !defined $this->{tty_saved_state}) { | ||
| 31 | $this->{tty_saved_state} = qx($stty -g 2>/dev/null); | ||
| 32 | if ($?) { | ||
| 33 | # stty -g not supported | ||
| 34 | $this->{tty_saved_state} = undef; | ||
| 35 | } else { | ||
| 36 | $this->{tty_saved_state} =~ s/\s+$//g; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | # run stty to set the mode | ||
| 41 | system $tty_raw_noecho if defined $tty_raw_noecho; | ||
| 42 | 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub reset_tty | ||
| 46 | { | ||
| 47 | my __PACKAGE__ $this = shift; | ||
| 48 | # do our best to restore the terminal state | ||
| 49 | if (defined $this->{tty_saved_state}) { | ||
| 50 | # use the saved state | ||
| 51 | system qq($stty '$this->{tty_saved_state}' 2>/dev/null); | ||
| 52 | if ($?) { | ||
| 53 | # error - fall back | ||
| 54 | system $tty_restore; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | elsif(defined $tty_restore) { | ||
| 58 | system $tty_restore; | ||
| 59 | } | ||
| 60 | 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub get_key | ||
| 64 | { | ||
| 65 | my __PACKAGE__ $this = shift; | ||
| 66 | getc($this->{in}); | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |