1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 916 $,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...
18 "build_requires_install_policy",
25 "commandnumber_in_prompt",
29 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
42 "inhibit_startup_message",
48 "make_install_make_command",
52 "mbuild_install_build_command",
60 "prerequisites_policy",
76 if ($^O eq "MSWin32") {
78 mbuild_install_build_command
79 make_install_make_command
82 if (exists $CPAN::Config->{$k}) {
83 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
84 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
86 delete $CPAN::Config->{$k};
91 # returns true on successful action
95 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
96 my($o,$str,$func,$args,$key_exists);
100 $self->$o(args => \@args); # o conf init => sub init => sub load
103 CPAN->debug("o[$o]") if $CPAN::DEBUG;
104 unless (exists $keys{$o}) {
105 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
110 CPAN->debug("func[$func]") if $CPAN::DEBUG;
112 # Let's avoid eval, it's easier to comprehend without.
113 if ($func eq "push") {
114 push @{$CPAN::Config->{$o}}, @args;
116 } elsif ($func eq "pop") {
117 pop @{$CPAN::Config->{$o}};
119 } elsif ($func eq "shift") {
120 shift @{$CPAN::Config->{$o}};
122 } elsif ($func eq "unshift") {
123 unshift @{$CPAN::Config->{$o}}, @args;
125 } elsif ($func eq "splice") {
126 splice @{$CPAN::Config->{$o}}, @args;
129 $CPAN::Config->{$o} = [@args];
132 $self->prettyprint($o);
135 if ($o eq "urllist") {
136 # reset the cached values
137 undef $CPAN::FTP::Thesite;
138 undef $CPAN::FTP::Themethod;
139 } elsif ($o eq "dontload_list") {
140 # empty it, it will be built up again
141 $CPAN::META->{dontload_hash} = {};
145 } elsif ($o =~ /_hash$/) {
146 @args = () if @args==1 && $args[0] eq "";
147 push @args, "" if @args % 2;
148 $CPAN::Config->{$o} = { @args };
150 $CPAN::Config->{$o} = $args[0] if defined $args[0];
151 $self->prettyprint($o);
159 my $v = $CPAN::Config->{$k};
162 if (ref $v eq "ARRAY") {
163 @report = map {"\t[$_]\n"} @$v;
165 @report = map { sprintf("\t%-18s => %s\n",
167 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
170 $CPAN::Frontend->myprint(
180 } elsif (defined $v) {
181 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
183 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
188 my($self,@args) = @_;
191 if ($args[0] eq "args") {
192 # we have not signed that contract
194 $configpm = $args[0];
197 unless (defined $configpm){
198 $configpm ||= $INC{"CPAN/MyConfig.pm"};
199 $configpm ||= $INC{"CPAN/Config.pm"};
200 $configpm || Carp::confess(q{
201 CPAN::Config::commit called without an argument.
202 Please specify a filename where to save the configuration or try
203 "o conf init" to have an interactive course through configing.
208 $mode = (stat $configpm)[2];
209 if ($mode && ! -w _) {
210 Carp::confess("$configpm is not writable");
215 $msg = <<EOF unless $configpm =~ /MyConfig/;
217 # This is CPAN.pm's systemwide configuration file. This file provides
218 # defaults for users, and the values can be changed in a per-user
219 # configuration file. The user-config file is being looked for as
220 # ~/.cpan/CPAN/MyConfig.pm.
224 my($fh) = FileHandle->new;
225 rename $configpm, "$configpm~" if -f $configpm;
226 open $fh, ">$configpm" or
227 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
228 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
229 foreach (sort keys %$CPAN::Config) {
230 unless (exists $keys{$_}) {
231 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
232 delete $CPAN::Config->{$_};
237 $self->neatvalue($CPAN::Config->{$_}),
242 $fh->print("};\n1;\n__END__\n");
245 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
246 #chmod $mode, $configpm;
247 ###why was that so? $self->defaults;
248 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
252 # stolen from MakeMaker; not taking the original because it is buggy;
253 # bugreport will have to say: keys of hashes remain unquoted and can
254 # produce syntax errors
257 return "undef" unless defined $v;
259 return "q[$v]" unless $t;
263 foreach my $elem (@$v) {
264 push @neat, "q[$elem]";
266 push @m, join ", ", @neat;
270 return "$v" unless $t eq 'HASH';
272 while (($key,$val) = each %$v){
273 last unless defined $key; # cautious programming in case (undef,undef) is true
274 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
276 return "{ ".join(', ',@m)." }";
282 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
284 CPAN::Shell->reload_this($config);
285 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
292 =head2 C<< CLASS->safe_quote ITEM >>
294 Quotes an item to become safe against spaces
295 in shell interpolation. An item is enclosed
298 - the item contains spaces in the middle
299 - the item does not start with a quote
301 This happens to avoid shell interpolation
302 problems when whitespace is present in
305 This method uses C<commands_quote> to determine
306 the correct quote. If C<commands_quote> is
307 a space, no quoting will take place.
310 if it starts and ends with the same quote character: leave it as it is
312 if it contains no whitespace: leave it as it is
314 if it contains whitespace, then
316 if it contains quotes: better leave it as it is
318 else: quote it with the correct quote type for the box we're on
323 # Instead of patching the guess, set commands_quote
325 my ($quotes,$use_quote)
332 my ($self, $command) = @_;
333 # Set up quote/default quote
334 my $quote = $CPAN::Config->{commands_quote} || $quotes;
338 and $command !~ /[$quote]/) {
339 return qq<$use_quote$command$use_quote>
346 my($self,@args) = @_;
347 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
356 # This is a piece of repeated code that is abstracted here for
357 # maintainability. RMB
360 my($configpmdir, $configpmtest) = @_;
361 if (-w $configpmtest) {
362 return $configpmtest;
363 } elsif (-w $configpmdir) {
364 #_#_# following code dumped core on me with 5.003_11, a.k.
365 my $configpm_bak = "$configpmtest.bak";
366 unlink $configpm_bak if -f $configpm_bak;
367 if( -f $configpmtest ) {
368 if( rename $configpmtest, $configpm_bak ) {
369 $CPAN::Frontend->mywarn(<<END);
370 Old configuration file $configpmtest
371 moved to $configpm_bak
375 my $fh = FileHandle->new;
376 if ($fh->open(">$configpmtest")) {
378 return $configpmtest;
380 # Should never happen
381 Carp::confess("Cannot open >$configpmtest");
386 sub require_myconfig_or_config () {
387 return if $INC{"CPAN/MyConfig.pm"};
390 unshift @INC, File::Spec->catdir($home,'.cpan');
391 eval { require CPAN::MyConfig };
392 my $err_myconfig = $@;
393 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
394 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
396 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
397 eval {require CPAN::Config;}; # not everybody has one
399 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
400 die "Error while requiring CPAN::Config:\n$err_config";
407 if ($CPAN::META->has_usable("File::HomeDir")) {
408 $home = File::HomeDir->my_data;
416 my($self, %args) = @_;
417 $CPAN::Be_Silent++ if $args{be_silent};
421 require_myconfig_or_config;
422 return unless @miss = $self->missing_config_data;
424 require CPAN::FirstTime;
425 my($configpm,$fh,$redo,$theycalled);
427 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
428 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
429 $configpm = $INC{"CPAN/Config.pm"};
431 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
432 $configpm = $INC{"CPAN/MyConfig.pm"};
435 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
436 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
437 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
439 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
440 $configpm = _configpmtest($configpmdir,$configpmtest);
441 $inc_key = "CPAN/Config.pm";
444 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
445 File::Path::mkpath($configpmdir);
446 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
447 $configpm = _configpmtest($configpmdir,$configpmtest);
448 $inc_key = "CPAN/MyConfig.pm";
451 $INC{$inc_key} = $configpm;
453 my $text = qq{WARNING: CPAN.pm is unable to } .
454 qq{create a configuration file.};
455 output($text, 'confess');
460 if ($redo && ! $theycalled){
461 $CPAN::Frontend->myprint(<<END);
462 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
463 the following indispensable but missing parameters:
467 $args{args} = \@miss;
470 # where do we need this?
471 $CPAN::Frontend->myprint(qq{
472 $configpm initialized.
475 CPAN::FirstTime::init($configpm, %args);
478 sub missing_config_data {
489 "inhibit_startup_message",
496 "mbuild_install_arg",
497 "mbuild_install_build_command",
501 "prerequisites_policy",
507 next unless exists $keys{$_};
508 push @miss, $_ unless defined $CPAN::Config->{$_};
514 $CPAN::Frontend->myprint(q[
516 commit commit session changes to disk
517 defaults reload default config values from disk
519 init enter a dialog to set all or a set of parameters
521 Edit key values as in the following (the "o" is a literal letter o):
522 o conf build_cache 15
523 o conf build_dir "/foo/bar"
525 o conf urllist unshift ftp://ftp.foo.bar/
526 o conf inhibit_startup_message 1
529 undef; #don't reprint CPAN::Config
533 my($word,$line,$pos) = @_;
535 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
536 my(@words) = split " ", substr($line,0,$pos+1);
545 @words == 4 && length($word)
548 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
549 } elsif (defined($words[2])
556 @words >= 4 && length($word)
558 return sort grep /^\Q$word\E/, keys %keys;
559 } elsif (@words >= 4) {
563 my(@o_conf) = sort grep { !$seen{$_}++ }
567 return grep /^\Q$word\E/, @o_conf;
572 CPAN::Config; ####::###### #hide from indexer
573 # note: J. Nick Koston wrote me that they are using
574 # CPAN::Config->commit although undocumented. I suggested
575 # CPAN::Shell->o("conf","commit") even when ugly it is at least
578 # that's why I added the CPAN::Config class with autoload and
582 use vars qw($AUTOLOAD $VERSION);
583 $VERSION = sprintf "%.2f", substr(q$Rev: 916 $,4)/100;
585 # formerly CPAN::HandleConfig was known as CPAN::Config
588 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
590 CPAN::HandleConfig->$l(@_);
599 This program is free software; you can redistribute it and/or
600 modify it under the same terms as Perl itself.
606 # cperl-indent-level: 4