1 //===-- CommandObjectScript.cpp ---------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectScript.h"
12 #include "lldb/Core/Debugger.h"
14 #include "lldb/DataFormatters/DataVisualization.h"
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Interpreter/CommandReturnObject.h"
18 #include "lldb/Interpreter/ScriptInterpreter.h"
19 #include "lldb/Utility/Args.h"
22 using namespace lldb_private;
24 // CommandObjectScript
26 CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
27 ScriptLanguage script_lang)
29 interpreter, "script",
30 "Invoke the script interpreter with provided code and display any "
31 "results. Start the interactive interpreter if no code is supplied.",
32 "script [<script-code>]") {}
34 CommandObjectScript::~CommandObjectScript() {}
36 bool CommandObjectScript::DoExecute(llvm::StringRef command,
37 CommandReturnObject &result) {
38 #ifdef LLDB_DISABLE_PYTHON
39 // if we ever support languages other than Python this simple #ifdef won't
41 result.AppendError("your copy of LLDB does not support scripting.");
42 result.SetStatus(eReturnStatusFailed);
45 if (m_interpreter.GetDebugger().GetScriptLanguage() ==
46 lldb::eScriptLanguageNone) {
48 "the script-lang setting is set to none - scripting not available");
49 result.SetStatus(eReturnStatusFailed);
53 ScriptInterpreter *script_interpreter = GetDebugger().GetScriptInterpreter();
55 if (script_interpreter == nullptr) {
56 result.AppendError("no script interpreter");
57 result.SetStatus(eReturnStatusFailed);
61 DataVisualization::ForceUpdate(); // script might change Python code we use
62 // for formatting.. make sure we keep up to
65 if (command.empty()) {
66 script_interpreter->ExecuteInterpreterLoop();
67 result.SetStatus(eReturnStatusSuccessFinishNoResult);
68 return result.Succeeded();
71 // We can do better when reporting the status of one-liner script execution.
72 if (script_interpreter->ExecuteOneLine(command, &result))
73 result.SetStatus(eReturnStatusSuccessFinishNoResult);
75 result.SetStatus(eReturnStatusFailed);
77 return result.Succeeded();