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 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Args.h"
17 
18 #include "lldb/Interpreter/CommandReturnObject.h"
19 #include "lldb/Interpreter/ScriptInterpreter.h"
20 #include "lldb/Interpreter/ScriptInterpreterPython.h"
21 #include "lldb/Interpreter/ScriptInterpreterNone.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 //-------------------------------------------------------------------------
27 // CommandObjectScript
28 //-------------------------------------------------------------------------
29 
30 CommandObjectScript::CommandObjectScript (ScriptLanguage script_lang) :
31     CommandObject ("script",
32                    "Passes an expression to the script interpreter for evaluation and returns the results. Drops user into the interactive interpreter if no expressions are given.",
33                    "script [<script-expressions-for-evaluation>]"),
34     m_script_lang (script_lang),
35     m_interpreter_ap ()
36 {
37 }
38 
39 CommandObjectScript::~CommandObjectScript ()
40 {
41 }
42 
43 bool
44 CommandObjectScript::ExecuteRawCommandString
45 (
46     const char *command,
47     CommandContext *context,
48     CommandInterpreter *interpreter,
49     CommandReturnObject &result
50 )
51 {
52     std::string arg_str (command);
53 
54     ScriptInterpreter *script_interpreter = GetInterpreter ();
55 
56     if (script_interpreter == NULL)
57     {
58         result.AppendError("no script interpeter");
59         result.SetStatus (eReturnStatusFailed);
60     }
61 
62     FILE *out_fh = Debugger::GetSharedInstance().GetOutputFileHandle();
63     FILE *err_fh = Debugger::GetSharedInstance().GetOutputFileHandle();
64     if (out_fh && err_fh)
65     {
66         if (arg_str.empty())
67             script_interpreter->ExecuteInterpreterLoop (out_fh, err_fh);
68         else
69             script_interpreter->ExecuteOneLine (arg_str, out_fh, err_fh);
70         result.SetStatus (eReturnStatusSuccessFinishNoResult);
71     }
72     else
73     {
74         if (out_fh == NULL)
75             result.AppendError("invalid output file handle");
76         else
77             result.AppendError("invalid error file handle");
78     }
79     return result.Succeeded();
80 }
81 
82 bool
83 CommandObjectScript::WantsRawCommandString()
84 {
85     return true;
86 }
87 
88 bool
89 CommandObjectScript::Execute
90 (
91     Args& command,
92     CommandContext *context,
93     CommandInterpreter *interpreter,
94     CommandReturnObject &result
95 )
96 {
97     std::string arg_str;
98     ScriptInterpreter *script_interpreter = GetInterpreter ();
99 
100     if (script_interpreter == NULL)
101     {
102         result.AppendError("no script interpeter");
103         result.SetStatus (eReturnStatusFailed);
104     }
105 
106     const int argc = command.GetArgumentCount();
107     for (int i = 0; i < argc; ++i)
108         arg_str.append(command.GetArgumentAtIndex(i));
109 
110 
111     FILE *out_fh = Debugger::GetSharedInstance().GetOutputFileHandle();
112     FILE *err_fh = Debugger::GetSharedInstance().GetOutputFileHandle();
113     if (out_fh && err_fh)
114     {
115         if (arg_str.empty())
116             script_interpreter->ExecuteInterpreterLoop (out_fh, err_fh);
117         else
118             script_interpreter->ExecuteOneLine (arg_str, out_fh, err_fh);
119         result.SetStatus (eReturnStatusSuccessFinishNoResult);
120     }
121     else
122     {
123         if (out_fh == NULL)
124             result.AppendError("invalid output file handle");
125         else
126             result.AppendError("invalid error file handle");
127     }
128     return result.Succeeded();
129 }
130 
131 
132 ScriptInterpreter *
133 CommandObjectScript::GetInterpreter ()
134 {
135     if (m_interpreter_ap.get() == NULL)
136     {
137         switch (m_script_lang)
138         {
139         case eScriptLanguagePython:
140             m_interpreter_ap.reset (new ScriptInterpreterPython ());
141             break;
142 
143         case eScriptLanguageNone:
144             m_interpreter_ap.reset (new ScriptInterpreterNone ());
145             break;
146         }
147     }
148     return m_interpreter_ap.get();
149 }
150