diff options
author | Scott Prager <splinterofchaos@gmail.com> | 2014-10-25 21:45:13 -0400 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-11-07 13:34:56 -0300 |
commit | e90973e0359b782a6e3ad4997258d9ca42495c2f (patch) | |
tree | 29527fb1a2227b3d4d27a6665b5eb220dae07dd6 /src/nvim/eval.c | |
parent | 37cbafa5bbf01c2619f009bdf876dd4453e9a28a (diff) | |
download | rneovim-e90973e0359b782a6e3ad4997258d9ca42495c2f.tar.gz rneovim-e90973e0359b782a6e3ad4997258d9ca42495c2f.tar.bz2 rneovim-e90973e0359b782a6e3ad4997258d9ca42495c2f.zip |
job: Make v:job_data[2] a list.
Factor out string_to_list() from f_system()'s implementation
and use that to set job_data. This has the technical advantage of
preserving NULs, and may be more convenient for users.
Required for #1176.
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r-- | src/nvim/eval.c | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index a58745f22a..ae40a10937 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -14434,6 +14434,29 @@ static void f_synstack(typval_T *argvars, typval_T *rettv) } } +static list_T* string_to_list(char_u *str, size_t len) +{ + list_T *list = list_alloc(); + + // Copy each line to a list element using NL as the delimiter. + for (size_t i = 0; i < len; i++) { + char_u *start = str + i; + size_t line_len = (char_u *) xmemscan(start, NL, len - i) - start; + i += line_len; + + // Don't use a str function to copy res as it may contains NULs. + char_u *s = xmemdupz(start, line_len); + memchrsub(s, NUL, NL, line_len); // Replace NUL with NL to avoid truncation + + listitem_T *li = listitem_alloc(); + li->li_tv.v_type = VAR_STRING; + li->li_tv.vval.v_string = s; + list_append(list, li); + } + + return list; +} + static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv, bool retlist) { @@ -14468,23 +14491,9 @@ static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv, } if (retlist) { - list_T *list = rettv_list_alloc(rettv); - - // Copy each line to a list element using NL as the delimiter. - for (size_t i = 0; i < nread; i++) { - char_u *start = (char_u *) res + i; - size_t len = (char_u *) xmemscan(start, NL, nread - i) - start; - i += len; - - // Don't use a str function to copy res as it may contains NULs. - char_u *s = xmemdupz(start, len); - memchrsub(s, NUL, NL, len); // Replace NUL with NL to avoid truncation. - - listitem_T *li = listitem_alloc(); - li->li_tv.v_type = VAR_STRING; - li->li_tv.vval.v_string = s; - list_append(list, li); - } + rettv->vval.v_list = string_to_list((char_u *) res, nread); + rettv->vval.v_list->lv_refcount++; + rettv->v_type = VAR_LIST; free(res); } else { @@ -19603,10 +19612,14 @@ static void apply_job_autocmds(int id, char *name, char *type, if (received) { listitem_T *str_slot = listitem_alloc(); - str_slot->li_tv.v_type = VAR_STRING; + str_slot->li_tv.v_type = VAR_LIST; str_slot->li_tv.v_lock = 0; - str_slot->li_tv.vval.v_string = (uint8_t *)received; + str_slot->li_tv.vval.v_list = + string_to_list((char_u *) received, received_len); + str_slot->li_tv.vval.v_list->lv_refcount++; list_append(list, str_slot); + + free(received); } // Update v:job_data for the autocommands |