diff options
author | Scott Prager <splinterofchaos@gmail.com> | 2015-04-01 23:07:41 -0400 |
---|---|---|
committer | Scott Prager <splinterofchaos@gmail.com> | 2015-04-14 15:31:04 -0400 |
commit | 296719428a5adc7c4f00a7f1778771c5ec9af9d0 (patch) | |
tree | a6842605c77eeddca41a35d5ecd14d414ea0d48c /src/nvim/msgpack_rpc/server.c | |
parent | 4a66c48d8b6e7f74b64f06d176a10d8b6d38491b (diff) | |
download | rneovim-296719428a5adc7c4f00a7f1778771c5ec9af9d0.tar.gz rneovim-296719428a5adc7c4f00a7f1778771c5ec9af9d0.tar.bz2 rneovim-296719428a5adc7c4f00a7f1778771c5ec9af9d0.zip |
eval: learn serverstart, list, and close().
Implement functions for spawning, destroying, and listing active
servers, and add server_address_list() to msgpack_rpc/server.c for the
serverlist() vimL function.
Diffstat (limited to 'src/nvim/msgpack_rpc/server.c')
-rw-r--r-- | src/nvim/msgpack_rpc/server.c | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/src/nvim/msgpack_rpc/server.c b/src/nvim/msgpack_rpc/server.c index 5469347db0..8fb2902b0b 100644 --- a/src/nvim/msgpack_rpc/server.c +++ b/src/nvim/msgpack_rpc/server.c @@ -56,13 +56,18 @@ bool server_init(void) { ga_init(&servers, sizeof(Server *), 1); - if (!os_getenv(LISTEN_ADDRESS_ENV_VAR)) { - char *listen_address = (char *)vim_tempname(); - os_setenv(LISTEN_ADDRESS_ENV_VAR, listen_address, 1); - xfree(listen_address); + bool must_free = false; + const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR); + if (listen_address == NULL || *listen_address == NUL) { + must_free = true; + listen_address = (char *)vim_tempname(); } - return server_start((char *)os_getenv(LISTEN_ADDRESS_ENV_VAR)) == 0; + bool ok = (server_start(listen_address) == 0); + if (must_free) { + xfree((char *) listen_address); + } + return ok; } /// Retrieve the file handle from a server. @@ -199,6 +204,12 @@ int server_start(const char *endpoint) return result; } + // Update $NVIM_LISTEN_ADDRESS, if not set. + const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR); + if (listen_address == NULL || *listen_address == NUL) { + os_setenv(LISTEN_ADDRESS_ENV_VAR, addr, 1); + } + server->type = server_type; // Add the server to the list. @@ -232,6 +243,12 @@ void server_stop(char *endpoint) return; } + // If we are invalidating the listen address, unset it. + const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR); + if (listen_address && strcmp(addr, listen_address) == 0) { + os_unsetenv(LISTEN_ADDRESS_ENV_VAR); + } + uv_close(server_handle(server), free_server); // Remove this server from the list by swapping it with the last item. @@ -242,6 +259,22 @@ void server_stop(char *endpoint) servers.ga_len--; } +/// Returns an allocated array of server addresses. +/// @param[out] size The size of the returned array. +char **server_address_list(size_t *size) + FUNC_ATTR_NONNULL_ALL +{ + if ((*size = (size_t) servers.ga_len) == 0) { + return NULL; + } + + char **addrs = xcalloc((size_t) servers.ga_len, sizeof(const char **)); + for (int i = 0; i < servers.ga_len; i++) { + addrs[i] = xstrdup(((Server **)servers.ga_data)[i]->addr); + } + return addrs; +} + static void connection_cb(uv_stream_t *server, int status) { int result; |