diff options
author | Peter Stephenson <pws@users.sourceforge.net> | 2008-08-01 11:48:38 +0000 |
---|---|---|
committer | Peter Stephenson <pws@users.sourceforge.net> | 2008-08-01 11:48:38 +0000 |
commit | 5e7e797ca90cd1c1b7ab54d39118f0b7469f348f (patch) | |
tree | d2b23a1d1d295b6e6007df82a3ce12d0e75cf4ad /Functions | |
parent | dc72699b6c400e530b818250c943587b3ecf663e (diff) | |
download | zsh-5e7e797ca90cd1c1b7ab54d39118f0b7469f348f.tar.gz zsh-5e7e797ca90cd1c1b7ab54d39118f0b7469f348f.tar.xz zsh-5e7e797ca90cd1c1b7ab54d39118f0b7469f348f.zip |
TCP functions: fix some variable handling problems,
also standardise indentation add-zsh-hook: fix check for existence of hook function in existing hook
Diffstat (limited to 'Functions')
-rw-r--r-- | Functions/Misc/add-zsh-hook | 2 | ||||
-rw-r--r-- | Functions/TCP/tcp_open | 215 | ||||
-rw-r--r-- | Functions/TCP/tcp_sess | 2 |
3 files changed, 111 insertions, 108 deletions
diff --git a/Functions/Misc/add-zsh-hook b/Functions/Misc/add-zsh-hook index f534980f5..aedc1e754 100644 --- a/Functions/Misc/add-zsh-hook +++ b/Functions/Misc/add-zsh-hook @@ -66,7 +66,7 @@ if (( del )); then fi else if (( ${(P)+hook} )); then - if (( ${(P)hook[(I)$fn]} == 0 )); then + if (( ${${(P)hook}[(I)$fn]} == 0 )); then set -A $hook ${(P)hook} $fn fi else diff --git a/Functions/TCP/tcp_open b/Functions/TCP/tcp_open index 60bcb5e5d..01327dc74 100644 --- a/Functions/TCP/tcp_open +++ b/Functions/TCP/tcp_open @@ -72,30 +72,30 @@ local -a sessnames sessargs integer stat while getopts "a:f:l:qs:z" opt; do - case $opt in - (a) accept=$OPTARG - if [[ $accept != [[:digit:]]## ]]; then - print "option -a takes a file descriptor" >&2 - return 1 - fi - ;; - (f) fake=$OPTARG - if [[ $fake != [[:digit:]]## ]]; then - print "option -f takes a file descriptor" >&2 - return 1 - fi - ;; - (l) sessnames+=(${(s.,.)OPTARG}) - ;; - (q) quiet=1 - ;; - (s) sessnames+=($OPTARG) - ;; - (z) nozle=1 - ;; - (*) return 1 - ;; - esac + case $opt in + (a) accept=$OPTARG + if [[ $accept != [[:digit:]]## ]]; then + print "option -a takes a file descriptor" >&2 + return 1 + fi + ;; + (f) fake=$OPTARG + if [[ $fake != [[:digit:]]## ]]; then + print "option -f takes a file descriptor" >&2 + return 1 + fi + ;; + (l) sessnames+=(${(s.,.)OPTARG}) + ;; + (q) quiet=1 + ;; + (s) sessnames+=($OPTARG) + ;; + (z) nozle=1 + ;; + (*) return 1 + ;; + esac done (( OPTIND > 1 )) && shift $(( OPTIND - 1 )) @@ -104,50 +104,50 @@ done typeset -A sessassoc if (( ${#sessnames} )); then - if [[ $# -ne 0 || -n $accept || -n $fake ]]; then - print "Incompatible arguments with \`-s' option." >&2 - return 1 + if [[ $# -ne 0 || -n $accept || -n $fake ]]; then + print "Incompatible arguments with \`-s' option." >&2 + return 1 + fi + for sess in ${sessnames}; do + sessassoc[$sess]= + done + + sessfile=${ZDOTDIR:-$HOME}/.ztcp_sessions + if [[ ! -r $sessfile ]]; then + print "No session file: $sessfile" >&2 + return 1 + fi + while read -A sessargs; do + [[ ${sessargs[1]} = '#'* ]] && continue + if (( ${+sessassoc[${sessargs[1]}]} )); then + sessassoc[${sessargs[1]}]="${sessargs[2,-1]}" fi - for sess in ${sessnames}; do - sessassoc[$sess]= - done - - sessfile=${ZDOTDIR:-$HOME}/.ztcp_sessions - if [[ ! -r $sessfile ]]; then - print "No session file: $sessfile" >&2 - return 1 + done < $sessfile + for sess in ${sessnames}; do + if [[ -z $sessassoc[$sess] ]]; then + print "Couldn't find session $sess in $sessfile." >&2 + return 1 fi - while read -A sessargs; do - [[ ${sessargs[1]} = '#'* ]] && continue - if (( ${+sessassoc[${sessargs[1]}]} )); then - sessassoc[${sessargs[1]}]="${sessargs[2,-1]}" - fi - done < $sessfile - for sess in ${sessnames}; do - if [[ -z $sessassoc[$sess] ]]; then - print "Couldn't find session $sess in $sessfile." >&2 - return 1 - fi - done + done else - if [[ -z $accept && -z $fake ]]; then - if (( $# < 2 )); then - set -- wrong number of arguments - else - host=$1 port=$2 - shift $(( $# > 1 ? 2 : 1 )) - fi - fi - if [[ -n $1 ]]; then - sessnames=($1) - shift + if [[ -z $accept && -z $fake ]]; then + if (( $# < 2 )); then + set -- wrong number of arguments else - sessnames=($(( ${#tcp_by_fd} + 1 ))) - while [[ -n $tcp_by_name[$sessnames[1]] ]]; do - (( sessnames[1]++ )) - done + host=$1 port=$2 + shift $(( $# > 1 ? 2 : 1 )) fi - sessassoc[$sessnames[1]]="$host $port" + fi + if [[ -n $1 ]]; then + sessnames=($1) + shift + else + sessnames=($(( ${#tcp_by_fd} + 1 ))) + while [[ -n $tcp_by_name[$sessnames[1]] ]]; do + (( sessnames[1]++ )) + done + fi + sessassoc[$sessnames[1]]="$host $port" fi if (( $# )); then @@ -158,64 +158,67 @@ fi local REPLY fd for sess in $sessnames; do - if [[ -n $tcp_by_name[$sess] ]]; then - print "Session \`$sess' already exists." >&2 - return 1 - fi - - sessargs=() - if [[ -n $fake ]]; then - fd=$fake; + if [[ -n $tcp_by_name[$sess] ]]; then + print "Session \`$sess' already exists." >&2 + return 1 + fi + + sessargs=() + if [[ -n $fake ]]; then + fd=$fake; + else + if [[ -n $accept ]]; then + ztcp -a $accept || return 1 else - if [[ -n $accept ]]; then - ztcp -a $accept || return 1 - else - sessargs=(${=sessassoc[$sess]}) - ztcp $sessargs || return 1 - fi - fd=$REPLY + sessargs=(${=sessassoc[$sess]}) + ztcp $sessargs || return 1 fi + fd=$REPLY + fi - tcp_by_fd[$fd]=$sess - tcp_by_name[$sess]=$fd + tcp_by_fd[$fd]=$sess + tcp_by_name[$sess]=$fd - [[ -o zle && -z $nozle ]] && zle -F $fd tcp_fd_handler + [[ -o zle && -z $nozle ]] && zle -F $fd tcp_fd_handler - # needed for new completion system, so I'm not too sanguine - # about requiring this here... - if zmodload -i zsh/parameter; then - if (( ${+functions[tcp_on_open]} )); then - if ! tcp_on_open $sess $fd; then - if [[ -z $quiet ]]; then - if (( ${#sessargs} )); then - print "Session $sess" \ + # needed for new completion system, so I'm not too sanguine + # about requiring this here... + if zmodload -i zsh/parameter; then + if (( ${+functions[tcp_on_open]} )); then + if ! tcp_on_open $sess $fd; then + if [[ -z $quiet ]]; then + if (( ${#sessargs} )); then + print "Session $sess" \ "(host $sessargs[1], port $sessargs[2] fd $fd): tcp_on_open FAILED." - else - print "Session $sess (fd $fd) tcp_on_open FAILED." - fi - tcp_close -- $sess - else - tcp_close -q -- $sess - fi - stat=1 - continue - fi + else + print "Session $sess (fd $fd) tcp_on_open FAILED." + fi + tcp_close -- $sess + else + tcp_close -q -- $sess fi + stat=1 + continue + fi fi + fi - if [[ -z $quiet ]]; then - if (( ${#sessargs} )); then - print "Session $sess" \ + if [[ -z $quiet ]]; then + if (( ${#sessargs} )); then + print "Session $sess" \ "(host $sessargs[1], port $sessargs[2] fd $fd) opened OK." - else - print "Session $sess (fd $fd) opened OK." - fi + else + print "Session $sess (fd $fd) opened OK." fi + fi done -if [[ -z $TCP_SESS ]]; then +if [[ -z $TCP_SESS || -z $tcp_by_name[$TCP_SESS] ]]; then + # careful in case we closed it again... + if [[ -n $tcp_by_name[$sessnames[1]] ]]; then [[ -z $quiet ]] && print "Setting default TCP session $sessnames[1]" typeset -g TCP_SESS=$sessnames[1] + fi fi return $stat diff --git a/Functions/TCP/tcp_sess b/Functions/TCP/tcp_sess index a07c33a03..cc6d42f80 100644 --- a/Functions/TCP/tcp_sess +++ b/Functions/TCP/tcp_sess @@ -17,7 +17,7 @@ if [[ -n $1 ]]; then eval $__cmd \$\* return else - TCP_SESS=$1 + typeset -g TCP_SESS=$1 return 0; fi fi |