Re: (OT) path_helper [was Re: Xterm not reading dotfiles]
Re: (OT) path_helper [was Re: Xterm not reading dotfiles]
- Subject: Re: (OT) path_helper [was Re: Xterm not reading dotfiles]
- From: "Andrew J. Hesford" <email@hidden>
- Date: Sun, 18 Nov 2007 22:33:10 -0600
On Nov 18, 2007, at 8:24 PM, Mark J. Reed wrote:
IMESHO, this particular program might be best written in a different
language. C, for instance. Or Perl/Python/Ruby/Tcl - they all ship
standard with OSX, right? Any of them would probably do this more
efficiently than bash; it's not the sort of thing the shell is
optimized for.
I re-implemented path_helper in Perl, and it's attached. I'd
appreciate if people would give it a try. I'm no Perl hacker, so maybe
it's horrendously stupid, but it seems okay to me. It seems to
duplicate the exact functionality of the original path_helper. Any
existing PATH or MANPATH contents are preserved, in order. Next, the /
etc/{man,}paths files are read for defaults, and finally, the files
in /etc/{man,}paths.d are processed.
For efficient checking for duplicates, I use hashes. Perl is supposed
to be good at this, so it should be reasonably efficient. Time tests
reveal no noticeable difference in user time (0.01 sec) or system time
(0.00 sec), but the wall-clock time of my script is consistently half
of the system path_helper run time (0.011 versus 0.022 sec). This even
includes the simplified glob posted earlier by Nate.
Oh, and it works properly with LaTeXiT, too. (Does anybody care about
this but me?)
#!/usr/bin/perl -w
# A replacement for /usr/libexec/path_helper
sub pathstring {
my @apath;
while (my ($key, $value) = each (%{$_[0]})) {
$apath[$value] = $key;
}
$pstr = join ":", @apath;
return $pstr;
}
sub hashpath {
my @apath = split ":", $_[0];
my %hpath;
my $offset = 0;
foreach $element (@apath) {
next if exists ($hpath{$element});
$hpath{$element} = $offset;
$offset++;
}
return %hpath;
}
sub procdir {
my $defaults = $_[0];
my %path = %{$_[1]};
my $pathloc = $defaults . ".d";
my @pathfiles = <$pathloc/*>;
my $offset = keys %path;
if (open PATHSRC, "< $defaults") {
chomp (my @elements = <PATHSRC>);
close (PATHSRC);
foreach $element (@elements) {
next if exists ($path{$element});
$path{$element} = $offset;
$offset++;
}
}
foreach $file (@pathfiles) {
next if not open (PATHSRC, "< $file");
chomp (my @elements = <PATHSRC>);
close (PATHSRC);
foreach $element (@elements) {
next if exists ($path{$element});
$path{$element} = $offset;
$offset++;
}
}
return %path;
}
my ̃fpath;
my ̃fmans;
̃fpath = hashpath $ENV{'PATH'} if exists ($ENV{'PATH'});
̃fmans = hashpath $ENV{'MANPATH'} if exists ($ENV{'MANPATH'});
my %newpath = procdir '/etc/paths', \̃fpath;
my %newmans = procdir '/etc/manpaths', \̃fmans;
my $pathstr = pathstring \%newpath;
my $manstr = pathstring \%newmans;
if ($#ARGV >= 0 && $ARGV[0] eq "-c") {
print "setenv PATH \"$pathstr\"\n";
print "setenv MANPATH \"$manstr\"\n";
} else {
print "export PATH=\"$pathstr\"\n";
print "export MANPATH=\"$manstr\"\n";
}
--
Andrew J. Hesford <email@hidden>
Department of Electrical and Computer Engineering
University of Illinois at Urbana-Champaign
_______________________________________________
Do not post admin requests to the list. They will be ignored.
X11-users mailing list (email@hidden)
This email sent to email@hidden