kinetic-devel/tools/rosbash/roszsh

712 lines
21 KiB
Plaintext

# roszsh - functions to support ROS users
# Useful things to know:
# 'local' variables get unset after function, all others stay forever
# 'reply' is the var used by zsh compctl builtin
function _rossed {
if [[ `uname` == Darwin || `uname` == FreeBSD ]]; then
sed -E "$@"
else
sed -r "$@"
fi
}
function _rosfind {
if [[ `uname` == Darwin || `uname` == FreeBSD ]]; then
# BSD find needs -E for extended regexp
find -E "$@"
else
find "$@"
fi
}
# _ros_location_find
# based on $ROS_LOCATIONS, which the user can set to any colon
# separated of key=folder pairs also resolves keys 'log' and
# 'test_results' to ROS defaults finally resolves to package, then
# stack echoes its result
function _ros_location_find {
local homedir ROS_LOCATION_KEYS_ARR ROS_LOCATIONS_ARR loc
homedir=`echo $HOME | sed -e "s/\//\t\//g" -e "s/\t/\\\\\/g"`
ROS_LOCATION_KEYS_ARR=(`echo $ROS_LOCATIONS | _rossed -e 's/([^:=]*)=([^:=]*)(:*[^=])*(:|$)/\1 /g'`)
ROS_LOCATIONS_ARR=(`echo $ROS_LOCATIONS | _rossed -e 's/([^:=]*)=([^:=]*)(:*[^=])*(:|$)/\2 /g' -e "s/~/${homedir}/g"`)
for (( i = 1 ; i <= ${#ROS_LOCATION_KEYS_ARR[@]} ; i++ )); do
if [[ $1 == ${ROS_LOCATION_KEYS_ARR[$i]} ]]; then
echo ${ROS_LOCATIONS_ARR[i]}
return 0
fi
done
if [[ $1 == log ]]; then
echo `roslaunch-logs`
return 0
elif [[ $1 == test_results ]]; then
echo `rosrun rosunit test_results_dir.py`
return 0
fi
loc=`export ROS_CACHE_TIMEOUT=-1.0 && rospack find $1 2> /dev/null`
if [[ $? != 0 ]]; then
loc=`export ROS_CACHE_TIMEOUT=-1.0 && rosstack find $1 2> /dev/null`
if [[ $? != 0 ]]; then
return 1
fi
echo $loc
return 0
fi
echo $loc
return 0
}
function _ros_list_locations {
local ROS_LOCATION_KEYS packages stacks
ROS_LOCATION_KEYS=`echo $ROS_LOCATIONS | _rossed -e 's/([^:=]*)=([^:=]*)(:*[^=])*(:|$)/\1 /g'`
packages=`export ROS_CACHE_TIMEOUT=-1.0 && rospack list-names`
stacks=`export ROS_CACHE_TIMEOUT=-1.0 && rosstack list-names`
echo $packages $stacks log test_results $ROS_LOCATION_KEYS | tr ' ' '\n'
return 0
}
function _ros_decode_path {
local rosname rosdir reldir last rospackdir rosstack_result rosstackdir
rosvals=()
if [[ -z $1 ]]; then
return 0
fi
echo $1 | grep -G '.\+/.*' > /dev/null
if [[ $? == 0 ]]; then
rosname=${1%%/*}
reldir=/${1#*/}
last=${reldir##*/}
reldir=${reldir%/*}/
else
rosname=$1
if [[ -z $2 || $2 != "forceeval" ]]; then
rosvals=(${rosname})
return 1
fi
fi
if [[ $rosname == ros ]]; then
rosdir=`rosstack find ros`
elif [[ $rosname == pkg ]]; then
rosdir=${ROS_PACKAGE_PATH%%:*}
elif [[ $rosname == log ]]; then
rosdir=`roslaunch-logs`
elif [[ $rosname == test_results ]]; then
rosdir=`rosrun rosunit test_results_dir.py`
else
rospackdir=`export ROS_CACHE_TIMEOUT=-1.0 && rospack find $rosname 2> /dev/null`
rospack_result=$?
rosstackdir=`export ROS_CACHE_TIMEOUT=-1.0 && rosstack find $rosname 2> /dev/null`
rosstack_result=$?
if [[ $rospack_result == 0 ]]; then
rosdir=$rospackdir
elif [[ $rosstack_result == 0 ]]; then
rosdir=$rosstackdir
else
rosvals=(${rosname})
return 1
fi
fi
rosvals=(${rosname} ${rosdir} ${reldir} ${last})
}
function rospython {
if [[ $1 = "--help" ]]; then
echo -e "usage: rospython [package] \n\nRun python loading package manifest first."
return 0
fi
if [[ -z $1 ]]; then
if [[ -f ./manifest.xml ]]; then
pkgname=`basename \`pwd\``
python -i -c "import roslib; roslib.load_manifest('$pkgname')"
else
python
fi
else
python -i -c "import roslib; roslib.load_manifest('$1')"
fi
}
function roscd {
local rosvals
if [[ $1 = "--help" ]] | [[ $# -gt 1 ]]; then
echo -e "usage: roscd package\n\nJump to target package."
return 0
fi
if [ -z $1 ]; then
if [ -z $ROS_WORKSPACE ]; then
echo -e "No ROS_WORKSPACE set. Please set ROS_WORKSPACE to use roscd with no arguments."
return 1
fi
cd ${ROS_WORKSPACE}
return 0
fi
_ros_decode_path $1 forceeval
if [ $? != 0 ]; then
echo "roscd: No such package '$1'"
return 1
elif [ -z ${rosvals[1]} ]; then
if [ -z $ROS_WORKSPACE ]; then
echo -e "No ROS_WORKSPACE set. Please set ROS_WORKSPACE to use roscd with no arguments."
return 1
fi
cd ${ROS_WORKSPACE}
return 0
else
cd ${rosvals[2]}${rosvals[3]}${rosvals[4]}
return 0
fi
}
function _is_integer {
[ "$1" -eq "$1" ] > /dev/null 2>&1
return $?
}
function rosd {
if [[ $1 = "--help" ]]; then
echo -e "usage: rosd\n\nDisplays the list of currently remembered directories with indexes."
return 0
fi
let count=0;
for items in `dirs`;
do
echo $count $items;
let count=$((count+1));
done
}
function rospd {
if [[ $1 = "--help" ]]; then
echo -e "usage: rospd\n\nLike pushd, also accepts indexes from rosd."
return 0
fi
if _is_integer $1; then
pushd +$1 > /dev/null ;
else
local rosvals
_ros_decode_path $1 forceeval
pushd ${rosvals[2]}${rosvals[3]}${rosvals[4]} > /dev/null ;
fi
rosd
}
function rosls {
local rosvals
if [[ $1 = "--help" ]]; then
echo -e "usage: rosls [package]\n\nLists contents of a package directory."
return 0
fi
_ros_decode_path $1 forceeval
ls ${rosvals[2]}${rosvals[3]}${rosvals[4]} $2
}
# sets arg as return value
function _roscmd {
local pkgdir exepath opt
pkgdir=`export ROS_CACHE_TIMEOUT=-1.0 && rospack find $1 2> /dev/null`
if [[ $? != 0 ]] ; then
echo "Couldn't find package [$1]"
return 1
fi
exepath=(`find $pkgdir -name $2 -type f`)
if [[ ${#exepath[@]} == 0 ]] ; then
echo "That file does not exist in that package."
return 1
elif [[ ${#exepath[@]} -gt 1 ]] ; then
echo "You have chosen a non-unique filename, please pick one of the following:"
select opt in ${exepath[@]}; do
echo $opt
break
done
else
opt=${exepath[1]}
fi
arg=${opt}
}
function rosed {
local arg
if [[ $1 = "--help" ]]; then
echo -e "usage: rosed [package] [file]\n\nEdit a file within a package."
return 0
fi
_roscmd ${1} ${2}
if [[ -z $EDITOR ]]; then
vim ${arg}
else
$EDITOR ${arg}
fi
}
function roscp {
local arg
if [[ $1 = "--help" ]] | [[ $# -ne 3 ]]; then
echo -e "usage: roscp package filename target\n\nCopy a file from a package to target location."
return 0
fi
_roscmd ${1} ${2}
cp ${arg} ${3}
}
function _roscomplete {
local arg opts stack_opts
reply=()
opts=`export ROS_CACHE_TIMEOUT=-1.0 && rospack list-names`
stack_opts=`export ROS_CACHE_TIMEOUT=-1.0 && rosstack list-names`
IFS=$'\n'
reply=(${=opts} ${=stack_opts})
unset IFS
}
function _roscomplete_rosmake {
local param
_roscomplete
if [[ $PREFIX == "--"* ]]; then
param="--test-only --all --mark-installed --unmark-installed --robust --build-everything --specified-only --buildtest --buildtest1 --output --pre-clean --bootstrap --disable-logging --target --pjobs --threads --profile --skip-blacklist --status-rate"
reply=(${=reply} ${=param})
fi
}
function _roscomplete_sub_dir {
local arg opts rosvals sedcmd stack_opts
reply=()
arg="$1"
_ros_decode_path ${arg}
if [[ -z ${rosvals[3]} ]]; then
opts=`export ROS_CACHE_TIMEOUT=-1.0 && rospack list-names`
stack_opts=`export ROS_CACHE_TIMEOUT=-1.0 && rosstack list-names`
IFS=$'\n'
reply=(${=opts} ${=stack_opts})
unset IFS
else
if [ -e ${rosvals[2]}${rosvals[3]} ]; then
sedcmd="s:^${rosvals[2]}:${rosvals[1]}:"g
#echo
#echo $sedcmd
#echo
opts=`find ${rosvals[2]}${rosvals[3]} -maxdepth 1 -mindepth 1 -type d ! -regex ".*/[.].*" -print0 | tr '\000' '\n' | sed -e "$sedcmd"`
#echo $opts
else
opts=''
fi
IFS=$'\n'
reply=(${=opts})
unset IFS
fi
}
function _roscomplete_search_dir {
local words arg opts pkgdir pkgdir_result stack_result
reply=()
words=(${=BUFFER})
pkgdir=`export ROS_CACHE_TIMEOUT=-1.0 && rospack find ${words[2]} 2> /dev/null`
pkgdir_result=$?
stackdir=`export ROS_CACHE_TIMEOUT=-1.0 && rosstack find ${words[2]} 2> /dev/null`
stack_result=$?
if [[ $pkgdir_result == 0 ]]; then
opts=`find $pkgdir ${=1} -print0 | tr '\000' '\n' | sed -e "s/.*\/\(.*\)/\1/g"`
elif [[ $stack_result == 0 ]] ; then
opts=`find $stackdir ${=1} -print0 | tr '\000' '\n' | sed -e "s/.*\/\(.*\)/\1/g"`
else
opts=""
fi
IFS=$'\n'
reply=(${=opts})
unset IFS
}
function _roscomplete_exe {
_roscomplete_search_dir "-type f -perm +111 -regex .*/.*$"
}
function _roscomplete_file {
_roscomplete_search_dir "-type f ! -regex .*/[.].* ! -regex .*[.][oa]$"
}
function _roscomplete_launchfile {
_roscomplete_search_dir "( -type f -regex .*\.launch$ -o -type f -regex .*\.test$ )"
}
function _roscomplete_rosbag {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="check compress decompress filter fix help info play record reindex"
reply=(${=opts})
else
if [[ ${=${(s: :)words}[$(( ${CURRENT} ))]} =~ \-\- ]]; then
opts="--all --help"
case ${=${(s: :)words}[2]} in
check)
opts="--genrules --append --noplugins --help"
;;
compress|decompress)
opts="--output-dir --force --quiet --help"
;;
filter)
opts="--print --help"
;;
fix)
opts="--force --noplugins --help"
;;
info)
opts="--yaml --key --freq --help"
;;
play)
opts="--help --quiet --immediate --pause --queue --clock --hz --delay --rate --start --skip-empty --loop --keep-alive --try-future-version --topics --bags"
;;
record)
opts="--help --all --regex --exclude --quiet --output-prefix --output-name --split --size --duration --buffsize --limit --node --bz2"
;;
reindex)
opts="--help --force --quiet --output-dir"
;;
esac
reply=(${=opts})
fi
fi
}
function _roscomplete_rospack {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="help find list list-names list-duplicates langs depends depends-manifests depends1 depends-indent depends-msgsrv depends-why rosdep rosdep0 vcs vcs0 depends-on depends-on1 export plugins cflags-only-I cflags-only-other libs-only-L libs-only-l libs-only-other profile"
reply=(${=opts})
else
opts=`rospack list-names`
reply=(${=opts})
fi
}
function _roscomplete_rosnode {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="ping list info machine kill"
reply=(${=opts})
elif [[ ${CURRENT} == 3 ]]; then
case ${=${(s: :)words}[2]} in
info)
opts=`rosnode list 2> /dev/null`
reply=(${=opts})
;;
ping|list|kill)
if [[ ${=${(s: :)words}[$(( ${CURRENT} ))]} =~ \-\- ]]; then
opts="--all --help"
else
opts=`rosnode list 2> /dev/null`
fi
reply=(${=opts})
;;
machine)
# This takes more logic to determine which machines are present.
;;
esac
else
case ${=${(s: :)words}[2]} in
kill)
# complete on node name
opts=`rosnode list 2> /dev/null`
reply=(${=opts})
;;
esac
fi
}
function _roscomplete_rosparam {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="set get load dump delete list"
reply=(${=opts})
elif [[ ${CURRENT} == 3 ]]; then
case ${=${(s: :)words}[2]} in
set|get|delete|list)
opts=`rosparam list 2> /dev/null`
reply=(${=opts})
;;
load|dump)
# complete on files
reply=(${=opts})
;;
esac
elif [[ ${CURRENT} == 4 ]]; then
case ${=${(s: :)words}[2]} in
load|dump)
# complete on namespace
opts=`rosparam list 2> /dev/null`
reply=(${=opts})
;;
esac
fi
}
function _roscomplete_rostopic {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="bw echo hz list pub type find info"
reply=(${=opts})
elif [[ ${CURRENT} > 2 ]]; then
if [[ ${=${(s: :)words}[$(( ${CURRENT} ))]} =~ \-\- ]]; then
case ${=${(s: :)words}[2]} in
pub)
opts="--help --rate --once --file --latch"
;;
bw)
opts="--help --window"
;;
echo)
opts="--help --bag --filter --nostr --noarr --clear --all --offset"
;;
hz)
opts="--help --window --filter"
;;
list)
opts="--help --bag --verbose --host"
;;
type|info)
opts="--help"
;;
esac
reply=(${=opts})
else
case ${=${(s: :)words}[2]} in
bw|echo|hz|list|type|info)
if [[ ${=${(s: :)words}[$(( ${CURRENT} -1 ))]} == "-b" ]]; then
opts=`find . -maxdepth 1 -type f -not -name ".*" -not -name "*[~#]" | sed 's!.*/!!'`
reply=(${=opts})
else
opts=`rostopic list 2> /dev/null`
reply=(${=opts})
fi
;;
find)
opts=`_msg_opts ${=${(s: :)words[-1]}}`
reply=(${=opts})
;;
pub)
if [[ ${CURRENT} == 3 ]]; then
opts=`rostopic list 2> /dev/null`
reply=(${=opts})
elif [[ ${CURRENT} == 4 ]]; then
if [[ ${=${(s: :)words}[$(( ${CURRENT} ))]} =~ / ]]; then
opts=`_msg_opts ${=${(s: :)words[-1]}} | sed -s 's/.*\///'`
else
opts=`_msg_opts ${=${(s: :)words[-1]}}`
fi
reply=(${=opts})
elif [[ ${CURRENT} == 5 ]]; then
opts=`rosmsg-proto msg 2> /dev/null -s ${=${(s: :)words[-1]}}`
reply=(${=opts})
fi
;;
esac
fi
fi
}
function _roscomplete_rosservice {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="list call type find uri"
reply=(${=opts})
elif [[ ${CURRENT} == 3 ]]; then
case ${=${(s: :)words}[2]} in
uri|list|type|call)
opts=`rosservice list 2> /dev/null`
IFS=$'\n'
reply=(${=opts})
unset IFS
;;
find)
opts=`_srv_opts ${=${(s: :)words[-1]}}`
reply=(${=opts})
;;
esac
elif [[ ${CURRENT} == 4 ]]; then
case ${=${(s: :)words}[2]} in
call)
type=`rosservice type ${=${(s: :)words[-1]}}`
opts=`rosmsg-proto srv 2> /dev/null -s ${type}`
reply=(${=opts})
;;
esac
fi
}
function _msg_opts {
local pkgname msgname searchmsg pkgs count pkgname2 opts result
unset searchmsg
if [[ $1 =~ .+/.* ]]; then
pkgname=${1%%/*}
msgname=${1#*/}
searchmsg=1
else
pkgname=${1}
fi
if [[ -z ${searchmsg} ]]; then
pkgs=`command rospack list | grep "^${pkgname}"`
count=0
opts=""
for pkg in ${(f)pkgs}; do
pkgdir=${=${(s: :)pkg}[2]}
if [[ -d $pkgdir/msg ]]; then
pkgname2=${=${(s: :)pkg}[1]}
opts="$opts $pkgname2/"
count=$((count+1))
fi
done
if [[ $count > 0 ]]; then
echo $opts
return 0
fi
fi
pkgpath=`rospack find ${pkgname} 2> /dev/null`
if [[ $? == 0 ]] && [[ -d ${pkgpath}/msg ]]; then
result=`find -L ${pkgpath}/msg -maxdepth 1 -mindepth 1 -name \*.msg ! -regex ".\*/[.].\*" -print0 | tr '\000' '\n' | sed -e "s/.*\/\(.*\)\.msg/${pkgname}\/\1/g"`
echo $result
fi
}
function _roscomplete_rosmsg {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="show list md5 package packages"
reply=(${=opts})
elif [[ ${CURRENT} == 3 ]]; then
case ${=${(s: :)words}[2]} in
show|users|md5)
opts=`_msg_opts ${=${(s: :)words[-1]}}`
reply=(${=opts})
;;
package)
opts=`rospack list-names`
reply=(${=opts})
;;
packages|list)
# This shouldn't really have a completion rule
;;
esac
fi
}
function _srv_opts {
local pkgname srvname searchsrv pkgs count opts pkgpath result
unset searchsrv
if [[ $1 =~ .+/.* ]]; then
pkgname=${1%%/*}
srvname=${1#*/}
searchsrv=1
else
pkgname=${1}
fi
if [[ -z ${searchsrv} ]]; then
pkgs=`command rospack list | grep "^${pkgname}"`
count=0
opts=""
for pkg in ${(f)pkgs}; do
pkgdir=${=${(s: :)pkg}[2]}
if [[ -d $pkgdir/srv ]]; then
pkgname2=${=${(s: :)pkg}[1]}
opts="$opts $pkgname2/"
count=$((count+1))
fi
done
if [[ $count > 0 ]]; then
echo $opts
return 0
fi
fi
pkgpath=`rospack find ${pkgname} 2> /dev/null`
if [[ $? == 0 ]] && [[ -d ${pkgpath}/srv ]]; then
result=`find -L ${pkgpath}/srv -maxdepth 1 -mindepth 1 -name \*.srv ! -regex ".\*/[.].\*" -print0 | tr '\000' '\n' | sed -e "s/.*\/\(.*\)\.srv/${pkgname}\/\1/g"`
echo $result
fi
}
function _roscomplete_rossrv {
local opts
reply=()
if [[ ${CURRENT} == 2 ]]; then
opts="show list md5 package packages"
reply=(${=opts})
elif [[ ${CURRENT} == 3 ]]; then
case ${=${(s: :)words}[2]} in
show|users|md5)
opts=`_srv_opts ${=${(s: :)words[-1]}}`
reply=(${=opts})
;;
package)
opts=`rospack list-names`
reply=(${=opts})
;;
packages|list)
# This shouldn't really have a completion rule
;;
esac
fi
}
function _roscomplete_roscreate_pkg {
local opts
reply=()
if [[ ${CURRENT} > 2 ]]; then
opts=`rospack list-names`
reply=(${=opts})
fi
}
compctl -K "_roscomplete_sub_dir" -S / "roscd" "rospd" "rosls"
compctl -K "_roscomplete_rosmake" "rosmake"
compctl -x 'p[1]' -k "(check purge)" -- "rosclean"
compctl -f -x 'p[1]' -K "_roscomplete" - 'p[2]' -K _roscomplete_file -- "rosed" "roscp"
compctl -f -x 'p[1]' -K "_roscomplete" - 'p[2]' -K _roscomplete_exe -- "rosrun"
compctl -/g '*.(launch|test)' -x 'p[1]' -K "_roscomplete" -tx - 'p[2]' -K _roscomplete_launchfile -- + -x 'S[--]' -k "(--files --args --nodes --find-node --child --local --screen --server_uri --run_id --wait --port --core --pid --dump-params)" -- "roslaunch"
compctl -/g '*.(launch|test)' -x 'p[1]' -K "_roscomplete" -tx - 'p[2]' -K _roscomplete_launchfile -- + -x 'S[--]' -k "(--bare --bare-limit --bare-name --pkgdir --package)" -- "rostest"
compctl -K "_roscomplete_rospack" "rospack"
compctl -K "_roscomplete_rosbag" "rosbag"
compctl -K "_roscomplete_rosnode" "rosnode"
compctl -K "_roscomplete_rosparam" "rosparam"
compctl -x 'p[0,2]' -K "_roscomplete_rostopic" - 'n[1,/] p[3]' -K "_roscomplete_rostopic" - 'p[3]' -S '' -K "_roscomplete_rostopic" - 'p[4]' -K "_roscomplete_rostopic" -- "rostopic"
compctl -K "_roscomplete_rosservice" "rosservice"
compctl -x 'p[1]' -k "(md5 package packages show users)" - 'p[2]' -S '' -K "_roscomplete_rosmsg" -- "rosmsg"
compctl -x 'p[1]' -k "(md5 package packages show users)" - 'p[2]' -S '' -K "_roscomplete_rossrv" -- "rossrv"
compctl -K "_roscomplete_roscreate_pkg" "roscreate-pkg"
compctl -/g '*.(launch|test)' -x 'S[--]' -k "(--all --no-plugins --offline)" -- "roswtf"