<rdar://problem/14134716>

This is a rewrite of the command history facility of LLDB

It takes the history management out of the CommandInterpreter into its own CommandHistory class
It reimplements the command history command to allow more combinations of options to work correctly (e.g. com hist -c 1 -s 5)
It adds a new --wipe (-w) option to command history to allow clearing the history on demand
It extends the lldbtest runCmd: and expect: methods to allow adding commands to history if need be
It adds a test case for the reimplemented facility

llvm-svn: 184140
diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp
index 737a449..3e46041 100644
--- a/lldb/source/Commands/CommandObjectCommands.cpp
+++ b/lldb/source/Commands/CommandObjectCommands.cpp
@@ -22,10 +22,12 @@
 #include "lldb/Core/InputReaderEZ.h"
 #include "lldb/Core/StringList.h"
 #include "lldb/Interpreter/Args.h"
+#include "lldb/Interpreter/CommandHistory.h"
 #include "lldb/Interpreter/CommandInterpreter.h"
 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
 #include "lldb/Interpreter/CommandReturnObject.h"
 #include "lldb/Interpreter/OptionValueBoolean.h"
+#include "lldb/Interpreter/OptionValueUInt64.h"
 #include "lldb/Interpreter/Options.h"
 #include "lldb/Interpreter/ScriptInterpreter.h"
 #include "lldb/Interpreter/ScriptInterpreterPython.h"
@@ -64,7 +66,11 @@
     public:
 
         CommandOptions (CommandInterpreter &interpreter) :
-            Options (interpreter)
+            Options (interpreter),
+            m_start_idx(0),
+            m_stop_idx(0),
+            m_count(0),
+            m_wipe(false)
         {
         }
 
@@ -76,27 +82,27 @@
         {
             Error error;
             const int short_option = m_getopt_table[option_idx].val;
-            bool success;
             
             switch (short_option)
             {
                 case 'c':
-                    m_end_idx = Args::StringToUInt32(option_arg, UINT_MAX, 0, &success);
-                    if (!success)
-                        error.SetErrorStringWithFormat("invalid value for count: %s", option_arg);
-                    if (m_end_idx != 0)
-                        m_end_idx--;
-                    m_start_idx = 0;
-                    break;
-                case 'e':
-                    m_end_idx = Args::StringToUInt32(option_arg, 0, 0, &success);
-                    if (!success)
-                        error.SetErrorStringWithFormat("invalid value for end index: %s", option_arg);
+                    error = m_count.SetValueFromCString(option_arg,eVarSetOperationAssign);
                     break;
                 case 's':
-                    m_start_idx = Args::StringToUInt32(option_arg, 0, 0, &success);
-                    if (!success)
-                        error.SetErrorStringWithFormat("invalid value for start index: %s", option_arg);
+                    if (option_arg && strcmp("end", option_arg) == 0)
+                    {
+                        m_start_idx.SetCurrentValue(UINT64_MAX);
+                        m_start_idx.SetOptionWasSet();
+                    }
+                    else
+                        error = m_start_idx.SetValueFromCString(option_arg,eVarSetOperationAssign);
+                    break;
+                case 'e':
+                    error = m_stop_idx.SetValueFromCString(option_arg,eVarSetOperationAssign);
+                    break;
+                case 'w':
+                    m_wipe.SetCurrentValue(true);
+                    m_wipe.SetOptionWasSet();
                     break;
                 default:
                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
@@ -109,8 +115,10 @@
         void
         OptionParsingStarting ()
         {
-            m_start_idx = 0;
-            m_end_idx = UINT_MAX;
+            m_start_idx.Clear();
+            m_stop_idx.Clear();
+            m_count.Clear();
+            m_wipe.Clear();
         }
 
         const OptionDefinition*
@@ -125,17 +133,90 @@
 
         // Instance variables to hold the values for command options.
 
-        uint32_t m_start_idx;
-        uint32_t m_end_idx;
+        OptionValueUInt64 m_start_idx;
+        OptionValueUInt64 m_stop_idx;
+        OptionValueUInt64 m_count;
+        OptionValueBoolean m_wipe;
     };
     
     bool
     DoExecute (Args& command, CommandReturnObject &result)
     {
-        
-        m_interpreter.DumpHistory (result.GetOutputStream(),
-                                   m_options.m_start_idx, 
-                                   m_options.m_end_idx);
+        if (m_options.m_wipe.GetCurrentValue() && m_options.m_wipe.OptionWasSet())
+        {
+            m_interpreter.GetCommandHistory().Clear();
+            result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
+        }
+        else
+        {
+            if (m_options.m_start_idx.OptionWasSet() && m_options.m_stop_idx.OptionWasSet() && m_options.m_count.OptionWasSet())
+            {
+                result.AppendError("--count, --start-index and --end-index cannot be all specified in the same invocation");
+                result.SetStatus(lldb::eReturnStatusFailed);
+            }
+            else
+            {
+                std::pair<bool,uint64_t> start_idx = {m_options.m_start_idx.OptionWasSet(),m_options.m_start_idx.GetCurrentValue()};
+                std::pair<bool,uint64_t> stop_idx = {m_options.m_stop_idx.OptionWasSet(),m_options.m_stop_idx.GetCurrentValue()};
+                std::pair<bool,uint64_t> count = {m_options.m_count.OptionWasSet(),m_options.m_count.GetCurrentValue()};
+                
+                const CommandHistory& history(m_interpreter.GetCommandHistory());
+                                              
+                if (start_idx.first && start_idx.second == UINT64_MAX)
+                {
+                    if (count.first)
+                    {
+                        start_idx.second = history.GetSize() - count.second;
+                        stop_idx.second = history.GetSize() - 1;
+                    }
+                    else if (stop_idx.first)
+                    {
+                        start_idx.second = stop_idx.second;
+                        stop_idx.second = history.GetSize() - 1;
+                    }
+                    else
+                    {
+                        start_idx.second = 0;
+                        stop_idx.second = history.GetSize() - 1;
+                    }
+                }
+                else
+                {
+                    if (!start_idx.first && !stop_idx.first && !count.first)
+                    {
+                        start_idx.second = 0;
+                        stop_idx.second = history.GetSize() - 1;
+                    }
+                    else if (start_idx.first)
+                    {
+                        if (count.first)
+                        {
+                            stop_idx.second = start_idx.second + count.second - 1;
+                        }
+                        else if (!stop_idx.first)
+                        {
+                            stop_idx.second = history.GetSize() - 1;
+                        }
+                    }
+                    else if (stop_idx.first)
+                    {
+                        if (count.first)
+                        {
+                            if (stop_idx.second >= count.second)
+                                start_idx.second = stop_idx.second - count.second + 1;
+                            else
+                                start_idx.second = 0;
+                        }
+                    }
+                    else /* if (count.first) */
+                    {
+                        start_idx.second = 0;
+                        stop_idx.second = count.second - 1;
+                    }
+                }
+                history.Dump(result.GetOutputStream(), start_idx.second, stop_idx.second);
+            }
+        }
         return result.Succeeded();
 
     }
@@ -147,8 +228,9 @@
 CommandObjectCommandsHistory::CommandOptions::g_option_table[] =
 {
 { LLDB_OPT_SET_1, false, "count", 'c', required_argument, NULL, 0, eArgTypeUnsignedInteger,        "How many history commands to print."},
-{ LLDB_OPT_SET_1, false, "start-index", 's', required_argument, NULL, 0, eArgTypeUnsignedInteger,  "Index at which to start printing history commands."},
+{ LLDB_OPT_SET_1, false, "start-index", 's', required_argument, NULL, 0, eArgTypeUnsignedInteger,  "Index at which to start printing history commands (or end to mean tail mode)."},
 { LLDB_OPT_SET_1, false, "end-index", 'e', required_argument, NULL, 0, eArgTypeUnsignedInteger,    "Index at which to stop printing history commands."},
+{ LLDB_OPT_SET_2, false, "wipe", 'w', no_argument, NULL, 0, eArgTypeBoolean,    "Clears the current command history."},
 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
 };