Consolidate handling of system()/:execute calls
Signed-off-by: James McCoy <vega.james@gmail.com>
This commit is contained in:
parent
571f46455e
commit
cd8aa8d0ce
130
plugin/gnupg.vim
130
plugin/gnupg.vim
@ -1,6 +1,6 @@
|
|||||||
" Name: gnupg.vim
|
" Name: gnupg.vim
|
||||||
" Last Change: 2011 Oct 18
|
" Last Change: 2011 Nov 23
|
||||||
" Maintainer: James Vega <vega.james@gmail.com>
|
" Maintainer: James McCoy <vega.james@gmail.com>
|
||||||
" Original Author: Markus Braun <markus.braun@krawel.de>
|
" Original Author: Markus Braun <markus.braun@krawel.de>
|
||||||
" Summary: Vim plugin for transparent editing of gpg encrypted files.
|
" Summary: Vim plugin for transparent editing of gpg encrypted files.
|
||||||
" License: This program is free software; you can redistribute it and/or
|
" License: This program is free software; you can redistribute it and/or
|
||||||
@ -298,14 +298,7 @@ function s:GPGInit()
|
|||||||
call s:GPGDebug(3, "shell implementation: " . resolve(s:shell))
|
call s:GPGDebug(3, "shell implementation: " . resolve(s:shell))
|
||||||
|
|
||||||
" find the supported algorithms
|
" find the supported algorithms
|
||||||
let commandline = s:GPGCommand . " --version"
|
let output = s:GPGSystem({ 'level': 2, 'args': '--version' })
|
||||||
call s:GPGDebug(2, "command: ". commandline)
|
|
||||||
let &shellredir = s:shellredir
|
|
||||||
let &shell = s:shell
|
|
||||||
let output = system(commandline)
|
|
||||||
let &shellredir = s:shellredirsave
|
|
||||||
let &shell = s:shellsave
|
|
||||||
call s:GPGDebug(2, "output: ". output)
|
|
||||||
|
|
||||||
let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "")
|
let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "")
|
||||||
let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "")
|
let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "")
|
||||||
@ -359,16 +352,9 @@ function s:GPGDecrypt()
|
|||||||
let b:GPGEncrypted = 0
|
let b:GPGEncrypted = 0
|
||||||
|
|
||||||
" find the recipients of the file
|
" find the recipients of the file
|
||||||
let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 " . shellescape(filename)
|
let cmd = { 'level': 3 }
|
||||||
call s:GPGDebug(3, "command: " . commandline)
|
let cmd.args = '--verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 ' . shellescape(filename)
|
||||||
let &shellredir = s:shellredir
|
let output = s:GPGSystem(cmd)
|
||||||
let &shell = s:shell
|
|
||||||
let &shelltemp = s:shelltemp
|
|
||||||
let output = system(commandline)
|
|
||||||
let &shellredir = s:shellredirsave
|
|
||||||
let &shell = s:shellsave
|
|
||||||
let &shelltemp = s:shelltempsave
|
|
||||||
call s:GPGDebug(3, "output: ". output)
|
|
||||||
|
|
||||||
let asymmPattern = 'gpg: public key is \%(0x\)\=[[:xdigit:]]\{8,16}'
|
let asymmPattern = 'gpg: public key is \%(0x\)\=[[:xdigit:]]\{8,16}'
|
||||||
" check if the file is symmetric/asymmetric encrypted
|
" check if the file is symmetric/asymmetric encrypted
|
||||||
@ -437,15 +423,10 @@ function s:GPGDecrypt()
|
|||||||
" since even with the --quiet option passphrase typos will be reported,
|
" since even with the --quiet option passphrase typos will be reported,
|
||||||
" we must redirect stderr (using shell temporarily)
|
" we must redirect stderr (using shell temporarily)
|
||||||
call s:GPGDebug(1, "decrypting file")
|
call s:GPGDebug(1, "decrypting file")
|
||||||
let commandline = "r !" . s:GPGCommand . ' --quiet --decrypt ' . shellescape(filename, 1) . ' ' . s:stderrredirnull
|
let cmd = { 'level': 1, 'ex': 'r !' }
|
||||||
call s:GPGDebug(1, "command: " . commandline)
|
let cmd.args = '--quiet --decrypt ' . shellescape(filename, 1)
|
||||||
let &shellredir = s:shellredir
|
call s:GPGExecute(cmd)
|
||||||
let &shell = s:shell
|
|
||||||
let &shelltemp = s:shelltemp
|
|
||||||
execute commandline
|
|
||||||
let &shellredir = s:shellredirsave
|
|
||||||
let &shell = s:shellsave
|
|
||||||
let &shelltemp = s:shelltempsave
|
|
||||||
if (v:shell_error) " message could not be decrypted
|
if (v:shell_error) " message could not be decrypted
|
||||||
echohl GPGError
|
echohl GPGError
|
||||||
let blackhole = input("Message could not be decrypted! (Press ENTER)")
|
let blackhole = input("Message could not be decrypted! (Press ENTER)")
|
||||||
@ -547,15 +528,10 @@ function s:GPGEncrypt()
|
|||||||
|
|
||||||
" encrypt the buffer
|
" encrypt the buffer
|
||||||
let destfile = tempname()
|
let destfile = tempname()
|
||||||
let commandline = "'[,']w !" . s:GPGCommand . ' --quiet --no-encrypt-to ' . options . '>' . shellescape(destfile, 1) . ' ' . s:stderrredirnull
|
let cmd = { 'level': 1, 'ex': "'[,']w !" }
|
||||||
call s:GPGDebug(1, "command: " . commandline)
|
let cmd.args = '--quiet --no-encrypt-to ' . options
|
||||||
let &shellredir = s:shellredir
|
let cmd.redirect = '>' . shellescape(destfile, 1)
|
||||||
let &shell = s:shell
|
call s:GPGExecute(cmd)
|
||||||
let &shelltemp = s:shelltemp
|
|
||||||
silent execute commandline
|
|
||||||
let &shellredir = s:shellredirsave
|
|
||||||
let &shell = s:shellsave
|
|
||||||
let &shelltemp = s:shelltempsave
|
|
||||||
|
|
||||||
" restore encoding
|
" restore encoding
|
||||||
if (s:GPGEncoding != "")
|
if (s:GPGEncoding != "")
|
||||||
@ -1034,14 +1010,9 @@ function s:GPGNameToID(name)
|
|||||||
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()")
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()")
|
||||||
|
|
||||||
" ask gpg for the id for a name
|
" ask gpg for the id for a name
|
||||||
let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . shellescape(a:name)
|
let cmd = { 'level': 2 }
|
||||||
call s:GPGDebug(2, "command: ". commandline)
|
let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . shellescape(a:name)
|
||||||
let &shellredir = s:shellredir
|
let output = s:GPGSystem(cmd)
|
||||||
let &shell = s:shell
|
|
||||||
let output = system(commandline)
|
|
||||||
let &shellredir = s:shellredirsave
|
|
||||||
let &shell = s:shellsave
|
|
||||||
call s:GPGDebug(2, "output: ". output)
|
|
||||||
|
|
||||||
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
||||||
" so convert it, if necessary
|
" so convert it, if necessary
|
||||||
@ -1115,14 +1086,9 @@ function s:GPGIDToName(identity)
|
|||||||
" TODO is the encryption subkey really unique?
|
" TODO is the encryption subkey really unique?
|
||||||
|
|
||||||
" ask gpg for the id for a name
|
" ask gpg for the id for a name
|
||||||
let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity
|
let cmd = { 'level': 2 }
|
||||||
call s:GPGDebug(2, "command: ". commandline)
|
let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . a:identity
|
||||||
let &shellredir = s:shellredir
|
let output = s:GPGSystem(cmd)
|
||||||
let &shell = s:shell
|
|
||||||
let output = system(commandline)
|
|
||||||
let &shellredir = s:shellredirsave
|
|
||||||
let &shell = s:shellsave
|
|
||||||
call s:GPGDebug(2, "output: ". output)
|
|
||||||
|
|
||||||
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
||||||
" so convert it, if necessary
|
" so convert it, if necessary
|
||||||
@ -1157,6 +1123,62 @@ function s:GPGIDToName(identity)
|
|||||||
return uid
|
return uid
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function s:GPGPreCmd()
|
||||||
|
let &shellredir = s:shellredir
|
||||||
|
let &shell = s:shell
|
||||||
|
let &shelltemp = s:shelltemp
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function s:GPGPostCmd()
|
||||||
|
let &shellredir = s:shellredirsave
|
||||||
|
let &shell = s:shellsave
|
||||||
|
let &shelltemp = s:shelltempsave
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" Function: s:GPGSystem(dict) {{{2
|
||||||
|
"
|
||||||
|
" run g:GPGCommand using system(), logging the commandline and output
|
||||||
|
" Recognized keys are:
|
||||||
|
" level - Debug level at which the commandline and output will be logged
|
||||||
|
" args - Arguments to be given to g:GPGCommand
|
||||||
|
"
|
||||||
|
" Returns: command output
|
||||||
|
"
|
||||||
|
function s:GPGSystem(dict)
|
||||||
|
let commandline = printf('%s %s', s:GPGCommand, a:dict.args)
|
||||||
|
let commandline .= ' ' . s:stderrredirnull
|
||||||
|
call s:GPGDebug(a:dict.level, "command: ". commandline)
|
||||||
|
|
||||||
|
call s:GPGPreCmd()
|
||||||
|
let output = system(commandline)
|
||||||
|
call s:GPGPostCmd()
|
||||||
|
|
||||||
|
call s:GPGDebug(a:dict.level, "output: ". output)
|
||||||
|
return output
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" Function: s:GPGExecute(dict) {{{2
|
||||||
|
"
|
||||||
|
" run g:GPGCommand using :execute, logging the commandline
|
||||||
|
" Recognized keys are:
|
||||||
|
" level - Debug level at which the commandline will be logged
|
||||||
|
" args - Arguments to be given to g:GPGCommand
|
||||||
|
" ex - Ex command which will be :executed
|
||||||
|
" redirect - Shell redirect to use, if needed
|
||||||
|
"
|
||||||
|
function s:GPGExecute(dict)
|
||||||
|
let commandline = printf('%s%s %s', a:dict.ex, s:GPGCommand, a:dict.args)
|
||||||
|
if (has_key(a:dict, 'redirect'))
|
||||||
|
let commandline .= ' ' . a:dict.redirect
|
||||||
|
endif
|
||||||
|
let commandline .= ' ' . s:stderrredirnull
|
||||||
|
call s:GPGDebug(a:dict.level, "command: " . commandline)
|
||||||
|
|
||||||
|
call s:GPGPreCmd()
|
||||||
|
execute commandline
|
||||||
|
call s:GPGPostCmd()
|
||||||
|
endfunction
|
||||||
|
|
||||||
" Function: s:GPGDebug(level, text) {{{2
|
" Function: s:GPGDebug(level, text) {{{2
|
||||||
"
|
"
|
||||||
" output debug message, if this message has high enough importance
|
" output debug message, if this message has high enough importance
|
||||||
|
Loading…
Reference in New Issue
Block a user