#!/usr/bin/env perl # # workon a given cvs module. Set appropriate values and exec shell # Copyright (C) 2000 Rajesh Vaidheeswarran # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. # # $Id: workon,v 1.4 2000/08/09 18:35:38 rv Exp $ use strict; my $prog = &progname($0); %::modules = (); %::repositories = (); my $workon = $ENV{'WORKON'}; unless (defined $workon) { my $dotdir = $ENV{'DOTDIR'} || $ENV{'HOME'} || (getpwuid $>)[7] || ''; $workon = $dotdir . '/.workon'; } eval "require '$workon'"; die "Cannot load $workon." if $@; &modulesget(); my $module = shift or &usage(); &moduleslist() if $module eq '-c'; delete $ENV{'CVSROOT'}; delete $ENV{'CVS_MODULE'}; delete $ENV{'CVS_RSH'}; delete $ENV{'MODULE_ROOT'}; &usage($module) unless defined $::modules{$module}; my $moduleprops = $::modules{$module}; $ENV{'CVS_MODULE'} = $module; foreach (sort keys %{$moduleprops}) { my $val = $moduleprops->{$_}; $ENV{$_} = $val if defined $val; warn "$_ not set.\n" unless defined $val; } my $dir = $moduleprops->{'MODULE_ROOT'}; warn "MODULE_ROOT doesn't seem to be defined.\n" unless $dir; if (substr($dir, 0, 1) ne '/') { $dir = $ENV{'HOME'} . '/' . $moduleprops->{'MODULE_ROOT'}; } warn("No directory $dir\n") unless -d $dir; chdir $dir || $ENV{'HOME'} if -d $dir; exec $ENV{'SHELL'}; exit 0; sub usage { my $arg = shift || undef; print "$arg: Invalid module\n" if $arg; print "$prog \n"; unless ($arg) { print "modules: "; foreach (sort keys %::modules) { print "$_ " } print "\n"; } exit 1; } sub warn { foreach (@_) { print STDERR $_; } } sub progname { my @junk; @junk = split '/', shift; return pop @junk; } sub modulesget { my $rep; foreach $rep (sort keys %::repositories) { my $repprop = $::repositories{$rep}; my $repmod = $repprop->{'MODULES'} or next; my $repmodkey; foreach $repmodkey (sort keys %$repmod) { my $module = $repmodkey; my %moduleprops; my $repkey; foreach $repkey (sort keys %$repprop) { $moduleprops{$repkey} = $repprop->{$repkey} unless $repkey eq 'MODULES'; } $moduleprops{'MODULE_ROOT'} = $repmod->{$repmodkey}; $moduleprops{'CVSROOT'} = $rep; $::modules{$module} = \%moduleprops; } } } sub moduleslist { print "-c "; foreach (sort keys %::modules) { print "$_ " } exit 0; } __END__; =head1 NAME workon - Given a CVS B, set the appropriate variables and exec a shell. =head1 SYNOPSIS B EB<-c> | BE =head1 DESCRIPTION B sets the essential CVS related variables like CVSROOT, CVS_RSH and even other user-defined variables, and execs a shell with the current directory set to the working root directory of the module. When executed without any parameters, workon lists the modules that it knows. =head1 OPTIONS =over 10 =item B<-c> List the modules to stdout and exit. Useful for setting up completions list. For example, a B completions list for B can be setup as follows: complete workon 'p/1/`workon -c`/' This will make tcsh complete on all the module names when a B or a B is typed after B. =item B The name of a CVS module defined in the B<.workon> file. This should be unique. Module names need not be the same as the CVS module names, since this is used only to parse properties. For example, if you have a module B from cvs.gnu.org and another module B from xemacs.org, you could name one of them B and the other one B in your B<.workon> file. =head1 FILES =item B<.workon> The workon config file. The modules are defined in a hash B<%main::repositories> as follows. %main::repositories = ( ':ext:cvs.gnu.org:/gd/gnu/cvsroot' => { 'MY_VAR' => 'my val', 'CVS_RSH' => '/usr/local/bin/krsh', 'MODULES' => { 'gnu-emacs' => 'homes/fsf/src/gnu/emacs', }, }, ':pserver:anonymous@cvs.netlab.co.jp:/home/cvs' => { 'MODULES' => { 'ruby' => 'homes/rv/src/ruby', }, }, ); 1; # Must exist to satisfy require; The repositories are keyed by the B that are common to a set of modules. This is a hash key which contains any set of user-defined variables, along with variables like B, if needed for the particular repository. The B defined under a given repository is a hash of all the module names, and the working directory of those. The names B for a given B<.workon> file. Note that they need not correspond to the name of the actual CVS module itself. If this working directory is a relative path (no leading B), then it is considered relative to the home directory. Also note that any user variable is just set into the environment. =head1 ENVIRONMENT =item WORKON Location of the B<.workon> file. =item DOTDIR Try B<${DOTDIR}/.workon> if not found above. =item HOME Try B<${HOME}/.workon> if not found above. =item CVS_MODULE This gets set in the shell exec-ed by B. The value would be the name of the module that was passed to B. =item MODULE_ROOT This gets set in the shell exec-ed by B. The value would be the working directory of the module. =item CVSROOT This gets set in the shell exec-ed by B with the repository key under which the module was defined. =head1 AUTHOR Rajesh Vaidheeswarran Erv@gnu.orgE =head1 LICENSE This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA =cut