rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
From: culliton@srg.af.mil (Tom Culliton x2278)
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	[thread overview]
Message-ID: <9206262015.aa16494@ceres.srg.af.mil> (raw)

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 $*
	}
}}


                 reply	other threads:[~1992-06-27  0:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9206262015.aa16494@ceres.srg.af.mil \
    --to=culliton@srg.af.mil \
    --cc=costorm@uunet.UU.NET \
    --cc=rc@hawkwind.utcs.toronto.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).