eval: serverstart: Return finalized address to user

In the process of setting up the socket watcher, the address may be
changed (e.g., adding the OS-selected port).
This commit is contained in:
James McCoy 2017-05-27 22:25:06 -04:00
parent 3f85c2e43a
commit 6c135b89ee
No known key found for this signature in database
GPG Key ID: DFE691AE331BA3DB
2 changed files with 25 additions and 7 deletions

View File

@ -14296,23 +14296,39 @@ static void f_serverstart(typval_T *argvars, typval_T *rettv, FunPtr fptr)
return;
}
char *address;
// If the user supplied an address, use it, otherwise use a temp.
if (argvars[0].v_type != VAR_UNKNOWN) {
if (argvars[0].v_type != VAR_STRING) {
EMSG(_(e_invarg));
return;
} else {
rettv->vval.v_string = (char_u *)xstrdup(tv_get_string(argvars));
address = xstrdup(tv_get_string(argvars));
}
} else {
rettv->vval.v_string = (char_u *)server_address_new();
address = server_address_new();
}
int result = server_start((char *) rettv->vval.v_string);
int result = server_start(address);
xfree(address);
if (result != 0) {
EMSG2("Failed to start server: %s",
result > 0 ? "Unknonwn system error" : uv_strerror(result));
return;
}
// Since it's possible server_start adjusted the given {address} (e.g.,
// "localhost:" will now have a port), return the final value to the user.
size_t n;
char **addrs = server_address_list(&n);
rettv->vval.v_string = (char_u *)addrs[n - 1];
n--;
for (size_t i = 0; i < n; i++) {
xfree(addrs[i]);
}
xfree(addrs);
}
/// "serverstop()" function

View File

@ -62,12 +62,14 @@ describe('serverstart(), serverstop()', function()
clear_serverlist()
eq({}, funcs.serverlist())
funcs.serverstart('127.0.0.1:0') -- assign random port
assert(string.match(funcs.serverlist()[1], '127.0.0.1:%d+'))
local s = funcs.serverstart('127.0.0.1:0') -- assign random port
assert(string.match(s, '127.0.0.1:%d+'))
eq(s, funcs.serverlist()[1])
clear_serverlist()
funcs.serverstart('127.0.0.1:') -- assign random port
assert(string.match(funcs.serverlist()[1], '127.0.0.1:%d+'))
s = funcs.serverstart('127.0.0.1:') -- assign random port
assert(string.match(s, '127.0.0.1:%d+'))
eq(s, funcs.serverlist()[1])
clear_serverlist()
funcs.serverstart('127.0.0.1:12345')