2021-10-11 15:03:24 -07:00
|
|
|
#!/usr/bin/env sh
|
2024-07-22 17:54:54 -07:00
|
|
|
# compat: +ash +bash +dash -hush -ksh +mksh +oksh +osh +posh +yash +zsh
|
2021-10-01 12:00:23 -07:00
|
|
|
|
2021-07-30 17:57:08 -07:00
|
|
|
e() { ### @-
|
|
|
|
### wrap around `$EDITOR` to run it as root if necessary.
|
|
|
|
### this still needs some work to detect root-owned directories.
|
|
|
|
###
|
|
|
|
### ```
|
|
|
|
### $ e /etc/sudoers
|
|
|
|
### [sudo] password for notwa:
|
|
|
|
### ```
|
2021-10-13 23:43:58 -07:00
|
|
|
###
|
|
|
|
### **NOTE:** there also exists an e(1) program provided by
|
|
|
|
### the *e-wrapper* package that i don't use.
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2021-10-11 15:03:24 -07:00
|
|
|
[ -z "$ZSH_OPTIONS" ] || setopt local_options sh_word_split
|
2021-10-11 19:48:31 -07:00
|
|
|
local d= f= temp= running= editor= needroot=0
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2021-10-11 15:03:24 -07:00
|
|
|
editor="${EDITOR%% *}"
|
2019-06-05 20:14:29 -07:00
|
|
|
|
2021-10-11 15:03:24 -07:00
|
|
|
(running pid cmd | while read -r pid cmd; do
|
|
|
|
if [ "$cmd" = "$editor" ]; then
|
|
|
|
printf "%s (%s)\n" "$editor is already running" "$pid" >&2
|
|
|
|
confirm
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
done) || return
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2021-10-11 19:48:31 -07:00
|
|
|
for f; do
|
|
|
|
temp="$(readlink -f "$f")" && f="$temp"
|
2021-10-11 15:03:24 -07:00
|
|
|
|
|
|
|
[ -z "$MSYSTEM" ] || f="$(cygpath -u "$f")" || continue
|
|
|
|
|
2021-07-29 00:37:35 -07:00
|
|
|
# easy: file exists, we have write permissions
|
|
|
|
[ -w "$f" ] && continue
|
|
|
|
|
|
|
|
# easy: file exists, but no write permissions
|
2021-10-11 15:03:24 -07:00
|
|
|
[ -e "$f" ] && { needroot=1; break; }
|
2021-07-29 00:37:35 -07:00
|
|
|
|
|
|
|
# hard: file may be in a directory that we can't inspect
|
2021-10-11 15:03:24 -07:00
|
|
|
d="$f"
|
|
|
|
while [ "${d%/*}" != "$d" ]; do
|
2021-07-29 00:37:35 -07:00
|
|
|
d="${d%/*}"
|
2021-10-11 19:53:46 -07:00
|
|
|
[ -w "$d/" ] && break
|
|
|
|
[ -e "$d/" ] && { needroot=1; break; }
|
2021-07-29 00:37:35 -07:00
|
|
|
done
|
2021-10-11 19:48:31 -07:00
|
|
|
[ $needroot = 1 ] && break
|
2021-07-29 00:37:35 -07:00
|
|
|
|
|
|
|
# easy: file just doesn't exist
|
2019-06-05 20:14:29 -07:00
|
|
|
done
|
|
|
|
|
2021-10-11 19:48:31 -07:00
|
|
|
if [ $needroot = 1 ] && [ -n "$MSYSTEM" ]; then
|
2021-10-11 15:03:24 -07:00
|
|
|
# this pretty much never happens, because permissions are so busted, but...
|
|
|
|
printf "NOTE: you need root permissions, but this is Windows." >&2
|
|
|
|
printf " this probably isn't going to work." >&2
|
|
|
|
pause
|
|
|
|
$EDITOR "$@"
|
2021-10-11 19:48:31 -07:00
|
|
|
elif [ $needroot = 0 ]; then
|
2021-10-11 15:03:24 -07:00
|
|
|
$EDITOR "$@"
|
2021-07-29 00:37:35 -07:00
|
|
|
else
|
|
|
|
sudo -e "$@"
|
|
|
|
fi
|
|
|
|
}
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2021-10-11 15:03:24 -07:00
|
|
|
[ -n "${preload+-}" ] || . ~/sh/preload || exit 2
|
|
|
|
eval ${preload:-preload} running confirm pause
|
2021-08-02 13:48:46 -07:00
|
|
|
[ -n "${preload+-}" ] || e "$@"
|