changeset 16097:2f4fa62089b3

improve end of file handling for lexer input * input.h, input.cc, oct-parse.yy (input_from_eval_string_pending): Delete variable and all uses. (gnu_readline, interactive_input, octave_gets, get_user_input): New argument, eof. (octave_gets): Pass eof to gnu_readline and interactive_input. (get_user_input): Simplify handling of eval string input. Pass eof to octave_gets. (octave_read): New static variable, eof. Pass eof to get_user_input. Simplify end of file handling. Return error condition if eof is not true when there are no more characters to read.
author John W. Eaton <jwe@octave.org>
date Mon, 25 Feb 2013 00:45:35 -0500
parents 9720ecf8d257
children 24b3800d30e7
files libinterp/interpfcn/input.cc libinterp/interpfcn/input.h libinterp/parse-tree/oct-parse.yy
diffstat 3 files changed, 41 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/interpfcn/input.cc	Sun Feb 24 20:38:33 2013 -0800
+++ b/libinterp/interpfcn/input.cc	Mon Feb 25 00:45:35 2013 -0500
@@ -100,10 +100,6 @@
 // TRUE means get input from current_eval_string.
 bool get_input_from_eval_string = false;
 
-// TRUE means we haven't been asked for the input from
-// current_eval_string yet.
-bool input_from_eval_string_pending = false;
-
 // TRUE means that input is coming from a file that was named on
 // the command line.
 bool input_from_command_line_file = false;
@@ -191,16 +187,16 @@
 }
 
 std::string
-gnu_readline (const std::string& s, bool force_readline)
+gnu_readline (const std::string& s, bool& eof, bool force_readline)
 {
   octave_quit ();
 
+  eof = false;
+
   std::string retval;
 
   if (line_editing || force_readline)
     {
-      bool eof;
-
       retval = command_editor::readline (s, eof);
 
       if (! eof && retval.empty ())
@@ -221,14 +217,22 @@
       if (reading_fcn_file || reading_script_file || reading_classdef_file)
         curr_stream = ff_instream;
 
-      retval = octave_fgets (curr_stream);
+      retval = octave_fgets (curr_stream, eof);
     }
 
   return retval;
 }
 
+extern std::string
+gnu_readline (const std::string& s, bool force_readline)
+{
+  bool eof = false;
+
+  return gnu_readline (s, eof, force_readline);
+}
+
 static inline std::string
-interactive_input (const std::string& s, bool force_readline = false)
+interactive_input (const std::string& s, bool& eof, bool force_readline)
 {
   Vlast_prompt_time.stamp ();
 
@@ -247,14 +251,24 @@
         return "\n";
     }
 
-  return gnu_readline (s, force_readline);
+  return gnu_readline (s, eof, force_readline);
+}
+
+static inline std::string
+interactive_input (const std::string& s, bool force_readline = false)
+{
+  bool eof = false;
+
+  return interactive_input (s, eof, force_readline);
 }
 
 static std::string
-octave_gets (void)
+octave_gets (bool& eof)
 {
   octave_quit ();
 
+  eof = false;
+
   std::string retval;
 
   bool history_skip_auto_repeated_debugging_command = false;
@@ -280,7 +294,7 @@
 
       octave_diary << prompt;
 
-      retval = interactive_input (prompt);
+      retval = interactive_input (prompt, eof, false);
 
       // There is no need to update the load_path cache if there is no
       // user input.
@@ -301,7 +315,7 @@
         }
     }
   else
-    retval = gnu_readline ("");
+    retval = gnu_readline ("", eof, false);
 
   current_input_line = retval;
 
@@ -330,28 +344,28 @@
 // Read a line from the input stream.
 
 static std::string
-get_user_input (void)
+get_user_input (bool& eof)
 {
   octave_quit ();
 
+  eof = false;
+
   std::string retval;
 
   if (get_input_from_eval_string)
     {
-      if (input_from_eval_string_pending)
-        {
-          input_from_eval_string_pending = false;
+      retval = current_eval_string;
 
-          retval = current_eval_string;
+      size_t len = retval.length ();
 
-          size_t len = retval.length ();
+      // Clear the global eval string so that the next call will return
+      // an empty character string with EOF = true.
+      current_eval_string = "";
 
-          if (len > 0 && retval[len-1] != '\n')
-            retval.append ("\n");
-        }
+      eof = true;
     }
   else
-    retval = octave_gets ();
+    retval = octave_gets (eof);
 
   current_input_line = retval;
 
@@ -367,13 +381,15 @@
   static std::string input_buf;
   static const char *pos = 0;
   static size_t chars_left = 0;
+  static bool eof = false;
 
   int status = 0;
+
   if (chars_left == 0)
     {
       pos = 0;
 
-      input_buf = get_user_input ();
+      input_buf = get_user_input (eof);
 
       chars_left = input_buf.length ();
 
@@ -410,14 +426,9 @@
         }
 
       status = len;
-
-    }
-  else if (chars_left == 0)
-    {
-      status = 0;
     }
   else
-    status = -1;
+    status = eof ? 0 : -1;
 
   return status;
 }
--- a/libinterp/interpfcn/input.h	Sun Feb 24 20:38:33 2013 -0800
+++ b/libinterp/interpfcn/input.h	Mon Feb 25 00:45:35 2013 -0500
@@ -45,10 +45,6 @@
 // TRUE means get input from current_eval_string.
 extern bool get_input_from_eval_string;
 
-// TRUE means we haven't been asked for the input from
-// current_eval_string yet.
-extern bool input_from_eval_string_pending;
-
 // TRUE means that input is coming from a file that was named on
 // the command line.
 extern bool input_from_command_line_file;
--- a/libinterp/parse-tree/oct-parse.yy	Sun Feb 24 20:38:33 2013 -0800
+++ b/libinterp/parse-tree/oct-parse.yy	Mon Feb 25 00:45:35 2013 -0500
@@ -4315,7 +4315,6 @@
   frame.protect_var (input_line_number);
   frame.protect_var (current_input_column);
   frame.protect_var (get_input_from_eval_string);
-  frame.protect_var (input_from_eval_string_pending);
   frame.protect_var (parser_end_of_input);
   frame.protect_var (line_editing);
   frame.protect_var (current_eval_string);
@@ -4331,7 +4330,6 @@
   input_line_number = 1;
   current_input_column = 1;
   get_input_from_eval_string = true;
-  input_from_eval_string_pending = true;
   parser_end_of_input = false;
   line_editing = false;
   current_function_depth = 0;