mirror of
https://github.com/neovim/neovim.git
synced 2024-12-20 03:05:11 -07:00
job: Let vimL jobsend() accept a list.
Use save_tv_as_string(), same as vimL system(). This also makes jobsend() more liberal in what it can accept. For example, `jobsend(j, 123)` is now valid. Closes #1176
This commit is contained in:
parent
e90973e035
commit
fd36dc208e
@ -4014,8 +4014,15 @@ items({dict}) *items()*
|
||||
|
||||
jobsend({job}, {data}) {Nvim} *jobsend()*
|
||||
Send data to {job} by writing it to the stdin of the process.
|
||||
Returns 1 if the write succeeded, 0 otherwise.
|
||||
See |job-control| for more information.
|
||||
|
||||
{data} may be a string, string convertible, or a list. If
|
||||
{data} is a list, the items will be separated by newlines and
|
||||
any newlines in an item will be sent as a NUL. For example: >
|
||||
:call jobsend(j, ["abc", "123\n456"])
|
||||
< will send "abc<NL>123<NUL>456<NL>".
|
||||
|
||||
jobstart({name}, {prog}[, {argv}]) {Nvim} *jobstart()*
|
||||
Spawns {prog} as a job and associate it with the {name} string,
|
||||
which will be used to match the "filename pattern" in
|
||||
|
@ -10593,9 +10593,9 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv)
|
||||
return;
|
||||
}
|
||||
|
||||
if (argvars[0].v_type != VAR_NUMBER || argvars[1].v_type != VAR_STRING) {
|
||||
// First argument is the job id and second is the string to write to
|
||||
// the job's stdin
|
||||
if (argvars[0].v_type != VAR_NUMBER || argvars[1].v_type == VAR_UNKNOWN) {
|
||||
// First argument is the job id and second is the string or list to write
|
||||
// to the job's stdin
|
||||
EMSG(_(e_invarg));
|
||||
return;
|
||||
}
|
||||
@ -10608,10 +10608,15 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv)
|
||||
return;
|
||||
}
|
||||
|
||||
WBuffer *buf = wstream_new_buffer(xstrdup((char *)argvars[1].vval.v_string),
|
||||
strlen((char *)argvars[1].vval.v_string),
|
||||
1,
|
||||
free);
|
||||
ssize_t input_len;
|
||||
char *input = (char *) save_tv_as_string(&argvars[1], &input_len, true);
|
||||
if (input_len < 0) {
|
||||
return; // Error handled by save_tv_as_string().
|
||||
} else if (input_len == 0) {
|
||||
return; // Not an error, but nothing to do.
|
||||
}
|
||||
|
||||
WBuffer *buf = wstream_new_buffer(input, input_len, 1, free);
|
||||
rettv->vval.v_number = job_write(job, buf);
|
||||
}
|
||||
|
||||
@ -14469,7 +14474,7 @@ static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv,
|
||||
|
||||
// get input to the shell command (if any), and its length
|
||||
ssize_t input_len;
|
||||
char *input = (char *) save_tv_as_string(&argvars[1], &input_len);
|
||||
char *input = (char *) save_tv_as_string(&argvars[1], &input_len, false);
|
||||
if (input_len == -1) {
|
||||
return;
|
||||
}
|
||||
@ -15158,9 +15163,10 @@ static bool write_list(FILE *fd, list_T *list, bool binary)
|
||||
///
|
||||
/// @param[in] tv A value to store as a string.
|
||||
/// @param[out] len The length of the resulting string or -1 on error.
|
||||
/// @param[in] endnl If true, the output will end in a newline (if a list).
|
||||
/// @returns an allocated string if `tv` represents a VimL string, list, or
|
||||
/// number; NULL otherwise.
|
||||
static char_u *save_tv_as_string(typval_T *tv, ssize_t *len)
|
||||
static char_u *save_tv_as_string(typval_T *tv, ssize_t *len, bool endnl)
|
||||
FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
|
||||
{
|
||||
if (tv->v_type == VAR_UNKNOWN) {
|
||||
@ -15192,13 +15198,13 @@ static char_u *save_tv_as_string(typval_T *tv, ssize_t *len)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char_u *ret = xmalloc(*len);
|
||||
char_u *ret = xmalloc(*len + endnl);
|
||||
char_u *end = ret;
|
||||
for (listitem_T *li = list->lv_first; li != NULL; li = li->li_next) {
|
||||
for (char_u *s = get_tv_string(&li->li_tv); *s != NUL; s++) {
|
||||
*end++ = (*s == '\n') ? NUL : *s;
|
||||
}
|
||||
if (li->li_next != NULL) {
|
||||
if (endnl || li->li_next != NULL) {
|
||||
*end++ = '\n';
|
||||
}
|
||||
}
|
||||
|
@ -44,11 +44,33 @@ describe('jobs', function()
|
||||
eq({'notification', 'stdout', {{'abc'}}}, next_message())
|
||||
nvim('command', 'call jobsend(j, "123\\nxyz\\n")')
|
||||
eq({'notification', 'stdout', {{'123', 'xyz'}}}, next_message())
|
||||
nvim('command', 'call jobsend(j, [123, "xyz"])')
|
||||
eq({'notification', 'stdout', {{'123', 'xyz'}}}, next_message())
|
||||
nvim('command', notify_str('v:job_data[1])'))
|
||||
nvim('command', "call jobstop(j)")
|
||||
eq({'notification', 'exit', {}}, next_message())
|
||||
end)
|
||||
|
||||
it('preserves NULs', function()
|
||||
-- Make a file with NULs in it.
|
||||
local filename = os.tmpname()
|
||||
local file = io.open(filename, "w")
|
||||
file:write("abc\0def\n")
|
||||
file:close()
|
||||
|
||||
-- v:job_data preserves NULs.
|
||||
nvim('command', notify_str('v:job_data[1]', 'v:job_data[2]'))
|
||||
nvim('command', "let j = jobstart('xxx', 'cat', ['"..filename.."'])")
|
||||
eq({'notification', 'stdout', {{'abc\ndef'}}}, next_message())
|
||||
os.remove(filename)
|
||||
|
||||
-- jobsend() preserves NULs.
|
||||
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
|
||||
nvim('command', [[call jobsend(j, ["123\n456"])]])
|
||||
eq({'notification', 'stdout', {{'123\n456'}}}, next_message())
|
||||
nvim('command', "call jobstop(j)")
|
||||
end)
|
||||
|
||||
it('will hold data if it does not end in a newline', function()
|
||||
nvim('command', notify_str('v:job_data[1]', 'v:job_data[2]'))
|
||||
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
|
||||
@ -58,7 +80,6 @@ describe('jobs', function()
|
||||
eq({'notification', 'stdout', {{'xyz'}}}, next_message())
|
||||
end)
|
||||
|
||||
|
||||
it('will not allow jobsend/stop on a non-existent job', function()
|
||||
eq(false, pcall(eval, "jobsend(-1, 'lol')"))
|
||||
eq(false, pcall(eval, "jobstop(-1, 'lol')"))
|
||||
|
Loading…
Reference in New Issue
Block a user