rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* An export solution for rc
@ 1992-06-27  0:15 Tom Culliton x2278
  0 siblings, 0 replies; only message in thread
From: Tom Culliton x2278 @ 1992-06-27  0:15 UTC (permalink / raw)
  To: costorm, rc

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 <command and args to be run with stripped down 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 <command and args to be run with stripped down 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 $*
	}
}}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1992-06-27  0:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-06-27  0:15 An export solution for rc Tom Culliton x2278

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).