1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 809 $,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 } (
15 # allow_unauthenticated ?? some day...
24 "commandnumber_in_prompt",
27 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
40 "inhibit_startup_message",
46 "make_install_make_command",
50 "mbuild_install_build_command",
58 "prerequisites_policy",
74 if ($^O eq "MSWin32") {
76 mbuild_install_build_command
77 make_install_make_command
80 if (exists $CPAN::Config->{$k}) {
81 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
82 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
84 delete $CPAN::Config->{$k};
89 # returns true on successful action
93 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
94 my($o,$str,$func,$args,$key_exists);
98 $self->$o(args => \@args); # o conf init => sub init => sub load
101 CPAN->debug("o[$o]") if $CPAN::DEBUG;
102 unless (exists $keys{$o}) {
103 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
108 CPAN->debug("func[$func]") if $CPAN::DEBUG;
110 # Let's avoid eval, it's easier to comprehend without.
111 if ($func eq "push") {
112 push @{$CPAN::Config->{$o}}, @args;
114 } elsif ($func eq "pop") {
115 pop @{$CPAN::Config->{$o}};
117 } elsif ($func eq "shift") {
118 shift @{$CPAN::Config->{$o}};
120 } elsif ($func eq "unshift") {
121 unshift @{$CPAN::Config->{$o}}, @args;
123 } elsif ($func eq "splice") {
124 splice @{$CPAN::Config->{$o}}, @args;
127 $CPAN::Config->{$o} = [@args];
130 $self->prettyprint($o);
133 if ($o eq "urllist") {
134 # reset the cached values
135 undef $CPAN::FTP::Thesite;
136 undef $CPAN::FTP::Themethod;
137 } elsif ($o eq "dontload_list") {
138 # empty it, it will be built up again
139 $CPAN::META->{dontload_hash} = {};
143 } elsif ($o =~ /_hash$/) {
144 @args = () if @args==1 && $args[0] eq "";
145 push @args, "" if @args % 2;
146 $CPAN::Config->{$o} = { @args };
148 $CPAN::Config->{$o} = $args[0] if defined $args[0];
149 $self->prettyprint($o);
157 my $v = $CPAN::Config->{$k};
160 if (ref $v eq "ARRAY") {
161 @report = map {"\t[$_]\n"} @$v;
163 @report = map { sprintf("\t%-18s => %s\n",
165 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
168 $CPAN::Frontend->myprint(
178 } elsif (defined $v) {
179 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
181 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
186 my($self,@args) = @_;
189 if ($args[0] eq "args") {
190 # we have not signed that contract
192 $configpm = $args[0];
195 unless (defined $configpm){
196 $configpm ||= $INC{"CPAN/MyConfig.pm"};
197 $configpm ||= $INC{"CPAN/Config.pm"};
198 $configpm || Carp::confess(q{
199 CPAN::Config::commit called without an argument.
200 Please specify a filename where to save the configuration or try
201 "o conf init" to have an interactive course through configing.
206 $mode = (stat $configpm)[2];
207 if ($mode && ! -w _) {
208 Carp::confess("$configpm is not writable");
213 $msg = <<EOF unless $configpm =~ /MyConfig/;
215 # This is CPAN.pm's systemwide configuration file. This file provides
216 # defaults for users, and the values can be changed in a per-user
217 # configuration file. The user-config file is being looked for as
218 # ~/.cpan/CPAN/MyConfig.pm.
222 my($fh) = FileHandle->new;
223 rename $configpm, "$configpm~" if -f $configpm;
224 open $fh, ">$configpm" or
225 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
226 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
227 foreach (sort keys %$CPAN::Config) {
228 unless (exists $keys{$_}) {
229 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
230 delete $CPAN::Config->{$_};
235 $self->neatvalue($CPAN::Config->{$_}),
240 $fh->print("};\n1;\n__END__\n");
243 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
244 #chmod $mode, $configpm;
245 ###why was that so? $self->defaults;
246 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
250 # stolen from MakeMaker; not taking the original because it is buggy;
251 # bugreport will have to say: keys of hashes remain unquoted and can
252 # produce syntax errors
255 return "undef" unless defined $v;
257 return "q[$v]" unless $t;
261 foreach my $elem (@$v) {
262 push @neat, "q[$elem]";
264 push @m, join ", ", @neat;
268 return "$v" unless $t eq 'HASH';
270 while (($key,$val) = each %$v){
271 last unless defined $key; # cautious programming in case (undef,undef) is true
272 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
274 return "{ ".join(', ',@m)." }";
280 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
281 CPAN::Shell->reload_this($config) and $done++;
287 =head2 C<< CLASS->safe_quote ITEM >>
289 Quotes an item to become safe against spaces
290 in shell interpolation. An item is enclosed
293 - the item contains spaces in the middle
294 - the item does not start with a quote
296 This happens to avoid shell interpolation
297 problems when whitespace is present in
300 This method uses C<commands_quote> to determine
301 the correct quote. If C<commands_quote> is
302 a space, no quoting will take place.
305 if it starts and ends with the same quote character: leave it as it is
307 if it contains no whitespace: leave it as it is
309 if it contains whitespace, then
311 if it contains quotes: better leave it as it is
313 else: quote it with the correct quote type for the box we're on
318 # Instead of patching the guess, set commands_quote
320 my ($quotes,$use_quote)
327 my ($self, $command) = @_;
328 # Set up quote/default quote
329 my $quote = $CPAN::Config->{commands_quote} || $quotes;
333 and $command !~ /[$quote]/) {
334 return qq<$use_quote$command$use_quote>
341 my($self,@args) = @_;
342 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
351 # This is a piece of repeated code that is abstracted here for
352 # maintainability. RMB
355 my($configpmdir, $configpmtest) = @_;
356 if (-w $configpmtest) {
357 return $configpmtest;
358 } elsif (-w $configpmdir) {
359 #_#_# following code dumped core on me with 5.003_11, a.k.
360 my $configpm_bak = "$configpmtest.bak";
361 unlink $configpm_bak if -f $configpm_bak;
362 if( -f $configpmtest ) {
363 if( rename $configpmtest, $configpm_bak ) {
364 $CPAN::Frontend->mywarn(<<END);
365 Old configuration file $configpmtest
366 moved to $configpm_bak
370 my $fh = FileHandle->new;
371 if ($fh->open(">$configpmtest")) {
373 return $configpmtest;
375 # Should never happen
376 Carp::confess("Cannot open >$configpmtest");
381 sub require_myconfig_or_config () {
382 return if $INC{"CPAN/MyConfig.pm"};
385 unshift @INC, File::Spec->catdir($home,'.cpan');
386 eval { require CPAN::MyConfig };
387 my $err_myconfig = $@;
388 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
389 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
391 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
392 eval {require CPAN::Config;}; # not everybody has one
394 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
395 die "Error while requiring CPAN::Config:\n$err_config";
402 if ($CPAN::META->has_usable("File::HomeDir")) {
403 $home = File::HomeDir->my_data;
411 my($self, %args) = @_;
412 $CPAN::Be_Silent++ if $args{be_silent};
416 require_myconfig_or_config;
417 return unless @miss = $self->missing_config_data;
419 require CPAN::FirstTime;
420 my($configpm,$fh,$redo,$theycalled);
422 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
423 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
424 $configpm = $INC{"CPAN/Config.pm"};
426 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
427 $configpm = $INC{"CPAN/MyConfig.pm"};
430 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
431 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
432 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
434 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
435 $configpm = _configpmtest($configpmdir,$configpmtest);
436 $inc_key = "CPAN/Config.pm";
439 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
440 File::Path::mkpath($configpmdir);
441 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
442 $configpm = _configpmtest($configpmdir,$configpmtest);
443 $inc_key = "CPAN/MyConfig.pm";
446 $INC{$inc_key} = $configpm;
448 my $text = qq{WARNING: CPAN.pm is unable to } .
449 qq{create a configuration file.};
450 output($text, 'confess');
455 if ($redo && ! $theycalled){
456 $CPAN::Frontend->myprint(<<END);
457 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
458 the following indispensable but missing parameters:
462 $args{args} = ["\\b".join("|",@miss)."\\b"];
464 $CPAN::Frontend->myprint(qq{
465 $configpm initialized.
467 CPAN::FirstTime::init($configpm, %args);
470 sub missing_config_data {
481 "inhibit_startup_message",
488 "mbuild_install_arg",
489 "mbuild_install_build_command",
493 "prerequisites_policy",
499 next unless exists $keys{$_};
500 push @miss, $_ unless defined $CPAN::Config->{$_};
506 $CPAN::Frontend->myprint(q[
508 commit commit session changes to disk
509 defaults reload default config values from disk
511 init go through a dialog to set all parameters
513 Edit key values as in the following (the "o" is a literal letter o):
514 o conf build_cache 15
515 o conf build_dir "/foo/bar"
517 o conf urllist unshift ftp://ftp.foo.bar/
518 o conf inhibit_startup_message 1
521 undef; #don't reprint CPAN::Config
525 my($word,$line,$pos) = @_;
527 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
528 my(@words) = split " ", substr($line,0,$pos+1);
537 @words == 4 && length($word)
540 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
541 } elsif (defined($words[2])
548 @words == 4 && length($word)
550 return sort grep /^\Q$word\E/, keys %keys;
551 } elsif (@words >= 4) {
555 my(@o_conf) = sort grep { !$seen{$_}++ }
559 return grep /^\Q$word\E/, @o_conf;
564 CPAN::Config; ####::###### #hide from indexer
565 # note: J. Nick Koston wrote me that they are using
566 # CPAN::Config->commit although undocumented. I suggested
567 # CPAN::Shell->o("conf","commit") even when ugly it is at least
570 # that's why I added the CPAN::Config class with autoload and
574 use vars qw($AUTOLOAD $VERSION);
575 $VERSION = sprintf "%.2f", substr(q$Rev: 809 $,4)/100;
577 # formerly CPAN::HandleConfig was known as CPAN::Config
580 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
582 CPAN::HandleConfig->$l(@_);
590 # cperl-indent-level: 4