Skip to content

Commit b6b5252

Browse files
committed
patch 7.4.1376
Problem: ch_setoptions() cannot set all options. Solution: Support more options.
1 parent e89ff04 commit b6b5252

File tree

5 files changed

+171
-41
lines changed

5 files changed

+171
-41
lines changed

src/channel.c

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -737,27 +737,58 @@ channel_set_job(channel_T *channel, job_T *job)
737737
}
738738

739739
/*
740-
* Set various properties from an "options" argument.
740+
* Set various properties from an "opt" argument.
741741
*/
742742
void
743-
channel_set_options(channel_T *channel, jobopt_T *options)
743+
channel_set_options(channel_T *channel, jobopt_T *opt)
744744
{
745-
int part;
745+
int part;
746+
char_u **cbp;
746747

747-
if (options->jo_set & JO_MODE)
748+
if (opt->jo_set & JO_MODE)
748749
for (part = PART_SOCK; part <= PART_IN; ++part)
749-
channel->ch_part[part].ch_mode = options->jo_mode;
750-
if (options->jo_set & JO_TIMEOUT)
750+
channel->ch_part[part].ch_mode = opt->jo_mode;
751+
if (opt->jo_set & JO_IN_MODE)
752+
channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
753+
if (opt->jo_set & JO_OUT_MODE)
754+
channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
755+
if (opt->jo_set & JO_ERR_MODE)
756+
channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
757+
758+
if (opt->jo_set & JO_TIMEOUT)
751759
for (part = PART_SOCK; part <= PART_IN; ++part)
752-
channel->ch_part[part].ch_timeout = options->jo_timeout;
760+
channel->ch_part[part].ch_timeout = opt->jo_timeout;
761+
if (opt->jo_set & JO_OUT_TIMEOUT)
762+
channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
763+
if (opt->jo_set & JO_ERR_TIMEOUT)
764+
channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
753765

754-
if (options->jo_set & JO_CALLBACK)
766+
if (opt->jo_set & JO_CALLBACK)
755767
{
756-
vim_free(channel->ch_callback);
757-
if (options->jo_callback != NULL && *options->jo_callback != NUL)
758-
channel->ch_callback = vim_strsave(options->jo_callback);
768+
cbp = &channel->ch_callback;
769+
vim_free(*cbp);
770+
if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
771+
*cbp = vim_strsave(opt->jo_callback);
759772
else
760-
channel->ch_callback = NULL;
773+
*cbp = NULL;
774+
}
775+
if (opt->jo_set & JO_OUT_CALLBACK)
776+
{
777+
cbp = &channel->ch_part[PART_OUT].ch_callback;
778+
vim_free(*cbp);
779+
if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
780+
*cbp = vim_strsave(opt->jo_out_cb);
781+
else
782+
*cbp = NULL;
783+
}
784+
if (opt->jo_set & JO_ERR_CALLBACK)
785+
{
786+
cbp = &channel->ch_part[PART_ERR].ch_callback;
787+
vim_free(*cbp);
788+
if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
789+
*cbp = vim_strsave(opt->jo_err_cb);
790+
else
791+
*cbp = NULL;
761792
}
762793
}
763794

src/eval.c

Lines changed: 101 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9873,6 +9873,34 @@ get_callback(typval_T *arg)
98739873
return NULL;
98749874
}
98759875

9876+
static int
9877+
handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9878+
{
9879+
char_u *val = get_tv_string(item);
9880+
9881+
opt->jo_set |= jo;
9882+
if (STRCMP(val, "nl") == 0)
9883+
*modep = MODE_NL;
9884+
else if (STRCMP(val, "raw") == 0)
9885+
*modep = MODE_RAW;
9886+
else if (STRCMP(val, "js") == 0)
9887+
*modep = MODE_JS;
9888+
else if (STRCMP(val, "json") == 0)
9889+
*modep = MODE_JSON;
9890+
else
9891+
{
9892+
EMSG2(_(e_invarg2), val);
9893+
return FAIL;
9894+
}
9895+
return OK;
9896+
}
9897+
9898+
static void
9899+
clear_job_options(jobopt_T *opt)
9900+
{
9901+
vim_memset(opt, 0, sizeof(jobopt_T));
9902+
}
9903+
98769904
/*
98779905
* Get the option entries from the dict in "tv", parse them and put the result
98789906
* in "opt".
@@ -9910,21 +9938,32 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
99109938
{
99119939
if (!(supported & JO_MODE))
99129940
break;
9913-
opt->jo_set |= JO_MODE;
9914-
val = get_tv_string(item);
9915-
if (STRCMP(val, "nl") == 0)
9916-
opt->jo_mode = MODE_NL;
9917-
else if (STRCMP(val, "raw") == 0)
9918-
opt->jo_mode = MODE_RAW;
9919-
else if (STRCMP(val, "js") == 0)
9920-
opt->jo_mode = MODE_JS;
9921-
else if (STRCMP(val, "json") == 0)
9922-
opt->jo_mode = MODE_JSON;
9923-
else
9924-
{
9925-
EMSG2(_(e_invarg2), val);
9941+
if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
9942+
return FAIL;
9943+
}
9944+
else if (STRCMP(hi->hi_key, "in-mode") == 0)
9945+
{
9946+
if (!(supported & JO_IN_MODE))
9947+
break;
9948+
if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
9949+
== FAIL)
9950+
return FAIL;
9951+
}
9952+
else if (STRCMP(hi->hi_key, "out-mode") == 0)
9953+
{
9954+
if (!(supported & JO_OUT_MODE))
9955+
break;
9956+
if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
9957+
== FAIL)
9958+
return FAIL;
9959+
}
9960+
else if (STRCMP(hi->hi_key, "err-mode") == 0)
9961+
{
9962+
if (!(supported & JO_ERR_MODE))
9963+
break;
9964+
if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
9965+
== FAIL)
99269966
return FAIL;
9927-
}
99289967
}
99299968
else if (STRCMP(hi->hi_key, "callback") == 0)
99309969
{
@@ -9938,6 +9977,30 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
99389977
return FAIL;
99399978
}
99409979
}
9980+
else if (STRCMP(hi->hi_key, "out-cb") == 0)
9981+
{
9982+
if (!(supported & JO_OUT_CALLBACK))
9983+
break;
9984+
opt->jo_set |= JO_OUT_CALLBACK;
9985+
opt->jo_out_cb = get_callback(item);
9986+
if (opt->jo_out_cb == NULL)
9987+
{
9988+
EMSG2(_(e_invarg2), "out-db");
9989+
return FAIL;
9990+
}
9991+
}
9992+
else if (STRCMP(hi->hi_key, "err-cb") == 0)
9993+
{
9994+
if (!(supported & JO_ERR_CALLBACK))
9995+
break;
9996+
opt->jo_set |= JO_ERR_CALLBACK;
9997+
opt->jo_err_cb = get_callback(item);
9998+
if (opt->jo_err_cb == NULL)
9999+
{
10000+
EMSG2(_(e_invarg2), "err-cb");
10001+
return FAIL;
10002+
}
10003+
}
994110004
else if (STRCMP(hi->hi_key, "waittime") == 0)
994210005
{
994310006
if (!(supported & JO_WAITTIME))
@@ -9952,6 +10015,20 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
995210015
opt->jo_set |= JO_TIMEOUT;
995310016
opt->jo_timeout = get_tv_number(item);
995410017
}
10018+
else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10019+
{
10020+
if (!(supported & JO_OUT_TIMEOUT))
10021+
break;
10022+
opt->jo_set |= JO_OUT_TIMEOUT;
10023+
opt->jo_out_timeout = get_tv_number(item);
10024+
}
10025+
else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10026+
{
10027+
if (!(supported & JO_ERR_TIMEOUT))
10028+
break;
10029+
opt->jo_set |= JO_ERR_TIMEOUT;
10030+
opt->jo_err_timeout = get_tv_number(item);
10031+
}
995510032
else if (STRCMP(hi->hi_key, "part") == 0)
995610033
{
995710034
if (!(supported & JO_PART))
@@ -10107,12 +10184,11 @@ f_ch_open(typval_T *argvars, typval_T *rettv)
1010710184
}
1010810185

1010910186
/* parse options */
10187+
clear_job_options(&opt);
1011010188
opt.jo_mode = MODE_JSON;
10111-
opt.jo_callback = NULL;
10112-
opt.jo_waittime = 0;
1011310189
opt.jo_timeout = 2000;
1011410190
if (get_job_options(&argvars[1], &opt,
10115-
JO_MODE + JO_CALLBACK + JO_WAITTIME + JO_TIMEOUT) == FAIL)
10191+
JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
1011610192
return;
1011710193
if (opt.jo_timeout < 0)
1011810194
{
@@ -10147,7 +10223,7 @@ common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
1014710223
rettv->v_type = VAR_STRING;
1014810224
rettv->vval.v_string = NULL;
1014910225

10150-
opt.jo_set = 0;
10226+
clear_job_options(&opt);
1015110227
if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
1015210228
== FAIL)
1015310229
return;
@@ -10219,7 +10295,7 @@ send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
1021910295
part_send = channel_part_send(channel);
1022010296
*part_read = channel_part_read(channel);
1022110297

10222-
opt.jo_callback = NULL;
10298+
clear_job_options(&opt);
1022310299
if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
1022410300
return NULL;
1022510301

@@ -10329,7 +10405,9 @@ f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1032910405
channel = get_channel_arg(&argvars[0]);
1033010406
if (channel == NULL)
1033110407
return;
10332-
if (get_job_options(&argvars[1], &opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
10408+
clear_job_options(&opt);
10409+
if (get_job_options(&argvars[1], &opt,
10410+
JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
1033310411
return;
1033410412
channel_set_options(channel, &opt);
1033510413
}
@@ -14684,9 +14762,10 @@ f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
1468414762
rettv->vval.v_job->jv_status = JOB_FAILED;
1468514763

1468614764
/* Default mode is NL. */
14765+
clear_job_options(&opt);
1468714766
opt.jo_mode = MODE_NL;
14688-
opt.jo_callback = NULL;
14689-
if (get_job_options(&argvars[1], &opt, JO_MODE + JO_CALLBACK) == FAIL)
14767+
if (get_job_options(&argvars[1], &opt,
14768+
JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL) == FAIL)
1469014769
return;
1469114770

1469214771
#ifndef USE_ARGV

src/structs.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,14 +1373,25 @@ struct channel_S {
13731373
int ch_refcount; /* reference count */
13741374
};
13751375

1376-
#define JO_MODE 1 /* all modes */
1377-
#define JO_CALLBACK 2 /* channel callback */
1378-
#define JO_WAITTIME 4 /* only for ch_open() */
1379-
#define JO_TIMEOUT 8 /* all timeouts */
1380-
#define JO_PART 16 /* "part" */
1381-
#define JO_ID 32 /* "id" */
1376+
#define JO_MODE 0x0001 /* channel mode */
1377+
#define JO_IN_MODE 0x0002 /* stdin mode */
1378+
#define JO_OUT_MODE 0x0004 /* stdout mode */
1379+
#define JO_ERR_MODE 0x0008 /* stderr mode */
1380+
#define JO_CALLBACK 0x0010 /* channel callback */
1381+
#define JO_OUT_CALLBACK 0x0020 /* stdout callback */
1382+
#define JO_ERR_CALLBACK 0x0040 /* stderr callback */
1383+
#define JO_WAITTIME 0x0080 /* only for ch_open() */
1384+
#define JO_TIMEOUT 0x0100 /* all timeouts */
1385+
#define JO_OUT_TIMEOUT 0x0200 /* stdout timeouts */
1386+
#define JO_ERR_TIMEOUT 0x0400 /* stderr timeouts */
1387+
#define JO_PART 0x0800 /* "part" */
1388+
#define JO_ID 0x1000 /* "id" */
13821389
#define JO_ALL 0xffffff
13831390

1391+
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
1392+
#define JO_CB_ALL (JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK)
1393+
#define JO_TIMEOUT_ALL (JO_TIMEOUT + JO_OUT_TIMEOUT + JO_ERR_TIMEOUT)
1394+
13841395
/*
13851396
* Options for job and channel commands.
13861397
*/
@@ -1389,9 +1400,16 @@ typedef struct
13891400
int jo_set; /* JO_ bits for values that were set */
13901401

13911402
ch_mode_T jo_mode;
1403+
ch_mode_T jo_in_mode;
1404+
ch_mode_T jo_out_mode;
1405+
ch_mode_T jo_err_mode;
13921406
char_u *jo_callback; /* not allocated! */
1407+
char_u *jo_out_cb; /* not allocated! */
1408+
char_u *jo_err_cb; /* not allocated! */
13931409
int jo_waittime;
13941410
int jo_timeout;
1411+
int jo_out_timeout;
1412+
int jo_err_timeout;
13951413
int jo_part;
13961414
int jo_id;
13971415
} jobopt_T;

src/testdir/test_channel.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ func s:communicate(port)
147147
" check setting options (without testing the effect)
148148
call ch_setoptions(handle, {'callback': 's:NotUsed'})
149149
call ch_setoptions(handle, {'timeout': 1111})
150+
call ch_setoptions(handle, {'mode': 'json'})
150151
call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
151-
call assert_fails("call ch_setoptions(handle, {'mode': 'json'})", "E475")
152152
call ch_setoptions(handle, {'callback': ''})
153153

154154
" Send an eval request that works.

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ static char *(features[]) =
747747

748748
static int included_patches[] =
749749
{ /* Add new patch number below this line */
750+
/**/
751+
1376,
750752
/**/
751753
1375,
752754
/**/

0 commit comments

Comments
 (0)