Compare commits

..

1 Commits

View File

@ -1,15 +1,12 @@
# save the current buffer to its file as root using `sudo` # save the current buffer to its file as root using `sudo`
# prompt and pass the user password to sudo if not cached # (optionally pass the user password to sudo if not cached)
# toggle fprint mode define-command -hidden sudo-write-cached-password %{
declare-option bool fprint_mode false
define-command -hidden sudo-write-cached %{
# easy case: the password was already cached, so we don't need any tricky handling # easy case: the password was already cached, so we don't need any tricky handling
evaluate-commands -save-regs f %{ eval -save-regs f %{
set-register f %sh{ mktemp -t XXXXXX } reg f %sh{ mktemp -t XXXXXX }
write! %reg{f} write! %reg{f}
evaluate-commands %sh{ eval %sh{
sudo -n -- dd if="$kak_main_reg_f" of="$kak_buffile" >/dev/null 2>&1 sudo -n -- dd if="$kak_main_reg_f" of="$kak_buffile" >/dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "edit!" echo "edit!"
@ -21,12 +18,12 @@ define-command -hidden sudo-write-cached %{
} }
} }
define-command -hidden sudo-write-prompt %{ define-command -hidden sudo-write-prompt-password %{
prompt -password 'Password:' %{ prompt -password 'Password:' %{
evaluate-commands -save-regs r %{ eval -save-regs r %{
evaluate-commands -draft -save-regs 'tf|"' %{ eval -draft -save-regs 'tf|"' %{
set-register t %val{buffile} reg t %val{buffile}
set-register f %sh{ mktemp -t XXXXXX } reg f %sh{ mktemp -t XXXXXX }
write! %reg{f} write! %reg{f}
# write the password in a buffer in order to pass it through STDIN to sudo # write the password in a buffer in order to pass it through STDIN to sudo
@ -34,9 +31,9 @@ define-command -hidden sudo-write-prompt %{
# through the shell scope's environment or interpolating it inside the shell string # through the shell scope's environment or interpolating it inside the shell string
# 'exec |' is pretty much the only way to pass data over STDIN # 'exec |' is pretty much the only way to pass data over STDIN
edit -scratch '*sudo-password-tmp*' edit -scratch '*sudo-password-tmp*'
set-register '"' "%val{text}" reg '"' "%val{text}"
execute-keys <a-P> exec <a-P>
set-register | %{ reg | %{
sudo -S -- dd if="$kak_main_reg_f" of="$kak_main_reg_t" > /dev/null 2>&1 sudo -S -- dd if="$kak_main_reg_f" of="$kak_main_reg_t" > /dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
printf 'edit!' printf 'edit!'
@ -45,22 +42,41 @@ define-command -hidden sudo-write-prompt %{
fi fi
rm -f "$kak_main_reg_f" rm -f "$kak_main_reg_f"
} }
execute-keys '|<ret>' exec '|<ret>'
execute-keys -save-regs '' '%"ry' exec -save-regs '' '%"ry'
delete-buffer! '*sudo-password-tmp*' delete-buffer! '*sudo-password-tmp*'
} }
evaluate-commands %reg{r} eval %reg{r}
} }
} }
} }
define-command -hidden sudo-write-fprint %{ define-command sudo-write -docstring "Write the content of the buffer using sudo" %{
evaluate-commands -save-regs f %{ eval %sh{
set-register f %sh{ mktemp -t XXXXXX } # tricky posix-way of getting the first character of a variable
# no subprocess!
if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then
# not entirely foolproof as a scratch buffer may start with '/', but good enough
printf 'fail "Not a file"'
exit
fi
# check if the password is cached
if sudo -n true > /dev/null 2>&1; then
printf sudo-write-cached-password
else
printf sudo-write-prompt-password
fi
}
}
# SAME AS ABOVE WITH 'QUIT' APPENEDED
define-command -hidden sudo-write-quit-cached-password %{
eval -save-regs f %{
reg f %sh{ mktemp -t XXXXXX }
write! %reg{f} write! %reg{f}
evaluate-commands %sh{ eval %sh{
# if fprint is enabled we use -S to bypass the password prompt sudo -n -- dd if="$kak_main_reg_f" of="$kak_buffile" >/dev/null 2>&1
sudo -S -- dd if="$kak_main_reg_f" of="$kak_buffile" >/dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "edit!" echo "edit!"
else else
@ -69,44 +85,49 @@ define-command -hidden sudo-write-fprint %{
rm -f "$kak_main_reg_f" rm -f "$kak_main_reg_f"
} }
} }
quit
} }
define-command -hidden sudo-write-quit-prompt-password %{
define-command sudo-write -docstring "Write the content of the buffer using sudo" %{ prompt -password 'Password:' %{
evaluate-commands %sh{ eval -save-regs r %{
# tricky posix-way of getting the first character of a variable eval -draft -save-regs 'tf|"' %{
# no subprocess! reg t %val{buffile}
if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then reg f %sh{ mktemp -t XXXXXX }
# not entirely foolproof as a scratch buffer may start with '/', but good enough write! %reg{f}
echo 'fail "Not a file"' edit -scratch '*sudo-password-tmp*'
exit reg '"' "%val{text}"
# check if fprint is enabled exec <a-P>
elif "$kak_opt_fprint_mode"; then reg | %{
echo sudo-write-fprint sudo -S -- dd if="$kak_main_reg_f" of="$kak_main_reg_t" > /dev/null 2>&1
# check if the password is cached if [ $? -eq 0 ]; then
elif sudo -n true > /dev/null 2>&1; then printf 'edit!'
echo sudo-write-cached
else else
echo sudo-write-prompt printf 'fail "Incorrect password?"'
fi fi
rm -f "$kak_main_reg_f"
}
exec '|<ret>'
exec -save-regs '' '%"ry'
delete-buffer! '*sudo-password-tmp*'
}
eval %reg{r}
}
quit
} }
} }
# SAME AS ABOVE WITH 'QUIT' APPENEDED
define-command sudo-write-quit -docstring "Write the content of the buffer using sudo, then quit" %{ define-command sudo-write-quit -docstring "Write the content of the buffer using sudo, then quit" %{
evaluate-commands %sh{ eval %sh{
if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then
echo 'fail "Not a file"' printf 'fail "Not a file"'
exit exit
elif "$kak_opt_fprint_mode"; then
echo "sudo-write-fprint"
elif sudo -n true > /dev/null 2>&1; then
echo "sudo-write-cached"
else
echo "sudo-write-prompt"
fi fi
echo "quit" if sudo -n true > /dev/null 2>&1; then
printf sudo-write-quit-cached-password
else
printf sudo-write-quit-prompt-password
fi
} }
} }