CommandObjectScript.cpp revision 353358
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
11254721Semaste
12254721Semaste#include "lldb/Core/Debugger.h"
13254721Semaste
14254721Semaste#include "lldb/DataFormatters/DataVisualization.h"
15254721Semaste
16254721Semaste#include "lldb/Interpreter/CommandInterpreter.h"
17254721Semaste#include "lldb/Interpreter/CommandReturnObject.h"
18254721Semaste#include "lldb/Interpreter/ScriptInterpreter.h"
19341825Sdim#include "lldb/Utility/Args.h"
20254721Semaste
21254721Semasteusing namespace lldb;
22254721Semasteusing namespace lldb_private;
23254721Semaste
24254721Semaste// CommandObjectScript
25254721Semaste
26314564SdimCommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
27314564Sdim                                         ScriptLanguage script_lang)
28314564Sdim    : CommandObjectRaw(
29314564Sdim          interpreter, "script",
30314564Sdim          "Invoke the script interpreter with provided code and display any "
31314564Sdim          "results.  Start the interactive interpreter if no code is supplied.",
32314564Sdim          "script [<script-code>]") {}
33254721Semaste
34314564SdimCommandObjectScript::~CommandObjectScript() {}
35254721Semaste
36341825Sdimbool CommandObjectScript::DoExecute(llvm::StringRef command,
37314564Sdim                                    CommandReturnObject &result) {
38254721Semaste#ifdef LLDB_DISABLE_PYTHON
39314564Sdim  // if we ever support languages other than Python this simple #ifdef won't
40314564Sdim  // work
41314564Sdim  result.AppendError("your copy of LLDB does not support scripting.");
42314564Sdim  result.SetStatus(eReturnStatusFailed);
43314564Sdim  return false;
44314564Sdim#else
45314564Sdim  if (m_interpreter.GetDebugger().GetScriptLanguage() ==
46314564Sdim      lldb::eScriptLanguageNone) {
47314564Sdim    result.AppendError(
48314564Sdim        "the script-lang setting is set to none - scripting not available");
49314564Sdim    result.SetStatus(eReturnStatusFailed);
50254721Semaste    return false;
51314564Sdim  }
52254721Semaste
53353358Sdim  ScriptInterpreter *script_interpreter = GetDebugger().GetScriptInterpreter();
54254721Semaste
55314564Sdim  if (script_interpreter == nullptr) {
56314564Sdim    result.AppendError("no script interpreter");
57314564Sdim    result.SetStatus(eReturnStatusFailed);
58314564Sdim    return false;
59314564Sdim  }
60254721Semaste
61314564Sdim  DataVisualization::ForceUpdate(); // script might change Python code we use
62314564Sdim                                    // for formatting.. make sure we keep up to
63314564Sdim                                    // date with it
64254721Semaste
65341825Sdim  if (command.empty()) {
66314564Sdim    script_interpreter->ExecuteInterpreterLoop();
67314564Sdim    result.SetStatus(eReturnStatusSuccessFinishNoResult);
68254721Semaste    return result.Succeeded();
69314564Sdim  }
70314564Sdim
71314564Sdim  // We can do better when reporting the status of one-liner script execution.
72314564Sdim  if (script_interpreter->ExecuteOneLine(command, &result))
73314564Sdim    result.SetStatus(eReturnStatusSuccessFinishNoResult);
74314564Sdim  else
75314564Sdim    result.SetStatus(eReturnStatusFailed);
76314564Sdim
77314564Sdim  return result.Succeeded();
78254721Semaste#endif
79254721Semaste}
80