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 // C Includes
13ac7ddfbfSEd Maste // C++ Includes
14ac7ddfbfSEd Maste // Other libraries and framework includes
15ac7ddfbfSEd Maste // Project includes
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste #include "lldb/Core/Debugger.h"
18ac7ddfbfSEd Maste 
19ac7ddfbfSEd Maste #include "lldb/DataFormatters/DataVisualization.h"
20ac7ddfbfSEd Maste 
21ac7ddfbfSEd Maste #include "lldb/Interpreter/Args.h"
22ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
23ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
24ac7ddfbfSEd Maste #include "lldb/Interpreter/ScriptInterpreter.h"
25ac7ddfbfSEd Maste 
26ac7ddfbfSEd Maste using namespace lldb;
27ac7ddfbfSEd Maste using namespace lldb_private;
28ac7ddfbfSEd Maste 
29ac7ddfbfSEd Maste //-------------------------------------------------------------------------
30ac7ddfbfSEd Maste // CommandObjectScript
31ac7ddfbfSEd Maste //-------------------------------------------------------------------------
32ac7ddfbfSEd Maste 
33435933ddSDimitry Andric CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
34435933ddSDimitry Andric                                          ScriptLanguage script_lang)
35435933ddSDimitry Andric     : CommandObjectRaw(
36435933ddSDimitry Andric           interpreter, "script",
37435933ddSDimitry Andric           "Invoke the script interpreter with provided code and display any "
384bb0738eSEd Maste           "results.  Start the interactive interpreter if no code is supplied.",
39435933ddSDimitry Andric           "script [<script-code>]") {}
40ac7ddfbfSEd Maste 
41435933ddSDimitry Andric CommandObjectScript::~CommandObjectScript() {}
42ac7ddfbfSEd Maste 
43435933ddSDimitry Andric bool CommandObjectScript::DoExecute(const char *command,
44435933ddSDimitry Andric                                     CommandReturnObject &result) {
45ac7ddfbfSEd Maste #ifdef LLDB_DISABLE_PYTHON
46435933ddSDimitry Andric   // if we ever support languages other than Python this simple #ifdef won't
47435933ddSDimitry Andric   // work
48ac7ddfbfSEd Maste   result.AppendError("your copy of LLDB does not support scripting.");
49ac7ddfbfSEd Maste   result.SetStatus(eReturnStatusFailed);
50ac7ddfbfSEd Maste   return false;
51ac7ddfbfSEd Maste #else
52435933ddSDimitry Andric   if (m_interpreter.GetDebugger().GetScriptLanguage() ==
53435933ddSDimitry Andric       lldb::eScriptLanguageNone) {
54435933ddSDimitry Andric     result.AppendError(
55435933ddSDimitry Andric         "the script-lang setting is set to none - scripting not available");
56ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusFailed);
57ac7ddfbfSEd Maste     return false;
58ac7ddfbfSEd Maste   }
59ac7ddfbfSEd Maste 
60ac7ddfbfSEd Maste   ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter();
61ac7ddfbfSEd Maste 
62435933ddSDimitry Andric   if (script_interpreter == nullptr) {
63ac7ddfbfSEd Maste     result.AppendError("no script interpreter");
64ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusFailed);
65ac7ddfbfSEd Maste     return false;
66ac7ddfbfSEd Maste   }
67ac7ddfbfSEd Maste 
68435933ddSDimitry Andric   DataVisualization::ForceUpdate(); // script might change Python code we use
69435933ddSDimitry Andric                                     // for formatting.. make sure we keep up to
70435933ddSDimitry Andric                                     // date with it
71ac7ddfbfSEd Maste 
72435933ddSDimitry Andric   if (command == nullptr || command[0] == '\0') {
73ac7ddfbfSEd Maste     script_interpreter->ExecuteInterpreterLoop();
74ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusSuccessFinishNoResult);
75ac7ddfbfSEd Maste     return result.Succeeded();
76ac7ddfbfSEd Maste   }
77ac7ddfbfSEd Maste 
78ac7ddfbfSEd Maste   // We can do better when reporting the status of one-liner script execution.
79ac7ddfbfSEd Maste   if (script_interpreter->ExecuteOneLine(command, &result))
80ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusSuccessFinishNoResult);
81ac7ddfbfSEd Maste   else
82ac7ddfbfSEd Maste     result.SetStatus(eReturnStatusFailed);
83ac7ddfbfSEd Maste 
84ac7ddfbfSEd Maste   return result.Succeeded();
85ac7ddfbfSEd Maste #endif
86ac7ddfbfSEd Maste }
87