serverstop(): return FALSE for invalid address

This commit is contained in:
Justin M. Keyes 2018-04-08 13:06:26 +02:00
parent b11b681289
commit 9f598e5765
5 changed files with 42 additions and 52 deletions

View File

@ -25,7 +25,7 @@ To enter commands in
type a colon type a colon
.Pq Sq \&: .Pq Sq \&:
which is also used in this manual to denote commands. which is also used in this manual to denote commands.
For more information, consult the on-line help system with the For more information, consult the online help system with the
.Ic :help .Ic :help
command. command.
.Bl -tag -width Fl .Bl -tag -width Fl
@ -337,12 +337,12 @@ Print version information and exit.
.Sh ENVIRONMENT .Sh ENVIRONMENT
.Bl -tag -width Fl .Bl -tag -width Fl
.It Ev VIM .It Ev VIM
Used to locate various user files, such as the user's init.vim. Used to locate various user files, such as init.vim.
.It Ev VIMRUNTIME .It Ev VIMRUNTIME
Used to locate run time files, such as on-line documentation and Used to locate runtime files, such as online documentation and
syntax highlighting definitions. syntax highlighting definitions.
.It Ev XDG_CONFIG_HOME .It Ev XDG_CONFIG_HOME
Path to use for the user-local configuration directory, see Path to the user-local configuration directory, see
.Sx FILES . .Sx FILES .
Defaults to Defaults to
.Pa ~/.config .Pa ~/.config
@ -356,7 +356,7 @@ Defaults to
.Pa ~/.local/share .Pa ~/.local/share
if not set. if not set.
.It Ev VIMINIT .It Ev VIMINIT
A string of Ex commands to be executed at startup. Ex commands to be executed at startup.
For example, the command to quit is For example, the command to quit is
.Ic :q , .Ic :q ,
so to have so to have
@ -375,41 +375,32 @@ command.
.Sh FILES .Sh FILES
.Bl -tag -width "~/.config/nvim/init.vim" .Bl -tag -width "~/.config/nvim/init.vim"
.It Pa ~/.config/nvim/init.vim .It Pa ~/.config/nvim/init.vim
The user-local User-local
.Nm .Nm
configuration file. configuration file.
See
.Ev XDG_CONFIG_HOME
above.
.It Pa ~/.config/nvim .It Pa ~/.config/nvim
The user-local User-local
.Nm .Nm
configuration directory. configuration directory.
See See also
.Ev XDG_CONFIG_HOME .Ev XDG_CONFIG_HOME .
above.
.It Pa $VIM/sysinit.vim .It Pa $VIM/sysinit.vim
The system-global System-global
.Nm .Nm
configuration file. configuration file.
.It Pa /usr/local/share/nvim .It Pa /usr/local/share/nvim
The system-global System-global
.Nm .Nm
runtime directory. runtime directory.
.El .El
.Sh AUTHORS .Sh AUTHORS
.Nm Nvim was started by
was started by .An Thiago de Arruda .
.An Thiago de Arruda ,
with a lot of help from others.
.Pp
Most of Vim was written by Most of Vim was written by
.An -nosplit .An -nosplit
.An Bram Moolenaar , .An Bram Moolenaar .
with a lot of help from others.
See See
.Ic :help credits . .Ic :help credits .
.Pp
Vim is based on Stevie, worked on by Vim is based on Stevie, worked on by
.An Tim Thompson , .An Tim Thompson ,
.An Tony Andrews , .An Tony Andrews ,

View File

@ -1789,8 +1789,10 @@ v:scrollstart String describing the script or function that caused the
*v:servername* *servername-variable* *v:servername* *servername-variable*
*$NVIM_LISTEN_ADDRESS* *$NVIM_LISTEN_ADDRESS*
v:servername Default Nvim server address. Equivalent to v:servername Primary listen address of the current Nvim instance, the first
|$NVIM_LISTEN_ADDRESS| on startup. |serverstop()| item returned by |serverlist()|. Can be set by
|--listen| or |$NVIM_LISTEN_ADDRESS| on startup.
See also |serverstart()| and |serverstop()|.
Read-only. Read-only.
@ -6638,15 +6640,11 @@ server2client({clientid}, {string}) *server2client()*
:echo server2client(expand("<client>"), "HELLO") :echo server2client(expand("<client>"), "HELLO")
< <
serverlist() *serverlist()* serverlist() *serverlist()*
Returns a list of available server names in a list. Returns a list of server addresses, or empty if all servers
When there are no servers an empty string is returned. were stopped. |serverstart()| |serverstop()|
Example: > Example: >
:echo serverlist() :echo serverlist()
< {Nvim} *--serverlist*
The Vim command-line option `--serverlist` was removed from
Nvim, but it can be imitated: >
nvim --cmd "echo serverlist()" --cmd "q"
<
serverstart([{address}]) *serverstart()* serverstart([{address}]) *serverstart()*
Opens a socket or named pipe at {address} and listens for Opens a socket or named pipe at {address} and listens for
|RPC| messages. Clients can send |API| commands to the address |RPC| messages. Clients can send |API| commands to the address
@ -6674,13 +6672,9 @@ serverstart([{address}]) *serverstart()*
< |$NVIM_LISTEN_ADDRESS| is set to {address} if not already set. < |$NVIM_LISTEN_ADDRESS| is set to {address} if not already set.
*--servername*
The Vim command-line option `--servername` can be imitated: >
nvim --cmd "let g:server_addr = serverstart('foo')"
<
serverstop({address}) *serverstop()* serverstop({address}) *serverstop()*
Closes the pipe or socket at {address}. Does nothing if Closes the pipe or socket at {address}.
{address} is empty or invalid. Returns TRUE if {address} is valid, else FALSE.
If |$NVIM_LISTEN_ADDRESS| is stopped it is unset. If |$NVIM_LISTEN_ADDRESS| is stopped it is unset.
If |v:servername| is stopped it is set to the next available If |v:servername| is stopped it is set to the next available
address returned by |serverlist()|. address returned by |serverlist()|.

View File

@ -14403,8 +14403,11 @@ static void f_serverstop(typval_T *argvars, typval_T *rettv, FunPtr fptr)
return; return;
} }
rettv->v_type = VAR_NUMBER;
rettv->vval.v_number = 0;
if (argvars[0].vval.v_string) { if (argvars[0].vval.v_string) {
server_stop((char *) argvars[0].vval.v_string); bool rv = server_stop((char *)argvars[0].vval.v_string);
rettv->vval.v_number = (rv ? 1 : 0);
} }
} }

View File

@ -177,7 +177,7 @@ int server_start(const char *endpoint)
/// Stops listening on the address specified by `endpoint`. /// Stops listening on the address specified by `endpoint`.
/// ///
/// @param endpoint Address of the server. /// @param endpoint Address of the server.
void server_stop(char *endpoint) bool server_stop(char *endpoint)
{ {
SocketWatcher *watcher; SocketWatcher *watcher;
bool watcher_found = false; bool watcher_found = false;
@ -196,8 +196,8 @@ void server_stop(char *endpoint)
} }
if (!watcher_found) { if (!watcher_found) {
ELOG("Not listening on %s", addr); WLOG("Not listening on %s", addr);
return; return false;
} }
// Unset $NVIM_LISTEN_ADDRESS if it is the stopped address. // Unset $NVIM_LISTEN_ADDRESS if it is the stopped address.
@ -219,6 +219,8 @@ void server_stop(char *endpoint)
if (STRCMP(addr, get_vim_var_str(VV_SEND_SERVER)) == 0) { if (STRCMP(addr, get_vim_var_str(VV_SEND_SERVER)) == 0) {
set_vservername(&watchers); set_vservername(&watchers);
} }
return true;
} }
/// Returns an allocated array of server addresses. /// Returns an allocated array of server addresses.

View File

@ -11,21 +11,21 @@ local function clear_serverlist()
end end
end end
describe('serverstart(), serverstop()', function() describe('server', function()
before_each(clear) before_each(clear)
it('sets $NVIM_LISTEN_ADDRESS on first invocation', function() it('serverstart() sets $NVIM_LISTEN_ADDRESS on first invocation', function()
-- Unset $NVIM_LISTEN_ADDRESS -- Unset $NVIM_LISTEN_ADDRESS
command('let $NVIM_LISTEN_ADDRESS = ""') command('let $NVIM_LISTEN_ADDRESS = ""')
local s = eval('serverstart()') local s = eval('serverstart()')
assert(s ~= nil and s:len() > 0, "serverstart() returned empty") assert(s ~= nil and s:len() > 0, "serverstart() returned empty")
eq(s, eval('$NVIM_LISTEN_ADDRESS')) eq(s, eval('$NVIM_LISTEN_ADDRESS'))
command("call serverstop('"..s.."')") eq(1, eval("serverstop('"..s.."')"))
eq('', eval('$NVIM_LISTEN_ADDRESS')) eq('', eval('$NVIM_LISTEN_ADDRESS'))
end) end)
it('sets v:servername _only_ on nvim startup unless all servers are stopped', it('serverstart() sets v:servername at startup or if all servers were stopped',
function() function()
local initial_server = meths.get_vvar('servername') local initial_server = meths.get_vvar('servername')
assert(initial_server ~= nil and initial_server:len() > 0, assert(initial_server ~= nil and initial_server:len() > 0,
@ -38,11 +38,11 @@ describe('serverstart(), serverstop()', function()
neq(initial_server, s) neq(initial_server, s)
-- serverstop() does _not_ modify v:servername... -- serverstop() does _not_ modify v:servername...
funcs.serverstop(s) eq(1, funcs.serverstop(s))
eq(initial_server, meths.get_vvar('servername')) eq(initial_server, meths.get_vvar('servername'))
-- ...unless we stop _all_ servers. -- ...unless we stop _all_ servers.
funcs.serverstop(funcs.serverlist()[1]) eq(1, funcs.serverstop(funcs.serverlist()[1]))
eq('', meths.get_vvar('servername')) eq('', meths.get_vvar('servername'))
-- v:servername will take the next available server. -- v:servername will take the next available server.
@ -53,9 +53,9 @@ describe('serverstart(), serverstop()', function()
eq(servername, meths.get_vvar('servername')) eq(servername, meths.get_vvar('servername'))
end) end)
it('serverstop() ignores invalid input', function() it('serverstop() returns false for invalid input', function()
command("call serverstop('')") eq(0, eval("serverstop('')"))
command("call serverstop('bogus-socket-name')") eq(0, eval("serverstop('bogus-socket-name')"))
end) end)
it('parses endpoints correctly', function() it('parses endpoints correctly', function()
@ -120,7 +120,7 @@ describe('serverlist()', function()
-- The new servers should be at the end of the list. -- The new servers should be at the end of the list.
for i = 1, #servs do for i = 1, #servs do
eq(servs[i], new_servs[i + n]) eq(servs[i], new_servs[i + n])
command("call serverstop('"..servs[i].."')") eq(1, eval("serverstop('"..servs[i].."')"))
end end
-- After serverstop() the servers should NOT be in the list. -- After serverstop() the servers should NOT be in the list.
eq(n, eval('len(serverlist())')) eq(n, eval('len(serverlist())'))