This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

"The PATH problem"


Hello,

Its interesting that Ehud Karni posted a shell script for mount tables and a 
request came in for suggestions about 'pushing Cygwin out' to many 
machines. I have been thinking a bit about Cygwin setup since I installed 
freshly three time over the year-end season, and each time the 
configuration I wanted to end up with was somewhat different. I learned a 
little more about Cygwin (still have a L O N G way to go).

logins are a funny thing -- users seem very specific and individualistic about 
that and everybody seems to have a slightly different preference. So my 
offering today might not be of much interest to the older users on this List, 
who long since made up their own minds how to do what they like on login. 
But newer users like I am might stumble across this message and I might 
even be so bold as to propose that my contribution be made known and 
available to them somehow.

As I see it the "PATH" problem is this: many people who use Cygwin are 
developers and developers, more than ordinary users (is such a thing?) 
often maintain multiple build environments (MinGW, Cygwin-DLL-based, 
etc), add new tools, and may do all sorts of frequent changes. In order to 
make such things easier there are diferent ways to go, I suppose. One could 
umount and then re-mount to different points everything that one needs, 
so that all the PATH-dependent processes unleashed by the hacker in a 
typical working session find the right places. Another way is to re-adjust the 
PATH list to point towards what one needs, precedence of course being of 
the essence especially where there are multiple versions of executables 
located in different branches on the filesystem.

Cygwin doesn't allow modification or unset on the PATH variable after 
login, so it has to be set right from 'Doze before bash gets fired up, no? This 
reality led me to concoct a little script to make setting up the PATH and 
guaranteeing no unpleasant surprises a bit easier. Rather than repeatedly 
typing in the needed SET commands every time before entering bash, I 
have Perl do it (such a thing could probably be done another way using a 
shell script but I am a Perl hacker .. not a `sed' wiz).

My script has a lot of points that one could single out and find a shortcoming 
or debatable. I just offer it, I don't claim it's elegant or perfect.

One such point is that some may want there not to be a dependency on a 
special Perl module that isn't part of standard core Perl and may need to be 
installed via CPAN before this will work. That module is "Tie::IxHash" (G. 
Sarathy, author) which allows one to employ the freak hybrid critter of a 
Perl "order [precedence]-retaining hash" (a tie-ed 'IxHash') in one's 
program. OTOH it's a simple install (no xs DLLs, simply copying the .pm file 
to the right location will suffice in a pinch). I have decided to simply attach 
the module to this message.

(PERL THEORY)This (use of such a module) is most desirable because the 
use of a hash is a means of cleanly eliminating duplicate entries without 
complex looping in one's script. At the same time, it is very good to be able 
to preserve the order of the entries in $PATH so that precedence works in 
one's favor.(/PERL THEORY)

Using this script one can "hard-code" paths which one needs to absolutely 
be present first in the PATH, followed by paths that already exist in there 
(like Windows/system[32]) and need to be retained -- but with no 
duplication.

Another point which might irritate the picky user is that a short but 
perceptible (on my system, maybe 1.5 sec) delay in accomplishing login is 
introduced.

The script prints out to STDOUT a list of the resulting {Windows-style} 
paths (and a user could want it to do more: to make a table showing both 
the styles corresponding, that's maybe a TO-DO) and writes to a ".file.bat" 
file (so named from the convention that a file beginning with a dot is a 
system configuration file that is written to/read from by processes and 
should be hidden most of the time) to accomplish the little trick of changing 
the PATH from inside a process that then ends (Perl beginner FAQ: there is 
no way [short of such hackery] to change the PATH from inside Perl so that 
it remains changed after program exit ...).

In order to do this, this requires TWO scripts, not a single file: 1 .BAT script 
which calls the perl and then reads-in the .file.bat after perl exists.

Script one ("cygwin.bat") follows:

-----  script  ----  watch for wrapping and low-flying owls ---
@ECHO OFF
D:\usr\local\bin\perl5.00503.exe D:\Cygwin\setpath.p

REM the next line sets the path from the file just written
REM by the Perl script above.
CALL D:\usr\.pathfix.bat
D:
SET CYGWIN=binmode tty ntea nontsec
@ECHO ON
\Cygwin\bin\bash --login -i

---- end script ----- cut here ---- use no hooks ----




Script two, the Perl script, follows:

---- start script -- watch for wrapping -----
#!D:\usr\local\bin\perl5.00503.exe -w  # <-- user configuration

# Perl script "setpath.p"
# Copyright (c)2001 Soren Andersen <libertador@flashmail.com>
# This is Free Software, it may be freely redistributed, modified,
# and copied, but this notice must be retained. NO warrantee,
# express or implied, is given; and the user assumes all risk from
# the use of this program.

use Tie::IxHash;

my ($HW, %Pash, $Pct, $Psep, @getP);
  $HW = 'hardwired';  $Pct = 1;
chdir 'D:\\Cygwin'; # User conguration here and next several lines
my $ixH = tie( %Pash, Tie::IxHash,
    	 q[D:\\Cygwin\\usr\\i686-pc-cygwin\\bin] => "$HW ". $Pct++,
		 q[D:\\usr\\local\\bin] => "$HW ". $Pct++,
		 q[D:\\Cygwin\\bin] => "$HW ". $Pct++,
          );
die qq[\nFailure to tie IxHash: $!] unless $ixH;
 if ($] =~m@5\.00503@i or $ENV{SHELL} =~m@/bin/sh/@)    {
      $Psep = ':';
      @getP = map {my $wP = `bin/cygpath -paw $_`; $wP;}
                             split $Psep, qx'echo $PATH';
 } else {
      $Psep = ';';
      @getP = split $Psep, $ENV{PATH};
 }
 for(@getP) {
      chomp $_;
      next if m@^\.$@;  # Cygwin adds dot '.' to end anyway, don't duplicate.
      $_ = ucfirst($_);
	  next if m@D:\\ActivePerl\\bin\Z@i;  # user configuration req'd (*see bottom)
	  $Pash{$_} = $Pct++ unless not -d $_ or exists $Pash{$_};
 }
 print STDERR qq[\n\n],'_' x 16,'  PATH VARIABLE  ' ,'_' x 16, qq[\n\n];

 open( BATF,q[> D:\\usr\\.pathfix.bat] ) or die $!;  # user configuration req'd
 
 print BATF qq[REM this file is generated automatically ],
            qq[each time Cygwin.bat is run\nSET PATH=];

 for (keys %Pash)   {
        if ($Pash{$_} =~m@^\d+$@) {
           printf STDERR "%2u", $Pash{$_};
        } else {
           printf STDERR "%s", $Pash{$_};
        }
        printf STDERR "%s", qq[ ~ $_\n];
		print BATF $_ , q[;];
 }
 print STDERR qq=The PATH has been adjusted for Cygwin use.\n=;
 close(BATF);

__END__


Notes:

I think this script is mostly self-explanatory and anyway I am
too busy to write proper pod right now ;-). But a note about *:
  The system setup I have is one in which several different
  versions of Perl are present, and I usually have one (the
  ActivePerl port) in use in my general 'Doze environment. But
  I need to remove it so that my Cygwin Perl is found instead, so
  the configuration of this script on MY system requires the use
  of the regex to remove such a PATH item. Similarly, other users
  (assuming adequate proficiency with perl regex's) could use the
  marked section to 'filter out' any undesirable PATH entries
  (as opposed to leaving them in but at low precedence).


---- end script ---- cut here ---- use no hooks -----
The following section of this message contains a file attachment
prepared for transmission using the Internet MIME message format.
If you are using Pegasus Mail, or any another MIME-compliant system,
you should be able to save it or view it from within your mailer.
If you cannot, please ask your system administrator for assistance.

   ---- File information -----------
     File:  IxHash.pm
     Date:  20 Nov 1997, 16:59
     Size:  13918 bytes.
     Type:  Text

IxHash.pm

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]