zsh-workers
 help / Atom feed
* [PATCH] Completion: Improve _getconf
@ 2019-02-26 20:35 dana
  0 siblings, 0 replies; 1+ messages in thread
From: dana @ 2019-02-26 20:35 UTC (permalink / raw)
  To: Zsh hackers list

* Complete -a, -v, and other options where applicable
* Get the full list of config variables even on systems without -a
* Don't pluralise descriptions
* Don't call _files with -/ (it's not restricted to directories)

dana


diff --git a/Completion/Unix/Command/_getconf b/Completion/Unix/Command/_getconf
index f2c713a52..36f58226c 100644
--- a/Completion/Unix/Command/_getconf
+++ b/Completion/Unix/Command/_getconf
@@ -1,6 +1,8 @@
 #compdef getconf
 
-local expl ret=1
+local variant list_cmd ret=1
+local -a context expl line state state_descr args
+local -A opt_args
 
 local -a syskeys posixkeys confkeys pathkeys1 pathkeys2 allkeys mykeys restkeys
 syskeys=(ARG_MAX BC_BASE_MAX BC_DIM_MAX BC_SCALE_MAX
@@ -23,38 +25,85 @@ pathkeys1=(PIPE_BUF _POSIX_CHOWN_RESTRICTED
 pathkeys2=(LINK_MAX MAX_CANON MAX_INPUT NAME_MAX PATH_MAX PIPE_BUF)
 mykeys=($syskeys $posixkeys $confkeys $pathkeys1 $pathkeys2)
 
-if [[ CURRENT -eq 2 ]]; then
-  _tags syswideconfig pathconfig standardsconfig confstring restconfig
+if _pick_variant -r variant gnu='(Free Soft|GLIBC)' $OSTYPE --version; then
+  # GNU getconf doesn't use getopt(3), strangely
+  args+=(
+    '(: * -)--help[display help information]'
+    '(: * -)--version[display version information]'
+    '(1 -)-a[display all configuration variables and their values]'
+    '(-)-v[specify programming environment]: :->env'
+  )
+  : ${list_cmd:='$words[1] -a'}
 
-  allkeys=(${${(f)"$(getconf -a 2>/dev/null)"}%%[: ]*})
-  restkeys=(${allkeys:|mykeys})
+else
+  [[ $variant == (netbsd*|solaris*) ]] && {
+    args+=( '(1 -)-a[display all configuration variables and their values]' )
+    : ${list_cmd:='$words[1] -a'}
+  }
 
-  while _tags; do
-    _requested -V syswideconfig expl 'systemwide configuration variables' \
-      compadd -S '' $syskeys && ret=0
+  [[ $variant == openbsd* ]] && {
+    args+=(
+      '(: - *)-l[display all system (non-path) configuration variables]'
+      '(: - *)-L[display all path configuration variables]'
+    )
+    : ${list_cmd:='$words[1] -l; $words[1] -L'}
+  }
 
-    _requested -V standardsconfig \
-        expl 'system-standards configuration variables' \
-      compadd -S '' $posixkeys && ret=0
+  [[ $variant == netbsd* ]] ||
+  args+=( '(-)-v+[specify programming environment]: :->env' )
 
-    _requested -V confstring \
-        expl 'configuration-dependent string variables' \
-      compadd -S '' $confkeys && ret=0
+  # This is a bit silly, but actually pretty accurate, where available
+  : ${list_cmd:='
+    command strings -- ${${(Q)words[1]}:c} |
+    LC_ALL=C GREP_OPTIONS= command grep -xE \
+      "_*[A-Z][A-Z0-9_]*_[A-Z0-9_]*|NZERO|PATH|[A-Z]+(BITS|SIZE)"
+  '}
+fi
 
-    _requested pathconfig &&
-    while _next_label -V pathconfig expl 'system path configuration variables'; do
-      compadd "$expl[@]" -S '' $pathkeys1 && ret=0
-      compadd "$expl[@]" -S ' ' $pathkeys2 && ret=0
-    done
+_arguments -S -A '-*' : $args '1: :->var' '2: :_files' && ret=0
 
-    if (( ${#restkeys} )); then
-      _requested -V restconfig \
-	expl 'remaining unclassified configuration variables' \
-	compadd -S '' $restkeys && ret=0
-    fi
+case $state in
+  env)
+    _wanted environments expl 'programming environment' compadd - \
+      POSIX_V{6,7}_ILP32_OFF32 \
+      POSIX_V{6,7}_ILP32_OFFBIG \
+      POSIX_V{6,7}_LP64_OFF64 \
+      POSIX_V{6,7}_LPBIG_OFFBIG \
+    && ret=0
+    ;;
+  var)
+    _tags syswideconfig pathconfig standardsconfig confstring restconfig
 
-    (( ret )) || return 0
-  done
-else
-  _files -/
-fi
+    allkeys=(${${(f)"$( _call_program variables $list_cmd )"}%%[=:[:space:]]*})
+    restkeys=(${allkeys:|mykeys})
+
+    while _tags; do
+      _requested -V syswideconfig expl 'system-wide configuration variable' \
+        compadd -S '' $syskeys && ret=0
+
+      _requested -V standardsconfig \
+        expl 'system-standards configuration variable' \
+        compadd -S '' $posixkeys && ret=0
+
+      _requested -V confstring \
+        expl 'configuration-dependent string variable' \
+        compadd -S '' $confkeys && ret=0
+
+      _requested pathconfig &&
+      while _next_label -V pathconfig expl 'system path configuration variable'; do
+        compadd "$expl[@]" -S '' $pathkeys1 && ret=0
+        compadd "$expl[@]" -S ' ' $pathkeys2 && ret=0
+      done
+
+      if (( ${#restkeys} )); then
+        _requested -V restconfig \
+          expl 'remaining unclassified configuration variable' \
+          compadd -S '' $restkeys && ret=0
+      fi
+
+      (( ret )) || break
+    done
+    ;;
+esac
+
+return ret


^ permalink raw reply	[flat|nested] 1+ messages in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-26 20:35 [PATCH] Completion: Improve _getconf dana

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/ public-inbox