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