# HG changeset patch
# User ZyX <[email protected]>
# Date 1383992075 -14400
# Sat Nov 09 14:14:35 2013 +0400
# Branch NL-funcs
# Node ID 268983a1e62d0f05a1cb9658a65e4578128f7f2c
# Parent 5938d7011707823ab1a66bc3b3fe554ab9539e7a
Make system() support second argument that is a list.
Note: system() is currently not tested and due to it being highly platform
dependent I did neither add any tests. Neither is writefile()/readfile()
pair for whatever reason.
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -5910,10 +5910,14 @@
system({expr} [, {input}]) *system()* *E677*
Get the output of the shell command {expr}.
- When {input} is given, this string is written to a file and
- passed as stdin to the command. The string is written as-is,
- you need to take care of using the correct line separators
- yourself. Pipes are not used.
+ When {input} is given and is a string this string is written
+ to a file and passed as stdin to the command. The string is
+ written as-is, you need to take care of using the correct line
+ separators yourself. If {input} is given and is a |List| it
+ is written to the file in a way |writefile()| does with
+ {binary} set to "b" (i.e. with a newline between each list
+ item with newlines inside list items converted to NULs).
+ Pipes are not used.
Note: Use |shellescape()| to escape special characters in a
command argument. Newlines in {expr} may cause the command to
fail. The characters in 'shellquote' and 'shellxquote' may
diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -830,6 +830,7 @@
static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int
*flagsp));
static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
+static int write_list __ARGS((FILE *fd, list_T *list, int binary));
#ifdef EBCDIC
@@ -18195,14 +18196,22 @@
EMSG2(_(e_notopen), infile);
goto done;
}
- p = get_tv_string_buf_chk(&argvars[1], buf);
- if (p == NULL)
- {
- fclose(fd);
- goto done; /* type error; errmsg already given */
- }
- if (fwrite(p, STRLEN(p), 1, fd) != 1)
- err = TRUE;
+ if (argvars[1].v_type == VAR_LIST)
+ {
+ if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
+ err = TRUE;
+ }
+ else
+ {
+ p = get_tv_string_buf_chk(&argvars[1], buf);
+ if (p == NULL)
+ {
+ fclose(fd);
+ goto done; /* type error; errmsg already given */
+ }
+ if (fwrite(p, STRLEN(p), 1, fd) != 1)
+ err = TRUE;
+ }
if (fclose(fd) != 0)
err = TRUE;
if (err)
@@ -19103,6 +19112,49 @@
}
/*
+ * Write list of strings to file
+ */
+ static int
+write_list(fd, list, binary)
+ FILE *fd;
+ list_T *list;
+ int binary;
+{
+ listitem_T *li;
+ int c;
+ int ret = OK;
+ char_u *s;
+
+ for (li = list->lv_first; li != NULL; li = li->li_next)
+ {
+ for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
+ {
+ if (*s == '\n')
+ c = putc(NUL, fd);
+ else
+ c = putc(*s, fd);
+ if (c == EOF)
+ {
+ ret = FAIL;
+ break;
+ }
+ }
+ if (!binary || li->li_next != NULL)
+ if (putc('\n', fd) == EOF)
+ {
+ ret = FAIL;
+ break;
+ }
+ if (ret == FAIL)
+ {
+ EMSG(_(e_write));
+ break;
+ }
+ }
+ return ret;
+}
+
+/*
* "writefile()" function
*/
static void
@@ -19113,10 +19165,7 @@
int binary = FALSE;
char_u *fname;
FILE *fd;
- listitem_T *li;
- char_u *s;
int ret = 0;
- int c;
if (check_restricted() || check_secure())
return;
@@ -19143,33 +19192,8 @@
}
else
{
- for (li = argvars[0].vval.v_list->lv_first; li != NULL;
- li = li->li_next)
- {
- for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
- {
- if (*s == '\n')
- c = putc(NUL, fd);
- else
- c = putc(*s, fd);
- if (c == EOF)
- {
- ret = -1;
- break;
- }
- }
- if (!binary || li->li_next != NULL)
- if (putc('\n', fd) == EOF)
- {
- ret = -1;
- break;
- }
- if (ret < 0)
- {
- EMSG(_(e_write));
- break;
- }
- }
+ if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
+ ret = -1;
fclose(fd);
}
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
diff -crN -a vim-small-patches.5938d7011707/runtime/doc/eval.txt vim-small-patches.268983a1e62d/runtime/doc/eval.txt
*** vim-small-patches.5938d7011707/runtime/doc/eval.txt 2013-11-09 14:15:05.818016610 +0400
--- vim-small-patches.268983a1e62d/runtime/doc/eval.txt 2013-11-09 14:15:05.860016612 +0400
***************
*** 5910,5919 ****
system({expr} [, {input}]) *system()* *E677*
Get the output of the shell command {expr}.
! When {input} is given, this string is written to a file and
! passed as stdin to the command. The string is written as-is,
! you need to take care of using the correct line separators
! yourself. Pipes are not used.
Note: Use |shellescape()| to escape special characters in a
command argument. Newlines in {expr} may cause the command to
fail. The characters in 'shellquote' and 'shellxquote' may
--- 5910,5923 ----
system({expr} [, {input}]) *system()* *E677*
Get the output of the shell command {expr}.
! When {input} is given and is a string this string is written
! to a file and passed as stdin to the command. The string is
! written as-is, you need to take care of using the correct line
! separators yourself. If {input} is given and is a |List| it
! is written to the file in a way |writefile()| does with
! {binary} set to "b" (i.e. with a newline between each list
! item with newlines inside list items converted to NULs).
! Pipes are not used.
Note: Use |shellescape()| to escape special characters in a
command argument. Newlines in {expr} may cause the command to
fail. The characters in 'shellquote' and 'shellxquote' may
diff -crN -a vim-small-patches.5938d7011707/src/eval.c vim-small-patches.268983a1e62d/src/eval.c
*** vim-small-patches.5938d7011707/src/eval.c 2013-11-09 14:15:05.850016611 +0400
--- vim-small-patches.268983a1e62d/src/eval.c 2013-11-09 14:15:05.880016612 +0400
***************
*** 830,835 ****
--- 830,836 ----
static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
+ static int write_list __ARGS((FILE *fd, list_T *list, int binary));
#ifdef EBCDIC
***************
*** 18195,18208 ****
EMSG2(_(e_notopen), infile);
goto done;
}
! p = get_tv_string_buf_chk(&argvars[1], buf);
! if (p == NULL)
{
! fclose(fd);
! goto done; /* type error; errmsg already given */
}
- if (fwrite(p, STRLEN(p), 1, fd) != 1)
- err = TRUE;
if (fclose(fd) != 0)
err = TRUE;
if (err)
--- 18196,18217 ----
EMSG2(_(e_notopen), infile);
goto done;
}
! if (argvars[1].v_type == VAR_LIST)
{
! if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
! err = TRUE;
! }
! else
! {
! p = get_tv_string_buf_chk(&argvars[1], buf);
! if (p == NULL)
! {
! fclose(fd);
! goto done; /* type error; errmsg already given */
! }
! if (fwrite(p, STRLEN(p), 1, fd) != 1)
! err = TRUE;
}
if (fclose(fd) != 0)
err = TRUE;
if (err)
***************
*** 19103,19108 ****
--- 19112,19160 ----
}
/*
+ * Write list of strings to file
+ */
+ static int
+ write_list(fd, list, binary)
+ FILE *fd;
+ list_T *list;
+ int binary;
+ {
+ listitem_T *li;
+ int c;
+ int ret = OK;
+ char_u *s;
+
+ for (li = list->lv_first; li != NULL; li = li->li_next)
+ {
+ for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
+ {
+ if (*s == '\n')
+ c = putc(NUL, fd);
+ else
+ c = putc(*s, fd);
+ if (c == EOF)
+ {
+ ret = FAIL;
+ break;
+ }
+ }
+ if (!binary || li->li_next != NULL)
+ if (putc('\n', fd) == EOF)
+ {
+ ret = FAIL;
+ break;
+ }
+ if (ret == FAIL)
+ {
+ EMSG(_(e_write));
+ break;
+ }
+ }
+ return ret;
+ }
+
+ /*
* "writefile()" function
*/
static void
***************
*** 19113,19122 ****
int binary = FALSE;
char_u *fname;
FILE *fd;
- listitem_T *li;
- char_u *s;
int ret = 0;
- int c;
if (check_restricted() || check_secure())
return;
--- 19165,19171 ----
***************
*** 19143,19175 ****
}
else
{
! for (li = argvars[0].vval.v_list->lv_first; li != NULL;
! li = li->li_next)
! {
! for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
! {
! if (*s == '\n')
! c = putc(NUL, fd);
! else
! c = putc(*s, fd);
! if (c == EOF)
! {
! ret = -1;
! break;
! }
! }
! if (!binary || li->li_next != NULL)
! if (putc('\n', fd) == EOF)
! {
! ret = -1;
! break;
! }
! if (ret < 0)
! {
! EMSG(_(e_write));
! break;
! }
! }
fclose(fd);
}
--- 19192,19199 ----
}
else
{
! if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
! ret = -1;
fclose(fd);
}