Added a new host function that allows us to run shell command and get the output from them along with the status and signal:

Error
Host::RunShellCommand (const char *command,
                       const char *working_dir,
                       int *status_ptr,
                       int *signo_ptr,
                       std::string *command_output_ptr,
                       uint32_t timeout_sec);

This will allow us to use this functionality in the host lldb_private::Platform, and also use it in our lldb-platform binary. It leverages the existing code in Host::LaunchProcess and ProcessLaunchInfo.



git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@154730 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Commands/CommandObjectPlatform.cpp b/source/Commands/CommandObjectPlatform.cpp
index 9618c6c..e4d0273 100644
--- a/source/Commands/CommandObjectPlatform.cpp
+++ b/source/Commands/CommandObjectPlatform.cpp
@@ -870,6 +870,75 @@
     DISALLOW_COPY_AND_ASSIGN (CommandObjectPlatformProcess);
 };
 
+
+class CommandObjectPlatformShell : public CommandObject
+{
+public:
+    CommandObjectPlatformShell (CommandInterpreter &interpreter) :
+    CommandObject (interpreter, 
+                   "platform shell",
+                   "Run a shell command on a the selected platform.",
+                   "platform shell <shell-command>",
+                   0)
+    {
+    }
+    
+    virtual
+    ~CommandObjectPlatformShell ()
+    {
+    }
+    
+    virtual bool
+    Execute (Args& command,
+             CommandReturnObject &result)
+    {
+        return false;
+    }
+    
+    virtual bool
+    WantsRawCommandString() { return true; }    
+
+    bool
+    ExecuteRawCommandString (const char *raw_command_line, CommandReturnObject &result)
+    {
+        // TODO: Implement "Platform::RunShellCommand()" and switch over to using
+        // the current platform when it is in the interface. 
+        const char *working_dir = NULL;
+        std::string output;
+        int status = -1;
+        int signo = -1;
+        Error error (Host::RunShellCommand (raw_command_line, working_dir, &status, &signo, &output, 10));
+        if (!output.empty())
+            result.GetOutputStream().PutCString(output.c_str());
+        if (status > 0)
+        {
+            if (signo > 0)
+            {
+                const char *signo_cstr = Host::GetSignalAsCString(signo);
+                if (signo_cstr)
+                    result.GetOutputStream().Printf("error: command returned with status %i and signal %s\n", status, signo_cstr);
+                else
+                    result.GetOutputStream().Printf("error: command returned with status %i and signal %i\n", status, signo);
+            }
+            else
+                result.GetOutputStream().Printf("error: command returned with status %i\n", status);
+        }
+
+        if (error.Fail())
+        {
+            result.AppendError(error.AsCString());
+            result.SetStatus (eReturnStatusFailed);
+        }
+        else
+        {
+            result.SetStatus (eReturnStatusSuccessFinishResult);
+        }
+        return true;
+    }
+
+protected:
+};
+
 //----------------------------------------------------------------------
 // CommandObjectPlatform constructor
 //----------------------------------------------------------------------
@@ -885,6 +954,7 @@
     LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect  (interpreter)));
     LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect  (interpreter)));
     LoadSubCommand ("process", CommandObjectSP (new CommandObjectPlatformProcess  (interpreter)));
+    LoadSubCommand ("shell", CommandObjectSP (new CommandObjectPlatformShell  (interpreter)));
 }