Add a new function to Debugger for finding the top/current 
input reader.

Always make sure the input reader stack is not empty before
trying to get the top element from the stack.

llvm-svn: 125255
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 10bb267..f699489 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -347,6 +347,23 @@
     return m_target_list;
 }
 
+InputReaderSP 
+Debugger::GetCurrentInputReader ()
+{
+    InputReaderSP reader_sp;
+    
+    if (!m_input_readers.empty())
+    {
+        // Clear any finished readers from the stack
+        while (CheckIfTopInputReaderIsDone()) ;
+        
+        if (!m_input_readers.empty())
+            reader_sp = m_input_readers.top();
+    }
+    
+    return reader_sp;
+}
+
 void
 Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
 {
@@ -371,14 +388,12 @@
 {
     m_input_reader_data.clear();
     
-    if (!m_input_readers.empty())
+    InputReaderSP reader_sp (GetCurrentInputReader ());
+    if (reader_sp)
     {
-        while (CheckIfTopInputReaderIsDone ()) ;
+        reader_sp->Notify (eInputReaderInterrupt);
         
-        InputReaderSP reader_sp(m_input_readers.top());
-        if (reader_sp)
-            reader_sp->Notify (eInputReaderInterrupt);
-
+        // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
         while (CheckIfTopInputReaderIsDone ()) ;
     }
 }
@@ -388,14 +403,12 @@
 {
     m_input_reader_data.clear();
     
-    if (!m_input_readers.empty())
+    InputReaderSP reader_sp (GetCurrentInputReader ());
+    if (reader_sp)
     {
-        while (CheckIfTopInputReaderIsDone ()) ;
+        reader_sp->Notify (eInputReaderEndOfFile);
         
-        InputReaderSP reader_sp(m_input_readers.top());
-        if (reader_sp)
-            reader_sp->Notify (eInputReaderEndOfFile);
-
+        // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack.
         while (CheckIfTopInputReaderIsDone ()) ;
     }
 }
@@ -405,11 +418,10 @@
 {
     m_input_reader_data.clear();
     
+    // The bottom input reader should be the main debugger input reader.  We do not want to close that one here.
     while (m_input_readers.size() > 1)
     {
-        while (CheckIfTopInputReaderIsDone ()) ;
-        
-        InputReaderSP reader_sp (m_input_readers.top());
+        InputReaderSP reader_sp (GetCurrentInputReader ());
         if (reader_sp)
         {
             reader_sp->Notify (eInputReaderEndOfFile);
@@ -429,12 +441,8 @@
 
     while (!m_input_readers.empty() && !m_input_reader_data.empty())
     {
-        while (CheckIfTopInputReaderIsDone ())
-            /* Do nothing. */;
-        
         // Get the input reader from the top of the stack
-        InputReaderSP reader_sp(m_input_readers.top());
-        
+        InputReaderSP reader_sp (GetCurrentInputReader ());
         if (!reader_sp)
             break;
 
@@ -452,7 +460,7 @@
         }
     }
     
-    // Flush out any input readers that are donesvn
+    // Flush out any input readers that are done.
     while (CheckIfTopInputReaderIsDone ())
         /* Do nothing. */;
 
@@ -463,13 +471,13 @@
 {
     if (!reader_sp)
         return;
-    if (!m_input_readers.empty())
-    {
-        // Deactivate the old top reader
-        InputReaderSP top_reader_sp (m_input_readers.top());
-        if (top_reader_sp)
-            top_reader_sp->Notify (eInputReaderDeactivate);
-    }
+ 
+    // Deactivate the old top reader
+    InputReaderSP top_reader_sp (GetCurrentInputReader ());
+    
+    if (top_reader_sp)
+        top_reader_sp->Notify (eInputReaderDeactivate);
+
     m_input_readers.push (reader_sp);
     reader_sp->Notify (eInputReaderActivate);
     ActivateInputReader (reader_sp);
@@ -484,6 +492,7 @@
     // read on the stack referesh its prompt and if there is one...
     if (!m_input_readers.empty())
     {
+        // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
         InputReaderSP reader_sp(m_input_readers.top());
         
         if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
@@ -513,6 +522,7 @@
     bool result = false;
     if (!m_input_readers.empty())
     {
+        // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
         InputReaderSP reader_sp(m_input_readers.top());
         
         if (reader_sp && reader_sp->IsDone())