[Lldb-commits] [lldb] r143230 - in /lldb/trunk: include/lldb/Interpreter/CommandInterpreter.h source/Interpreter/CommandInterpreter.cpp

Greg Clayton gclayton at apple.com
Fri Oct 28 14:38:01 PDT 2011


Author: gclayton
Date: Fri Oct 28 16:38:01 2011
New Revision: 143230

URL: http://llvm.org/viewvc/llvm-project?rev=143230&view=rev
Log:
Added the ability to have GDB formats appended to any command so you can do
things like:

(lldb) x/32xb 0x1000

"x" is an alias to "memory read", so this will actually turn into:

(lldb) memory read --gdb-format=32xb 0x1000

This applies to all commands, so the GDB formats will work with "register read",
"frame variable", "target variable" and others. All commands that can accept
formats, counts and sizes have been modified to support the "--gdb-format"
option.



Modified:
    lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
    lldb/trunk/source/Interpreter/CommandInterpreter.cpp

Modified: lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h?rev=143230&r1=143229&r2=143230&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h Fri Oct 28 16:38:01 2011
@@ -129,11 +129,10 @@
     AddOrReplaceAliasOptions (const char *alias_name, 
                               OptionArgVectorSP &option_arg_vector_sp);
 
-    void
+    CommandObject *
     BuildAliasResult (const char *alias_name, 
                       std::string &raw_input_string, 
                       std::string &alias_result, 
-                      CommandObject *&alias_cmd_obj, 
                       CommandReturnObject &result);
 
     bool

Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=143230&r1=143229&r2=143230&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Fri Oct 28 16:38:01 2011
@@ -644,7 +644,7 @@
         }
     }
     
-    if (options_string.size() > 0)
+    if (!options_string.empty())
     {
         if (cmd_obj_sp->WantsRawCommandString ())
             option_arg_vector->push_back (OptionArgPair ("<argument>",
@@ -779,7 +779,7 @@
 
     }
 
-    if (m_alias_dict.size() > 0 && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
+    if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
     {
         result.AppendMessage("The following is a list of your current command abbreviations "
                              "(see 'help command alias' for more info):");
@@ -801,7 +801,7 @@
         result.AppendMessage("");
     }
 
-    if (m_user_dict.size() > 0 && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
+    if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
     {
         result.AppendMessage ("The following is a list of your current user-defined commands:");
         result.AppendMessage("");
@@ -879,7 +879,7 @@
 }
 
 static const char *k_white_space = " \t\v";
-
+static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
 static void
 StripLeadingSpaces (std::string &s)
 {
@@ -895,9 +895,10 @@
 }
 
 static bool
-StripFirstWord (std::string &command_string, std::string &word, char &quote_char)
+ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
 {
-    word.clear();
+    command.clear();
+    suffix.clear();
     StripLeadingSpaces (command_string);
 
     bool result = false;
@@ -912,12 +913,12 @@
             const size_t end_quote_pos = command_string.find (quote_char, 1);
             if (end_quote_pos == std::string::npos)
             {
-                word.swap (command_string);
+                command.swap (command_string);
                 command_string.erase ();
             }
             else
             {
-                word = command_string.substr (1, end_quote_pos - 1);
+                command.assign (command_string, 1, end_quote_pos - 1);
                 if (end_quote_pos + 1 < command_string.size())
                     command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
                 else
@@ -929,25 +930,43 @@
             const size_t first_space_pos = command_string.find_first_of (k_white_space);
             if (first_space_pos == std::string::npos)
             {
-                word.swap (command_string);
+                command.swap (command_string);
                 command_string.erase();
             }
             else
             {
-                word = command_string.substr (0, first_space_pos);
-                command_string.erase(0,command_string.find_first_not_of (k_white_space, first_space_pos));
+                command.assign (command_string, 0, first_space_pos);
+                command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
             }
         }
         result = true;
     }
+    
+    
+    if (!command.empty())
+    {
+        // actual commands can't start with '-' or '_'
+        if (command[0] != '-' && command[0] != '_')
+        {
+            size_t pos = command.find_first_not_of(k_valid_command_chars);
+            if (pos > 0 && pos != std::string::npos)
+            {
+                suffix.assign (command.begin() + pos, command.end());
+                command.erase (pos);
+            }
+        }
+    }
 
     return result;
 }
 
-void
-CommandInterpreter::BuildAliasResult (const char *alias_name, std::string &raw_input_string, std::string &alias_result,
-                                      CommandObject *&alias_cmd_obj, CommandReturnObject &result)
+CommandObject *
+CommandInterpreter::BuildAliasResult (const char *alias_name, 
+                                      std::string &raw_input_string, 
+                                      std::string &alias_result,
+                                      CommandReturnObject &result)
 {
+    CommandObject *alias_cmd_obj = NULL;
     Args cmd_args (raw_input_string.c_str());
     alias_cmd_obj = GetCommandObject (alias_name);
     StreamString result_str;
@@ -992,7 +1011,7 @@
                             ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
                              index);
                             result.SetStatus (eReturnStatusFailed);
-                            return;
+                            return alias_cmd_obj;
                         }
                         else
                         {
@@ -1009,6 +1028,7 @@
         
         alias_result = result_str.GetData();
     }
+    return alias_cmd_obj;
 }
 
 Error
@@ -1135,7 +1155,6 @@
 
     bool done = false;
     CommandObject *cmd_obj = NULL;
-    std::string next_word;
     bool wants_raw_input = false;
     std::string command_string (command_line);
     std::string original_command_string (command_line);
@@ -1242,62 +1261,70 @@
 
     StreamString revised_command_line;
     size_t actual_cmd_name_len = 0;
+    std::string next_word;
     while (!done)
     {
         char quote_char = '\0';
-        StripFirstWord (command_string, next_word, quote_char);
-        if (!cmd_obj && AliasExists (next_word.c_str())) 
-        {
-            std::string alias_result;
-            BuildAliasResult (next_word.c_str(), command_string, alias_result, cmd_obj, result);
-            revised_command_line.Printf ("%s", alias_result.c_str());
-            if (cmd_obj)
-            {
-                wants_raw_input = cmd_obj->WantsRawCommandString ();
-                actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
-            }
-        }
-        else if (!cmd_obj)
+        std::string suffix;
+        ExtractCommand (command_string, next_word, suffix, quote_char);
+        if (cmd_obj == NULL)
         {
-            cmd_obj = GetCommandObject (next_word.c_str());
-            if (cmd_obj)
+            if (AliasExists (next_word.c_str())) 
             {
-                actual_cmd_name_len += next_word.length();
-                revised_command_line.Printf ("%s", next_word.c_str());
-                wants_raw_input = cmd_obj->WantsRawCommandString ();
+                std::string alias_result;
+                cmd_obj = BuildAliasResult (next_word.c_str(), command_string, alias_result, result);
+                revised_command_line.Printf ("%s", alias_result.c_str());
+                if (cmd_obj)
+                {
+                    wants_raw_input = cmd_obj->WantsRawCommandString ();
+                    actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
+                }
             }
             else
             {
-                revised_command_line.Printf ("%s", next_word.c_str());
+                cmd_obj = GetCommandObject (next_word.c_str());
+                if (cmd_obj)
+                {
+                    actual_cmd_name_len += next_word.length();
+                    revised_command_line.Printf ("%s", next_word.c_str());
+                    wants_raw_input = cmd_obj->WantsRawCommandString ();
+                }
+                else
+                {
+                    revised_command_line.Printf ("%s", next_word.c_str());
+                }
             }
         }
-        else if (cmd_obj->IsMultiwordObject ())
+        else
         {
-            CommandObject *sub_cmd_obj = ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (next_word.c_str());
-            if (sub_cmd_obj)
+            if (cmd_obj->IsMultiwordObject ())
             {
-                actual_cmd_name_len += next_word.length() + 1;
-                revised_command_line.Printf (" %s", next_word.c_str());
-                cmd_obj = sub_cmd_obj;
-                wants_raw_input = cmd_obj->WantsRawCommandString ();
+                CommandObject *sub_cmd_obj = ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (next_word.c_str());
+                if (sub_cmd_obj)
+                {
+                    actual_cmd_name_len += next_word.length() + 1;
+                    revised_command_line.Printf (" %s", next_word.c_str());
+                    cmd_obj = sub_cmd_obj;
+                    wants_raw_input = cmd_obj->WantsRawCommandString ();
+                }
+                else
+                {
+                    if (quote_char)
+                        revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
+                    else
+                        revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
+                    done = true;
+                }
             }
             else
             {
                 if (quote_char)
-                    revised_command_line.Printf (" %c%s%c", quote_char, next_word.c_str(), quote_char);
+                    revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
                 else
-                    revised_command_line.Printf (" %s", next_word.c_str());
+                    revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
                 done = true;
             }
         }
-        else
-        {
-            if (quote_char)
-                revised_command_line.Printf (" %c%s%c", quote_char, next_word.c_str(), quote_char);
-            else
-                revised_command_line.Printf (" %s", next_word.c_str());
-            done = true;
-        }
 
         if (cmd_obj == NULL)
         {
@@ -1306,13 +1333,39 @@
             return false;
         }
 
-        next_word.erase ();
+        if (cmd_obj->IsMultiwordObject ())
+        {
+            if (!suffix.empty())
+            {
+
+                result.AppendErrorWithFormat ("multi-word commands ('%s') can't have shorthand suffixes: '%s'\n", 
+                                              next_word.c_str(),
+                                              suffix.c_str());
+                result.SetStatus (eReturnStatusFailed);
+                return false;
+            }
+        }
+        else
+        {
+            // If we found a normal command, we are done
+            done = true;
+            if (!suffix.empty())
+            {
+                switch (suffix[0])
+                {
+                case '/':
+                    // GDB format suffixes
+                    revised_command_line.Printf (" --gdb-format=%s", suffix.c_str() + 1);
+                    break;
+                }
+            }
+        }
         if (command_string.length() == 0)
             done = true;
             
     }
 
-    if (command_string.size() > 0)
+    if (!command_string.empty())
         revised_command_line.Printf (" %s", command_string.c_str());
 
     // End of Phase 1.
@@ -1345,7 +1398,7 @@
                 m_repeat_command.assign(original_command_string.c_str());
             
             // Don't keep pushing the same command onto the history...
-            if (m_command_history.size() == 0 || m_command_history.back() != original_command_string) 
+            if (m_command_history.empty() || m_command_history.back() != original_command_string) 
                 m_command_history.push_back (original_command_string);
         }
         
@@ -2450,15 +2503,13 @@
 void
 CommandInterpreter::DumpHistory (Stream &stream, uint32_t start, uint32_t end) const
 {
-    size_t num_history_elements = m_command_history.size();
-    if (start > num_history_elements)
-        return;
-    for (uint32_t i = start; i < num_history_elements && i <= end; i++)
+    const size_t last_idx = std::min<size_t>(m_command_history.size(), end + 1);
+    for (size_t i = start; i < last_idx; i++)
     {
         if (!m_command_history[i].empty())
         {
             stream.Indent();
-            stream.Printf ("%4d: %s\n", i, m_command_history[i].c_str());
+            stream.Printf ("%4zu: %s\n", i, m_command_history[i].c_str());
         }
     }
 }





More information about the lldb-commits mailing list