jobsend: Don't append extra newline after last item

This allows sending binary data that is not newline terminated
This commit is contained in:
Björn Linse 2015-02-28 13:39:26 +01:00
parent 6b7ece6cc8
commit 5be040ffe4
3 changed files with 18 additions and 6 deletions

View File

@ -4019,8 +4019,9 @@ jobsend({job}, {data}) {Nvim} *jobsend()*
{data} may be a string, string convertible, or a list. If {data} may be a string, string convertible, or a list. If
{data} is a list, the items will be separated by newlines and {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: > any newlines in an item will be sent as a NUL. A final newline
:call jobsend(j, ["abc", "123\n456"]) can be sent by adding a final empty string. For example: >
:call jobsend(j, ["abc", "123\n456", ""])
< will send "abc<NL>123<NUL>456<NL>". < will send "abc<NL>123<NUL>456<NL>".
jobstart({name}, {prog}[, {argv}]) {Nvim} *jobstart()* jobstart({name}, {prog}[, {argv}]) {Nvim} *jobstart()*

View File

@ -10656,7 +10656,7 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv)
} }
ssize_t input_len; ssize_t input_len;
char *input = (char *) save_tv_as_string(&argvars[1], &input_len, true); char *input = (char *) save_tv_as_string(&argvars[1], &input_len, false);
if (!input) { if (!input) {
// Either the error has been handled by save_tv_as_string(), or there is no // Either the error has been handled by save_tv_as_string(), or there is no
// input to send. // input to send.

View File

@ -45,7 +45,7 @@ describe('jobs', function()
eq({'notification', 'stdout', {{'abc', ''}}}, next_message()) eq({'notification', 'stdout', {{'abc', ''}}}, next_message())
nvim('command', 'call jobsend(j, "123\\nxyz\\n")') nvim('command', 'call jobsend(j, "123\\nxyz\\n")')
eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message()) eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message())
nvim('command', 'call jobsend(j, [123, "xyz"])') nvim('command', 'call jobsend(j, [123, "xyz", ""])')
eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message()) eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message())
nvim('command', "call jobstop(j)") nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message()) eq({'notification', 'exit', {0}}, next_message())
@ -67,7 +67,7 @@ describe('jobs', function()
-- jobsend() preserves NULs. -- jobsend() preserves NULs.
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', [[call jobsend(j, ["123\n456"])]]) nvim('command', [[call jobsend(j, ["123\n456",""])]])
eq({'notification', 'stdout', {{'123\n456', ''}}}, next_message()) eq({'notification', 'stdout', {{'123\n456', ''}}}, next_message())
nvim('command', "call jobstop(j)") nvim('command', "call jobstop(j)")
end) end)
@ -92,13 +92,24 @@ describe('jobs', function()
it('can preserve nuls', function() it('can preserve nuls', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', 'call jobsend(j, ["\n123\n", "abc\\nxyz\n"])') nvim('command', 'call jobsend(j, ["\n123\n", "abc\\nxyz\n", ""])')
eq({'notification', 'stdout', {{'\n123\n', 'abc\nxyz\n', ''}}}, eq({'notification', 'stdout', {{'\n123\n', 'abc\nxyz\n', ''}}},
next_message()) next_message())
nvim('command', "call jobstop(j)") nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message()) eq({'notification', 'exit', {0}}, next_message())
end) end)
it('can avoid sending final newline', function()
nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)'))
nvim('command', "let j = jobstart('xxx', 'cat', ['-'])")
nvim('command', 'call jobsend(j, ["some data", "without\nfinal nl"])')
eq({'notification', 'stdout', {{'some data', 'without\nfinal nl'}}},
next_message())
nvim('command', "call jobstop(j)")
eq({'notification', 'exit', {0}}, next_message())
end)
it('will not allow jobsend/stop on a non-existent job', function() it('will not allow jobsend/stop on a non-existent job', function()
eq(false, pcall(eval, "jobsend(-1, 'lol')")) eq(false, pcall(eval, "jobsend(-1, 'lol')"))
eq(false, pcall(eval, "jobstop(-1)")) eq(false, pcall(eval, "jobstop(-1)"))