<rdar://problem/11328896> Fixing a bug where regex commands were saved in the history even if they came from a 'command sourced' file - this fix introduces a command sourcing depth and disables history for all levels of depth > 0, which means no commands go into history when being sourced from a file. we need an integer depth because command files might themselves source other command files, ...

git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@157727 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/API/SBCommandInterpreter.cpp b/source/API/SBCommandInterpreter.cpp
index 598197d..31ecfc5 100644
--- a/source/API/SBCommandInterpreter.cpp
+++ b/source/API/SBCommandInterpreter.cpp
@@ -94,7 +94,7 @@
         Mutex::Locker api_locker;
         if (target_sp)
             api_locker.Lock(target_sp->GetAPIMutex());
-        m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref());
+        m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
     }
     else
     {
diff --git a/source/Commands/CommandObjectBreakpointCommand.cpp b/source/Commands/CommandObjectBreakpointCommand.cpp
index ae519af..40e9ee9 100644
--- a/source/Commands/CommandObjectBreakpointCommand.cpp
+++ b/source/Commands/CommandObjectBreakpointCommand.cpp
@@ -866,7 +866,8 @@
                                                              stop_on_continue, 
                                                              data->stop_on_error, 
                                                              echo_commands, 
-                                                             print_results, 
+                                                             print_results,
+                                                             eLazyBoolNo,
                                                              result);
             result.GetImmediateOutputStream()->Flush();
             result.GetImmediateErrorStream()->Flush();
diff --git a/source/Commands/CommandObjectCommands.cpp b/source/Commands/CommandObjectCommands.cpp
index c881a6f..7bbf943 100644
--- a/source/Commands/CommandObjectCommands.cpp
+++ b/source/Commands/CommandObjectCommands.cpp
@@ -284,7 +284,8 @@
                                                   m_options.m_stop_on_continue, 
                                                   m_options.m_stop_on_error, 
                                                   echo_commands, 
-                                                  print_results, 
+                                                  print_results,
+                                                  eLazyBoolCalculate,
                                                   result);
         }
         else
@@ -295,6 +296,36 @@
         return result.Succeeded();
 
     }
+    
+    virtual const char*
+    GetRepeatCommand (Args &current_command_args, uint32_t index)
+    {
+        return "";
+    }
+    
+    int
+    HandleArgumentCompletion (Args &input,
+                              int &cursor_index,
+                              int &cursor_char_position,
+                              OptionElementVector &opt_element_vector,
+                              int match_start_point,
+                              int max_return_elements,
+                              bool &word_complete,
+                              StringList &matches)
+    {
+        std::string completion_str (input.GetArgumentAtIndex(cursor_index));
+        completion_str.erase (cursor_char_position);
+        
+        CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 
+                                                             CommandCompletions::eDiskFileCompletion,
+                                                             completion_str.c_str(),
+                                                             match_start_point,
+                                                             max_return_elements,
+                                                             NULL,
+                                                             word_complete,
+                                                             matches);
+        return matches.GetSize();
+    }
 };
 
 OptionDefinition
diff --git a/source/Interpreter/CommandInterpreter.cpp b/source/Interpreter/CommandInterpreter.cpp
index 3e6597b..4f26645 100644
--- a/source/Interpreter/CommandInterpreter.cpp
+++ b/source/Interpreter/CommandInterpreter.cpp
@@ -80,8 +80,8 @@
     m_script_interpreter_ap (),
     m_comment_char ('#'),
     m_repeat_char ('!'),
-    m_batch_command_mode (false),
-    m_truncation_warning(eNoTruncation)
+    m_truncation_warning(eNoTruncation),
+    m_command_source_depth (0)
 {
     const char *dbg_name = debugger.GetInstanceName().AsCString();
     std::string lang_name = ScriptInterpreter::LanguageToString (script_language);
@@ -1218,7 +1218,7 @@
 
 bool
 CommandInterpreter::HandleCommand (const char *command_line, 
-                                   bool add_to_history,
+                                   LazyBool lazy_add_to_history,
                                    CommandReturnObject &result,
                                    ExecutionContext *override_context,
                                    bool repeat_on_empty_command,
@@ -1246,7 +1246,14 @@
     
     if (!no_context_switching)
         UpdateExecutionContext (override_context);
-
+    
+    // <rdar://problem/11328896>
+    bool add_to_history;
+    if (lazy_add_to_history == eLazyBoolCalculate)
+        add_to_history = (m_command_source_depth == 0);
+    else
+        add_to_history = (lazy_add_to_history == eLazyBoolYes);
+    
     bool empty_command = false;
     bool comment_command = false;
     if (command_string.empty())
@@ -2226,7 +2233,7 @@
         bool echo_commands    = false;
         bool print_results    = false;
         
-        HandleCommandsFromFile (init_file, exe_ctx, stop_on_continue, stop_on_error, echo_commands, print_results, result);
+        HandleCommandsFromFile (init_file, exe_ctx, stop_on_continue, stop_on_error, echo_commands, print_results, eLazyBoolNo, result);
     }
     else
     {
@@ -2258,6 +2265,7 @@
                                     bool stop_on_error,
                                     bool echo_commands,
                                     bool print_results,
+                                    LazyBool add_to_history,
                                     CommandReturnObject &result)
 {
     size_t num_lines = commands.GetSize();
@@ -2294,7 +2302,7 @@
         CommandReturnObject tmp_result;
         // If override_context is not NULL, pass no_context_switching = true for
         // HandleCommand() since we updated our context already.
-        bool success = HandleCommand(cmd, false, tmp_result,
+        bool success = HandleCommand(cmd, add_to_history, tmp_result,
                                      NULL, /* override_context */
                                      true, /* repeat_on_empty_command */
                                      override_context != NULL /* no_context_switching */);
@@ -2372,6 +2380,7 @@
                                             bool stop_on_error,
                                             bool echo_command,
                                             bool print_result,
+                                            LazyBool add_to_history,
                                             CommandReturnObject &result)
 {
     if (cmd_file.Exists())
@@ -2385,7 +2394,9 @@
             result.SetStatus (eReturnStatusFailed);
             return;
         }
-        HandleCommands (commands, context, stop_on_continue, stop_on_error, echo_command, print_result, result);
+        m_command_source_depth++;
+        HandleCommands (commands, context, stop_on_continue, stop_on_error, echo_command, print_result, add_to_history, result);
+        m_command_source_depth--;
     }
     else
     {
diff --git a/source/Interpreter/CommandObjectRegexCommand.cpp b/source/Interpreter/CommandObjectRegexCommand.cpp
index 7f2e8c0..871922c 100644
--- a/source/Interpreter/CommandObjectRegexCommand.cpp
+++ b/source/Interpreter/CommandObjectRegexCommand.cpp
@@ -88,7 +88,7 @@
                 }
                 // Interpret the new command and return this as the result!
                 result.GetOutputStream().Printf("%s\n", new_command.c_str());
-                return m_interpreter.HandleCommand(new_command.c_str(), true, result);
+                return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result);
             }
         }
         result.SetStatus(eReturnStatusFailed);
diff --git a/source/Target/Target.cpp b/source/Target/Target.cpp
index 25d18aa..7f8ae45 100644
--- a/source/Target/Target.cpp
+++ b/source/Target/Target.cpp
@@ -2001,7 +2001,8 @@
                                                                       stop_on_continue, 
                                                                       stop_on_error, 
                                                                       echo_commands,
-                                                                      print_results, 
+                                                                      print_results,
+                                                                      eLazyBoolNo,
                                                                       result);
 
                 // If the command started the target going again, we should bag out of