Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: _netcat



Sven wrote:

> 1) I like the change because it's more extensible. 2) Can't help you
> with a better name, sorry. 3) I don't worry to much about the tag-name
> change either.

Good. Thanks.

> Hm, some kind of helper-function for this?

I've considered that before but wasn't sure how to make it sufficiently
general.

How about the following. Usage is, for example:
   _pick_variant netcat=connect nedit -h
or _pick_variant -c diff gnu=GNU unix --version

The -c option is for where $words[1] isn't to be used. Then you can
have any number of command=pattern arguments (so it can differentiate
more than two variants of a command). Then you have the command for if
no patterns matched and then any subsequent arguments are passed as
parameters to the command. Can you think of a better way of
describing this information in the arguments?

I've got it to return 1 if it resorts to the last choice so in most
cases the function can be used with if to avoid checking $_cmd_variant
in the calling function. I'm not sure whether that is a good idea
though.

> > -if [[ -n "$_is_gnu[$words[1]]" ]]; then
> > +if [[ -n "$_cmd_variant[$words[1]]" ]]; then
> 
> I'm confused. Shoudn't this test `"$_cmd_variant[$words[1]]" = gnu'?

Yes, it should really. It was like that before - I just changed yes to
gnu and _is_gnu to _cmd_variant. When it is not gnu, I'll use a
different word in _cmd_variant like `no' or `unix' instead of the empty
string (as it currently is).

> And, btw., on this Suse-box:
> 
>   % which netcat
>   /usr/bin/netcat
> 
> And nc is the nedit-thing. (You asked for this, right? OR was it
> someone else?)

Yes, I did ask that. Thanks. I see that _netcat already has netcat in
the compdef line so we have it right as long as no other distributions
have done something different about the name clash.

Oliver

#autoload

local output command cmd pat
local -a var

(( $+_cmd_variant )) || typeset -gA _cmd_variant

if [[ $1 = -c ]]; then
  command="$2"
  shift 2
else
  command="$words[1]"
fi


while [[ $1 = *=* ]]; do
  var+=( "${1%%\=*}" "${1#*=}" )
  shift
done

if (( $+_cmd_variant[$command] )); then
  [[ $_cmd_variant[$command] = "$1" ]] && return 1
  return 0
fi

output="$(_call_program variant $command "${@[2,-1]}" </dev/null 2>/dev/null)"

for cmd pat in "$var[@]"; do
  if [[ $output = *$~pat* ]]; then
    _cmd_variant[$command]="$cmd"
    return
  fi
done

_cmd_variant[$command]="$1"
return 1
-- 

This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.



Messages sorted by: Reverse Date, Date, Thread, Author