From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from relay1.UU.NET ([137.39.1.5]) by hawkwind.utcs.toronto.edu with SMTP id <2717>; Fri, 26 Jun 1992 20:49:25 -0400 Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP (5.61/UUNET-internet-primary) id AA24161; Fri, 26 Jun 92 20:49:09 -0400 Received: from srg.UUCP by uunet.uu.net with UUCP/RMAIL (queueing-rmail) id 204828.9492; Fri, 26 Jun 1992 20:48:28 EDT Received: from ceres.srg.af.mil by srg.srg.af.mil id aa03298; Fri, 26 Jun 92 20:15:19 EDT From: culliton@srg.af.mil (Tom Culliton x2278) X-Mailer: SCO System V Mail (version 3.2) To: costorm@uunet.UU.NET, rc@hawkwind.utcs.toronto.edu Subject: An export solution for rc Date: Fri, 26 Jun 1992 20:15:26 -0400 Message-Id: <9206262015.aa16494@ceres.srg.af.mil> Well after much tinkering here two (tested!) alternatives for dealing with the export problem. Both are implemented entirely with builtins, as indeed they must be, when the enviroment reachs the size where only rc can handle it. ;-) Note that like the orginals these are setup so that you can give them multiple names (e.g. "fn clean_env gmake gcc sh { ...") and they will transparently run the commands with their environments reduced as specified. Each has it's appropriate domain, "clean_env" is easy to use and very thorough but relatively slow. It's not something to run an external command in the inner most loop of a script with. On the other hand strip_env is much faster and simpler, but listing every variable and function to be "unexported" is tedious and messy. Maybe we should put together a FAQ to be sent to new subscribers to the mailing list and eventually distributed with future releases with hints like Chris's prompt trick, and examples like this. I've had to rediscover a bunch of things that "everyone knows" or "we figured out months ago". ------------------------------------------------------------------------------ #!/bin/rc # This code can be fooled by really pathological cases involving embedded # newlines followed by either 'fn ' or '='. Fixing this is left as an # exercise for the reader. ;-) It's already pretty slow. # Usage: clean_env # Only "exported" variables will be passed through exported=() fn export { exported=($exported $*) } fn clean_env { _i=() _n=() _x=() @{ _x=($exported _i _n _x '*') for (_i in `` ($nl) {whatis}) { if (~ $_i 'fn '*) { _n=`` (' ') {echo $_i} fn $_n(2) } else if (~ $_i *'='*) { _n=`` ('=') {echo $_i} if (! ~ $_n(1) $_x) { $_n(1)=() } } } _i=(); _n=(); _x=(); fn clean_env if (~ $0 clean_env) { exec $* } else { exec builtin $0 $* } }} # You can also do it this way, which is MUCH faster, but specifying all # the things to unexport can be a royal pain in the butt. This is # definitely the way to go if the function has to be invoked inside of a # loop. You can also fiddle clean_env above into a function to build the # unexported list. # Usage: strip_env # Variables and functions which are "unexported" will be removed from # the environment before the command is executed unexported=() fn unexport { unexported=($unexported $*) } fn strip_env { _i=() @{ for (_i in $unexported) { $_i=() fn $_i } if (~ $0 strip_env) { exec $* } else { exec builtin $0 $* } }}