blob: e74c1f9ff99dd7f99c2fb0750a7f4d679c133e33 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectScript.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Enrico Granataa37a0652011-07-24 00:14:56 +000018
19#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Enrico Granata5548cb52013-01-28 23:47:25 +000021#include "lldb/DataFormatters/DataVisualization.h"
22
23#include "lldb/Interpreter/Args.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Interpreter/CommandReturnObject.h"
26#include "lldb/Interpreter/ScriptInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31//-------------------------------------------------------------------------
32// CommandObjectScript
33//-------------------------------------------------------------------------
34
Greg Claytona7015092010-09-18 01:14:36 +000035CommandObjectScript::CommandObjectScript (CommandInterpreter &interpreter, ScriptLanguage script_lang) :
Jim Ingham5a988412012-06-08 21:56:10 +000036 CommandObjectRaw (interpreter,
37 "script",
38 "Pass an expression to the script interpreter for evaluation and return the results. Drop into the interactive interpreter if no expression is given.",
Daniel Maleaa85e6b62012-12-07 22:21:08 +000039 "script [<script-expression-for-evaluation>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040{
41}
42
43CommandObjectScript::~CommandObjectScript ()
44{
45}
46
47bool
Jim Ingham5a988412012-06-08 21:56:10 +000048CommandObjectScript::DoExecute
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049(
50 const char *command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 CommandReturnObject &result
52)
53{
Enrico Granata4af6bf62013-02-19 22:34:01 +000054#ifdef LLDB_DISABLE_PYTHON
55 // if we ever support languages other than Python this simple #ifdef won't work
56 result.AppendError("your copy of LLDB does not support scripting.")
57 result.SetStatus (eReturnStatusFailed);
58 return false;
59#else
60 if (m_interpreter.GetDebugger().GetScriptLanguage() == lldb::eScriptLanguageNone)
61 {
62 result.AppendError("the script-lang setting is set to none - scripting not available");
63 result.SetStatus (eReturnStatusFailed);
64 return false;
65 }
66
Caroline Tice2f88aad2011-01-14 00:29:16 +000067 ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
69 if (script_interpreter == NULL)
70 {
Greg Clayton710dd5a2011-01-08 20:28:42 +000071 result.AppendError("no script interpreter");
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 result.SetStatus (eReturnStatusFailed);
Jim Ingham28eb5712012-10-12 17:34:26 +000073 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 }
75
Enrico Granatac482a192011-08-17 22:13:59 +000076 DataVisualization::ForceUpdate(); // script might change Python code we use for formatting.. make sure we keep up to date with it
Enrico Granataa37a0652011-07-24 00:14:56 +000077
Enrico Granata85933ed2011-08-18 16:38:26 +000078 if (command == NULL || command[0] == '\0')
79 {
Greg Claytona7015092010-09-18 01:14:36 +000080 script_interpreter->ExecuteInterpreterLoop ();
Johnny Chen7dc2e472010-07-30 22:33:14 +000081 result.SetStatus (eReturnStatusSuccessFinishNoResult);
82 return result.Succeeded();
83 }
84
85 // We can do better when reporting the status of one-liner script execution.
Enrico Granata085577f2012-10-31 00:01:26 +000086 if (script_interpreter->ExecuteOneLine (command, &result))
Johnny Chen7dc2e472010-07-30 22:33:14 +000087 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 else
Johnny Chen7dc2e472010-07-30 22:33:14 +000089 result.SetStatus(eReturnStatusFailed);
90
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 return result.Succeeded();
Enrico Granata4af6bf62013-02-19 22:34:01 +000092#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093}