CommandObjectScript.cpp revision 341825
1//===-- CommandObjectScript.cpp ---------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectScript.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16
17#include "lldb/Core/Debugger.h"
18
19#include "lldb/DataFormatters/DataVisualization.h"
20
21#include "lldb/Interpreter/CommandInterpreter.h"
22#include "lldb/Interpreter/CommandReturnObject.h"
23#include "lldb/Interpreter/ScriptInterpreter.h"
24#include "lldb/Utility/Args.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29//-------------------------------------------------------------------------
30// CommandObjectScript
31//-------------------------------------------------------------------------
32
33CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
34                                         ScriptLanguage script_lang)
35    : CommandObjectRaw(
36          interpreter, "script",
37          "Invoke the script interpreter with provided code and display any "
38          "results.  Start the interactive interpreter if no code is supplied.",
39          "script [<script-code>]") {}
40
41CommandObjectScript::~CommandObjectScript() {}
42
43bool CommandObjectScript::DoExecute(llvm::StringRef command,
44                                    CommandReturnObject &result) {
45#ifdef LLDB_DISABLE_PYTHON
46  // if we ever support languages other than Python this simple #ifdef won't
47  // work
48  result.AppendError("your copy of LLDB does not support scripting.");
49  result.SetStatus(eReturnStatusFailed);
50  return false;
51#else
52  if (m_interpreter.GetDebugger().GetScriptLanguage() ==
53      lldb::eScriptLanguageNone) {
54    result.AppendError(
55        "the script-lang setting is set to none - scripting not available");
56    result.SetStatus(eReturnStatusFailed);
57    return false;
58  }
59
60  ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter();
61
62  if (script_interpreter == nullptr) {
63    result.AppendError("no script interpreter");
64    result.SetStatus(eReturnStatusFailed);
65    return false;
66  }
67
68  DataVisualization::ForceUpdate(); // script might change Python code we use
69                                    // for formatting.. make sure we keep up to
70                                    // date with it
71
72  if (command.empty()) {
73    script_interpreter->ExecuteInterpreterLoop();
74    result.SetStatus(eReturnStatusSuccessFinishNoResult);
75    return result.Succeeded();
76  }
77
78  // We can do better when reporting the status of one-liner script execution.
79  if (script_interpreter->ExecuteOneLine(command, &result))
80    result.SetStatus(eReturnStatusSuccessFinishNoResult);
81  else
82    result.SetStatus(eReturnStatusFailed);
83
84  return result.Succeeded();
85#endif
86}
87