OpenPKG CVS Repository
http://cvs.openpkg.org/
____________________________________________________________________________
Server: cvs.openpkg.org Name: Thomas Lotterer
Root: /e/openpkg/cvs Email: [EMAIL PROTECTED]
Module: openpkg-tools Date: 01-Jul-2004 16:53:40
Branch: HEAD Handle: -NONE-
Modified files:
openpkg-tools/cmd dev.sh
Log:
suck in latest variant of smart dealing with temporary root privileges
Summary:
Revision Changes Path
1.25 +170 -6 openpkg-tools/cmd/dev.sh
____________________________________________________________________________
patch -p0 <<'@@ .'
Index: openpkg-tools/cmd/dev.sh
============================================================================
$ cvs diff -u -r1.24 -r1.25 dev.sh
--- openpkg-tools/cmd/dev.sh 17 Jun 2004 10:40:26 -0000 1.24
+++ openpkg-tools/cmd/dev.sh 1 Jul 2004 14:53:40 -0000 1.25
@@ -354,18 +354,179 @@
openpkg rpm2cpio "$@"
}
+# generate shell script for re-evaluation of command arguments
+# input: foo 'bar baz' quux (3 args)
+# output: "foo \"bar baz\" quux" (1 arg)
+_arg2sh () {
+ local opt_e=no
+ local opt_t=no
+ local opt
+ OPTIND=1
+ while getopts et opt; do
+ case ${opt} in
+ e ) opt_e=yes ;;
+ t ) opt_t=yes ;;
+ esac
+ done
+ shift $(($OPTIND - 1))
+ local cmd=""
+ local arg
+ for arg in "$@"; do
+ # NOTICE: there are three(!) escaping layers here:
+ # - escape layer 1: backticks (`..xx..`)
+ # - escape layer 2: quotes ('..xx..')
+ # - escape layer 3: sed regex (s;..xx..;..xx..;)
+ local orig=$arg
+ if [ ${opt_e} = yes ]; then
+ arg=`echo "${arg}" | sed -e 's;\\\\;\\\\\\\\;g' -e
's;\\(["]\\);\\\\\\1;g'`
+ elif [ ${opt_t} = yes ]; then
+ arg=`echo "${arg}" | sed -e 's;\\\\;\\\\\\\\;g' -e
's;\\(["!\$\`]\\);\\\\\\1;g'`
+ fi
+ local quote=no
+ if [ "${arg}" != "${orig}" ]; then
+ quote=yes
+ else
+ quote=`echo "X${arg}" | sed -e 's;^X.*[ ].*\$;yes;' -e 's;^X.*;no;'`
+ fi
+ if [ ${quote} = yes ]; then
+ arg="\"${arg}\""
+ fi
+ cmd="${cmd}${cmd:+ }${arg}"
+ done
+ printf "%s" "${cmd}" # intentionally not echo(1)
+}
+
+# generate shell script for re-evaluation of command arguments
+# input: foo 'bar baz' quux (3 args)
+# output: "foo \"bar baz\" quux" (1 arg)
+_arg2sh () {
+ local opt_e=no
+ local opt_t=no
+ local opt
+ OPTIND=1
+ while getopts et opt; do
+ case ${opt} in
+ e ) opt_e=yes ;;
+ t ) opt_t=yes ;;
+ esac
+ done
+ shift $(($OPTIND - 1))
+ local cmd=""
+ local arg
+ for arg in "$@"; do
+ # NOTICE: there are three(!) escaping layers here:
+ # - escape layer 1: backticks (`..xx..`)
+ # - escape layer 2: quotes ('..xx..')
+ # - escape layer 3: sed regex (s;..xx..;..xx..;)
+ local orig=$arg
+ if [ ${opt_e} = yes ]; then
+ arg=`echo "@${arg}" | sed -e 's;^@;;' -e 's;\\\\;\\\\\\\\;g' -e
's;\\(["]\\);\\\\\\1;g'`
+ elif [ ${opt_t} = yes ]; then
+ arg=`echo "@${arg}" | sed -e 's;^@;;' -e 's;\\\\;\\\\\\\\;g' -e
's;\\(["!\$\`]\\);\\\\\\1;g'`
+ fi
+ local quote=no
+ if [ "${arg}" != "${orig}" ]; then
+ quote=yes
+ else
+ quote=`echo "@${arg}" | sed -e 's;[EMAIL PROTECTED] ].*\$;yes;' -e
's;[EMAIL PROTECTED];no;'`
+ fi
+ if [ ${quote} = yes ]; then
+ arg=`echo "@${arg}" |\
+ sed -e 's;[EMAIL PROTECTED]([A-Z][A-Z0-9_]*\)=\(.*\)$;\1="\2";' \
+ -e 's;[EMAIL PROTECTED](.*\)$;"\1";'`
+ fi
+ cmd="${cmd}${cmd:+ }${arg}"
+ done
+ printf "%s" "${cmd}" # intentionally not echo(1)
+}
+
+# generate shell script for re-evaluation of environment variables
+# input: PATH HOME (2 args)
+# output: "PATH=\"...\"; HOME=\"...\"" (1 arg)
+_env2sh () {
+ local cmd=""
+ local arg
+ for arg in "$@"; do
+ local val
+ eval "val=\"\$${arg}\""
+ # NOTICE: there are three(!) escaping layers here, too.
+ val=`echo "@${val}" | sed -e 's;^@;;' -e 's;\\\\;\\\\\\\\;g' -e
's;\\(["!\$\`]\\);\\\\\\1;g'`
+ cmd="${cmd}${cmd:+; }${arg}=\"${val}\""
+ done
+ printf "%s" "${cmd}" # intentionally not echo(1)
+}
+
# smart dealing with temporary root privileges
+_root () {
+ # default operation
+ if [ $# -eq 0 ]; then
+ set -- -i
+ fi
+
+ # parse command line options
+ local opt_i=no # interactive shell
+ local opt_l=no # last command-line
+ local opt_e=no # remote expansion mode
+ local opt
+ OPTIND=1
+ while getopts ileh opt; do
+ case ${opt} in
+ i ) opt_i=yes ;;
+ l ) opt_l=yes ;;
+ e ) opt_e=yes ;;
+ h ) echo "root:Usage: root [-h] [-l] [-e] command"; return 0 ;;
+ ? ) echo "root:Error: invalid command line"; return 1 ;;
+ esac
+ done
+ shift $(($OPTIND - 1))
+
+ # determine action(s)
+ local prolog=":"
+ local epilog=":"
+ local command
+ if [ ${opt_i} = yes ]; then
+ # enter interactive session with root privileges
+ local xtermcontrol=`find_tool xtermcontrol xtermcolors`
+ if [ -n "${xtermcontrol}" ]; then
+ prolog="${xtermcontrol} --cursor='#cc3333'"
+ epilog="${xtermcontrol} --cursor='#000000'"
+ command="PS1=\"\\[\\e[31;[EMAIL PROTECTED]:\\w\\n\\\\\\$ \"; export PS1"
+ fi
+ command="${command}${command:+; }exec ${SHELL} --norc --noprofile"
+ elif [ ${opt_l} = yes -a ${opt_e} = yes ]; then
+ eval "set -- $(builtin history 2 | sed -n -e '1p' | cut -c8-)"
+ command=$(_arg2sh -e -- "$@")
+ elif [ ${opt_l} = yes ]; then
+ # execute last command-line with root privileges
+ command=$(builtin history 2 | sed -n -e '1p' | cut -c8-)
+ elif [ ${opt_e} = yes ]; then
+ # execute given command-line with root privileges (expansion mode)
+ command=$(_arg2sh -e -- "$@")
+ else
+ # execute given command-line with root privileges (transparent mode)
+ command=$(_arg2sh -t -- "$@")
+ fi
+
+ # generate action command
+ local cmd=$(_env2sh ${ROOT_ENV:-PATH MANPATH INFOPATH LD_LIBRARY_PATH TERM HOME
EDITOR PAGER LOGNAME USER})
+ cmd="${cmd}; cd ${PWD} && ${command}"
+ cmd=$(_arg2sh -t -- "${cmd}")
+ eval ${prolog}; ssh -t -q -x [EMAIL PROTECTED] ${SHELL} -c "${cmd}"; rv=$?;
eval ${epilog}
+
+ # exit with return value of remote command
+ return $rv
+}
+
+# smart dealing with temporary root privileges with dev.sh specifics
root () {
- if [ ".$1" = . ]; then
- ssh -t -x [EMAIL PROTECTED] builtin cd ${PWD} \&\& ${SHELL}
- elif [ ".$1" = .rpm ]; then
+ if [ ".$1" = .rpm ]; then
initmpx $E
shift
if [ ".${RPMCMD}" = . ]; then
echo "$0:ERROR: no openpkg rpm found at instance \"$E\""
return 1
fi
- ssh -t -x [EMAIL PROTECTED] builtin cd ${PWD} \&\& HOME=${OPENPKG_WORK}
command ${RPMCMD} "$@"
+ _root HOME=${OPENPKG_WORK} command ${RPMCMD} "$@"
elif [ ".$1" = .openpkg ]; then
initmpx $E
shift
@@ -373,9 +534,9 @@
echo "$0:ERROR: no openpkg multiplexer found at instance \"$E\""
return 1
fi
- ssh -t -x [EMAIL PROTECTED] builtin cd ${PWD} \&\& HOME=${OPENPKG_WORK}
command ${MPX} "$@"
+ _root HOME=${OPENPKG_WORK} command ${MPX} "$@"
else
- ssh -x [EMAIL PROTECTED] builtin cd ${PWD} \&\& "$@"
+ _root "$@"
fi
}
@@ -1170,6 +1331,9 @@
dumpfunc openpkg_dev_execute >>.bashrc
dumpfunc openpkg_dev_tools >>.bashrc
dumpfunc openpkg_dev_srcdir >>.bashrc
+ dumpfunc _arg2sh >>.bashrc
+ dumpfunc _env2sh >>.bashrc
+ dumpfunc _root >>.bashrc
dumpfunc root >>.bashrc
dumpfunc cvs >>.bashrc
dumpfunc -i bashrcepilog >>.bashrc
@@ .
______________________________________________________________________
The OpenPKG Project www.openpkg.org
CVS Repository Commit List [EMAIL PROTECTED]