Sourcing with a twist of Parsing ? 
Author Message
 Sourcing with a twist of Parsing ?

I've got some files (cov_sourceme and prot_sourceme) that need sourcing
before a program is called.

When sourced each one makes the program behave differently and I'd like
to be able to switch "modes" with a Perl script.

I've tried :

|  system ($source_cov, $run_sim);

where $source_cov contains the string to source cov_sourceme and
$run_sim contains the string to execute the program - in this case a
simulator i.e. :

|  $source_cov = "source /path/cov_sourceme";
|
|  $run_sim = "<long line of swtiches>";

The file cov_sourceme contains a load of 'setenv' and 'set' commands.

I get errors because cov_sourceme also sources some files itself when
run.  In bash this would be okay but the system command doesn't like it.

Now, I've written environmental variable parsers before that pick out
the 'setenv' and 'set' commands and make use of %ENV in perl.

I wondered whether I'd be better to write a somewhat more complicated
version which searches out 'source' commands, locates the files they
source and applies the environment parsing on them until everything is
parsed.  This could be quite complicated looking a little deeper into
the files.

I was wondering what you guys/girls thought of this, is there another
way ?

Any advice / help would be much appreciated.

Andy        - Computers are like air conditioners,

                - They stop working when you open Windows.



Sat, 01 May 2004 15:17:11 GMT  
 Sourcing with a twist of Parsing ?

Quote:

> I've got some files (cov_sourceme and prot_sourceme) that need
> sourcing before a program is called.

Sourcing means interpreting by a shell.  I dunno which shell, since you
haven't said, though I would guess csh.

Quote:
> When sourced each one makes the program behave differently and I'd
> like to be able to switch "modes" with a Perl script.

> I've tried :

> |  system ($source_cov, $run_sim);

> where $source_cov contains the string to source cov_sourceme and
> $run_sim contains the string to execute the program - in this case a
> simulator i.e. :

> |  $source_cov = "source /path/cov_sourceme";
> |
> |  $run_sim = "<long line of swtiches>";

> The file cov_sourceme contains a load of 'setenv' and 'set' commands.

Sounds like csh.

Quote:
> I get errors because cov_sourceme also sources some files itself when
> run.  In bash this would be okay but the system command doesn't like
> it.

Because you're using the system command wrong.  If you pass a single
item, it splits on whitespace: the first item in the resulting list is
the program, either a relative or absolute path, or something findable
in the PATH environment variable, and each of the other things in the
result list are arguments; each element is one argument.  If you pass a
list of items, then the first must be the program, either a relative or
... in the PATH, and each of the other things in the list are arguments
etc.

You've combined the program [well, it isn't really a program, which is
why you're having problems, but anyway], "source", with it's first
argument /path/cov_sourceme, and you've combined all the switches into
one string.

If "source" were a real program, you would be calling it something like
this:


or like this:
   system("$source_cov $run_sim");

Unfortunatly for you, "source" isn't a real program -- it's a shell
builtin command... so of course system won't find it.

Even if it did find it, you would still have a problem -- system starts
a new, seperate environment, and in that environment, runs the program
which was given as it's argument.  Any changes made in that environment
do not exist within perl's environment... so any setenvs which occur
there won't affect the perl process.

Quote:
> Now, I've written environmental variable parsers before that pick out
> the 'setenv' and 'set' commands and make use of %ENV in perl.

Neat idea... but what does your parser do if these variables are set to
things more complicated than simple strings, or if there is shell flow
control in the file?  [Flow control means things like if/else/ etc.]

Quote:
> I wondered whether I'd be better to write a somewhat more complicated
> version which searches out 'source' commands, locates the files they
> source and applies the environment parsing on them until everything is
> parsed.  This could be quite complicated looking a little deeper into
> the files.

You mean that in addition to recognizing 'set' and 'setenv', recognize
'source', and apply it [recursively]?

If you follow that route, you might end up with a csh parser written in
perl.  Blech.

Quote:
> I was wondering what you guys/girls thought of this, is there another
> way ?

Parsing csh isn't really that great an idea... a better would be:

my %acceptable_imports = list of things it's ok to get from the script.

foreach (qx[$source_cov $run_sim >> /dev/null; /bin/env]) {
    no re 'taint';
    next unless my ($key, $val) = /^([^=]*)=(.*)/;
    next unless $acceptable_imports{$key};
    $ENV{$key} = $val;

Quote:
}

Not having some sort of filter to *only* import things you consider safe
to import would be quite unsafe, and could also lead to strange and hard
to find bugs.  If you don't know all the names of the things you
consider 'acceptable', but do know what they "look like", then use a
regex.  For example, if your source files contained stuff for setting up
a database environment, your code might look like this:

foreach (qx[. /path/setup_database >> /dev/null; /bin/env]) {
    no re 'taint';
    next unless my ($key, $val) = /^([^=]*)=(.*)/;
    next unless $key =~ /^DB[ID]_/;
    $ENV{$key} = $val;

Quote:
}

This would import only DBI_* and DBD_* variables, but no others.

--
Klein bottle for rent - inquire within.



Fri, 07 May 2004 03:31:54 GMT  
 Sourcing with a twist of Parsing ?

Quote:

>my %acceptable_imports = list of things it's ok to get from the script.

    ^
    ^

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Fri, 07 May 2004 04:03:48 GMT  
 Sourcing with a twist of Parsing ?

Quote:


> >my %acceptable_imports = list of things it's ok to get from the
> >script.
>     ^
>     ^


Yup, that was a typo.

--
Klein bottle for rent - inquire within.



Sat, 08 May 2004 00:53:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Because twisted minds think twisted things...

2. How about a little twist on the shredding?

3. char to number conversion w/ a twist

4. search with a twist

5. s2p twisted

6. Random Numbers with a Twist

7. A problematic twist with =~ and regex

8. Counter with an NT twist

9. Alphabetizing with a twist

10. Not sourcing .cshrc

11. Sourcing from within perl ?

 

 
Powered by phpBB® Forum Software