Mail Archives: cygwin/1996/12/09/20:28:52
I have written a perl script for the win32 version of perl that
converts envinronment variables over to unix type variables for bash,
then resets the variables after leaving bash. I should have posted
this a while ago, but richardd AT cix DOT compulink DOT co DOT uk got my butt moving
with his post. I call it "shell.bat" and here is how it goes:
cut-------------------------------------------------------------------
@rem = '--*-Perl-*--';
@rem = '
@echo off
perl.exe c:\usr\bin\shell.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl @rem ';
# Set root dir
$ROOTDRIVE = "C";
# Set an array of variables to skip
@skip_vars = (
HOME,
MAKESTARTUP,
SHELL,
);
# Get the temp environment var
$tempdir = $ENV{'TEMP'};
# If temp wasnt set set it to default
if (! $tempdir)
{
$tempdir = "$ROOTDRIVE:\\tmp";
}
# Copy the temp envir var for later
$tempsave = $ENV{'TEMP'};
#######################################################################
# REMEMBER! if you change these file names you need to change them
# after :endofperl as well!
$bashenv = "bashenv.bat";
$saveenv = "saveenv.bat";
# Open file handles to creat environment batch files for before and
# after bash runs
open (BASHENV, "> $tempdir\\$bashenv")
|| die "ERROR: Couldn't open \"$tempdir\\$bashenv\": $!\n";
open (SAVEENV, "> $tempdir\\$saveenv")
|| die "ERROR: Couldn't open \"$tempdir\\$saveenv\": $!\n";
# If comspec is set use it otherwise set a default
if ($ENV{'COMPSPEC'})
{
$comspec = $ENV{'COMSPEC'};
}
else
{
$comspec = "c:\\command.com";
}
# Get the environment from comspec
open (ENV, "$comspec /c set |")
|| die "ERROR: Couldn't get the environment from \"$comspec\": $!\n";
# Copy the environment to an array for easy parsing
@env = <ENV>;
# Process the environment
foreach $var_data (@env)
{
chop $var_data;
# Print the var as is to the save file
print SAVEENV "set $var_data\n";
# Break down the var and data
($var, $data) = ($var_data =~ /([^=]+)=(.*)/);
if (grep(m#$var#, @skip_vars))
{
print BASHENV "set $var=$data\n";
next;
}
# Unixify the paths
$data =~ s/\\/\//g;
# Remove quote marks
$data =~ s/"//g;
# # This is for Novell servers mapped drives, change the server\sys
# to # F: if ($data =~ /server_name\/sys:/i) {
# $data =~ s/server_name\/sys:/F:/ig;
# }
# If data contains a semi-colon assume it is a path
if ($data =~ /\S+;\S+/)
{
# Clear the path variable
$path = "";
# Build an array consisting of path parts
@path_dirs = split(/;/, $data);
foreach $dir (@path_dirs)
{
# If $dir matches "X:" X being a letter, then must be a
drive # letter, change it to gnu-win32 //X format
if ($dir =~ /^[A-Za-z]:/)
{
$dir = &gnu_drive($dir);
}
# Now that we have the directory formatted correctly add
it to # The path $path="$path$dir:";
}
# Replace $data with the $path we created
$data = $path;
}
# If we got this far and match X: must be a drive letter by itself
no # directory following elsif ($data =~ /[A-Za-z]:/) {
$data = &gnu_drive($data);
}
# Print the variable and the data to bashenv file
print BASHENV "set $var=$data\n";
}
# Add tempsave in DOS env format to $tempsave so we can use it later
print BASHENV "set TEMPSAVE=$tempsave\n";
# If $HOME isn't set assume it is H:
if (!($ENV{'HOME'}))
{
print BASHENV "set HOME=H:\n";
}
#######################################################################
# gnu_drive takes a path and changes it from "X:\directory" to
# "//X/directory" for bash
sub gnu_drive
{
# Keep variable safe and local
local($path) = @_;
local($drive, $dir);
# if path matches "X:\directory" break it down.
if ($path =~ /[A-Za-z]:\S+/)
{
($drive, $dir) = ($path =~ /([A-Za-z]):(\S+)/);
}
# If path matches just "X:" take the drive and clear the dir
elsif ($path =~ /^[A-Za-z]:$/)
{
($drive) = ($path =~ /([A-Za-z]):/);
$dir = "";
}
if ($drive =~ /$ROOTDRIVE/i)
{
$drive = "";
}
else
{
$drive = "//$drive";
}
# If dir does not start with "\" add it.
if (!($dir =~ /^\//))
{
$dir = "/$dir";
}
# Give back the rewritten environment
return "$drive$dir";
}
# That is all of the perl script!
__END__
:endofperl
@rem Now run that batch file at the end.
@rem Run the bashenv.bat file to reset the environment to bash style
@call %TEMP%\bashenv.bat
@rem Run bash
@c:\usr\bin\bash.exe
@rem Reset the environment to the original configuration
@call %TEMPSAVE%\saveenv.bat
cut-------------------------------------------------------------------
Make sure you have perl installed in your path and run this from the
command line (I set up a short cut to it). Change the ROOTDIR
variable to the applicable drive letter, add variables that you do
not want change to "skip_vars" and away you go.
I hope this helps some one.
Later,
Rick
-------------------------------------------------------------------
o80808o | I know I am mad, I have always
|-----|ri Deja Brew... We have | been mad as most of us have.
| | | | | all been beer before | Yet you must explain why you
| | | |/ | are mad... Even if you are not
|_____| | mad.
I_____I | -Pink Floyd
-------------------------------------------------------------------
-
For help on using this list, send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".
- Raw text -