# utility functions {{{ # this function checks if a command exists and returns either true # or false. This avoids using 'which' and 'whence', which will # avoid problems with aliases for which on certain weird systems. :-) # Usage: check_com [-c|-g] word # -c only checks for external commands # -g does the usual tests and also checks for global aliases check_com() { emulate -L zsh local -i comonly gatoo if [[ $1 == '-c' ]] ; then (( comonly = 1 )) shift elif [[ $1 == '-g' ]] ; then (( gatoo = 1 )) else (( comonly = 0 )) (( gatoo = 0 )) fi if (( ${#argv} != 1 )) ; then printf 'usage: check_com [-cg] \n' >&2 return 1 fi if (( comonly > 0 )) ; then [[ -n ${commands[$1]} ]] && return 0 return 1 fi if [[ -n ${commands[$1]} ]] \ || [[ -n ${functions[$1]} ]] \ || [[ -n ${aliases[$1]} ]] \ || [[ -n ${reswords[(r)$1]} ]] ; then return 0 fi if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then return 0 fi return 1 } # creates an alias and precedes the command with # sudo if $EUID is not zero. salias() { emulate -L zsh local only=0 ; local multi=0 while [[ $1 == -* ]] ; do case $1 in (-o) only=1 ;; (-a) multi=1 ;; (--) shift ; break ;; (-h) printf 'usage: salias [-h|-o|-a] \n' printf ' -h shows this help text.\n' printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n' printf ' be careful using this option.\n' printf ' -o only sets an alias if a preceding sudo would be needed.\n' return 0 ;; (*) printf "unkown option: '%s'\n" "$1" ; return 1 ;; esac shift done if (( ${#argv} > 1 )) ; then printf 'Too many arguments %s\n' "${#argv}" return 1 fi key="${1%%\=*}" ; val="${1#*\=}" if (( EUID == 0 )) && (( only == 0 )); then alias -- "${key}=${val}" elif (( EUID > 0 )) ; then (( multi > 0 )) && val="${val// ; / ; sudo }" alias -- "${key}=sudo ${val}" fi return 0 } # Check if we can read given files and source those we can. xsource() { emulate -L zsh if (( ${#argv} < 1 )) ; then printf 'usage: xsource FILE(s)...\n' >&2 return 1 fi while (( ${#argv} > 0 )) ; do [[ -r $1 ]] && source $1 shift done return 0 } # Check if we can read a given file and 'cat(1)' it. xcat() { emulate -L zsh if (( ${#argv} != 1 )) ; then printf 'usage: xcat FILE\n' >&2 return 1 fi [[ -r $1 ]] && cat $1 return 0 } # Remove these functions again, they are of use only in these # setup files. This should be called at the end of .zshrc. xunfunction() { emulate -L zsh local -a funcs funcs=(salias xcat xsource xunfunction zrcautoload) for func in $funcs ; do [[ -n ${functions[$func]} ]] \ && unfunction $func done return 0 } #}}}