Compare commits

..

2 Commits

View File

@ -1,12 +1,15 @@
# save the current buffer to its file as root using `sudo` # save the current buffer to its file as root using `sudo`
# (optionally pass the user password to sudo if not cached) # prompt and pass the user password to sudo if not cached
define-command -hidden sudo-write-cached-password %{ # toggle fprint mode
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
eval -save-regs f %{ evaluate-commands -save-regs f %{
reg f %sh{ mktemp -t XXXXXX } set-register f %sh{ mktemp -t XXXXXX }
write! %reg{f} write! %reg{f}
eval %sh{ evaluate-commands %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!"
@ -18,12 +21,12 @@ define-command -hidden sudo-write-cached-password %{
} }
} }
define-command -hidden sudo-write-prompt-password %{ define-command -hidden sudo-write-prompt %{
prompt -password 'Password:' %{ prompt -password 'Password:' %{
eval -save-regs r %{ evaluate-commands -save-regs r %{
eval -draft -save-regs 'tf|"' %{ evaluate-commands -draft -save-regs 'tf|"' %{
reg t %val{buffile} set-register t %val{buffile}
reg f %sh{ mktemp -t XXXXXX } set-register 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
@ -31,9 +34,9 @@ define-command -hidden sudo-write-prompt-password %{
# 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*'
reg '"' "%val{text}" set-register '"' "%val{text}"
exec <a-P> execute-keys <a-P>
reg | %{ set-register | %{
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!'
@ -42,41 +45,22 @@ define-command -hidden sudo-write-prompt-password %{
fi fi
rm -f "$kak_main_reg_f" rm -f "$kak_main_reg_f"
} }
exec '|<ret>' execute-keys '|<ret>'
exec -save-regs '' '%"ry' execute-keys -save-regs '' '%"ry'
delete-buffer! '*sudo-password-tmp*' delete-buffer! '*sudo-password-tmp*'
} }
eval %reg{r} evaluate-commands %reg{r}
} }
} }
} }
define-command sudo-write -docstring "Write the content of the buffer using sudo" %{ define-command -hidden sudo-write-fprint %{
eval %sh{ evaluate-commands -save-regs f %{
# tricky posix-way of getting the first character of a variable set-register f %sh{ mktemp -t XXXXXX }
# 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}
eval %sh{ evaluate-commands %sh{
sudo -n -- dd if="$kak_main_reg_f" of="$kak_buffile" >/dev/null 2>&1 # if fprint is enabled we use -S to bypass the password prompt
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
@ -85,49 +69,44 @@ define-command -hidden sudo-write-quit-cached-password %{
rm -f "$kak_main_reg_f" rm -f "$kak_main_reg_f"
} }
} }
quit
} }
define-command -hidden sudo-write-quit-prompt-password %{
prompt -password 'Password:' %{ define-command sudo-write -docstring "Write the content of the buffer using sudo" %{
eval -save-regs r %{ evaluate-commands %sh{
eval -draft -save-regs 'tf|"' %{ # tricky posix-way of getting the first character of a variable
reg t %val{buffile} # no subprocess!
reg f %sh{ mktemp -t XXXXXX } if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then
write! %reg{f} # not entirely foolproof as a scratch buffer may start with '/', but good enough
edit -scratch '*sudo-password-tmp*' echo 'fail "Not a file"'
reg '"' "%val{text}" exit
exec <a-P> # check if fprint is enabled
reg | %{ elif "$kak_opt_fprint_mode"; then
sudo -S -- dd if="$kak_main_reg_f" of="$kak_main_reg_t" > /dev/null 2>&1 echo sudo-write-fprint
if [ $? -eq 0 ]; then # check if the password is cached
printf 'edit!' elif sudo -n true > /dev/null 2>&1; then
else echo sudo-write-cached
printf 'fail "Incorrect password?"' else
fi echo sudo-write-prompt
rm -f "$kak_main_reg_f" fi
}
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" %{
eval %sh{ evaluate-commands %sh{
if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then if [ "${kak_buffile%"${kak_buffile#?}"}" != "/" ]; then
printf 'fail "Not a file"' echo 'fail "Not a file"'
exit exit
fi elif "$kak_opt_fprint_mode"; then
if sudo -n true > /dev/null 2>&1; then echo "sudo-write-fprint"
printf sudo-write-quit-cached-password elif sudo -n true > /dev/null 2>&1; then
echo "sudo-write-cached"
else else
printf sudo-write-quit-prompt-password echo "sudo-write-prompt"
fi fi
echo "quit"
} }
} }