#!/bin/rc
#
# Executes a command over SSH, taking care of the following, along the way:
# - the remote host name is your environment, $CPU, or deduced with dircpu
# - the remote working directory is deduced from local one, withing certain convention
# - the remote command line is escaped to preserve argument lists as is
# - the remote environment is initialized from your profiles
#
# To publish: cp $% /n/sources/contrib/yk/lab/unix9/
#
rfork e
fn quotesh {
whatis '*' | sed 's!\*=!!; s!^[(]!!; s![)]$!!; s!''''!''\\''''!g'
}
xflags = ()
if(~ $1 -*) {
xflags = $1
shift
}
if(~ $#CPU 0)
CPU = `{dircpu}
if(! ~ $#CPU 1){
echo >[2=1] 'x: pick one CPU'
exit usage
}
if(~ $#sshuser 1 && ! ~ $CPU *@*)
CPU = $sshuser@$CPU
plaincpu = `{echo $CPU | sed 's/.*@//'}
remdir = `{pwd | sed '
s#/n/(remote|local|'$CPU^'|'$"plaincpu^')($|/)#/#
s#^'$home^'$#.#
'}
pre = ()
if(~ $#xnoprofile 0)
pre = ($pre 'service=x; . /etc/profile; . ./.profile;')
if(~ $#xnocwd 0)
pre = ($pre '{ \cd '$remdir^'; } >&2;')
nl = '
'
argv = ()
# rewrite absolute pathnames in the arguments
for(arg) {
if(~ $arg */n/^(remote local other $CPU $plaincpu)^/* && test -e $arg) {
ifs=$nl {
arg = `{echo $arg | sed 's!^/n/[^/]*/!/!'}
}
}
argv = ($argv $arg)
}
ifs='' {
* = ($pre `{quotesh $argv})
}
ssh $xflags $CPU $"*
|