1 # Copyright (c) 2010 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19 package Amanda::Interactivity::tty;
23 use POSIX qw( :errno_h );
25 @ISA = qw( Amanda::Interactivity );
29 use Amanda::Debug qw( debug );
31 use Amanda::MainLoop qw( :GIOCondition );
35 Amanda::Interactivity::tty -- Interactivity class to read user request from /dev/tty
39 Amanda::Interactivity class to write user request on /dev/tty and read reply
52 my $r = open($input, '<', "/dev/tty");
54 $abort_message = "Failed to open /dev/tty: $!";
56 $r = open($output, '>', "/dev/tty");
58 $abort_message = "Failed to open /dev/tty: $!";
67 property => $property,
68 abort_message => $abort_message,
70 return bless ($self, $class);
76 close($self->{'input'}) if $self->{'input'};
77 close($self->{'output'}) if $self->{'output'};
83 if ($self->{'input_src'}) {
84 $self->{'input_src'}->remove();
85 $self->{'input_src'} = undef;
94 my $message = $params{'message'};
95 my $label = $params{'label'};
96 my $new_volume = $params{'new_volume'};
97 my $err = $params{'err'};
98 my $chg_name = $params{'chg_name'};
102 my $n_read = POSIX::read(fileno($self->{'input'}), $b, 1);
103 if (!defined $n_read) {
104 return if ($! == EINTR);
106 return $params{'request_cb'}->(
107 Amanda::Changer::Error->new('fatal',
108 message => "Fail to read from /dev/tty"));
109 } elsif ($n_read == 0) {
111 return $params{'request_cb'}->(
112 Amanda::Changer::Error->new('fatal',
113 message => "Aborted by user"));
121 return $params{'request_cb'}->(undef, $line);
126 if ($self->{'abort_message'}) {
127 return $params{'request_cb'}->(
128 Amanda::Changer::Error->new('fatal',
129 message => $self->{'abort_message'}));
132 print {$self->{'output'}} "$err\n";
133 if ($label && $new_volume) {
134 print {$self->{'output'}} "Insert volume labeled '$label' or a new volume in $chg_name\n";
136 print {$self->{'output'}} "Insert volume labeled '$label' in $chg_name\n";
138 print {$self->{'output'}} "Insert a new volume in $chg_name\n";
140 print {$self->{'output'}} "and press <enter> enter, or ^D to abort.\n";
142 $self->{'input_src'} = Amanda::MainLoop::fd_source(
143 fileno($self->{'input'}),
144 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
145 $self->{'input_src'}->set_callback($data_in);