1 # Copyright (c) 2010-2012 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20 package Amanda::Interactivity::tty;
24 use POSIX qw( :errno_h );
26 @ISA = qw( Amanda::Interactivity );
30 use Amanda::Debug qw( debug );
32 use Amanda::MainLoop qw( :GIOCondition );
36 Amanda::Interactivity::tty -- Interactivity class to read user request from /dev/tty
40 Amanda::Interactivity class to write user request on /dev/tty and read reply
53 my $r = open($input, '<', "/dev/tty");
55 $abort_message = "Failed to open /dev/tty: $!";
57 $r = open($output, '>', "/dev/tty");
59 $abort_message = "Failed to open /dev/tty: $!";
68 property => $property,
69 abort_message => $abort_message,
71 return bless ($self, $class);
77 close($self->{'input'}) if $self->{'input'};
78 close($self->{'output'}) if $self->{'output'};
84 if ($self->{'input_src'}) {
85 $self->{'input_src'}->remove();
86 $self->{'input_src'} = undef;
95 my $message = $params{'message'};
96 my $label = $params{'label'};
97 my $new_volume = $params{'new_volume'};
98 my $err = $params{'err'};
99 my $chg_name = $params{'chg_name'};
103 my $n_read = POSIX::read(fileno($self->{'input'}), $b, 1);
104 if (!defined $n_read) {
105 return if ($! == EINTR);
107 return $params{'request_cb'}->(
108 Amanda::Changer::Error->new('fatal',
109 message => "Fail to read from /dev/tty"));
110 } elsif ($n_read == 0) {
112 return $params{'request_cb'}->(
113 Amanda::Changer::Error->new('fatal',
114 message => "Aborted by user"));
122 return $params{'request_cb'}->(undef, $line);
127 if ($self->{'abort_message'}) {
128 return $params{'request_cb'}->(
129 Amanda::Changer::Error->new('fatal',
130 message => $self->{'abort_message'}));
133 print {$self->{'output'}} "$err\n";
134 if ($label && $new_volume) {
135 print {$self->{'output'}} "Insert volume labeled '$label' or a new volume in $chg_name\n";
137 print {$self->{'output'}} "Insert volume labeled '$label' in $chg_name\n";
139 print {$self->{'output'}} "Insert a new volume in $chg_name\n";
141 print {$self->{'output'}} "and press <enter> enter, or ^D to abort.\n";
143 $self->{'input_src'} = Amanda::MainLoop::fd_source(
144 fileno($self->{'input'}),
145 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
146 $self->{'input_src'}->set_callback($data_in);