| 1 | package Psh::Support::Env; | ||
| 2 | |||
| 3 | # | ||
| 4 | # string do_setenv(string command) | ||
| 5 | # | ||
| 6 | # command is of the form "VAR VALUE" or "VAR = VALUE" or "VAR"; sets | ||
| 7 | # $ENV{VAR} to "VALUE" in the first two cases, or to "$VAR" in the | ||
| 8 | # third case unless $VAR is undefined. Used by the setenv and export | ||
| 9 | # builtins. Returns VAR (which is a string with no $). | ||
| 10 | |||
| 11 | sub do_setenv | ||
| 12 | { | ||
| 13 | my $arg = shift; | ||
| 14 | if( $arg=~ /^\s*(\w+)(\s+|\s*=\s*)(.+)/ ) { | ||
| 15 | my $var= $1; | ||
| 16 | my $value= $3; | ||
| 17 | if( $value=~ /^\'(.*)\'\s*$/ ) { | ||
| 18 | # If single quotes were used, do not interpret | ||
| 19 | # variables | ||
| 20 | $ENV{$var}=$1; | ||
| 21 | } else { | ||
| 22 | $var =~ s/^\$//; | ||
| 23 | if ($value=~ /^\"(.*)\"/) { | ||
| 24 | $value=$1; | ||
| 25 | } | ||
| 26 | # Use eval so that variables may appear on RHS | ||
| 27 | # ($value); use protected_eval so that lexicals | ||
| 28 | # in this file don't shadow package variables | ||
| 29 | Psh::PerlEval::protected_eval("\$ENV{$var}=\"$value\"", 'do_setenv'); | ||
| 30 | } | ||
| 31 | return $var; | ||
| 32 | } elsif( $arg=~ /(\w+)/ ) { | ||
| 33 | my $var= $1; | ||
| 34 | $var =~ s/^\$//; | ||
| 35 | Psh::PerlEval::protected_eval("\$ENV{$var}=\$$var if defined(\$$var);", | ||
| 36 | 'do_setenv'); | ||
| 37 | return $var; | ||
| 38 | } | ||
| 39 | return ''; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | 1; |