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 "lldb/lldb-python.h"
11 
12 #include "CommandObjectScript.h"
13 
14 // C Includes
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 
19 #include "lldb/Core/Debugger.h"
20 
21 #include "lldb/DataFormatters/DataVisualization.h"
22 
23 #include "lldb/Interpreter/Args.h"
24 #include "lldb/Interpreter/CommandInterpreter.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Interpreter/ScriptInterpreter.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 //-------------------------------------------------------------------------
32 // CommandObjectScript
33 //-------------------------------------------------------------------------
34 
CommandObjectScript(CommandInterpreter & interpreter,ScriptLanguage script_lang)35 CommandObjectScript::CommandObjectScript (CommandInterpreter &interpreter, ScriptLanguage script_lang) :
36     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.",
39                       "script [<script-expression-for-evaluation>]")
40 {
41 }
42 
~CommandObjectScript()43 CommandObjectScript::~CommandObjectScript ()
44 {
45 }
46 
47 bool
DoExecute(const char * command,CommandReturnObject & result)48 CommandObjectScript::DoExecute
49 (
50     const char *command,
51     CommandReturnObject &result
52 )
53 {
54 #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 
67     ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter ();
68 
69     if (script_interpreter == NULL)
70     {
71         result.AppendError("no script interpreter");
72         result.SetStatus (eReturnStatusFailed);
73         return false;
74     }
75 
76     DataVisualization::ForceUpdate(); // script might change Python code we use for formatting.. make sure we keep up to date with it
77 
78     if (command == NULL || command[0] == '\0')
79     {
80         script_interpreter->ExecuteInterpreterLoop ();
81         result.SetStatus (eReturnStatusSuccessFinishNoResult);
82         return result.Succeeded();
83     }
84 
85     // We can do better when reporting the status of one-liner script execution.
86     if (script_interpreter->ExecuteOneLine (command, &result))
87         result.SetStatus(eReturnStatusSuccessFinishNoResult);
88     else
89         result.SetStatus(eReturnStatusFailed);
90 
91     return result.Succeeded();
92 #endif
93 }
94