1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 847 $,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",
28 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
41 "inhibit_startup_message",
47 "make_install_make_command",
51 "mbuild_install_build_command",
59 "prerequisites_policy",
75 if ($^O eq "MSWin32") {
77 mbuild_install_build_command
78 make_install_make_command
81 if (exists $CPAN::Config->{$k}) {
82 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
83 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
85 delete $CPAN::Config->{$k};
90 # returns true on successful action
94 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
95 my($o,$str,$func,$args,$key_exists);
99 $self->$o(args => \@args); # o conf init => sub init => sub load
102 CPAN->debug("o[$o]") if $CPAN::DEBUG;
103 unless (exists $keys{$o}) {
104 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
109 CPAN->debug("func[$func]") if $CPAN::DEBUG;
111 # Let's avoid eval, it's easier to comprehend without.
112 if ($func eq "push") {
113 push @{$CPAN::Config->{$o}}, @args;
115 } elsif ($func eq "pop") {
116 pop @{$CPAN::Config->{$o}};
118 } elsif ($func eq "shift") {
119 shift @{$CPAN::Config->{$o}};
121 } elsif ($func eq "unshift") {
122 unshift @{$CPAN::Config->{$o}}, @args;
124 } elsif ($func eq "splice") {
125 splice @{$CPAN::Config->{$o}}, @args;
128 $CPAN::Config->{$o} = [@args];
131 $self->prettyprint($o);
134 if ($o eq "urllist") {
135 # reset the cached values
136 undef $CPAN::FTP::Thesite;
137 undef $CPAN::FTP::Themethod;
138 } elsif ($o eq "dontload_list") {
139 # empty it, it will be built up again
140 $CPAN::META->{dontload_hash} = {};
144 } elsif ($o =~ /_hash$/) {
145 @args = () if @args==1 && $args[0] eq "";
146 push @args, "" if @args % 2;
147 $CPAN::Config->{$o} = { @args };
149 $CPAN::Config->{$o} = $args[0] if defined $args[0];
150 $self->prettyprint($o);
158 my $v = $CPAN::Config->{$k};
161 if (ref $v eq "ARRAY") {
162 @report = map {"\t[$_]\n"} @$v;
164 @report = map { sprintf("\t%-18s => %s\n",
166 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
169 $CPAN::Frontend->myprint(
179 } elsif (defined $v) {
180 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
182 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
187 my($self,@args) = @_;
190 if ($args[0] eq "args") {
191 # we have not signed that contract
193 $configpm = $args[0];
196 unless (defined $configpm){
197 $configpm ||= $INC{"CPAN/MyConfig.pm"};
198 $configpm ||= $INC{"CPAN/Config.pm"};
199 $configpm || Carp::confess(q{
200 CPAN::Config::commit called without an argument.
201 Please specify a filename where to save the configuration or try
202 "o conf init" to have an interactive course through configing.
207 $mode = (stat $configpm)[2];
208 if ($mode && ! -w _) {
209 Carp::confess("$configpm is not writable");
214 $msg = <<EOF unless $configpm =~ /MyConfig/;
216 # This is CPAN.pm's systemwide configuration file. This file provides
217 # defaults for users, and the values can be changed in a per-user
218 # configuration file. The user-config file is being looked for as
219 # ~/.cpan/CPAN/MyConfig.pm.
223 my($fh) = FileHandle->new;
224 rename $configpm, "$configpm~" if -f $configpm;
225 open $fh, ">$configpm" or
226 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
227 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
228 foreach (sort keys %$CPAN::Config) {
229 unless (exists $keys{$_}) {
230 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
231 delete $CPAN::Config->{$_};
236 $self->neatvalue($CPAN::Config->{$_}),
241 $fh->print("};\n1;\n__END__\n");
244 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
245 #chmod $mode, $configpm;
246 ###why was that so? $self->defaults;
247 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
251 # stolen from MakeMaker; not taking the original because it is buggy;
252 # bugreport will have to say: keys of hashes remain unquoted and can
253 # produce syntax errors
256 return "undef" unless defined $v;
258 return "q[$v]" unless $t;
262 foreach my $elem (@$v) {
263 push @neat, "q[$elem]";
265 push @m, join ", ", @neat;
269 return "$v" unless $t eq 'HASH';
271 while (($key,$val) = each %$v){
272 last unless defined $key; # cautious programming in case (undef,undef) is true
273 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
275 return "{ ".join(', ',@m)." }";
281 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
283 CPAN::Shell->reload_this($config);
284 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
291 =head2 C<< CLASS->safe_quote ITEM >>
293 Quotes an item to become safe against spaces
294 in shell interpolation. An item is enclosed
297 - the item contains spaces in the middle
298 - the item does not start with a quote
300 This happens to avoid shell interpolation
301 problems when whitespace is present in
304 This method uses C<commands_quote> to determine
305 the correct quote. If C<commands_quote> is
306 a space, no quoting will take place.
309 if it starts and ends with the same quote character: leave it as it is
311 if it contains no whitespace: leave it as it is
313 if it contains whitespace, then
315 if it contains quotes: better leave it as it is
317 else: quote it with the correct quote type for the box we're on
322 # Instead of patching the guess, set commands_quote
324 my ($quotes,$use_quote)
331 my ($self, $command) = @_;
332 # Set up quote/default quote
333 my $quote = $CPAN::Config->{commands_quote} || $quotes;
337 and $command !~ /[$quote]/) {
338 return qq<$use_quote$command$use_quote>
345 my($self,@args) = @_;
346 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
355 # This is a piece of repeated code that is abstracted here for
356 # maintainability. RMB
359 my($configpmdir, $configpmtest) = @_;
360 if (-w $configpmtest) {
361 return $configpmtest;
362 } elsif (-w $configpmdir) {
363 #_#_# following code dumped core on me with 5.003_11, a.k.
364 my $configpm_bak = "$configpmtest.bak";
365 unlink $configpm_bak if -f $configpm_bak;
366 if( -f $configpmtest ) {
367 if( rename $configpmtest, $configpm_bak ) {
368 $CPAN::Frontend->mywarn(<<END);
369 Old configuration file $configpmtest
370 moved to $configpm_bak
374 my $fh = FileHandle->new;
375 if ($fh->open(">$configpmtest")) {
377 return $configpmtest;
379 # Should never happen
380 Carp::confess("Cannot open >$configpmtest");
385 sub require_myconfig_or_config () {
386 return if $INC{"CPAN/MyConfig.pm"};
389 unshift @INC, File::Spec->catdir($home,'.cpan');
390 eval { require CPAN::MyConfig };
391 my $err_myconfig = $@;
392 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
393 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
395 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
396 eval {require CPAN::Config;}; # not everybody has one
398 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
399 die "Error while requiring CPAN::Config:\n$err_config";
406 if ($CPAN::META->has_usable("File::HomeDir")) {
407 $home = File::HomeDir->my_data;
415 my($self, %args) = @_;
416 $CPAN::Be_Silent++ if $args{be_silent};
420 require_myconfig_or_config;
421 return unless @miss = $self->missing_config_data;
423 require CPAN::FirstTime;
424 my($configpm,$fh,$redo,$theycalled);
426 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
427 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
428 $configpm = $INC{"CPAN/Config.pm"};
430 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
431 $configpm = $INC{"CPAN/MyConfig.pm"};
434 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
435 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
436 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
438 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
439 $configpm = _configpmtest($configpmdir,$configpmtest);
440 $inc_key = "CPAN/Config.pm";
443 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
444 File::Path::mkpath($configpmdir);
445 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
446 $configpm = _configpmtest($configpmdir,$configpmtest);
447 $inc_key = "CPAN/MyConfig.pm";
450 $INC{$inc_key} = $configpm;
452 my $text = qq{WARNING: CPAN.pm is unable to } .
453 qq{create a configuration file.};
454 output($text, 'confess');
459 if ($redo && ! $theycalled){
460 $CPAN::Frontend->myprint(<<END);
461 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
462 the following indispensable but missing parameters:
466 $args{args} = \@miss;
469 # where do we need this?
470 $CPAN::Frontend->myprint(qq{
471 $configpm initialized.
474 CPAN::FirstTime::init($configpm, %args);
477 sub missing_config_data {
488 "inhibit_startup_message",
495 "mbuild_install_arg",
496 "mbuild_install_build_command",
500 "prerequisites_policy",
506 next unless exists $keys{$_};
507 push @miss, $_ unless defined $CPAN::Config->{$_};
513 $CPAN::Frontend->myprint(q[
515 commit commit session changes to disk
516 defaults reload default config values from disk
518 init enter a dialog to set all or a set of parameters
520 Edit key values as in the following (the "o" is a literal letter o):
521 o conf build_cache 15
522 o conf build_dir "/foo/bar"
524 o conf urllist unshift ftp://ftp.foo.bar/
525 o conf inhibit_startup_message 1
528 undef; #don't reprint CPAN::Config
532 my($word,$line,$pos) = @_;
534 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
535 my(@words) = split " ", substr($line,0,$pos+1);
544 @words == 4 && length($word)
547 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
548 } elsif (defined($words[2])
555 @words >= 4 && length($word)
557 return sort grep /^\Q$word\E/, keys %keys;
558 } elsif (@words >= 4) {
562 my(@o_conf) = sort grep { !$seen{$_}++ }
566 return grep /^\Q$word\E/, @o_conf;
571 CPAN::Config; ####::###### #hide from indexer
572 # note: J. Nick Koston wrote me that they are using
573 # CPAN::Config->commit although undocumented. I suggested
574 # CPAN::Shell->o("conf","commit") even when ugly it is at least
577 # that's why I added the CPAN::Config class with autoload and
581 use vars qw($AUTOLOAD $VERSION);
582 $VERSION = sprintf "%.2f", substr(q$Rev: 847 $,4)/100;
584 # formerly CPAN::HandleConfig was known as CPAN::Config
587 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
589 CPAN::HandleConfig->$l(@_);
598 This program is free software; you can redistribute it and/or
599 modify it under the same terms as Perl itself.
605 # cperl-indent-level: 4