1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $dot_cpan $VERSION);
5 $VERSION = sprintf "%.2f", substr(q$Rev: 488 $,4)/100;
8 'commit' => "Commit changes to disk",
9 'defaults' => "Reload defaults from disk",
10 'init' => "Interactive setting of all options",
13 %keys = map { $_ => undef } qw(
14 build_cache build_dir bzip2
15 cache_metadata commandnumber_in_prompt cpan_home curl
17 ftp ftp_passive ftp_proxy
19 histfile histsize http_proxy
20 inactivity_timeout index_expire inhibit_startup_message
23 make make_arg make_install_arg make_install_make_command makepl_arg
24 mbuild_arg mbuild_install_arg mbuild_install_build_command mbuildpl_arg
25 ncftp ncftpget no_proxy pager
26 prefer_installer prerequisites_policy
27 scan_cache shell show_upload_date
32 if ($^O eq "MSWin32") {
34 mbuild_install_build_command
35 make_install_make_command
38 if (exists $CPAN::Config->{$k}) {
39 $CPAN::Frontend->mywarn("deleting previously set config variable ".
40 "'$k' => '$CPAN::Config->{$k}'");
41 delete $CPAN::Config->{$k};
46 # returns true on successful action
50 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
51 my($o,$str,$func,$args,$key_exists);
55 $self->$o(args => \@args);
58 CPAN->debug("o[$o]") if $CPAN::DEBUG;
59 unless (exists $keys{$o}) {
60 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
65 CPAN->debug("func[$func]") if $CPAN::DEBUG;
67 # Let's avoid eval, it's easier to comprehend without.
68 if ($func eq "push") {
69 push @{$CPAN::Config->{$o}}, @args;
71 } elsif ($func eq "pop") {
72 pop @{$CPAN::Config->{$o}};
74 } elsif ($func eq "shift") {
75 shift @{$CPAN::Config->{$o}};
77 } elsif ($func eq "unshift") {
78 unshift @{$CPAN::Config->{$o}}, @args;
80 } elsif ($func eq "splice") {
81 splice @{$CPAN::Config->{$o}}, @args;
84 $CPAN::Config->{$o} = [@args];
87 $self->prettyprint($o);
89 if ($o eq "urllist" && $changed) {
90 # reset the cached values
91 undef $CPAN::FTP::Thesite;
92 undef $CPAN::FTP::Themethod;
95 } elsif ($o =~ /_hash$/) {
96 @args = () if @args==1 && $args[0] eq "";
97 push @args, "" if @args % 2;
98 $CPAN::Config->{$o} = { @args };
100 $CPAN::Config->{$o} = $args[0] if defined $args[0];
101 $self->prettyprint($o);
108 my $v = $CPAN::Config->{$k};
111 if (ref $v eq "ARRAY") {
112 @report = map {"\t[$_]\n"} @$v;
114 @report = map { sprintf("\t%-18s => %s\n",
116 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
119 $CPAN::Frontend->myprint(
129 } elsif (defined $v) {
130 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
132 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
137 my($self,@args) = @_;
140 if ($args[0] eq "args") {
141 # we have not signed that contract
143 $configpm = $args[0];
146 unless (defined $configpm){
147 $configpm ||= $INC{"CPAN/MyConfig.pm"};
148 $configpm ||= $INC{"CPAN/Config.pm"};
149 $configpm || Carp::confess(q{
150 CPAN::Config::commit called without an argument.
151 Please specify a filename where to save the configuration or try
152 "o conf init" to have an interactive course through configing.
157 $mode = (stat $configpm)[2];
158 if ($mode && ! -w _) {
159 Carp::confess("$configpm is not writable");
164 $msg = <<EOF unless $configpm =~ /MyConfig/;
166 # This is CPAN.pm's systemwide configuration file. This file provides
167 # defaults for users, and the values can be changed in a per-user
168 # configuration file. The user-config file is being looked for as
169 # ~/.cpan/CPAN/MyConfig.pm.
173 my($fh) = FileHandle->new;
174 rename $configpm, "$configpm~" if -f $configpm;
175 open $fh, ">$configpm" or
176 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
177 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
178 foreach (sort keys %$CPAN::Config) {
179 unless (exists $keys{$_}) {
180 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
181 delete $CPAN::Config->{$_};
186 $self->neatvalue($CPAN::Config->{$_}),
191 $fh->print("};\n1;\n__END__\n");
194 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
195 #chmod $mode, $configpm;
196 ###why was that so? $self->defaults;
197 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
201 # stolen from MakeMaker; not taking the original because it is buggy;
202 # bugreport will have to say: keys of hashes remain unquoted and can
203 # produce syntax errors
206 return "undef" unless defined $v;
208 return "q[$v]" unless $t;
212 foreach my $elem (@$v) {
213 push @neat, "q[$elem]";
215 push @m, join ", ", @neat;
219 return "$v" unless $t eq 'HASH';
221 while (($key,$val) = each %$v){
222 last unless defined $key; # cautious programming in case (undef,undef) is true
223 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
225 return "{ ".join(', ',@m)." }";
231 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
232 CPAN::Shell->reload_this($config) and $done++;
239 my($self,@args) = @_;
240 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
249 # This is a piece of repeated code that is abstracted here for
250 # maintainability. RMB
253 my($configpmdir, $configpmtest) = @_;
254 if (-w $configpmtest) {
255 return $configpmtest;
256 } elsif (-w $configpmdir) {
257 #_#_# following code dumped core on me with 5.003_11, a.k.
258 my $configpm_bak = "$configpmtest.bak";
259 unlink $configpm_bak if -f $configpm_bak;
260 if( -f $configpmtest ) {
261 if( rename $configpmtest, $configpm_bak ) {
262 $CPAN::Frontend->mywarn(<<END);
263 Old configuration file $configpmtest
264 moved to $configpm_bak
268 my $fh = FileHandle->new;
269 if ($fh->open(">$configpmtest")) {
271 return $configpmtest;
273 # Should never happen
274 Carp::confess("Cannot open >$configpmtest");
280 my($self, %args) = @_;
281 $CPAN::Be_Silent++ if $args{be_silent};
285 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
286 eval {require CPAN::Config;}; # not everybody has one
288 unless ($dot_cpan++){
289 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
290 eval {require CPAN::MyConfig;}; # override system wide settings
293 return unless @miss = $self->missing_config_data;
295 require CPAN::FirstTime;
296 my($configpm,$fh,$redo,$theycalled);
298 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
299 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
300 $configpm = $INC{"CPAN/Config.pm"};
302 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
303 $configpm = $INC{"CPAN/MyConfig.pm"};
306 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
307 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
308 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
310 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
311 $configpm = _configpmtest($configpmdir,$configpmtest);
312 $inc_key = "CPAN/Config.pm";
315 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
316 File::Path::mkpath($configpmdir);
317 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
318 $configpm = _configpmtest($configpmdir,$configpmtest);
319 $inc_key = "CPAN/MyConfig.pm";
322 $INC{$inc_key} = $configpm;
324 my $text = qq{WARNING: CPAN.pm is unable to } .
325 qq{create a configuration file.};
326 output($text, 'confess');
331 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
332 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
333 the following indispensable but missing parameters:
337 $CPAN::Frontend->myprint(qq{
338 $configpm initialized.
342 CPAN::FirstTime::init($configpm, %args);
345 sub missing_config_data {
356 "inhibit_startup_message",
363 "mbuild_install_arg",
364 "mbuild_install_build_command",
368 "prerequisites_policy",
374 next unless exists $keys{$_};
375 push @miss, $_ unless defined $CPAN::Config->{$_};
381 $CPAN::Frontend->myprint(q[
383 defaults reload default config values from disk
384 commit commit session changes to disk
385 init go through a dialog to set all parameters
387 You may edit key values in the follow fashion (the "o" is a literal
390 o conf build_cache 15
392 o conf build_dir "/foo/bar"
396 o conf urllist unshift ftp://ftp.foo.bar/
399 undef; #don't reprint CPAN::Config
403 my($word,$line,$pos) = @_;
405 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
406 my(@words) = split " ", substr($line,0,$pos+1);
411 $words[2] =~ /list$/ && @words == 3
413 $words[2] =~ /list$/ && @words == 4 && length($word)
416 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
417 } elsif (@words >= 4) {
421 my(@o_conf) = sort grep { !$seen{$_}++ }
425 return grep /^\Q$word\E/, @o_conf;
429 package ####::###### #hide from indexer
431 # note: J. Nick Koston wrote me that they are using
432 # CPAN::Config->commit although undocumented. I suggested
433 # CPAN::Shell->o("conf","commit") even when ugly it is at least
436 # that's why I added the CPAN::Config class with autoload and
440 use vars qw($AUTOLOAD $VERSION);
441 $VERSION = sprintf "%.2f", substr(q$Rev: 488 $,4)/100;
443 # formerly CPAN::HandleConfig was known as CPAN::Config
446 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
448 CPAN::HandleConfig->$l(@_);
456 # cperl-indent-level: 4