changeset 11123:fbec4b3be59f

toplev.cc (Fsystem): allow optional return_output and type arguments to be specified independently
author John W. Eaton <jwe@octave.org>
date Wed, 20 Oct 2010 22:15:34 -0400
parents 7abc783e202c
children e79f59d31a74
files src/ChangeLog src/toplev.cc
diffstat 2 files changed, 55 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Oct 20 21:03:13 2010 -0400
+++ b/src/ChangeLog	Wed Oct 20 22:15:34 2010 -0400
@@ -1,3 +1,8 @@
+2010-10-20  John W. Eaton  <jwe@octave.org>
+
+	* toplev.cc (Fsystem): Allow optional RETURN_OUTPUT and TYPE
+	arguments to be specified independently.  Update doc string.
+
 2010-10-18  John W. Eaton  <jwe@octave.org>
 
 	* load-path.cc (load_path::do_find_first_of,
--- a/src/toplev.cc	Wed Oct 20 21:03:13 2010 -0400
+++ b/src/toplev.cc	Wed Oct 20 22:15:34 2010 -0400
@@ -825,21 +825,20 @@
 
 DEFUN (system, args, nargout,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\
-Execute a shell command specified by @var{string}.  The second\n\
-argument is optional.  If @var{type} is @code{\"async\"}, the process\n\
+@deftypefn {Built-in Function} {[@var{status}, @var{output}]} system (@var{string}, @var{return_output}, @var{type})\n\
+Execute a shell command specified by @var{string}.\n\
+If the optional argument @var{type} is @code{\"async\"}, the process\n\
 is started in the background and the process id of the child process\n\
 is returned immediately.  Otherwise, the process is started, and\n\
 Octave waits until it exits.  If the @var{type} argument is omitted, a\n\
 value of @code{\"sync\"} is assumed.\n\
 \n\
-If two input arguments are given (the actual value of\n\
-@var{return_output} is irrelevant) and the subprocess is started\n\
-synchronously, or if @var{system} is called with one input argument and\n\
-one or more output arguments, the output from the command is returned.\n\
-Otherwise, if the subprocess is executed synchronously, its output is\n\
-sent to the standard output.  To send the output of a command executed\n\
-with @var{system} through the pager, use a command like\n\
+If the optional argument @var{return_output} is true and the subprocess\n\
+is started synchronously, or if @var{system} is called with one input\n\
+argument and one or more output arguments, the output from the command\n\
+is returned.  Otherwise, if the subprocess is executed synchronously, its\n\
+output is sent to the standard output.  To send the output of a command\n\
+executed with @code{system} through the pager, use a command like\n\
 \n\
 @example\n\
 disp (system (cmd, 1));\n\
@@ -863,6 +862,9 @@
 @noindent\n\
 will set the variable @code{output} to the string @samp{foo}, and the\n\
 variable @code{status} to the integer @samp{2}.\n\
+\n\
+For commands run asynchronously, @var{status} is the process id of the\n\
+command shell that is started to run the command.\n\
 @end deftypefn")
 {
   octave_value_list retval;
@@ -873,33 +875,51 @@
 
   if (nargin > 0 && nargin < 4)
     {
-      bool return_output = (nargout > 1 || nargin > 1);
-
-      std::string cmd_str = args(0).string_value ();
+      bool return_output = (nargin == 1 && nargout > 1);
 
       system_exec_type type = et_sync;
 
-      if (! error_state)
+      if (nargin == 3)
         {
-          if (nargin > 2)
+          std::string type_str = args(2).string_value ();
+
+          if (! error_state)
             {
-              std::string type_str = args(2).string_value ();
-
-              if (! error_state)
+              if (type_str == "sync")
+                type = et_sync;
+              else if (type_str == "async")
+                type = et_async;
+              else
                 {
-                  if (type_str == "sync")
-                    type = et_sync;
-                  else if (type_str == "async")
-                    type = et_async;
-                  else
-                    error ("system: third arg must be \"sync\" or \"async\"");
+                  error ("system: third arg must be \"sync\" or \"async\"");
+                  return retval;
                 }
-              else
-                error ("system: third argument must be a string");
+            }
+          else
+            {
+              error ("system: third argument must be a character string");
+              return retval;
             }
         }
-      else
-        error ("system: expecting std::string as first argument");
+
+      if (nargin > 1)
+        {
+          return_output = args(1).is_true ();
+
+          if (error_state)
+            {
+              error ("system: expecting second argument to be true or false");
+              return retval;
+            }
+        }
+
+      if (return_output && type == et_async)
+        {
+          error ("system: can't return output from commands run asynchronously");
+          return retval;
+        }
+
+      std::string cmd_str = args(0).string_value ();
 
       if (! error_state)
         {
@@ -966,6 +986,8 @@
               retval(0) = status;
             }
         }
+      else
+        error ("system: expecting std::string as first argument");
     }
   else
     print_usage ();