1 //===-- CommandObjectScript.cpp -------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CommandObjectScript.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/DataFormatters/DataVisualization.h"
12 #include "lldb/Host/Config.h"
13 #include "lldb/Interpreter/CommandInterpreter.h"
14 #include "lldb/Interpreter/CommandReturnObject.h"
15 #include "lldb/Interpreter/ScriptInterpreter.h"
16 #include "lldb/Utility/Args.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 // CommandObjectScript
22 
23 CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter)
24     : CommandObjectRaw(
25           interpreter, "script",
26           "Invoke the script interpreter with provided code and display any "
27           "results.  Start the interactive interpreter if no code is supplied.",
28           "script [<script-code>]") {}
29 
30 CommandObjectScript::~CommandObjectScript() {}
31 
32 bool CommandObjectScript::DoExecute(llvm::StringRef command,
33                                     CommandReturnObject &result) {
34   if (m_interpreter.GetDebugger().GetScriptLanguage() ==
35       lldb::eScriptLanguageNone) {
36     result.AppendError(
37         "the script-lang setting is set to none - scripting not available");
38     result.SetStatus(eReturnStatusFailed);
39     return false;
40   }
41 
42   ScriptInterpreter *script_interpreter = GetDebugger().GetScriptInterpreter();
43 
44   if (script_interpreter == nullptr) {
45     result.AppendError("no script interpreter");
46     result.SetStatus(eReturnStatusFailed);
47     return false;
48   }
49 
50   // Script might change Python code we use for formatting. Make sure we keep
51   // up to date with it.
52   DataVisualization::ForceUpdate();
53 
54   if (command.empty()) {
55     script_interpreter->ExecuteInterpreterLoop();
56     result.SetStatus(eReturnStatusSuccessFinishNoResult);
57     return result.Succeeded();
58   }
59 
60   // We can do better when reporting the status of one-liner script execution.
61   if (script_interpreter->ExecuteOneLine(command, &result))
62     result.SetStatus(eReturnStatusSuccessFinishNoResult);
63   else
64     result.SetStatus(eReturnStatusFailed);
65 
66   return result.Succeeded();
67 }
68