| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::Client::ConfModule - client module for ConfModules |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
use Debconf::Client::ConfModule ':all'; |
| 10 |
version('2.0'); |
| 11 |
my $capb=capb('backup'); |
| 12 |
input("medium", "foo/bar"); |
| 13 |
my @ret=go(); |
| 14 |
if ($ret[0] == 30) { |
| 15 |
# Back button pressed. |
| 16 |
... |
| 17 |
} |
| 18 |
... |
| 19 |
|
| 20 |
=head1 DESCRIPTION |
| 21 |
|
| 22 |
This is a module to ease writing ConfModules for Debian's configuration |
| 23 |
management system. It can communicate with a FrontEnd via the debconf |
| 24 |
protocol (which is documented in full in the debconf_specification in |
| 25 |
Debian policy). |
| 26 |
|
| 27 |
The design is that each command in the protocol is represented by one |
| 28 |
function in this module (with the name lower-cased). Call the function and |
| 29 |
pass in any parameters you want to follow the command. If the function is |
| 30 |
called in scalar context, it will return any textual return code. If it is |
| 31 |
called in list context, an array consisting of the numeric return code and |
| 32 |
the textual return code will be returned. |
| 33 |
|
| 34 |
This module uses Exporter to export all functions it defines. To import |
| 35 |
everything, simply import ":all". |
| 36 |
|
| 37 |
=over 4 |
| 38 |
|
| 39 |
=cut |
| 40 |
|
| 41 |
package Debconf::Client::ConfModule; |
| 42 |
use warnings; |
| 43 |
use strict; |
| 44 |
use base qw(Exporter); |
| 45 |
|
| 46 |
# List all valid commands here. |
| 47 |
our @EXPORT_OK=qw(version capb stop reset title input beginblock endblock go |
| 48 |
unset set get register unregister clear |
| 49 |
start_frontend fset fget subst purge metaget visible exist |
| 50 |
settitle info progress data x_loadtemplatefile); |
| 51 |
|
| 52 |
# Import :all to get everything. |
| 53 |
our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
| 54 |
|
| 55 |
# Set up valid command lookup hash. |
| 56 |
my %commands; |
| 57 |
map { $commands{uc $_}=1; } @EXPORT_OK; |
| 58 |
|
| 59 |
# Unbuffered output is required. |
| 60 |
$|=1; |
| 61 |
|
| 62 |
=item import |
| 63 |
|
| 64 |
Ensure that a FrontEnd is running. It's a little hackish. If |
| 65 |
DEBIAN_HAS_FRONTEND is set, a FrontEnd is assumed to be running. |
| 66 |
If not, one is started up automatically and stdin and out are |
| 67 |
connected to it. Note that this function is always run when the |
| 68 |
module is loaded in the usual way. |
| 69 |
|
| 70 |
=cut |
| 71 |
|
| 72 |
sub import { |
| 73 |
if (! $ENV{DEBIAN_HAS_FRONTEND}) { |
| 74 |
$ENV{PERL_DL_NONLAZY}=1; |
| 75 |
if (exists $ENV{DEBCONF_USE_CDEBCONF} and |
| 76 |
$ENV{DEBCONF_USE_CDEBCONF} ne '') { |
| 77 |
exec "/usr/lib/cdebconf/debconf", $0, @ARGV; |
| 78 |
} else { |
| 79 |
exec "/usr/share/debconf/frontend", $0, @ARGV; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
# Make the Exporter still work. |
| 84 |
Debconf::Client::ConfModule->export_to_level(1, @_); |
| 85 |
|
| 86 |
# A truly gross hack. This is only needed if |
| 87 |
# /usr/share/debconf/confmodule is loaded, and then this |
| 88 |
# perl module is used. In that case, this module needs to write |
| 89 |
# to fd #3, rather than stdout. See changelog 0.3.74. |
| 90 |
if (exists $ENV{DEBCONF_REDIR} && $ENV{DEBCONF_REDIR}) { |
| 91 |
open(STDOUT, ">&", 3); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
=item stop |
| 96 |
|
| 97 |
The frontend doesn't send a return code here, so we cannot try to read it |
| 98 |
or we'll block. |
| 99 |
|
| 100 |
=cut |
| 101 |
|
| 102 |
sub stop { |
| 103 |
print "STOP\n"; |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
=item AUTOLOAD |
| 108 |
|
| 109 |
Creates handler functions for commands on the fly. |
| 110 |
|
| 111 |
=cut |
| 112 |
|
| 113 |
sub AUTOLOAD { |
| 114 |
my $command = uc our $AUTOLOAD; |
| 115 |
$command =~ s|.*:||; # strip fully-qualified portion |
| 116 |
|
| 117 |
die "Unsupported command `$command'." |
| 118 |
unless $commands{$command}; |
| 119 |
|
| 120 |
no strict 'refs'; |
| 121 |
*$AUTOLOAD = sub { |
| 122 |
my $c=join (' ', $command, @_); |
| 123 |
|
| 124 |
# Newlines in input can really badly confuse the protocol, so |
| 125 |
# detect and warn. |
| 126 |
if ($c=~m/\n/) { |
| 127 |
warn "Warning: Newline present in parameters passed to debconf.\n"; |
| 128 |
warn "This will probably cause strange things to happen!\n"; |
| 129 |
} |
| 130 |
|
| 131 |
print "$c\n"; |
| 132 |
my $ret=<STDIN>; |
| 133 |
chomp $ret; |
| 134 |
my @ret=split(/\s/, $ret, 2); |
| 135 |
if ($ret[0] eq '1') { |
| 136 |
# escaped data |
| 137 |
local $_; |
| 138 |
my $unescaped=''; |
| 139 |
for (split /(\\.)/, $ret[1]) { |
| 140 |
s/\\(.)/$1 eq "n" ? "\n" : $1/eg; |
| 141 |
$unescaped.=$_; |
| 142 |
} |
| 143 |
$ret[0]='0'; |
| 144 |
$ret[1]=$unescaped; |
| 145 |
} |
| 146 |
return @ret if wantarray; |
| 147 |
return $ret[1]; |
| 148 |
}; |
| 149 |
goto &$AUTOLOAD; |
| 150 |
} |
| 151 |
|
| 152 |
=back |
| 153 |
|
| 154 |
=head1 SEE ALSO |
| 155 |
|
| 156 |
The debconf specification |
| 157 |
(/usr/share/doc/debian-policy/debconf_specification.txt.gz). |
| 158 |
|
| 159 |
=head1 AUTHOR |
| 160 |
|
| 161 |
Joey Hess <[email protected]> |
| 162 |
|
| 163 |
=cut |
| 164 |
|
| 165 |
1 |