zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] new function to complete names of running process  (and three example usage)
@ 2018-06-20 12:30 Jun T
  2018-06-21  2:23 ` dana
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T @ 2018-06-20 12:30 UTC (permalink / raw)
  To: zsh-workers

_process_names completes names of running processes.

I wrote this for using it from three new completions for macOS-specific commands
dtruss, fs_usage and sc_usage (also included in the patch).

_process_names is tested only on macOS, Linux and {Free,Net,Open}BSD.
Please test on other OS (Solaris?) if possible.


diff --git a/Completion/Darwin/Command/_dtruss b/Completion/Darwin/Command/_dtruss
new file mode 100644
index 000000000..bd1ae8bc5
--- /dev/null
+++ b/Completion/Darwin/Command/_dtruss
@@ -0,0 +1,18 @@
+#compdef dtruss
+
+_arguments -s : \
+  '-a[print all details]' \
+  '-b+[specify dynamic variable buffer size]:buffer size (default 4m)' \
+  '-c[print system call counts]' \
+  '-d[print relative timestamps]' \
+  '-e[print elapsed times]' \
+  '-f[follow children as they are forked]' \
+  '-l[force printing of pid/lwpid per line]' \
+  "-L[don't print pid/lwpid per line]" \
+  '(-p :)-n+[examine processes with the specified name]: : _process_names -a' \
+  '-o[print on-cpu times]' \
+  '-s[print stack backtraces]' \
+  '(-n :)-p+[examine process with the specified pid]: : _pids' \
+  '-t+[examine only the specified syscall]: : _sys_calls' \
+  '1: : _command_names -e' \
+  '*:: : _normal'
diff --git a/Completion/Darwin/Command/_fs_usage b/Completion/Darwin/Command/_fs_usage
new file mode 100644
index 000000000..956816169
--- /dev/null
+++ b/Completion/Darwin/Command/_fs_usage
@@ -0,0 +1,28 @@
+#compdef fs_usage
+
+local curcontext="$curcontext" state state_descr line ret=1
+typeset -A opt_args
+
+_arguments -s -C -A '-*' : \
+  '-e[exclude fs_usage and the specified processes from sampling]' \
+  '-w[use wider output]' \
+  '*-f+[specify output filtering mode]:mode:(nework filesys pathname exec diskio cachehit)' \
+  '-b[annotate disk I/O events with BootCache info]' \
+  '(-R -S -E)-t+[specify run timeout]:seconds' \
+  '(-t)-R+[specify raw trace file to process]:raw trace file:_files' \
+  '(-t)-S+[specify time to begin processing the trace file]:seconds' \
+  '(-t)-E+[specify time to stop processing the trace file]:seconds' \
+  '*: :->pid-or-pname' && ret=0
+
+case $state in
+  (pid-or-pname)
+    if [[ -z $opt_args[-R] ]]; then
+      _alternative "processes:: _pids" \
+		  "processes-names:: _process_names -a" && ret=0
+    else
+      _message 'pid or process name in the trace file' && ret=0
+    fi
+    ;;
+esac
+
+return ret
diff --git a/Completion/Darwin/Command/_sc_usage b/Completion/Darwin/Command/_sc_usage
new file mode 100644
index 000000000..3a11a1bff
--- /dev/null
+++ b/Completion/Darwin/Command/_sc_usage
@@ -0,0 +1,10 @@
+#compdef sc_usage
+
+_arguments -s -A '-*' : \
+  '-c+[specify code file to use]:code file:_files' \
+  '-e[sort output by call count]' \
+  '-l[use scrolling output style instead of window updating style]' \
+  '-s+[specify sampling interval]:seconds' \
+  '(- :)-E[specify command path and args to excute]: :_absolute_command_paths:*:: :_normal' \
+  '1: : _alternative "processes:: _pids"
+	"processes-names:: _process_names -a"'
diff --git a/Completion/Unix/Type/_process_names b/Completion/Unix/Type/_process_names
new file mode 100644
index 000000000..ef3a95379
--- /dev/null
+++ b/Completion/Unix/Type/_process_names
@@ -0,0 +1,30 @@
+#autoload
+#
+# completes names of running processes
+#
+# option -a: include all processes (owned by others, no tty, etc.)
+#
+typeset -a expl names all
+local opts tagname
+tagname='processes-names'   # or 'process-names' ?
+
+zparseopts -E -D 'a=all'
+(( $#all )) && opts='ax'
+
+case $OSTYPE in
+  (linux*|*bsd*|dragonfly*)
+    # On these systems, 'ps o comm' (or 'ps co args') truncates long process
+    # names. To avoid this, we get the whole command line by 'ps o args' and
+    # remove arguments, but this does not work if proess names contain spaces.
+    opts+='o args='
+    names=( ${(f)"$(_call_program $tagname ps $opts 2>/dev/null )"} )
+    names=( ${${${${${names:#\[*]}%% *}%:}#-}:t} ${${${(M)names:#\[*]}#\[}%]} )
+    ;;
+  (*)
+    # tested only on macOS
+    opts+='o comm='
+    names=( ${${${(f)"$(_call_program $tagname ps $opts 2>/dev/null)"}#-}:t} )
+    ;;
+esac
+
+_wanted $tagname expl 'process name' compadd "$@" -F '(ps)' -a - names



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

* Re: [PATCH] new function to complete names of running process  (and three example usage)
  2018-06-20 12:30 [PATCH] new function to complete names of running process (and three example usage) Jun T
@ 2018-06-21  2:23 ` dana
  2018-06-21 11:41   ` Jun T
  0 siblings, 1 reply; 5+ messages in thread
From: dana @ 2018-06-21  2:23 UTC (permalink / raw)
  To: Jun T; +Cc: zsh-workers

On 20 Jun 2018, at 07:30, Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>_process_names completes names of running processes.

Solaris ps doesn't support BSD-style options (it needs -o rather than o), but if
you account for that it should work as intended there.

btw, this method of getting the process names seems fine for most tools on
macOS, but there might be trouble elsewhere. For example, it's tempting to have
pgrep/pkill use it, but on Linux they specifically *want* that truncated 'comm'
name (unless -f is used). If the function is ever used for other tools it might
need to learn some extra options to control how it gets the names.

On 20 Jun 2018, at 07:30, Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>I wrote this for using it from three new completions for macOS-specific commands
>dtruss

dtruss isn't Mac-specific. It was originally written for Solaris, and is also
available on *BSD. It should probably go under Unix i think.

dana


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

* Re: [PATCH] new function to complete names of running process  (and three example usage)
  2018-06-21  2:23 ` dana
@ 2018-06-21 11:41   ` Jun T
  2018-06-21 13:46     ` dana
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T @ 2018-06-21 11:41 UTC (permalink / raw)
  To: zsh-workers


> 2018/06/21 11:23、dana <dana@dana.is>のメール:
> 
> Solaris ps doesn't support BSD-style options (it needs -o rather than o), but if
> you account for that it should work as intended there.

Thanks. Do you know 'ps -o comm' truncates the name or not?

> For example, it's tempting to have
> pgrep/pkill use it, but on Linux they specifically *want* that truncated 'comm'
> name

Yes, I've noticed this, but I intended to use _process_names only with the
three commands at least for now, because _pgrep, _pidof and _killall all seem to
have their own tweaks.

But I've just found that killall on FreeBSD requires the truncated process names.
Probably better to add an option to _process_names to truncate the name and use
it from _killall.


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

* Re: [PATCH] new function to complete names of running process  (and three example usage)
  2018-06-21 11:41   ` Jun T
@ 2018-06-21 13:46     ` dana
  2018-06-22 12:11       ` Jun T
  0 siblings, 1 reply; 5+ messages in thread
From: dana @ 2018-06-21 13:46 UTC (permalink / raw)
  To: Jun T; +Cc: zsh-workers

On 21 Jun 2018, at 06:41, Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>Thanks. Do you know 'ps -o comm' truncates the name or not?

Sorry, didn't think to mention. Both comm and args on Solaris use psinfo_t's
pr_psargs, which is limited to 80 bytes. (comm is just args cut off at the first
white-space character.)

From what i can tell, though, DTrace's execname variable (what `dtruss -n`
matches against) seems to use pr_fname, and that's limited to 16 bytes.
Unfortunately ps's fname output is truncated further to 8 bytes for no apparent
reason, so i guess if you wanted dtruss process-name completion to be
semi-accurate on Solaris you might be better off with something like this?

  ${(@M)${(@)${(f)"$( ps -Ao comm= )"}:t}##?(#c1,16)}

dana


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

* Re: [PATCH] new function to complete names of running process  (and three example usage)
  2018-06-21 13:46     ` dana
@ 2018-06-22 12:11       ` Jun T
  0 siblings, 0 replies; 5+ messages in thread
From: Jun T @ 2018-06-22 12:11 UTC (permalink / raw)
  To: zsh-workers


> 2018/06/21 22:46、dana <dana@dana.is>のメール:
> 
> From what i can tell, though, DTrace's execname variable (what `dtruss -n`
> matches against) seems to use pr_fname, and that's limited to 16 bytes.

dtruss accepts long process name; it just uses at most 16 bytes of it.
So I *think* it is OK (or "acceptable") to complete long process names
without truncation.


diff --git a/Completion/Unix/Type/_process_names b/Completion/Unix/Type/_process_names
new file mode 100644
index 000000000..c6108373c
--- /dev/null
+++ b/Completion/Unix/Type/_process_names
@@ -0,0 +1,44 @@
+#autoload
+#
+# complete names of running processes
+#
+# options:
+#   -a: include all processes (owned by others, no tty, etc.)
+#   -t: use truncated process names (e.g., those in /proc/PID/stat)
+#       (this option is supported only on Linux and BSDs)
+#
+# this name has been used in _killall and documented in zshcompsys(1)
+local tagname='processes-names'
+typeset -a expl opts names all truncate
+
+zparseopts -E -D 'a=all' 't=truncate'
+(( $#all )) && opts=( -A )
+
+local hyphen=''
+# on Linux, use BSD-style option to include processes on other ttys
+[[ $OSTYPE != linux* ]] && hyphen='-'
+
+case $OSTYPE in
+  (linux*|freebsd*|openbsd*|netbsd*)
+    if (( $#truncate )); then
+      if [[ $OSTYPE == netbsd* ]]; then
+	opts+=(-co args=)
+      else
+	opts+=(${hyphen}o comm=)
+      fi
+      names=( ${${(f)"$(_call_program $tagname ps $opts 2>/dev/null)"}#-} )
+    else
+      opts+=(${hyphen}o args=)
+      names=( ${(f)"$(_call_program $tagname ps $opts 2>/dev/null )"} )
+      names=( ${${${${${names:#\[*]}%% *}%:}#-}:t}
+	       ${${${(M)names:#\[*]}#\[}%]} )
+    fi
+    ;;
+  (*)
+    # ignore -t option
+    opts+=(-o comm=)
+    names=( ${${${(f)"$(_call_program $tagname ps $opts 2>/dev/null)"}#-}:t} )
+    ;;
+esac
+
+_wanted $tagname expl 'process name' compadd "$@" -F '(ps)' -a - names






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

end of thread, other threads:[~2018-06-22 12:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-20 12:30 [PATCH] new function to complete names of running process (and three example usage) Jun T
2018-06-21  2:23 ` dana
2018-06-21 11:41   ` Jun T
2018-06-21 13:46     ` dana
2018-06-22 12:11       ` Jun T

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).