From 8100b4872e2cfcc1d9f5ee8b8f42b1a101a96357 Mon Sep 17 00:00:00 2001 From: Greg Hurrell Date: Fri, 19 Jun 2020 18:07:14 +0200 Subject: [PATCH] feat: attempt to autodetect need for -N switch to nc Based on discussion here: https://github.com/wincent/vim-clipper/issues/2 --- autoload/clipper/private.vim | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/autoload/clipper/private.vim b/autoload/clipper/private.vim index 7388011..86f8ca1 100644 --- a/autoload/clipper/private.vim +++ b/autoload/clipper/private.vim @@ -10,15 +10,44 @@ endfunction function! clipper#private#clip() abort if exists('s:invocation') && s:invocation != '' call system(s:invocation, @0) - elseif executable('nc') == 1 + elseif clipper#private#executable() != '' + let l:executable = clipper#private#executable() let l:address = get(g:, 'ClipperAddress', 'localhost') let l:port = +(get(g:, 'ClipperPort', 8377)) " Co-erce to number. if l:port - call system('nc ' . l:address . ' ' . l:port, @0) + " nc + call system(l:executable . ' ' . l:address . ' ' . l:port, @0) else - call system('nc -U ' . l:address, @0) + " nc -U + call system(l:executable . ' -U ' . l:address, @0) endif else echoerr 'Clipper: nc executable does not exist' endif endfunction + +let s:executable='' + +function! clipper#private#executable() abort + if s:executable == '' && executable('nc') == 1 + " Try to figure out whether -N switch is supported and required. + " Examples: + " - Ubuntu, FreeBSD and similar: + " Supports and requires -N: + " "shutdown(2) the network socket after EOF on the input" + " http://manpages.ubuntu.com/manpages/bionic/man1/nc_openbsd.1.html + " https://www.freebsd.org/cgi/man.cgi?nc + " - Darwin: + " Supports but does not require -N; it does something else: + " "Number of probes to send before generating a write timeout event" + " - CentOS etc: + " Does not support or need -N. + let l:help = system('nc -h') + if match(l:help, '\v\s-N\s.+shutdown>') != -1 + let s:executable = 'nc -N' + else + let s:executable = 'nc' + endif + endif + return s:executable +endfunction