Blame SOURCES/7.4.248

d6ba96
To: vim_dev@googlegroups.com
d6ba96
Subject: Patch 7.4.248
d6ba96
Fcc: outbox
d6ba96
From: Bram Moolenaar <Bram@moolenaar.net>
d6ba96
Mime-Version: 1.0
d6ba96
Content-Type: text/plain; charset=UTF-8
d6ba96
Content-Transfer-Encoding: 8bit
d6ba96
------------
d6ba96
d6ba96
Patch 7.4.248
d6ba96
Problem:    Cannot distinguish between NL and NUL in output of system().
d6ba96
Solution:   Add systemlist(). (ZyX)
d6ba96
Files:	    runtime/doc/eval.txt, src/eval.c, src/ex_cmds2.c, src/misc1.c,
d6ba96
	    src/proto/misc1.pro
d6ba96
d6ba96
d6ba96
*** ../vim-7.4.247/runtime/doc/eval.txt	2014-04-05 18:55:40.471154309 +0200
d6ba96
--- runtime/doc/eval.txt	2014-04-05 19:03:05.419155281 +0200
d6ba96
***************
d6ba96
*** 2001,2006 ****
d6ba96
--- 2002,2008 ----
d6ba96
  synconcealed( {lnum}, {col})	List    info about concealing
d6ba96
  synstack( {lnum}, {col})	List	stack of syntax IDs at {lnum} and {col}
d6ba96
  system( {expr} [, {input}])	String	output of shell command/filter {expr}
d6ba96
+ systemlist( {expr} [, {input}])	List	output of shell command/filter {expr}
d6ba96
  tabpagebuflist( [{arg}])	List	list of buffer numbers in tab page
d6ba96
  tabpagenr( [{arg}])		Number	number of current or last tab page
d6ba96
  tabpagewinnr( {tabarg}[, {arg}])
d6ba96
***************
d6ba96
*** 5950,5956 ****
d6ba96
  		valid positions.
d6ba96
  
d6ba96
  system({expr} [, {input}])				*system()* *E677*
d6ba96
! 		Get the output of the shell command {expr}.
d6ba96
  
d6ba96
  		When {input} is given and is a string this string is written 
d6ba96
  		to a file and passed as stdin to the command.  The string is 
d6ba96
--- 5964,5971 ----
d6ba96
  		valid positions.
d6ba96
  
d6ba96
  system({expr} [, {input}])				*system()* *E677*
d6ba96
! 		Get the output of the shell command {expr} as a string.  See
d6ba96
! 		|systemlist()| to get the output as a List.
d6ba96
  
d6ba96
  		When {input} is given and is a string this string is written 
d6ba96
  		to a file and passed as stdin to the command.  The string is 
d6ba96
***************
d6ba96
*** 5998,6003 ****
d6ba96
--- 6013,6028 ----
d6ba96
  		Use |:checktime| to force a check.
d6ba96
  
d6ba96
  
d6ba96
+ systemlist({expr} [, {input}])				*systemlist()*
d6ba96
+ 		Same as |system()|, but returns a |List| with lines (parts of 
d6ba96
+ 		output separated by NL) with NULs transformed into NLs. Output 
d6ba96
+ 		is the same as |readfile()| will output with {binary} argument 
d6ba96
+ 		set to "b".
d6ba96
+ 
d6ba96
+ 		Returns an empty string on error, so be careful not to run 
d6ba96
+ 		into |E706|.
d6ba96
+ 
d6ba96
+ 
d6ba96
  tabpagebuflist([{arg}])					*tabpagebuflist()*
d6ba96
  		The result is a |List|, where each item is the number of the
d6ba96
  		buffer associated with each window in the current tab page.
d6ba96
*** ../vim-7.4.247/src/eval.c	2014-04-05 18:55:40.479154309 +0200
d6ba96
--- src/eval.c	2014-04-05 19:42:37.675160463 +0200
d6ba96
***************
d6ba96
*** 726,731 ****
d6ba96
--- 726,732 ----
d6ba96
  static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
  static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
  static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
+ static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
  static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
  static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
  static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
d6ba96
***************
d6ba96
*** 837,842 ****
d6ba96
--- 838,844 ----
d6ba96
  static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
d6ba96
  static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
d6ba96
  static int write_list __ARGS((FILE *fd, list_T *list, int binary));
d6ba96
+ static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
d6ba96
  
d6ba96
  
d6ba96
  #ifdef EBCDIC
d6ba96
***************
d6ba96
*** 8139,8144 ****
d6ba96
--- 8141,8147 ----
d6ba96
      {"synconcealed",	2, 2, f_synconcealed},
d6ba96
      {"synstack",	2, 2, f_synstack},
d6ba96
      {"system",		1, 2, f_system},
d6ba96
+     {"systemlist",	1, 2, f_systemlist},
d6ba96
      {"tabpagebuflist",	0, 1, f_tabpagebuflist},
d6ba96
      {"tabpagenr",	0, 1, f_tabpagenr},
d6ba96
      {"tabpagewinnr",	1, 2, f_tabpagewinnr},
d6ba96
***************
d6ba96
*** 18232,18244 ****
d6ba96
  #endif
d6ba96
  }
d6ba96
  
d6ba96
- /*
d6ba96
-  * "system()" function
d6ba96
-  */
d6ba96
      static void
d6ba96
! f_system(argvars, rettv)
d6ba96
      typval_T	*argvars;
d6ba96
      typval_T	*rettv;
d6ba96
  {
d6ba96
      char_u	*res = NULL;
d6ba96
      char_u	*p;
d6ba96
--- 18235,18245 ----
d6ba96
  #endif
d6ba96
  }
d6ba96
  
d6ba96
      static void
d6ba96
! get_cmd_output_as_rettv(argvars, rettv, retlist)
d6ba96
      typval_T	*argvars;
d6ba96
      typval_T	*rettv;
d6ba96
+     int		retlist;
d6ba96
  {
d6ba96
      char_u	*res = NULL;
d6ba96
      char_u	*p;
d6ba96
***************
d6ba96
*** 18246,18254 ****
d6ba96
      char_u	buf[NUMBUFLEN];
d6ba96
      int		err = FALSE;
d6ba96
      FILE	*fd;
d6ba96
  
d6ba96
      if (check_restricted() || check_secure())
d6ba96
! 	goto done;
d6ba96
  
d6ba96
      if (argvars[1].v_type != VAR_UNKNOWN)
d6ba96
      {
d6ba96
--- 18247,18258 ----
d6ba96
      char_u	buf[NUMBUFLEN];
d6ba96
      int		err = FALSE;
d6ba96
      FILE	*fd;
d6ba96
+     list_T	*list = NULL;
d6ba96
  
d6ba96
+     rettv->v_type = VAR_STRING;
d6ba96
+     rettv->vval.v_string = NULL;
d6ba96
      if (check_restricted() || check_secure())
d6ba96
! 	goto errret;
d6ba96
  
d6ba96
      if (argvars[1].v_type != VAR_UNKNOWN)
d6ba96
      {
d6ba96
***************
d6ba96
*** 18259,18272 ****
d6ba96
  	if ((infile = vim_tempname('i')) == NULL)
d6ba96
  	{
d6ba96
  	    EMSG(_(e_notmp));
d6ba96
! 	    goto done;
d6ba96
  	}
d6ba96
  
d6ba96
  	fd = mch_fopen((char *)infile, WRITEBIN);
d6ba96
  	if (fd == NULL)
d6ba96
  	{
d6ba96
  	    EMSG2(_(e_notopen), infile);
d6ba96
! 	    goto done;
d6ba96
  	}
d6ba96
  	if (argvars[1].v_type == VAR_LIST)
d6ba96
  	{
d6ba96
--- 18263,18276 ----
d6ba96
  	if ((infile = vim_tempname('i')) == NULL)
d6ba96
  	{
d6ba96
  	    EMSG(_(e_notmp));
d6ba96
! 	    goto errret;
d6ba96
  	}
d6ba96
  
d6ba96
  	fd = mch_fopen((char *)infile, WRITEBIN);
d6ba96
  	if (fd == NULL)
d6ba96
  	{
d6ba96
  	    EMSG2(_(e_notopen), infile);
d6ba96
! 	    goto errret;
d6ba96
  	}
d6ba96
  	if (argvars[1].v_type == VAR_LIST)
d6ba96
  	{
d6ba96
***************
d6ba96
*** 18279,18285 ****
d6ba96
  	    if (p == NULL)
d6ba96
  	    {
d6ba96
  		fclose(fd);
d6ba96
! 		goto done;		/* type error; errmsg already given */
d6ba96
  	    }
d6ba96
  	    if (fwrite(p, STRLEN(p), 1, fd) != 1)
d6ba96
  		err = TRUE;
d6ba96
--- 18283,18289 ----
d6ba96
  	    if (p == NULL)
d6ba96
  	    {
d6ba96
  		fclose(fd);
d6ba96
! 		goto errret;		/* type error; errmsg already given */
d6ba96
  	    }
d6ba96
  	    if (fwrite(p, STRLEN(p), 1, fd) != 1)
d6ba96
  		err = TRUE;
d6ba96
***************
d6ba96
*** 18289,18340 ****
d6ba96
  	if (err)
d6ba96
  	{
d6ba96
  	    EMSG(_("E677: Error writing temp file"));
d6ba96
! 	    goto done;
d6ba96
  	}
d6ba96
      }
d6ba96
  
d6ba96
!     res = get_cmd_output(get_tv_string(&argvars[0]), infile,
d6ba96
! 						 SHELL_SILENT | SHELL_COOKED);
d6ba96
! 
d6ba96
! #ifdef USE_CR
d6ba96
!     /* translate <CR> into <NL> */
d6ba96
!     if (res != NULL)
d6ba96
      {
d6ba96
! 	char_u	*s;
d6ba96
  
d6ba96
! 	for (s = res; *s; ++s)
d6ba96
! 	{
d6ba96
! 	    if (*s == CAR)
d6ba96
! 		*s = NL;
d6ba96
  	}
d6ba96
      }
d6ba96
! #else
d6ba96
! # ifdef USE_CRNL
d6ba96
!     /* translate <CR><NL> into <NL> */
d6ba96
!     if (res != NULL)
d6ba96
      {
d6ba96
! 	char_u	*s, *d;
d6ba96
  
d6ba96
! 	d = res;
d6ba96
! 	for (s = res; *s; ++s)
d6ba96
  	{
d6ba96
! 	    if (s[0] == CAR && s[1] == NL)
d6ba96
! 		++s;
d6ba96
! 	    *d++ = *s;
d6ba96
  	}
d6ba96
- 	*d = NUL;
d6ba96
-     }
d6ba96
  # endif
d6ba96
  #endif
d6ba96
  
d6ba96
! done:
d6ba96
      if (infile != NULL)
d6ba96
      {
d6ba96
  	mch_remove(infile);
d6ba96
  	vim_free(infile);
d6ba96
      }
d6ba96
!     rettv->v_type = VAR_STRING;
d6ba96
!     rettv->vval.v_string = res;
d6ba96
  }
d6ba96
  
d6ba96
  /*
d6ba96
--- 18293,18420 ----
d6ba96
  	if (err)
d6ba96
  	{
d6ba96
  	    EMSG(_("E677: Error writing temp file"));
d6ba96
! 	    goto errret;
d6ba96
  	}
d6ba96
      }
d6ba96
  
d6ba96
!     if (retlist)
d6ba96
      {
d6ba96
! 	int		len;
d6ba96
! 	listitem_T	*li;
d6ba96
! 	char_u		*s = NULL;
d6ba96
! 	char_u		*start;
d6ba96
! 	char_u		*end;
d6ba96
! 	char_u		*p;
d6ba96
! 	int		i;
d6ba96
! 
d6ba96
! 	res = get_cmd_output(get_tv_string(&argvars[0]), infile,
d6ba96
! 					   SHELL_SILENT | SHELL_COOKED, &len;;
d6ba96
! 	if (res == NULL)
d6ba96
! 	    goto errret;
d6ba96
! 
d6ba96
! 	list = list_alloc();
d6ba96
! 	if (list == NULL)
d6ba96
! 	    goto errret;
d6ba96
! 
d6ba96
! 	for (i = 0; i < len; ++i)
d6ba96
! 	{
d6ba96
! 	    start = res + i;
d6ba96
! 	    for (end = start; i < len && *end != NL; ++end)
d6ba96
! 		++i;
d6ba96
  
d6ba96
! 	    s = vim_strnsave(start, (int)(end - start));
d6ba96
! 	    if (s == NULL)
d6ba96
! 		goto errret;
d6ba96
! 
d6ba96
! 	    for (p = s, end = s + (end - start); p < end; ++p)
d6ba96
! 		if (*p == NUL)
d6ba96
! 		    *p = NL;
d6ba96
! 
d6ba96
! 	    li = listitem_alloc();
d6ba96
! 	    if (li == NULL)
d6ba96
! 	    {
d6ba96
! 		vim_free(s);
d6ba96
! 		goto errret;
d6ba96
! 	    }
d6ba96
! 	    li->li_tv.v_type = VAR_STRING;
d6ba96
! 	    li->li_tv.vval.v_string = s;
d6ba96
! 	    list_append(list, li);
d6ba96
  	}
d6ba96
+ 
d6ba96
+ 	rettv->v_type = VAR_LIST;
d6ba96
+ 	rettv->vval.v_list = list;
d6ba96
+ 	list = NULL;
d6ba96
      }
d6ba96
!     else
d6ba96
      {
d6ba96
! 	res = get_cmd_output(get_tv_string(&argvars[0]), infile,
d6ba96
! 					   SHELL_SILENT | SHELL_COOKED, NULL);
d6ba96
! #ifdef USE_CR
d6ba96
! 	/* translate <CR> into <NL> */
d6ba96
! 	if (res != NULL)
d6ba96
! 	{
d6ba96
! 	    char_u	*s;
d6ba96
  
d6ba96
! 	    for (s = res; *s; ++s)
d6ba96
! 	    {
d6ba96
! 		if (*s == CAR)
d6ba96
! 		    *s = NL;
d6ba96
! 	    }
d6ba96
! 	}
d6ba96
! #else
d6ba96
! # ifdef USE_CRNL
d6ba96
! 	/* translate <CR><NL> into <NL> */
d6ba96
! 	if (res != NULL)
d6ba96
  	{
d6ba96
! 	    char_u	*s, *d;
d6ba96
! 
d6ba96
! 	    d = res;
d6ba96
! 	    for (s = res; *s; ++s)
d6ba96
! 	    {
d6ba96
! 		if (s[0] == CAR && s[1] == NL)
d6ba96
! 		    ++s;
d6ba96
! 		*d++ = *s;
d6ba96
! 	    }
d6ba96
! 	    *d = NUL;
d6ba96
  	}
d6ba96
  # endif
d6ba96
  #endif
d6ba96
+ 	rettv->vval.v_string = res;
d6ba96
+ 	res = NULL;
d6ba96
+     }
d6ba96
  
d6ba96
! errret:
d6ba96
      if (infile != NULL)
d6ba96
      {
d6ba96
  	mch_remove(infile);
d6ba96
  	vim_free(infile);
d6ba96
      }
d6ba96
!     if (res != NULL)
d6ba96
! 	vim_free(res);
d6ba96
!     if (list != NULL)
d6ba96
! 	list_free(list, TRUE);
d6ba96
! }
d6ba96
! 
d6ba96
! /*
d6ba96
!  * "system()" function
d6ba96
!  */
d6ba96
!     static void
d6ba96
! f_system(argvars, rettv)
d6ba96
!     typval_T	*argvars;
d6ba96
!     typval_T	*rettv;
d6ba96
! {
d6ba96
!     get_cmd_output_as_rettv(argvars, rettv, FALSE);
d6ba96
! }
d6ba96
! 
d6ba96
! /*
d6ba96
!  * "systemlist()" function
d6ba96
!  */
d6ba96
!     static void
d6ba96
! f_systemlist(argvars, rettv)
d6ba96
!     typval_T	*argvars;
d6ba96
!     typval_T	*rettv;
d6ba96
! {
d6ba96
!     get_cmd_output_as_rettv(argvars, rettv, TRUE);
d6ba96
  }
d6ba96
  
d6ba96
  /*
d6ba96
*** ../vim-7.4.247/src/ex_cmds2.c	2013-11-09 03:31:45.000000000 +0100
d6ba96
--- src/ex_cmds2.c	2014-04-05 19:20:25.023157552 +0200
d6ba96
***************
d6ba96
*** 4341,4347 ****
d6ba96
      /* Find all available locales by running command "locale -a".  If this
d6ba96
       * doesn't work we won't have completion. */
d6ba96
      char_u *locale_a = get_cmd_output((char_u *)"locale -a",
d6ba96
! 							NULL, SHELL_SILENT);
d6ba96
      if (locale_a == NULL)
d6ba96
  	return NULL;
d6ba96
      ga_init2(&locales_ga, sizeof(char_u *), 20);
d6ba96
--- 4341,4347 ----
d6ba96
      /* Find all available locales by running command "locale -a".  If this
d6ba96
       * doesn't work we won't have completion. */
d6ba96
      char_u *locale_a = get_cmd_output((char_u *)"locale -a",
d6ba96
! 						    NULL, SHELL_SILENT, NULL);
d6ba96
      if (locale_a == NULL)
d6ba96
  	return NULL;
d6ba96
      ga_init2(&locales_ga, sizeof(char_u *), 20);
d6ba96
*** ../vim-7.4.247/src/misc1.c	2014-04-01 21:00:45.436733663 +0200
d6ba96
--- src/misc1.c	2014-04-05 19:21:36.603157708 +0200
d6ba96
***************
d6ba96
*** 10665,10671 ****
d6ba96
      else
d6ba96
  #endif
d6ba96
  	buffer = get_cmd_output(cmd, NULL,
d6ba96
! 				      (flags & EW_SILENT) ? SHELL_SILENT : 0);
d6ba96
      vim_free(cmd);
d6ba96
      if (buffer == NULL)
d6ba96
  	return 0;
d6ba96
--- 10665,10671 ----
d6ba96
      else
d6ba96
  #endif
d6ba96
  	buffer = get_cmd_output(cmd, NULL,
d6ba96
! 				(flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
d6ba96
      vim_free(cmd);
d6ba96
      if (buffer == NULL)
d6ba96
  	return 0;
d6ba96
***************
d6ba96
*** 10765,10777 ****
d6ba96
  
d6ba96
  /*
d6ba96
   * Get the stdout of an external command.
d6ba96
   * Returns an allocated string, or NULL for error.
d6ba96
   */
d6ba96
      char_u *
d6ba96
! get_cmd_output(cmd, infile, flags)
d6ba96
      char_u	*cmd;
d6ba96
      char_u	*infile;	/* optional input file name */
d6ba96
      int		flags;		/* can be SHELL_SILENT */
d6ba96
  {
d6ba96
      char_u	*tempname;
d6ba96
      char_u	*command;
d6ba96
--- 10765,10780 ----
d6ba96
  
d6ba96
  /*
d6ba96
   * Get the stdout of an external command.
d6ba96
+  * If "ret_len" is NULL replace NUL characters with NL.  When "ret_len" is not
d6ba96
+  * NULL store the length there.
d6ba96
   * Returns an allocated string, or NULL for error.
d6ba96
   */
d6ba96
      char_u *
d6ba96
! get_cmd_output(cmd, infile, flags, ret_len)
d6ba96
      char_u	*cmd;
d6ba96
      char_u	*infile;	/* optional input file name */
d6ba96
      int		flags;		/* can be SHELL_SILENT */
d6ba96
+     int		*ret_len;
d6ba96
  {
d6ba96
      char_u	*tempname;
d6ba96
      char_u	*command;
d6ba96
***************
d6ba96
*** 10841,10847 ****
d6ba96
  	vim_free(buffer);
d6ba96
  	buffer = NULL;
d6ba96
      }
d6ba96
!     else
d6ba96
      {
d6ba96
  	/* Change NUL into SOH, otherwise the string is truncated. */
d6ba96
  	for (i = 0; i < len; ++i)
d6ba96
--- 10844,10850 ----
d6ba96
  	vim_free(buffer);
d6ba96
  	buffer = NULL;
d6ba96
      }
d6ba96
!     else if (ret_len == NULL)
d6ba96
      {
d6ba96
  	/* Change NUL into SOH, otherwise the string is truncated. */
d6ba96
  	for (i = 0; i < len; ++i)
d6ba96
***************
d6ba96
*** 10850,10855 ****
d6ba96
--- 10853,10860 ----
d6ba96
  
d6ba96
  	buffer[len] = NUL;	/* make sure the buffer is terminated */
d6ba96
      }
d6ba96
+     else
d6ba96
+ 	*ret_len = len;
d6ba96
  
d6ba96
  done:
d6ba96
      vim_free(tempname);
d6ba96
*** ../vim-7.4.247/src/proto/misc1.pro	2013-11-06 04:01:31.000000000 +0100
d6ba96
--- src/proto/misc1.pro	2014-04-05 19:06:26.427155720 +0200
d6ba96
***************
d6ba96
*** 100,106 ****
d6ba96
  void remove_duplicates __ARGS((garray_T *gap));
d6ba96
  int gen_expand_wildcards __ARGS((int num_pat, char_u **pat, int *num_file, char_u ***file, int flags));
d6ba96
  void addfile __ARGS((garray_T *gap, char_u *f, int flags));
d6ba96
! char_u *get_cmd_output __ARGS((char_u *cmd, char_u *infile, int flags));
d6ba96
  void FreeWild __ARGS((int count, char_u **files));
d6ba96
  int goto_im __ARGS((void));
d6ba96
  /* vim: set ft=c : */
d6ba96
--- 100,106 ----
d6ba96
  void remove_duplicates __ARGS((garray_T *gap));
d6ba96
  int gen_expand_wildcards __ARGS((int num_pat, char_u **pat, int *num_file, char_u ***file, int flags));
d6ba96
  void addfile __ARGS((garray_T *gap, char_u *f, int flags));
d6ba96
! char_u *get_cmd_output __ARGS((char_u *cmd, char_u *infile, int flags, int *ret_len));
d6ba96
  void FreeWild __ARGS((int count, char_u **files));
d6ba96
  int goto_im __ARGS((void));
d6ba96
  /* vim: set ft=c : */
d6ba96
*** ../vim-7.4.247/src/version.c	2014-04-05 18:55:40.479154309 +0200
d6ba96
--- src/version.c	2014-04-05 19:07:12.447155821 +0200
d6ba96
***************
d6ba96
*** 736,737 ****
d6ba96
--- 736,739 ----
d6ba96
  {   /* Add new patch number below this line */
d6ba96
+ /**/
d6ba96
+     248,
d6ba96
  /**/
d6ba96
d6ba96
-- 
d6ba96
Tips for aliens in New York: Land anywhere.  Central Park, anywhere.
d6ba96
No one will care or indeed even notice.
d6ba96
		-- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"
d6ba96
d6ba96
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
d6ba96
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
d6ba96
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
d6ba96
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///