changeset 8773:9e3111d203c0

disallow reading from stdin while running interactively
author John W. Eaton <jwe@octave.org>
date Tue, 17 Feb 2009 01:33:10 -0500
parents aeedc045cfe3
children b756ce0002db
files src/ChangeLog src/file-io.cc src/oct-stream.cc
diffstat 3 files changed, 45 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,14 @@
 2009-02-17  John W. Eaton  <jwe@octave.org>
 
+	* file-io.cc (Ffscanf, Fsscanf): Check error_state after call to
+	octave_stream::scanf.
+	(Ffgetl): Check error state after call to octave_stream::getl.
+	(Ffgets): Check error state after call to octave_stream::gets.
+
+	* oct-stream.cc (octave_base_stream::do_gets,
+	octave_base_stream::do_scanf):
+	Disallow reading from stdin when running interactively.
+
 	* toplev.cc (octave_config_info): Add CARBON_LIBS, X11_INCFLAGS,
 	and X11_LIBS to the struct.
 	* oct-conf.h.in (OCTAVE_CONF_CARBON_LIBS,
--- a/src/file-io.cc
+++ b/src/file-io.cc
@@ -335,7 +335,7 @@
 
 	  std::string tmp = os.getl (len_arg, err, who);
 
-	  if (! err)
+	  if (! (error_state || err))
 	    {
 	      retval(1) = tmp.length ();
 	      retval(0) = tmp;
@@ -383,7 +383,7 @@
 
 	  std::string tmp = os.gets (len_arg, err, who);
 
-	  if (! err)
+	  if (! (error_state || err))
 	    {
 	      retval(1) = tmp.length ();
 	      retval(0) = tmp;
@@ -1118,8 +1118,11 @@
 		    {
 		      octave_value tmp = os.scanf (args(1), size, count, who);
 
-		      retval(1) = count;
-		      retval(0) = tmp;
+		      if (! error_state)
+			{
+			  retval(1) = count;
+			  retval(0) = tmp;
+			}
 		    }
 		}
 	      else
@@ -1198,15 +1201,18 @@
 
 		      octave_value tmp = os.scanf (args(1), size, count, who);
 
-		      // FIXME -- is this the right thing to do?
-		      // Extract error message first, because getting
-		      // position will clear it.
-		      std::string errmsg = os.error ();
+		      if (! error_state)
+			{
+			  // FIXME -- is this the right thing to do?
+			  // Extract error message first, because getting
+			  // position will clear it.
+			  std::string errmsg = os.error ();
 
-		      retval(3) = os.tell () + 1;
-		      retval(2) = errmsg;
-		      retval(1) = count;
-		      retval(0) = tmp;
+			  retval(3) = os.tell () + 1;
+			  retval(2) = errmsg;
+			  retval(1) = count;
+			  retval(0) = tmp;
+			}
 		    }
 		  else
 		    ::error ("%s: format must be a string", who.c_str ());
--- a/src/oct-stream.cc
+++ b/src/oct-stream.cc
@@ -964,6 +964,14 @@
 {
   std::string retval;
 
+  if ((interactive || forced_interactive) && file_number () == 0)
+    {
+      ::error ("%s: unable to read from stdin while running interactively",
+	       who.c_str ());
+	     
+      return retval;
+    }
+
   err = false;
 
   std::istream *isp = input_stream ();
@@ -1651,14 +1659,22 @@
 			      octave_idx_type nr, octave_idx_type nc, bool one_elt_size_spec,
 			      octave_idx_type& conversion_count, const std::string& who)
 {
+  octave_value retval = Matrix ();
+
+  if ((interactive || forced_interactive) && file_number () == 0)
+    {
+      ::error ("%s: unable to read from stdin while running interactively",
+	       who.c_str ());
+	     
+      return retval;
+    }
+
   conversion_count = 0;
 
   int nconv = fmt_list.num_conversions ();
 
   octave_idx_type data_index = 0;
 
-  octave_value retval = Matrix ();
-
   if (nr == 0 || nc == 0)
     {
       if (one_elt_size_spec)