1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $dot_cpan $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 581 $,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 $CPAN::Frontend->mywarn("deleting previously set config variable ".
71 "'$k' => '$CPAN::Config->{$k}'");
72 delete $CPAN::Config->{$k};
77 # returns true on successful action
81 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
82 my($o,$str,$func,$args,$key_exists);
86 $self->$o(args => \@args);
89 CPAN->debug("o[$o]") if $CPAN::DEBUG;
90 unless (exists $keys{$o}) {
91 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
96 CPAN->debug("func[$func]") if $CPAN::DEBUG;
98 # Let's avoid eval, it's easier to comprehend without.
99 if ($func eq "push") {
100 push @{$CPAN::Config->{$o}}, @args;
102 } elsif ($func eq "pop") {
103 pop @{$CPAN::Config->{$o}};
105 } elsif ($func eq "shift") {
106 shift @{$CPAN::Config->{$o}};
108 } elsif ($func eq "unshift") {
109 unshift @{$CPAN::Config->{$o}}, @args;
111 } elsif ($func eq "splice") {
112 splice @{$CPAN::Config->{$o}}, @args;
115 $CPAN::Config->{$o} = [@args];
118 $self->prettyprint($o);
121 if ($o eq "urllist") {
122 # reset the cached values
123 undef $CPAN::FTP::Thesite;
124 undef $CPAN::FTP::Themethod;
125 } elsif ($o eq "dontload_list") {
126 # empty it, it will be built up again
127 $CPAN::META->{dontload_hash} = {};
131 } elsif ($o =~ /_hash$/) {
132 @args = () if @args==1 && $args[0] eq "";
133 push @args, "" if @args % 2;
134 $CPAN::Config->{$o} = { @args };
136 $CPAN::Config->{$o} = $args[0] if defined $args[0];
137 $self->prettyprint($o);
144 my $v = $CPAN::Config->{$k};
147 if (ref $v eq "ARRAY") {
148 @report = map {"\t[$_]\n"} @$v;
150 @report = map { sprintf("\t%-18s => %s\n",
152 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
155 $CPAN::Frontend->myprint(
165 } elsif (defined $v) {
166 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
168 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
173 my($self,@args) = @_;
176 if ($args[0] eq "args") {
177 # we have not signed that contract
179 $configpm = $args[0];
182 unless (defined $configpm){
183 $configpm ||= $INC{"CPAN/MyConfig.pm"};
184 $configpm ||= $INC{"CPAN/Config.pm"};
185 $configpm || Carp::confess(q{
186 CPAN::Config::commit called without an argument.
187 Please specify a filename where to save the configuration or try
188 "o conf init" to have an interactive course through configing.
193 $mode = (stat $configpm)[2];
194 if ($mode && ! -w _) {
195 Carp::confess("$configpm is not writable");
200 $msg = <<EOF unless $configpm =~ /MyConfig/;
202 # This is CPAN.pm's systemwide configuration file. This file provides
203 # defaults for users, and the values can be changed in a per-user
204 # configuration file. The user-config file is being looked for as
205 # ~/.cpan/CPAN/MyConfig.pm.
209 my($fh) = FileHandle->new;
210 rename $configpm, "$configpm~" if -f $configpm;
211 open $fh, ">$configpm" or
212 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
213 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
214 foreach (sort keys %$CPAN::Config) {
215 unless (exists $keys{$_}) {
216 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
217 delete $CPAN::Config->{$_};
222 $self->neatvalue($CPAN::Config->{$_}),
227 $fh->print("};\n1;\n__END__\n");
230 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
231 #chmod $mode, $configpm;
232 ###why was that so? $self->defaults;
233 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
237 # stolen from MakeMaker; not taking the original because it is buggy;
238 # bugreport will have to say: keys of hashes remain unquoted and can
239 # produce syntax errors
242 return "undef" unless defined $v;
244 return "q[$v]" unless $t;
248 foreach my $elem (@$v) {
249 push @neat, "q[$elem]";
251 push @m, join ", ", @neat;
255 return "$v" unless $t eq 'HASH';
257 while (($key,$val) = each %$v){
258 last unless defined $key; # cautious programming in case (undef,undef) is true
259 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
261 return "{ ".join(', ',@m)." }";
267 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
268 CPAN::Shell->reload_this($config) and $done++;
275 my($self,@args) = @_;
276 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
285 # This is a piece of repeated code that is abstracted here for
286 # maintainability. RMB
289 my($configpmdir, $configpmtest) = @_;
290 if (-w $configpmtest) {
291 return $configpmtest;
292 } elsif (-w $configpmdir) {
293 #_#_# following code dumped core on me with 5.003_11, a.k.
294 my $configpm_bak = "$configpmtest.bak";
295 unlink $configpm_bak if -f $configpm_bak;
296 if( -f $configpmtest ) {
297 if( rename $configpmtest, $configpm_bak ) {
298 $CPAN::Frontend->mywarn(<<END);
299 Old configuration file $configpmtest
300 moved to $configpm_bak
304 my $fh = FileHandle->new;
305 if ($fh->open(">$configpmtest")) {
307 return $configpmtest;
309 # Should never happen
310 Carp::confess("Cannot open >$configpmtest");
316 my($self, %args) = @_;
317 $CPAN::Be_Silent++ if $args{be_silent};
321 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
322 eval {require CPAN::Config;}; # not everybody has one
324 unless ($dot_cpan++){
325 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
326 eval {require CPAN::MyConfig;}; # override system wide settings
329 return unless @miss = $self->missing_config_data;
331 require CPAN::FirstTime;
332 my($configpm,$fh,$redo,$theycalled);
334 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
335 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
336 $configpm = $INC{"CPAN/Config.pm"};
338 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
339 $configpm = $INC{"CPAN/MyConfig.pm"};
342 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
343 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
344 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
346 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
347 $configpm = _configpmtest($configpmdir,$configpmtest);
348 $inc_key = "CPAN/Config.pm";
351 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
352 File::Path::mkpath($configpmdir);
353 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
354 $configpm = _configpmtest($configpmdir,$configpmtest);
355 $inc_key = "CPAN/MyConfig.pm";
358 $INC{$inc_key} = $configpm;
360 my $text = qq{WARNING: CPAN.pm is unable to } .
361 qq{create a configuration file.};
362 output($text, 'confess');
367 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
368 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
369 the following indispensable but missing parameters:
373 $CPAN::Frontend->myprint(qq{
374 $configpm initialized.
378 CPAN::FirstTime::init($configpm, %args);
381 sub missing_config_data {
392 "inhibit_startup_message",
399 "mbuild_install_arg",
400 "mbuild_install_build_command",
404 "prerequisites_policy",
410 next unless exists $keys{$_};
411 push @miss, $_ unless defined $CPAN::Config->{$_};
417 $CPAN::Frontend->myprint(q[
419 commit commit session changes to disk
420 defaults reload default config values from disk
422 init go through a dialog to set all parameters
424 Edit key values as in the following (the "o" is a literal letter o):
425 o conf build_cache 15
426 o conf build_dir "/foo/bar"
428 o conf urllist unshift ftp://ftp.foo.bar/
429 o conf inhibit_startup_message 1
432 undef; #don't reprint CPAN::Config
436 my($word,$line,$pos) = @_;
438 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
439 my(@words) = split " ", substr($line,0,$pos+1);
444 $words[2] =~ /list$/ && @words == 3
446 $words[2] =~ /list$/ && @words == 4 && length($word)
449 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
450 } elsif (@words >= 4) {
454 my(@o_conf) = sort grep { !$seen{$_}++ }
458 return grep /^\Q$word\E/, @o_conf;
463 CPAN::Config; ####::###### #hide from indexer
464 # note: J. Nick Koston wrote me that they are using
465 # CPAN::Config->commit although undocumented. I suggested
466 # CPAN::Shell->o("conf","commit") even when ugly it is at least
469 # that's why I added the CPAN::Config class with autoload and
473 use vars qw($AUTOLOAD $VERSION);
474 $VERSION = sprintf "%.2f", substr(q$Rev: 581 $,4)/100;
476 # formerly CPAN::HandleConfig was known as CPAN::Config
479 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
481 CPAN::HandleConfig->$l(@_);
489 # cperl-indent-level: 4