blob: 0891298c5ac099b12c98a0e579f84a8a6682dd8f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Enrico Granataa37a0652011-07-24 00:14:56 +000016
17#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Enrico Granata5548cb52013-01-28 23:47:25 +000019#include "lldb/DataFormatters/DataVisualization.h"
20
21#include "lldb/Interpreter/Args.h"
22#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Interpreter/CommandReturnObject.h"
24#include "lldb/Interpreter/ScriptInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
29//-------------------------------------------------------------------------
30// CommandObjectScript
31//-------------------------------------------------------------------------
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033CommandObjectScript::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>]") {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041CommandObjectScript::~CommandObjectScript() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043bool CommandObjectScript::DoExecute(const char *command,
44 CommandReturnObject &result) {
Enrico Granata4af6bf62013-02-19 22:34:01 +000045#ifdef LLDB_DISABLE_PYTHON
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 // 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;
Enrico Granata4af6bf62013-02-19 22:34:01 +000051#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 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 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 if (script_interpreter == nullptr) {
63 result.AppendError("no script interpreter");
64 result.SetStatus(eReturnStatusFailed);
65 return false;
66 }
Johnny Chen7dc2e472010-07-30 22:33:14 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 DataVisualization::ForceUpdate(); // script might change Python code we use
69 // for formatting.. make sure we keep up to
70 // date with it
Johnny Chen7dc2e472010-07-30 22:33:14 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 if (command == nullptr || command[0] == '\0') {
73 script_interpreter->ExecuteInterpreterLoop();
74 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 return result.Succeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 }
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();
Enrico Granata4af6bf62013-02-19 22:34:01 +000085#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086}