1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 657 $,4)/1000000 + 5.4;
8 commit => "Commit changes to disk",
9 defaults => "Reload defaults from disk",
10 help => "Short help about 'o conf' usage",
11 init => "Interactive setting of all options",
14 %keys = map { $_ => undef } (
19 "commandnumber_in_prompt",
22 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
35 "inhibit_startup_message",
41 "make_install_make_command",
45 "mbuild_install_build_command",
52 "prerequisites_policy",
63 if ($^O eq "MSWin32") {
65 mbuild_install_build_command
66 make_install_make_command
69 if (exists $CPAN::Config->{$k}) {
70 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
71 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
73 delete $CPAN::Config->{$k};
78 # returns true on successful action
82 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
83 my($o,$str,$func,$args,$key_exists);
87 $self->$o(args => \@args);
90 CPAN->debug("o[$o]") if $CPAN::DEBUG;
91 unless (exists $keys{$o}) {
92 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
97 CPAN->debug("func[$func]") if $CPAN::DEBUG;
99 # Let's avoid eval, it's easier to comprehend without.
100 if ($func eq "push") {
101 push @{$CPAN::Config->{$o}}, @args;
103 } elsif ($func eq "pop") {
104 pop @{$CPAN::Config->{$o}};
106 } elsif ($func eq "shift") {
107 shift @{$CPAN::Config->{$o}};
109 } elsif ($func eq "unshift") {
110 unshift @{$CPAN::Config->{$o}}, @args;
112 } elsif ($func eq "splice") {
113 splice @{$CPAN::Config->{$o}}, @args;
116 $CPAN::Config->{$o} = [@args];
119 $self->prettyprint($o);
122 if ($o eq "urllist") {
123 # reset the cached values
124 undef $CPAN::FTP::Thesite;
125 undef $CPAN::FTP::Themethod;
126 } elsif ($o eq "dontload_list") {
127 # empty it, it will be built up again
128 $CPAN::META->{dontload_hash} = {};
132 } elsif ($o =~ /_hash$/) {
133 @args = () if @args==1 && $args[0] eq "";
134 push @args, "" if @args % 2;
135 $CPAN::Config->{$o} = { @args };
137 $CPAN::Config->{$o} = $args[0] if defined $args[0];
138 $self->prettyprint($o);
145 my $v = $CPAN::Config->{$k};
148 if (ref $v eq "ARRAY") {
149 @report = map {"\t[$_]\n"} @$v;
151 @report = map { sprintf("\t%-18s => %s\n",
153 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
156 $CPAN::Frontend->myprint(
166 } elsif (defined $v) {
167 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
169 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
174 my($self,@args) = @_;
177 if ($args[0] eq "args") {
178 # we have not signed that contract
180 $configpm = $args[0];
183 unless (defined $configpm){
184 $configpm ||= $INC{"CPAN/MyConfig.pm"};
185 $configpm ||= $INC{"CPAN/Config.pm"};
186 $configpm || Carp::confess(q{
187 CPAN::Config::commit called without an argument.
188 Please specify a filename where to save the configuration or try
189 "o conf init" to have an interactive course through configing.
194 $mode = (stat $configpm)[2];
195 if ($mode && ! -w _) {
196 Carp::confess("$configpm is not writable");
201 $msg = <<EOF unless $configpm =~ /MyConfig/;
203 # This is CPAN.pm's systemwide configuration file. This file provides
204 # defaults for users, and the values can be changed in a per-user
205 # configuration file. The user-config file is being looked for as
206 # ~/.cpan/CPAN/MyConfig.pm.
210 my($fh) = FileHandle->new;
211 rename $configpm, "$configpm~" if -f $configpm;
212 open $fh, ">$configpm" or
213 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
214 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
215 foreach (sort keys %$CPAN::Config) {
216 unless (exists $keys{$_}) {
217 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
218 delete $CPAN::Config->{$_};
223 $self->neatvalue($CPAN::Config->{$_}),
228 $fh->print("};\n1;\n__END__\n");
231 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
232 #chmod $mode, $configpm;
233 ###why was that so? $self->defaults;
234 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
238 # stolen from MakeMaker; not taking the original because it is buggy;
239 # bugreport will have to say: keys of hashes remain unquoted and can
240 # produce syntax errors
243 return "undef" unless defined $v;
245 return "q[$v]" unless $t;
249 foreach my $elem (@$v) {
250 push @neat, "q[$elem]";
252 push @m, join ", ", @neat;
256 return "$v" unless $t eq 'HASH';
258 while (($key,$val) = each %$v){
259 last unless defined $key; # cautious programming in case (undef,undef) is true
260 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
262 return "{ ".join(', ',@m)." }";
268 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
269 CPAN::Shell->reload_this($config) and $done++;
276 my($self,@args) = @_;
277 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
286 # This is a piece of repeated code that is abstracted here for
287 # maintainability. RMB
290 my($configpmdir, $configpmtest) = @_;
291 if (-w $configpmtest) {
292 return $configpmtest;
293 } elsif (-w $configpmdir) {
294 #_#_# following code dumped core on me with 5.003_11, a.k.
295 my $configpm_bak = "$configpmtest.bak";
296 unlink $configpm_bak if -f $configpm_bak;
297 if( -f $configpmtest ) {
298 if( rename $configpmtest, $configpm_bak ) {
299 $CPAN::Frontend->mywarn(<<END);
300 Old configuration file $configpmtest
301 moved to $configpm_bak
305 my $fh = FileHandle->new;
306 if ($fh->open(">$configpmtest")) {
308 return $configpmtest;
310 # Should never happen
311 Carp::confess("Cannot open >$configpmtest");
316 sub require_myconfig_or_config () {
317 return if $INC{"CPAN/MyConfig.pm"};
320 unshift @INC, File::Spec->catdir($home,'.cpan');
321 eval { require CPAN::MyConfig };
322 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
323 eval {require CPAN::Config;}; # not everybody has one
329 if ($CPAN::META->has_usable("File::HomeDir")) {
330 $home = File::HomeDir->my_data;
338 my($self, %args) = @_;
339 $CPAN::Be_Silent++ if $args{be_silent};
343 require_myconfig_or_config;
344 return unless @miss = $self->missing_config_data;
346 require CPAN::FirstTime;
347 my($configpm,$fh,$redo,$theycalled);
349 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
350 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
351 $configpm = $INC{"CPAN/Config.pm"};
353 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
354 $configpm = $INC{"CPAN/MyConfig.pm"};
357 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
358 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
359 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
361 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
362 $configpm = _configpmtest($configpmdir,$configpmtest);
363 $inc_key = "CPAN/Config.pm";
366 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
367 File::Path::mkpath($configpmdir);
368 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
369 $configpm = _configpmtest($configpmdir,$configpmtest);
370 $inc_key = "CPAN/MyConfig.pm";
373 $INC{$inc_key} = $configpm;
375 my $text = qq{WARNING: CPAN.pm is unable to } .
376 qq{create a configuration file.};
377 output($text, 'confess');
382 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
383 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
384 the following indispensable but missing parameters:
388 $CPAN::Frontend->myprint(qq{
389 $configpm initialized.
393 CPAN::FirstTime::init($configpm, %args);
396 sub missing_config_data {
407 "inhibit_startup_message",
414 "mbuild_install_arg",
415 "mbuild_install_build_command",
419 "prerequisites_policy",
425 next unless exists $keys{$_};
426 push @miss, $_ unless defined $CPAN::Config->{$_};
432 $CPAN::Frontend->myprint(q[
434 commit commit session changes to disk
435 defaults reload default config values from disk
437 init go through a dialog to set all parameters
439 Edit key values as in the following (the "o" is a literal letter o):
440 o conf build_cache 15
441 o conf build_dir "/foo/bar"
443 o conf urllist unshift ftp://ftp.foo.bar/
444 o conf inhibit_startup_message 1
447 undef; #don't reprint CPAN::Config
451 my($word,$line,$pos) = @_;
453 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
454 my(@words) = split " ", substr($line,0,$pos+1);
459 $words[2] =~ /list$/ && @words == 3
461 $words[2] =~ /list$/ && @words == 4 && length($word)
464 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
465 } elsif (@words >= 4) {
469 my(@o_conf) = sort grep { !$seen{$_}++ }
473 return grep /^\Q$word\E/, @o_conf;
478 CPAN::Config; ####::###### #hide from indexer
479 # note: J. Nick Koston wrote me that they are using
480 # CPAN::Config->commit although undocumented. I suggested
481 # CPAN::Shell->o("conf","commit") even when ugly it is at least
484 # that's why I added the CPAN::Config class with autoload and
488 use vars qw($AUTOLOAD $VERSION);
489 $VERSION = sprintf "%.2f", substr(q$Rev: 657 $,4)/100;
491 # formerly CPAN::HandleConfig was known as CPAN::Config
494 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
496 CPAN::HandleConfig->$l(@_);
504 # cperl-indent-level: 4