Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusive file lock during cmd execution and some bugs. #296

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions util/stumpish/stumpish
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### STUMPwm Interactive SHell.

# use a busy-waiting delay of 1 second if floats are not supported by sleep
DELAY=0.01

if ! sleep $DELAY 2>/dev/null >&2
Expand All @@ -37,11 +38,34 @@ if [ "$(echo -e foo)" = foo ]; then
echo() { builtin echo -e "$@"; }
fi

stumpwm_pid ()
{
local pid=$$

while :
do
if [ $pid -eq 1 ]
then
echo "StumpWM not found in the process tree, are you sure a graphical " 1>&2
echo "session is running and StumpWM is your WM? If you think this is " 1>&2
echo "a bug in stumpish, please report it." 1>&2
echo 1>&2
exit 1
elif [ "$(cat /proc/${pid}/comm)" = "stumpwm" ]
then
STUMPWM_PID=$pid
break
else
pid=$(cut -f 4 -d " " < /proc/$pid/stat)
fi
done
}

wait_result ()
{
while true
do
RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8s \
RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8u \
STUMPWM_COMMAND_RESULT 2>/dev/null |
sed -E 's/\\([[:digit:]]+)/\\0\1/g')
if echo "$RESULT" | grep -v -q 'not found.$'
Expand Down Expand Up @@ -70,6 +94,9 @@ wait_result ()

send_cmd ()
{
(
flock -n 3 || fail "Cannot obtain a file lock to exclusively talk to StumpWM."

local cmd="$1"

if [ "$cmd" = "stumpwm-quit" ]
Expand All @@ -80,9 +107,10 @@ send_cmd ()
exit
fi

xprop -root -f STUMPWM_COMMAND 8s -set STUMPWM_COMMAND "$cmd"
xprop -root -f STUMPWM_COMMAND 8u -set STUMPWM_COMMAND "$cmd"

wait_result
) 3>"${TMPDIR:-/tmp}/.stumpish.lock.$STUMPWM_PID"
}

usage ()
Expand Down Expand Up @@ -138,6 +166,9 @@ fi
if [ $# -gt 0 ]
then
[ "$1" = "--help" ] && usage

stumpwm_pid

if [ "$1" = "-e" ]
then
if [ $# -ne 2 ]
Expand All @@ -154,6 +185,8 @@ then
send_cmd "$*"
fi
else
stumpwm_pid

if [ -t 0 ]
then
if ! type rlwrap 2>/dev/null >&2
Expand All @@ -180,7 +213,7 @@ else
tput me sgr0
echo \ for a list of commands.

while (echo -n '> '; read -r REPLY)
while { echo -n '> '; read -r REPLY; }
do
tput md bold
tput AF setaf 2
Expand Down