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

Re: PATCH: _netcat



On Fri, Mar 15, 2002 at 09:39:49AM +0100, Sven Wischnowsky wrote:
> 
> The only thing I would suggest is that it (optionally?) gets a
> parameter name as argument into which the command variant is stored so
> that the calling function doesn't have to do the lookup itself (yes,
> only a little easier, but...).

Okay. I've added a -r option for that. Following patch includes all the
old _is_gnu using functions and documentation. I'll commit it now to get
it out of my way but can always change it next week. The -r was useful
in a couple of places but mostly I've used the return code.

One point about zparseopts -K with an association though. With opts[-c]
set before zparseopts and a -r option passed to _pick_variant, it
loses the value of opts[-c]. This came up with _make and I solved it
by not using -K. I suspect that it is applying -K across the whole
association instead of per-element as I would have expected.

Oliver

Index: Completion/Base/Utility/.distfiles
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/Utility/.distfiles,v
retrieving revision 1.3
diff -u -r1.3 .distfiles
--- Completion/Base/Utility/.distfiles	5 Mar 2002 18:34:19 -0000	1.3
+++ Completion/Base/Utility/.distfiles	15 Mar 2002 16:04:33 -0000
@@ -3,6 +3,6 @@
 _alternative      _call_program     _nothing          _sub_commands
 _arg_compile      _combination      _regex_arguments  _values
 _arguments        _set_command      _retrieve_cache   _guard
-_cache_invalid    _describe         _sep_parts
+_cache_invalid    _describe         _sep_parts        _pick_variant
 _call_function    _multi_parts      _store_cache
 '
Index: Completion/Unix/Command/_cpio
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_cpio,v
retrieving revision 1.2
diff -u -r1.2 _cpio
--- Completion/Unix/Command/_cpio	16 Jan 2002 16:29:52 -0000	1.2
+++ Completion/Unix/Command/_cpio	15 Mar 2002 16:04:33 -0000
@@ -1,33 +1,23 @@
 #compdef cpio
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-
-local cmd=$service args ig curcontext="$curcontext" state line
+local args ig curcontext="$curcontext" state line
 local expl ret
 local fmts='(bar bin odc newc crc tar ustar hpbin hpodc)'
 
-if (( ! $+_is_gnu[$cmd] )); then
-  if [[ $(_call_program version $cmd --version </dev/null 2>/dev/null) = *GNU* ]]
-  then
-    _is_gnu[$cmd]=yes
-  else
-    _is_gnu[$cmd]=
-  fi
-fi 
-ig=$_is_gnu[$cmd]
+_pick_variant -r ig gnu=GNU unix --version
 
-if (( $CURRENT == 2 )); then
+if (( CURRENT == 2 )); then
    # Complete arguments 
    args=('-o[create archive]' '-i[extract from archive]'
    '-p[run as filter on directory tree]')
-   [[ -n $ig ]] && args=($args '--create[create archive]'
+   [[ $ig = gnu ]] && args=($args '--create[create archive]'
    '--extract[extract from archive]' 
    '--pass-through[run as filter on directory tree]'
    '--help[show help text]' '--version[show version information]')
 else
   if [[ -n ${words[(r)(-o*|-[^-]*o*|--create)]} ]]; then
-    # Optiona for creating archive
-    if [[ -n $ig ]]; then
+    # Options for creating archive
+    if [[ $ig = gnu ]]; then
 	args=(
 	   '--file=:archive file:->afile'
 	   "--format=:format type:$fmts"
@@ -51,7 +41,7 @@
 	  '-O[set output archive file]:output archive file:_files'
 	 )
   elif [[ -n ${words[(r)(-i*|-[^-]*i*|--extract)]} ]]; then
-    if [[ -n $ig ]]; then
+    if [[ $ig = gnu ]]; then
       args=('--file=:archive file:->afile'
 	    "--format=:format type:$fmts"
 	    '--make-directories' '--nonmatching' 
@@ -90,7 +80,7 @@
 	  '*:pattern to extract'
 	  )
   elif [[ -n ${words[(r)(-p*|-[^-]*p*|--pass-through)]} ]]; then
-    if [[ -n $ig ]]; then
+    if [[ $ig = gnu ]]; then
       args=('--null' '--reset-access-time' '--make-directories'
             '--link' '--quiet' '--preserve-modification-time'
 	    '--unconditional' '--verbose' '--dot' '--dereference'
Index: Completion/Unix/Command/_grep
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_grep,v
retrieving revision 1.5
diff -u -r1.5 _grep
--- Completion/Unix/Command/_grep	15 Mar 2002 14:49:39 -0000	1.5
+++ Completion/Unix/Command/_grep	15 Mar 2002 16:04:33 -0000
@@ -16,17 +16,6 @@
   command="$words[1]"
 fi
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-
-if (( ! $+_is_gnu[$command] )); then
-  if [[ $(_call_program version $command --help </dev/null 2>/dev/null) = *gnu* ]]
-  then
-    _is_gnu[$command]=yes
-  else
-    _is_gnu[$command]=
-  fi
-fi
-
 if [[ $service != [ef]grep ]]; then
   matchers='(--extended-regexp --fixed-strings --basic-regexp --perl-regexp -E -F -G -P)'
   arguments+=(
@@ -77,7 +66,7 @@
 )
 
 # remove long options?
-[[ -z "$_is_gnu[$command]" ]] &&
+_pick_variant -c "$command" gnu=gnu unix --help ||
     arguments=( ${arguments:#(|*\)(\*|))--*} )
 
 _arguments -s $arguments[@]
Index: Completion/Unix/Command/_iconv
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_iconv,v
retrieving revision 1.1
diff -u -r1.1 _iconv
--- Completion/Unix/Command/_iconv	2 Apr 2001 11:53:06 -0000	1.1
+++ Completion/Unix/Command/_iconv	15 Mar 2002 16:04:33 -0000
@@ -2,17 +2,7 @@
 
 local expl curcontext="$curcontext" state line codeset LOCPATH
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-if (( ! $+_is_gnu[$words[1]] )); then
-  if [[ $(_call_program version $words[1] --version </dev/null 2>/dev/null) = *GNU* ]]; 
-  then
-    _is_gnu[$words[1]]=yes
-  else
-    _is_gnu[$words[1]]=
-  fi
-fi
-
-if [[ -n "$_is_gnu[$words[1]]" ]]; then
+if _pick_variant gnu=GNU unix --version; then
 
   _arguments -C \
     {'(--from-code --list)-f','(-f --list)--from-code='}'[specify code set of input file]:code set:->codeset' \
Index: Completion/Unix/Command/_ls
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_ls,v
retrieving revision 1.3
diff -u -r1.3 _ls
--- Completion/Unix/Command/_ls	6 Aug 2001 14:42:04 -0000	1.3
+++ Completion/Unix/Command/_ls	15 Mar 2002 16:04:33 -0000
@@ -3,17 +3,6 @@
 
 local arguments
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-
-if (( ! $+_is_gnu[$words[1]] )); then
-  if [[ $(_call_program version $words[1] --help </dev/null 2>/dev/null) = *gnu* ]]
-  then
-    _is_gnu[$words[1]]=yes
-  else
-    _is_gnu[$words[1]]=
-  fi
-fi
-
 arguments=(
   '(--all -a -A --almost-all)'{--all,-a}'[list entries starting with .]'
   '(--almost-all -A -a --all)'{--almost-all,-A}'[list all except . and ..]'
@@ -79,7 +68,7 @@
 )
 
 # remove long options?
-[[ -z "$_is_gnu[$words[1]]" ]] &&
+_pick_variant gnu=gnu unix --help ||
     arguments=( ${${${arguments:#(|*\))--*}//--[^ )]#/}/\( #\)/} )
 
 _arguments -s $arguments
Index: Completion/Unix/Command/_make
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_make,v
retrieving revision 1.2
diff -u -r1.2 _make
--- Completion/Unix/Command/_make	13 Mar 2002 09:28:05 -0000	1.2
+++ Completion/Unix/Command/_make	15 Mar 2002 16:04:33 -0000
@@ -1,17 +1,8 @@
 #compdef make gmake pmake dmake
 
-local prev="$words[CURRENT-1]" file expl tmp
+local prev="$words[CURRENT-1]" file expl tmp is_gnu
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-
-if (( ! $+_is_gnu[$words[1]] )); then
-  if [[ $(_call_program version $words[1] -v -f /dev/null </dev/null 2>/dev/null) = *GNU* ]]
-  then
-    _is_gnu[$words[1]]=yes
-  else
-    _is_gnu[$words[1]]=
-  fi
-fi
+_pick_variant -r is_gnu gnu=GNU unix -v -f
 
 if [[ "$prev" = -[CI] ]]; then
   _files -/
@@ -25,14 +16,14 @@
     file=Makefile
   elif [[ -e makefile ]]; then
     file=makefile
-  elif [[ -n "$_is_gnu[$words[1]]" && -e GNUmakefile ]]; then
+  elif [[ $is_gnu = gnu && -e GNUmakefile ]]; then
     file=GNUmakefile
   else
     file=''
   fi
 
   if [[ -n "$file" ]] && _tags targets; then
-    if [[ -n "$_is_gnu[$words[1]]" ]] &&
+    if [[ $is_gnu = gnu ]] &&
        zstyle -t ":completion:${curcontext}:targets" call-command; then
       tmp=( $(_call_program targets "$words[1]" -nsp --no-print-directory -f "$file" .PHONY 2> /dev/null | awk '/^[a-zA-Z0-9][^\/\t=]+:/ {print $1}' FS=:) )
     else
Index: Completion/Unix/Command/_netcat
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_netcat,v
retrieving revision 1.1
diff -u -r1.1 _netcat
--- Completion/Unix/Command/_netcat	10 Mar 2002 04:20:56 -0000	1.1
+++ Completion/Unix/Command/_netcat	15 Mar 2002 16:04:33 -0000
@@ -3,6 +3,10 @@
 local curcontext="$curcontext" state line expl
 typeset -A opt_args
 
+# handle name clash with the nedit client
+[[ $service = nc ]] && ! _pick_variant netcat=connect nedit -h &&
+    _nedit && return
+
 if (( ! $+_nc_args )); then
   local help="$(_call_program options nc -h < /dev/null 2>&1)"
   local -A optionmap
Index: Completion/Unix/Command/_zcat
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_zcat,v
retrieving revision 1.1
diff -u -r1.1 _zcat
--- Completion/Unix/Command/_zcat	2 Apr 2001 12:07:09 -0000	1.1
+++ Completion/Unix/Command/_zcat	15 Mar 2002 16:04:33 -0000
@@ -1,18 +1,6 @@
 #compdef zcat
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-
-if (( ! $+_is_gnu[$words[1]] )); then
-  if [[ $(_call_program version $words[1] --license </dev/null 2>&1) = *GNU* ]]
-  then
-    _is_gnu[$words[1]]=yes
-  else
-    _is_gnu[$words[1]]=
-  fi
-fi
-
-if [[ -n "$_is_gnu[$words[1]]" ]]
-then
+if _pick_variant gnu=GNU unix --license; then
   _gzip "$@"
 else
   _compress "$@"
Index: Completion/Unix/Type/_diff_options
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Type/_diff_options,v
retrieving revision 1.3
diff -u -r1.3 _diff_options
--- Completion/Unix/Type/_diff_options	6 Aug 2001 14:42:04 -0000	1.3
+++ Completion/Unix/Type/_diff_options	15 Mar 2002 16:04:33 -0000
@@ -2,20 +2,10 @@
 
 local of ofwuc ouc oss ofwy ofwg ofwl cmd
 
-(( $+_is_gnu )) || typeset -gA _is_gnu
-
 cmd="$1"
 shift
 
-if (( ! $+_is_gnu[$cmd] )); then
-  if [[ $(_call_program version $cmd -v </dev/null 2>/dev/null) = *GNU* ]]; then
-    _is_gnu[$cmd]=yes
-  else
-    _is_gnu[$cmd]=
-  fi
-fi
-
-if [[ -n "$_is_gnu[$cmd]" ]]; then
+if _pick_variant -c $cmd gnu=GNU unix -v; then
   # output formats
   of="-y --side-by-side -n --rcs -e -f --ed -q --brief -c -C --context -u -U \
   --unified --old-group-format --new-group-format --changed-group-format \
Index: Completion/X/Command/_nedit
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/X/Command/_nedit,v
retrieving revision 1.1
diff -u -r1.1 _nedit
--- Completion/X/Command/_nedit	2 Apr 2001 12:14:08 -0000	1.1
+++ Completion/X/Command/_nedit	15 Mar 2002 16:04:33 -0000
@@ -1,4 +1,4 @@
-#compdef nedit nc
+#compdef nedit nedit-nc=nc ncl=nc
 
 local state line expl nedit_common curcontext="$curcontext" ret=1
 typeset -A opt_args
@@ -14,7 +14,7 @@
   '*-xrm:resource:_x_resource' \
   '*:file:_files' )
 
-if [[ $service = *nc ]]; then
+if [[ $service = nc ]]; then
   _x_arguments -C \
     '(-noask)-ask[prompt if no server found]' \
     '(-ask)-noask[start a new server without asking if none found]' \
Index: Completion/Zsh/Command/_zstyle
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Command/_zstyle,v
retrieving revision 1.7
diff -u -r1.7 _zstyle
--- Completion/Zsh/Command/_zstyle	17 Jan 2002 18:49:36 -0000	1.7
+++ Completion/Zsh/Command/_zstyle	15 Mar 2002 16:04:33 -0000
@@ -141,7 +141,7 @@
   other-accounts packages parameters path-directories paths pods ports
   prefixes printers processes processes-names ps regex sequences
   sessions signals strings styles tags targets timezones types urls
-  users values version visuals warnings widgets windows zsh-options
+  users values variant visuals warnings widgets windows zsh-options
 )
 
 _arguments -C \
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.148
diff -u -r1.148 compsys.yo
--- Doc/Zsh/compsys.yo	13 Mar 2002 09:28:05 -0000	1.148
+++ Doc/Zsh/compsys.yo	15 Mar 2002 16:04:33 -0000
@@ -864,10 +864,10 @@
 when completing a value out of a set of values (or a list of such
 values)
 )
-kindex(version, completion tag)
-item(tt(version))(
-used by tt(_call_program) to look up the command to run to determine the installed
-version of various other commands (such as tt(diff) and tt(make)).
+kindex(variant, completion tag)
+item(tt(variant))(
+used by tt(_pick_variant) to look up the command to run when determining
+what program is installed for a particular command name.
 )
 kindex(visuals, completion tag)
 item(tt(visuals))(
@@ -3941,6 +3941,26 @@
 
 Finally, the tt(_path_files) function  uses the styles tt(expand),
 tt(ambiguous), tt(special-dirs), tt(list-suffixes) and tt(file-sort).
+)
+findex(_pick_variant)
+item(tt(_pick_variant [ tt(-c) var(command) ] [ tt(-r) var(name) ] var(command)tt(=)var(pattern) ... var(command) [ var(args ... ]))(
+This function is used to resolve situations where a command has more
+than one variant, each requiring different handling or where there is a
+name clash between two different commands.
+
+_pick_variant does this by running the command and comparing its output
+against a series of patterns. The command to run is taken from the
+first element of the var(words) array unless specified with the tt(-c)
+option. Arguments to be passed on to it can be specified at the end
+after all the other arguments. The patterns are specified by the series
+of var(command)tt(=)var(pattern) arguments where var(command) is a
+label used to describe each command. If none of the patterns match, the
+final command label is taken and 1 is returned.
+
+The results are cached in the var(_cmd_variant) associative array so
+the calling function can either look up the result in there or it can
+pass the name of a parameter with the tt(-r) option which will be set
+to the result.
 )
 findex(_regex_arguments)
 item(tt(_regex_arguments) var(name) var(specs) ...)(

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