1
0
dotfiles/.config/zsh/lib/pswalk.zsh

84 lines
1.9 KiB
Bash
Raw Normal View History

2021-03-21 18:25:10 +03:00
#!/bin/zsh
2024-03-04 15:35:55 +03:00
z-proc-exists() {
[ -n "${1:?}" ]
while [ -n "${ZSHU[procfs]}" ] ; do
[ -d "${ZSHU[procfs]}" ] || return 1
[ -f "${ZSHU[procfs]}/$1/status" ]
return $?
done
ps -o 'pid=' -p "$1" >/dev/null 2>&1
}
2021-03-21 18:25:10 +03:00
typeset -Uga ZSHU_PARENTS_PID
typeset -ga ZSHU_PARENTS_NAME
function {
2024-03-04 15:35:55 +03:00
local procfs
while [ -n "${ZSHU[procfs]}" ] ; do
[ -d "${ZSHU[procfs]}" ] || break
procfs=1 ; break
done
local i c x _unused
2021-03-21 18:25:10 +03:00
2024-02-28 23:22:33 +03:00
i=${PPID}
while : ; do
[ -n "$i" ] || break
2021-04-07 15:51:49 +03:00
## don't deal with PID1
2024-02-28 23:22:33 +03:00
[ "$i" = 1 ] && break
2021-03-21 18:25:10 +03:00
2024-02-28 23:22:33 +03:00
ZSHU_PARENTS_PID+=( $i )
2024-03-04 15:35:55 +03:00
c=
while [ "${procfs}" = 1 ] ; do
[ -f "${ZSHU[procfs]}/$i/cmdline" ] || break
read -d $'\0' -rs c <<< $(cat "${ZSHU[procfs]}/$i/cmdline")
break
done
if [ -z "$c" ] ; then
read -rs c _unused <<< "$(ps -o 'comm=' -p "$i" 2>/dev/null)"
fi
2024-02-28 23:22:33 +03:00
[ -n "$c" ] && ZSHU_PARENTS_NAME+=( "${c:t}" )
2024-03-04 15:35:55 +03:00
x=
while [ "${procfs}" = 1 ] ; do
[ -f "${ZSHU[procfs]}/$i/status" ] || break
# read -rs _unused x <<< "$(cat "${ZSHU[procfs]}/$i/status" | grep -F 'PPid:')"
while read -rs _unused c ; do
[ "${_unused}" = 'PPid:' ] || continue
x=$c ; break
done < "${ZSHU[procfs]}/$i/status"
break
done
if [ -z "$x" ] ; then
read -rs x _unused <<< "$(ps -o 'ppid=' -p "$i" 2>/dev/null)"
fi
i=$x
2021-03-21 18:25:10 +03:00
done
2024-02-28 23:22:33 +03:00
typeset -r ZSHU_PARENTS_PID ZSHU_PARENTS_NAME
2021-03-21 18:25:10 +03:00
}
typeset -gA ZSHU_RUN
z-run-test() {
2024-01-16 02:55:39 +03:00
local k i
2022-07-19 02:41:45 +03:00
2024-01-16 02:55:39 +03:00
k=$1 ; shift
2021-03-21 18:25:10 +03:00
for i ( ${ZSHU_PARENTS_NAME} ) ; do
2024-01-16 02:55:39 +03:00
(( ${+argv[(r)$i]} )) || continue
ZSHU_RUN[$k]=1
return
2021-03-21 18:25:10 +03:00
done
2024-01-16 02:55:39 +03:00
ZSHU_RUN[$k]=0
2021-03-21 18:25:10 +03:00
}
z-run-test gui konsole xterm x-terminal-emulator
2021-03-22 15:32:51 +03:00
z-run-test nested screen tmux mc
2024-01-16 02:55:39 +03:00
z-run-test nested1L mc
2021-03-21 18:25:10 +03:00
z-run-test elevated sudo su