rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* "pathos" script
@ 1992-06-17 16:09 John Mackin
  0 siblings, 0 replies; only message in thread
From: John Mackin @ 1992-06-17 16:09 UTC (permalink / raw)
  To: The rc Mailing List

I guess I should mail this one to the list.

You know how you often want to do something that does `just that
little bit extra'?  Like, in that script front-end I sent earlier,
you want to call that one "script" too and then have it execute
the command by the same name further along $path?  In my earlier
mail, I wrote "exec /full/path/to/real/script", but I always thought
it was a right bummer to have to wire those pathnames in.  Of course,
if the frontend you are writing is a function, you win since you
can just use "builtin" to get to the executable; but there's
nothing built-in to rc to help you in the more common case
where the frontend you are writing is a script.

Naturally, it doesn't have to be built in to rc; here it is.
I've been using this one for ages but didn't get around to sending
it out until now, so it should be solid.

You use it, say in my "script" example, by ending the command with:

exec pathos $0 $*

As simple as that.  It has one known deficiency, which I can't think
of a nice fix for: if you are in the directory containing the frontend
when you run it, then it gets exec'd with a relative pathname and the
script won't work.  In practice this doesn't present a problem.

It illustrates a neat technique for eliminating export of variables
(useful here since I often use pathos in a context that will end
up spawning an interactive shell somewhere down the track).  The
reason the variables all have funky names starting with underscore
is to minimise the chances of one of the variables clashing with
the name of the command you want to execute (i.e., the name of
the frontend), because in that case my trick with "whatis" won't
work right.  I did try, ages ago, to convince Byron that whatis
needed to take options so that I wouldn't need to do this, but
he wouldn't wear it, so...

(Like:

	whatis -e	shows only an executable
	whatis -v	shows only a variable
	whatis -f	shows only a function

Then I could have just used whatis -e and gone to a lot less
trouble -- and not needed funky names.)

Enjoy.

OK,
John.

#!/full/path/to/rc

#
# usage: pathos /full/path/to/command [args]
#
# The idea is that often we want to supersede a command that's further
# down the path.  This gives us the same function in a script -- pick
# up the command of the same name further along -- that we can get by
# using `builtin' in a function.
#

_Vars = ( _Vars _me _dir _cmd _tpath _chop _fatal )

_me = ` { basename $0 }

fn _fatal {
	echo $_me ^ : $* >[1=2]
	exit 1
}

if ( ~ $#* 0 )
	_fatal usage: $_me /full/path/to/command '[args]'

if ( ! ~ $1 /* )
	_fatal first argument must be a full pathname

_dir = ` { dirname $1 }
_cmd = ` { basename $1 }
shift

if ( ! ~ $_dir $path )
	_fatal can''''t find directory $_dir in path

if ( ~ $_cmd $_Vars )
	_fatal command name $_cmd matches an internal variable or function

fn _chop {
	shift
	_tpath = $*
}

_tpath = $path
for ( p in $path ) {
	_chop $_tpath
	if ( ~ $p $_dir )
		break
}
p = ()

if ( ~ $#_tpath 0 )
	_fatal directory $_dir is the last one in the path


if ( ! path = $_tpath $_cmd = () whatis $_cmd >/dev/null >[2=1] )
	_fatal command $_cmd not found in path fragment $_tpath

pid = ` { path = $_tpath $_cmd = () whatis $_cmd }		# chortle

for ( v in $_Vars ) $v = ()
v = ()
fn _chop
fn _fatal

exec $pid $*


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

only message in thread, other threads:[~1992-06-17 16:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-06-17 16:09 "pathos" script John Mackin

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).