]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Interpreter/CommandObjectScript.cpp
Vendor import of lldb trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / source / Interpreter / CommandObjectScript.cpp
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
13 #include "lldb/Core/Debugger.h"
14
15 #include "lldb/DataFormatters/DataVisualization.h"
16
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/CommandReturnObject.h"
19 #include "lldb/Interpreter/ScriptInterpreter.h"
20 #include "lldb/Utility/Args.h"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 //-------------------------------------------------------------------------
26 // CommandObjectScript
27 //-------------------------------------------------------------------------
28
29 CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
30                                          ScriptLanguage script_lang)
31     : CommandObjectRaw(
32           interpreter, "script",
33           "Invoke the script interpreter with provided code and display any "
34           "results.  Start the interactive interpreter if no code is supplied.",
35           "script [<script-code>]") {}
36
37 CommandObjectScript::~CommandObjectScript() {}
38
39 bool CommandObjectScript::DoExecute(llvm::StringRef command,
40                                     CommandReturnObject &result) {
41 #ifdef LLDB_DISABLE_PYTHON
42   // if we ever support languages other than Python this simple #ifdef won't
43   // work
44   result.AppendError("your copy of LLDB does not support scripting.");
45   result.SetStatus(eReturnStatusFailed);
46   return false;
47 #else
48   if (m_interpreter.GetDebugger().GetScriptLanguage() ==
49       lldb::eScriptLanguageNone) {
50     result.AppendError(
51         "the script-lang setting is set to none - scripting not available");
52     result.SetStatus(eReturnStatusFailed);
53     return false;
54   }
55
56   ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter();
57
58   if (script_interpreter == nullptr) {
59     result.AppendError("no script interpreter");
60     result.SetStatus(eReturnStatusFailed);
61     return false;
62   }
63
64   DataVisualization::ForceUpdate(); // script might change Python code we use
65                                     // for formatting.. make sure we keep up to
66                                     // date with it
67
68   if (command.empty()) {
69     script_interpreter->ExecuteInterpreterLoop();
70     result.SetStatus(eReturnStatusSuccessFinishNoResult);
71     return result.Succeeded();
72   }
73
74   // We can do better when reporting the status of one-liner script execution.
75   if (script_interpreter->ExecuteOneLine(command, &result))
76     result.SetStatus(eReturnStatusSuccessFinishNoResult);
77   else
78     result.SetStatus(eReturnStatusFailed);
79
80   return result.Succeeded();
81 #endif
82 }