From d1cf11a74df14374c8be20dd43e805d2dfb1de07 Mon Sep 17 00:00:00 2001 From: Greg Clayton Date: Sat, 14 Apr 2012 01:42:46 +0000 Subject: 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. llvm-svn: 154730 --- lldb/source/Commands/CommandObjectPlatform.cpp | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'lldb/source/Commands/CommandObjectPlatform.cpp') diff --git a/lldb/source/Commands/CommandObjectPlatform.cpp b/lldb/source/Commands/CommandObjectPlatform.cpp index 9618c6c..e4d0273 100644 --- a/lldb/source/Commands/CommandObjectPlatform.cpp +++ b/lldb/source/Commands/CommandObjectPlatform.cpp @@ -870,6 +870,75 @@ private: 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 ", + 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 @@ CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter) : LoadSubCommand ("connect", CommandObjectSP (new CommandObjectPlatformConnect (interpreter))); LoadSubCommand ("disconnect", CommandObjectSP (new CommandObjectPlatformDisconnect (interpreter))); LoadSubCommand ("process", CommandObjectSP (new CommandObjectPlatformProcess (interpreter))); + LoadSubCommand ("shell", CommandObjectSP (new CommandObjectPlatformShell (interpreter))); } -- cgit v1.1