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