1254721Semaste//===-- CommandObjectScript.cpp ---------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#include "CommandObjectScript.h"
10254721Semaste#include "lldb/Core/Debugger.h"
11254721Semaste#include "lldb/DataFormatters/DataVisualization.h"
12360784Sdim#include "lldb/Host/Config.h"
13254721Semaste#include "lldb/Interpreter/CommandInterpreter.h"
14254721Semaste#include "lldb/Interpreter/CommandReturnObject.h"
15254721Semaste#include "lldb/Interpreter/ScriptInterpreter.h"
16341825Sdim#include "lldb/Utility/Args.h"
17254721Semaste
18254721Semasteusing namespace lldb;
19254721Semasteusing namespace lldb_private;
20254721Semaste
21254721Semaste// CommandObjectScript
22254721Semaste
23314564SdimCommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
24314564Sdim                                         ScriptLanguage script_lang)
25314564Sdim    : CommandObjectRaw(
26314564Sdim          interpreter, "script",
27314564Sdim          "Invoke the script interpreter with provided code and display any "
28314564Sdim          "results.  Start the interactive interpreter if no code is supplied.",
29314564Sdim          "script [<script-code>]") {}
30254721Semaste
31314564SdimCommandObjectScript::~CommandObjectScript() {}
32254721Semaste
33341825Sdimbool CommandObjectScript::DoExecute(llvm::StringRef command,
34314564Sdim                                    CommandReturnObject &result) {
35314564Sdim  if (m_interpreter.GetDebugger().GetScriptLanguage() ==
36314564Sdim      lldb::eScriptLanguageNone) {
37314564Sdim    result.AppendError(
38314564Sdim        "the script-lang setting is set to none - scripting not available");
39314564Sdim    result.SetStatus(eReturnStatusFailed);
40254721Semaste    return false;
41314564Sdim  }
42254721Semaste
43353358Sdim  ScriptInterpreter *script_interpreter = GetDebugger().GetScriptInterpreter();
44254721Semaste
45314564Sdim  if (script_interpreter == nullptr) {
46314564Sdim    result.AppendError("no script interpreter");
47314564Sdim    result.SetStatus(eReturnStatusFailed);
48314564Sdim    return false;
49314564Sdim  }
50254721Semaste
51360784Sdim  // Script might change Python code we use for formatting. Make sure we keep
52360784Sdim  // up to date with it.
53360784Sdim  DataVisualization::ForceUpdate();
54254721Semaste
55341825Sdim  if (command.empty()) {
56314564Sdim    script_interpreter->ExecuteInterpreterLoop();
57314564Sdim    result.SetStatus(eReturnStatusSuccessFinishNoResult);
58254721Semaste    return result.Succeeded();
59314564Sdim  }
60314564Sdim
61314564Sdim  // We can do better when reporting the status of one-liner script execution.
62314564Sdim  if (script_interpreter->ExecuteOneLine(command, &result))
63314564Sdim    result.SetStatus(eReturnStatusSuccessFinishNoResult);
64314564Sdim  else
65314564Sdim    result.SetStatus(eReturnStatusFailed);
66314564Sdim
67314564Sdim  return result.Succeeded();
68254721Semaste}
69