1ac7ddfbfSEd Maste //===-- CommandObjectScript.cpp ---------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "CommandObjectScript.h"
11ac7ddfbfSEd Maste 
12ac7ddfbfSEd Maste 
13ac7ddfbfSEd Maste #include "lldb/Core/Debugger.h"
14ac7ddfbfSEd Maste 
15ac7ddfbfSEd Maste #include "lldb/DataFormatters/DataVisualization.h"
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
18ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
19ac7ddfbfSEd Maste #include "lldb/Interpreter/ScriptInterpreter.h"
20*4ba319b5SDimitry Andric #include "lldb/Utility/Args.h"
21ac7ddfbfSEd Maste 
22ac7ddfbfSEd Maste using namespace lldb;
23ac7ddfbfSEd Maste using namespace lldb_private;
24ac7ddfbfSEd Maste 
25ac7ddfbfSEd Maste //-------------------------------------------------------------------------
26ac7ddfbfSEd Maste // CommandObjectScript
27ac7ddfbfSEd Maste //-------------------------------------------------------------------------
28ac7ddfbfSEd Maste 
CommandObjectScript(CommandInterpreter & interpreter,ScriptLanguage script_lang)29435933ddSDimitry Andric CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
30435933ddSDimitry Andric                                          ScriptLanguage script_lang)
31435933ddSDimitry Andric     : CommandObjectRaw(
32435933ddSDimitry Andric           interpreter, "script",
33435933ddSDimitry Andric           "Invoke the script interpreter with provided code and display any "
344bb0738eSEd Maste           "results.  Start the interactive interpreter if no code is supplied.",
35435933ddSDimitry Andric           "script [<script-code>]") {}
36ac7ddfbfSEd Maste 
~CommandObjectScript()37435933ddSDimitry Andric CommandObjectScript::~CommandObjectScript() {}
38ac7ddfbfSEd Maste 
DoExecute(llvm::StringRef command,CommandReturnObject & result)39*4ba319b5SDimitry Andric bool CommandObjectScript::DoExecute(llvm::StringRef command,
40435933ddSDimitry Andric                                     CommandReturnObject &result) {
41ac7ddfbfSEd Maste #ifdef LLDB_DISABLE_PYTHON
42435933ddSDimitry Andric   // if we ever support languages other than Python this simple #ifdef won't
43435933ddSDimitry Andric   // work
44ac7ddfbfSEd Maste   result.AppendError("your copy of LLDB does not support scripting.");
45ac7ddfbfSEd Maste   result.SetStatus(eReturnStatusFailed);
46ac7ddfbfSEd Maste   return false;
47ac7ddfbfSEd Maste #else
48435933ddSDimitry Andric   if (m_interpreter.GetDebugger().GetScriptLanguage() ==
49435933ddSDimitry Andric       lldb::eScriptLanguageNone) {
50435933ddSDimitry Andric     result.AppendError(
51435933ddSDimitry Andric         "the script-lang setting is set to none - scripting not available");
52ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusFailed);
53ac7ddfbfSEd Maste     return false;
54ac7ddfbfSEd Maste   }
55ac7ddfbfSEd Maste 
56ac7ddfbfSEd Maste   ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter();
57ac7ddfbfSEd Maste 
58435933ddSDimitry Andric   if (script_interpreter == nullptr) {
59ac7ddfbfSEd Maste     result.AppendError("no script interpreter");
60ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusFailed);
61ac7ddfbfSEd Maste     return false;
62ac7ddfbfSEd Maste   }
63ac7ddfbfSEd Maste 
64435933ddSDimitry Andric   DataVisualization::ForceUpdate(); // script might change Python code we use
65435933ddSDimitry Andric                                     // for formatting.. make sure we keep up to
66435933ddSDimitry Andric                                     // date with it
67ac7ddfbfSEd Maste 
68*4ba319b5SDimitry Andric   if (command.empty()) {
69ac7ddfbfSEd Maste     script_interpreter->ExecuteInterpreterLoop();
70ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusSuccessFinishNoResult);
71ac7ddfbfSEd Maste     return result.Succeeded();
72ac7ddfbfSEd Maste   }
73ac7ddfbfSEd Maste 
74ac7ddfbfSEd Maste   // We can do better when reporting the status of one-liner script execution.
75ac7ddfbfSEd Maste   if (script_interpreter->ExecuteOneLine(command, &result))
76ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusSuccessFinishNoResult);
77ac7ddfbfSEd Maste   else
78ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusFailed);
79ac7ddfbfSEd Maste 
80ac7ddfbfSEd Maste   return result.Succeeded();
81ac7ddfbfSEd Maste #endif
82ac7ddfbfSEd Maste }
83