xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/dotsh.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#
2*0Sstevel@tonic-gate#   @(#)dotsh.pl                                               03/19/94
3*0Sstevel@tonic-gate#
4*0Sstevel@tonic-gate# This library is no longer being maintained, and is included for backward
5*0Sstevel@tonic-gate# compatibility with Perl 4 programs which may require it.
6*0Sstevel@tonic-gate#
7*0Sstevel@tonic-gate# In particular, this should not be used as an example of modern Perl
8*0Sstevel@tonic-gate# programming techniques.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate#
11*0Sstevel@tonic-gate#   Author: Charles Collins
12*0Sstevel@tonic-gate#
13*0Sstevel@tonic-gate#   Description:
14*0Sstevel@tonic-gate#      This routine takes a shell script and 'dots' it into the current perl
15*0Sstevel@tonic-gate#      environment. This makes it possible to use existing system scripts
16*0Sstevel@tonic-gate#      to alter environment variables on the fly.
17*0Sstevel@tonic-gate#
18*0Sstevel@tonic-gate#   Usage:
19*0Sstevel@tonic-gate#      &dotsh ('ShellScript', 'DependentVariable(s)');
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate#         where
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#      'ShellScript' is the full name of the shell script to be dotted
24*0Sstevel@tonic-gate#
25*0Sstevel@tonic-gate#      'DependentVariable(s)' is an optional list of shell variables in the
26*0Sstevel@tonic-gate#         form VARIABLE=VALUE,VARIABLE=VALUE,... that 'ShellScript' is
27*0Sstevel@tonic-gate#         dependent upon. These variables MUST be defined using shell syntax.
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate#   Example:
30*0Sstevel@tonic-gate#      &dotsh ('/foo/bar', 'arg1');
31*0Sstevel@tonic-gate#      &dotsh ('/foo/bar');
32*0Sstevel@tonic-gate#      &dotsh ('/foo/bar arg1 ... argN');
33*0Sstevel@tonic-gate#
34*0Sstevel@tonic-gatesub dotsh {
35*0Sstevel@tonic-gate   local(@sh) = @_;
36*0Sstevel@tonic-gate   local($tmp,$key,$shell,$command,$args,$vars) = '';
37*0Sstevel@tonic-gate   local(*dotsh);
38*0Sstevel@tonic-gate   undef *dotsh;
39*0Sstevel@tonic-gate   $dotsh = shift(@sh);
40*0Sstevel@tonic-gate   @dotsh = split (/\s/, $dotsh);
41*0Sstevel@tonic-gate   $command = shift (@dotsh);
42*0Sstevel@tonic-gate   $args = join (" ", @dotsh);
43*0Sstevel@tonic-gate   $vars = join ("\n", @sh);
44*0Sstevel@tonic-gate   open (_SH_ENV, "$command") || die "Could not open $dotsh!\n";
45*0Sstevel@tonic-gate   chop($_ = <_SH_ENV>);
46*0Sstevel@tonic-gate   $shell = "$1 -c" if ($_ =~ /^\#\!\s*(\S+(\/sh|\/ksh|\/zsh|\/csh))\s*$/);
47*0Sstevel@tonic-gate   close (_SH_ENV);
48*0Sstevel@tonic-gate   if (!$shell) {
49*0Sstevel@tonic-gate      if ($ENV{'SHELL'} =~ /\/sh$|\/ksh$|\/zsh$|\/bash$|\/csh$/) {
50*0Sstevel@tonic-gate	 $shell = "$ENV{'SHELL'} -c";
51*0Sstevel@tonic-gate      } else {
52*0Sstevel@tonic-gate	 print "SHELL not recognized!\nUsing /bin/sh...\n";
53*0Sstevel@tonic-gate	 $shell = "/bin/sh -c";
54*0Sstevel@tonic-gate      }
55*0Sstevel@tonic-gate   }
56*0Sstevel@tonic-gate   if (length($vars) > 0) {
57*0Sstevel@tonic-gate      open (_SH_ENV, "$shell \"$vars && . $command $args && set \" |") || die;
58*0Sstevel@tonic-gate   } else {
59*0Sstevel@tonic-gate      open (_SH_ENV, "$shell \". $command $args && set \" |") || die;
60*0Sstevel@tonic-gate   }
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate   while (<_SH_ENV>) {
63*0Sstevel@tonic-gate       chop;
64*0Sstevel@tonic-gate       m/^([^=]*)=(.*)/s;
65*0Sstevel@tonic-gate       $ENV{$1} = $2;
66*0Sstevel@tonic-gate   }
67*0Sstevel@tonic-gate   close (_SH_ENV);
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate   foreach $key (keys(%ENV)) {
70*0Sstevel@tonic-gate       $tmp .= "\$$key = \$ENV{'$key'};" if $key =~ /^[A-Za-z]\w*$/;
71*0Sstevel@tonic-gate   }
72*0Sstevel@tonic-gate   eval $tmp;
73*0Sstevel@tonic-gate}
74*0Sstevel@tonic-gate1;
75