Imported Upstream version 3.3.3
[debian/amanda] / perl / Amanda / Interactivity / stdin.pm
1 # Copyright (c) 2010-2012 Zmanda, Inc.  All Rights Reserved.
2 #
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.
7 #
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
11 # for more details.
12 #
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
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19
20 package Amanda::Interactivity::stdin;
21
22 use strict;
23 use warnings;
24 use POSIX qw( :errno_h );
25 use vars qw( @ISA );
26 @ISA = qw( Amanda::Interactivity );
27
28 use Amanda::Paths;
29 use Amanda::Util;
30 use Amanda::Debug qw( debug );
31 use Amanda::Changer;
32 use Amanda::MainLoop qw( :GIOCondition );
33
34 =head1 NAME
35
36 Amanda::Interactivity::stdin -- Interactivity class to read user request from stdin
37
38 =head1 SYNOPSIS
39
40 Amanda::Interactivity class to write user request on stdout and read reply
41 from stdin.
42
43 =cut
44
45 sub new {
46     my $class = shift;
47
48     my $self = {
49         input_src => undef,
50     };
51     return bless ($self, $class);
52 }
53
54 sub abort {
55     my $self = shift @_;
56
57     if ($self->{'input_src'}) {
58         $self->{'input_src'}->remove();
59         $self->{'input_src'} = undef;
60     }
61 }
62
63 sub user_request {
64     my $self = shift @_;
65     my %params = @_;
66     my $buffer = "";
67
68     my $message  = $params{'message'};
69     my $label    = $params{'label'};
70     my $err      = $params{'err'};
71     my $chg_name = $params{'chg_name'};
72
73     my $data_in = sub {
74         my $b;
75         my $n_read = POSIX::read(0, $b, 1);
76         if (!defined $n_read) {
77             return if ($! == EINTR);
78             $self->abort();
79             return $params{'request_cb'}->(
80                         Amanda::Changer::Error->new('fatal',
81                                 message => "Fail to read from stdin"));
82         } elsif ($n_read == 0) {
83             $self->abort();
84             return $params{'request_cb'}->(
85                         Amanda::Changer::Error->new('fatal',
86                                 message => "Aborted by user"));
87         } else {
88             $buffer .= $b;
89             if ($b eq "\n") {
90                 my $line = $buffer;
91                 chomp $line;
92                 $buffer = "";
93                 $self->abort();
94                 return $params{'request_cb'}->(undef, $line);
95             }
96         }
97     };
98
99     print "$err\n";
100     print "Insert volume labeled '$label' in $chg_name\n";
101     print "and press <enter> enter, or ^D to abort.\n";
102
103     $self->{'input_src'} = Amanda::MainLoop::fd_source(0, $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
104     $self->{'input_src'}->set_callback($data_in);
105     return;
106 }
107
108 1;