Skip to content

Commit

Permalink
patch 7.4.1518
Browse files Browse the repository at this point in the history
Problem:    Channel with disconnected in/out/err is not supported.
Solution:   Implement it for Unix.
  • Loading branch information
brammool committed Mar 8, 2016
1 parent 367aabd commit f65333c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 43 deletions.
33 changes: 15 additions & 18 deletions src/eval.c
Expand Up @@ -10285,7 +10285,7 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
* Returns NULL if the handle is invalid.
*/
static channel_T *
get_channel_arg(typval_T *tv)
get_channel_arg(typval_T *tv, int check_open)
{
channel_T *channel = NULL;

Expand All @@ -10304,7 +10304,7 @@ get_channel_arg(typval_T *tv)
return NULL;
}

if (channel == NULL || !channel_is_open(channel))
if (check_open && (channel == NULL || !channel_is_open(channel)))
{
EMSG(_("E906: not an open channel"));
return NULL;
Expand All @@ -10318,7 +10318,7 @@ get_channel_arg(typval_T *tv)
static void
f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
{
channel_T *channel = get_channel_arg(&argvars[0]);
channel_T *channel = get_channel_arg(&argvars[0], TRUE);

if (channel != NULL)
{
Expand All @@ -10333,7 +10333,7 @@ f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
static void
f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
{
channel_T *channel = get_channel_arg(&argvars[0]);
channel_T *channel = get_channel_arg(&argvars[0], TRUE);

rettv->vval.v_number = -1;
if (channel != NULL)
Expand Down Expand Up @@ -10361,7 +10361,7 @@ f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
static void
f_ch_getjob(typval_T *argvars, typval_T *rettv)
{
channel_T *channel = get_channel_arg(&argvars[0]);
channel_T *channel = get_channel_arg(&argvars[0], TRUE);

if (channel != NULL)
{
Expand All @@ -10383,7 +10383,7 @@ f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
channel_T *channel = NULL;

if (argvars[1].v_type != VAR_UNKNOWN)
channel = get_channel_arg(&argvars[1]);
channel = get_channel_arg(&argvars[1], TRUE);

ch_log(channel, (char *)msg);
}
Expand Down Expand Up @@ -10500,7 +10500,7 @@ common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
== FAIL)
return;

channel = get_channel_arg(&argvars[0]);
channel = get_channel_arg(&argvars[0], TRUE);
if (channel != NULL)
{
if (opt.jo_set & JO_PART)
Expand Down Expand Up @@ -10570,7 +10570,7 @@ send_common(
channel_T *channel;
int part_send;

channel = get_channel_arg(&argvars[0]);
channel = get_channel_arg(&argvars[0], TRUE);
if (channel == NULL)
return NULL;
part_send = channel_part_send(channel);
Expand Down Expand Up @@ -10619,7 +10619,7 @@ ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;

channel = get_channel_arg(&argvars[0]);
channel = get_channel_arg(&argvars[0], TRUE);
if (channel == NULL)
return;
part_send = channel_part_send(channel);
Expand Down Expand Up @@ -10736,7 +10736,7 @@ f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
channel_T *channel;
jobopt_T opt;

channel = get_channel_arg(&argvars[0]);
channel = get_channel_arg(&argvars[0], TRUE);
if (channel == NULL)
return;
clear_job_options(&opt);
Expand All @@ -10752,17 +10752,14 @@ f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
static void
f_ch_status(typval_T *argvars, typval_T *rettv)
{
channel_T *channel;

/* return an empty string by default */
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;

if (argvars[0].v_type != VAR_CHANNEL)
{
EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
rettv->vval.v_string = NULL;
}
else
rettv->vval.v_string = vim_strsave(
(char_u *)channel_status(argvars[0].vval.v_channel));
channel = get_channel_arg(&argvars[0], FALSE);
rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
}
#endif

Expand Down
91 changes: 68 additions & 23 deletions src/os_unix.c
Expand Up @@ -5045,11 +5045,17 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
int fd_out[2]; /* for stdout */
int fd_err[2]; /* for stderr */
channel_T *channel = NULL;
int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;

if (use_out_for_err && use_null_for_out)
use_null_for_err = TRUE;

/* default is to fail */
job->jv_status = JOB_FAILED;
fd_in[0] = -1;
Expand All @@ -5072,7 +5078,7 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
goto failed;
}
}
else if (pipe(fd_in) < 0)
else if (!use_null_for_in && pipe(fd_in) < 0)
goto failed;

if (use_file_for_out)
Expand All @@ -5086,7 +5092,7 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
goto failed;
}
}
else if (pipe(fd_out) < 0)
else if (!use_null_for_out && pipe(fd_out) < 0)
goto failed;

if (use_file_for_err)
Expand All @@ -5100,12 +5106,15 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
goto failed;
}
}
else if (!use_out_for_err && pipe(fd_err) < 0)
else if (!use_out_for_err && !use_null_for_err && pipe(fd_err) < 0)
goto failed;

channel = add_channel();
if (channel == NULL)
goto failed;
if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
{
channel = add_channel();
if (channel == NULL)
goto failed;
}
# endif

pid = fork(); /* maybe we should use vfork() */
Expand All @@ -5117,6 +5126,10 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)

if (pid == 0)
{
# ifdef FEAT_CHANNEL
int null_fd = -1;
# endif

/* child */
reset_signals(); /* handle signals normally */

Expand All @@ -5131,15 +5144,31 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)

/* TODO: re-enable this when pipes connect without a channel */
# ifdef FEAT_CHANNEL
if (use_null_for_in || use_null_for_out || use_null_for_err)
null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);

/* set up stdin for the child */
if (!use_file_for_in)
close(fd_in[1]);
close(0);
ignored = dup(fd_in[0]);
close(fd_in[0]);
if (use_null_for_in)
{
close(0);
ignored = dup(null_fd);
}
else
{
if (!use_file_for_in)
close(fd_in[1]);
close(0);
ignored = dup(fd_in[0]);
close(fd_in[0]);
}

/* set up stderr for the child */
if (use_out_for_err)
if (use_null_for_err)
{
close(2);
ignored = dup(null_fd);
}
else if (use_out_for_err)
{
close(2);
ignored = dup(fd_out[1]);
Expand All @@ -5154,11 +5183,21 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
}

/* set up stdout for the child */
if (!use_file_for_out)
close(fd_out[0]);
close(1);
ignored = dup(fd_out[1]);
close(fd_out[1]);
if (use_null_for_out)
{
close(0);
ignored = dup(null_fd);
}
else
{
if (!use_file_for_out)
close(fd_out[0]);
close(1);
ignored = dup(fd_out[1]);
close(fd_out[1]);
}
if (null_fd >= 0)
close(null_fd);
# endif

/* See above for type of argv. */
Expand All @@ -5183,17 +5222,23 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED)
close(fd_out[1]);
if (!use_out_for_err && !use_file_for_err)
close(fd_err[1]);
channel_set_pipes(channel,
use_file_for_in ? INVALID_FD : fd_in[1],
use_file_for_out ? INVALID_FD : fd_out[0],
use_out_for_err || use_file_for_err
if (channel != NULL)
{
channel_set_pipes(channel,
use_file_for_in || use_null_for_in
? INVALID_FD : fd_in[1],
use_file_for_out || use_null_for_out
? INVALID_FD : fd_out[0],
use_out_for_err || use_file_for_err || use_null_for_err
? INVALID_FD : fd_err[0]);
channel_set_job(channel, job, options);
channel_set_job(channel, job, options);
# ifdef FEAT_GUI
channel_gui_register(channel);
channel_gui_register(channel);
# endif
}
# endif

/* success! */
return;

failed: ;
Expand Down
2 changes: 1 addition & 1 deletion src/structs.h
Expand Up @@ -1417,8 +1417,8 @@ struct channel_S {
#define JO_TIMEOUT_ALL (JO_TIMEOUT + JO_OUT_TIMEOUT + JO_ERR_TIMEOUT)

typedef enum {
JIO_PIPE, /* default */
JIO_NULL,
JIO_PIPE,
JIO_FILE,
JIO_BUFFER,
JIO_OUT
Expand Down
52 changes: 52 additions & 0 deletions src/testdir/test_channel.vim
Expand Up @@ -784,6 +784,58 @@ func Test_pipe_io_one_buffer()
endtry
endfunc

func Test_pipe_null()
if !has('job')
return
endif
" TODO: implement this for MS-Windows
if !has('unix')
return
endif
call ch_log('Test_pipe_null()')

" We cannot check that no I/O works, we only check that the job starts
" properly.
let job = job_start(s:python . " test_channel_pipe.py something",
\ {'in-io': 'null'})
call assert_equal("run", job_status(job))
try
call assert_equal('something', ch_read(job))
finally
call job_stop(job)
endtry

let job = job_start(s:python . " test_channel_pipe.py err-out",
\ {'out-io': 'null'})
call assert_equal("run", job_status(job))
try
call assert_equal('err-out', ch_read(job, {"part": "err"}))
finally
call job_stop(job)
endtry

let job = job_start(s:python . " test_channel_pipe.py something",
\ {'err-io': 'null'})
call assert_equal("run", job_status(job))
try
call assert_equal('something', ch_read(job))
finally
call job_stop(job)
endtry

let job = job_start(s:python . " test_channel_pipe.py something",
\ {'out-io': 'null', 'err-io': 'out'})
call assert_equal("run", job_status(job))
call job_stop(job)

let job = job_start(s:python . " test_channel_pipe.py something",
\ {'in-io': 'null', 'out-io': 'null', 'err-io': 'null'})
call assert_equal("run", job_status(job))
call assert_equal('channel fail', string(job_getchannel(job)))
call assert_equal('fail', ch_status(job))
call job_stop(job)
endfunc

""""""""""

let s:unletResponse = ''
Expand Down
7 changes: 6 additions & 1 deletion src/testdir/test_channel_pipe.py
Expand Up @@ -10,7 +10,12 @@
if __name__ == "__main__":

if len(sys.argv) > 1:
print(sys.argv[1])
if sys.argv[1].startswith("err"):
print(sys.argv[1], file=sys.stderr)
sys.stderr.flush()
else:
print(sys.argv[1])
sys.stdout.flush()

while True:
typed = sys.stdin.readline()
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -743,6 +743,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1518,
/**/
1517,
/**/
Expand Down

0 comments on commit f65333c

Please sign in to comment.